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 |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 26 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
| 27 | // too, why the expression could not be folded. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 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(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 66 | |
| 67 | const Expr *Base = B.get<const Expr*>(); |
| 68 | |
| 69 | // For a materialized temporary, the type of the temporary we materialized |
| 70 | // may not be the type of the expression. |
| 71 | if (const MaterializeTemporaryExpr *MTE = |
| 72 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 73 | SmallVector<const Expr *, 2> CommaLHSs; |
| 74 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 75 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 76 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 77 | Adjustments); |
| 78 | // Keep any cv-qualifiers from the reference if we generated a temporary |
| 79 | // for it. |
| 80 | if (Inner != Temp) |
| 81 | return Inner->getType(); |
| 82 | } |
| 83 | |
| 84 | return Base->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 87 | /// 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] | 88 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 89 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 90 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 91 | APValue::BaseOrMemberType Value; |
| 92 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 93 | return Value; |
| 94 | } |
| 95 | |
| 96 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 97 | /// field declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 98 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 99 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 100 | } |
| 101 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 102 | /// base class declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 103 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 104 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 105 | } |
| 106 | /// Determine whether this LValue path entry for a base class names a virtual |
| 107 | /// base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 108 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 109 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 112 | /// Find the path length and type of the most-derived subobject in the given |
| 113 | /// path, and find the size of the containing array, if any. |
| 114 | static |
| 115 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 116 | ArrayRef<APValue::LValuePathEntry> Path, |
| 117 | uint64_t &ArraySize, QualType &Type) { |
| 118 | unsigned MostDerivedLength = 0; |
| 119 | Type = Base; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 120 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 121 | if (Type->isArrayType()) { |
| 122 | const ConstantArrayType *CAT = |
| 123 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 124 | Type = CAT->getElementType(); |
| 125 | ArraySize = CAT->getSize().getZExtValue(); |
| 126 | MostDerivedLength = I + 1; |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 127 | } else if (Type->isAnyComplexType()) { |
| 128 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 129 | Type = CT->getElementType(); |
| 130 | ArraySize = 2; |
| 131 | MostDerivedLength = I + 1; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 132 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 133 | Type = FD->getType(); |
| 134 | ArraySize = 0; |
| 135 | MostDerivedLength = I + 1; |
| 136 | } else { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 137 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 138 | ArraySize = 0; |
| 139 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 140 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 141 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 144 | // The order of this enum is important for diagnostics. |
| 145 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 146 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 147 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 150 | /// A path from a glvalue to a subobject of that glvalue. |
| 151 | struct SubobjectDesignator { |
| 152 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 153 | /// lvalues can still be folded, but they are not core constant expressions |
| 154 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 155 | bool Invalid : 1; |
| 156 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 157 | /// Is this a pointer one past the end of an object? |
| 158 | bool IsOnePastTheEnd : 1; |
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 | /// The length of the path to the most-derived object of which this is a |
| 161 | /// subobject. |
| 162 | unsigned MostDerivedPathLength : 30; |
| 163 | |
| 164 | /// The size of the array of which the most-derived object is an element, or |
| 165 | /// 0 if the most-derived object is not an array element. |
| 166 | uint64_t MostDerivedArraySize; |
| 167 | |
| 168 | /// The type of the most derived object referred to by this address. |
| 169 | QualType MostDerivedType; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 170 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | typedef APValue::LValuePathEntry PathEntry; |
| 172 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 173 | /// The entries on the path from the glvalue to the designated subobject. |
| 174 | SmallVector<PathEntry, 8> Entries; |
| 175 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 176 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 177 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 178 | explicit SubobjectDesignator(QualType T) |
| 179 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 180 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 181 | |
| 182 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 183 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 184 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 185 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 186 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 187 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 188 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 189 | if (V.getLValueBase()) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 190 | MostDerivedPathLength = |
| 191 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 192 | V.getLValuePath(), MostDerivedArraySize, |
| 193 | MostDerivedType); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 197 | void setInvalid() { |
| 198 | Invalid = true; |
| 199 | Entries.clear(); |
| 200 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 201 | |
| 202 | /// Determine whether this is a one-past-the-end pointer. |
| 203 | bool isOnePastTheEnd() const { |
| 204 | if (IsOnePastTheEnd) |
| 205 | return true; |
| 206 | if (MostDerivedArraySize && |
| 207 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 208 | return true; |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | /// Check that this refers to a valid subobject. |
| 213 | bool isValidSubobject() const { |
| 214 | if (Invalid) |
| 215 | return false; |
| 216 | return !isOnePastTheEnd(); |
| 217 | } |
| 218 | /// Check that this refers to a valid subobject, and if not, produce a |
| 219 | /// relevant diagnostic and set the designator as invalid. |
| 220 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 221 | |
| 222 | /// Update this designator to refer to the first element within this array. |
| 223 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 224 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 225 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 226 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 227 | |
| 228 | // This is a most-derived object. |
| 229 | MostDerivedType = CAT->getElementType(); |
| 230 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 231 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 232 | } |
| 233 | /// Update this designator to refer to the given base or member of this |
| 234 | /// object. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 235 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 236 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 237 | APValue::BaseOrMemberType Value(D, Virtual); |
| 238 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 239 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 240 | |
| 241 | // If this isn't a base class, it's a new most-derived object. |
| 242 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 243 | MostDerivedType = FD->getType(); |
| 244 | MostDerivedArraySize = 0; |
| 245 | MostDerivedPathLength = Entries.size(); |
| 246 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 247 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 248 | /// Update this designator to refer to the given complex component. |
| 249 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 250 | PathEntry Entry; |
| 251 | Entry.ArrayIndex = Imag; |
| 252 | Entries.push_back(Entry); |
| 253 | |
| 254 | // This is technically a most-derived object, though in practice this |
| 255 | // is unlikely to matter. |
| 256 | MostDerivedType = EltTy; |
| 257 | MostDerivedArraySize = 2; |
| 258 | MostDerivedPathLength = Entries.size(); |
| 259 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 260 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 261 | /// Add N to the address of this subobject. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 262 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 263 | if (Invalid) return; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 264 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 265 | Entries.back().ArrayIndex += N; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 266 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 267 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 268 | setInvalid(); |
| 269 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 272 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 273 | // nonarray object behaves the same as a pointer to the first element of |
| 274 | // an array of length one with the type of the object as its element type. |
| 275 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 276 | IsOnePastTheEnd = false; |
| 277 | else if (!IsOnePastTheEnd && N == 1) |
| 278 | IsOnePastTheEnd = true; |
| 279 | else if (N != 0) { |
| 280 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 281 | setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 282 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 286 | /// A stack frame in the constexpr call stack. |
| 287 | struct CallStackFrame { |
| 288 | EvalInfo &Info; |
| 289 | |
| 290 | /// Parent - The caller of this stack frame. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 291 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 292 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 293 | /// CallLoc - The location of the call expression for this call. |
| 294 | SourceLocation CallLoc; |
| 295 | |
| 296 | /// Callee - The function which was called. |
| 297 | const FunctionDecl *Callee; |
| 298 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 299 | /// Index - The call index of this call. |
| 300 | unsigned Index; |
| 301 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 302 | /// This - The binding for the this pointer in this call, if any. |
| 303 | const LValue *This; |
| 304 | |
Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 305 | /// Arguments - Parameter bindings for this function call, indexed by |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 306 | /// parameters' function scope indices. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 307 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 308 | |
Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 309 | // Note that we intentionally use std::map here so that references to |
| 310 | // values are stable. |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 311 | typedef std::map<const void*, APValue> MapTy; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 312 | typedef MapTy::const_iterator temp_iterator; |
| 313 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 314 | MapTy Temporaries; |
| 315 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 316 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 317 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 318 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 319 | ~CallStackFrame(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 320 | |
| 321 | APValue *getTemporary(const void *Key) { |
| 322 | MapTy::iterator I = Temporaries.find(Key); |
| 323 | return I == Temporaries.end() ? 0 : &I->second; |
| 324 | } |
| 325 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 328 | /// Temporarily override 'this'. |
| 329 | class ThisOverrideRAII { |
| 330 | public: |
| 331 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 332 | : Frame(Frame), OldThis(Frame.This) { |
| 333 | if (Enable) |
| 334 | Frame.This = NewThis; |
| 335 | } |
| 336 | ~ThisOverrideRAII() { |
| 337 | Frame.This = OldThis; |
| 338 | } |
| 339 | private: |
| 340 | CallStackFrame &Frame; |
| 341 | const LValue *OldThis; |
| 342 | }; |
| 343 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 344 | /// A partial diagnostic which we might know in advance that we are not going |
| 345 | /// to emit. |
| 346 | class OptionalDiagnostic { |
| 347 | PartialDiagnostic *Diag; |
| 348 | |
| 349 | public: |
| 350 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 351 | |
| 352 | template<typename T> |
| 353 | OptionalDiagnostic &operator<<(const T &v) { |
| 354 | if (Diag) |
| 355 | *Diag << v; |
| 356 | return *this; |
| 357 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 358 | |
| 359 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 360 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 361 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 362 | I.toString(Buffer); |
| 363 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 364 | } |
| 365 | return *this; |
| 366 | } |
| 367 | |
| 368 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 369 | if (Diag) { |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 370 | // FIXME: Force the precision of the source value down so we don't |
| 371 | // print digits which are usually useless (we don't really care here if |
| 372 | // we truncate a digit by accident in edge cases). Ideally, |
| 373 | // APFloat::toString would automatically print the shortest |
| 374 | // representation which rounds to the correct value, but it's a bit |
| 375 | // tricky to implement. |
| 376 | unsigned precision = |
| 377 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
| 378 | precision = (precision * 59 + 195) / 196; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 379 | SmallVector<char, 32> Buffer; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 380 | F.toString(Buffer, precision); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 381 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 382 | } |
| 383 | return *this; |
| 384 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 387 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 388 | class Cleanup { |
| 389 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| 390 | |
| 391 | public: |
| 392 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
| 393 | : Value(Val, IsLifetimeExtended) {} |
| 394 | |
| 395 | bool isLifetimeExtended() const { return Value.getInt(); } |
| 396 | void endLifetime() { |
| 397 | *Value.getPointer() = APValue(); |
| 398 | } |
| 399 | }; |
| 400 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 401 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 402 | /// information about a subexpression as it is folded. It retains information |
| 403 | /// about the AST context, but also maintains information about the folded |
| 404 | /// expression. |
| 405 | /// |
| 406 | /// If an expression could be evaluated, it is still possible it is not a C |
| 407 | /// "integer constant expression" or constant expression. If not, this struct |
| 408 | /// captures information about how and why not. |
| 409 | /// |
| 410 | /// One bit of information passed *into* the request for constant folding |
| 411 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 412 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 413 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 414 | /// certain things in certain situations. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 415 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 416 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 417 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 418 | /// EvalStatus - Contains information about the evaluation. |
| 419 | Expr::EvalStatus &EvalStatus; |
| 420 | |
| 421 | /// CurrentCall - The top of the constexpr call stack. |
| 422 | CallStackFrame *CurrentCall; |
| 423 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 424 | /// CallStackDepth - The number of calls in the call stack right now. |
| 425 | unsigned CallStackDepth; |
| 426 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 427 | /// NextCallIndex - The next call index to assign. |
| 428 | unsigned NextCallIndex; |
| 429 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 430 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 431 | /// to perform. This is essentially a limit for the number of statements |
| 432 | /// we will evaluate. |
| 433 | unsigned StepsLeft; |
| 434 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 435 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 436 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 437 | CallStackFrame BottomFrame; |
| 438 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 439 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 440 | /// evaluation frame. |
| 441 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 442 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 443 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 444 | /// evaluated, if any. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 445 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 446 | |
| 447 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 448 | /// declaration whose initializer is being evaluated, if any. |
| 449 | APValue *EvaluatingDeclValue; |
| 450 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 451 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 452 | /// notes attached to it will also be stored, otherwise they will not be. |
| 453 | bool HasActiveDiagnostic; |
| 454 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 455 | enum EvaluationMode { |
| 456 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 457 | /// is not a constant expression. |
| 458 | EM_ConstantExpression, |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 459 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 460 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 461 | /// construct that we can't evaluate yet (because we don't yet know the |
| 462 | /// value of something) but stop if we hit something that could never be |
| 463 | /// a constant expression. |
| 464 | EM_PotentialConstantExpression, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 465 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 466 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 467 | /// we can't model. |
| 468 | EM_ConstantFold, |
| 469 | |
| 470 | /// Evaluate the expression looking for integer overflow and similar |
| 471 | /// issues. Don't worry about side-effects, and try to visit all |
| 472 | /// subexpressions. |
| 473 | EM_EvaluateForOverflow, |
| 474 | |
| 475 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 476 | /// can't be modeled. |
| 477 | EM_IgnoreSideEffects |
| 478 | } EvalMode; |
| 479 | |
| 480 | /// Are we checking whether the expression is a potential constant |
| 481 | /// expression? |
| 482 | bool checkingPotentialConstantExpression() const { |
| 483 | return EvalMode == EM_PotentialConstantExpression; |
| 484 | } |
| 485 | |
| 486 | /// Are we checking an expression for overflow? |
| 487 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 488 | // in such constructs, not just overflow. |
| 489 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
| 490 | |
| 491 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 492 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 493 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 494 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 495 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 496 | EvaluatingDecl((const ValueDecl*)0), EvaluatingDeclValue(0), |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 497 | HasActiveDiagnostic(false), EvalMode(Mode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 498 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 499 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 500 | EvaluatingDecl = Base; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 501 | EvaluatingDeclValue = &Value; |
| 502 | } |
| 503 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 504 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 505 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 506 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 507 | // Don't perform any constexpr calls (other than the call we're checking) |
| 508 | // when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 509 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 510 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 511 | if (NextCallIndex == 0) { |
| 512 | // NextCallIndex has wrapped around. |
| 513 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 514 | return false; |
| 515 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 516 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 517 | return true; |
| 518 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 519 | << getLangOpts().ConstexprCallDepth; |
| 520 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 521 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 522 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 523 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 524 | assert(CallIndex && "no call index in getCallFrame"); |
| 525 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 526 | // be null in this loop. |
| 527 | CallStackFrame *Frame = CurrentCall; |
| 528 | while (Frame->Index > CallIndex) |
| 529 | Frame = Frame->Caller; |
| 530 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 531 | } |
| 532 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 533 | bool nextStep(const Stmt *S) { |
| 534 | if (!StepsLeft) { |
| 535 | Diag(S->getLocStart(), diag::note_constexpr_step_limit_exceeded); |
| 536 | return false; |
| 537 | } |
| 538 | --StepsLeft; |
| 539 | return true; |
| 540 | } |
| 541 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 542 | private: |
| 543 | /// Add a diagnostic to the diagnostics list. |
| 544 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 545 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 546 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 547 | return EvalStatus.Diag->back().second; |
| 548 | } |
| 549 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 550 | /// Add notes containing a call stack to the current point of evaluation. |
| 551 | void addCallStack(unsigned Limit); |
| 552 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 553 | public: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 554 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 555 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 556 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 557 | unsigned ExtraNotes = 0) { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 558 | if (EvalStatus.Diag) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 559 | // If we have a prior diagnostic, it will be noting that the expression |
| 560 | // isn't a constant expression. This diagnostic is more important, |
| 561 | // unless we require this evaluation to produce a constant expression. |
| 562 | // |
| 563 | // FIXME: We might want to show both diagnostics to the user in |
| 564 | // EM_ConstantFold mode. |
| 565 | if (!EvalStatus.Diag->empty()) { |
| 566 | switch (EvalMode) { |
| 567 | case EM_ConstantExpression: |
| 568 | case EM_PotentialConstantExpression: |
| 569 | case EM_EvaluateForOverflow: |
| 570 | HasActiveDiagnostic = false; |
| 571 | return OptionalDiagnostic(); |
| 572 | |
| 573 | case EM_ConstantFold: |
| 574 | case EM_IgnoreSideEffects: |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 579 | unsigned CallStackNotes = CallStackDepth - 1; |
| 580 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 581 | if (Limit) |
| 582 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 583 | if (checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 584 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 585 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 586 | HasActiveDiagnostic = true; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 587 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 588 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 589 | addDiag(Loc, DiagId); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 590 | if (!checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 591 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 592 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 593 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 594 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 595 | return OptionalDiagnostic(); |
| 596 | } |
| 597 | |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 598 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 599 | = diag::note_invalid_subexpr_in_const_expr, |
| 600 | unsigned ExtraNotes = 0) { |
| 601 | if (EvalStatus.Diag) |
| 602 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 603 | HasActiveDiagnostic = false; |
| 604 | return OptionalDiagnostic(); |
| 605 | } |
| 606 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 607 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 608 | /// expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 609 | /// |
| 610 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 611 | /// EM_PotentialConstantExpression mode and we produce one of these. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 612 | template<typename LocArg> |
| 613 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 614 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 615 | unsigned ExtraNotes = 0) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 616 | // Don't override a previous diagnostic. Don't bother collecting |
| 617 | // diagnostics if we're evaluating for overflow. |
| 618 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty() || |
| 619 | EvalMode == EM_EvaluateForOverflow) { |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 620 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 621 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 622 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 623 | return Diag(Loc, DiagId, ExtraNotes); |
| 624 | } |
| 625 | |
| 626 | /// Add a note to a prior diagnostic. |
| 627 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 628 | if (!HasActiveDiagnostic) |
| 629 | return OptionalDiagnostic(); |
| 630 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 631 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 632 | |
| 633 | /// Add a stack of notes to a prior diagnostic. |
| 634 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 635 | if (HasActiveDiagnostic) { |
| 636 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 637 | Diags.begin(), Diags.end()); |
| 638 | } |
| 639 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 640 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 641 | /// Should we continue evaluation after encountering a side-effect that we |
| 642 | /// couldn't model? |
| 643 | bool keepEvaluatingAfterSideEffect() { |
| 644 | switch (EvalMode) { |
| 645 | case EM_EvaluateForOverflow: |
| 646 | case EM_IgnoreSideEffects: |
| 647 | return true; |
| 648 | |
| 649 | case EM_PotentialConstantExpression: |
| 650 | case EM_ConstantExpression: |
| 651 | case EM_ConstantFold: |
| 652 | return false; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /// Note that we have had a side-effect, and determine whether we should |
| 657 | /// keep evaluating. |
| 658 | bool noteSideEffect() { |
| 659 | EvalStatus.HasSideEffects = true; |
| 660 | return keepEvaluatingAfterSideEffect(); |
| 661 | } |
| 662 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 663 | /// Should we continue evaluation as much as possible after encountering a |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 664 | /// construct which can't be reduced to a value? |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 665 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 666 | if (!StepsLeft) |
| 667 | return false; |
| 668 | |
| 669 | switch (EvalMode) { |
| 670 | case EM_PotentialConstantExpression: |
| 671 | case EM_EvaluateForOverflow: |
| 672 | return true; |
| 673 | |
| 674 | case EM_ConstantExpression: |
| 675 | case EM_ConstantFold: |
| 676 | case EM_IgnoreSideEffects: |
| 677 | return false; |
| 678 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 679 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 680 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 681 | |
| 682 | /// Object used to treat all foldable expressions as constant expressions. |
| 683 | struct FoldConstant { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 684 | EvalInfo &Info; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 685 | bool Enabled; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 686 | bool HadNoPriorDiags; |
| 687 | EvalInfo::EvaluationMode OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 688 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 689 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 690 | : Info(Info), |
| 691 | Enabled(Enabled), |
| 692 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 693 | Info.EvalStatus.Diag->empty() && |
| 694 | !Info.EvalStatus.HasSideEffects), |
| 695 | OldMode(Info.EvalMode) { |
| 696 | if (Enabled && Info.EvalMode == EvalInfo::EM_ConstantExpression) |
| 697 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 698 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 699 | void keepDiagnostics() { Enabled = false; } |
| 700 | ~FoldConstant() { |
| 701 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 702 | !Info.EvalStatus.HasSideEffects) |
| 703 | Info.EvalStatus.Diag->clear(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 704 | Info.EvalMode = OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 705 | } |
| 706 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 707 | |
| 708 | /// RAII object used to suppress diagnostics and side-effects from a |
| 709 | /// speculative evaluation. |
| 710 | class SpeculativeEvaluationRAII { |
| 711 | EvalInfo &Info; |
| 712 | Expr::EvalStatus Old; |
| 713 | |
| 714 | public: |
| 715 | SpeculativeEvaluationRAII(EvalInfo &Info, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 716 | SmallVectorImpl<PartialDiagnosticAt> *NewDiag = 0) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 717 | : Info(Info), Old(Info.EvalStatus) { |
| 718 | Info.EvalStatus.Diag = NewDiag; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 719 | // If we're speculatively evaluating, we may have skipped over some |
| 720 | // evaluations and missed out a side effect. |
| 721 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 722 | } |
| 723 | ~SpeculativeEvaluationRAII() { |
| 724 | Info.EvalStatus = Old; |
| 725 | } |
| 726 | }; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 727 | |
| 728 | /// RAII object wrapping a full-expression or block scope, and handling |
| 729 | /// the ending of the lifetime of temporaries created within it. |
| 730 | template<bool IsFullExpression> |
| 731 | class ScopeRAII { |
| 732 | EvalInfo &Info; |
| 733 | unsigned OldStackSize; |
| 734 | public: |
| 735 | ScopeRAII(EvalInfo &Info) |
| 736 | : Info(Info), OldStackSize(Info.CleanupStack.size()) {} |
| 737 | ~ScopeRAII() { |
| 738 | // Body moved to a static method to encourage the compiler to inline away |
| 739 | // instances of this class. |
| 740 | cleanup(Info, OldStackSize); |
| 741 | } |
| 742 | private: |
| 743 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
| 744 | unsigned NewEnd = OldStackSize; |
| 745 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
| 746 | I != N; ++I) { |
| 747 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
| 748 | // Full-expression cleanup of a lifetime-extended temporary: nothing |
| 749 | // to do, just move this cleanup to the right place in the stack. |
| 750 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
| 751 | ++NewEnd; |
| 752 | } else { |
| 753 | // End the lifetime of the object. |
| 754 | Info.CleanupStack[I].endLifetime(); |
| 755 | } |
| 756 | } |
| 757 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
| 758 | Info.CleanupStack.end()); |
| 759 | } |
| 760 | }; |
| 761 | typedef ScopeRAII<false> BlockScopeRAII; |
| 762 | typedef ScopeRAII<true> FullExpressionRAII; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 763 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 764 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 765 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 766 | CheckSubobjectKind CSK) { |
| 767 | if (Invalid) |
| 768 | return false; |
| 769 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 770 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 771 | << CSK; |
| 772 | setInvalid(); |
| 773 | return false; |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 779 | const Expr *E, uint64_t N) { |
| 780 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 781 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 782 | << static_cast<int>(N) << /*array*/ 0 |
| 783 | << static_cast<unsigned>(MostDerivedArraySize); |
| 784 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 785 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 786 | << static_cast<int>(N) << /*non-array*/ 1; |
| 787 | setInvalid(); |
| 788 | } |
| 789 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 790 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 791 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 792 | APValue *Arguments) |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 793 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 794 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 795 | Info.CurrentCall = this; |
| 796 | ++Info.CallStackDepth; |
| 797 | } |
| 798 | |
| 799 | CallStackFrame::~CallStackFrame() { |
| 800 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 801 | --Info.CallStackDepth; |
| 802 | Info.CurrentCall = Caller; |
| 803 | } |
| 804 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 805 | APValue &CallStackFrame::createTemporary(const void *Key, |
| 806 | bool IsLifetimeExtended) { |
| 807 | APValue &Result = Temporaries[Key]; |
| 808 | assert(Result.isUninit() && "temporary created multiple times"); |
| 809 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
| 810 | return Result; |
| 811 | } |
| 812 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 813 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 814 | |
| 815 | void EvalInfo::addCallStack(unsigned Limit) { |
| 816 | // Determine which calls to skip, if any. |
| 817 | unsigned ActiveCalls = CallStackDepth - 1; |
| 818 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 819 | if (Limit && Limit < ActiveCalls) { |
| 820 | SkipStart = Limit / 2 + Limit % 2; |
| 821 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 824 | // Walk the call stack and add the diagnostics. |
| 825 | unsigned CallIdx = 0; |
| 826 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 827 | Frame = Frame->Caller, ++CallIdx) { |
| 828 | // Skip this call? |
| 829 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 830 | if (CallIdx == SkipStart) { |
| 831 | // Note that we're skipping calls. |
| 832 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 833 | << unsigned(ActiveCalls - Limit); |
| 834 | } |
| 835 | continue; |
| 836 | } |
| 837 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 838 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 839 | llvm::raw_svector_ostream Out(Buffer); |
| 840 | describeCall(Frame, Out); |
| 841 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 846 | struct ComplexValue { |
| 847 | private: |
| 848 | bool IsInt; |
| 849 | |
| 850 | public: |
| 851 | APSInt IntReal, IntImag; |
| 852 | APFloat FloatReal, FloatImag; |
| 853 | |
| 854 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 855 | |
| 856 | void makeComplexFloat() { IsInt = false; } |
| 857 | bool isComplexFloat() const { return !IsInt; } |
| 858 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 859 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 860 | |
| 861 | void makeComplexInt() { IsInt = true; } |
| 862 | bool isComplexInt() const { return IsInt; } |
| 863 | APSInt &getComplexIntReal() { return IntReal; } |
| 864 | APSInt &getComplexIntImag() { return IntImag; } |
| 865 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 866 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 867 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 868 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 869 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 870 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 871 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 872 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 873 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 874 | if (v.isComplexFloat()) { |
| 875 | makeComplexFloat(); |
| 876 | FloatReal = v.getComplexFloatReal(); |
| 877 | FloatImag = v.getComplexFloatImag(); |
| 878 | } else { |
| 879 | makeComplexInt(); |
| 880 | IntReal = v.getComplexIntReal(); |
| 881 | IntImag = v.getComplexIntImag(); |
| 882 | } |
| 883 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 884 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 885 | |
| 886 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 887 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 888 | CharUnits Offset; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 889 | unsigned CallIndex; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 890 | SubobjectDesignator Designator; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 891 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 892 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 893 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 894 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 895 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 896 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 897 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 898 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 899 | void moveInto(APValue &V) const { |
| 900 | if (Designator.Invalid) |
| 901 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 902 | else |
| 903 | V = APValue(Base, Offset, Designator.Entries, |
| 904 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 905 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 906 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 907 | assert(V.isLValue()); |
| 908 | Base = V.getLValueBase(); |
| 909 | Offset = V.getLValueOffset(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 910 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 911 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 914 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 915 | Base = B; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 916 | Offset = CharUnits::Zero(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 917 | CallIndex = I; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 918 | Designator = SubobjectDesignator(getType(B)); |
| 919 | } |
| 920 | |
| 921 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 922 | // a diagnostic and mark the designator as invalid. |
| 923 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 924 | CheckSubobjectKind CSK) { |
| 925 | if (Designator.Invalid) |
| 926 | return false; |
| 927 | if (!Base) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 928 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 929 | << CSK; |
| 930 | Designator.setInvalid(); |
| 931 | return false; |
| 932 | } |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | // Check this LValue refers to an object. If not, set the designator to be |
| 937 | // invalid and emit a diagnostic. |
| 938 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 939 | // Outside C++11, do not build a designator referring to a subobject of |
| 940 | // any object: we won't use such a designator for anything. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 941 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 942 | Designator.setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 943 | return checkNullPointer(Info, E, CSK) && |
| 944 | Designator.checkSubobject(Info, E, CSK); |
| 945 | } |
| 946 | |
| 947 | void addDecl(EvalInfo &Info, const Expr *E, |
| 948 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 949 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 950 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 951 | } |
| 952 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 953 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 954 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 955 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 956 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 957 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 958 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 959 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 960 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 961 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 962 | Designator.adjustIndex(Info, E, N); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 963 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 964 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 965 | |
| 966 | struct MemberPtr { |
| 967 | MemberPtr() {} |
| 968 | explicit MemberPtr(const ValueDecl *Decl) : |
| 969 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 970 | |
| 971 | /// The member or (direct or indirect) field referred to by this member |
| 972 | /// pointer, or 0 if this is a null member pointer. |
| 973 | const ValueDecl *getDecl() const { |
| 974 | return DeclAndIsDerivedMember.getPointer(); |
| 975 | } |
| 976 | /// Is this actually a member of some type derived from the relevant class? |
| 977 | bool isDerivedMember() const { |
| 978 | return DeclAndIsDerivedMember.getInt(); |
| 979 | } |
| 980 | /// Get the class which the declaration actually lives in. |
| 981 | const CXXRecordDecl *getContainingRecord() const { |
| 982 | return cast<CXXRecordDecl>( |
| 983 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 984 | } |
| 985 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 986 | void moveInto(APValue &V) const { |
| 987 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 988 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 989 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 990 | assert(V.isMemberPointer()); |
| 991 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 992 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 993 | Path.clear(); |
| 994 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 995 | Path.insert(Path.end(), P.begin(), P.end()); |
| 996 | } |
| 997 | |
| 998 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 999 | /// whether the member is a member of some class derived from the class type |
| 1000 | /// of the member pointer. |
| 1001 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1002 | /// Path - The path of base/derived classes from the member declaration's |
| 1003 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1004 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1005 | |
| 1006 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1007 | /// hierarchy). |
| 1008 | bool castBack(const CXXRecordDecl *Class) { |
| 1009 | assert(!Path.empty()); |
| 1010 | const CXXRecordDecl *Expected; |
| 1011 | if (Path.size() >= 2) |
| 1012 | Expected = Path[Path.size() - 2]; |
| 1013 | else |
| 1014 | Expected = getContainingRecord(); |
| 1015 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1016 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1017 | // if B does not contain the original member and is not a base or |
| 1018 | // derived class of the class containing the original member, the result |
| 1019 | // of the cast is undefined. |
| 1020 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1021 | // (D::*). We consider that to be a language defect. |
| 1022 | return false; |
| 1023 | } |
| 1024 | Path.pop_back(); |
| 1025 | return true; |
| 1026 | } |
| 1027 | /// Perform a base-to-derived member pointer cast. |
| 1028 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1029 | if (!getDecl()) |
| 1030 | return true; |
| 1031 | if (!isDerivedMember()) { |
| 1032 | Path.push_back(Derived); |
| 1033 | return true; |
| 1034 | } |
| 1035 | if (!castBack(Derived)) |
| 1036 | return false; |
| 1037 | if (Path.empty()) |
| 1038 | DeclAndIsDerivedMember.setInt(false); |
| 1039 | return true; |
| 1040 | } |
| 1041 | /// Perform a derived-to-base member pointer cast. |
| 1042 | bool castToBase(const CXXRecordDecl *Base) { |
| 1043 | if (!getDecl()) |
| 1044 | return true; |
| 1045 | if (Path.empty()) |
| 1046 | DeclAndIsDerivedMember.setInt(true); |
| 1047 | if (isDerivedMember()) { |
| 1048 | Path.push_back(Base); |
| 1049 | return true; |
| 1050 | } |
| 1051 | return castBack(Base); |
| 1052 | } |
| 1053 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1054 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1055 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1056 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1057 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1058 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1059 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1060 | return false; |
| 1061 | return LHS.Path == RHS.Path; |
| 1062 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1063 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1064 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1065 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1066 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1067 | const LValue &This, const Expr *E, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1068 | bool AllowNonLiteralTypes = false); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1069 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 1070 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1071 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1072 | EvalInfo &Info); |
| 1073 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1074 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1075 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1076 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1077 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1078 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 1079 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1080 | |
| 1081 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1082 | // Misc utilities |
| 1083 | //===----------------------------------------------------------------------===// |
| 1084 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1085 | /// Produce a string describing the given constexpr call. |
| 1086 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 1087 | unsigned ArgIndex = 0; |
| 1088 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 1089 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 1090 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 1091 | |
| 1092 | if (!IsMemberCall) |
| 1093 | Out << *Frame->Callee << '('; |
| 1094 | |
| 1095 | if (Frame->This && IsMemberCall) { |
| 1096 | APValue Val; |
| 1097 | Frame->This->moveInto(Val); |
| 1098 | Val.printPretty(Out, Frame->Info.Ctx, |
| 1099 | Frame->This->Designator.MostDerivedType); |
| 1100 | // FIXME: Add parens around Val if needed. |
| 1101 | Out << "->" << *Frame->Callee << '('; |
| 1102 | IsMemberCall = false; |
| 1103 | } |
| 1104 | |
| 1105 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 1106 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 1107 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1108 | Out << ", "; |
| 1109 | |
| 1110 | const ParmVarDecl *Param = *I; |
| 1111 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 1112 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 1113 | |
| 1114 | if (ArgIndex == 0 && IsMemberCall) |
| 1115 | Out << "->" << *Frame->Callee << '('; |
| 1116 | } |
| 1117 | |
| 1118 | Out << ')'; |
| 1119 | } |
| 1120 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1121 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1122 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1123 | /// \return \c true if the caller should keep evaluating. |
| 1124 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1125 | APValue Scratch; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1126 | if (!Evaluate(Scratch, Info, E)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1127 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1128 | return Info.keepEvaluatingAfterFailure(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 1129 | // FIXME: return Info.noteSideEffect(); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1130 | } |
| 1131 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1134 | /// Sign- or zero-extend a value to 64 bits. If it's already 64 bits, just |
| 1135 | /// return its existing value. |
| 1136 | static int64_t getExtValue(const APSInt &Value) { |
| 1137 | return Value.isSigned() ? Value.getSExtValue() |
| 1138 | : static_cast<int64_t>(Value.getZExtValue()); |
| 1139 | } |
| 1140 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1141 | /// Should this call expression be treated as a string literal? |
| 1142 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 1143 | unsigned Builtin = E->isBuiltinCall(); |
| 1144 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1145 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1146 | } |
| 1147 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1148 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1149 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1150 | // constant expression of pointer type that evaluates to... |
| 1151 | |
| 1152 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1153 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1154 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1155 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1156 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1157 | // ... the address of an object with static storage duration, |
| 1158 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1159 | return VD->hasGlobalStorage(); |
| 1160 | // ... the address of a function, |
| 1161 | return isa<FunctionDecl>(D); |
| 1162 | } |
| 1163 | |
| 1164 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1165 | switch (E->getStmtClass()) { |
| 1166 | default: |
| 1167 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1168 | case Expr::CompoundLiteralExprClass: { |
| 1169 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1170 | return CLE->isFileScope() && CLE->isLValue(); |
| 1171 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1172 | case Expr::MaterializeTemporaryExprClass: |
| 1173 | // A materialized temporary might have been lifetime-extended to static |
| 1174 | // storage duration. |
| 1175 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1176 | // A string literal has static storage duration. |
| 1177 | case Expr::StringLiteralClass: |
| 1178 | case Expr::PredefinedExprClass: |
| 1179 | case Expr::ObjCStringLiteralClass: |
| 1180 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1181 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1182 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1183 | return true; |
| 1184 | case Expr::CallExprClass: |
| 1185 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1186 | // For GCC compatibility, &&label has static storage duration. |
| 1187 | case Expr::AddrLabelExprClass: |
| 1188 | return true; |
| 1189 | // A Block literal expression may be used as the initialization value for |
| 1190 | // Block variables at global or local static scope. |
| 1191 | case Expr::BlockExprClass: |
| 1192 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1193 | case Expr::ImplicitValueInitExprClass: |
| 1194 | // FIXME: |
| 1195 | // We can never form an lvalue with an implicit value initialization as its |
| 1196 | // base through expression evaluation, so these only appear in one case: the |
| 1197 | // implicit variable declaration we invent when checking whether a constexpr |
| 1198 | // constructor can produce a constant expression. We must assume that such |
| 1199 | // an expression might be a global lvalue. |
| 1200 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1201 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1204 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1205 | assert(Base && "no location for a null lvalue"); |
| 1206 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1207 | if (VD) |
| 1208 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1209 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1210 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1211 | diag::note_constexpr_temporary_here); |
| 1212 | } |
| 1213 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1214 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1215 | /// value for an address or reference constant expression. Return true if we |
| 1216 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1217 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1218 | QualType Type, const LValue &LVal) { |
| 1219 | bool IsReferenceType = Type->isReferenceType(); |
| 1220 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1221 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1222 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1223 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1224 | // Check that the object is a global. Note that the fake 'this' object we |
| 1225 | // manufacture when checking potential constant expressions is conservatively |
| 1226 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1227 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1228 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1229 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1230 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1231 | << IsReferenceType << !Designator.Entries.empty() |
| 1232 | << !!VD << VD; |
| 1233 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1234 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1235 | Info.Diag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1236 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1237 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1238 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1239 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 1240 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1241 | LVal.getLValueCallIndex() == 0) && |
| 1242 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1243 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1244 | // Check if this is a thread-local variable. |
| 1245 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1246 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1247 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1252 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1253 | // an extension: the standard requires them to point to an object. |
| 1254 | if (!IsReferenceType) |
| 1255 | return true; |
| 1256 | |
| 1257 | // A reference constant expression must refer to an object. |
| 1258 | if (!Base) { |
| 1259 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1260 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1261 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1264 | // Does this refer one past the end of some object? |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1265 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1266 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1267 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1268 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1269 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1272 | return true; |
| 1273 | } |
| 1274 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1275 | /// Check that this core constant expression is of literal type, and if not, |
| 1276 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1277 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
| 1278 | const LValue *This = 0) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1279 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1280 | return true; |
| 1281 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1282 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1283 | // constexpr constructors for o and its subobjects even if those objects |
| 1284 | // are of non-literal class types. |
| 1285 | if (Info.getLangOpts().CPlusPlus1y && This && |
Richard Smith | 37dc92e | 2013-05-16 05:04:51 +0000 | [diff] [blame] | 1286 | Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1287 | return true; |
| 1288 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1289 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1290 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1291 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1292 | << E->getType(); |
| 1293 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1294 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1298 | /// 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] | 1299 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1300 | /// check that the expression is of literal type. |
| 1301 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1302 | QualType Type, const APValue &Value) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1303 | if (Value.isUninit()) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1304 | Info.Diag(DiagLoc, diag::note_constexpr_uninitialized) |
| 1305 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1306 | return false; |
| 1307 | } |
| 1308 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1309 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1310 | // each subobject of its value shall have been initialized by a constant |
| 1311 | // expression. |
| 1312 | if (Value.isArray()) { |
| 1313 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1314 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1315 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1316 | Value.getArrayInitializedElt(I))) |
| 1317 | return false; |
| 1318 | } |
| 1319 | if (!Value.hasArrayFiller()) |
| 1320 | return true; |
| 1321 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1322 | Value.getArrayFiller()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1323 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1324 | if (Value.isUnion() && Value.getUnionField()) { |
| 1325 | return CheckConstantExpression(Info, DiagLoc, |
| 1326 | Value.getUnionField()->getType(), |
| 1327 | Value.getUnionValue()); |
| 1328 | } |
| 1329 | if (Value.isStruct()) { |
| 1330 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1331 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1332 | unsigned BaseIndex = 0; |
| 1333 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1334 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1335 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1336 | Value.getStructBase(BaseIndex))) |
| 1337 | return false; |
| 1338 | } |
| 1339 | } |
| 1340 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1341 | I != E; ++I) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1342 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1343 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1344 | return false; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1349 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1350 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1351 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1352 | } |
| 1353 | |
| 1354 | // Everything else is fine. |
| 1355 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1358 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1359 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1363 | if (Value.CallIndex) |
| 1364 | return false; |
| 1365 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1366 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1369 | static bool IsWeakLValue(const LValue &Value) { |
| 1370 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1371 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1374 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1375 | // A null base expression indicates a null pointer. These are always |
| 1376 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1377 | if (!Value.getLValueBase()) { |
| 1378 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1379 | return true; |
| 1380 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1381 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1382 | // We have a non-null base. These are generally known to be true, but if it's |
| 1383 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1384 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1385 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1386 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1389 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1390 | switch (Val.getKind()) { |
| 1391 | case APValue::Uninitialized: |
| 1392 | return false; |
| 1393 | case APValue::Int: |
| 1394 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1395 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1396 | case APValue::Float: |
| 1397 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1398 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1399 | case APValue::ComplexInt: |
| 1400 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1401 | Val.getComplexIntImag().getBoolValue(); |
| 1402 | return true; |
| 1403 | case APValue::ComplexFloat: |
| 1404 | Result = !Val.getComplexFloatReal().isZero() || |
| 1405 | !Val.getComplexFloatImag().isZero(); |
| 1406 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1407 | case APValue::LValue: |
| 1408 | return EvalPointerValueAsBool(Val, Result); |
| 1409 | case APValue::MemberPointer: |
| 1410 | Result = Val.getMemberPointerDecl(); |
| 1411 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1412 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1413 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1414 | case APValue::Struct: |
| 1415 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1416 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1417 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1420 | llvm_unreachable("unknown APValue kind"); |
| 1421 | } |
| 1422 | |
| 1423 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1424 | EvalInfo &Info) { |
| 1425 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1426 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1427 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1428 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1429 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1432 | template<typename T> |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1433 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1434 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1435 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1436 | << SrcValue << DestType; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1440 | QualType SrcType, const APFloat &Value, |
| 1441 | QualType DestType, APSInt &Result) { |
| 1442 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1443 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1444 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1446 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1447 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1448 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1449 | & APFloat::opInvalidOp) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1450 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1451 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1454 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1455 | QualType SrcType, QualType DestType, |
| 1456 | APFloat &Result) { |
| 1457 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1458 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1459 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1460 | APFloat::rmNearestTiesToEven, &ignored) |
| 1461 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1462 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1463 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1466 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1467 | QualType DestType, QualType SrcType, |
| 1468 | APSInt &Value) { |
| 1469 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1470 | APSInt Result = Value; |
| 1471 | // Figure out if this is a truncate, extend or noop cast. |
| 1472 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1473 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1474 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1475 | return Result; |
| 1476 | } |
| 1477 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1478 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1479 | QualType SrcType, const APSInt &Value, |
| 1480 | QualType DestType, APFloat &Result) { |
| 1481 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1482 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1483 | APFloat::rmNearestTiesToEven) |
| 1484 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1485 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1486 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 1489 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 1490 | APValue &Value, const FieldDecl *FD) { |
| 1491 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 1492 | |
| 1493 | if (!Value.isInt()) { |
| 1494 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 1495 | // FIXME: In this case, we should provide the diagnostic for casting |
| 1496 | // a pointer to an integer. |
| 1497 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
| 1498 | Info.Diag(E); |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
| 1502 | APSInt &Int = Value.getInt(); |
| 1503 | unsigned OldBitWidth = Int.getBitWidth(); |
| 1504 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 1505 | if (NewBitWidth < OldBitWidth) |
| 1506 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 1507 | return true; |
| 1508 | } |
| 1509 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1510 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1511 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1512 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1513 | if (!Evaluate(SVal, Info, E)) |
| 1514 | return false; |
| 1515 | if (SVal.isInt()) { |
| 1516 | Res = SVal.getInt(); |
| 1517 | return true; |
| 1518 | } |
| 1519 | if (SVal.isFloat()) { |
| 1520 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1521 | return true; |
| 1522 | } |
| 1523 | if (SVal.isVector()) { |
| 1524 | QualType VecTy = E->getType(); |
| 1525 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1526 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1527 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1528 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1529 | Res = llvm::APInt::getNullValue(VecSize); |
| 1530 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1531 | APValue &Elt = SVal.getVectorElt(i); |
| 1532 | llvm::APInt EltAsInt; |
| 1533 | if (Elt.isInt()) { |
| 1534 | EltAsInt = Elt.getInt(); |
| 1535 | } else if (Elt.isFloat()) { |
| 1536 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1537 | } else { |
| 1538 | // Don't try to handle vectors of anything other than int or float |
| 1539 | // (not sure if it's possible to hit this case). |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1540 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1541 | return false; |
| 1542 | } |
| 1543 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1544 | if (BigEndian) |
| 1545 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1546 | else |
| 1547 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1548 | } |
| 1549 | return true; |
| 1550 | } |
| 1551 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1552 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1553 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
| 1556 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1557 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 1558 | /// bits, and check for overflow in the original type (if that type was not an |
| 1559 | /// unsigned type). |
| 1560 | template<typename Operation> |
| 1561 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 1562 | const APSInt &LHS, const APSInt &RHS, |
| 1563 | unsigned BitWidth, Operation Op) { |
| 1564 | if (LHS.isUnsigned()) |
| 1565 | return Op(LHS, RHS); |
| 1566 | |
| 1567 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 1568 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 1569 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 1570 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1571 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 1572 | diag::warn_integer_constant_overflow) |
| 1573 | << Result.toString(10) << E->getType(); |
| 1574 | else |
| 1575 | HandleOverflow(Info, E, Value, E->getType()); |
| 1576 | } |
| 1577 | return Result; |
| 1578 | } |
| 1579 | |
| 1580 | /// Perform the given binary integer operation. |
| 1581 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 1582 | BinaryOperatorKind Opcode, APSInt RHS, |
| 1583 | APSInt &Result) { |
| 1584 | switch (Opcode) { |
| 1585 | default: |
| 1586 | Info.Diag(E); |
| 1587 | return false; |
| 1588 | case BO_Mul: |
| 1589 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 1590 | std::multiplies<APSInt>()); |
| 1591 | return true; |
| 1592 | case BO_Add: |
| 1593 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1594 | std::plus<APSInt>()); |
| 1595 | return true; |
| 1596 | case BO_Sub: |
| 1597 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1598 | std::minus<APSInt>()); |
| 1599 | return true; |
| 1600 | case BO_And: Result = LHS & RHS; return true; |
| 1601 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 1602 | case BO_Or: Result = LHS | RHS; return true; |
| 1603 | case BO_Div: |
| 1604 | case BO_Rem: |
| 1605 | if (RHS == 0) { |
| 1606 | Info.Diag(E, diag::note_expr_divide_by_zero); |
| 1607 | return false; |
| 1608 | } |
| 1609 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. |
| 1610 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 1611 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 1612 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 1613 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 1614 | return true; |
| 1615 | case BO_Shl: { |
| 1616 | if (Info.getLangOpts().OpenCL) |
| 1617 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1618 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1619 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1620 | RHS.isUnsigned()); |
| 1621 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1622 | // During constant-folding, a negative shift is an opposite shift. Such |
| 1623 | // a shift is not a constant expression. |
| 1624 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1625 | RHS = -RHS; |
| 1626 | goto shift_right; |
| 1627 | } |
| 1628 | shift_left: |
| 1629 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 1630 | // the shifted type. |
| 1631 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1632 | if (SA != RHS) { |
| 1633 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1634 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1635 | } else if (LHS.isSigned()) { |
| 1636 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 1637 | // operand, and must not overflow the corresponding unsigned type. |
| 1638 | if (LHS.isNegative()) |
| 1639 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 1640 | else if (LHS.countLeadingZeros() < SA) |
| 1641 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 1642 | } |
| 1643 | Result = LHS << SA; |
| 1644 | return true; |
| 1645 | } |
| 1646 | case BO_Shr: { |
| 1647 | if (Info.getLangOpts().OpenCL) |
| 1648 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1649 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1650 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1651 | RHS.isUnsigned()); |
| 1652 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1653 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 1654 | // shift is not a constant expression. |
| 1655 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1656 | RHS = -RHS; |
| 1657 | goto shift_left; |
| 1658 | } |
| 1659 | shift_right: |
| 1660 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 1661 | // shifted type. |
| 1662 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1663 | if (SA != RHS) |
| 1664 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1665 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1666 | Result = LHS >> SA; |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
| 1670 | case BO_LT: Result = LHS < RHS; return true; |
| 1671 | case BO_GT: Result = LHS > RHS; return true; |
| 1672 | case BO_LE: Result = LHS <= RHS; return true; |
| 1673 | case BO_GE: Result = LHS >= RHS; return true; |
| 1674 | case BO_EQ: Result = LHS == RHS; return true; |
| 1675 | case BO_NE: Result = LHS != RHS; return true; |
| 1676 | } |
| 1677 | } |
| 1678 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1679 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 1680 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 1681 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 1682 | const APFloat &RHS) { |
| 1683 | switch (Opcode) { |
| 1684 | default: |
| 1685 | Info.Diag(E); |
| 1686 | return false; |
| 1687 | case BO_Mul: |
| 1688 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1689 | break; |
| 1690 | case BO_Add: |
| 1691 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 1692 | break; |
| 1693 | case BO_Sub: |
| 1694 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1695 | break; |
| 1696 | case BO_Div: |
| 1697 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1698 | break; |
| 1699 | } |
| 1700 | |
| 1701 | if (LHS.isInfinity() || LHS.isNaN()) |
| 1702 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
| 1703 | return true; |
| 1704 | } |
| 1705 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1706 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1707 | /// truncating the lvalue's path to the given length. |
| 1708 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1709 | const RecordDecl *TruncatedType, |
| 1710 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1711 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Check we actually point to a derived class object. |
| 1714 | if (TruncatedElements == D.Entries.size()) |
| 1715 | return true; |
| 1716 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1717 | "not casting to a derived class"); |
| 1718 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1719 | return false; |
| 1720 | |
| 1721 | // 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] | 1722 | const RecordDecl *RD = TruncatedType; |
| 1723 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1724 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1725 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1726 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1727 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1728 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1729 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1730 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1731 | RD = Base; |
| 1732 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1733 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1734 | return true; |
| 1735 | } |
| 1736 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1737 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1738 | const CXXRecordDecl *Derived, |
| 1739 | const CXXRecordDecl *Base, |
| 1740 | const ASTRecordLayout *RL = 0) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1741 | if (!RL) { |
| 1742 | if (Derived->isInvalidDecl()) return false; |
| 1743 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1744 | } |
| 1745 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1746 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1747 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1748 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1751 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1752 | const CXXRecordDecl *DerivedDecl, |
| 1753 | const CXXBaseSpecifier *Base) { |
| 1754 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1755 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1756 | if (!Base->isVirtual()) |
| 1757 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1758 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1759 | SubobjectDesignator &D = Obj.Designator; |
| 1760 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1761 | return false; |
| 1762 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1763 | // Extract most-derived object and corresponding type. |
| 1764 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1765 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1766 | return false; |
| 1767 | |
| 1768 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1769 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1770 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1771 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1772 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1773 | return true; |
| 1774 | } |
| 1775 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1776 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 1777 | QualType Type, LValue &Result) { |
| 1778 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1779 | PathE = E->path_end(); |
| 1780 | PathI != PathE; ++PathI) { |
| 1781 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 1782 | *PathI)) |
| 1783 | return false; |
| 1784 | Type = (*PathI)->getType(); |
| 1785 | } |
| 1786 | return true; |
| 1787 | } |
| 1788 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1789 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1790 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1791 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1792 | const FieldDecl *FD, |
| 1793 | const ASTRecordLayout *RL = 0) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1794 | if (!RL) { |
| 1795 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1796 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1797 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1798 | |
| 1799 | unsigned I = FD->getFieldIndex(); |
| 1800 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1801 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1802 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1805 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1806 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1807 | LValue &LVal, |
| 1808 | const IndirectFieldDecl *IFD) { |
| 1809 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1810 | CE = IFD->chain_end(); C != CE; ++C) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1811 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C))) |
| 1812 | return false; |
| 1813 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1816 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1817 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1818 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1819 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1820 | // extension. |
| 1821 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1822 | Size = CharUnits::One(); |
| 1823 | return true; |
| 1824 | } |
| 1825 | |
| 1826 | if (!Type->isConstantSizeType()) { |
| 1827 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1828 | // FIXME: Better diagnostic. |
| 1829 | Info.Diag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | /// Update a pointer value to model pointer arithmetic. |
| 1838 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1839 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1840 | /// \param LVal - The pointer value to be updated. |
| 1841 | /// \param EltTy - The pointee type represented by LVal. |
| 1842 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1843 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1844 | LValue &LVal, QualType EltTy, |
| 1845 | int64_t Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1846 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1847 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1848 | return false; |
| 1849 | |
| 1850 | // Compute the new offset in the appropriate width. |
| 1851 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1852 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1853 | return true; |
| 1854 | } |
| 1855 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1856 | /// Update an lvalue to refer to a component of a complex number. |
| 1857 | /// \param Info - Information about the ongoing evaluation. |
| 1858 | /// \param LVal - The lvalue to be updated. |
| 1859 | /// \param EltTy - The complex number's component type. |
| 1860 | /// \param Imag - False for the real component, true for the imaginary. |
| 1861 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1862 | LValue &LVal, QualType EltTy, |
| 1863 | bool Imag) { |
| 1864 | if (Imag) { |
| 1865 | CharUnits SizeOfComponent; |
| 1866 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1867 | return false; |
| 1868 | LVal.Offset += SizeOfComponent; |
| 1869 | } |
| 1870 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1871 | return true; |
| 1872 | } |
| 1873 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1874 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1875 | /// |
| 1876 | /// \param Info Information about the ongoing evaluation. |
| 1877 | /// \param E An expression to be used when printing diagnostics. |
| 1878 | /// \param VD The variable whose initializer should be obtained. |
| 1879 | /// \param Frame The frame in which the variable was created. Must be null |
| 1880 | /// if this variable is not local to the evaluation. |
| 1881 | /// \param Result Filled in with a pointer to the value of the variable. |
| 1882 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1883 | const VarDecl *VD, CallStackFrame *Frame, |
| 1884 | APValue *&Result) { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1885 | // If this is a parameter to an active constexpr function call, perform |
| 1886 | // argument substitution. |
| 1887 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1888 | // Assume arguments of a potential constant expression are unknown |
| 1889 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 1890 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1891 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1892 | if (!Frame || !Frame->Arguments) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1893 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1894 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1895 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1896 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1897 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1898 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1899 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1900 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1901 | if (Frame) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1902 | Result = Frame->getTemporary(VD); |
| 1903 | assert(Result && "missing value for local variable"); |
| 1904 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1907 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1908 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1909 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1910 | // If we're checking a potential constant expression, the variable could be |
| 1911 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 1912 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1913 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1914 | return false; |
| 1915 | } |
| 1916 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1917 | // If we're currently evaluating the initializer of this declaration, use that |
| 1918 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1919 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1920 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1921 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1924 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1925 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1926 | if (VD->isWeak()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1927 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1928 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1929 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1930 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1931 | // Check that we can fold the initializer. In C++, we will have already done |
| 1932 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1933 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1934 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1935 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1936 | Notes.size() + 1) << VD; |
| 1937 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1938 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1939 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1940 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1941 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1942 | Notes.size() + 1) << VD; |
| 1943 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1944 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1945 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1946 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1947 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1948 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1951 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1952 | Qualifiers Quals = T.getQualifiers(); |
| 1953 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1954 | } |
| 1955 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1956 | /// Get the base index of the given base class within an APValue representing |
| 1957 | /// the given derived class. |
| 1958 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1959 | const CXXRecordDecl *Base) { |
| 1960 | Base = Base->getCanonicalDecl(); |
| 1961 | unsigned Index = 0; |
| 1962 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1963 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1964 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1965 | return Index; |
| 1966 | } |
| 1967 | |
| 1968 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1969 | } |
| 1970 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1971 | /// Extract the value of a character from a string literal. |
| 1972 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 1973 | uint64_t Index) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1974 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1975 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1976 | const ConstantArrayType *CAT = |
| 1977 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1978 | assert(CAT && "string literal isn't an array"); |
| 1979 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1980 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1981 | |
| 1982 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1983 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1984 | if (Index < S->getLength()) |
| 1985 | Value = S->getCodeUnit(Index); |
| 1986 | return Value; |
| 1987 | } |
| 1988 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1989 | // Expand a string literal into an array of characters. |
| 1990 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 1991 | APValue &Result) { |
| 1992 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1993 | const ConstantArrayType *CAT = |
| 1994 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1995 | assert(CAT && "string literal isn't an array"); |
| 1996 | QualType CharType = CAT->getElementType(); |
| 1997 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 1998 | |
| 1999 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2000 | Result = APValue(APValue::UninitArray(), |
| 2001 | std::min(S->getLength(), Elts), Elts); |
| 2002 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2003 | CharType->isUnsignedIntegerType()); |
| 2004 | if (Result.hasArrayFiller()) |
| 2005 | Result.getArrayFiller() = APValue(Value); |
| 2006 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2007 | Value = S->getCodeUnit(I); |
| 2008 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | // Expand an array so that it has more than Index filled elements. |
| 2013 | static void expandArray(APValue &Array, unsigned Index) { |
| 2014 | unsigned Size = Array.getArraySize(); |
| 2015 | assert(Index < Size); |
| 2016 | |
| 2017 | // Always at least double the number of elements for which we store a value. |
| 2018 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2019 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2020 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2021 | |
| 2022 | // Copy the data across. |
| 2023 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2024 | for (unsigned I = 0; I != OldElts; ++I) |
| 2025 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2026 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2027 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2028 | if (NewValue.hasArrayFiller()) |
| 2029 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2030 | Array.swap(NewValue); |
| 2031 | } |
| 2032 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2033 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2034 | enum AccessKinds { |
| 2035 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2036 | AK_Assign, |
| 2037 | AK_Increment, |
| 2038 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2039 | }; |
| 2040 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2041 | /// A handle to a complete object (an object that is not a subobject of |
| 2042 | /// another object). |
| 2043 | struct CompleteObject { |
| 2044 | /// The value of the complete object. |
| 2045 | APValue *Value; |
| 2046 | /// The type of the complete object. |
| 2047 | QualType Type; |
| 2048 | |
| 2049 | CompleteObject() : Value(0) {} |
| 2050 | CompleteObject(APValue *Value, QualType Type) |
| 2051 | : Value(Value), Type(Type) { |
| 2052 | assert(Value && "missing value for complete object"); |
| 2053 | } |
| 2054 | |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2055 | LLVM_EXPLICIT operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2056 | }; |
| 2057 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2058 | /// Find the designated sub-object of an rvalue. |
| 2059 | template<typename SubobjectHandler> |
| 2060 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2061 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2062 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2063 | if (Sub.Invalid) |
| 2064 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2065 | return handler.failed(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2066 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2067 | if (Info.getLangOpts().CPlusPlus11) |
| 2068 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2069 | << handler.AccessKind; |
| 2070 | else |
| 2071 | Info.Diag(E); |
| 2072 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2073 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2074 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2075 | APValue *O = Obj.Value; |
| 2076 | QualType ObjType = Obj.Type; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2077 | const FieldDecl *LastField = 0; |
| 2078 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2079 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2080 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2081 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 2082 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2083 | Info.Diag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
| 2084 | return handler.failed(); |
| 2085 | } |
| 2086 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2087 | if (I == N) { |
| 2088 | if (!handler.found(*O, ObjType)) |
| 2089 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2090 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2091 | // If we modified a bit-field, truncate it to the right width. |
| 2092 | if (handler.AccessKind != AK_Read && |
| 2093 | LastField && LastField->isBitField() && |
| 2094 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2095 | return false; |
| 2096 | |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | LastField = 0; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2101 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2102 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2103 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2104 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2105 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2106 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2107 | // Note, it should not be possible to form a pointer with a valid |
| 2108 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2109 | if (Info.getLangOpts().CPlusPlus11) |
| 2110 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2111 | << handler.AccessKind; |
| 2112 | else |
| 2113 | Info.Diag(E); |
| 2114 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2115 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2116 | |
| 2117 | ObjType = CAT->getElementType(); |
| 2118 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2119 | // An array object is represented as either an Array APValue or as an |
| 2120 | // LValue which refers to a string literal. |
| 2121 | if (O->isLValue()) { |
| 2122 | assert(I == N - 1 && "extracting subobject of character?"); |
| 2123 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2124 | if (handler.AccessKind != AK_Read) |
| 2125 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 2126 | *O); |
| 2127 | else |
| 2128 | return handler.foundString(*O, ObjType, Index); |
| 2129 | } |
| 2130 | |
| 2131 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2132 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2133 | else if (handler.AccessKind != AK_Read) { |
| 2134 | expandArray(*O, Index); |
| 2135 | O = &O->getArrayInitializedElt(Index); |
| 2136 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2137 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2138 | } else if (ObjType->isAnyComplexType()) { |
| 2139 | // Next subobject is a complex number. |
| 2140 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2141 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2142 | if (Info.getLangOpts().CPlusPlus11) |
| 2143 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2144 | << handler.AccessKind; |
| 2145 | else |
| 2146 | Info.Diag(E); |
| 2147 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2148 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2149 | |
| 2150 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2151 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2152 | if (WasConstQualified) |
| 2153 | ObjType.addConst(); |
| 2154 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2155 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2156 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2157 | return handler.found(Index ? O->getComplexIntImag() |
| 2158 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2159 | } else { |
| 2160 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2161 | return handler.found(Index ? O->getComplexFloatImag() |
| 2162 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2163 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2164 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2165 | if (Field->isMutable() && handler.AccessKind == AK_Read) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2166 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2167 | << Field; |
| 2168 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2169 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2172 | // Next subobject is a class, struct or union field. |
| 2173 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2174 | if (RD->isUnion()) { |
| 2175 | const FieldDecl *UnionField = O->getUnionField(); |
| 2176 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2177 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2178 | Info.Diag(E, diag::note_constexpr_access_inactive_union_member) |
| 2179 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2180 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2181 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2182 | O = &O->getUnionValue(); |
| 2183 | } else |
| 2184 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2185 | |
| 2186 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2187 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2188 | if (WasConstQualified && !Field->isMutable()) |
| 2189 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2190 | |
| 2191 | if (ObjType.isVolatileQualified()) { |
| 2192 | if (Info.getLangOpts().CPlusPlus) { |
| 2193 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2194 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2195 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2196 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2197 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2198 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2199 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2200 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2201 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2202 | |
| 2203 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2204 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2205 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2206 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2207 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2208 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2209 | |
| 2210 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2211 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2212 | if (WasConstQualified) |
| 2213 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2214 | } |
| 2215 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2218 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2219 | struct ExtractSubobjectHandler { |
| 2220 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2221 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2222 | |
| 2223 | static const AccessKinds AccessKind = AK_Read; |
| 2224 | |
| 2225 | typedef bool result_type; |
| 2226 | bool failed() { return false; } |
| 2227 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2228 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2229 | return true; |
| 2230 | } |
| 2231 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2232 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2233 | return true; |
| 2234 | } |
| 2235 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2236 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2237 | return true; |
| 2238 | } |
| 2239 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2240 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2241 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2242 | return true; |
| 2243 | } |
| 2244 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2245 | } // end anonymous namespace |
| 2246 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2247 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2248 | |
| 2249 | /// Extract the designated sub-object of an rvalue. |
| 2250 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2251 | const CompleteObject &Obj, |
| 2252 | const SubobjectDesignator &Sub, |
| 2253 | APValue &Result) { |
| 2254 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2255 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2258 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2259 | struct ModifySubobjectHandler { |
| 2260 | EvalInfo &Info; |
| 2261 | APValue &NewVal; |
| 2262 | const Expr *E; |
| 2263 | |
| 2264 | typedef bool result_type; |
| 2265 | static const AccessKinds AccessKind = AK_Assign; |
| 2266 | |
| 2267 | bool checkConst(QualType QT) { |
| 2268 | // Assigning to a const object has undefined behavior. |
| 2269 | if (QT.isConstQualified()) { |
| 2270 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2271 | return false; |
| 2272 | } |
| 2273 | return true; |
| 2274 | } |
| 2275 | |
| 2276 | bool failed() { return false; } |
| 2277 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2278 | if (!checkConst(SubobjType)) |
| 2279 | return false; |
| 2280 | // We've been given ownership of NewVal, so just swap it in. |
| 2281 | Subobj.swap(NewVal); |
| 2282 | return true; |
| 2283 | } |
| 2284 | bool found(APSInt &Value, QualType SubobjType) { |
| 2285 | if (!checkConst(SubobjType)) |
| 2286 | return false; |
| 2287 | if (!NewVal.isInt()) { |
| 2288 | // Maybe trying to write a cast pointer value into a complex? |
| 2289 | Info.Diag(E); |
| 2290 | return false; |
| 2291 | } |
| 2292 | Value = NewVal.getInt(); |
| 2293 | return true; |
| 2294 | } |
| 2295 | bool found(APFloat &Value, QualType SubobjType) { |
| 2296 | if (!checkConst(SubobjType)) |
| 2297 | return false; |
| 2298 | Value = NewVal.getFloat(); |
| 2299 | return true; |
| 2300 | } |
| 2301 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2302 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 2303 | } |
| 2304 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2305 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2306 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2307 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 2308 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2309 | /// Update the designated sub-object of an rvalue to the given value. |
| 2310 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2311 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2312 | const SubobjectDesignator &Sub, |
| 2313 | APValue &NewVal) { |
| 2314 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2315 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2318 | /// Find the position where two subobject designators diverge, or equivalently |
| 2319 | /// the length of the common initial subsequence. |
| 2320 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 2321 | const SubobjectDesignator &A, |
| 2322 | const SubobjectDesignator &B, |
| 2323 | bool &WasArrayIndex) { |
| 2324 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 2325 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2326 | if (!ObjType.isNull() && |
| 2327 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2328 | // Next subobject is an array element. |
| 2329 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 2330 | WasArrayIndex = true; |
| 2331 | return I; |
| 2332 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2333 | if (ObjType->isAnyComplexType()) |
| 2334 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2335 | else |
| 2336 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2337 | } else { |
| 2338 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 2339 | WasArrayIndex = false; |
| 2340 | return I; |
| 2341 | } |
| 2342 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 2343 | // Next subobject is a field. |
| 2344 | ObjType = FD->getType(); |
| 2345 | else |
| 2346 | // Next subobject is a base class. |
| 2347 | ObjType = QualType(); |
| 2348 | } |
| 2349 | } |
| 2350 | WasArrayIndex = false; |
| 2351 | return I; |
| 2352 | } |
| 2353 | |
| 2354 | /// Determine whether the given subobject designators refer to elements of the |
| 2355 | /// same array object. |
| 2356 | static bool AreElementsOfSameArray(QualType ObjType, |
| 2357 | const SubobjectDesignator &A, |
| 2358 | const SubobjectDesignator &B) { |
| 2359 | if (A.Entries.size() != B.Entries.size()) |
| 2360 | return false; |
| 2361 | |
| 2362 | bool IsArray = A.MostDerivedArraySize != 0; |
| 2363 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 2364 | // A is a subobject of the array element. |
| 2365 | return false; |
| 2366 | |
| 2367 | // If A (and B) designates an array element, the last entry will be the array |
| 2368 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 2369 | // of length 1' case, and the entire path must match. |
| 2370 | bool WasArrayIndex; |
| 2371 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 2372 | return CommonLength >= A.Entries.size() - IsArray; |
| 2373 | } |
| 2374 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2375 | /// Find the complete object to which an LValue refers. |
| 2376 | CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, AccessKinds AK, |
| 2377 | const LValue &LVal, QualType LValType) { |
| 2378 | if (!LVal.Base) { |
| 2379 | Info.Diag(E, diag::note_constexpr_access_null) << AK; |
| 2380 | return CompleteObject(); |
| 2381 | } |
| 2382 | |
| 2383 | CallStackFrame *Frame = 0; |
| 2384 | if (LVal.CallIndex) { |
| 2385 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 2386 | if (!Frame) { |
| 2387 | Info.Diag(E, diag::note_constexpr_lifetime_ended, 1) |
| 2388 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 2389 | NoteLValueLocation(Info, LVal.Base); |
| 2390 | return CompleteObject(); |
| 2391 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 2395 | // is not a constant expression (even if the object is non-volatile). We also |
| 2396 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 2397 | // semantics. |
| 2398 | if (LValType.isVolatileQualified()) { |
| 2399 | if (Info.getLangOpts().CPlusPlus) |
| 2400 | Info.Diag(E, diag::note_constexpr_access_volatile_type) |
| 2401 | << AK << LValType; |
| 2402 | else |
| 2403 | Info.Diag(E); |
| 2404 | return CompleteObject(); |
| 2405 | } |
| 2406 | |
| 2407 | // Compute value storage location and type of base object. |
| 2408 | APValue *BaseVal = 0; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2409 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2410 | |
| 2411 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 2412 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 2413 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 2414 | // expressions are constant expressions too. Inside constexpr functions, |
| 2415 | // parameters are constant expressions even if they're non-const. |
| 2416 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 2417 | // both readable and writable inside constant expressions. |
| 2418 | // In C, such things can also be folded, although they are not ICEs. |
| 2419 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 2420 | if (VD) { |
| 2421 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 2422 | VD = VDef; |
| 2423 | } |
| 2424 | if (!VD || VD->isInvalidDecl()) { |
| 2425 | Info.Diag(E); |
| 2426 | return CompleteObject(); |
| 2427 | } |
| 2428 | |
| 2429 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2430 | if (BaseType.isVolatileQualified()) { |
| 2431 | if (Info.getLangOpts().CPlusPlus) { |
| 2432 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2433 | << AK << 1 << VD; |
| 2434 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2435 | } else { |
| 2436 | Info.Diag(E); |
| 2437 | } |
| 2438 | return CompleteObject(); |
| 2439 | } |
| 2440 | |
| 2441 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 2442 | // the variable we're reading must be const. |
| 2443 | if (!Frame) { |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2444 | if (Info.getLangOpts().CPlusPlus1y && |
| 2445 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 2446 | // OK, we can read and modify an object if we're in the process of |
| 2447 | // evaluating its initializer, because its lifetime began in this |
| 2448 | // evaluation. |
| 2449 | } else if (AK != AK_Read) { |
| 2450 | // All the remaining cases only permit reading. |
| 2451 | Info.Diag(E, diag::note_constexpr_modify_global); |
| 2452 | return CompleteObject(); |
| 2453 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2454 | // OK, we can read this variable. |
| 2455 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| 2456 | if (!BaseType.isConstQualified()) { |
| 2457 | if (Info.getLangOpts().CPlusPlus) { |
| 2458 | Info.Diag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 2459 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2460 | } else { |
| 2461 | Info.Diag(E); |
| 2462 | } |
| 2463 | return CompleteObject(); |
| 2464 | } |
| 2465 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 2466 | // We support folding of const floating-point types, in order to make |
| 2467 | // static const data members of such types (supported as an extension) |
| 2468 | // more useful. |
| 2469 | if (Info.getLangOpts().CPlusPlus11) { |
| 2470 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2471 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2472 | } else { |
| 2473 | Info.CCEDiag(E); |
| 2474 | } |
| 2475 | } else { |
| 2476 | // FIXME: Allow folding of values of any literal type in all languages. |
| 2477 | if (Info.getLangOpts().CPlusPlus11) { |
| 2478 | Info.Diag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2479 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2480 | } else { |
| 2481 | Info.Diag(E); |
| 2482 | } |
| 2483 | return CompleteObject(); |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal)) |
| 2488 | return CompleteObject(); |
| 2489 | } else { |
| 2490 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 2491 | |
| 2492 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2493 | if (const MaterializeTemporaryExpr *MTE = |
| 2494 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 2495 | assert(MTE->getStorageDuration() == SD_Static && |
| 2496 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2497 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2498 | // Per C++1y [expr.const]p2: |
| 2499 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 2500 | // - a [...] glvalue of integral or enumeration type that refers to |
| 2501 | // a non-volatile const object [...] |
| 2502 | // [...] |
| 2503 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 2504 | // object whose lifetime began within the evaluation of e. |
| 2505 | // |
| 2506 | // C++11 misses the 'began within the evaluation of e' check and |
| 2507 | // instead allows all temporaries, including things like: |
| 2508 | // int &&r = 1; |
| 2509 | // int x = ++r; |
| 2510 | // constexpr int k = r; |
| 2511 | // Therefore we use the C++1y rules in C++11 too. |
| 2512 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 2513 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 2514 | if (!(BaseType.isConstQualified() && |
| 2515 | BaseType->isIntegralOrEnumerationType()) && |
| 2516 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
| 2517 | Info.Diag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
| 2518 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2519 | return CompleteObject(); |
| 2520 | } |
| 2521 | |
| 2522 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 2523 | assert(BaseVal && "got reference to unevaluated temporary"); |
| 2524 | } else { |
| 2525 | Info.Diag(E); |
| 2526 | return CompleteObject(); |
| 2527 | } |
| 2528 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2529 | BaseVal = Frame->getTemporary(Base); |
| 2530 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2531 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 2534 | if (BaseType.isVolatileQualified()) { |
| 2535 | if (Info.getLangOpts().CPlusPlus) { |
| 2536 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2537 | << AK << 0; |
| 2538 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2539 | } else { |
| 2540 | Info.Diag(E); |
| 2541 | } |
| 2542 | return CompleteObject(); |
| 2543 | } |
| 2544 | } |
| 2545 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2546 | // During the construction of an object, it is not yet 'const'. |
| 2547 | // FIXME: We don't set up EvaluatingDecl for local variables or temporaries, |
| 2548 | // and this doesn't do quite the right thing for const subobjects of the |
| 2549 | // object under construction. |
| 2550 | if (LVal.getLValueBase() == Info.EvaluatingDecl) { |
| 2551 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 2552 | BaseType.removeLocalConst(); |
| 2553 | } |
| 2554 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 2555 | // In C++1y, we can't safely access any mutable state when we might be |
| 2556 | // evaluating after an unmodeled side effect or an evaluation failure. |
| 2557 | // |
| 2558 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 2559 | // to be read here (but take care with 'mutable' fields). |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2560 | if (Frame && Info.getLangOpts().CPlusPlus1y && |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 2561 | (Info.EvalStatus.HasSideEffects || Info.keepEvaluatingAfterFailure())) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2562 | return CompleteObject(); |
| 2563 | |
| 2564 | return CompleteObject(BaseVal, BaseType); |
| 2565 | } |
| 2566 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2567 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 2568 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 2569 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2570 | /// |
| 2571 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2572 | /// \param Conv - The expression for which we are performing the conversion. |
| 2573 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2574 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 2575 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2576 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 2577 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2578 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2579 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2580 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2581 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2582 | return false; |
| 2583 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2584 | // 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] | 2585 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2586 | if (!LVal.Designator.Invalid && Base && !LVal.CallIndex && |
| 2587 | !Type.isVolatileQualified()) { |
| 2588 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 2589 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 2590 | // initializer until now for such expressions. Such an expression can't be |
| 2591 | // an ICE in C, so this only matters for fold. |
| 2592 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2593 | if (Type.isVolatileQualified()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2594 | Info.Diag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2595 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2596 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2597 | APValue Lit; |
| 2598 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 2599 | return false; |
| 2600 | CompleteObject LitObj(&Lit, Base->getType()); |
| 2601 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
| 2602 | } else if (isa<StringLiteral>(Base)) { |
| 2603 | // We represent a string literal array as an lvalue pointing at the |
| 2604 | // corresponding expression, rather than building an array of chars. |
| 2605 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 2606 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
| 2607 | CompleteObject StrObj(&Str, Base->getType()); |
| 2608 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2609 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2612 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 2613 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2617 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2618 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2619 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2620 | return false; |
| 2621 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2622 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2623 | Info.Diag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2624 | return false; |
| 2625 | } |
| 2626 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2627 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2628 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2631 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { |
| 2632 | return T->isSignedIntegerType() && |
| 2633 | Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); |
| 2634 | } |
| 2635 | |
| 2636 | namespace { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2637 | struct CompoundAssignSubobjectHandler { |
| 2638 | EvalInfo &Info; |
| 2639 | const Expr *E; |
| 2640 | QualType PromotedLHSType; |
| 2641 | BinaryOperatorKind Opcode; |
| 2642 | const APValue &RHS; |
| 2643 | |
| 2644 | static const AccessKinds AccessKind = AK_Assign; |
| 2645 | |
| 2646 | typedef bool result_type; |
| 2647 | |
| 2648 | bool checkConst(QualType QT) { |
| 2649 | // Assigning to a const object has undefined behavior. |
| 2650 | if (QT.isConstQualified()) { |
| 2651 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2652 | return false; |
| 2653 | } |
| 2654 | return true; |
| 2655 | } |
| 2656 | |
| 2657 | bool failed() { return false; } |
| 2658 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2659 | switch (Subobj.getKind()) { |
| 2660 | case APValue::Int: |
| 2661 | return found(Subobj.getInt(), SubobjType); |
| 2662 | case APValue::Float: |
| 2663 | return found(Subobj.getFloat(), SubobjType); |
| 2664 | case APValue::ComplexInt: |
| 2665 | case APValue::ComplexFloat: |
| 2666 | // FIXME: Implement complex compound assignment. |
| 2667 | Info.Diag(E); |
| 2668 | return false; |
| 2669 | case APValue::LValue: |
| 2670 | return foundPointer(Subobj, SubobjType); |
| 2671 | default: |
| 2672 | // FIXME: can this happen? |
| 2673 | Info.Diag(E); |
| 2674 | return false; |
| 2675 | } |
| 2676 | } |
| 2677 | bool found(APSInt &Value, QualType SubobjType) { |
| 2678 | if (!checkConst(SubobjType)) |
| 2679 | return false; |
| 2680 | |
| 2681 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 2682 | // We don't support compound assignment on integer-cast-to-pointer |
| 2683 | // values. |
| 2684 | Info.Diag(E); |
| 2685 | return false; |
| 2686 | } |
| 2687 | |
| 2688 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 2689 | SubobjType, Value); |
| 2690 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 2691 | return false; |
| 2692 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 2693 | return true; |
| 2694 | } |
| 2695 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2696 | return checkConst(SubobjType) && |
| 2697 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 2698 | Value) && |
| 2699 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 2700 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2701 | } |
| 2702 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2703 | if (!checkConst(SubobjType)) |
| 2704 | return false; |
| 2705 | |
| 2706 | QualType PointeeType; |
| 2707 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2708 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2709 | |
| 2710 | if (PointeeType.isNull() || !RHS.isInt() || |
| 2711 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2712 | Info.Diag(E); |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2716 | int64_t Offset = getExtValue(RHS.getInt()); |
| 2717 | if (Opcode == BO_Sub) |
| 2718 | Offset = -Offset; |
| 2719 | |
| 2720 | LValue LVal; |
| 2721 | LVal.setFrom(Info.Ctx, Subobj); |
| 2722 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 2723 | return false; |
| 2724 | LVal.moveInto(Subobj); |
| 2725 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2726 | } |
| 2727 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2728 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2729 | } |
| 2730 | }; |
| 2731 | } // end anonymous namespace |
| 2732 | |
| 2733 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 2734 | |
| 2735 | /// Perform a compound assignment of LVal <op>= RVal. |
| 2736 | static bool handleCompoundAssignment( |
| 2737 | EvalInfo &Info, const Expr *E, |
| 2738 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 2739 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 2740 | if (LVal.Designator.Invalid) |
| 2741 | return false; |
| 2742 | |
| 2743 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2744 | Info.Diag(E); |
| 2745 | return false; |
| 2746 | } |
| 2747 | |
| 2748 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2749 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 2750 | RVal }; |
| 2751 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2752 | } |
| 2753 | |
| 2754 | namespace { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2755 | struct IncDecSubobjectHandler { |
| 2756 | EvalInfo &Info; |
| 2757 | const Expr *E; |
| 2758 | AccessKinds AccessKind; |
| 2759 | APValue *Old; |
| 2760 | |
| 2761 | typedef bool result_type; |
| 2762 | |
| 2763 | bool checkConst(QualType QT) { |
| 2764 | // Assigning to a const object has undefined behavior. |
| 2765 | if (QT.isConstQualified()) { |
| 2766 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2767 | return false; |
| 2768 | } |
| 2769 | return true; |
| 2770 | } |
| 2771 | |
| 2772 | bool failed() { return false; } |
| 2773 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2774 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 2775 | // if we're post-incrementing a complex. |
| 2776 | if (Old) { |
| 2777 | *Old = Subobj; |
| 2778 | Old = 0; |
| 2779 | } |
| 2780 | |
| 2781 | switch (Subobj.getKind()) { |
| 2782 | case APValue::Int: |
| 2783 | return found(Subobj.getInt(), SubobjType); |
| 2784 | case APValue::Float: |
| 2785 | return found(Subobj.getFloat(), SubobjType); |
| 2786 | case APValue::ComplexInt: |
| 2787 | return found(Subobj.getComplexIntReal(), |
| 2788 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2789 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2790 | case APValue::ComplexFloat: |
| 2791 | return found(Subobj.getComplexFloatReal(), |
| 2792 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2793 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2794 | case APValue::LValue: |
| 2795 | return foundPointer(Subobj, SubobjType); |
| 2796 | default: |
| 2797 | // FIXME: can this happen? |
| 2798 | Info.Diag(E); |
| 2799 | return false; |
| 2800 | } |
| 2801 | } |
| 2802 | bool found(APSInt &Value, QualType SubobjType) { |
| 2803 | if (!checkConst(SubobjType)) |
| 2804 | return false; |
| 2805 | |
| 2806 | if (!SubobjType->isIntegerType()) { |
| 2807 | // We don't support increment / decrement on integer-cast-to-pointer |
| 2808 | // values. |
| 2809 | Info.Diag(E); |
| 2810 | return false; |
| 2811 | } |
| 2812 | |
| 2813 | if (Old) *Old = APValue(Value); |
| 2814 | |
| 2815 | // bool arithmetic promotes to int, and the conversion back to bool |
| 2816 | // doesn't reduce mod 2^n, so special-case it. |
| 2817 | if (SubobjType->isBooleanType()) { |
| 2818 | if (AccessKind == AK_Increment) |
| 2819 | Value = 1; |
| 2820 | else |
| 2821 | Value = !Value; |
| 2822 | return true; |
| 2823 | } |
| 2824 | |
| 2825 | bool WasNegative = Value.isNegative(); |
| 2826 | if (AccessKind == AK_Increment) { |
| 2827 | ++Value; |
| 2828 | |
| 2829 | if (!WasNegative && Value.isNegative() && |
| 2830 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2831 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 2832 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2833 | } |
| 2834 | } else { |
| 2835 | --Value; |
| 2836 | |
| 2837 | if (WasNegative && !Value.isNegative() && |
| 2838 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2839 | unsigned BitWidth = Value.getBitWidth(); |
| 2840 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 2841 | ActualValue.setBit(BitWidth); |
| 2842 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2843 | } |
| 2844 | } |
| 2845 | return true; |
| 2846 | } |
| 2847 | bool found(APFloat &Value, QualType SubobjType) { |
| 2848 | if (!checkConst(SubobjType)) |
| 2849 | return false; |
| 2850 | |
| 2851 | if (Old) *Old = APValue(Value); |
| 2852 | |
| 2853 | APFloat One(Value.getSemantics(), 1); |
| 2854 | if (AccessKind == AK_Increment) |
| 2855 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 2856 | else |
| 2857 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 2858 | return true; |
| 2859 | } |
| 2860 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2861 | if (!checkConst(SubobjType)) |
| 2862 | return false; |
| 2863 | |
| 2864 | QualType PointeeType; |
| 2865 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2866 | PointeeType = PT->getPointeeType(); |
| 2867 | else { |
| 2868 | Info.Diag(E); |
| 2869 | return false; |
| 2870 | } |
| 2871 | |
| 2872 | LValue LVal; |
| 2873 | LVal.setFrom(Info.Ctx, Subobj); |
| 2874 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 2875 | AccessKind == AK_Increment ? 1 : -1)) |
| 2876 | return false; |
| 2877 | LVal.moveInto(Subobj); |
| 2878 | return true; |
| 2879 | } |
| 2880 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2881 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2882 | } |
| 2883 | }; |
| 2884 | } // end anonymous namespace |
| 2885 | |
| 2886 | /// Perform an increment or decrement on LVal. |
| 2887 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 2888 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 2889 | if (LVal.Designator.Invalid) |
| 2890 | return false; |
| 2891 | |
| 2892 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2893 | Info.Diag(E); |
| 2894 | return false; |
| 2895 | } |
| 2896 | |
| 2897 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 2898 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 2899 | IncDecSubobjectHandler Handler = { Info, E, AK, Old }; |
| 2900 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2901 | } |
| 2902 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2903 | /// Build an lvalue for the object argument of a member function call. |
| 2904 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 2905 | LValue &This) { |
| 2906 | if (Object->getType()->isPointerType()) |
| 2907 | return EvaluatePointer(Object, This, Info); |
| 2908 | |
| 2909 | if (Object->isGLValue()) |
| 2910 | return EvaluateLValue(Object, This, Info); |
| 2911 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2912 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2913 | return EvaluateTemporary(Object, This, Info); |
| 2914 | |
| 2915 | return false; |
| 2916 | } |
| 2917 | |
| 2918 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 2919 | /// lvalue referring to the result. |
| 2920 | /// |
| 2921 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2922 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 2923 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2924 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 2925 | /// the resulting LValue subobject designator. This is not possible when |
| 2926 | /// creating a bound member function. |
| 2927 | /// \return The field or method declaration to which the member pointer refers, |
| 2928 | /// or 0 if evaluation fails. |
| 2929 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2930 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2931 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2932 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2933 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2934 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2935 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2936 | return 0; |
| 2937 | |
| 2938 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 2939 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2940 | if (!MemPtr.getDecl()) { |
| 2941 | // FIXME: Specific diagnostic. |
| 2942 | Info.Diag(RHS); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2943 | return 0; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2944 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2945 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2946 | if (MemPtr.isDerivedMember()) { |
| 2947 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2948 | // The end of the derived-to-base path for the base object must match the |
| 2949 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2950 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2951 | LV.Designator.Entries.size()) { |
| 2952 | Info.Diag(RHS); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2953 | return 0; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2954 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2955 | unsigned PathLengthToMember = |
| 2956 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 2957 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 2958 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 2959 | LV.Designator.Entries[PathLengthToMember + I]); |
| 2960 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2961 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
| 2962 | Info.Diag(RHS); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2963 | return 0; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2964 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2968 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2969 | PathLengthToMember)) |
| 2970 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2971 | } else if (!MemPtr.Path.empty()) { |
| 2972 | // Extend the LValue path with the member pointer's path. |
| 2973 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 2974 | MemPtr.Path.size() + IncludeMember); |
| 2975 | |
| 2976 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2977 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 2978 | LVType = PT->getPointeeType(); |
| 2979 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 2980 | assert(RD && "member pointer access on non-class-type expression"); |
| 2981 | // The first class in the path is that of the lvalue. |
| 2982 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 2983 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2984 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2985 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2986 | RD = Base; |
| 2987 | } |
| 2988 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2989 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 2990 | MemPtr.getContainingRecord())) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2991 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | // Add the member. Note that we cannot build bound member functions here. |
| 2995 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2996 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2997 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2998 | return 0; |
| 2999 | } else if (const IndirectFieldDecl *IFD = |
| 3000 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3001 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3002 | return 0; |
| 3003 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3004 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3005 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | return MemPtr.getDecl(); |
| 3009 | } |
| 3010 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3011 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3012 | const BinaryOperator *BO, |
| 3013 | LValue &LV, |
| 3014 | bool IncludeMember = true) { |
| 3015 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3016 | |
| 3017 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
| 3018 | if (Info.keepEvaluatingAfterFailure()) { |
| 3019 | MemberPtr MemPtr; |
| 3020 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3021 | } |
| 3022 | return 0; |
| 3023 | } |
| 3024 | |
| 3025 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3026 | BO->getRHS(), IncludeMember); |
| 3027 | } |
| 3028 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3029 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3030 | /// the provided lvalue, which currently refers to the base object. |
| 3031 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3032 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3033 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3034 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3035 | return false; |
| 3036 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3037 | QualType TargetQT = E->getType(); |
| 3038 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3039 | TargetQT = PT->getPointeeType(); |
| 3040 | |
| 3041 | // Check this cast lands within the final derived-to-base subobject path. |
| 3042 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3043 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3044 | << D.MostDerivedType << TargetQT; |
| 3045 | return false; |
| 3046 | } |
| 3047 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3048 | // Check the type of the final cast. We don't need to check the path, |
| 3049 | // since a cast can only be formed if the path is unique. |
| 3050 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3051 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3052 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3053 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3054 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3055 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3056 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3057 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3058 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3059 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3060 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3061 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3062 | |
| 3063 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3064 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3067 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3068 | enum EvalStmtResult { |
| 3069 | /// Evaluation failed. |
| 3070 | ESR_Failed, |
| 3071 | /// Hit a 'return' statement. |
| 3072 | ESR_Returned, |
| 3073 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3074 | ESR_Succeeded, |
| 3075 | /// Hit a 'continue' statement. |
| 3076 | ESR_Continue, |
| 3077 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3078 | ESR_Break, |
| 3079 | /// Still scanning for 'case' or 'default' statement. |
| 3080 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3081 | }; |
| 3082 | } |
| 3083 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3084 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3085 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3086 | // We don't need to evaluate the initializer for a static local. |
| 3087 | if (!VD->hasLocalStorage()) |
| 3088 | return true; |
| 3089 | |
| 3090 | LValue Result; |
| 3091 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3092 | APValue &Val = Info.CurrentCall->createTemporary(VD, true); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3093 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3094 | if (!VD->getInit()) { |
| 3095 | Info.Diag(D->getLocStart(), diag::note_constexpr_uninitialized) |
| 3096 | << false << VD->getType(); |
| 3097 | Val = APValue(); |
| 3098 | return false; |
| 3099 | } |
| 3100 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3101 | if (!EvaluateInPlace(Val, Info, Result, VD->getInit())) { |
| 3102 | // Wipe out any partially-computed value, to allow tracking that this |
| 3103 | // evaluation failed. |
| 3104 | Val = APValue(); |
| 3105 | return false; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | return true; |
| 3110 | } |
| 3111 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3112 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3113 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3114 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3115 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3116 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3117 | return false; |
| 3118 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3119 | } |
| 3120 | |
| 3121 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3122 | const Stmt *S, const SwitchCase *SC = 0); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3123 | |
| 3124 | /// Evaluate the body of a loop, and translate the result as appropriate. |
| 3125 | static EvalStmtResult EvaluateLoopBody(APValue &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3126 | const Stmt *Body, |
| 3127 | const SwitchCase *Case = 0) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3128 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3129 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3130 | case ESR_Break: |
| 3131 | return ESR_Succeeded; |
| 3132 | case ESR_Succeeded: |
| 3133 | case ESR_Continue: |
| 3134 | return ESR_Continue; |
| 3135 | case ESR_Failed: |
| 3136 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3137 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3138 | return ESR; |
| 3139 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3140 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3143 | /// Evaluate a switch statement. |
| 3144 | static EvalStmtResult EvaluateSwitch(APValue &Result, EvalInfo &Info, |
| 3145 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3146 | BlockScopeRAII Scope(Info); |
| 3147 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3148 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3149 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3150 | { |
| 3151 | FullExpressionRAII Scope(Info); |
| 3152 | if (SS->getConditionVariable() && |
| 3153 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3154 | return ESR_Failed; |
| 3155 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3156 | return ESR_Failed; |
| 3157 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3158 | |
| 3159 | // Find the switch case corresponding to the value of the condition. |
| 3160 | // FIXME: Cache this lookup. |
| 3161 | const SwitchCase *Found = 0; |
| 3162 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3163 | SC = SC->getNextSwitchCase()) { |
| 3164 | if (isa<DefaultStmt>(SC)) { |
| 3165 | Found = SC; |
| 3166 | continue; |
| 3167 | } |
| 3168 | |
| 3169 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3170 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 3171 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 3172 | : LHS; |
| 3173 | if (LHS <= Value && Value <= RHS) { |
| 3174 | Found = SC; |
| 3175 | break; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | if (!Found) |
| 3180 | return ESR_Succeeded; |
| 3181 | |
| 3182 | // Search the switch body for the switch case and evaluate it from there. |
| 3183 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 3184 | case ESR_Break: |
| 3185 | return ESR_Succeeded; |
| 3186 | case ESR_Succeeded: |
| 3187 | case ESR_Continue: |
| 3188 | case ESR_Failed: |
| 3189 | case ESR_Returned: |
| 3190 | return ESR; |
| 3191 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3192 | // This can only happen if the switch case is nested within a statement |
| 3193 | // expression. We have no intention of supporting that. |
| 3194 | Info.Diag(Found->getLocStart(), diag::note_constexpr_stmt_expr_unsupported); |
| 3195 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3196 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 3197 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3200 | // Evaluate a statement. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3201 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3202 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 3203 | if (!Info.nextStep(S)) |
| 3204 | return ESR_Failed; |
| 3205 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3206 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 3207 | // substatements until we hit the label. |
| 3208 | if (Case) { |
| 3209 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 3210 | // jump over. However, such objects must be of class type with a trivial |
| 3211 | // default constructor that initialize all subobjects, so must be empty, |
| 3212 | // so this almost never matters. |
| 3213 | switch (S->getStmtClass()) { |
| 3214 | case Stmt::CompoundStmtClass: |
| 3215 | // FIXME: Precompute which substatement of a compound statement we |
| 3216 | // would jump to, and go straight there rather than performing a |
| 3217 | // linear scan each time. |
| 3218 | case Stmt::LabelStmtClass: |
| 3219 | case Stmt::AttributedStmtClass: |
| 3220 | case Stmt::DoStmtClass: |
| 3221 | break; |
| 3222 | |
| 3223 | case Stmt::CaseStmtClass: |
| 3224 | case Stmt::DefaultStmtClass: |
| 3225 | if (Case == S) |
| 3226 | Case = 0; |
| 3227 | break; |
| 3228 | |
| 3229 | case Stmt::IfStmtClass: { |
| 3230 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3231 | // straight there rather than scanning both sides. |
| 3232 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3233 | |
| 3234 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 3235 | // preceded by our switch label. |
| 3236 | BlockScopeRAII Scope(Info); |
| 3237 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3238 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3239 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3240 | return ESR; |
| 3241 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3242 | } |
| 3243 | |
| 3244 | case Stmt::WhileStmtClass: { |
| 3245 | EvalStmtResult ESR = |
| 3246 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3247 | if (ESR != ESR_Continue) |
| 3248 | return ESR; |
| 3249 | break; |
| 3250 | } |
| 3251 | |
| 3252 | case Stmt::ForStmtClass: { |
| 3253 | const ForStmt *FS = cast<ForStmt>(S); |
| 3254 | EvalStmtResult ESR = |
| 3255 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 3256 | if (ESR != ESR_Continue) |
| 3257 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3258 | if (FS->getInc()) { |
| 3259 | FullExpressionRAII IncScope(Info); |
| 3260 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3261 | return ESR_Failed; |
| 3262 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3263 | break; |
| 3264 | } |
| 3265 | |
| 3266 | case Stmt::DeclStmtClass: |
| 3267 | // FIXME: If the variable has initialization that can't be jumped over, |
| 3268 | // bail out of any immediately-surrounding compound-statement too. |
| 3269 | default: |
| 3270 | return ESR_CaseNotFound; |
| 3271 | } |
| 3272 | } |
| 3273 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3274 | switch (S->getStmtClass()) { |
| 3275 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3276 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3277 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 3278 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3279 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3280 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3281 | return ESR_Failed; |
| 3282 | return ESR_Succeeded; |
| 3283 | } |
| 3284 | |
| 3285 | Info.Diag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3286 | return ESR_Failed; |
| 3287 | |
| 3288 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3289 | return ESR_Succeeded; |
| 3290 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3291 | case Stmt::DeclStmtClass: { |
| 3292 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 3293 | for (DeclStmt::const_decl_iterator DclIt = DS->decl_begin(), |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3294 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 3295 | // Each declaration initialization is its own full-expression. |
| 3296 | // FIXME: This isn't quite right; if we're performing aggregate |
| 3297 | // initialization, each braced subexpression is its own full-expression. |
| 3298 | FullExpressionRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3299 | if (!EvaluateDecl(Info, *DclIt) && !Info.keepEvaluatingAfterFailure()) |
| 3300 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3301 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3302 | return ESR_Succeeded; |
| 3303 | } |
| 3304 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3305 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3306 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3307 | FullExpressionRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3308 | if (RetExpr && !Evaluate(Result, Info, RetExpr)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3309 | return ESR_Failed; |
| 3310 | return ESR_Returned; |
| 3311 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3312 | |
| 3313 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3314 | BlockScopeRAII Scope(Info); |
| 3315 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3316 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 3317 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 3318 | BE = CS->body_end(); BI != BE; ++BI) { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3319 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI, Case); |
| 3320 | if (ESR == ESR_Succeeded) |
| 3321 | Case = 0; |
| 3322 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3323 | return ESR; |
| 3324 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3325 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3326 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3327 | |
| 3328 | case Stmt::IfStmtClass: { |
| 3329 | const IfStmt *IS = cast<IfStmt>(S); |
| 3330 | |
| 3331 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3332 | BlockScopeRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3333 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3334 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3335 | return ESR_Failed; |
| 3336 | |
| 3337 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 3338 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 3339 | if (ESR != ESR_Succeeded) |
| 3340 | return ESR; |
| 3341 | } |
| 3342 | return ESR_Succeeded; |
| 3343 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3344 | |
| 3345 | case Stmt::WhileStmtClass: { |
| 3346 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 3347 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3348 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3349 | bool Continue; |
| 3350 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 3351 | Continue)) |
| 3352 | return ESR_Failed; |
| 3353 | if (!Continue) |
| 3354 | break; |
| 3355 | |
| 3356 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 3357 | if (ESR != ESR_Continue) |
| 3358 | return ESR; |
| 3359 | } |
| 3360 | return ESR_Succeeded; |
| 3361 | } |
| 3362 | |
| 3363 | case Stmt::DoStmtClass: { |
| 3364 | const DoStmt *DS = cast<DoStmt>(S); |
| 3365 | bool Continue; |
| 3366 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3367 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3368 | if (ESR != ESR_Continue) |
| 3369 | return ESR; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3370 | Case = 0; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3371 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3372 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3373 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 3374 | return ESR_Failed; |
| 3375 | } while (Continue); |
| 3376 | return ESR_Succeeded; |
| 3377 | } |
| 3378 | |
| 3379 | case Stmt::ForStmtClass: { |
| 3380 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3381 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3382 | if (FS->getInit()) { |
| 3383 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 3384 | if (ESR != ESR_Succeeded) |
| 3385 | return ESR; |
| 3386 | } |
| 3387 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3388 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3389 | bool Continue = true; |
| 3390 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 3391 | FS->getCond(), Continue)) |
| 3392 | return ESR_Failed; |
| 3393 | if (!Continue) |
| 3394 | break; |
| 3395 | |
| 3396 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3397 | if (ESR != ESR_Continue) |
| 3398 | return ESR; |
| 3399 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3400 | if (FS->getInc()) { |
| 3401 | FullExpressionRAII IncScope(Info); |
| 3402 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3403 | return ESR_Failed; |
| 3404 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3405 | } |
| 3406 | return ESR_Succeeded; |
| 3407 | } |
| 3408 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3409 | case Stmt::CXXForRangeStmtClass: { |
| 3410 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3411 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3412 | |
| 3413 | // Initialize the __range variable. |
| 3414 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 3415 | if (ESR != ESR_Succeeded) |
| 3416 | return ESR; |
| 3417 | |
| 3418 | // Create the __begin and __end iterators. |
| 3419 | ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt()); |
| 3420 | if (ESR != ESR_Succeeded) |
| 3421 | return ESR; |
| 3422 | |
| 3423 | while (true) { |
| 3424 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3425 | { |
| 3426 | bool Continue = true; |
| 3427 | FullExpressionRAII CondExpr(Info); |
| 3428 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 3429 | return ESR_Failed; |
| 3430 | if (!Continue) |
| 3431 | break; |
| 3432 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3433 | |
| 3434 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3435 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3436 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 3437 | if (ESR != ESR_Succeeded) |
| 3438 | return ESR; |
| 3439 | |
| 3440 | // Loop body. |
| 3441 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3442 | if (ESR != ESR_Continue) |
| 3443 | return ESR; |
| 3444 | |
| 3445 | // Increment: ++__begin |
| 3446 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3447 | return ESR_Failed; |
| 3448 | } |
| 3449 | |
| 3450 | return ESR_Succeeded; |
| 3451 | } |
| 3452 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3453 | case Stmt::SwitchStmtClass: |
| 3454 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 3455 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3456 | case Stmt::ContinueStmtClass: |
| 3457 | return ESR_Continue; |
| 3458 | |
| 3459 | case Stmt::BreakStmtClass: |
| 3460 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3461 | |
| 3462 | case Stmt::LabelStmtClass: |
| 3463 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 3464 | |
| 3465 | case Stmt::AttributedStmtClass: |
| 3466 | // As a general principle, C++11 attributes can be ignored without |
| 3467 | // any semantic impact. |
| 3468 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 3469 | Case); |
| 3470 | |
| 3471 | case Stmt::CaseStmtClass: |
| 3472 | case Stmt::DefaultStmtClass: |
| 3473 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3477 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 3478 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 3479 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 3480 | /// so we need special handling. |
| 3481 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3482 | const CXXConstructorDecl *CD, |
| 3483 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3484 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 3485 | return false; |
| 3486 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3487 | // Value-initialization does not call a trivial default constructor, so such a |
| 3488 | // call is a core constant expression whether or not the constructor is |
| 3489 | // constexpr. |
| 3490 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3491 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3492 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 3493 | // we should be much more explicit about why it's not constexpr. |
| 3494 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 3495 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 3496 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3497 | } else { |
| 3498 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 3499 | } |
| 3500 | } |
| 3501 | return true; |
| 3502 | } |
| 3503 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3504 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 3505 | /// expression. |
| 3506 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 3507 | const FunctionDecl *Declaration, |
| 3508 | const FunctionDecl *Definition) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3509 | // Potential constant expressions can contain calls to declared, but not yet |
| 3510 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3511 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3512 | Declaration->isConstexpr()) |
| 3513 | return false; |
| 3514 | |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 3515 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 3516 | // We will have produced a relevant diagnostic while parsing it. |
| 3517 | if (Declaration->isInvalidDecl()) |
| 3518 | return false; |
| 3519 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3520 | // Can we evaluate this function call? |
| 3521 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 3522 | return true; |
| 3523 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3524 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3525 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3526 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 3527 | // should be much more explicit about why it's not constexpr. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3528 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 3529 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 3530 | << DiagDecl; |
| 3531 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 3532 | } else { |
| 3533 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 3534 | } |
| 3535 | return false; |
| 3536 | } |
| 3537 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3538 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3539 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 3543 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 3544 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3545 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3546 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3547 | I != E; ++I) { |
| 3548 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 3549 | // If we're checking for a potential constant expression, evaluate all |
| 3550 | // initializers even if some of them fail. |
| 3551 | if (!Info.keepEvaluatingAfterFailure()) |
| 3552 | return false; |
| 3553 | Success = false; |
| 3554 | } |
| 3555 | } |
| 3556 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3559 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3560 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 3561 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3562 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3563 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3564 | ArgVector ArgValues(Args.size()); |
| 3565 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3566 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3567 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3568 | if (!Info.CheckCallLimit(CallLoc)) |
| 3569 | return false; |
| 3570 | |
| 3571 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3572 | |
| 3573 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 3574 | // essential for unions, where the operations performed by the assignment |
| 3575 | // operator cannot be represented as statements. |
| 3576 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 3577 | if (MD && MD->isDefaulted() && MD->isTrivial()) { |
| 3578 | assert(This && |
| 3579 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 3580 | LValue RHS; |
| 3581 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 3582 | APValue RHSValue; |
| 3583 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 3584 | RHS, RHSValue)) |
| 3585 | return false; |
| 3586 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 3587 | RHSValue)) |
| 3588 | return false; |
| 3589 | This->moveInto(Result); |
| 3590 | return true; |
| 3591 | } |
| 3592 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3593 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3594 | if (ESR == ESR_Succeeded) { |
| 3595 | if (Callee->getResultType()->isVoidType()) |
| 3596 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3597 | Info.Diag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3598 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3599 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3602 | /// Evaluate a constructor call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3603 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3604 | ArrayRef<const Expr*> Args, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3605 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3606 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3607 | ArgVector ArgValues(Args.size()); |
| 3608 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3609 | return false; |
| 3610 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3611 | if (!Info.CheckCallLimit(CallLoc)) |
| 3612 | return false; |
| 3613 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3614 | const CXXRecordDecl *RD = Definition->getParent(); |
| 3615 | if (RD->getNumVBases()) { |
| 3616 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 3617 | return false; |
| 3618 | } |
| 3619 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3620 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3621 | |
| 3622 | // If it's a delegating constructor, just delegate. |
| 3623 | if (Definition->isDelegatingConstructor()) { |
| 3624 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3625 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 3626 | return false; |
| 3627 | return EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3630 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 3631 | // essential for unions, where the operations performed by the constructor |
| 3632 | // cannot be represented by ctor-initializers. |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3633 | if (Definition->isDefaulted() && |
Douglas Gregor | 093d4be | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 3634 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 3635 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3636 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3637 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3638 | return handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3639 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3643 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3644 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3645 | std::distance(RD->field_begin(), RD->field_end())); |
| 3646 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3647 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3648 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3649 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3650 | // A scope for temporaries lifetime-extended by reference members. |
| 3651 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 3652 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3653 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3654 | unsigned BasesSeen = 0; |
| 3655 | #ifndef NDEBUG |
| 3656 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 3657 | #endif |
| 3658 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 3659 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3660 | LValue Subobject = This; |
| 3661 | APValue *Value = &Result; |
| 3662 | |
| 3663 | // Determine the subobject to initialize. |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3664 | FieldDecl *FD = 0; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3665 | if ((*I)->isBaseInitializer()) { |
| 3666 | QualType BaseType((*I)->getBaseClass(), 0); |
| 3667 | #ifndef NDEBUG |
| 3668 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3669 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3670 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 3671 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 3672 | "base class initializers not in expected order"); |
| 3673 | ++BaseIt; |
| 3674 | #endif |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3675 | if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
| 3676 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 3677 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3678 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3679 | } else if ((FD = (*I)->getMember())) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3680 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout)) |
| 3681 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3682 | if (RD->isUnion()) { |
| 3683 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3684 | Value = &Result.getUnionValue(); |
| 3685 | } else { |
| 3686 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 3687 | } |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3688 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3689 | // Walk the indirect field decl's chain to find the object to initialize, |
| 3690 | // and make sure we've initialized every step along it. |
| 3691 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 3692 | CE = IFD->chain_end(); |
| 3693 | C != CE; ++C) { |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3694 | FD = cast<FieldDecl>(*C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3695 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 3696 | // Switch the union field if it differs. This happens if we had |
| 3697 | // preceding zero-initialization, and we're now initializing a union |
| 3698 | // subobject other than the first. |
| 3699 | // FIXME: In this case, the values of the other subobjects are |
| 3700 | // specified, since zero-initialization sets all padding bits to zero. |
| 3701 | if (Value->isUninit() || |
| 3702 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 3703 | if (CD->isUnion()) |
| 3704 | *Value = APValue(FD); |
| 3705 | else |
| 3706 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 3707 | std::distance(CD->field_begin(), CD->field_end())); |
| 3708 | } |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3709 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD)) |
| 3710 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3711 | if (CD->isUnion()) |
| 3712 | Value = &Value->getUnionValue(); |
| 3713 | else |
| 3714 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3715 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3716 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3717 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3718 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3719 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3720 | FullExpressionRAII InitScope(Info); |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3721 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit()) || |
| 3722 | (FD && FD->isBitField() && !truncateBitfieldValue(Info, (*I)->getInit(), |
| 3723 | *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3724 | // If we're checking for a potential constant expression, evaluate all |
| 3725 | // initializers even if some of them fail. |
| 3726 | if (!Info.keepEvaluatingAfterFailure()) |
| 3727 | return false; |
| 3728 | Success = false; |
| 3729 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3732 | return Success && |
| 3733 | EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3736 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3737 | // Generic Evaluation |
| 3738 | //===----------------------------------------------------------------------===// |
| 3739 | namespace { |
| 3740 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3741 | // FIXME: RetTy is always bool. Remove it. |
| 3742 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3743 | class ExprEvaluatorBase |
| 3744 | : public ConstStmtVisitor<Derived, RetTy> { |
| 3745 | private: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3746 | RetTy DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3747 | return static_cast<Derived*>(this)->Success(V, E); |
| 3748 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3749 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 3750 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3751 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3752 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3753 | // Check whether a conditional operator with a non-constant condition is a |
| 3754 | // potential constant expression. If neither arm is a potential constant |
| 3755 | // expression, then the conditional operator is not either. |
| 3756 | template<typename ConditionalOperator> |
| 3757 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3758 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3759 | |
| 3760 | // Speculatively evaluate both arms. |
| 3761 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3762 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3763 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 3764 | |
| 3765 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 3766 | if (Diag.empty()) |
| 3767 | return; |
| 3768 | |
| 3769 | Diag.clear(); |
| 3770 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 3771 | if (Diag.empty()) |
| 3772 | return; |
| 3773 | } |
| 3774 | |
| 3775 | Error(E, diag::note_constexpr_conditional_never_const); |
| 3776 | } |
| 3777 | |
| 3778 | |
| 3779 | template<typename ConditionalOperator> |
| 3780 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 3781 | bool BoolResult; |
| 3782 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3783 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3784 | CheckPotentialConstantConditional(E); |
| 3785 | return false; |
| 3786 | } |
| 3787 | |
| 3788 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 3789 | return StmtVisitorTy::Visit(EvalExpr); |
| 3790 | } |
| 3791 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3792 | protected: |
| 3793 | EvalInfo &Info; |
| 3794 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 3795 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 3796 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3797 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3798 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3801 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
| 3802 | |
| 3803 | public: |
| 3804 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 3805 | |
| 3806 | EvalInfo &getEvalInfo() { return Info; } |
| 3807 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3808 | /// Report an evaluation error. This should only be called when an error is |
| 3809 | /// first discovered. When propagating an error, just return false. |
| 3810 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3811 | Info.Diag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3812 | return false; |
| 3813 | } |
| 3814 | bool Error(const Expr *E) { |
| 3815 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 3816 | } |
| 3817 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3818 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3819 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3820 | } |
| 3821 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3822 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | RetTy VisitParenExpr(const ParenExpr *E) |
| 3826 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3827 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 3828 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3829 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 3830 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3831 | RetTy VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 3832 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3833 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 3834 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3835 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 3836 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | f8120ca | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 3837 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 3838 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 3839 | RetTy VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
| 3840 | // The initializer may not have been parsed yet, or might be erroneous. |
| 3841 | if (!E->getExpr()) |
| 3842 | return Error(E); |
| 3843 | return StmtVisitorTy::Visit(E->getExpr()); |
| 3844 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 3845 | // We cannot create any objects for which cleanups are required, so there is |
| 3846 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 3847 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 3848 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3849 | |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3850 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 3851 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 3852 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3853 | } |
| 3854 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 3855 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 3856 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3857 | } |
| 3858 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3859 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 3860 | switch (E->getOpcode()) { |
| 3861 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3862 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3863 | |
| 3864 | case BO_Comma: |
| 3865 | VisitIgnoredValue(E->getLHS()); |
| 3866 | return StmtVisitorTy::Visit(E->getRHS()); |
| 3867 | |
| 3868 | case BO_PtrMemD: |
| 3869 | case BO_PtrMemI: { |
| 3870 | LValue Obj; |
| 3871 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 3872 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3873 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3874 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3875 | return false; |
| 3876 | return DerivedSuccess(Result, E); |
| 3877 | } |
| 3878 | } |
| 3879 | } |
| 3880 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3881 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3882 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 3883 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3884 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3885 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3886 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3887 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3888 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3892 | bool IsBcpCall = false; |
| 3893 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 3894 | // the result is a constant expression if it can be folded without |
| 3895 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 3896 | // for discussion. |
| 3897 | if (const CallExpr *CallCE = |
| 3898 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 3899 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 3900 | IsBcpCall = true; |
| 3901 | |
| 3902 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 3903 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3904 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3905 | return false; |
| 3906 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3907 | FoldConstant Fold(Info, IsBcpCall); |
| 3908 | if (!HandleConditionalOperator(E)) { |
| 3909 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3910 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 3911 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3912 | |
| 3913 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3917 | if (APValue *Value = Info.CurrentCall->getTemporary(E)) |
| 3918 | return DerivedSuccess(*Value, E); |
| 3919 | |
| 3920 | const Expr *Source = E->getSourceExpr(); |
| 3921 | if (!Source) |
| 3922 | return Error(E); |
| 3923 | if (Source == E) { // sanity checking. |
| 3924 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 3925 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3926 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3927 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3928 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3929 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3930 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3931 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3932 | QualType CalleeType = Callee->getType(); |
| 3933 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3934 | const FunctionDecl *FD = 0; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3935 | LValue *This = 0, ThisVal; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3936 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3937 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 3938 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3939 | // Extract function decl and 'this' pointer from the callee. |
| 3940 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3941 | const ValueDecl *Member = 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3942 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 3943 | // Explicit bound member calls, such as x.f() or p->g(); |
| 3944 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3945 | return false; |
| 3946 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3947 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3948 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3949 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 3950 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3951 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 3952 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3953 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3954 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3955 | return Error(Callee); |
| 3956 | |
| 3957 | FD = dyn_cast<FunctionDecl>(Member); |
| 3958 | if (!FD) |
| 3959 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3960 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3961 | LValue Call; |
| 3962 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3963 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3964 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3965 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3966 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3967 | FD = dyn_cast_or_null<FunctionDecl>( |
| 3968 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3969 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3970 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3971 | |
| 3972 | // Overloaded operator calls to member functions are represented as normal |
| 3973 | // calls with '*this' as the first argument. |
| 3974 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 3975 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3976 | // FIXME: When selecting an implicit conversion for an overloaded |
| 3977 | // operator delete, we sometimes try to evaluate calls to conversion |
| 3978 | // operators without a 'this' parameter! |
| 3979 | if (Args.empty()) |
| 3980 | return Error(E); |
| 3981 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3982 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 3983 | return false; |
| 3984 | This = &ThisVal; |
| 3985 | Args = Args.slice(1); |
| 3986 | } |
| 3987 | |
| 3988 | // Don't call function pointers which have been cast to some other type. |
| 3989 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3990 | return Error(E); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3991 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3992 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3993 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 3994 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 3995 | return false; |
| 3996 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3997 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 3998 | // calls to such functions in constant expressions. |
| 3999 | if (This && !HasQualifier && |
| 4000 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4001 | return Error(E, diag::note_constexpr_virtual_call); |
| 4002 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4003 | const FunctionDecl *Definition = 0; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4004 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4005 | APValue Result; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4006 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4007 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4008 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 4009 | Info, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4010 | return false; |
| 4011 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4012 | return DerivedSuccess(Result, E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4015 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 4016 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 4017 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4018 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 4019 | if (E->getNumInits() == 0) |
| 4020 | return DerivedZeroInitialization(E); |
| 4021 | if (E->getNumInits() == 1) |
| 4022 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4023 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4024 | } |
| 4025 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4026 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4027 | } |
| 4028 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4029 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4030 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4031 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4032 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4033 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4034 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4035 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 4036 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 4037 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 4038 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4039 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4040 | if (!Evaluate(Val, Info, E->getBase())) |
| 4041 | return false; |
| 4042 | |
| 4043 | QualType BaseTy = E->getBase()->getType(); |
| 4044 | |
| 4045 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4046 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4047 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4048 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4049 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4050 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4051 | CompleteObject Obj(&Val, BaseTy); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4052 | SubobjectDesignator Designator(BaseTy); |
| 4053 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4054 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4055 | APValue Result; |
| 4056 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 4057 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4060 | RetTy VisitCastExpr(const CastExpr *E) { |
| 4061 | switch (E->getCastKind()) { |
| 4062 | default: |
| 4063 | break; |
| 4064 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4065 | case CK_AtomicToNonAtomic: { |
| 4066 | APValue AtomicVal; |
| 4067 | if (!EvaluateAtomic(E->getSubExpr(), AtomicVal, Info)) |
| 4068 | return false; |
| 4069 | return DerivedSuccess(AtomicVal, E); |
| 4070 | } |
| 4071 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4072 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4073 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4074 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 4075 | |
| 4076 | case CK_LValueToRValue: { |
| 4077 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4078 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 4079 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4080 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4081 | // 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] | 4082 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4083 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4084 | return false; |
| 4085 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4086 | } |
| 4087 | } |
| 4088 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4089 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4092 | RetTy VisitUnaryPostInc(const UnaryOperator *UO) { |
| 4093 | return VisitUnaryPostIncDec(UO); |
| 4094 | } |
| 4095 | RetTy VisitUnaryPostDec(const UnaryOperator *UO) { |
| 4096 | return VisitUnaryPostIncDec(UO); |
| 4097 | } |
| 4098 | RetTy VisitUnaryPostIncDec(const UnaryOperator *UO) { |
| 4099 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 4100 | return Error(UO); |
| 4101 | |
| 4102 | LValue LVal; |
| 4103 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 4104 | return false; |
| 4105 | APValue RVal; |
| 4106 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 4107 | UO->isIncrementOp(), &RVal)) |
| 4108 | return false; |
| 4109 | return DerivedSuccess(RVal, UO); |
| 4110 | } |
| 4111 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4112 | RetTy VisitStmtExpr(const StmtExpr *E) { |
| 4113 | // We will have checked the full-expressions inside the statement expression |
| 4114 | // when they were completed, and don't need to check them again now. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 4115 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4116 | return Error(E); |
| 4117 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4118 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4119 | const CompoundStmt *CS = E->getSubStmt(); |
| 4120 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 4121 | BE = CS->body_end(); |
| 4122 | /**/; ++BI) { |
| 4123 | if (BI + 1 == BE) { |
| 4124 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 4125 | if (!FinalExpr) { |
| 4126 | Info.Diag((*BI)->getLocStart(), |
| 4127 | diag::note_constexpr_stmt_expr_unsupported); |
| 4128 | return false; |
| 4129 | } |
| 4130 | return this->Visit(FinalExpr); |
| 4131 | } |
| 4132 | |
| 4133 | APValue ReturnValue; |
| 4134 | EvalStmtResult ESR = EvaluateStmt(ReturnValue, Info, *BI); |
| 4135 | if (ESR != ESR_Succeeded) { |
| 4136 | // FIXME: If the statement-expression terminated due to 'return', |
| 4137 | // 'break', or 'continue', it would be nice to propagate that to |
| 4138 | // the outer statement evaluation rather than bailing out. |
| 4139 | if (ESR != ESR_Failed) |
| 4140 | Info.Diag((*BI)->getLocStart(), |
| 4141 | diag::note_constexpr_stmt_expr_unsupported); |
| 4142 | return false; |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4147 | /// Visit a value which is evaluated, but whose value is ignored. |
| 4148 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4149 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4150 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4151 | }; |
| 4152 | |
| 4153 | } |
| 4154 | |
| 4155 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4156 | // Common base class for lvalue and temporary evaluation. |
| 4157 | //===----------------------------------------------------------------------===// |
| 4158 | namespace { |
| 4159 | template<class Derived> |
| 4160 | class LValueExprEvaluatorBase |
| 4161 | : public ExprEvaluatorBase<Derived, bool> { |
| 4162 | protected: |
| 4163 | LValue &Result; |
| 4164 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 4165 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 4166 | |
| 4167 | bool Success(APValue::LValueBase B) { |
| 4168 | Result.set(B); |
| 4169 | return true; |
| 4170 | } |
| 4171 | |
| 4172 | public: |
| 4173 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 4174 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 4175 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4176 | bool Success(const APValue &V, const Expr *E) { |
| 4177 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4178 | return true; |
| 4179 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4180 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4181 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4182 | // Handle non-static data members. |
| 4183 | QualType BaseTy; |
| 4184 | if (E->isArrow()) { |
| 4185 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 4186 | return false; |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4187 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4188 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 4189 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4190 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 4191 | return false; |
| 4192 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4193 | } else { |
| 4194 | if (!this->Visit(E->getBase())) |
| 4195 | return false; |
| 4196 | BaseTy = E->getBase()->getType(); |
| 4197 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4198 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4199 | const ValueDecl *MD = E->getMemberDecl(); |
| 4200 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 4201 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 4202 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4203 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4204 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 4205 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4206 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4207 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 4208 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4209 | } else |
| 4210 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4211 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4212 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4213 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4214 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4215 | RefValue)) |
| 4216 | return false; |
| 4217 | return Success(RefValue, E); |
| 4218 | } |
| 4219 | return true; |
| 4220 | } |
| 4221 | |
| 4222 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 4223 | switch (E->getOpcode()) { |
| 4224 | default: |
| 4225 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 4226 | |
| 4227 | case BO_PtrMemD: |
| 4228 | case BO_PtrMemI: |
| 4229 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | bool VisitCastExpr(const CastExpr *E) { |
| 4234 | switch (E->getCastKind()) { |
| 4235 | default: |
| 4236 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4237 | |
| 4238 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4239 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4240 | if (!this->Visit(E->getSubExpr())) |
| 4241 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4242 | |
| 4243 | // Now figure out the necessary offset to add to the base LV to get from |
| 4244 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4245 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 4246 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | }; |
| 4250 | } |
| 4251 | |
| 4252 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4253 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4254 | // |
| 4255 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 4256 | // function designators (in C), decl references to void objects (in C), and |
| 4257 | // temporaries (if building with -Wno-address-of-temporary). |
| 4258 | // |
| 4259 | // LValue evaluation produces values comprising a base expression of one of the |
| 4260 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4261 | // - Declarations |
| 4262 | // * VarDecl |
| 4263 | // * FunctionDecl |
| 4264 | // - Literals |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4265 | // * CompoundLiteralExpr in C |
| 4266 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4267 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4268 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4269 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4270 | // * ObjCEncodeExpr |
| 4271 | // * AddrLabelExpr |
| 4272 | // * BlockExpr |
| 4273 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4274 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4275 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4276 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4277 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 4278 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4279 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 4280 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4281 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4282 | //===----------------------------------------------------------------------===// |
| 4283 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4284 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4285 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4286 | public: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4287 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 4288 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4290 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4291 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4292 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4293 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 4294 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4295 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4296 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 4297 | bool VisitMemberExpr(const MemberExpr *E); |
| 4298 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 4299 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4300 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4301 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4302 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 4303 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4304 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4305 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4306 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 4307 | return VisitUnaryPreIncDec(UO); |
| 4308 | } |
| 4309 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 4310 | return VisitUnaryPreIncDec(UO); |
| 4311 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4312 | bool VisitBinAssign(const BinaryOperator *BO); |
| 4313 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4314 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4315 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4316 | switch (E->getCastKind()) { |
| 4317 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4318 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4319 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4320 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4321 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4322 | if (!Visit(E->getSubExpr())) |
| 4323 | return false; |
| 4324 | Result.Designator.setInvalid(); |
| 4325 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4326 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4327 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4328 | if (!Visit(E->getSubExpr())) |
| 4329 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4330 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4331 | } |
| 4332 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4333 | }; |
| 4334 | } // end anonymous namespace |
| 4335 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4336 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 4337 | /// expressions which are not glvalues, in two cases: |
| 4338 | /// * function designators in C, and |
| 4339 | /// * "extern void" objects |
| 4340 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 4341 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| 4342 | E->getType()->isVoidType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4343 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4346 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4347 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 4348 | return Success(FD); |
| 4349 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4350 | return VisitVarDecl(E, VD); |
| 4351 | return Error(E); |
| 4352 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 4353 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4354 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4355 | CallStackFrame *Frame = 0; |
| 4356 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) |
| 4357 | Frame = Info.CurrentCall; |
| 4358 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4359 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4360 | if (Frame) { |
| 4361 | Result.set(VD, Frame->Index); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4362 | return true; |
| 4363 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4364 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4365 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 4366 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4367 | APValue *V; |
| 4368 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4369 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4370 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 4371 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4372 | Info.Diag(E, diag::note_constexpr_use_uninit_reference); |
| 4373 | return false; |
| 4374 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4375 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4378 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 4379 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4380 | // Walk through the expression to find the materialized temporary itself. |
| 4381 | SmallVector<const Expr *, 2> CommaLHSs; |
| 4382 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 4383 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 4384 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4385 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4386 | // If we passed any comma operators, evaluate their LHSs. |
| 4387 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 4388 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 4389 | return false; |
| 4390 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4391 | // A materialized temporary with static storage duration can appear within the |
| 4392 | // result of a constant expression evaluation, so we need to preserve its |
| 4393 | // value for use outside this evaluation. |
| 4394 | APValue *Value; |
| 4395 | if (E->getStorageDuration() == SD_Static) { |
| 4396 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4397 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4398 | Result.set(E); |
| 4399 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4400 | Value = &Info.CurrentCall-> |
| 4401 | createTemporary(E, E->getStorageDuration() == SD_Automatic); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4402 | Result.set(E, Info.CurrentCall->Index); |
| 4403 | } |
| 4404 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4405 | QualType Type = Inner->getType(); |
| 4406 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4407 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4408 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 4409 | (E->getStorageDuration() == SD_Static && |
| 4410 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 4411 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4412 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4413 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4414 | |
| 4415 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4416 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 4417 | --I; |
| 4418 | switch (Adjustments[I].Kind) { |
| 4419 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 4420 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 4421 | Type, Result)) |
| 4422 | return false; |
| 4423 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 4424 | break; |
| 4425 | |
| 4426 | case SubobjectAdjustment::FieldAdjustment: |
| 4427 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 4428 | return false; |
| 4429 | Type = Adjustments[I].Field->getType(); |
| 4430 | break; |
| 4431 | |
| 4432 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 4433 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 4434 | Adjustments[I].Ptr.RHS)) |
| 4435 | return false; |
| 4436 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 4437 | break; |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4444 | bool |
| 4445 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4446 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 4447 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 4448 | // 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] | 4449 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4452 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4453 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4454 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4455 | |
| 4456 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 4457 | << E->getExprOperand()->getType() |
| 4458 | << E->getExprOperand()->getSourceRange(); |
| 4459 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4462 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4463 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4464 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4465 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4466 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4467 | // Handle static data members. |
| 4468 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 4469 | VisitIgnoredValue(E->getBase()); |
| 4470 | return VisitVarDecl(E, VD); |
| 4471 | } |
| 4472 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4473 | // Handle static member functions. |
| 4474 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 4475 | if (MD->isStatic()) { |
| 4476 | VisitIgnoredValue(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4477 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4478 | } |
| 4479 | } |
| 4480 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4481 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4482 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4485 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4486 | // FIXME: Deal with vectors as array subscript bases. |
| 4487 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4488 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4489 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4490 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4491 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4493 | APSInt Index; |
| 4494 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4495 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4496 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4497 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), |
| 4498 | getExtValue(Index)); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4499 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4500 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4501 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4502 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4505 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4506 | if (!Visit(E->getSubExpr())) |
| 4507 | return false; |
| 4508 | // __real is a no-op on scalar lvalues. |
| 4509 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 4510 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 4511 | return true; |
| 4512 | } |
| 4513 | |
| 4514 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 4515 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 4516 | "lvalue __imag__ on scalar?"); |
| 4517 | if (!Visit(E->getSubExpr())) |
| 4518 | return false; |
| 4519 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 4520 | return true; |
| 4521 | } |
| 4522 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4523 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
| 4524 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4525 | return Error(UO); |
| 4526 | |
| 4527 | if (!this->Visit(UO->getSubExpr())) |
| 4528 | return false; |
| 4529 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4530 | return handleIncDec( |
| 4531 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
| 4532 | UO->isIncrementOp(), 0); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 4536 | const CompoundAssignOperator *CAO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4537 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4538 | return Error(CAO); |
| 4539 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4540 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4541 | |
| 4542 | // The overall lvalue result is the result of evaluating the LHS. |
| 4543 | if (!this->Visit(CAO->getLHS())) { |
| 4544 | if (Info.keepEvaluatingAfterFailure()) |
| 4545 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 4546 | return false; |
| 4547 | } |
| 4548 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4549 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 4550 | return false; |
| 4551 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 4552 | return handleCompoundAssignment( |
| 4553 | this->Info, CAO, |
| 4554 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 4555 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4559 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 4560 | return Error(E); |
| 4561 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4562 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4563 | |
| 4564 | if (!this->Visit(E->getLHS())) { |
| 4565 | if (Info.keepEvaluatingAfterFailure()) |
| 4566 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 4567 | return false; |
| 4568 | } |
| 4569 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4570 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 4571 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4572 | |
| 4573 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4574 | NewVal); |
| 4575 | } |
| 4576 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4577 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4578 | // Pointer Evaluation |
| 4579 | //===----------------------------------------------------------------------===// |
| 4580 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4581 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4582 | class PointerExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4583 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4584 | LValue &Result; |
| 4585 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4586 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4587 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4588 | return true; |
| 4589 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4590 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4592 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4593 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4594 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4595 | bool Success(const APValue &V, const Expr *E) { |
| 4596 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4597 | return true; |
| 4598 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4599 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4600 | return Success((Expr*)0); |
| 4601 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4602 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4603 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4604 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4605 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4606 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4607 | { return Success(E); } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 4608 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4609 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4610 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4611 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4612 | bool VisitCallExpr(const CallExpr *E); |
| 4613 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4614 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4615 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4616 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4617 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4618 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4619 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 4620 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4621 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4622 | if (!Info.CurrentCall->This) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4623 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4624 | Result = *Info.CurrentCall->This; |
| 4625 | return true; |
| 4626 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4627 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4628 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4629 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4630 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4631 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4632 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4633 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4634 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4637 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4638 | if (E->getOpcode() != BO_Add && |
| 4639 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4640 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4642 | const Expr *PExp = E->getLHS(); |
| 4643 | const Expr *IExp = E->getRHS(); |
| 4644 | if (IExp->getType()->isPointerType()) |
| 4645 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4647 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 4648 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4649 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4651 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4652 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4653 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4654 | |
| 4655 | int64_t AdditionalOffset = getExtValue(Offset); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4656 | if (E->getOpcode() == BO_Sub) |
| 4657 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4658 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4659 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4660 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 4661 | AdditionalOffset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4662 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4663 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4664 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4665 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4666 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4667 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4668 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4669 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4670 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4671 | switch (E->getCastKind()) { |
| 4672 | default: |
| 4673 | break; |
| 4674 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4675 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4676 | case CK_CPointerToObjCPointerCast: |
| 4677 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4678 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4679 | if (!Visit(SubExpr)) |
| 4680 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4681 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 4682 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 4683 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4684 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4685 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4686 | if (SubExpr->getType()->isVoidPointerType()) |
| 4687 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 4688 | << 3 << SubExpr->getType(); |
| 4689 | else |
| 4690 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4691 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4692 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4693 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4694 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4695 | case CK_UncheckedDerivedToBase: |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4696 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4697 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4698 | if (!Result.Base && Result.Offset.isZero()) |
| 4699 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4700 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4701 | // 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] | 4702 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4703 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 4704 | castAs<PointerType>()->getPointeeType(), |
| 4705 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4706 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4707 | case CK_BaseToDerived: |
| 4708 | if (!Visit(E->getSubExpr())) |
| 4709 | return false; |
| 4710 | if (!Result.Base && Result.Offset.isZero()) |
| 4711 | return true; |
| 4712 | return HandleBaseToDerivedCast(Info, E, Result); |
| 4713 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4714 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4715 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4716 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4717 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4718 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4719 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4720 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4721 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4722 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4723 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4724 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4725 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4726 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 4727 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4728 | Result.Base = (Expr*)0; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4729 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4730 | Result.CallIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4731 | Result.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4732 | return true; |
| 4733 | } else { |
| 4734 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4735 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4736 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4737 | } |
| 4738 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4739 | case CK_ArrayToPointerDecay: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4740 | if (SubExpr->isGLValue()) { |
| 4741 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 4742 | return false; |
| 4743 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4744 | Result.set(SubExpr, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4745 | if (!EvaluateInPlace(Info.CurrentCall->createTemporary(SubExpr, false), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4746 | Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4747 | return false; |
| 4748 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4749 | // The result is a pointer to the first element of the array. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4750 | if (const ConstantArrayType *CAT |
| 4751 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 4752 | Result.addArray(Info, E, CAT); |
| 4753 | else |
| 4754 | Result.Designator.setInvalid(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4755 | return true; |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4756 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4757 | case CK_FunctionToPointerDecay: |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4758 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4761 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4762 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4763 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4764 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4765 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4766 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 4767 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 4768 | switch (E->isBuiltinCall()) { |
| 4769 | case Builtin::BI__builtin_addressof: |
| 4770 | return EvaluateLValue(E->getArg(0), Result, Info); |
| 4771 | |
| 4772 | default: |
| 4773 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 4774 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4775 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4776 | |
| 4777 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4778 | // Member Pointer Evaluation |
| 4779 | //===----------------------------------------------------------------------===// |
| 4780 | |
| 4781 | namespace { |
| 4782 | class MemberPointerExprEvaluator |
| 4783 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 4784 | MemberPtr &Result; |
| 4785 | |
| 4786 | bool Success(const ValueDecl *D) { |
| 4787 | Result = MemberPtr(D); |
| 4788 | return true; |
| 4789 | } |
| 4790 | public: |
| 4791 | |
| 4792 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 4793 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 4794 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4795 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4796 | Result.setFrom(V); |
| 4797 | return true; |
| 4798 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4799 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4800 | return Success((const ValueDecl*)0); |
| 4801 | } |
| 4802 | |
| 4803 | bool VisitCastExpr(const CastExpr *E); |
| 4804 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 4805 | }; |
| 4806 | } // end anonymous namespace |
| 4807 | |
| 4808 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 4809 | EvalInfo &Info) { |
| 4810 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 4811 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 4812 | } |
| 4813 | |
| 4814 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4815 | switch (E->getCastKind()) { |
| 4816 | default: |
| 4817 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4818 | |
| 4819 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4820 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4821 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4822 | |
| 4823 | case CK_BaseToDerivedMemberPointer: { |
| 4824 | if (!Visit(E->getSubExpr())) |
| 4825 | return false; |
| 4826 | if (E->path_empty()) |
| 4827 | return true; |
| 4828 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 4829 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 4830 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 4831 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 4832 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 4833 | PathI != PathE; ++PathI) { |
| 4834 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 4835 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4836 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4837 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4838 | } |
| 4839 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 4840 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4841 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4842 | return true; |
| 4843 | } |
| 4844 | |
| 4845 | case CK_DerivedToBaseMemberPointer: |
| 4846 | if (!Visit(E->getSubExpr())) |
| 4847 | return false; |
| 4848 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 4849 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 4850 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 4851 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4852 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4853 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4854 | } |
| 4855 | return true; |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4860 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 4861 | // member can be formed. |
| 4862 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 4863 | } |
| 4864 | |
| 4865 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4866 | // Record Evaluation |
| 4867 | //===----------------------------------------------------------------------===// |
| 4868 | |
| 4869 | namespace { |
| 4870 | class RecordExprEvaluator |
| 4871 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 4872 | const LValue &This; |
| 4873 | APValue &Result; |
| 4874 | public: |
| 4875 | |
| 4876 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 4877 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 4878 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4879 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4880 | Result = V; |
| 4881 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4882 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4883 | bool ZeroInitialization(const Expr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4884 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4885 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4886 | bool VisitInitListExpr(const InitListExpr *E); |
| 4887 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 4888 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4889 | }; |
| 4890 | } |
| 4891 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4892 | /// Perform zero-initialization on an object of non-union class type. |
| 4893 | /// C++11 [dcl.init]p5: |
| 4894 | /// To zero-initialize an object or reference of type T means: |
| 4895 | /// [...] |
| 4896 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 4897 | /// each non-static data member and each base-class subobject is |
| 4898 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4899 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 4900 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4901 | const LValue &This, APValue &Result) { |
| 4902 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 4903 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 4904 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 4905 | std::distance(RD->field_begin(), RD->field_end())); |
| 4906 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4907 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4908 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4909 | |
| 4910 | if (CD) { |
| 4911 | unsigned Index = 0; |
| 4912 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4913 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4914 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 4915 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4916 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 4917 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4918 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4919 | Result.getStructBase(Index))) |
| 4920 | return false; |
| 4921 | } |
| 4922 | } |
| 4923 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4924 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 4925 | I != End; ++I) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4926 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4927 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4928 | continue; |
| 4929 | |
| 4930 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4931 | if (!HandleLValueMember(Info, E, Subobject, *I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4932 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4933 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4934 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4935 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4936 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4937 | return false; |
| 4938 | } |
| 4939 | |
| 4940 | return true; |
| 4941 | } |
| 4942 | |
| 4943 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 4944 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4945 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4946 | if (RD->isUnion()) { |
| 4947 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 4948 | // object's first non-static named data member is zero-initialized |
| 4949 | RecordDecl::field_iterator I = RD->field_begin(); |
| 4950 | if (I == RD->field_end()) { |
| 4951 | Result = APValue((const FieldDecl*)0); |
| 4952 | return true; |
| 4953 | } |
| 4954 | |
| 4955 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4956 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4957 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4958 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4959 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4960 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4963 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4964 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4965 | return false; |
| 4966 | } |
| 4967 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4968 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4969 | } |
| 4970 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4971 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4972 | switch (E->getCastKind()) { |
| 4973 | default: |
| 4974 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4975 | |
| 4976 | case CK_ConstructorConversion: |
| 4977 | return Visit(E->getSubExpr()); |
| 4978 | |
| 4979 | case CK_DerivedToBase: |
| 4980 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4981 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4982 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4983 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4984 | if (!DerivedObject.isStruct()) |
| 4985 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4986 | |
| 4987 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 4988 | APValue *Value = &DerivedObject; |
| 4989 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 4990 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 4991 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 4992 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 4993 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4994 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 4995 | RD = Base; |
| 4996 | } |
| 4997 | Result = *Value; |
| 4998 | return true; |
| 4999 | } |
| 5000 | } |
| 5001 | } |
| 5002 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5003 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5004 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5005 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5006 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5007 | |
| 5008 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5009 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 5010 | Result = APValue(Field); |
| 5011 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5012 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5013 | |
| 5014 | // If the initializer list for a union does not contain any elements, the |
| 5015 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5016 | // FIXME: The element should be initialized from an initializer list. |
| 5017 | // Is this difference ever observable for initializer lists which |
| 5018 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5019 | ImplicitValueInitExpr VIE(Field->getType()); |
| 5020 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 5021 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5022 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5023 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 5024 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5025 | |
| 5026 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5027 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5028 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 5029 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5030 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 5034 | "initializer list for class with base classes"); |
| 5035 | Result = APValue(APValue::UninitStruct(), 0, |
| 5036 | std::distance(RD->field_begin(), RD->field_end())); |
| 5037 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5038 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5039 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 5040 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 5041 | // Anonymous bit-fields are not considered members of the class for |
| 5042 | // purposes of aggregate initialization. |
| 5043 | if (Field->isUnnamedBitfield()) |
| 5044 | continue; |
| 5045 | |
| 5046 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5047 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5048 | bool HaveInit = ElementNo < E->getNumInits(); |
| 5049 | |
| 5050 | // FIXME: Diagnostics here should point to the end of the initializer |
| 5051 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5052 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5053 | Subobject, *Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5054 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5055 | |
| 5056 | // Perform an implicit value-initialization for members beyond the end of |
| 5057 | // the initializer list. |
| 5058 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5059 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5060 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5061 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5062 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5063 | isa<CXXDefaultInitExpr>(Init)); |
| 5064 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 5065 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 5066 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 5067 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
| 5068 | FieldVal, *Field))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5069 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5070 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5071 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5075 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5079 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5080 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 5081 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5082 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5083 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5084 | // If we've already performed zero-initialization, we're already done. |
| 5085 | if (!Result.isUninit()) |
| 5086 | return true; |
| 5087 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5088 | if (ZeroInit) |
| 5089 | return ZeroInitialization(E); |
| 5090 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5091 | const CXXRecordDecl *RD = FD->getParent(); |
| 5092 | if (RD->isUnion()) |
| 5093 | Result = APValue((FieldDecl*)0); |
| 5094 | else |
| 5095 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 5096 | std::distance(RD->field_begin(), RD->field_end())); |
| 5097 | return true; |
| 5098 | } |
| 5099 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5100 | const FunctionDecl *Definition = 0; |
| 5101 | FD->getBody(Definition); |
| 5102 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5103 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5104 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5105 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5106 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5107 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5108 | if (const MaterializeTemporaryExpr *ME |
| 5109 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 5110 | return Visit(ME->GetTemporaryExpr()); |
| 5111 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5112 | if (ZeroInit && !ZeroInitialization(E)) |
| 5113 | return false; |
| 5114 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5115 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5116 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5117 | cast<CXXConstructorDecl>(Definition), Info, |
| 5118 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5121 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 5122 | const CXXStdInitializerListExpr *E) { |
| 5123 | const ConstantArrayType *ArrayType = |
| 5124 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 5125 | |
| 5126 | LValue Array; |
| 5127 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 5128 | return false; |
| 5129 | |
| 5130 | // Get a pointer to the first element of the array. |
| 5131 | Array.addArray(Info, E, ArrayType); |
| 5132 | |
| 5133 | // FIXME: Perform the checks on the field types in SemaInit. |
| 5134 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 5135 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 5136 | if (Field == Record->field_end()) |
| 5137 | return Error(E); |
| 5138 | |
| 5139 | // Start pointer. |
| 5140 | if (!Field->getType()->isPointerType() || |
| 5141 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5142 | ArrayType->getElementType())) |
| 5143 | return Error(E); |
| 5144 | |
| 5145 | // FIXME: What if the initializer_list type has base classes, etc? |
| 5146 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 5147 | Array.moveInto(Result.getStructField(0)); |
| 5148 | |
| 5149 | if (++Field == Record->field_end()) |
| 5150 | return Error(E); |
| 5151 | |
| 5152 | if (Field->getType()->isPointerType() && |
| 5153 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5154 | ArrayType->getElementType())) { |
| 5155 | // End pointer. |
| 5156 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 5157 | ArrayType->getElementType(), |
| 5158 | ArrayType->getSize().getZExtValue())) |
| 5159 | return false; |
| 5160 | Array.moveInto(Result.getStructField(1)); |
| 5161 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 5162 | // Length. |
| 5163 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 5164 | else |
| 5165 | return Error(E); |
| 5166 | |
| 5167 | if (++Field != Record->field_end()) |
| 5168 | return Error(E); |
| 5169 | |
| 5170 | return true; |
| 5171 | } |
| 5172 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5173 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 5174 | APValue &Result, EvalInfo &Info) { |
| 5175 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5176 | "can't evaluate expression as a record rvalue"); |
| 5177 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 5178 | } |
| 5179 | |
| 5180 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5181 | // Temporary Evaluation |
| 5182 | // |
| 5183 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 5184 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 5185 | // materialized so that a reference can bind to it. |
| 5186 | //===----------------------------------------------------------------------===// |
| 5187 | namespace { |
| 5188 | class TemporaryExprEvaluator |
| 5189 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 5190 | public: |
| 5191 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 5192 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 5193 | |
| 5194 | /// Visit an expression which constructs the value of this temporary. |
| 5195 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5196 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5197 | return EvaluateInPlace(Info.CurrentCall->createTemporary(E, false), |
| 5198 | Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | bool VisitCastExpr(const CastExpr *E) { |
| 5202 | switch (E->getCastKind()) { |
| 5203 | default: |
| 5204 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5205 | |
| 5206 | case CK_ConstructorConversion: |
| 5207 | return VisitConstructExpr(E->getSubExpr()); |
| 5208 | } |
| 5209 | } |
| 5210 | bool VisitInitListExpr(const InitListExpr *E) { |
| 5211 | return VisitConstructExpr(E); |
| 5212 | } |
| 5213 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5214 | return VisitConstructExpr(E); |
| 5215 | } |
| 5216 | bool VisitCallExpr(const CallExpr *E) { |
| 5217 | return VisitConstructExpr(E); |
| 5218 | } |
| 5219 | }; |
| 5220 | } // end anonymous namespace |
| 5221 | |
| 5222 | /// Evaluate an expression of record type as a temporary. |
| 5223 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5224 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5225 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 5226 | } |
| 5227 | |
| 5228 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5229 | // Vector Evaluation |
| 5230 | //===----------------------------------------------------------------------===// |
| 5231 | |
| 5232 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5233 | class VectorExprEvaluator |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5234 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 5235 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5236 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5237 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5238 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 5239 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5240 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5241 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 5242 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 5243 | // FIXME: remove this APValue copy. |
| 5244 | Result = APValue(V.data(), V.size()); |
| 5245 | return true; |
| 5246 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5247 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5248 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5249 | Result = V; |
| 5250 | return true; |
| 5251 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5252 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5254 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5255 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5256 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5257 | bool VisitInitListExpr(const InitListExpr *E); |
| 5258 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5259 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5260 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5261 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5262 | }; |
| 5263 | } // end anonymous namespace |
| 5264 | |
| 5265 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5266 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5267 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5270 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 5271 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5272 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 5274 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5275 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5276 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5277 | switch (E->getCastKind()) { |
| 5278 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5279 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5280 | if (SETy->isIntegerType()) { |
| 5281 | APSInt IntResult; |
| 5282 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5283 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5284 | Val = APValue(IntResult); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5285 | } else if (SETy->isRealFloatingType()) { |
| 5286 | APFloat F(0.0); |
| 5287 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5288 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5289 | Val = APValue(F); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5290 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5291 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5292 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5293 | |
| 5294 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5295 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 5296 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5297 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5298 | case CK_BitCast: { |
| 5299 | // Evaluate the operand into an APInt we can extract from. |
| 5300 | llvm::APInt SValInt; |
| 5301 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 5302 | return false; |
| 5303 | // Extract the elements |
| 5304 | QualType EltTy = VTy->getElementType(); |
| 5305 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 5306 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 5307 | SmallVector<APValue, 4> Elts; |
| 5308 | if (EltTy->isRealFloatingType()) { |
| 5309 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5310 | unsigned FloatEltSize = EltSize; |
| 5311 | if (&Sem == &APFloat::x87DoubleExtended) |
| 5312 | FloatEltSize = 80; |
| 5313 | for (unsigned i = 0; i < NElts; i++) { |
| 5314 | llvm::APInt Elt; |
| 5315 | if (BigEndian) |
| 5316 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 5317 | else |
| 5318 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 5319 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5320 | } |
| 5321 | } else if (EltTy->isIntegerType()) { |
| 5322 | for (unsigned i = 0; i < NElts; i++) { |
| 5323 | llvm::APInt Elt; |
| 5324 | if (BigEndian) |
| 5325 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 5326 | else |
| 5327 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 5328 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 5329 | } |
| 5330 | } else { |
| 5331 | return Error(E); |
| 5332 | } |
| 5333 | return Success(Elts, E); |
| 5334 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5335 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5336 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5337 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5340 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5341 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5342 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5343 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5344 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5345 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5346 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5347 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5348 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5349 | // The number of initializers can be less than the number of |
| 5350 | // vector elements. For OpenCL, this can be due to nested vector |
| 5351 | // initialization. For GCC compatibility, missing trailing elements |
| 5352 | // should be initialized with zeroes. |
| 5353 | unsigned CountInits = 0, CountElts = 0; |
| 5354 | while (CountElts < NumElements) { |
| 5355 | // Handle nested vector initialization. |
| 5356 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 5357 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5358 | APValue v; |
| 5359 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 5360 | return Error(E); |
| 5361 | unsigned vlen = v.getVectorLength(); |
| 5362 | for (unsigned j = 0; j < vlen; j++) |
| 5363 | Elements.push_back(v.getVectorElt(j)); |
| 5364 | CountElts += vlen; |
| 5365 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5366 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5367 | if (CountInits < NumInits) { |
| 5368 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5369 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5370 | } else // trailing integer zero. |
| 5371 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 5372 | Elements.push_back(APValue(sInt)); |
| 5373 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5374 | } else { |
| 5375 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5376 | if (CountInits < NumInits) { |
| 5377 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5378 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5379 | } else // trailing float zero. |
| 5380 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 5381 | Elements.push_back(APValue(f)); |
| 5382 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 5383 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5384 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5385 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5386 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5389 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5390 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5391 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5392 | QualType EltTy = VT->getElementType(); |
| 5393 | APValue ZeroElement; |
| 5394 | if (EltTy->isIntegerType()) |
| 5395 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 5396 | else |
| 5397 | ZeroElement = |
| 5398 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 5399 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5400 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5401 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5404 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5405 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5406 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5409 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5410 | // Array Evaluation |
| 5411 | //===----------------------------------------------------------------------===// |
| 5412 | |
| 5413 | namespace { |
| 5414 | class ArrayExprEvaluator |
| 5415 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5416 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5417 | APValue &Result; |
| 5418 | public: |
| 5419 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5420 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 5421 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5422 | |
| 5423 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5424 | assert((V.isArray() || V.isLValue()) && |
| 5425 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5426 | Result = V; |
| 5427 | return true; |
| 5428 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5429 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5430 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5431 | const ConstantArrayType *CAT = |
| 5432 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5433 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5434 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5435 | |
| 5436 | Result = APValue(APValue::UninitArray(), 0, |
| 5437 | CAT->getSize().getZExtValue()); |
| 5438 | if (!Result.hasArrayFiller()) return true; |
| 5439 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5440 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5441 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5442 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5443 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5444 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5447 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5448 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5449 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5450 | const LValue &Subobject, |
| 5451 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5452 | }; |
| 5453 | } // end anonymous namespace |
| 5454 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5455 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 5456 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5457 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5458 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5462 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5463 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5464 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5465 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5466 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 5467 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 5468 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5469 | LValue LV; |
| 5470 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 5471 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5472 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5473 | LV.moveInto(Val); |
| 5474 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5477 | bool Success = true; |
| 5478 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5479 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 5480 | "zero-initialized array shouldn't have any initialized elts"); |
| 5481 | APValue Filler; |
| 5482 | if (Result.isArray() && Result.hasArrayFiller()) |
| 5483 | Filler = Result.getArrayFiller(); |
| 5484 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5485 | unsigned NumEltsToInit = E->getNumInits(); |
| 5486 | unsigned NumElts = CAT->getSize().getZExtValue(); |
| 5487 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : 0; |
| 5488 | |
| 5489 | // If the initializer might depend on the array index, run it for each |
| 5490 | // array element. For now, just whitelist non-class value-initialization. |
| 5491 | if (NumEltsToInit != NumElts && !isa<ImplicitValueInitExpr>(FillerExpr)) |
| 5492 | NumEltsToInit = NumElts; |
| 5493 | |
| 5494 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5495 | |
| 5496 | // If the array was previously zero-initialized, preserve the |
| 5497 | // zero-initialized values. |
| 5498 | if (!Filler.isUninit()) { |
| 5499 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 5500 | Result.getArrayInitializedElt(I) = Filler; |
| 5501 | if (Result.hasArrayFiller()) |
| 5502 | Result.getArrayFiller() = Filler; |
| 5503 | } |
| 5504 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5505 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5506 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5507 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 5508 | const Expr *Init = |
| 5509 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5510 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5511 | Info, Subobject, Init) || |
| 5512 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5513 | CAT->getElementType(), 1)) { |
| 5514 | if (!Info.keepEvaluatingAfterFailure()) |
| 5515 | return false; |
| 5516 | Success = false; |
| 5517 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5518 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5519 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5520 | if (!Result.hasArrayFiller()) |
| 5521 | return Success; |
| 5522 | |
| 5523 | // If we get here, we have a trivial filler, which we can just evaluate |
| 5524 | // once and splat over the rest of the array elements. |
| 5525 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 5526 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 5527 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5530 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5531 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 5532 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5533 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5534 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5535 | const LValue &Subobject, |
| 5536 | APValue *Value, |
| 5537 | QualType Type) { |
| 5538 | bool HadZeroInit = !Value->isUninit(); |
| 5539 | |
| 5540 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 5541 | unsigned N = CAT->getSize().getZExtValue(); |
| 5542 | |
| 5543 | // Preserve the array filler if we had prior zero-initialization. |
| 5544 | APValue Filler = |
| 5545 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 5546 | : APValue(); |
| 5547 | |
| 5548 | *Value = APValue(APValue::UninitArray(), N, N); |
| 5549 | |
| 5550 | if (HadZeroInit) |
| 5551 | for (unsigned I = 0; I != N; ++I) |
| 5552 | Value->getArrayInitializedElt(I) = Filler; |
| 5553 | |
| 5554 | // Initialize the elements. |
| 5555 | LValue ArrayElt = Subobject; |
| 5556 | ArrayElt.addArray(Info, E, CAT); |
| 5557 | for (unsigned I = 0; I != N; ++I) |
| 5558 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 5559 | CAT->getElementType()) || |
| 5560 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 5561 | CAT->getElementType(), 1)) |
| 5562 | return false; |
| 5563 | |
| 5564 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5565 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5566 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5567 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 5568 | return Error(E); |
| 5569 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5570 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5571 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5572 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5573 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5574 | if (HadZeroInit) |
| 5575 | return true; |
| 5576 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5577 | if (ZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5578 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5579 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5582 | const CXXRecordDecl *RD = FD->getParent(); |
| 5583 | if (RD->isUnion()) |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5584 | *Value = APValue((FieldDecl*)0); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5585 | else |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5586 | *Value = |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5587 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 5588 | std::distance(RD->field_begin(), RD->field_end())); |
| 5589 | return true; |
| 5590 | } |
| 5591 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5592 | const FunctionDecl *Definition = 0; |
| 5593 | FD->getBody(Definition); |
| 5594 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5595 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5596 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5597 | |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5598 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5599 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5600 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5601 | return false; |
| 5602 | } |
| 5603 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5604 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5605 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5606 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5607 | Info, *Value); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5610 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5611 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5612 | // |
| 5613 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 5614 | // types and back in constant folding. Integer values are thus represented |
| 5615 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5616 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5617 | |
| 5618 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5619 | class IntExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5620 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5621 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5622 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5623 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5624 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5625 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5626 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5627 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5628 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5629 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5630 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5631 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5632 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5633 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5634 | return true; |
| 5635 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5636 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 5637 | return Success(SI, E, Result); |
| 5638 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5639 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5640 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5641 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5642 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5643 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5644 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5645 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5646 | Result.getInt().setIsUnsigned( |
| 5647 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5648 | return true; |
| 5649 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5650 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 5651 | return Success(I, E, Result); |
| 5652 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5653 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5654 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5655 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5656 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5657 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5658 | return true; |
| 5659 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5660 | bool Success(uint64_t Value, const Expr *E) { |
| 5661 | return Success(Value, E, Result); |
| 5662 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5663 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5664 | bool Success(CharUnits Size, const Expr *E) { |
| 5665 | return Success(Size.getQuantity(), E); |
| 5666 | } |
| 5667 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5668 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 5669 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 5670 | Result = V; |
| 5671 | return true; |
| 5672 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5673 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 5674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5675 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5676 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5677 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5678 | //===--------------------------------------------------------------------===// |
| 5679 | // Visitor Methods |
| 5680 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5681 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5682 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5683 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5684 | } |
| 5685 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5686 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5687 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5688 | |
| 5689 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 5690 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5691 | if (CheckReferencedDecl(E, E->getDecl())) |
| 5692 | return true; |
| 5693 | |
| 5694 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5695 | } |
| 5696 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5697 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5698 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5699 | return true; |
| 5700 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5701 | |
| 5702 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5705 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5706 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5707 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5708 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 5709 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5710 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5711 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5712 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5713 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5714 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5715 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5716 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5717 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 5718 | return Success(E->getValue(), E); |
| 5719 | } |
| 5720 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5721 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 5722 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5723 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5726 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 5727 | return Success(E->getValue(), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5730 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 5731 | return Success(E->getValue(), E); |
| 5732 | } |
| 5733 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5734 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 5735 | return Success(E->getValue(), E); |
| 5736 | } |
| 5737 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5738 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 5739 | return Success(E->getValue(), E); |
| 5740 | } |
| 5741 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5742 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 5743 | return Success(E->getValue(), E); |
| 5744 | } |
| 5745 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5746 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5747 | bool VisitUnaryImag(const UnaryOperator *E); |
| 5748 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5749 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5750 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 5751 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5752 | private: |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5753 | CharUnits GetAlignOfExpr(const Expr *E); |
| 5754 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5755 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5756 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5757 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5758 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5759 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5760 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5761 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 5762 | /// produce either the integer value or a pointer. |
| 5763 | /// |
| 5764 | /// GCC has a heinous extension which folds casts between pointer types and |
| 5765 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 5766 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 5767 | /// Some simple arithmetic on such values is supported (they are treated much |
| 5768 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5769 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5770 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5771 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5772 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5773 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5774 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5775 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5776 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5777 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5778 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5779 | if (!Val.isInt()) { |
| 5780 | // FIXME: It would be better to produce the diagnostic for casting |
| 5781 | // a pointer to an integer. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5782 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5783 | return false; |
| 5784 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5785 | Result = Val.getInt(); |
| 5786 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5787 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5788 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5789 | /// Check whether the given declaration can be directly converted to an integral |
| 5790 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 5791 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5792 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5793 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 5794 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5795 | // Check for signedness/width mismatches between E type and ECD value. |
| 5796 | bool SameSign = (ECD->getInitVal().isSigned() |
| 5797 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 5798 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 5799 | == Info.Ctx.getIntWidth(E->getType())); |
| 5800 | if (SameSign && SameWidth) |
| 5801 | return Success(ECD->getInitVal(), E); |
| 5802 | else { |
| 5803 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 5804 | // by computing a new value matching the type of E. |
| 5805 | llvm::APSInt Val = ECD->getInitVal(); |
| 5806 | if (!SameSign) |
| 5807 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 5808 | if (!SameWidth) |
| 5809 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 5810 | return Success(Val, E); |
| 5811 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 5812 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5813 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5816 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 5817 | /// as GCC. |
| 5818 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 5819 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5820 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5821 | enum gcc_type_class { |
| 5822 | no_type_class = -1, |
| 5823 | void_type_class, integer_type_class, char_type_class, |
| 5824 | enumeral_type_class, boolean_type_class, |
| 5825 | pointer_type_class, reference_type_class, offset_type_class, |
| 5826 | real_type_class, complex_type_class, |
| 5827 | function_type_class, method_type_class, |
| 5828 | record_type_class, union_type_class, |
| 5829 | array_type_class, string_type_class, |
| 5830 | lang_type_class |
| 5831 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5832 | |
| 5833 | // 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] | 5834 | // ideal, however it is what gcc does. |
| 5835 | if (E->getNumArgs() == 0) |
| 5836 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5837 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5838 | QualType ArgTy = E->getArg(0)->getType(); |
| 5839 | if (ArgTy->isVoidType()) |
| 5840 | return void_type_class; |
| 5841 | else if (ArgTy->isEnumeralType()) |
| 5842 | return enumeral_type_class; |
| 5843 | else if (ArgTy->isBooleanType()) |
| 5844 | return boolean_type_class; |
| 5845 | else if (ArgTy->isCharType()) |
| 5846 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 5847 | else if (ArgTy->isIntegerType()) |
| 5848 | return integer_type_class; |
| 5849 | else if (ArgTy->isPointerType()) |
| 5850 | return pointer_type_class; |
| 5851 | else if (ArgTy->isReferenceType()) |
| 5852 | return reference_type_class; |
| 5853 | else if (ArgTy->isRealType()) |
| 5854 | return real_type_class; |
| 5855 | else if (ArgTy->isComplexType()) |
| 5856 | return complex_type_class; |
| 5857 | else if (ArgTy->isFunctionType()) |
| 5858 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 5859 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5860 | return record_type_class; |
| 5861 | else if (ArgTy->isUnionType()) |
| 5862 | return union_type_class; |
| 5863 | else if (ArgTy->isArrayType()) |
| 5864 | return array_type_class; |
| 5865 | else if (ArgTy->isUnionType()) |
| 5866 | return union_type_class; |
| 5867 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5868 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5871 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 5872 | /// __builtin_constant_p when applied to the given lvalue. |
| 5873 | /// |
| 5874 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 5875 | /// character of a string literal. |
| 5876 | template<typename LValue> |
| 5877 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 5878 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5879 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 5880 | } |
| 5881 | |
| 5882 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 5883 | /// GCC as we can manage. |
| 5884 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 5885 | QualType ArgType = Arg->getType(); |
| 5886 | |
| 5887 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 5888 | // are not precisely documented, but are as follows: |
| 5889 | // |
| 5890 | // - If the operand is of integral, floating, complex or enumeration type, |
| 5891 | // and can be folded to a known value of that type, it returns 1. |
| 5892 | // - If the operand and can be folded to a pointer to the first character |
| 5893 | // of a string literal (or such a pointer cast to an integral type), it |
| 5894 | // returns 1. |
| 5895 | // |
| 5896 | // Otherwise, it returns 0. |
| 5897 | // |
| 5898 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 5899 | // its support for this does not currently work. |
| 5900 | if (ArgType->isIntegralOrEnumerationType()) { |
| 5901 | Expr::EvalResult Result; |
| 5902 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 5903 | return false; |
| 5904 | |
| 5905 | APValue &V = Result.Val; |
| 5906 | if (V.getKind() == APValue::Int) |
| 5907 | return true; |
| 5908 | |
| 5909 | return EvaluateBuiltinConstantPForLValue(V); |
| 5910 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 5911 | return Arg->isEvaluatable(Ctx); |
| 5912 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 5913 | LValue LV; |
| 5914 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 5915 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5916 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 5917 | : EvaluatePointer(Arg, LV, Info)) && |
| 5918 | !Status.HasSideEffects) |
| 5919 | return EvaluateBuiltinConstantPForLValue(LV); |
| 5920 | } |
| 5921 | |
| 5922 | // Anything else isn't considered to be sufficiently constant. |
| 5923 | return false; |
| 5924 | } |
| 5925 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5926 | /// Retrieves the "underlying object type" of the given expression, |
| 5927 | /// as used by __builtin_object_size. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5928 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 5929 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 5930 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5931 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5932 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 5933 | if (isa<CompoundLiteralExpr>(E)) |
| 5934 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | return QualType(); |
| 5938 | } |
| 5939 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5940 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5941 | LValue Base; |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5942 | |
| 5943 | { |
| 5944 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 5945 | // If there are any, but we can determine the pointed-to object anyway, then |
| 5946 | // ignore the side-effects. |
| 5947 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 5948 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 5949 | return false; |
| 5950 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5951 | |
| 5952 | // If we can prove the base is null, lower to zero now. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5953 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5954 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5955 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5956 | if (T.isNull() || |
| 5957 | T->isIncompleteType() || |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5958 | T->isFunctionType() || |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5959 | T->isVariablyModifiedType() || |
| 5960 | T->isDependentType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5961 | return Error(E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5962 | |
| 5963 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 5964 | CharUnits Offset = Base.getLValueOffset(); |
| 5965 | |
| 5966 | if (!Offset.isNegative() && Offset <= Size) |
| 5967 | Size -= Offset; |
| 5968 | else |
| 5969 | Size = CharUnits::Zero(); |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5970 | return Success(Size, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5973 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5974 | switch (unsigned BuiltinOp = E->isBuiltinCall()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5975 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5976 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5977 | |
| 5978 | case Builtin::BI__builtin_object_size: { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5979 | if (TryEvaluateBuiltinObjectSize(E)) |
| 5980 | return true; |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5981 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 5982 | // If evaluating the argument has side-effects, we can't determine the size |
| 5983 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 5984 | // handle all cases where the expression has side-effects. |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 5985 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5986 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | 4f10559 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 5987 | return Success(-1ULL, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5988 | return Success(0, E); |
| 5989 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 5990 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5991 | // Expression had no side effects, but we couldn't statically determine the |
| 5992 | // size of the referenced object. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5993 | return Error(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 5996 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 5997 | case Builtin::BI__builtin_bswap32: |
| 5998 | case Builtin::BI__builtin_bswap64: { |
| 5999 | APSInt Val; |
| 6000 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6001 | return false; |
| 6002 | |
| 6003 | return Success(Val.byteSwap(), E); |
| 6004 | } |
| 6005 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6006 | case Builtin::BI__builtin_classify_type: |
| 6007 | return Success(EvaluateBuiltinClassifyType(E), E); |
| 6008 | |
| 6009 | // FIXME: BI__builtin_clrsb |
| 6010 | // FIXME: BI__builtin_clrsbl |
| 6011 | // FIXME: BI__builtin_clrsbll |
| 6012 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6013 | case Builtin::BI__builtin_clz: |
| 6014 | case Builtin::BI__builtin_clzl: |
| 6015 | case Builtin::BI__builtin_clzll: { |
| 6016 | APSInt Val; |
| 6017 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6018 | return false; |
| 6019 | if (!Val) |
| 6020 | return Error(E); |
| 6021 | |
| 6022 | return Success(Val.countLeadingZeros(), E); |
| 6023 | } |
| 6024 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6025 | case Builtin::BI__builtin_constant_p: |
| 6026 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 6027 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6028 | case Builtin::BI__builtin_ctz: |
| 6029 | case Builtin::BI__builtin_ctzl: |
| 6030 | case Builtin::BI__builtin_ctzll: { |
| 6031 | APSInt Val; |
| 6032 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6033 | return false; |
| 6034 | if (!Val) |
| 6035 | return Error(E); |
| 6036 | |
| 6037 | return Success(Val.countTrailingZeros(), E); |
| 6038 | } |
| 6039 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6040 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 6041 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 6042 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 6043 | return Success(Operand, E); |
| 6044 | } |
| 6045 | |
| 6046 | case Builtin::BI__builtin_expect: |
| 6047 | return Visit(E->getArg(0)); |
| 6048 | |
| 6049 | case Builtin::BI__builtin_ffs: |
| 6050 | case Builtin::BI__builtin_ffsl: |
| 6051 | case Builtin::BI__builtin_ffsll: { |
| 6052 | APSInt Val; |
| 6053 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6054 | return false; |
| 6055 | |
| 6056 | unsigned N = Val.countTrailingZeros(); |
| 6057 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 6058 | } |
| 6059 | |
| 6060 | case Builtin::BI__builtin_fpclassify: { |
| 6061 | APFloat Val(0.0); |
| 6062 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 6063 | return false; |
| 6064 | unsigned Arg; |
| 6065 | switch (Val.getCategory()) { |
| 6066 | case APFloat::fcNaN: Arg = 0; break; |
| 6067 | case APFloat::fcInfinity: Arg = 1; break; |
| 6068 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 6069 | case APFloat::fcZero: Arg = 4; break; |
| 6070 | } |
| 6071 | return Visit(E->getArg(Arg)); |
| 6072 | } |
| 6073 | |
| 6074 | case Builtin::BI__builtin_isinf_sign: { |
| 6075 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 6076 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6077 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 6078 | } |
| 6079 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 6080 | case Builtin::BI__builtin_isinf: { |
| 6081 | APFloat Val(0.0); |
| 6082 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6083 | Success(Val.isInfinity() ? 1 : 0, E); |
| 6084 | } |
| 6085 | |
| 6086 | case Builtin::BI__builtin_isfinite: { |
| 6087 | APFloat Val(0.0); |
| 6088 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6089 | Success(Val.isFinite() ? 1 : 0, E); |
| 6090 | } |
| 6091 | |
| 6092 | case Builtin::BI__builtin_isnan: { |
| 6093 | APFloat Val(0.0); |
| 6094 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6095 | Success(Val.isNaN() ? 1 : 0, E); |
| 6096 | } |
| 6097 | |
| 6098 | case Builtin::BI__builtin_isnormal: { |
| 6099 | APFloat Val(0.0); |
| 6100 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6101 | Success(Val.isNormal() ? 1 : 0, E); |
| 6102 | } |
| 6103 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6104 | case Builtin::BI__builtin_parity: |
| 6105 | case Builtin::BI__builtin_parityl: |
| 6106 | case Builtin::BI__builtin_parityll: { |
| 6107 | APSInt Val; |
| 6108 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6109 | return false; |
| 6110 | |
| 6111 | return Success(Val.countPopulation() % 2, E); |
| 6112 | } |
| 6113 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6114 | case Builtin::BI__builtin_popcount: |
| 6115 | case Builtin::BI__builtin_popcountl: |
| 6116 | case Builtin::BI__builtin_popcountll: { |
| 6117 | APSInt Val; |
| 6118 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6119 | return false; |
| 6120 | |
| 6121 | return Success(Val.countPopulation(), E); |
| 6122 | } |
| 6123 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6124 | case Builtin::BIstrlen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6125 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6126 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6127 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6128 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 6129 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6130 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6131 | // Fall through. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6132 | case Builtin::BI__builtin_strlen: |
| 6133 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 6134 | // expressions when the argument is a string literal. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6135 | if (const StringLiteral *S |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6136 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 6137 | // The string literal may have embedded null characters. Find the first |
| 6138 | // one and truncate there. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6139 | StringRef Str = S->getString(); |
| 6140 | StringRef::size_type Pos = Str.find(0); |
| 6141 | if (Pos != StringRef::npos) |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6142 | Str = Str.substr(0, Pos); |
| 6143 | |
| 6144 | return Success(Str.size(), E); |
| 6145 | } |
| 6146 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6147 | return Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6148 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6149 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 6150 | case Builtin::BI__atomic_is_lock_free: |
| 6151 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6152 | APSInt SizeVal; |
| 6153 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 6154 | return false; |
| 6155 | |
| 6156 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 6157 | // of two less than the maximum inline atomic width, we know it is |
| 6158 | // lock-free. If the size isn't a power of two, or greater than the |
| 6159 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 6160 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 6161 | // the answer can only be determined at runtime; for example, 16-byte |
| 6162 | // atomics have lock-free implementations on some, but not all, |
| 6163 | // x86-64 processors. |
| 6164 | |
| 6165 | // Check power-of-two. |
| 6166 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6167 | if (Size.isPowerOfTwo()) { |
| 6168 | // Check against inlining width. |
| 6169 | unsigned InlineWidthBits = |
| 6170 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 6171 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 6172 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 6173 | Size == CharUnits::One() || |
| 6174 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 6175 | Expr::NPC_NeverValueDependent)) |
| 6176 | // OK, we will inline appropriately-aligned operations of this size, |
| 6177 | // and _Atomic(T) is appropriately-aligned. |
| 6178 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6179 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6180 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 6181 | castAs<PointerType>()->getPointeeType(); |
| 6182 | if (!PointeeType->isIncompleteType() && |
| 6183 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 6184 | // OK, we will inline operations on this object. |
| 6185 | return Success(1, E); |
| 6186 | } |
| 6187 | } |
| 6188 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6189 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6190 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 6191 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6192 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6193 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6194 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6195 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6196 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 6197 | if (!A.getLValueBase()) |
| 6198 | return !B.getLValueBase(); |
| 6199 | if (!B.getLValueBase()) |
| 6200 | return false; |
| 6201 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6202 | if (A.getLValueBase().getOpaqueValue() != |
| 6203 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6204 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 6205 | if (!ADecl) |
| 6206 | return false; |
| 6207 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 6208 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6209 | return false; |
| 6210 | } |
| 6211 | |
| 6212 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6213 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6216 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6217 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6218 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 6219 | /// |
| 6220 | /// We use a data recursive algorithm for binary operators so that we are able |
| 6221 | /// to handle extreme cases of chained binary operators without causing stack |
| 6222 | /// overflow. |
| 6223 | class DataRecursiveIntBinOpEvaluator { |
| 6224 | struct EvalResult { |
| 6225 | APValue Val; |
| 6226 | bool Failed; |
| 6227 | |
| 6228 | EvalResult() : Failed(false) { } |
| 6229 | |
| 6230 | void swap(EvalResult &RHS) { |
| 6231 | Val.swap(RHS.Val); |
| 6232 | Failed = RHS.Failed; |
| 6233 | RHS.Failed = false; |
| 6234 | } |
| 6235 | }; |
| 6236 | |
| 6237 | struct Job { |
| 6238 | const Expr *E; |
| 6239 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 6240 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| 6241 | |
| 6242 | Job() : StoredInfo(0) { } |
| 6243 | void startSpeculativeEval(EvalInfo &Info) { |
| 6244 | OldEvalStatus = Info.EvalStatus; |
| 6245 | Info.EvalStatus.Diag = 0; |
| 6246 | StoredInfo = &Info; |
| 6247 | } |
| 6248 | ~Job() { |
| 6249 | if (StoredInfo) { |
| 6250 | StoredInfo->EvalStatus = OldEvalStatus; |
| 6251 | } |
| 6252 | } |
| 6253 | private: |
| 6254 | EvalInfo *StoredInfo; // non-null if status changed. |
| 6255 | Expr::EvalStatus OldEvalStatus; |
| 6256 | }; |
| 6257 | |
| 6258 | SmallVector<Job, 16> Queue; |
| 6259 | |
| 6260 | IntExprEvaluator &IntEval; |
| 6261 | EvalInfo &Info; |
| 6262 | APValue &FinalResult; |
| 6263 | |
| 6264 | public: |
| 6265 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 6266 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 6267 | |
| 6268 | /// \brief True if \param E is a binary operator that we are going to handle |
| 6269 | /// data recursively. |
| 6270 | /// We handle binary operators that are comma, logical, or that have operands |
| 6271 | /// with integral or enumeration type. |
| 6272 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 6273 | return E->getOpcode() == BO_Comma || |
| 6274 | E->isLogicalOp() || |
| 6275 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6276 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6279 | bool Traverse(const BinaryOperator *E) { |
| 6280 | enqueue(E); |
| 6281 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6282 | while (!Queue.empty()) |
| 6283 | process(PrevResult); |
| 6284 | |
| 6285 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6286 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6287 | FinalResult.swap(PrevResult.Val); |
| 6288 | return true; |
| 6289 | } |
| 6290 | |
| 6291 | private: |
| 6292 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 6293 | return IntEval.Success(Value, E, Result); |
| 6294 | } |
| 6295 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 6296 | return IntEval.Success(Value, E, Result); |
| 6297 | } |
| 6298 | bool Error(const Expr *E) { |
| 6299 | return IntEval.Error(E); |
| 6300 | } |
| 6301 | bool Error(const Expr *E, diag::kind D) { |
| 6302 | return IntEval.Error(E, D); |
| 6303 | } |
| 6304 | |
| 6305 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 6306 | return Info.CCEDiag(E, D); |
| 6307 | } |
| 6308 | |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6309 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 6310 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6311 | bool &SuppressRHSDiags); |
| 6312 | |
| 6313 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6314 | const BinaryOperator *E, APValue &Result); |
| 6315 | |
| 6316 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 6317 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 6318 | if (Result.Failed) |
| 6319 | Result.Val = APValue(); |
| 6320 | } |
| 6321 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6322 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6323 | |
| 6324 | void enqueue(const Expr *E) { |
| 6325 | E = E->IgnoreParens(); |
| 6326 | Queue.resize(Queue.size()+1); |
| 6327 | Queue.back().E = E; |
| 6328 | Queue.back().Kind = Job::AnyExprKind; |
| 6329 | } |
| 6330 | }; |
| 6331 | |
| 6332 | } |
| 6333 | |
| 6334 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6335 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6336 | bool &SuppressRHSDiags) { |
| 6337 | if (E->getOpcode() == BO_Comma) { |
| 6338 | // Ignore LHS but note if we could not evaluate it. |
| 6339 | if (LHSResult.Failed) |
| 6340 | Info.EvalStatus.HasSideEffects = true; |
| 6341 | return true; |
| 6342 | } |
| 6343 | |
| 6344 | if (E->isLogicalOp()) { |
| 6345 | bool lhsResult; |
| 6346 | if (HandleConversionToBool(LHSResult.Val, lhsResult)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6347 | // We were able to evaluate the LHS, see if we can get away with not |
| 6348 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6349 | if (lhsResult == (E->getOpcode() == BO_LOr)) { |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6350 | Success(lhsResult, E, LHSResult.Val); |
| 6351 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6352 | } |
| 6353 | } else { |
| 6354 | // Since we weren't able to evaluate the left hand side, it |
| 6355 | // must have had side effects. |
| 6356 | Info.EvalStatus.HasSideEffects = true; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6357 | |
| 6358 | // We can't evaluate the LHS; however, sometimes the result |
| 6359 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6360 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 6361 | SuppressRHSDiags = true; |
| 6362 | } |
| 6363 | |
| 6364 | return true; |
| 6365 | } |
| 6366 | |
| 6367 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6368 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6369 | |
| 6370 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6371 | return false; // Ignore RHS; |
| 6372 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6373 | return true; |
| 6374 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6375 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6376 | bool DataRecursiveIntBinOpEvaluator:: |
| 6377 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6378 | const BinaryOperator *E, APValue &Result) { |
| 6379 | if (E->getOpcode() == BO_Comma) { |
| 6380 | if (RHSResult.Failed) |
| 6381 | return false; |
| 6382 | Result = RHSResult.Val; |
| 6383 | return true; |
| 6384 | } |
| 6385 | |
| 6386 | if (E->isLogicalOp()) { |
| 6387 | bool lhsResult, rhsResult; |
| 6388 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 6389 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 6390 | |
| 6391 | if (LHSIsOK) { |
| 6392 | if (RHSIsOK) { |
| 6393 | if (E->getOpcode() == BO_LOr) |
| 6394 | return Success(lhsResult || rhsResult, E, Result); |
| 6395 | else |
| 6396 | return Success(lhsResult && rhsResult, E, Result); |
| 6397 | } |
| 6398 | } else { |
| 6399 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6400 | // We can't evaluate the LHS; however, sometimes the result |
| 6401 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6402 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6403 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6404 | } |
| 6405 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6406 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6407 | return false; |
| 6408 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6409 | |
| 6410 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6411 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6412 | |
| 6413 | if (LHSResult.Failed || RHSResult.Failed) |
| 6414 | return false; |
| 6415 | |
| 6416 | const APValue &LHSVal = LHSResult.Val; |
| 6417 | const APValue &RHSVal = RHSResult.Val; |
| 6418 | |
| 6419 | // Handle cases like (unsigned long)&a + 4. |
| 6420 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 6421 | Result = LHSVal; |
| 6422 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 6423 | RHSVal.getInt().getZExtValue()); |
| 6424 | if (E->getOpcode() == BO_Add) |
| 6425 | Result.getLValueOffset() += AdditionalOffset; |
| 6426 | else |
| 6427 | Result.getLValueOffset() -= AdditionalOffset; |
| 6428 | return true; |
| 6429 | } |
| 6430 | |
| 6431 | // Handle cases like 4 + (unsigned long)&a |
| 6432 | if (E->getOpcode() == BO_Add && |
| 6433 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 6434 | Result = RHSVal; |
| 6435 | Result.getLValueOffset() += CharUnits::fromQuantity( |
| 6436 | LHSVal.getInt().getZExtValue()); |
| 6437 | return true; |
| 6438 | } |
| 6439 | |
| 6440 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 6441 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 6442 | if (!LHSVal.getLValueOffset().isZero() || |
| 6443 | !RHSVal.getLValueOffset().isZero()) |
| 6444 | return false; |
| 6445 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6446 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6447 | if (!LHSExpr || !RHSExpr) |
| 6448 | return false; |
| 6449 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6450 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6451 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6452 | return false; |
| 6453 | // Make sure both labels come from the same function. |
| 6454 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6455 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6456 | return false; |
| 6457 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 6458 | return true; |
| 6459 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6460 | |
| 6461 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6462 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 6463 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6464 | |
| 6465 | // Set up the width and signedness manually, in case it can't be deduced |
| 6466 | // from the operation we're performing. |
| 6467 | // FIXME: Don't do this in the cases where we can deduce it. |
| 6468 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 6469 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 6470 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 6471 | RHSVal.getInt(), Value)) |
| 6472 | return false; |
| 6473 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6474 | } |
| 6475 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6476 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6477 | Job &job = Queue.back(); |
| 6478 | |
| 6479 | switch (job.Kind) { |
| 6480 | case Job::AnyExprKind: { |
| 6481 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 6482 | if (shouldEnqueue(Bop)) { |
| 6483 | job.Kind = Job::BinOpKind; |
| 6484 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6485 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | EvaluateExpr(job.E, Result); |
| 6490 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6491 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | case Job::BinOpKind: { |
| 6495 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6496 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6497 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6498 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6499 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6500 | } |
| 6501 | if (SuppressRHSDiags) |
| 6502 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6503 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6504 | job.Kind = Job::BinOpVisitedLHSKind; |
| 6505 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6506 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | case Job::BinOpVisitedLHSKind: { |
| 6510 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 6511 | EvalResult RHS; |
| 6512 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6513 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6514 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6515 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | llvm_unreachable("Invalid Job::Kind!"); |
| 6520 | } |
| 6521 | |
| 6522 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 6523 | if (E->isAssignmentOp()) |
| 6524 | return Error(E); |
| 6525 | |
| 6526 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 6527 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6528 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6529 | QualType LHSTy = E->getLHS()->getType(); |
| 6530 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6531 | |
| 6532 | if (LHSTy->isAnyComplexType()) { |
| 6533 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6534 | ComplexValue LHS, RHS; |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6535 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6536 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 6537 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6538 | return false; |
| 6539 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6540 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6541 | return false; |
| 6542 | |
| 6543 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6544 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6545 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6546 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6547 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 6548 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6549 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6550 | return Success((CR_r == APFloat::cmpEqual && |
| 6551 | CR_i == APFloat::cmpEqual), E); |
| 6552 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6553 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6554 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6555 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6556 | CR_r == APFloat::cmpLessThan || |
| 6557 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6558 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6559 | CR_i == APFloat::cmpLessThan || |
| 6560 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6561 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6562 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6563 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6564 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 6565 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 6566 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6567 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6568 | "Invalid compex comparison."); |
| 6569 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 6570 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 6571 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6572 | } |
| 6573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6574 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6575 | if (LHSTy->isRealFloatingType() && |
| 6576 | RHSTy->isRealFloatingType()) { |
| 6577 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6578 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6579 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 6580 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6581 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6582 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6583 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6584 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6585 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6586 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 6587 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6588 | switch (E->getOpcode()) { |
| 6589 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6590 | llvm_unreachable("Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6591 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6592 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6593 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6594 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6595 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6596 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6597 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6598 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6599 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6600 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6601 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6602 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6603 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6604 | || CR == APFloat::cmpLessThan |
| 6605 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6606 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6607 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6608 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 6609 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6610 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6611 | LValue LHSValue, RHSValue; |
| 6612 | |
| 6613 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 6614 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6615 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6616 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6617 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6618 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6619 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6620 | // Reject differing bases from the normal codepath; we special-case |
| 6621 | // comparisons to null. |
| 6622 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6623 | if (E->getOpcode() == BO_Sub) { |
| 6624 | // Handle &&A - &&B. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6625 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 6626 | return false; |
| 6627 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | daa09612 | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 6628 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6629 | if (!LHSExpr || !RHSExpr) |
| 6630 | return false; |
| 6631 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6632 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6633 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6634 | return false; |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 6635 | // Make sure both labels come from the same function. |
| 6636 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6637 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6638 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6639 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6640 | return true; |
| 6641 | } |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6642 | // Inequalities and subtractions between unrelated pointers have |
| 6643 | // unspecified or undefined behavior. |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 6644 | if (!E->isEqualityOp()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6645 | return Error(E); |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 6646 | // A constant address may compare equal to the address of a symbol. |
| 6647 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 6648 | // to a null pointer constant. |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 6649 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 6650 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6651 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6652 | // It's implementation-defined whether distinct literals will have |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 6653 | // distinct addresses. In clang, the result of such a comparison is |
| 6654 | // unspecified, so it is not a constant expression. However, we do know |
| 6655 | // that the address of a literal will be non-null. |
Richard Smith | e9e20dd3 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 6656 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 6657 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6658 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6659 | // We can't tell whether weak symbols will end up pointing to the same |
| 6660 | // object. |
| 6661 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6662 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6663 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 6664 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 6665 | // lead to inconsistent results for comparisons involving the address |
| 6666 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6667 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 6668 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6669 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6670 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 6671 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 6672 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6673 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 6674 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 6675 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6676 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6677 | // C++11 [expr.add]p6: |
| 6678 | // Unless both pointers point to elements of the same array object, or |
| 6679 | // one past the last element of the array object, the behavior is |
| 6680 | // undefined. |
| 6681 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 6682 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 6683 | LHSDesignator, RHSDesignator)) |
| 6684 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 6685 | |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 6686 | QualType Type = E->getLHS()->getType(); |
| 6687 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6688 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6689 | CharUnits ElementSize; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6690 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6691 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6692 | |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 6693 | // As an extension, a type may have zero size (empty struct or union in |
| 6694 | // C, array of zero length). Pointer subtraction in such cases has |
| 6695 | // undefined behavior, so is not constant. |
| 6696 | if (ElementSize.isZero()) { |
| 6697 | Info.Diag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 6698 | << ElementType; |
| 6699 | return false; |
| 6700 | } |
| 6701 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6702 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 6703 | // and produce incorrect results when it overflows. Such behavior |
| 6704 | // appears to be non-conforming, but is common, so perhaps we should |
| 6705 | // assume the standard intended for such cases to be undefined behavior |
| 6706 | // and check for them. |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6707 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6708 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 6709 | // overflow in the final conversion to ptrdiff_t. |
| 6710 | APSInt LHS( |
| 6711 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 6712 | APSInt RHS( |
| 6713 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 6714 | APSInt ElemSize( |
| 6715 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 6716 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 6717 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 6718 | |
| 6719 | if (Result.extend(65) != TrueResult) |
| 6720 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 6721 | return Success(Result, E); |
| 6722 | } |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 6723 | |
| 6724 | // C++11 [expr.rel]p3: |
| 6725 | // Pointers to void (after pointer conversions) can be compared, with a |
| 6726 | // result defined as follows: If both pointers represent the same |
| 6727 | // address or are both the null pointer value, the result is true if the |
| 6728 | // operator is <= or >= and false otherwise; otherwise the result is |
| 6729 | // unspecified. |
| 6730 | // We interpret this as applying to pointers to *cv* void. |
| 6731 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6732 | E->isRelationalOp()) |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 6733 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 6734 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6735 | // C++11 [expr.rel]p2: |
| 6736 | // - If two pointers point to non-static data members of the same object, |
| 6737 | // or to subobjects or array elements fo such members, recursively, the |
| 6738 | // pointer to the later declared member compares greater provided the |
| 6739 | // two members have the same access control and provided their class is |
| 6740 | // not a union. |
| 6741 | // [...] |
| 6742 | // - Otherwise pointer comparisons are unspecified. |
| 6743 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 6744 | E->isRelationalOp()) { |
| 6745 | bool WasArrayIndex; |
| 6746 | unsigned Mismatch = |
| 6747 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 6748 | RHSDesignator, WasArrayIndex); |
| 6749 | // At the point where the designators diverge, the comparison has a |
| 6750 | // specified value if: |
| 6751 | // - we are comparing array indices |
| 6752 | // - we are comparing fields of a union, or fields with the same access |
| 6753 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 6754 | // constant expression. |
| 6755 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 6756 | Mismatch < RHSDesignator.Entries.size()) { |
| 6757 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 6758 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 6759 | if (!LF && !RF) |
| 6760 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 6761 | else if (!LF) |
| 6762 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 6763 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 6764 | << RF->getParent() << RF; |
| 6765 | else if (!RF) |
| 6766 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 6767 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 6768 | << LF->getParent() << LF; |
| 6769 | else if (!LF->getParent()->isUnion() && |
| 6770 | LF->getAccess() != RF->getAccess()) |
| 6771 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 6772 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 6773 | << LF->getParent(); |
| 6774 | } |
| 6775 | } |
| 6776 | |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6777 | // The comparison here must be unsigned, and performed with the same |
| 6778 | // width as the pointer. |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6779 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 6780 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 6781 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 6782 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 6783 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 6784 | CompareLHS &= Mask; |
| 6785 | CompareRHS &= Mask; |
| 6786 | |
Eli Friedman | 2f5b7c5 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 6787 | // If there is a base and this is a relational operator, we can only |
| 6788 | // compare pointers within the object in question; otherwise, the result |
| 6789 | // depends on where the object is located in memory. |
| 6790 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 6791 | QualType BaseTy = getType(LHSValue.Base); |
| 6792 | if (BaseTy->isIncompleteType()) |
| 6793 | return Error(E); |
| 6794 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 6795 | uint64_t OffsetLimit = Size.getQuantity(); |
| 6796 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 6797 | return Error(E); |
| 6798 | } |
| 6799 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6800 | switch (E->getOpcode()) { |
| 6801 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6802 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 6803 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 6804 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 6805 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 6806 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 6807 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 6808 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6809 | } |
| 6810 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 6811 | |
| 6812 | if (LHSTy->isMemberPointerType()) { |
| 6813 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 6814 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 6815 | |
| 6816 | MemberPtr LHSValue, RHSValue; |
| 6817 | |
| 6818 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 6819 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 6820 | return false; |
| 6821 | |
| 6822 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 6823 | return false; |
| 6824 | |
| 6825 | // C++11 [expr.eq]p2: |
| 6826 | // If both operands are null, they compare equal. Otherwise if only one is |
| 6827 | // null, they compare unequal. |
| 6828 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 6829 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 6830 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 6831 | } |
| 6832 | |
| 6833 | // Otherwise if either is a pointer to a virtual member function, the |
| 6834 | // result is unspecified. |
| 6835 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 6836 | if (MD->isVirtual()) |
| 6837 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 6838 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 6839 | if (MD->isVirtual()) |
| 6840 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 6841 | |
| 6842 | // Otherwise they compare equal if and only if they would refer to the |
| 6843 | // same member of the same most derived object or the same subobject if |
| 6844 | // they were dereferenced with a hypothetical object of the associated |
| 6845 | // class type. |
| 6846 | bool Equal = LHSValue == RHSValue; |
| 6847 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 6848 | } |
| 6849 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 6850 | if (LHSTy->isNullPtrType()) { |
| 6851 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 6852 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 6853 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 6854 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 6855 | // false otherwise. |
| 6856 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 6857 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 6858 | } |
| 6859 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6860 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 6861 | !RHSTy->isIntegralOrEnumerationType()) && |
| 6862 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 6863 | // We can't continue from here for non-integral types. |
| 6864 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6867 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 6868 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 6869 | // result shall be the alignment of the referenced type." |
| 6870 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 6871 | T = Ref->getPointeeType(); |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 6872 | |
| 6873 | // __alignof is defined to return the preferred alignment. |
| 6874 | return Info.Ctx.toCharUnitsFromBits( |
| 6875 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6876 | } |
| 6877 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6878 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6879 | E = E->IgnoreParens(); |
| 6880 | |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 6881 | // The kinds of expressions that we have special-case logic here for |
| 6882 | // should be kept up to date with the special checks for those |
| 6883 | // expressions in Sema. |
| 6884 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6885 | // 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] | 6886 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6887 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6888 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 6889 | /*RefAsPointee*/true); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6890 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6891 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6892 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 6893 | /*RefAsPointee*/true); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6894 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6895 | return GetAlignOfType(E->getType()); |
| 6896 | } |
| 6897 | |
| 6898 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6899 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 6900 | /// a result as the expression's type. |
| 6901 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 6902 | const UnaryExprOrTypeTraitExpr *E) { |
| 6903 | switch(E->getKind()) { |
| 6904 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6905 | if (E->isArgumentType()) |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 6906 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6907 | else |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 6908 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6909 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6910 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6911 | case UETT_VecStep: { |
| 6912 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 6913 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6914 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 6915 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6916 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6917 | // The vec_step built-in functions that take a 3-component |
| 6918 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 6919 | if (n == 3) |
| 6920 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 6921 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6922 | return Success(n, E); |
| 6923 | } else |
| 6924 | return Success(1, E); |
| 6925 | } |
| 6926 | |
| 6927 | case UETT_SizeOf: { |
| 6928 | QualType SrcTy = E->getTypeOfArgument(); |
| 6929 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 6930 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6931 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 6932 | SrcTy = Ref->getPointeeType(); |
| 6933 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6934 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6935 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6936 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6937 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6938 | } |
| 6939 | } |
| 6940 | |
| 6941 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 6942 | } |
| 6943 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6944 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6945 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6946 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6947 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6948 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6949 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6950 | for (unsigned i = 0; i != n; ++i) { |
| 6951 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 6952 | switch (ON.getKind()) { |
| 6953 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6954 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6955 | APSInt IdxResult; |
| 6956 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 6957 | return false; |
| 6958 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 6959 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6960 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6961 | CurrentType = AT->getElementType(); |
| 6962 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 6963 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 6964 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6965 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6966 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6967 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 6968 | FieldDecl *MemberDecl = ON.getField(); |
| 6969 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6970 | if (!RT) |
| 6971 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6972 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6973 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6974 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 6975 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6976 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 6977 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6978 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 6979 | break; |
| 6980 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6981 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6982 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 6983 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6984 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6985 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 6986 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 6987 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6988 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6989 | |
| 6990 | // Find the layout of the class whose base we are looking into. |
| 6991 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6992 | if (!RT) |
| 6993 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6994 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6995 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6996 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 6997 | |
| 6998 | // Find the base class itself. |
| 6999 | CurrentType = BaseSpec->getType(); |
| 7000 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 7001 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7002 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7003 | |
| 7004 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 7005 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7006 | break; |
| 7007 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7008 | } |
| 7009 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7010 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7013 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7014 | switch (E->getOpcode()) { |
| 7015 | default: |
| 7016 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 7017 | // See C99 6.6p3. |
| 7018 | return Error(E); |
| 7019 | case UO_Extension: |
| 7020 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 7021 | // If so, we could clear the diagnostic ID. |
| 7022 | return Visit(E->getSubExpr()); |
| 7023 | case UO_Plus: |
| 7024 | // The result is just the value. |
| 7025 | return Visit(E->getSubExpr()); |
| 7026 | case UO_Minus: { |
| 7027 | if (!Visit(E->getSubExpr())) |
| 7028 | return false; |
| 7029 | if (!Result.isInt()) return Error(E); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 7030 | const APSInt &Value = Result.getInt(); |
| 7031 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 7032 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 7033 | E->getType()); |
| 7034 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7035 | } |
| 7036 | case UO_Not: { |
| 7037 | if (!Visit(E->getSubExpr())) |
| 7038 | return false; |
| 7039 | if (!Result.isInt()) return Error(E); |
| 7040 | return Success(~Result.getInt(), E); |
| 7041 | } |
| 7042 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7043 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7044 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7045 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7046 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7047 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7048 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7049 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7050 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7051 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 7052 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7053 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7054 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7055 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 7056 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7057 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7058 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7059 | case CK_BaseToDerived: |
| 7060 | case CK_DerivedToBase: |
| 7061 | case CK_UncheckedDerivedToBase: |
| 7062 | case CK_Dynamic: |
| 7063 | case CK_ToUnion: |
| 7064 | case CK_ArrayToPointerDecay: |
| 7065 | case CK_FunctionToPointerDecay: |
| 7066 | case CK_NullToPointer: |
| 7067 | case CK_NullToMemberPointer: |
| 7068 | case CK_BaseToDerivedMemberPointer: |
| 7069 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7070 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7071 | case CK_ConstructorConversion: |
| 7072 | case CK_IntegralToPointer: |
| 7073 | case CK_ToVoid: |
| 7074 | case CK_VectorSplat: |
| 7075 | case CK_IntegralToFloating: |
| 7076 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7077 | case CK_CPointerToObjCPointerCast: |
| 7078 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7079 | case CK_AnyPointerToBlockPointerCast: |
| 7080 | case CK_ObjCObjectLValueCast: |
| 7081 | case CK_FloatingRealToComplex: |
| 7082 | case CK_FloatingComplexToReal: |
| 7083 | case CK_FloatingComplexCast: |
| 7084 | case CK_FloatingComplexToIntegralComplex: |
| 7085 | case CK_IntegralRealToComplex: |
| 7086 | case CK_IntegralComplexCast: |
| 7087 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7088 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7089 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7090 | case CK_NonAtomicToAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7091 | llvm_unreachable("invalid cast kind for integral value"); |
| 7092 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 7093 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7094 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7095 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7096 | case CK_ARCProduceObject: |
| 7097 | case CK_ARCConsumeObject: |
| 7098 | case CK_ARCReclaimReturnedObject: |
| 7099 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7100 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7101 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7102 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 7103 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7104 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7105 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7106 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7107 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7108 | |
| 7109 | case CK_MemberPointerToBoolean: |
| 7110 | case CK_PointerToBoolean: |
| 7111 | case CK_IntegralToBoolean: |
| 7112 | case CK_FloatingToBoolean: |
| 7113 | case CK_FloatingComplexToBoolean: |
| 7114 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7115 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7116 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7117 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7118 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7121 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7122 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7123 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 7124 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7125 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7126 | // Allow casts of address-of-label differences if they are no-ops |
| 7127 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 7128 | // be constant-evaluatable except in some narrow cases which are hard |
| 7129 | // to detect here. We let it through on the assumption the user knows |
| 7130 | // what they are doing.) |
| 7131 | if (Result.isAddrLabelDiff()) |
| 7132 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7133 | // Only allow casts of lvalues if they are lossless. |
| 7134 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 7135 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7136 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7137 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 7138 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7140 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7141 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7142 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 7143 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7144 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 7145 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7146 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7147 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7148 | if (LV.getLValueBase()) { |
| 7149 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7150 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 7151 | // narrowing back down to pointer width in subsequent integral casts. |
| 7152 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7153 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7154 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7155 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 7156 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7157 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7158 | return true; |
| 7159 | } |
| 7160 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 7161 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 7162 | SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7163 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7164 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7165 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7166 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7167 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7168 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 7169 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7170 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7171 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7172 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7173 | case CK_FloatingToIntegral: { |
| 7174 | APFloat F(0.0); |
| 7175 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 7176 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7177 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7178 | APSInt Value; |
| 7179 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 7180 | return false; |
| 7181 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7182 | } |
| 7183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7184 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7185 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7186 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7187 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7188 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 7189 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7190 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7191 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7192 | return false; |
| 7193 | if (!LV.isComplexInt()) |
| 7194 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7195 | return Success(LV.getComplexIntReal(), E); |
| 7196 | } |
| 7197 | |
| 7198 | return Visit(E->getSubExpr()); |
| 7199 | } |
| 7200 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7201 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7202 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7203 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7204 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7205 | return false; |
| 7206 | if (!LV.isComplexInt()) |
| 7207 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7208 | return Success(LV.getComplexIntImag(), E); |
| 7209 | } |
| 7210 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7211 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7212 | return Success(0, E); |
| 7213 | } |
| 7214 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7215 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 7216 | return Success(E->getPackLength(), E); |
| 7217 | } |
| 7218 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7219 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 7220 | return Success(E->getValue(), E); |
| 7221 | } |
| 7222 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7223 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7224 | // Float Evaluation |
| 7225 | //===----------------------------------------------------------------------===// |
| 7226 | |
| 7227 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7228 | class FloatExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7229 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7230 | APFloat &Result; |
| 7231 | public: |
| 7232 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7233 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7234 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7235 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7236 | Result = V.getFloat(); |
| 7237 | return true; |
| 7238 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7239 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7240 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7241 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 7242 | return true; |
| 7243 | } |
| 7244 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7245 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7246 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7247 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7248 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 7249 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7250 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7251 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7252 | bool VisitUnaryReal(const UnaryOperator *E); |
| 7253 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 7254 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7255 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7256 | }; |
| 7257 | } // end anonymous namespace |
| 7258 | |
| 7259 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7260 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7261 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7264 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7265 | QualType ResultTy, |
| 7266 | const Expr *Arg, |
| 7267 | bool SNaN, |
| 7268 | llvm::APFloat &Result) { |
| 7269 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 7270 | if (!S) return false; |
| 7271 | |
| 7272 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 7273 | |
| 7274 | llvm::APInt fill; |
| 7275 | |
| 7276 | // Treat empty strings as if they were zero. |
| 7277 | if (S->getString().empty()) |
| 7278 | fill = llvm::APInt(32, 0); |
| 7279 | else if (S->getString().getAsInteger(0, fill)) |
| 7280 | return false; |
| 7281 | |
| 7282 | if (SNaN) |
| 7283 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 7284 | else |
| 7285 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 7286 | return true; |
| 7287 | } |
| 7288 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7289 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7290 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7291 | default: |
| 7292 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 7293 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7294 | case Builtin::BI__builtin_huge_val: |
| 7295 | case Builtin::BI__builtin_huge_valf: |
| 7296 | case Builtin::BI__builtin_huge_vall: |
| 7297 | case Builtin::BI__builtin_inf: |
| 7298 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7299 | case Builtin::BI__builtin_infl: { |
| 7300 | const llvm::fltSemantics &Sem = |
| 7301 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 7302 | Result = llvm::APFloat::getInf(Sem); |
| 7303 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7304 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7305 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7306 | case Builtin::BI__builtin_nans: |
| 7307 | case Builtin::BI__builtin_nansf: |
| 7308 | case Builtin::BI__builtin_nansl: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7309 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7310 | true, Result)) |
| 7311 | return Error(E); |
| 7312 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7313 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7314 | case Builtin::BI__builtin_nan: |
| 7315 | case Builtin::BI__builtin_nanf: |
| 7316 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 7317 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7318 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7319 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7320 | false, Result)) |
| 7321 | return Error(E); |
| 7322 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7323 | |
| 7324 | case Builtin::BI__builtin_fabs: |
| 7325 | case Builtin::BI__builtin_fabsf: |
| 7326 | case Builtin::BI__builtin_fabsl: |
| 7327 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 7328 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7329 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7330 | if (Result.isNegative()) |
| 7331 | Result.changeSign(); |
| 7332 | return true; |
| 7333 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7334 | // FIXME: Builtin::BI__builtin_powi |
| 7335 | // FIXME: Builtin::BI__builtin_powif |
| 7336 | // FIXME: Builtin::BI__builtin_powil |
| 7337 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7338 | case Builtin::BI__builtin_copysign: |
| 7339 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7340 | case Builtin::BI__builtin_copysignl: { |
| 7341 | APFloat RHS(0.); |
| 7342 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 7343 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 7344 | return false; |
| 7345 | Result.copySign(RHS); |
| 7346 | return true; |
| 7347 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7348 | } |
| 7349 | } |
| 7350 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7351 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7352 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7353 | ComplexValue CV; |
| 7354 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7355 | return false; |
| 7356 | Result = CV.FloatReal; |
| 7357 | return true; |
| 7358 | } |
| 7359 | |
| 7360 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
| 7363 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7364 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7365 | ComplexValue CV; |
| 7366 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7367 | return false; |
| 7368 | Result = CV.FloatImag; |
| 7369 | return true; |
| 7370 | } |
| 7371 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7372 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7373 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 7374 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7375 | return true; |
| 7376 | } |
| 7377 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7378 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7379 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7380 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7381 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7382 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7383 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7384 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 7385 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7386 | Result.changeSign(); |
| 7387 | return true; |
| 7388 | } |
| 7389 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7390 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7391 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7392 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 7393 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 7394 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7395 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7396 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 7397 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7398 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7399 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 7400 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 7404 | Result = E->getValue(); |
| 7405 | return true; |
| 7406 | } |
| 7407 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7408 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7409 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7410 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7411 | switch (E->getCastKind()) { |
| 7412 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7413 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7414 | |
| 7415 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7416 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7417 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 7418 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 7419 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7420 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7421 | |
| 7422 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7423 | if (!Visit(SubExpr)) |
| 7424 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7425 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 7426 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7427 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7428 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7429 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7430 | ComplexValue V; |
| 7431 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 7432 | return false; |
| 7433 | Result = V.getComplexFloatReal(); |
| 7434 | return true; |
| 7435 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7436 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7437 | } |
| 7438 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7439 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7440 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7441 | //===----------------------------------------------------------------------===// |
| 7442 | |
| 7443 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7444 | class ComplexExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7445 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7446 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7447 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7448 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7449 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7450 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 7451 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7452 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7453 | Result.setFrom(V); |
| 7454 | return true; |
| 7455 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7456 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7457 | bool ZeroInitialization(const Expr *E); |
| 7458 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7459 | //===--------------------------------------------------------------------===// |
| 7460 | // Visitor Methods |
| 7461 | //===--------------------------------------------------------------------===// |
| 7462 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7463 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7464 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7465 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7466 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7467 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7468 | }; |
| 7469 | } // end anonymous namespace |
| 7470 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7471 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 7472 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7473 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7474 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7475 | } |
| 7476 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7477 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7478 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7479 | if (ElemTy->isRealFloatingType()) { |
| 7480 | Result.makeComplexFloat(); |
| 7481 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 7482 | Result.FloatReal = Zero; |
| 7483 | Result.FloatImag = Zero; |
| 7484 | } else { |
| 7485 | Result.makeComplexInt(); |
| 7486 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 7487 | Result.IntReal = Zero; |
| 7488 | Result.IntImag = Zero; |
| 7489 | } |
| 7490 | return true; |
| 7491 | } |
| 7492 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7493 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 7494 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7495 | |
| 7496 | if (SubExpr->getType()->isRealFloatingType()) { |
| 7497 | Result.makeComplexFloat(); |
| 7498 | APFloat &Imag = Result.FloatImag; |
| 7499 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 7500 | return false; |
| 7501 | |
| 7502 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 7503 | return true; |
| 7504 | } else { |
| 7505 | assert(SubExpr->getType()->isIntegerType() && |
| 7506 | "Unexpected imaginary literal."); |
| 7507 | |
| 7508 | Result.makeComplexInt(); |
| 7509 | APSInt &Imag = Result.IntImag; |
| 7510 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 7511 | return false; |
| 7512 | |
| 7513 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 7514 | return true; |
| 7515 | } |
| 7516 | } |
| 7517 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7518 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7519 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7520 | switch (E->getCastKind()) { |
| 7521 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7522 | case CK_BaseToDerived: |
| 7523 | case CK_DerivedToBase: |
| 7524 | case CK_UncheckedDerivedToBase: |
| 7525 | case CK_Dynamic: |
| 7526 | case CK_ToUnion: |
| 7527 | case CK_ArrayToPointerDecay: |
| 7528 | case CK_FunctionToPointerDecay: |
| 7529 | case CK_NullToPointer: |
| 7530 | case CK_NullToMemberPointer: |
| 7531 | case CK_BaseToDerivedMemberPointer: |
| 7532 | case CK_DerivedToBaseMemberPointer: |
| 7533 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7534 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7535 | case CK_ConstructorConversion: |
| 7536 | case CK_IntegralToPointer: |
| 7537 | case CK_PointerToIntegral: |
| 7538 | case CK_PointerToBoolean: |
| 7539 | case CK_ToVoid: |
| 7540 | case CK_VectorSplat: |
| 7541 | case CK_IntegralCast: |
| 7542 | case CK_IntegralToBoolean: |
| 7543 | case CK_IntegralToFloating: |
| 7544 | case CK_FloatingToIntegral: |
| 7545 | case CK_FloatingToBoolean: |
| 7546 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7547 | case CK_CPointerToObjCPointerCast: |
| 7548 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7549 | case CK_AnyPointerToBlockPointerCast: |
| 7550 | case CK_ObjCObjectLValueCast: |
| 7551 | case CK_FloatingComplexToReal: |
| 7552 | case CK_FloatingComplexToBoolean: |
| 7553 | case CK_IntegralComplexToReal: |
| 7554 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7555 | case CK_ARCProduceObject: |
| 7556 | case CK_ARCConsumeObject: |
| 7557 | case CK_ARCReclaimReturnedObject: |
| 7558 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7559 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7560 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7561 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7562 | case CK_NonAtomicToAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7563 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 7564 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7565 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7566 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7567 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7568 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7569 | |
| 7570 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7571 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7572 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7573 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7574 | |
| 7575 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7576 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7577 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7578 | return false; |
| 7579 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7580 | Result.makeComplexFloat(); |
| 7581 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 7582 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7583 | } |
| 7584 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7585 | case CK_FloatingComplexCast: { |
| 7586 | if (!Visit(E->getSubExpr())) |
| 7587 | return false; |
| 7588 | |
| 7589 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7590 | QualType From |
| 7591 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7592 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7593 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 7594 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7595 | } |
| 7596 | |
| 7597 | case CK_FloatingComplexToIntegralComplex: { |
| 7598 | if (!Visit(E->getSubExpr())) |
| 7599 | return false; |
| 7600 | |
| 7601 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7602 | QualType From |
| 7603 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7604 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7605 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 7606 | To, Result.IntReal) && |
| 7607 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 7608 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | case CK_IntegralRealToComplex: { |
| 7612 | APSInt &Real = Result.IntReal; |
| 7613 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 7614 | return false; |
| 7615 | |
| 7616 | Result.makeComplexInt(); |
| 7617 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 7618 | return true; |
| 7619 | } |
| 7620 | |
| 7621 | case CK_IntegralComplexCast: { |
| 7622 | if (!Visit(E->getSubExpr())) |
| 7623 | return false; |
| 7624 | |
| 7625 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7626 | QualType From |
| 7627 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7628 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7629 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 7630 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7631 | return true; |
| 7632 | } |
| 7633 | |
| 7634 | case CK_IntegralComplexToFloatingComplex: { |
| 7635 | if (!Visit(E->getSubExpr())) |
| 7636 | return false; |
| 7637 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7638 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7639 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7640 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7641 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7642 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 7643 | To, Result.FloatReal) && |
| 7644 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 7645 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7646 | } |
| 7647 | } |
| 7648 | |
| 7649 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7652 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7653 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 7654 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 7655 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7656 | bool LHSOK = Visit(E->getLHS()); |
| 7657 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7658 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7659 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7660 | ComplexValue RHS; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7661 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7662 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7663 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7664 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 7665 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7666 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7667 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7668 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7669 | if (Result.isComplexFloat()) { |
| 7670 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 7671 | APFloat::rmNearestTiesToEven); |
| 7672 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 7673 | APFloat::rmNearestTiesToEven); |
| 7674 | } else { |
| 7675 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 7676 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 7677 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7678 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7679 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7680 | if (Result.isComplexFloat()) { |
| 7681 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 7682 | APFloat::rmNearestTiesToEven); |
| 7683 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 7684 | APFloat::rmNearestTiesToEven); |
| 7685 | } else { |
| 7686 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 7687 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 7688 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7689 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7690 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7691 | if (Result.isComplexFloat()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7692 | ComplexValue LHS = Result; |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7693 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 7694 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 7695 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 7696 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7697 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7698 | APFloat Tmp = LHS_r; |
| 7699 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7700 | Result.getComplexFloatReal() = Tmp; |
| 7701 | Tmp = LHS_i; |
| 7702 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7703 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 7704 | |
| 7705 | Tmp = LHS_r; |
| 7706 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7707 | Result.getComplexFloatImag() = Tmp; |
| 7708 | Tmp = LHS_i; |
| 7709 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7710 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 7711 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7712 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7713 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7714 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 7715 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7716 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7717 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 7718 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 7719 | } |
| 7720 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7721 | case BO_Div: |
| 7722 | if (Result.isComplexFloat()) { |
| 7723 | ComplexValue LHS = Result; |
| 7724 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 7725 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 7726 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 7727 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 7728 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 7729 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 7730 | |
| 7731 | APFloat Den = RHS_r; |
| 7732 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7733 | APFloat Tmp = RHS_i; |
| 7734 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7735 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 7736 | |
| 7737 | Res_r = LHS_r; |
| 7738 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7739 | Tmp = LHS_i; |
| 7740 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7741 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 7742 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 7743 | |
| 7744 | Res_i = LHS_i; |
| 7745 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7746 | Tmp = LHS_r; |
| 7747 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7748 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 7749 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 7750 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7751 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 7752 | return Error(E, diag::note_expr_divide_by_zero); |
| 7753 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7754 | ComplexValue LHS = Result; |
| 7755 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 7756 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 7757 | Result.getComplexIntReal() = |
| 7758 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 7759 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 7760 | Result.getComplexIntImag() = |
| 7761 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 7762 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 7763 | } |
| 7764 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7765 | } |
| 7766 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7767 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7770 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 7771 | // Get the operand value into 'Result'. |
| 7772 | if (!Visit(E->getSubExpr())) |
| 7773 | return false; |
| 7774 | |
| 7775 | switch (E->getOpcode()) { |
| 7776 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7777 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7778 | case UO_Extension: |
| 7779 | return true; |
| 7780 | case UO_Plus: |
| 7781 | // The result is always just the subexpr. |
| 7782 | return true; |
| 7783 | case UO_Minus: |
| 7784 | if (Result.isComplexFloat()) { |
| 7785 | Result.getComplexFloatReal().changeSign(); |
| 7786 | Result.getComplexFloatImag().changeSign(); |
| 7787 | } |
| 7788 | else { |
| 7789 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 7790 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 7791 | } |
| 7792 | return true; |
| 7793 | case UO_Not: |
| 7794 | if (Result.isComplexFloat()) |
| 7795 | Result.getComplexFloatImag().changeSign(); |
| 7796 | else |
| 7797 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 7798 | return true; |
| 7799 | } |
| 7800 | } |
| 7801 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7802 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 7803 | if (E->getNumInits() == 2) { |
| 7804 | if (E->getType()->isComplexType()) { |
| 7805 | Result.makeComplexFloat(); |
| 7806 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 7807 | return false; |
| 7808 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 7809 | return false; |
| 7810 | } else { |
| 7811 | Result.makeComplexInt(); |
| 7812 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 7813 | return false; |
| 7814 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 7815 | return false; |
| 7816 | } |
| 7817 | return true; |
| 7818 | } |
| 7819 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 7820 | } |
| 7821 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7822 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7823 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 7824 | // implicit conversion. |
| 7825 | //===----------------------------------------------------------------------===// |
| 7826 | |
| 7827 | namespace { |
| 7828 | class AtomicExprEvaluator : |
| 7829 | public ExprEvaluatorBase<AtomicExprEvaluator, bool> { |
| 7830 | APValue &Result; |
| 7831 | public: |
| 7832 | AtomicExprEvaluator(EvalInfo &Info, APValue &Result) |
| 7833 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 7834 | |
| 7835 | bool Success(const APValue &V, const Expr *E) { |
| 7836 | Result = V; |
| 7837 | return true; |
| 7838 | } |
| 7839 | |
| 7840 | bool ZeroInitialization(const Expr *E) { |
| 7841 | ImplicitValueInitExpr VIE( |
| 7842 | E->getType()->castAs<AtomicType>()->getValueType()); |
| 7843 | return Evaluate(Result, Info, &VIE); |
| 7844 | } |
| 7845 | |
| 7846 | bool VisitCastExpr(const CastExpr *E) { |
| 7847 | switch (E->getCastKind()) { |
| 7848 | default: |
| 7849 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 7850 | case CK_NonAtomicToAtomic: |
| 7851 | return Evaluate(Result, Info, E->getSubExpr()); |
| 7852 | } |
| 7853 | } |
| 7854 | }; |
| 7855 | } // end anonymous namespace |
| 7856 | |
| 7857 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info) { |
| 7858 | assert(E->isRValue() && E->getType()->isAtomicType()); |
| 7859 | return AtomicExprEvaluator(Info, Result).Visit(E); |
| 7860 | } |
| 7861 | |
| 7862 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7863 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 7864 | // comma operator |
| 7865 | //===----------------------------------------------------------------------===// |
| 7866 | |
| 7867 | namespace { |
| 7868 | class VoidExprEvaluator |
| 7869 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 7870 | public: |
| 7871 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 7872 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7873 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7874 | |
| 7875 | bool VisitCastExpr(const CastExpr *E) { |
| 7876 | switch (E->getCastKind()) { |
| 7877 | default: |
| 7878 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 7879 | case CK_ToVoid: |
| 7880 | VisitIgnoredValue(E->getSubExpr()); |
| 7881 | return true; |
| 7882 | } |
| 7883 | } |
| 7884 | }; |
| 7885 | } // end anonymous namespace |
| 7886 | |
| 7887 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 7888 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 7889 | return VoidExprEvaluator(Info).Visit(E); |
| 7890 | } |
| 7891 | |
| 7892 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7893 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7894 | //===----------------------------------------------------------------------===// |
| 7895 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7896 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7897 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 7898 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7899 | QualType T = E->getType(); |
| 7900 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7901 | LValue LV; |
| 7902 | if (!EvaluateLValue(E, LV, Info)) |
| 7903 | return false; |
| 7904 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7905 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7906 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7907 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7908 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7909 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7910 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7911 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7912 | LValue LV; |
| 7913 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7914 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7915 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7916 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7917 | llvm::APFloat F(0.0); |
| 7918 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7919 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7920 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7921 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7922 | ComplexValue C; |
| 7923 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7924 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7925 | C.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7926 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7927 | MemberPtr P; |
| 7928 | if (!EvaluateMemberPointer(E, P, Info)) |
| 7929 | return false; |
| 7930 | P.moveInto(Result); |
| 7931 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7932 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7933 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7934 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 7935 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 7936 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7937 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 7938 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7939 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7940 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7941 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 7942 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 7943 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7944 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 7945 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7946 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7947 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7948 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7949 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7950 | if (!EvaluateVoid(E, Info)) |
| 7951 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7952 | } else if (T->isAtomicType()) { |
| 7953 | if (!EvaluateAtomic(E, Result, Info)) |
| 7954 | return false; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7955 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7956 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7957 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7958 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7959 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 7960 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7961 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7962 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 7963 | return true; |
| 7964 | } |
| 7965 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7966 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 7967 | /// cases, the in-place evaluation is essential, since later initializers for |
| 7968 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 7969 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 7970 | const Expr *E, bool AllowNonLiteralTypes) { |
| 7971 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7972 | return false; |
| 7973 | |
| 7974 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7975 | // Evaluate arrays and record types in-place, so that later initializers can |
| 7976 | // refer to earlier-initialized members of the object. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7977 | if (E->getType()->isArrayType()) |
| 7978 | return EvaluateArray(E, This, Result, Info); |
| 7979 | else if (E->getType()->isRecordType()) |
| 7980 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7981 | } |
| 7982 | |
| 7983 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7984 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7987 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 7988 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 7989 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7990 | if (!CheckLiteralType(Info, E)) |
| 7991 | return false; |
| 7992 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7993 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7994 | return false; |
| 7995 | |
| 7996 | if (E->isGLValue()) { |
| 7997 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7998 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7999 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8000 | return false; |
| 8001 | } |
| 8002 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8003 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8004 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8005 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8006 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8007 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| 8008 | const ASTContext &Ctx, bool &IsConst) { |
| 8009 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 8010 | // containing vast quantities of these. |
| 8011 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 8012 | Result.Val = APValue(APSInt(L->getValue(), |
| 8013 | L->getType()->isUnsignedIntegerType())); |
| 8014 | IsConst = true; |
| 8015 | return true; |
| 8016 | } |
| 8017 | |
| 8018 | // FIXME: Evaluating values of large array and record types can cause |
| 8019 | // performance problems. Only do so in C++11 for now. |
| 8020 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 8021 | Exp->getType()->isRecordType()) && |
| 8022 | !Ctx.getLangOpts().CPlusPlus11) { |
| 8023 | IsConst = false; |
| 8024 | return true; |
| 8025 | } |
| 8026 | return false; |
| 8027 | } |
| 8028 | |
| 8029 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8030 | /// 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] | 8031 | /// any crazy technique (that has nothing to do with language standards) that |
| 8032 | /// 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] | 8033 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 8034 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8035 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8036 | bool IsConst; |
| 8037 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
| 8038 | return IsConst; |
| 8039 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8040 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8041 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8042 | } |
| 8043 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8044 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 8045 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8046 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8047 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8048 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 8049 | } |
| 8050 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8051 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 8052 | SideEffectsKind AllowSideEffects) const { |
| 8053 | if (!getType()->isIntegralOrEnumerationType()) |
| 8054 | return false; |
| 8055 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8056 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8057 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 8058 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8059 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8060 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8061 | Result = ExprResult.Val.getInt(); |
| 8062 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8063 | } |
| 8064 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8065 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8066 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 8067 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8068 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8069 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 8070 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 8071 | Ctx.getLValueReferenceType(getType()), LV)) |
| 8072 | return false; |
| 8073 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8074 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8075 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8078 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 8079 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8080 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8081 | // FIXME: Evaluating initializers for large array and record types can cause |
| 8082 | // performance problems. Only do so in C++11 for now. |
| 8083 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8084 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8085 | return false; |
| 8086 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8087 | Expr::EvalStatus EStatus; |
| 8088 | EStatus.Diag = &Notes; |
| 8089 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8090 | EvalInfo InitInfo(Ctx, EStatus, EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8091 | InitInfo.setEvaluatingDecl(VD, Value); |
| 8092 | |
| 8093 | LValue LVal; |
| 8094 | LVal.set(VD); |
| 8095 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8096 | // C++11 [basic.start.init]p2: |
| 8097 | // Variables with static storage duration or thread storage duration shall be |
| 8098 | // zero-initialized before any other initialization takes place. |
| 8099 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8100 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8101 | !VD->getType()->isReferenceType()) { |
| 8102 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8103 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8104 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8105 | return false; |
| 8106 | } |
| 8107 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8108 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 8109 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8110 | EStatus.HasSideEffects) |
| 8111 | return false; |
| 8112 | |
| 8113 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 8114 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8115 | } |
| 8116 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8117 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 8118 | /// constant folded, but discard the result. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8119 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 8120 | EvalResult Result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8121 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 8122 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8123 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8124 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8125 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8126 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8127 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8128 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 8129 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8130 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8131 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8132 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8133 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8134 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8135 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8136 | void Expr::EvaluateForOverflow(const ASTContext &Ctx, |
| 8137 | SmallVectorImpl<PartialDiagnosticAt> *Diags) const { |
| 8138 | bool IsConst; |
| 8139 | EvalResult EvalResult; |
| 8140 | EvalResult.Diag = Diags; |
| 8141 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8142 | EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8143 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 8144 | } |
| 8145 | } |
| 8146 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8147 | bool Expr::EvalResult::isGlobalLValue() const { |
| 8148 | assert(Val.isLValue()); |
| 8149 | return IsGlobalLValue(Val.getLValueBase()); |
| 8150 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 8151 | |
| 8152 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8153 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 8154 | /// an integer constant expression. |
| 8155 | |
| 8156 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 8157 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8158 | |
| 8159 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8160 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 8161 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 8162 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8163 | // Note that to reduce code duplication, this helper does no evaluation |
| 8164 | // itself; the caller checks whether the expression is evaluatable, and |
| 8165 | // in the rare cases where CheckICE actually cares about the evaluated |
| 8166 | // value, it calls into Evalute. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8167 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8168 | namespace { |
| 8169 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8170 | enum ICEKind { |
| 8171 | /// This expression is an ICE. |
| 8172 | IK_ICE, |
| 8173 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 8174 | /// a legal subexpression for an ICE. This return value is used to handle |
| 8175 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 8176 | IK_ICEIfUnevaluated, |
| 8177 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 8178 | IK_NotICE |
| 8179 | }; |
| 8180 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8181 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8182 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8183 | SourceLocation Loc; |
| 8184 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8185 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8186 | }; |
| 8187 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8188 | } |
| 8189 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8190 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 8191 | |
| 8192 | 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] | 8193 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8194 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8195 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8196 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8197 | !EVResult.Val.isInt()) |
| 8198 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 8199 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8200 | return NoDiag(); |
| 8201 | } |
| 8202 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8203 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8204 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8205 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 8206 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8207 | |
| 8208 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 8209 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8210 | #define STMT(Node, Base) case Expr::Node##Class: |
| 8211 | #define EXPR(Node, Base) |
| 8212 | #include "clang/AST/StmtNodes.inc" |
| 8213 | case Expr::PredefinedExprClass: |
| 8214 | case Expr::FloatingLiteralClass: |
| 8215 | case Expr::ImaginaryLiteralClass: |
| 8216 | case Expr::StringLiteralClass: |
| 8217 | case Expr::ArraySubscriptExprClass: |
| 8218 | case Expr::MemberExprClass: |
| 8219 | case Expr::CompoundAssignOperatorClass: |
| 8220 | case Expr::CompoundLiteralExprClass: |
| 8221 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8222 | case Expr::DesignatedInitExprClass: |
| 8223 | case Expr::ImplicitValueInitExprClass: |
| 8224 | case Expr::ParenListExprClass: |
| 8225 | case Expr::VAArgExprClass: |
| 8226 | case Expr::AddrLabelExprClass: |
| 8227 | case Expr::StmtExprClass: |
| 8228 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8229 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8230 | case Expr::CXXDynamicCastExprClass: |
| 8231 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 8232 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 8233 | case Expr::MSPropertyRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8234 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 8235 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8236 | case Expr::CXXThisExprClass: |
| 8237 | case Expr::CXXThrowExprClass: |
| 8238 | case Expr::CXXNewExprClass: |
| 8239 | case Expr::CXXDeleteExprClass: |
| 8240 | case Expr::CXXPseudoDestructorExprClass: |
| 8241 | case Expr::UnresolvedLookupExprClass: |
| 8242 | case Expr::DependentScopeDeclRefExprClass: |
| 8243 | case Expr::CXXConstructExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8244 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8245 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 8246 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8247 | case Expr::CXXTemporaryObjectExprClass: |
| 8248 | case Expr::CXXUnresolvedConstructExprClass: |
| 8249 | case Expr::CXXDependentScopeMemberExprClass: |
| 8250 | case Expr::UnresolvedMemberExprClass: |
| 8251 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 8252 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8253 | case Expr::ObjCArrayLiteralClass: |
| 8254 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8255 | case Expr::ObjCEncodeExprClass: |
| 8256 | case Expr::ObjCMessageExprClass: |
| 8257 | case Expr::ObjCSelectorExprClass: |
| 8258 | case Expr::ObjCProtocolExprClass: |
| 8259 | case Expr::ObjCIvarRefExprClass: |
| 8260 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8261 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8262 | case Expr::ObjCIsaExprClass: |
| 8263 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 8264 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8265 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8266 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 8267 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 8268 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 8269 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 8270 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 8271 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8272 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 8273 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8274 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 8275 | case Expr::AtomicExprClass: |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 8276 | case Expr::InitListExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 8277 | case Expr::LambdaExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8278 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 8279 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 8280 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8281 | case Expr::GNUNullExprClass: |
| 8282 | // GCC considers the GNU __null value to be an integral constant expression. |
| 8283 | return NoDiag(); |
| 8284 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 8285 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 8286 | return |
| 8287 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 8288 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8289 | case Expr::ParenExprClass: |
| 8290 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 8291 | case Expr::GenericSelectionExprClass: |
| 8292 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8293 | case Expr::IntegerLiteralClass: |
| 8294 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8295 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8296 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 8297 | case Expr::CXXScalarValueInitExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8298 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 8299 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 8300 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 8301 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 8302 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 8303 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8304 | return NoDiag(); |
| 8305 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 8306 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8307 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 8308 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8309 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8310 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8311 | if (CE->isBuiltinCall()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8312 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8313 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8314 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8315 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8316 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 8317 | return NoDiag(); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8318 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8319 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8320 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8321 | // Parameter variables are never constants. Without this check, |
| 8322 | // getAnyInitializer() can find a default argument, which leads |
| 8323 | // to chaos. |
| 8324 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8325 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8326 | |
| 8327 | // C++ 7.1.5.1p2 |
| 8328 | // A variable of non-volatile const-qualified integral or enumeration |
| 8329 | // type initialized by an ICE can be used in ICEs. |
| 8330 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8331 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8332 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8333 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8334 | const VarDecl *VD; |
| 8335 | // Look for a declaration of this variable that has an initializer, and |
| 8336 | // check whether it is an ICE. |
| 8337 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 8338 | return NoDiag(); |
| 8339 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8340 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8341 | } |
| 8342 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8343 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8344 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8345 | case Expr::UnaryOperatorClass: { |
| 8346 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 8347 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8348 | case UO_PostInc: |
| 8349 | case UO_PostDec: |
| 8350 | case UO_PreInc: |
| 8351 | case UO_PreDec: |
| 8352 | case UO_AddrOf: |
| 8353 | case UO_Deref: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8354 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 8355 | // subexpressions of constant expressions, but they can never be ICEs |
| 8356 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8357 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8358 | case UO_Extension: |
| 8359 | case UO_LNot: |
| 8360 | case UO_Plus: |
| 8361 | case UO_Minus: |
| 8362 | case UO_Not: |
| 8363 | case UO_Real: |
| 8364 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8365 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8366 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8367 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8368 | // OffsetOf falls through here. |
| 8369 | } |
| 8370 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8371 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 8372 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 8373 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 8374 | // compliance: we should warn earlier for offsetof expressions with |
| 8375 | // array subscripts that aren't ICEs, and if the array subscripts |
| 8376 | // are ICEs, the value of the offsetof must be an integer constant. |
| 8377 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8378 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8379 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 8380 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 8381 | if ((Exp->getKind() == UETT_SizeOf) && |
| 8382 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8383 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8384 | return NoDiag(); |
| 8385 | } |
| 8386 | case Expr::BinaryOperatorClass: { |
| 8387 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 8388 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8389 | case BO_PtrMemD: |
| 8390 | case BO_PtrMemI: |
| 8391 | case BO_Assign: |
| 8392 | case BO_MulAssign: |
| 8393 | case BO_DivAssign: |
| 8394 | case BO_RemAssign: |
| 8395 | case BO_AddAssign: |
| 8396 | case BO_SubAssign: |
| 8397 | case BO_ShlAssign: |
| 8398 | case BO_ShrAssign: |
| 8399 | case BO_AndAssign: |
| 8400 | case BO_XorAssign: |
| 8401 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8402 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 8403 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8404 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8405 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8406 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8407 | case BO_Mul: |
| 8408 | case BO_Div: |
| 8409 | case BO_Rem: |
| 8410 | case BO_Add: |
| 8411 | case BO_Sub: |
| 8412 | case BO_Shl: |
| 8413 | case BO_Shr: |
| 8414 | case BO_LT: |
| 8415 | case BO_GT: |
| 8416 | case BO_LE: |
| 8417 | case BO_GE: |
| 8418 | case BO_EQ: |
| 8419 | case BO_NE: |
| 8420 | case BO_And: |
| 8421 | case BO_Xor: |
| 8422 | case BO_Or: |
| 8423 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8424 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8425 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8426 | if (Exp->getOpcode() == BO_Div || |
| 8427 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8428 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8429 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8430 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8431 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8432 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8433 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8434 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8435 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8436 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8437 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8438 | } |
| 8439 | } |
| 8440 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8441 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8442 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8443 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 8444 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8445 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 8446 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8447 | } else { |
| 8448 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8449 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8450 | } |
| 8451 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8452 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8453 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8454 | case BO_LAnd: |
| 8455 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8456 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8457 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8458 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8459 | // Rare case where the RHS has a comma "side-effect"; we need |
| 8460 | // to actually check the condition to see whether the side |
| 8461 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8462 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8463 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8464 | return RHSResult; |
| 8465 | return NoDiag(); |
| 8466 | } |
| 8467 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8468 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8469 | } |
| 8470 | } |
| 8471 | } |
| 8472 | case Expr::ImplicitCastExprClass: |
| 8473 | case Expr::CStyleCastExprClass: |
| 8474 | case Expr::CXXFunctionalCastExprClass: |
| 8475 | case Expr::CXXStaticCastExprClass: |
| 8476 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 8477 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8478 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8479 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 8480 | if (isa<ExplicitCastExpr>(E)) { |
| 8481 | if (const FloatingLiteral *FL |
| 8482 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 8483 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 8484 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 8485 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 8486 | bool Ignored; |
| 8487 | // If the value does not fit in the destination type, the behavior is |
| 8488 | // undefined, so we are not required to treat it as a constant |
| 8489 | // expression. |
| 8490 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 8491 | llvm::APFloat::rmTowardZero, |
| 8492 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8493 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 8494 | return NoDiag(); |
| 8495 | } |
| 8496 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8497 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 8498 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 8499 | case CK_AtomicToNonAtomic: |
| 8500 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8501 | case CK_NoOp: |
| 8502 | case CK_IntegralToBoolean: |
| 8503 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8504 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8505 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8506 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8507 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8508 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8509 | case Expr::BinaryConditionalOperatorClass: { |
| 8510 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 8511 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8512 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8513 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8514 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 8515 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 8516 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 8517 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8518 | return FalseResult; |
| 8519 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8520 | case Expr::ConditionalOperatorClass: { |
| 8521 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 8522 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 8523 | // then only the true side is actually considered in an integer constant |
| 8524 | // expression, and it is fully evaluated. This is an important GNU |
| 8525 | // extension. See GCC PR38377 for discussion. |
| 8526 | if (const CallExpr *CallCE |
| 8527 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8528 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 8529 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8530 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8531 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8532 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8533 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8534 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 8535 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8536 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8537 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8538 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8539 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8540 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8541 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8542 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8543 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8544 | return NoDiag(); |
| 8545 | // Rare case where the diagnostics depend on which side is evaluated |
| 8546 | // Note that if we get here, CondResult is 0, and at least one of |
| 8547 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8548 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8549 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8550 | return TrueResult; |
| 8551 | } |
| 8552 | case Expr::CXXDefaultArgExprClass: |
| 8553 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8554 | case Expr::CXXDefaultInitExprClass: |
| 8555 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8556 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 8557 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8558 | } |
| 8559 | } |
| 8560 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 8561 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8564 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8565 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8566 | const Expr *E, |
| 8567 | llvm::APSInt *Value, |
| 8568 | SourceLocation *Loc) { |
| 8569 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 8570 | if (Loc) *Loc = E->getExprLoc(); |
| 8571 | return false; |
| 8572 | } |
| 8573 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8574 | APValue Result; |
| 8575 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 8576 | return false; |
| 8577 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8578 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 8579 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 8580 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8583 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 8584 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8585 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8586 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 8587 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8588 | ICEDiag D = CheckICE(this, Ctx); |
| 8589 | if (D.Kind != IK_ICE) { |
| 8590 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8591 | return false; |
| 8592 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8593 | return true; |
| 8594 | } |
| 8595 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8596 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8597 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8598 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8599 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 8600 | |
| 8601 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 8602 | return false; |
| 8603 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8604 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8605 | return true; |
| 8606 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8607 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8608 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8609 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8612 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8613 | SourceLocation *Loc) const { |
| 8614 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 8615 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8616 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8617 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 8618 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8619 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8620 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8621 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8622 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8623 | |
| 8624 | APValue Scratch; |
| 8625 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 8626 | |
| 8627 | if (!Diags.empty()) { |
| 8628 | IsConstExpr = false; |
| 8629 | if (Loc) *Loc = Diags[0].first; |
| 8630 | } else if (!IsConstExpr) { |
| 8631 | // FIXME: This shouldn't happen. |
| 8632 | if (Loc) *Loc = getExprLoc(); |
| 8633 | } |
| 8634 | |
| 8635 | return IsConstExpr; |
| 8636 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8637 | |
| 8638 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8639 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8640 | PartialDiagnosticAt> &Diags) { |
| 8641 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 8642 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 8643 | // ASTs which we build for dependent expressions. |
| 8644 | if (FD->isDependentContext()) |
| 8645 | return true; |
| 8646 | |
| 8647 | Expr::EvalStatus Status; |
| 8648 | Status.Diag = &Diags; |
| 8649 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame^] | 8650 | EvalInfo Info(FD->getASTContext(), Status, |
| 8651 | EvalInfo::EM_PotentialConstantExpression); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8652 | |
| 8653 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 8654 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 8655 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8656 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8657 | // is a temporary being used as the 'this' pointer. |
| 8658 | LValue This; |
| 8659 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8660 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8661 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8662 | ArrayRef<const Expr*> Args; |
| 8663 | |
| 8664 | SourceLocation Loc = FD->getLocation(); |
| 8665 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8666 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8667 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 8668 | // Evaluate the call as a constant initializer, to allow the construction |
| 8669 | // of objects of non-literal types. |
| 8670 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8671 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8672 | } else |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8673 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 8674 | Args, FD->getBody(), Info, Scratch); |
| 8675 | |
| 8676 | return Diags.empty(); |
| 8677 | } |