Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
Richard Smith | a49a7fe | 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 | 745f514 | 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 | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 41 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 42 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 63 | if (!B) return QualType(); |
| 64 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 65 | return D->getType(); |
Richard Smith | 8a66bf7 | 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 | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Richard Smith | 180f479 | 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 | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 88 | /// field or base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 89 | static |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 90 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 91 | APValue::BaseOrMemberType Value; |
| 92 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | f15fda0 | 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 | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 98 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 99 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 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 | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 103 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 104 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 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 | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 108 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 109 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Richard Smith | b4e85ed | 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 | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 120 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 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 | 8602401 | 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 | b4e85ed | 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 | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 137 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 138 | ArraySize = 0; |
| 139 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 140 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 141 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 144 | // The order of this enum is important for diagnostics. |
| 145 | enum CheckSubobjectKind { |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 146 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 147 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Richard Smith | 0a3bdb6 | 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 | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 170 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | typedef APValue::LValuePathEntry PathEntry; |
| 172 | |
Richard Smith | 0a3bdb6 | 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 | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 176 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 177 | |
Richard Smith | b4e85ed | 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 | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 185 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 186 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 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 | b4e85ed | 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 | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 197 | void setInvalid() { |
| 198 | Invalid = true; |
| 199 | Entries.clear(); |
| 200 | } |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 224 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 225 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 226 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 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 | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 235 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 236 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 237 | APValue::BaseOrMemberType Value(D, Virtual); |
| 238 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 239 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 247 | } |
Richard Smith | 8602401 | 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 | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 260 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 261 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 262 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 263 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 264 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 265 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
Richard Smith | b4e85ed | 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 | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 281 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 282 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
Richard Smith | d0dccea | 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 | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 291 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 292 | |
Richard Smith | 08d6e03 | 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 | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 299 | /// Index - The call index of this call. |
| 300 | unsigned Index; |
| 301 | |
Richard Smith | 180f479 | 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 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 305 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 306 | /// parameters' function scope indices. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 307 | APValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 308 | |
Eli Friedman | f6172ae | 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 | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 311 | typedef std::map<const void*, APValue> MapTy; |
Richard Smith | bd552ef | 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 | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 316 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 317 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 318 | APValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 319 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 322 | /// Temporarily override 'this'. |
| 323 | class ThisOverrideRAII { |
| 324 | public: |
| 325 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 326 | : Frame(Frame), OldThis(Frame.This) { |
| 327 | if (Enable) |
| 328 | Frame.This = NewThis; |
| 329 | } |
| 330 | ~ThisOverrideRAII() { |
| 331 | Frame.This = OldThis; |
| 332 | } |
| 333 | private: |
| 334 | CallStackFrame &Frame; |
| 335 | const LValue *OldThis; |
| 336 | }; |
| 337 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 338 | /// A partial diagnostic which we might know in advance that we are not going |
| 339 | /// to emit. |
| 340 | class OptionalDiagnostic { |
| 341 | PartialDiagnostic *Diag; |
| 342 | |
| 343 | public: |
| 344 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 345 | |
| 346 | template<typename T> |
| 347 | OptionalDiagnostic &operator<<(const T &v) { |
| 348 | if (Diag) |
| 349 | *Diag << v; |
| 350 | return *this; |
| 351 | } |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 352 | |
| 353 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 354 | if (Diag) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 355 | SmallVector<char, 32> Buffer; |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 356 | I.toString(Buffer); |
| 357 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 358 | } |
| 359 | return *this; |
| 360 | } |
| 361 | |
| 362 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 363 | if (Diag) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 364 | SmallVector<char, 32> Buffer; |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 365 | F.toString(Buffer); |
| 366 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 367 | } |
| 368 | return *this; |
| 369 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 372 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 373 | /// information about a subexpression as it is folded. It retains information |
| 374 | /// about the AST context, but also maintains information about the folded |
| 375 | /// expression. |
| 376 | /// |
| 377 | /// If an expression could be evaluated, it is still possible it is not a C |
| 378 | /// "integer constant expression" or constant expression. If not, this struct |
| 379 | /// captures information about how and why not. |
| 380 | /// |
| 381 | /// One bit of information passed *into* the request for constant folding |
| 382 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 383 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 384 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 385 | /// certain things in certain situations. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 386 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 387 | ASTContext &Ctx; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 388 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 389 | /// EvalStatus - Contains information about the evaluation. |
| 390 | Expr::EvalStatus &EvalStatus; |
| 391 | |
| 392 | /// CurrentCall - The top of the constexpr call stack. |
| 393 | CallStackFrame *CurrentCall; |
| 394 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 395 | /// CallStackDepth - The number of calls in the call stack right now. |
| 396 | unsigned CallStackDepth; |
| 397 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 398 | /// NextCallIndex - The next call index to assign. |
| 399 | unsigned NextCallIndex; |
| 400 | |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 401 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 402 | /// to perform. This is essentially a limit for the number of statements |
| 403 | /// we will evaluate. |
| 404 | unsigned StepsLeft; |
| 405 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 406 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 407 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 408 | CallStackFrame BottomFrame; |
| 409 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 410 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 411 | /// evaluated, if any. |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 412 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 413 | |
| 414 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 415 | /// declaration whose initializer is being evaluated, if any. |
| 416 | APValue *EvaluatingDeclValue; |
| 417 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 418 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 419 | /// notes attached to it will also be stored, otherwise they will not be. |
| 420 | bool HasActiveDiagnostic; |
| 421 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 422 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 423 | /// expression is a potential constant expression? If so, some diagnostics |
| 424 | /// are suppressed. |
| 425 | bool CheckingPotentialConstantExpression; |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 426 | |
| 427 | bool IntOverflowCheckMode; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 428 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 429 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 430 | bool OverflowCheckMode = false) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 431 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 432 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 433 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 434 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 435 | EvaluatingDecl((const ValueDecl*)0), EvaluatingDeclValue(0), |
| 436 | HasActiveDiagnostic(false), CheckingPotentialConstantExpression(false), |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 437 | IntOverflowCheckMode(OverflowCheckMode) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 438 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 439 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 440 | EvaluatingDecl = Base; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 441 | EvaluatingDeclValue = &Value; |
| 442 | } |
| 443 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 444 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 445 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 446 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 447 | // Don't perform any constexpr calls (other than the call we're checking) |
| 448 | // when checking a potential constant expression. |
| 449 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 450 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 451 | if (NextCallIndex == 0) { |
| 452 | // NextCallIndex has wrapped around. |
| 453 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 454 | return false; |
| 455 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 456 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 457 | return true; |
| 458 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 459 | << getLangOpts().ConstexprCallDepth; |
| 460 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 461 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 462 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 463 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 464 | assert(CallIndex && "no call index in getCallFrame"); |
| 465 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 466 | // be null in this loop. |
| 467 | CallStackFrame *Frame = CurrentCall; |
| 468 | while (Frame->Index > CallIndex) |
| 469 | Frame = Frame->Caller; |
| 470 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 471 | } |
| 472 | |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 473 | bool nextStep(const Stmt *S) { |
| 474 | if (!StepsLeft) { |
| 475 | Diag(S->getLocStart(), diag::note_constexpr_step_limit_exceeded); |
| 476 | return false; |
| 477 | } |
| 478 | --StepsLeft; |
| 479 | return true; |
| 480 | } |
| 481 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 482 | private: |
| 483 | /// Add a diagnostic to the diagnostics list. |
| 484 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 485 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 486 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 487 | return EvalStatus.Diag->back().second; |
| 488 | } |
| 489 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 490 | /// Add notes containing a call stack to the current point of evaluation. |
| 491 | void addCallStack(unsigned Limit); |
| 492 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 493 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 494 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 495 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 496 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 497 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 498 | // If we have a prior diagnostic, it will be noting that the expression |
| 499 | // isn't a constant expression. This diagnostic is more important. |
| 500 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 501 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 502 | unsigned CallStackNotes = CallStackDepth - 1; |
| 503 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 504 | if (Limit) |
| 505 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 506 | if (CheckingPotentialConstantExpression) |
| 507 | CallStackNotes = 0; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 508 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 509 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 510 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 511 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 512 | addDiag(Loc, DiagId); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 513 | if (!CheckingPotentialConstantExpression) |
| 514 | addCallStack(Limit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 515 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 516 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 517 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 518 | return OptionalDiagnostic(); |
| 519 | } |
| 520 | |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 521 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 522 | = diag::note_invalid_subexpr_in_const_expr, |
| 523 | unsigned ExtraNotes = 0) { |
| 524 | if (EvalStatus.Diag) |
| 525 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 526 | HasActiveDiagnostic = false; |
| 527 | return OptionalDiagnostic(); |
| 528 | } |
| 529 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 530 | bool getIntOverflowCheckMode() { return IntOverflowCheckMode; } |
| 531 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 532 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 533 | /// expression. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 534 | template<typename LocArg> |
| 535 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 536 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 537 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 538 | // Don't override a previous diagnostic. |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 539 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
| 540 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 541 | return OptionalDiagnostic(); |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 542 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 543 | return Diag(Loc, DiagId, ExtraNotes); |
| 544 | } |
| 545 | |
| 546 | /// Add a note to a prior diagnostic. |
| 547 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 548 | if (!HasActiveDiagnostic) |
| 549 | return OptionalDiagnostic(); |
| 550 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 551 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 552 | |
| 553 | /// Add a stack of notes to a prior diagnostic. |
| 554 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 555 | if (HasActiveDiagnostic) { |
| 556 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 557 | Diags.begin(), Diags.end()); |
| 558 | } |
| 559 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 560 | |
| 561 | /// Should we continue evaluation as much as possible after encountering a |
| 562 | /// construct which can't be folded? |
| 563 | bool keepEvaluatingAfterFailure() { |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 564 | // Should return true in IntOverflowCheckMode, so that we check for |
| 565 | // overflow even if some subexpressions can't be evaluated as constants. |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 566 | return StepsLeft && (IntOverflowCheckMode || |
| 567 | (CheckingPotentialConstantExpression && |
| 568 | EvalStatus.Diag && EvalStatus.Diag->empty())); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 569 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 570 | }; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 571 | |
| 572 | /// Object used to treat all foldable expressions as constant expressions. |
| 573 | struct FoldConstant { |
| 574 | bool Enabled; |
| 575 | |
| 576 | explicit FoldConstant(EvalInfo &Info) |
| 577 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 578 | !Info.EvalStatus.HasSideEffects) { |
| 579 | } |
| 580 | // Treat the value we've computed since this object was created as constant. |
| 581 | void Fold(EvalInfo &Info) { |
| 582 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 583 | !Info.EvalStatus.HasSideEffects) |
| 584 | Info.EvalStatus.Diag->clear(); |
| 585 | } |
| 586 | }; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 587 | |
| 588 | /// RAII object used to suppress diagnostics and side-effects from a |
| 589 | /// speculative evaluation. |
| 590 | class SpeculativeEvaluationRAII { |
| 591 | EvalInfo &Info; |
| 592 | Expr::EvalStatus Old; |
| 593 | |
| 594 | public: |
| 595 | SpeculativeEvaluationRAII(EvalInfo &Info, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 596 | SmallVectorImpl<PartialDiagnosticAt> *NewDiag = 0) |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 597 | : Info(Info), Old(Info.EvalStatus) { |
| 598 | Info.EvalStatus.Diag = NewDiag; |
| 599 | } |
| 600 | ~SpeculativeEvaluationRAII() { |
| 601 | Info.EvalStatus = Old; |
| 602 | } |
| 603 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 604 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 605 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 606 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 607 | CheckSubobjectKind CSK) { |
| 608 | if (Invalid) |
| 609 | return false; |
| 610 | if (isOnePastTheEnd()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 611 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 612 | << CSK; |
| 613 | setInvalid(); |
| 614 | return false; |
| 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 620 | const Expr *E, uint64_t N) { |
| 621 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 622 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 623 | << static_cast<int>(N) << /*array*/ 0 |
| 624 | << static_cast<unsigned>(MostDerivedArraySize); |
| 625 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 626 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 627 | << static_cast<int>(N) << /*non-array*/ 1; |
| 628 | setInvalid(); |
| 629 | } |
| 630 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 631 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 632 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 633 | APValue *Arguments) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 634 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 635 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 636 | Info.CurrentCall = this; |
| 637 | ++Info.CallStackDepth; |
| 638 | } |
| 639 | |
| 640 | CallStackFrame::~CallStackFrame() { |
| 641 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 642 | --Info.CallStackDepth; |
| 643 | Info.CurrentCall = Caller; |
| 644 | } |
| 645 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 646 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 647 | |
| 648 | void EvalInfo::addCallStack(unsigned Limit) { |
| 649 | // Determine which calls to skip, if any. |
| 650 | unsigned ActiveCalls = CallStackDepth - 1; |
| 651 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 652 | if (Limit && Limit < ActiveCalls) { |
| 653 | SkipStart = Limit / 2 + Limit % 2; |
| 654 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 657 | // Walk the call stack and add the diagnostics. |
| 658 | unsigned CallIdx = 0; |
| 659 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 660 | Frame = Frame->Caller, ++CallIdx) { |
| 661 | // Skip this call? |
| 662 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 663 | if (CallIdx == SkipStart) { |
| 664 | // Note that we're skipping calls. |
| 665 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 666 | << unsigned(ActiveCalls - Limit); |
| 667 | } |
| 668 | continue; |
| 669 | } |
| 670 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 671 | SmallVector<char, 128> Buffer; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 672 | llvm::raw_svector_ostream Out(Buffer); |
| 673 | describeCall(Frame, Out); |
| 674 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 679 | struct ComplexValue { |
| 680 | private: |
| 681 | bool IsInt; |
| 682 | |
| 683 | public: |
| 684 | APSInt IntReal, IntImag; |
| 685 | APFloat FloatReal, FloatImag; |
| 686 | |
| 687 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 688 | |
| 689 | void makeComplexFloat() { IsInt = false; } |
| 690 | bool isComplexFloat() const { return !IsInt; } |
| 691 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 692 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 693 | |
| 694 | void makeComplexInt() { IsInt = true; } |
| 695 | bool isComplexInt() const { return IsInt; } |
| 696 | APSInt &getComplexIntReal() { return IntReal; } |
| 697 | APSInt &getComplexIntImag() { return IntImag; } |
| 698 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 699 | void moveInto(APValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 700 | if (isComplexFloat()) |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 701 | v = APValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 702 | else |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 703 | v = APValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 704 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 705 | void setFrom(const APValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 706 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 707 | if (v.isComplexFloat()) { |
| 708 | makeComplexFloat(); |
| 709 | FloatReal = v.getComplexFloatReal(); |
| 710 | FloatImag = v.getComplexFloatImag(); |
| 711 | } else { |
| 712 | makeComplexInt(); |
| 713 | IntReal = v.getComplexIntReal(); |
| 714 | IntImag = v.getComplexIntImag(); |
| 715 | } |
| 716 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 717 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 718 | |
| 719 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 720 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 721 | CharUnits Offset; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 722 | unsigned CallIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 723 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 724 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 725 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 726 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 727 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 728 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 729 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 730 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 731 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 732 | void moveInto(APValue &V) const { |
| 733 | if (Designator.Invalid) |
| 734 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 735 | else |
| 736 | V = APValue(Base, Offset, Designator.Entries, |
| 737 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 738 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 739 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 740 | assert(V.isLValue()); |
| 741 | Base = V.getLValueBase(); |
| 742 | Offset = V.getLValueOffset(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 743 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 744 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 747 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 748 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 749 | Offset = CharUnits::Zero(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 750 | CallIndex = I; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 751 | Designator = SubobjectDesignator(getType(B)); |
| 752 | } |
| 753 | |
| 754 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 755 | // a diagnostic and mark the designator as invalid. |
| 756 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 757 | CheckSubobjectKind CSK) { |
| 758 | if (Designator.Invalid) |
| 759 | return false; |
| 760 | if (!Base) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 761 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 762 | << CSK; |
| 763 | Designator.setInvalid(); |
| 764 | return false; |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | |
| 769 | // Check this LValue refers to an object. If not, set the designator to be |
| 770 | // invalid and emit a diagnostic. |
| 771 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 772 | // Outside C++11, do not build a designator referring to a subobject of |
| 773 | // any object: we won't use such a designator for anything. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 774 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 775 | Designator.setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 776 | return checkNullPointer(Info, E, CSK) && |
| 777 | Designator.checkSubobject(Info, E, CSK); |
| 778 | } |
| 779 | |
| 780 | void addDecl(EvalInfo &Info, const Expr *E, |
| 781 | const Decl *D, bool Virtual = false) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 782 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 783 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 784 | } |
| 785 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 786 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 787 | Designator.addArrayUnchecked(CAT); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 788 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 789 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 790 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 791 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 792 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 793 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 794 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 795 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 796 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 797 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 798 | |
| 799 | struct MemberPtr { |
| 800 | MemberPtr() {} |
| 801 | explicit MemberPtr(const ValueDecl *Decl) : |
| 802 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 803 | |
| 804 | /// The member or (direct or indirect) field referred to by this member |
| 805 | /// pointer, or 0 if this is a null member pointer. |
| 806 | const ValueDecl *getDecl() const { |
| 807 | return DeclAndIsDerivedMember.getPointer(); |
| 808 | } |
| 809 | /// Is this actually a member of some type derived from the relevant class? |
| 810 | bool isDerivedMember() const { |
| 811 | return DeclAndIsDerivedMember.getInt(); |
| 812 | } |
| 813 | /// Get the class which the declaration actually lives in. |
| 814 | const CXXRecordDecl *getContainingRecord() const { |
| 815 | return cast<CXXRecordDecl>( |
| 816 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 817 | } |
| 818 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 819 | void moveInto(APValue &V) const { |
| 820 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 821 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 822 | void setFrom(const APValue &V) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 823 | assert(V.isMemberPointer()); |
| 824 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 825 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 826 | Path.clear(); |
| 827 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 828 | Path.insert(Path.end(), P.begin(), P.end()); |
| 829 | } |
| 830 | |
| 831 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 832 | /// whether the member is a member of some class derived from the class type |
| 833 | /// of the member pointer. |
| 834 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 835 | /// Path - The path of base/derived classes from the member declaration's |
| 836 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 837 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 838 | |
| 839 | /// Perform a cast towards the class of the Decl (either up or down the |
| 840 | /// hierarchy). |
| 841 | bool castBack(const CXXRecordDecl *Class) { |
| 842 | assert(!Path.empty()); |
| 843 | const CXXRecordDecl *Expected; |
| 844 | if (Path.size() >= 2) |
| 845 | Expected = Path[Path.size() - 2]; |
| 846 | else |
| 847 | Expected = getContainingRecord(); |
| 848 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 849 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 850 | // if B does not contain the original member and is not a base or |
| 851 | // derived class of the class containing the original member, the result |
| 852 | // of the cast is undefined. |
| 853 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 854 | // (D::*). We consider that to be a language defect. |
| 855 | return false; |
| 856 | } |
| 857 | Path.pop_back(); |
| 858 | return true; |
| 859 | } |
| 860 | /// Perform a base-to-derived member pointer cast. |
| 861 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 862 | if (!getDecl()) |
| 863 | return true; |
| 864 | if (!isDerivedMember()) { |
| 865 | Path.push_back(Derived); |
| 866 | return true; |
| 867 | } |
| 868 | if (!castBack(Derived)) |
| 869 | return false; |
| 870 | if (Path.empty()) |
| 871 | DeclAndIsDerivedMember.setInt(false); |
| 872 | return true; |
| 873 | } |
| 874 | /// Perform a derived-to-base member pointer cast. |
| 875 | bool castToBase(const CXXRecordDecl *Base) { |
| 876 | if (!getDecl()) |
| 877 | return true; |
| 878 | if (Path.empty()) |
| 879 | DeclAndIsDerivedMember.setInt(true); |
| 880 | if (isDerivedMember()) { |
| 881 | Path.push_back(Base); |
| 882 | return true; |
| 883 | } |
| 884 | return castBack(Base); |
| 885 | } |
| 886 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 887 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 888 | /// Compare two member pointers, which are assumed to be of the same type. |
| 889 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 890 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 891 | return !LHS.getDecl() && !RHS.getDecl(); |
| 892 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 893 | return false; |
| 894 | return LHS.Path == RHS.Path; |
| 895 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 896 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 897 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 898 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 899 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 900 | const LValue &This, const Expr *E, |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 901 | bool AllowNonLiteralTypes = false); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 902 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 903 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 904 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 905 | EvalInfo &Info); |
| 906 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 907 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 908 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 909 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 910 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 911 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 912 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 913 | |
| 914 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 915 | // Misc utilities |
| 916 | //===----------------------------------------------------------------------===// |
| 917 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 918 | /// Produce a string describing the given constexpr call. |
| 919 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 920 | unsigned ArgIndex = 0; |
| 921 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 922 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 923 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 924 | |
| 925 | if (!IsMemberCall) |
| 926 | Out << *Frame->Callee << '('; |
| 927 | |
| 928 | if (Frame->This && IsMemberCall) { |
| 929 | APValue Val; |
| 930 | Frame->This->moveInto(Val); |
| 931 | Val.printPretty(Out, Frame->Info.Ctx, |
| 932 | Frame->This->Designator.MostDerivedType); |
| 933 | // FIXME: Add parens around Val if needed. |
| 934 | Out << "->" << *Frame->Callee << '('; |
| 935 | IsMemberCall = false; |
| 936 | } |
| 937 | |
| 938 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 939 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 940 | if (ArgIndex > (unsigned)IsMemberCall) |
| 941 | Out << ", "; |
| 942 | |
| 943 | const ParmVarDecl *Param = *I; |
| 944 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 945 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 946 | |
| 947 | if (ArgIndex == 0 && IsMemberCall) |
| 948 | Out << "->" << *Frame->Callee << '('; |
| 949 | } |
| 950 | |
| 951 | Out << ')'; |
| 952 | } |
| 953 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 954 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 955 | /// result. |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 956 | /// \return \c true if the caller should keep evaluating. |
| 957 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 958 | APValue Scratch; |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 959 | if (!Evaluate(Scratch, Info, E)) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 960 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 961 | return Info.keepEvaluatingAfterFailure(); |
| 962 | } |
| 963 | return true; |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 966 | /// Sign- or zero-extend a value to 64 bits. If it's already 64 bits, just |
| 967 | /// return its existing value. |
| 968 | static int64_t getExtValue(const APSInt &Value) { |
| 969 | return Value.isSigned() ? Value.getSExtValue() |
| 970 | : static_cast<int64_t>(Value.getZExtValue()); |
| 971 | } |
| 972 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 973 | /// Should this call expression be treated as a string literal? |
| 974 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 975 | unsigned Builtin = E->isBuiltinCall(); |
| 976 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 977 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 978 | } |
| 979 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 980 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 981 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 982 | // constant expression of pointer type that evaluates to... |
| 983 | |
| 984 | // ... a null pointer value, or a prvalue core constant expression of type |
| 985 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 986 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 987 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 988 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 989 | // ... the address of an object with static storage duration, |
| 990 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 991 | return VD->hasGlobalStorage(); |
| 992 | // ... the address of a function, |
| 993 | return isa<FunctionDecl>(D); |
| 994 | } |
| 995 | |
| 996 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 997 | switch (E->getStmtClass()) { |
| 998 | default: |
| 999 | return false; |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1000 | case Expr::CompoundLiteralExprClass: { |
| 1001 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1002 | return CLE->isFileScope() && CLE->isLValue(); |
| 1003 | } |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1004 | case Expr::MaterializeTemporaryExprClass: |
| 1005 | // A materialized temporary might have been lifetime-extended to static |
| 1006 | // storage duration. |
| 1007 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1008 | // A string literal has static storage duration. |
| 1009 | case Expr::StringLiteralClass: |
| 1010 | case Expr::PredefinedExprClass: |
| 1011 | case Expr::ObjCStringLiteralClass: |
| 1012 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1013 | case Expr::CXXTypeidExprClass: |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1014 | case Expr::CXXUuidofExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | case Expr::CallExprClass: |
| 1017 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1018 | // For GCC compatibility, &&label has static storage duration. |
| 1019 | case Expr::AddrLabelExprClass: |
| 1020 | return true; |
| 1021 | // A Block literal expression may be used as the initialization value for |
| 1022 | // Block variables at global or local static scope. |
| 1023 | case Expr::BlockExprClass: |
| 1024 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1025 | case Expr::ImplicitValueInitExprClass: |
| 1026 | // FIXME: |
| 1027 | // We can never form an lvalue with an implicit value initialization as its |
| 1028 | // base through expression evaluation, so these only appear in one case: the |
| 1029 | // implicit variable declaration we invent when checking whether a constexpr |
| 1030 | // constructor can produce a constant expression. We must assume that such |
| 1031 | // an expression might be a global lvalue. |
| 1032 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1033 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1036 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1037 | assert(Base && "no location for a null lvalue"); |
| 1038 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1039 | if (VD) |
| 1040 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1041 | else |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1042 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1043 | diag::note_constexpr_temporary_here); |
| 1044 | } |
| 1045 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1046 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1047 | /// value for an address or reference constant expression. Return true if we |
| 1048 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1049 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1050 | QualType Type, const LValue &LVal) { |
| 1051 | bool IsReferenceType = Type->isReferenceType(); |
| 1052 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1053 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1054 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1055 | |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1056 | // Check that the object is a global. Note that the fake 'this' object we |
| 1057 | // manufacture when checking potential constant expressions is conservatively |
| 1058 | // assumed to be global here. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1059 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1060 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1061 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1062 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1063 | << IsReferenceType << !Designator.Entries.empty() |
| 1064 | << !!VD << VD; |
| 1065 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1066 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1067 | Info.Diag(Loc); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1068 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1069 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1070 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1071 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1072 | assert((Info.CheckingPotentialConstantExpression || |
| 1073 | LVal.getLValueCallIndex() == 0) && |
| 1074 | "have call index for global lvalue"); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1075 | |
Hans Wennborg | 48def65 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1076 | // Check if this is a thread-local variable. |
| 1077 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1078 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1079 | if (Var->getTLSKind()) |
Hans Wennborg | 48def65 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1080 | return false; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1084 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1085 | // an extension: the standard requires them to point to an object. |
| 1086 | if (!IsReferenceType) |
| 1087 | return true; |
| 1088 | |
| 1089 | // A reference constant expression must refer to an object. |
| 1090 | if (!Base) { |
| 1091 | // FIXME: diagnostic |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1092 | Info.CCEDiag(Loc); |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1093 | return true; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1096 | // Does this refer one past the end of some object? |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1097 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1098 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1099 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1100 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1101 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1104 | return true; |
| 1105 | } |
| 1106 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1107 | /// Check that this core constant expression is of literal type, and if not, |
| 1108 | /// produce an appropriate diagnostic. |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1109 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
| 1110 | const LValue *This = 0) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1111 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1112 | return true; |
| 1113 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1114 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1115 | // constexpr constructors for o and its subobjects even if those objects |
| 1116 | // are of non-literal class types. |
| 1117 | if (Info.getLangOpts().CPlusPlus1y && This && |
Richard Smith | c45c8dd | 2013-05-16 05:04:51 +0000 | [diff] [blame] | 1118 | Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1119 | return true; |
| 1120 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1121 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1122 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1123 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1124 | << E->getType(); |
| 1125 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1126 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
| 1129 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1130 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1131 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1132 | /// check that the expression is of literal type. |
| 1133 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1134 | QualType Type, const APValue &Value) { |
| 1135 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1136 | // each subobject of its value shall have been initialized by a constant |
| 1137 | // expression. |
| 1138 | if (Value.isArray()) { |
| 1139 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1140 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1141 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1142 | Value.getArrayInitializedElt(I))) |
| 1143 | return false; |
| 1144 | } |
| 1145 | if (!Value.hasArrayFiller()) |
| 1146 | return true; |
| 1147 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1148 | Value.getArrayFiller()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1149 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1150 | if (Value.isUnion() && Value.getUnionField()) { |
| 1151 | return CheckConstantExpression(Info, DiagLoc, |
| 1152 | Value.getUnionField()->getType(), |
| 1153 | Value.getUnionValue()); |
| 1154 | } |
| 1155 | if (Value.isStruct()) { |
| 1156 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1157 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1158 | unsigned BaseIndex = 0; |
| 1159 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1160 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1161 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1162 | Value.getStructBase(BaseIndex))) |
| 1163 | return false; |
| 1164 | } |
| 1165 | } |
| 1166 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1167 | I != E; ++I) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1168 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1169 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1170 | return false; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | if (Value.isLValue()) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1175 | LValue LVal; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1176 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1177 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1178 | } |
| 1179 | |
| 1180 | // Everything else is fine. |
| 1181 | return true; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1184 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1185 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1189 | if (Value.CallIndex) |
| 1190 | return false; |
| 1191 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1192 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1195 | static bool IsWeakLValue(const LValue &Value) { |
| 1196 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1197 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1200 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1201 | // A null base expression indicates a null pointer. These are always |
| 1202 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1203 | if (!Value.getLValueBase()) { |
| 1204 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1205 | return true; |
| 1206 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1207 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1208 | // We have a non-null base. These are generally known to be true, but if it's |
| 1209 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1210 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1211 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1212 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1215 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1216 | switch (Val.getKind()) { |
| 1217 | case APValue::Uninitialized: |
| 1218 | return false; |
| 1219 | case APValue::Int: |
| 1220 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1221 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1222 | case APValue::Float: |
| 1223 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1224 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1225 | case APValue::ComplexInt: |
| 1226 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1227 | Val.getComplexIntImag().getBoolValue(); |
| 1228 | return true; |
| 1229 | case APValue::ComplexFloat: |
| 1230 | Result = !Val.getComplexFloatReal().isZero() || |
| 1231 | !Val.getComplexFloatImag().isZero(); |
| 1232 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1233 | case APValue::LValue: |
| 1234 | return EvalPointerValueAsBool(Val, Result); |
| 1235 | case APValue::MemberPointer: |
| 1236 | Result = Val.getMemberPointerDecl(); |
| 1237 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1238 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1239 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1240 | case APValue::Struct: |
| 1241 | case APValue::Union: |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1242 | case APValue::AddrLabelDiff: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1243 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1246 | llvm_unreachable("unknown APValue kind"); |
| 1247 | } |
| 1248 | |
| 1249 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1250 | EvalInfo &Info) { |
| 1251 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1252 | APValue Val; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1253 | if (!Evaluate(Val, Info, E)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1254 | return false; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1255 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1258 | template<typename T> |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1259 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1260 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1261 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1262 | << SrcValue << DestType; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1266 | QualType SrcType, const APFloat &Value, |
| 1267 | QualType DestType, APSInt &Result) { |
| 1268 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1269 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1270 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1272 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1273 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1274 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1275 | & APFloat::opInvalidOp) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1276 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1277 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1280 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1281 | QualType SrcType, QualType DestType, |
| 1282 | APFloat &Result) { |
| 1283 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1284 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1285 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1286 | APFloat::rmNearestTiesToEven, &ignored) |
| 1287 | & APFloat::opOverflow) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1288 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1289 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1292 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1293 | QualType DestType, QualType SrcType, |
| 1294 | APSInt &Value) { |
| 1295 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1296 | APSInt Result = Value; |
| 1297 | // Figure out if this is a truncate, extend or noop cast. |
| 1298 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1299 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1300 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1301 | return Result; |
| 1302 | } |
| 1303 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1304 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1305 | QualType SrcType, const APSInt &Value, |
| 1306 | QualType DestType, APFloat &Result) { |
| 1307 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1308 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1309 | APFloat::rmNearestTiesToEven) |
| 1310 | & APFloat::opOverflow) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1311 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1312 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1315 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1316 | llvm::APInt &Res) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1317 | APValue SVal; |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1318 | if (!Evaluate(SVal, Info, E)) |
| 1319 | return false; |
| 1320 | if (SVal.isInt()) { |
| 1321 | Res = SVal.getInt(); |
| 1322 | return true; |
| 1323 | } |
| 1324 | if (SVal.isFloat()) { |
| 1325 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1326 | return true; |
| 1327 | } |
| 1328 | if (SVal.isVector()) { |
| 1329 | QualType VecTy = E->getType(); |
| 1330 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1331 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1332 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1333 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1334 | Res = llvm::APInt::getNullValue(VecSize); |
| 1335 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1336 | APValue &Elt = SVal.getVectorElt(i); |
| 1337 | llvm::APInt EltAsInt; |
| 1338 | if (Elt.isInt()) { |
| 1339 | EltAsInt = Elt.getInt(); |
| 1340 | } else if (Elt.isFloat()) { |
| 1341 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1342 | } else { |
| 1343 | // Don't try to handle vectors of anything other than int or float |
| 1344 | // (not sure if it's possible to hit this case). |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1345 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1346 | return false; |
| 1347 | } |
| 1348 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1349 | if (BigEndian) |
| 1350 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1351 | else |
| 1352 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1353 | } |
| 1354 | return true; |
| 1355 | } |
| 1356 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1357 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1358 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1359 | return false; |
| 1360 | } |
| 1361 | |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1362 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 1363 | /// bits, and check for overflow in the original type (if that type was not an |
| 1364 | /// unsigned type). |
| 1365 | template<typename Operation> |
| 1366 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 1367 | const APSInt &LHS, const APSInt &RHS, |
| 1368 | unsigned BitWidth, Operation Op) { |
| 1369 | if (LHS.isUnsigned()) |
| 1370 | return Op(LHS, RHS); |
| 1371 | |
| 1372 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 1373 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 1374 | if (Result.extend(BitWidth) != Value) { |
| 1375 | if (Info.getIntOverflowCheckMode()) |
| 1376 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 1377 | diag::warn_integer_constant_overflow) |
| 1378 | << Result.toString(10) << E->getType(); |
| 1379 | else |
| 1380 | HandleOverflow(Info, E, Value, E->getType()); |
| 1381 | } |
| 1382 | return Result; |
| 1383 | } |
| 1384 | |
| 1385 | /// Perform the given binary integer operation. |
| 1386 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 1387 | BinaryOperatorKind Opcode, APSInt RHS, |
| 1388 | APSInt &Result) { |
| 1389 | switch (Opcode) { |
| 1390 | default: |
| 1391 | Info.Diag(E); |
| 1392 | return false; |
| 1393 | case BO_Mul: |
| 1394 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 1395 | std::multiplies<APSInt>()); |
| 1396 | return true; |
| 1397 | case BO_Add: |
| 1398 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1399 | std::plus<APSInt>()); |
| 1400 | return true; |
| 1401 | case BO_Sub: |
| 1402 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1403 | std::minus<APSInt>()); |
| 1404 | return true; |
| 1405 | case BO_And: Result = LHS & RHS; return true; |
| 1406 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 1407 | case BO_Or: Result = LHS | RHS; return true; |
| 1408 | case BO_Div: |
| 1409 | case BO_Rem: |
| 1410 | if (RHS == 0) { |
| 1411 | Info.Diag(E, diag::note_expr_divide_by_zero); |
| 1412 | return false; |
| 1413 | } |
| 1414 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. |
| 1415 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 1416 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 1417 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 1418 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 1419 | return true; |
| 1420 | case BO_Shl: { |
| 1421 | if (Info.getLangOpts().OpenCL) |
| 1422 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1423 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1424 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1425 | RHS.isUnsigned()); |
| 1426 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1427 | // During constant-folding, a negative shift is an opposite shift. Such |
| 1428 | // a shift is not a constant expression. |
| 1429 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1430 | RHS = -RHS; |
| 1431 | goto shift_right; |
| 1432 | } |
| 1433 | shift_left: |
| 1434 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 1435 | // the shifted type. |
| 1436 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1437 | if (SA != RHS) { |
| 1438 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1439 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1440 | } else if (LHS.isSigned()) { |
| 1441 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 1442 | // operand, and must not overflow the corresponding unsigned type. |
| 1443 | if (LHS.isNegative()) |
| 1444 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 1445 | else if (LHS.countLeadingZeros() < SA) |
| 1446 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 1447 | } |
| 1448 | Result = LHS << SA; |
| 1449 | return true; |
| 1450 | } |
| 1451 | case BO_Shr: { |
| 1452 | if (Info.getLangOpts().OpenCL) |
| 1453 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1454 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1455 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1456 | RHS.isUnsigned()); |
| 1457 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1458 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 1459 | // shift is not a constant expression. |
| 1460 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1461 | RHS = -RHS; |
| 1462 | goto shift_left; |
| 1463 | } |
| 1464 | shift_right: |
| 1465 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 1466 | // shifted type. |
| 1467 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1468 | if (SA != RHS) |
| 1469 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1470 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1471 | Result = LHS >> SA; |
| 1472 | return true; |
| 1473 | } |
| 1474 | |
| 1475 | case BO_LT: Result = LHS < RHS; return true; |
| 1476 | case BO_GT: Result = LHS > RHS; return true; |
| 1477 | case BO_LE: Result = LHS <= RHS; return true; |
| 1478 | case BO_GE: Result = LHS >= RHS; return true; |
| 1479 | case BO_EQ: Result = LHS == RHS; return true; |
| 1480 | case BO_NE: Result = LHS != RHS; return true; |
| 1481 | } |
| 1482 | } |
| 1483 | |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1484 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 1485 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 1486 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 1487 | const APFloat &RHS) { |
| 1488 | switch (Opcode) { |
| 1489 | default: |
| 1490 | Info.Diag(E); |
| 1491 | return false; |
| 1492 | case BO_Mul: |
| 1493 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1494 | break; |
| 1495 | case BO_Add: |
| 1496 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 1497 | break; |
| 1498 | case BO_Sub: |
| 1499 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1500 | break; |
| 1501 | case BO_Div: |
| 1502 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1503 | break; |
| 1504 | } |
| 1505 | |
| 1506 | if (LHS.isInfinity() || LHS.isNaN()) |
| 1507 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
| 1508 | return true; |
| 1509 | } |
| 1510 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1511 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1512 | /// truncating the lvalue's path to the given length. |
| 1513 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1514 | const RecordDecl *TruncatedType, |
| 1515 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1516 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1517 | |
| 1518 | // Check we actually point to a derived class object. |
| 1519 | if (TruncatedElements == D.Entries.size()) |
| 1520 | return true; |
| 1521 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1522 | "not casting to a derived class"); |
| 1523 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1524 | return false; |
| 1525 | |
| 1526 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1527 | const RecordDecl *RD = TruncatedType; |
| 1528 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1529 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1530 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1531 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1532 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1533 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1534 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1535 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1536 | RD = Base; |
| 1537 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1538 | D.Entries.resize(TruncatedElements); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1539 | return true; |
| 1540 | } |
| 1541 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1542 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1543 | const CXXRecordDecl *Derived, |
| 1544 | const CXXRecordDecl *Base, |
| 1545 | const ASTRecordLayout *RL = 0) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1546 | if (!RL) { |
| 1547 | if (Derived->isInvalidDecl()) return false; |
| 1548 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1549 | } |
| 1550 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1551 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1552 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1553 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1556 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1557 | const CXXRecordDecl *DerivedDecl, |
| 1558 | const CXXBaseSpecifier *Base) { |
| 1559 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1560 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1561 | if (!Base->isVirtual()) |
| 1562 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1563 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1564 | SubobjectDesignator &D = Obj.Designator; |
| 1565 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1566 | return false; |
| 1567 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1568 | // Extract most-derived object and corresponding type. |
| 1569 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1570 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1571 | return false; |
| 1572 | |
| 1573 | // Find the virtual base class. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1574 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1575 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1576 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1577 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1578 | return true; |
| 1579 | } |
| 1580 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1581 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 1582 | QualType Type, LValue &Result) { |
| 1583 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1584 | PathE = E->path_end(); |
| 1585 | PathI != PathE; ++PathI) { |
| 1586 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 1587 | *PathI)) |
| 1588 | return false; |
| 1589 | Type = (*PathI)->getType(); |
| 1590 | } |
| 1591 | return true; |
| 1592 | } |
| 1593 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1594 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1595 | /// currently described by LVal. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1596 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1597 | const FieldDecl *FD, |
| 1598 | const ASTRecordLayout *RL = 0) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1599 | if (!RL) { |
| 1600 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1601 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1602 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1603 | |
| 1604 | unsigned I = FD->getFieldIndex(); |
| 1605 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1606 | LVal.addDecl(Info, E, FD); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1607 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1610 | /// Update LVal to refer to the given indirect field. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1611 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1612 | LValue &LVal, |
| 1613 | const IndirectFieldDecl *IFD) { |
| 1614 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1615 | CE = IFD->chain_end(); C != CE; ++C) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1616 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C))) |
| 1617 | return false; |
| 1618 | return true; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1621 | /// Get the size of the given type in char units. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1622 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1623 | QualType Type, CharUnits &Size) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1624 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1625 | // extension. |
| 1626 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1627 | Size = CharUnits::One(); |
| 1628 | return true; |
| 1629 | } |
| 1630 | |
| 1631 | if (!Type->isConstantSizeType()) { |
| 1632 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1633 | // FIXME: Better diagnostic. |
| 1634 | Info.Diag(Loc); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1639 | return true; |
| 1640 | } |
| 1641 | |
| 1642 | /// Update a pointer value to model pointer arithmetic. |
| 1643 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1644 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1645 | /// \param LVal - The pointer value to be updated. |
| 1646 | /// \param EltTy - The pointee type represented by LVal. |
| 1647 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1648 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1649 | LValue &LVal, QualType EltTy, |
| 1650 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1651 | CharUnits SizeOfPointee; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1652 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1653 | return false; |
| 1654 | |
| 1655 | // Compute the new offset in the appropriate width. |
| 1656 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1657 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1658 | return true; |
| 1659 | } |
| 1660 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1661 | /// Update an lvalue to refer to a component of a complex number. |
| 1662 | /// \param Info - Information about the ongoing evaluation. |
| 1663 | /// \param LVal - The lvalue to be updated. |
| 1664 | /// \param EltTy - The complex number's component type. |
| 1665 | /// \param Imag - False for the real component, true for the imaginary. |
| 1666 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1667 | LValue &LVal, QualType EltTy, |
| 1668 | bool Imag) { |
| 1669 | if (Imag) { |
| 1670 | CharUnits SizeOfComponent; |
| 1671 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1672 | return false; |
| 1673 | LVal.Offset += SizeOfComponent; |
| 1674 | } |
| 1675 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1679 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1680 | /// |
| 1681 | /// \param Info Information about the ongoing evaluation. |
| 1682 | /// \param E An expression to be used when printing diagnostics. |
| 1683 | /// \param VD The variable whose initializer should be obtained. |
| 1684 | /// \param Frame The frame in which the variable was created. Must be null |
| 1685 | /// if this variable is not local to the evaluation. |
| 1686 | /// \param Result Filled in with a pointer to the value of the variable. |
| 1687 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1688 | const VarDecl *VD, CallStackFrame *Frame, |
| 1689 | APValue *&Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1690 | // If this is a parameter to an active constexpr function call, perform |
| 1691 | // argument substitution. |
| 1692 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1693 | // Assume arguments of a potential constant expression are unknown |
| 1694 | // constant expressions. |
| 1695 | if (Info.CheckingPotentialConstantExpression) |
| 1696 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1697 | if (!Frame || !Frame->Arguments) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1698 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1699 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1700 | } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1701 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1702 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1703 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1704 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1705 | // If this is a local variable, dig out its value. |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1706 | if (Frame) { |
| 1707 | Result = &Frame->Temporaries[VD]; |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1708 | // If we've carried on past an unevaluatable local variable initializer, |
| 1709 | // we can't go any further. This can happen during potential constant |
| 1710 | // expression checking. |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1711 | return !Result->isUninit(); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1714 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1715 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1716 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1717 | // If we're checking a potential constant expression, the variable could be |
| 1718 | // initialized later. |
| 1719 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1720 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1721 | return false; |
| 1722 | } |
| 1723 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1724 | // If we're currently evaluating the initializer of this declaration, use that |
| 1725 | // in-flight value. |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1726 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1727 | Result = Info.EvaluatingDeclValue; |
| 1728 | return !Result->isUninit(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1731 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1732 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1733 | if (VD->isWeak()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1734 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1735 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1736 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1737 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1738 | // Check that we can fold the initializer. In C++, we will have already done |
| 1739 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1740 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1741 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1742 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1743 | Notes.size() + 1) << VD; |
| 1744 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1745 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1746 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1747 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1748 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1749 | Notes.size() + 1) << VD; |
| 1750 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1751 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1752 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1753 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1754 | Result = VD->getEvaluatedValue(); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1755 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1758 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1759 | Qualifiers Quals = T.getQualifiers(); |
| 1760 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1761 | } |
| 1762 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1763 | /// Get the base index of the given base class within an APValue representing |
| 1764 | /// the given derived class. |
| 1765 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1766 | const CXXRecordDecl *Base) { |
| 1767 | Base = Base->getCanonicalDecl(); |
| 1768 | unsigned Index = 0; |
| 1769 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1770 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1771 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1772 | return Index; |
| 1773 | } |
| 1774 | |
| 1775 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1776 | } |
| 1777 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1778 | /// Extract the value of a character from a string literal. |
| 1779 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 1780 | uint64_t Index) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1781 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1782 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1783 | const ConstantArrayType *CAT = |
| 1784 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1785 | assert(CAT && "string literal isn't an array"); |
| 1786 | QualType CharType = CAT->getElementType(); |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1787 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1788 | |
| 1789 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1790 | CharType->isUnsignedIntegerType()); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1791 | if (Index < S->getLength()) |
| 1792 | Value = S->getCodeUnit(Index); |
| 1793 | return Value; |
| 1794 | } |
| 1795 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1796 | // Expand a string literal into an array of characters. |
| 1797 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 1798 | APValue &Result) { |
| 1799 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1800 | const ConstantArrayType *CAT = |
| 1801 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1802 | assert(CAT && "string literal isn't an array"); |
| 1803 | QualType CharType = CAT->getElementType(); |
| 1804 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 1805 | |
| 1806 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 1807 | Result = APValue(APValue::UninitArray(), |
| 1808 | std::min(S->getLength(), Elts), Elts); |
| 1809 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1810 | CharType->isUnsignedIntegerType()); |
| 1811 | if (Result.hasArrayFiller()) |
| 1812 | Result.getArrayFiller() = APValue(Value); |
| 1813 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 1814 | Value = S->getCodeUnit(I); |
| 1815 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | // Expand an array so that it has more than Index filled elements. |
| 1820 | static void expandArray(APValue &Array, unsigned Index) { |
| 1821 | unsigned Size = Array.getArraySize(); |
| 1822 | assert(Index < Size); |
| 1823 | |
| 1824 | // Always at least double the number of elements for which we store a value. |
| 1825 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 1826 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 1827 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 1828 | |
| 1829 | // Copy the data across. |
| 1830 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 1831 | for (unsigned I = 0; I != OldElts; ++I) |
| 1832 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 1833 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 1834 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 1835 | if (NewValue.hasArrayFiller()) |
| 1836 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 1837 | Array.swap(NewValue); |
| 1838 | } |
| 1839 | |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1840 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1841 | enum AccessKinds { |
| 1842 | AK_Read, |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 1843 | AK_Assign, |
| 1844 | AK_Increment, |
| 1845 | AK_Decrement |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1846 | }; |
| 1847 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1848 | /// A handle to a complete object (an object that is not a subobject of |
| 1849 | /// another object). |
| 1850 | struct CompleteObject { |
| 1851 | /// The value of the complete object. |
| 1852 | APValue *Value; |
| 1853 | /// The type of the complete object. |
| 1854 | QualType Type; |
| 1855 | |
| 1856 | CompleteObject() : Value(0) {} |
| 1857 | CompleteObject(APValue *Value, QualType Type) |
| 1858 | : Value(Value), Type(Type) { |
| 1859 | assert(Value && "missing value for complete object"); |
| 1860 | } |
| 1861 | |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 1862 | LLVM_EXPLICIT operator bool() const { return Value; } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1863 | }; |
| 1864 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1865 | /// Find the designated sub-object of an rvalue. |
| 1866 | template<typename SubobjectHandler> |
| 1867 | typename SubobjectHandler::result_type |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1868 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1869 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1870 | if (Sub.Invalid) |
| 1871 | // A diagnostic will have already been produced. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1872 | return handler.failed(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1873 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1874 | if (Info.getLangOpts().CPlusPlus11) |
| 1875 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1876 | << handler.AccessKind; |
| 1877 | else |
| 1878 | Info.Diag(E); |
| 1879 | return handler.failed(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1880 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1881 | if (Sub.Entries.empty()) |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1882 | return handler.found(*Obj.Value, Obj.Type); |
| 1883 | if (Info.CheckingPotentialConstantExpression && Obj.Value->isUninit()) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1884 | // This object might be initialized later. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1885 | return handler.failed(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1886 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1887 | APValue *O = Obj.Value; |
| 1888 | QualType ObjType = Obj.Type; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1889 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1890 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1891 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1892 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1893 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1894 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1895 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1896 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1897 | // Note, it should not be possible to form a pointer with a valid |
| 1898 | // designator which points more than one past the end of the array. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1899 | if (Info.getLangOpts().CPlusPlus11) |
| 1900 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1901 | << handler.AccessKind; |
| 1902 | else |
| 1903 | Info.Diag(E); |
| 1904 | return handler.failed(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1905 | } |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1906 | |
| 1907 | ObjType = CAT->getElementType(); |
| 1908 | |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1909 | // An array object is represented as either an Array APValue or as an |
| 1910 | // LValue which refers to a string literal. |
| 1911 | if (O->isLValue()) { |
| 1912 | assert(I == N - 1 && "extracting subobject of character?"); |
| 1913 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1914 | if (handler.AccessKind != AK_Read) |
| 1915 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 1916 | *O); |
| 1917 | else |
| 1918 | return handler.foundString(*O, ObjType, Index); |
| 1919 | } |
| 1920 | |
| 1921 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1922 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1923 | else if (handler.AccessKind != AK_Read) { |
| 1924 | expandArray(*O, Index); |
| 1925 | O = &O->getArrayInitializedElt(Index); |
| 1926 | } else |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1927 | O = &O->getArrayFiller(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1928 | } else if (ObjType->isAnyComplexType()) { |
| 1929 | // Next subobject is a complex number. |
| 1930 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 1931 | if (Index > 1) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1932 | if (Info.getLangOpts().CPlusPlus11) |
| 1933 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1934 | << handler.AccessKind; |
| 1935 | else |
| 1936 | Info.Diag(E); |
| 1937 | return handler.failed(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1938 | } |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1939 | |
| 1940 | bool WasConstQualified = ObjType.isConstQualified(); |
| 1941 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1942 | if (WasConstQualified) |
| 1943 | ObjType.addConst(); |
| 1944 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1945 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 1946 | if (O->isComplexInt()) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1947 | return handler.found(Index ? O->getComplexIntImag() |
| 1948 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1949 | } else { |
| 1950 | assert(O->isComplexFloat()); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1951 | return handler.found(Index ? O->getComplexFloatImag() |
| 1952 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1953 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1954 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1955 | if (Field->isMutable() && handler.AccessKind == AK_Read) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1956 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1957 | << Field; |
| 1958 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1959 | return handler.failed(); |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1962 | // Next subobject is a class, struct or union field. |
| 1963 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1964 | if (RD->isUnion()) { |
| 1965 | const FieldDecl *UnionField = O->getUnionField(); |
| 1966 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1967 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1968 | Info.Diag(E, diag::note_constexpr_access_inactive_union_member) |
| 1969 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 1970 | return handler.failed(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1971 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1972 | O = &O->getUnionValue(); |
| 1973 | } else |
| 1974 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1975 | |
| 1976 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1977 | ObjType = Field->getType(); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1978 | if (WasConstQualified && !Field->isMutable()) |
| 1979 | ObjType.addConst(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1980 | |
| 1981 | if (ObjType.isVolatileQualified()) { |
| 1982 | if (Info.getLangOpts().CPlusPlus) { |
| 1983 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1984 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 1985 | << handler.AccessKind << 2 << Field; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1986 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1987 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1988 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1989 | } |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1990 | return handler.failed(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1991 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1992 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1993 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1994 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1995 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1996 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1997 | |
| 1998 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1999 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2000 | if (WasConstQualified) |
| 2001 | ObjType.addConst(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2002 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2003 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2004 | if (O->isUninit()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2005 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2006 | Info.Diag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
| 2007 | return handler.failed(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2008 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2011 | return handler.found(*O, ObjType); |
| 2012 | } |
| 2013 | |
Benjamin Kramer | 888d345 | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2014 | namespace { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2015 | struct ExtractSubobjectHandler { |
| 2016 | EvalInfo &Info; |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2017 | APValue &Result; |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2018 | |
| 2019 | static const AccessKinds AccessKind = AK_Read; |
| 2020 | |
| 2021 | typedef bool result_type; |
| 2022 | bool failed() { return false; } |
| 2023 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2024 | Result = Subobj; |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2025 | return true; |
| 2026 | } |
| 2027 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2028 | Result = APValue(Value); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2029 | return true; |
| 2030 | } |
| 2031 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2032 | Result = APValue(Value); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2033 | return true; |
| 2034 | } |
| 2035 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2036 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2037 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2038 | return true; |
| 2039 | } |
| 2040 | }; |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2041 | } // end anonymous namespace |
| 2042 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2043 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2044 | |
| 2045 | /// Extract the designated sub-object of an rvalue. |
| 2046 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2047 | const CompleteObject &Obj, |
| 2048 | const SubobjectDesignator &Sub, |
| 2049 | APValue &Result) { |
| 2050 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2051 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2054 | namespace { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2055 | struct ModifySubobjectHandler { |
| 2056 | EvalInfo &Info; |
| 2057 | APValue &NewVal; |
| 2058 | const Expr *E; |
| 2059 | |
| 2060 | typedef bool result_type; |
| 2061 | static const AccessKinds AccessKind = AK_Assign; |
| 2062 | |
| 2063 | bool checkConst(QualType QT) { |
| 2064 | // Assigning to a const object has undefined behavior. |
| 2065 | if (QT.isConstQualified()) { |
| 2066 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2067 | return false; |
| 2068 | } |
| 2069 | return true; |
| 2070 | } |
| 2071 | |
| 2072 | bool failed() { return false; } |
| 2073 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2074 | if (!checkConst(SubobjType)) |
| 2075 | return false; |
| 2076 | // We've been given ownership of NewVal, so just swap it in. |
| 2077 | Subobj.swap(NewVal); |
| 2078 | return true; |
| 2079 | } |
| 2080 | bool found(APSInt &Value, QualType SubobjType) { |
| 2081 | if (!checkConst(SubobjType)) |
| 2082 | return false; |
| 2083 | if (!NewVal.isInt()) { |
| 2084 | // Maybe trying to write a cast pointer value into a complex? |
| 2085 | Info.Diag(E); |
| 2086 | return false; |
| 2087 | } |
| 2088 | Value = NewVal.getInt(); |
| 2089 | return true; |
| 2090 | } |
| 2091 | bool found(APFloat &Value, QualType SubobjType) { |
| 2092 | if (!checkConst(SubobjType)) |
| 2093 | return false; |
| 2094 | Value = NewVal.getFloat(); |
| 2095 | return true; |
| 2096 | } |
| 2097 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2098 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 2099 | } |
| 2100 | }; |
Benjamin Kramer | 888d345 | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2101 | } // end anonymous namespace |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2102 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2103 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 2104 | |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2105 | /// Update the designated sub-object of an rvalue to the given value. |
| 2106 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2107 | const CompleteObject &Obj, |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2108 | const SubobjectDesignator &Sub, |
| 2109 | APValue &NewVal) { |
| 2110 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2111 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2114 | /// Find the position where two subobject designators diverge, or equivalently |
| 2115 | /// the length of the common initial subsequence. |
| 2116 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 2117 | const SubobjectDesignator &A, |
| 2118 | const SubobjectDesignator &B, |
| 2119 | bool &WasArrayIndex) { |
| 2120 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 2121 | for (/**/; I != N; ++I) { |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2122 | if (!ObjType.isNull() && |
| 2123 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2124 | // Next subobject is an array element. |
| 2125 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 2126 | WasArrayIndex = true; |
| 2127 | return I; |
| 2128 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2129 | if (ObjType->isAnyComplexType()) |
| 2130 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2131 | else |
| 2132 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2133 | } else { |
| 2134 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 2135 | WasArrayIndex = false; |
| 2136 | return I; |
| 2137 | } |
| 2138 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 2139 | // Next subobject is a field. |
| 2140 | ObjType = FD->getType(); |
| 2141 | else |
| 2142 | // Next subobject is a base class. |
| 2143 | ObjType = QualType(); |
| 2144 | } |
| 2145 | } |
| 2146 | WasArrayIndex = false; |
| 2147 | return I; |
| 2148 | } |
| 2149 | |
| 2150 | /// Determine whether the given subobject designators refer to elements of the |
| 2151 | /// same array object. |
| 2152 | static bool AreElementsOfSameArray(QualType ObjType, |
| 2153 | const SubobjectDesignator &A, |
| 2154 | const SubobjectDesignator &B) { |
| 2155 | if (A.Entries.size() != B.Entries.size()) |
| 2156 | return false; |
| 2157 | |
| 2158 | bool IsArray = A.MostDerivedArraySize != 0; |
| 2159 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 2160 | // A is a subobject of the array element. |
| 2161 | return false; |
| 2162 | |
| 2163 | // If A (and B) designates an array element, the last entry will be the array |
| 2164 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 2165 | // of length 1' case, and the entire path must match. |
| 2166 | bool WasArrayIndex; |
| 2167 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 2168 | return CommonLength >= A.Entries.size() - IsArray; |
| 2169 | } |
| 2170 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2171 | /// Find the complete object to which an LValue refers. |
| 2172 | CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, AccessKinds AK, |
| 2173 | const LValue &LVal, QualType LValType) { |
| 2174 | if (!LVal.Base) { |
| 2175 | Info.Diag(E, diag::note_constexpr_access_null) << AK; |
| 2176 | return CompleteObject(); |
| 2177 | } |
| 2178 | |
| 2179 | CallStackFrame *Frame = 0; |
| 2180 | if (LVal.CallIndex) { |
| 2181 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 2182 | if (!Frame) { |
| 2183 | Info.Diag(E, diag::note_constexpr_lifetime_ended, 1) |
| 2184 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 2185 | NoteLValueLocation(Info, LVal.Base); |
| 2186 | return CompleteObject(); |
| 2187 | } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 2191 | // is not a constant expression (even if the object is non-volatile). We also |
| 2192 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 2193 | // semantics. |
| 2194 | if (LValType.isVolatileQualified()) { |
| 2195 | if (Info.getLangOpts().CPlusPlus) |
| 2196 | Info.Diag(E, diag::note_constexpr_access_volatile_type) |
| 2197 | << AK << LValType; |
| 2198 | else |
| 2199 | Info.Diag(E); |
| 2200 | return CompleteObject(); |
| 2201 | } |
| 2202 | |
| 2203 | // Compute value storage location and type of base object. |
| 2204 | APValue *BaseVal = 0; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2205 | QualType BaseType = getType(LVal.Base); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2206 | |
| 2207 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 2208 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 2209 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 2210 | // expressions are constant expressions too. Inside constexpr functions, |
| 2211 | // parameters are constant expressions even if they're non-const. |
| 2212 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 2213 | // both readable and writable inside constant expressions. |
| 2214 | // In C, such things can also be folded, although they are not ICEs. |
| 2215 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 2216 | if (VD) { |
| 2217 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 2218 | VD = VDef; |
| 2219 | } |
| 2220 | if (!VD || VD->isInvalidDecl()) { |
| 2221 | Info.Diag(E); |
| 2222 | return CompleteObject(); |
| 2223 | } |
| 2224 | |
| 2225 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2226 | if (BaseType.isVolatileQualified()) { |
| 2227 | if (Info.getLangOpts().CPlusPlus) { |
| 2228 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2229 | << AK << 1 << VD; |
| 2230 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2231 | } else { |
| 2232 | Info.Diag(E); |
| 2233 | } |
| 2234 | return CompleteObject(); |
| 2235 | } |
| 2236 | |
| 2237 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 2238 | // the variable we're reading must be const. |
| 2239 | if (!Frame) { |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2240 | if (Info.getLangOpts().CPlusPlus1y && |
| 2241 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 2242 | // OK, we can read and modify an object if we're in the process of |
| 2243 | // evaluating its initializer, because its lifetime began in this |
| 2244 | // evaluation. |
| 2245 | } else if (AK != AK_Read) { |
| 2246 | // All the remaining cases only permit reading. |
| 2247 | Info.Diag(E, diag::note_constexpr_modify_global); |
| 2248 | return CompleteObject(); |
| 2249 | } else if (VD->isConstexpr()) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2250 | // OK, we can read this variable. |
| 2251 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| 2252 | if (!BaseType.isConstQualified()) { |
| 2253 | if (Info.getLangOpts().CPlusPlus) { |
| 2254 | Info.Diag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 2255 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2256 | } else { |
| 2257 | Info.Diag(E); |
| 2258 | } |
| 2259 | return CompleteObject(); |
| 2260 | } |
| 2261 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 2262 | // We support folding of const floating-point types, in order to make |
| 2263 | // static const data members of such types (supported as an extension) |
| 2264 | // more useful. |
| 2265 | if (Info.getLangOpts().CPlusPlus11) { |
| 2266 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2267 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2268 | } else { |
| 2269 | Info.CCEDiag(E); |
| 2270 | } |
| 2271 | } else { |
| 2272 | // FIXME: Allow folding of values of any literal type in all languages. |
| 2273 | if (Info.getLangOpts().CPlusPlus11) { |
| 2274 | Info.Diag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2275 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2276 | } else { |
| 2277 | Info.Diag(E); |
| 2278 | } |
| 2279 | return CompleteObject(); |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal)) |
| 2284 | return CompleteObject(); |
| 2285 | } else { |
| 2286 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 2287 | |
| 2288 | if (!Frame) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2289 | if (const MaterializeTemporaryExpr *MTE = |
| 2290 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 2291 | assert(MTE->getStorageDuration() == SD_Static && |
| 2292 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2293 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2294 | // Per C++1y [expr.const]p2: |
| 2295 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 2296 | // - a [...] glvalue of integral or enumeration type that refers to |
| 2297 | // a non-volatile const object [...] |
| 2298 | // [...] |
| 2299 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 2300 | // object whose lifetime began within the evaluation of e. |
| 2301 | // |
| 2302 | // C++11 misses the 'began within the evaluation of e' check and |
| 2303 | // instead allows all temporaries, including things like: |
| 2304 | // int &&r = 1; |
| 2305 | // int x = ++r; |
| 2306 | // constexpr int k = r; |
| 2307 | // Therefore we use the C++1y rules in C++11 too. |
| 2308 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 2309 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 2310 | if (!(BaseType.isConstQualified() && |
| 2311 | BaseType->isIntegralOrEnumerationType()) && |
| 2312 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
| 2313 | Info.Diag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
| 2314 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2315 | return CompleteObject(); |
| 2316 | } |
| 2317 | |
| 2318 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 2319 | assert(BaseVal && "got reference to unevaluated temporary"); |
| 2320 | } else { |
| 2321 | Info.Diag(E); |
| 2322 | return CompleteObject(); |
| 2323 | } |
| 2324 | } else { |
| 2325 | BaseVal = &Frame->Temporaries[Base]; |
| 2326 | } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 2329 | if (BaseType.isVolatileQualified()) { |
| 2330 | if (Info.getLangOpts().CPlusPlus) { |
| 2331 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2332 | << AK << 0; |
| 2333 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2334 | } else { |
| 2335 | Info.Diag(E); |
| 2336 | } |
| 2337 | return CompleteObject(); |
| 2338 | } |
| 2339 | } |
| 2340 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2341 | // During the construction of an object, it is not yet 'const'. |
| 2342 | // FIXME: We don't set up EvaluatingDecl for local variables or temporaries, |
| 2343 | // and this doesn't do quite the right thing for const subobjects of the |
| 2344 | // object under construction. |
| 2345 | if (LVal.getLValueBase() == Info.EvaluatingDecl) { |
| 2346 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 2347 | BaseType.removeLocalConst(); |
| 2348 | } |
| 2349 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2350 | // In C++1y, we can't safely access any mutable state when checking a |
| 2351 | // potential constant expression. |
| 2352 | if (Frame && Info.getLangOpts().CPlusPlus1y && |
| 2353 | Info.CheckingPotentialConstantExpression) |
| 2354 | return CompleteObject(); |
| 2355 | |
| 2356 | return CompleteObject(BaseVal, BaseType); |
| 2357 | } |
| 2358 | |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2359 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 2360 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 2361 | /// glvalue referred to by an entity of reference type. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2362 | /// |
| 2363 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2364 | /// \param Conv - The expression for which we are performing the conversion. |
| 2365 | /// Used for diagnostics. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2366 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 2367 | /// case of a non-class type). |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2368 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 2369 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2370 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2371 | QualType Type, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2372 | const LValue &LVal, APValue &RVal) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2373 | if (LVal.Designator.Invalid) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2374 | return false; |
| 2375 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2376 | // Check for special cases where there is no existing APValue to look at. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2377 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2378 | if (!LVal.Designator.Invalid && Base && !LVal.CallIndex && |
| 2379 | !Type.isVolatileQualified()) { |
| 2380 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 2381 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 2382 | // initializer until now for such expressions. Such an expression can't be |
| 2383 | // an ICE in C, so this only matters for fold. |
| 2384 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2385 | if (Type.isVolatileQualified()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2386 | Info.Diag(Conv); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2387 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2388 | } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2389 | APValue Lit; |
| 2390 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 2391 | return false; |
| 2392 | CompleteObject LitObj(&Lit, Base->getType()); |
| 2393 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
| 2394 | } else if (isa<StringLiteral>(Base)) { |
| 2395 | // We represent a string literal array as an lvalue pointing at the |
| 2396 | // corresponding expression, rather than building an array of chars. |
| 2397 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 2398 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
| 2399 | CompleteObject StrObj(&Str, Base->getType()); |
| 2400 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2401 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2404 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 2405 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2409 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2410 | QualType LValType, APValue &Val) { |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2411 | if (LVal.Designator.Invalid) |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2412 | return false; |
| 2413 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2414 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2415 | Info.Diag(E); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2416 | return false; |
| 2417 | } |
| 2418 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2419 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2420 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2423 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { |
| 2424 | return T->isSignedIntegerType() && |
| 2425 | Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); |
| 2426 | } |
| 2427 | |
| 2428 | namespace { |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2429 | struct CompoundAssignSubobjectHandler { |
| 2430 | EvalInfo &Info; |
| 2431 | const Expr *E; |
| 2432 | QualType PromotedLHSType; |
| 2433 | BinaryOperatorKind Opcode; |
| 2434 | const APValue &RHS; |
| 2435 | |
| 2436 | static const AccessKinds AccessKind = AK_Assign; |
| 2437 | |
| 2438 | typedef bool result_type; |
| 2439 | |
| 2440 | bool checkConst(QualType QT) { |
| 2441 | // Assigning to a const object has undefined behavior. |
| 2442 | if (QT.isConstQualified()) { |
| 2443 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2444 | return false; |
| 2445 | } |
| 2446 | return true; |
| 2447 | } |
| 2448 | |
| 2449 | bool failed() { return false; } |
| 2450 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2451 | switch (Subobj.getKind()) { |
| 2452 | case APValue::Int: |
| 2453 | return found(Subobj.getInt(), SubobjType); |
| 2454 | case APValue::Float: |
| 2455 | return found(Subobj.getFloat(), SubobjType); |
| 2456 | case APValue::ComplexInt: |
| 2457 | case APValue::ComplexFloat: |
| 2458 | // FIXME: Implement complex compound assignment. |
| 2459 | Info.Diag(E); |
| 2460 | return false; |
| 2461 | case APValue::LValue: |
| 2462 | return foundPointer(Subobj, SubobjType); |
| 2463 | default: |
| 2464 | // FIXME: can this happen? |
| 2465 | Info.Diag(E); |
| 2466 | return false; |
| 2467 | } |
| 2468 | } |
| 2469 | bool found(APSInt &Value, QualType SubobjType) { |
| 2470 | if (!checkConst(SubobjType)) |
| 2471 | return false; |
| 2472 | |
| 2473 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 2474 | // We don't support compound assignment on integer-cast-to-pointer |
| 2475 | // values. |
| 2476 | Info.Diag(E); |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 2481 | SubobjType, Value); |
| 2482 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 2483 | return false; |
| 2484 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 2485 | return true; |
| 2486 | } |
| 2487 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2488 | return checkConst(SubobjType) && |
| 2489 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 2490 | Value) && |
| 2491 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 2492 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2493 | } |
| 2494 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2495 | if (!checkConst(SubobjType)) |
| 2496 | return false; |
| 2497 | |
| 2498 | QualType PointeeType; |
| 2499 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2500 | PointeeType = PT->getPointeeType(); |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (PointeeType.isNull() || !RHS.isInt() || |
| 2503 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2504 | Info.Diag(E); |
| 2505 | return false; |
| 2506 | } |
| 2507 | |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2508 | int64_t Offset = getExtValue(RHS.getInt()); |
| 2509 | if (Opcode == BO_Sub) |
| 2510 | Offset = -Offset; |
| 2511 | |
| 2512 | LValue LVal; |
| 2513 | LVal.setFrom(Info.Ctx, Subobj); |
| 2514 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 2515 | return false; |
| 2516 | LVal.moveInto(Subobj); |
| 2517 | return true; |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2518 | } |
| 2519 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2520 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2521 | } |
| 2522 | }; |
| 2523 | } // end anonymous namespace |
| 2524 | |
| 2525 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 2526 | |
| 2527 | /// Perform a compound assignment of LVal <op>= RVal. |
| 2528 | static bool handleCompoundAssignment( |
| 2529 | EvalInfo &Info, const Expr *E, |
| 2530 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 2531 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 2532 | if (LVal.Designator.Invalid) |
| 2533 | return false; |
| 2534 | |
| 2535 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2536 | Info.Diag(E); |
| 2537 | return false; |
| 2538 | } |
| 2539 | |
| 2540 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2541 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 2542 | RVal }; |
| 2543 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2544 | } |
| 2545 | |
| 2546 | namespace { |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2547 | struct IncDecSubobjectHandler { |
| 2548 | EvalInfo &Info; |
| 2549 | const Expr *E; |
| 2550 | AccessKinds AccessKind; |
| 2551 | APValue *Old; |
| 2552 | |
| 2553 | typedef bool result_type; |
| 2554 | |
| 2555 | bool checkConst(QualType QT) { |
| 2556 | // Assigning to a const object has undefined behavior. |
| 2557 | if (QT.isConstQualified()) { |
| 2558 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2559 | return false; |
| 2560 | } |
| 2561 | return true; |
| 2562 | } |
| 2563 | |
| 2564 | bool failed() { return false; } |
| 2565 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2566 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 2567 | // if we're post-incrementing a complex. |
| 2568 | if (Old) { |
| 2569 | *Old = Subobj; |
| 2570 | Old = 0; |
| 2571 | } |
| 2572 | |
| 2573 | switch (Subobj.getKind()) { |
| 2574 | case APValue::Int: |
| 2575 | return found(Subobj.getInt(), SubobjType); |
| 2576 | case APValue::Float: |
| 2577 | return found(Subobj.getFloat(), SubobjType); |
| 2578 | case APValue::ComplexInt: |
| 2579 | return found(Subobj.getComplexIntReal(), |
| 2580 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2581 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2582 | case APValue::ComplexFloat: |
| 2583 | return found(Subobj.getComplexFloatReal(), |
| 2584 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2585 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2586 | case APValue::LValue: |
| 2587 | return foundPointer(Subobj, SubobjType); |
| 2588 | default: |
| 2589 | // FIXME: can this happen? |
| 2590 | Info.Diag(E); |
| 2591 | return false; |
| 2592 | } |
| 2593 | } |
| 2594 | bool found(APSInt &Value, QualType SubobjType) { |
| 2595 | if (!checkConst(SubobjType)) |
| 2596 | return false; |
| 2597 | |
| 2598 | if (!SubobjType->isIntegerType()) { |
| 2599 | // We don't support increment / decrement on integer-cast-to-pointer |
| 2600 | // values. |
| 2601 | Info.Diag(E); |
| 2602 | return false; |
| 2603 | } |
| 2604 | |
| 2605 | if (Old) *Old = APValue(Value); |
| 2606 | |
| 2607 | // bool arithmetic promotes to int, and the conversion back to bool |
| 2608 | // doesn't reduce mod 2^n, so special-case it. |
| 2609 | if (SubobjType->isBooleanType()) { |
| 2610 | if (AccessKind == AK_Increment) |
| 2611 | Value = 1; |
| 2612 | else |
| 2613 | Value = !Value; |
| 2614 | return true; |
| 2615 | } |
| 2616 | |
| 2617 | bool WasNegative = Value.isNegative(); |
| 2618 | if (AccessKind == AK_Increment) { |
| 2619 | ++Value; |
| 2620 | |
| 2621 | if (!WasNegative && Value.isNegative() && |
| 2622 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2623 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 2624 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2625 | } |
| 2626 | } else { |
| 2627 | --Value; |
| 2628 | |
| 2629 | if (WasNegative && !Value.isNegative() && |
| 2630 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2631 | unsigned BitWidth = Value.getBitWidth(); |
| 2632 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 2633 | ActualValue.setBit(BitWidth); |
| 2634 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2635 | } |
| 2636 | } |
| 2637 | return true; |
| 2638 | } |
| 2639 | bool found(APFloat &Value, QualType SubobjType) { |
| 2640 | if (!checkConst(SubobjType)) |
| 2641 | return false; |
| 2642 | |
| 2643 | if (Old) *Old = APValue(Value); |
| 2644 | |
| 2645 | APFloat One(Value.getSemantics(), 1); |
| 2646 | if (AccessKind == AK_Increment) |
| 2647 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 2648 | else |
| 2649 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 2650 | return true; |
| 2651 | } |
| 2652 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2653 | if (!checkConst(SubobjType)) |
| 2654 | return false; |
| 2655 | |
| 2656 | QualType PointeeType; |
| 2657 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2658 | PointeeType = PT->getPointeeType(); |
| 2659 | else { |
| 2660 | Info.Diag(E); |
| 2661 | return false; |
| 2662 | } |
| 2663 | |
| 2664 | LValue LVal; |
| 2665 | LVal.setFrom(Info.Ctx, Subobj); |
| 2666 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 2667 | AccessKind == AK_Increment ? 1 : -1)) |
| 2668 | return false; |
| 2669 | LVal.moveInto(Subobj); |
| 2670 | return true; |
| 2671 | } |
| 2672 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2673 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2674 | } |
| 2675 | }; |
| 2676 | } // end anonymous namespace |
| 2677 | |
| 2678 | /// Perform an increment or decrement on LVal. |
| 2679 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 2680 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 2681 | if (LVal.Designator.Invalid) |
| 2682 | return false; |
| 2683 | |
| 2684 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2685 | Info.Diag(E); |
| 2686 | return false; |
| 2687 | } |
| 2688 | |
| 2689 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 2690 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 2691 | IncDecSubobjectHandler Handler = { Info, E, AK, Old }; |
| 2692 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2693 | } |
| 2694 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2695 | /// Build an lvalue for the object argument of a member function call. |
| 2696 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 2697 | LValue &This) { |
| 2698 | if (Object->getType()->isPointerType()) |
| 2699 | return EvaluatePointer(Object, This, Info); |
| 2700 | |
| 2701 | if (Object->isGLValue()) |
| 2702 | return EvaluateLValue(Object, This, Info); |
| 2703 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2704 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2705 | return EvaluateTemporary(Object, This, Info); |
| 2706 | |
| 2707 | return false; |
| 2708 | } |
| 2709 | |
| 2710 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 2711 | /// lvalue referring to the result. |
| 2712 | /// |
| 2713 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2714 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 2715 | /// \param RHS - The member pointer expression. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2716 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 2717 | /// the resulting LValue subobject designator. This is not possible when |
| 2718 | /// creating a bound member function. |
| 2719 | /// \return The field or method declaration to which the member pointer refers, |
| 2720 | /// or 0 if evaluation fails. |
| 2721 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2722 | QualType LVType, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2723 | LValue &LV, |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2724 | const Expr *RHS, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2725 | bool IncludeMember = true) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2726 | MemberPtr MemPtr; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2727 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2728 | return 0; |
| 2729 | |
| 2730 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 2731 | // member value, the behavior is undefined. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2732 | if (!MemPtr.getDecl()) { |
| 2733 | // FIXME: Specific diagnostic. |
| 2734 | Info.Diag(RHS); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2735 | return 0; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2736 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2737 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2738 | if (MemPtr.isDerivedMember()) { |
| 2739 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2740 | // The end of the derived-to-base path for the base object must match the |
| 2741 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2742 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2743 | LV.Designator.Entries.size()) { |
| 2744 | Info.Diag(RHS); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2745 | return 0; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2746 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2747 | unsigned PathLengthToMember = |
| 2748 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 2749 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 2750 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 2751 | LV.Designator.Entries[PathLengthToMember + I]); |
| 2752 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2753 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
| 2754 | Info.Diag(RHS); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2755 | return 0; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2756 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2760 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2761 | PathLengthToMember)) |
| 2762 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2763 | } else if (!MemPtr.Path.empty()) { |
| 2764 | // Extend the LValue path with the member pointer's path. |
| 2765 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 2766 | MemPtr.Path.size() + IncludeMember); |
| 2767 | |
| 2768 | // Walk down to the appropriate base class. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2769 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 2770 | LVType = PT->getPointeeType(); |
| 2771 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 2772 | assert(RD && "member pointer access on non-class-type expression"); |
| 2773 | // The first class in the path is that of the lvalue. |
| 2774 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 2775 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2776 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2777 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2778 | RD = Base; |
| 2779 | } |
| 2780 | // Finally cast to the class containing the member. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2781 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 2782 | MemPtr.getContainingRecord())) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2783 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | // Add the member. Note that we cannot build bound member functions here. |
| 2787 | if (IncludeMember) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2788 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2789 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2790 | return 0; |
| 2791 | } else if (const IndirectFieldDecl *IFD = |
| 2792 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2793 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2794 | return 0; |
| 2795 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2796 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2797 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | return MemPtr.getDecl(); |
| 2801 | } |
| 2802 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2803 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 2804 | const BinaryOperator *BO, |
| 2805 | LValue &LV, |
| 2806 | bool IncludeMember = true) { |
| 2807 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 2808 | |
| 2809 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
| 2810 | if (Info.keepEvaluatingAfterFailure()) { |
| 2811 | MemberPtr MemPtr; |
| 2812 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 2813 | } |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
| 2817 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 2818 | BO->getRHS(), IncludeMember); |
| 2819 | } |
| 2820 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2821 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 2822 | /// the provided lvalue, which currently refers to the base object. |
| 2823 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 2824 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2825 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2826 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2827 | return false; |
| 2828 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2829 | QualType TargetQT = E->getType(); |
| 2830 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 2831 | TargetQT = PT->getPointeeType(); |
| 2832 | |
| 2833 | // Check this cast lands within the final derived-to-base subobject path. |
| 2834 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2835 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2836 | << D.MostDerivedType << TargetQT; |
| 2837 | return false; |
| 2838 | } |
| 2839 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2840 | // Check the type of the final cast. We don't need to check the path, |
| 2841 | // since a cast can only be formed if the path is unique. |
| 2842 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2843 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 2844 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2845 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 2846 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2847 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2848 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2849 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2850 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2851 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2852 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2853 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2854 | |
| 2855 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2856 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2859 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2860 | enum EvalStmtResult { |
| 2861 | /// Evaluation failed. |
| 2862 | ESR_Failed, |
| 2863 | /// Hit a 'return' statement. |
| 2864 | ESR_Returned, |
| 2865 | /// Evaluation succeeded. |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2866 | ESR_Succeeded, |
| 2867 | /// Hit a 'continue' statement. |
| 2868 | ESR_Continue, |
| 2869 | /// Hit a 'break' statement. |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2870 | ESR_Break, |
| 2871 | /// Still scanning for 'case' or 'default' statement. |
| 2872 | ESR_CaseNotFound |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2873 | }; |
| 2874 | } |
| 2875 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2876 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 2877 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2878 | // We don't need to evaluate the initializer for a static local. |
| 2879 | if (!VD->hasLocalStorage()) |
| 2880 | return true; |
| 2881 | |
| 2882 | LValue Result; |
| 2883 | Result.set(VD, Info.CurrentCall->Index); |
| 2884 | APValue &Val = Info.CurrentCall->Temporaries[VD]; |
| 2885 | |
| 2886 | if (!EvaluateInPlace(Val, Info, Result, VD->getInit())) { |
| 2887 | // Wipe out any partially-computed value, to allow tracking that this |
| 2888 | // evaluation failed. |
| 2889 | Val = APValue(); |
| 2890 | return false; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | return true; |
| 2895 | } |
| 2896 | |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2897 | /// Evaluate a condition (either a variable declaration or an expression). |
| 2898 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 2899 | const Expr *Cond, bool &Result) { |
| 2900 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 2901 | return false; |
| 2902 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 2903 | } |
| 2904 | |
| 2905 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2906 | const Stmt *S, const SwitchCase *SC = 0); |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2907 | |
| 2908 | /// Evaluate the body of a loop, and translate the result as appropriate. |
| 2909 | static EvalStmtResult EvaluateLoopBody(APValue &Result, EvalInfo &Info, |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2910 | const Stmt *Body, |
| 2911 | const SwitchCase *Case = 0) { |
| 2912 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2913 | case ESR_Break: |
| 2914 | return ESR_Succeeded; |
| 2915 | case ESR_Succeeded: |
| 2916 | case ESR_Continue: |
| 2917 | return ESR_Continue; |
| 2918 | case ESR_Failed: |
| 2919 | case ESR_Returned: |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2920 | case ESR_CaseNotFound: |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2921 | return ESR; |
| 2922 | } |
Hans Wennborg | dbce2c6 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 2923 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2926 | /// Evaluate a switch statement. |
| 2927 | static EvalStmtResult EvaluateSwitch(APValue &Result, EvalInfo &Info, |
| 2928 | const SwitchStmt *SS) { |
| 2929 | // Evaluate the switch condition. |
| 2930 | if (SS->getConditionVariable() && |
| 2931 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 2932 | return ESR_Failed; |
| 2933 | APSInt Value; |
| 2934 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 2935 | return ESR_Failed; |
| 2936 | |
| 2937 | // Find the switch case corresponding to the value of the condition. |
| 2938 | // FIXME: Cache this lookup. |
| 2939 | const SwitchCase *Found = 0; |
| 2940 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 2941 | SC = SC->getNextSwitchCase()) { |
| 2942 | if (isa<DefaultStmt>(SC)) { |
| 2943 | Found = SC; |
| 2944 | continue; |
| 2945 | } |
| 2946 | |
| 2947 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 2948 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 2949 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 2950 | : LHS; |
| 2951 | if (LHS <= Value && Value <= RHS) { |
| 2952 | Found = SC; |
| 2953 | break; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | if (!Found) |
| 2958 | return ESR_Succeeded; |
| 2959 | |
| 2960 | // Search the switch body for the switch case and evaluate it from there. |
| 2961 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 2962 | case ESR_Break: |
| 2963 | return ESR_Succeeded; |
| 2964 | case ESR_Succeeded: |
| 2965 | case ESR_Continue: |
| 2966 | case ESR_Failed: |
| 2967 | case ESR_Returned: |
| 2968 | return ESR; |
| 2969 | case ESR_CaseNotFound: |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2970 | llvm_unreachable("couldn't find switch case"); |
| 2971 | } |
Richard Smith | 1071b9f | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 2972 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2975 | // Evaluate a statement. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2976 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2977 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | e756563 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 2978 | if (!Info.nextStep(S)) |
| 2979 | return ESR_Failed; |
| 2980 | |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 2981 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 2982 | // substatements until we hit the label. |
| 2983 | if (Case) { |
| 2984 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 2985 | // jump over. However, such objects must be of class type with a trivial |
| 2986 | // default constructor that initialize all subobjects, so must be empty, |
| 2987 | // so this almost never matters. |
| 2988 | switch (S->getStmtClass()) { |
| 2989 | case Stmt::CompoundStmtClass: |
| 2990 | // FIXME: Precompute which substatement of a compound statement we |
| 2991 | // would jump to, and go straight there rather than performing a |
| 2992 | // linear scan each time. |
| 2993 | case Stmt::LabelStmtClass: |
| 2994 | case Stmt::AttributedStmtClass: |
| 2995 | case Stmt::DoStmtClass: |
| 2996 | break; |
| 2997 | |
| 2998 | case Stmt::CaseStmtClass: |
| 2999 | case Stmt::DefaultStmtClass: |
| 3000 | if (Case == S) |
| 3001 | Case = 0; |
| 3002 | break; |
| 3003 | |
| 3004 | case Stmt::IfStmtClass: { |
| 3005 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3006 | // straight there rather than scanning both sides. |
| 3007 | const IfStmt *IS = cast<IfStmt>(S); |
| 3008 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3009 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3010 | return ESR; |
| 3011 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3012 | } |
| 3013 | |
| 3014 | case Stmt::WhileStmtClass: { |
| 3015 | EvalStmtResult ESR = |
| 3016 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3017 | if (ESR != ESR_Continue) |
| 3018 | return ESR; |
| 3019 | break; |
| 3020 | } |
| 3021 | |
| 3022 | case Stmt::ForStmtClass: { |
| 3023 | const ForStmt *FS = cast<ForStmt>(S); |
| 3024 | EvalStmtResult ESR = |
| 3025 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 3026 | if (ESR != ESR_Continue) |
| 3027 | return ESR; |
| 3028 | if (FS->getInc() && !EvaluateIgnoredValue(Info, FS->getInc())) |
| 3029 | return ESR_Failed; |
| 3030 | break; |
| 3031 | } |
| 3032 | |
| 3033 | case Stmt::DeclStmtClass: |
| 3034 | // FIXME: If the variable has initialization that can't be jumped over, |
| 3035 | // bail out of any immediately-surrounding compound-statement too. |
| 3036 | default: |
| 3037 | return ESR_CaseNotFound; |
| 3038 | } |
| 3039 | } |
| 3040 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3041 | // FIXME: Mark all temporaries in the current frame as destroyed at |
| 3042 | // the end of each full-expression. |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3043 | switch (S->getStmtClass()) { |
| 3044 | default: |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3045 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3046 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 3047 | // be evaluated. |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3048 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3049 | return ESR_Failed; |
| 3050 | return ESR_Succeeded; |
| 3051 | } |
| 3052 | |
| 3053 | Info.Diag(S->getLocStart()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3054 | return ESR_Failed; |
| 3055 | |
| 3056 | case Stmt::NullStmtClass: |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3057 | return ESR_Succeeded; |
| 3058 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3059 | case Stmt::DeclStmtClass: { |
| 3060 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 3061 | for (DeclStmt::const_decl_iterator DclIt = DS->decl_begin(), |
| 3062 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) |
| 3063 | if (!EvaluateDecl(Info, *DclIt) && !Info.keepEvaluatingAfterFailure()) |
| 3064 | return ESR_Failed; |
| 3065 | return ESR_Succeeded; |
| 3066 | } |
| 3067 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3068 | case Stmt::ReturnStmtClass: { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3069 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3070 | if (RetExpr && !Evaluate(Result, Info, RetExpr)) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3071 | return ESR_Failed; |
| 3072 | return ESR_Returned; |
| 3073 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3074 | |
| 3075 | case Stmt::CompoundStmtClass: { |
| 3076 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 3077 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 3078 | BE = CS->body_end(); BI != BE; ++BI) { |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3079 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI, Case); |
| 3080 | if (ESR == ESR_Succeeded) |
| 3081 | Case = 0; |
| 3082 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3083 | return ESR; |
| 3084 | } |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3085 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3086 | } |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3087 | |
| 3088 | case Stmt::IfStmtClass: { |
| 3089 | const IfStmt *IS = cast<IfStmt>(S); |
| 3090 | |
| 3091 | // Evaluate the condition, as either a var decl or as an expression. |
| 3092 | bool Cond; |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3093 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3094 | return ESR_Failed; |
| 3095 | |
| 3096 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 3097 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 3098 | if (ESR != ESR_Succeeded) |
| 3099 | return ESR; |
| 3100 | } |
| 3101 | return ESR_Succeeded; |
| 3102 | } |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3103 | |
| 3104 | case Stmt::WhileStmtClass: { |
| 3105 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 3106 | while (true) { |
| 3107 | bool Continue; |
| 3108 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 3109 | Continue)) |
| 3110 | return ESR_Failed; |
| 3111 | if (!Continue) |
| 3112 | break; |
| 3113 | |
| 3114 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 3115 | if (ESR != ESR_Continue) |
| 3116 | return ESR; |
| 3117 | } |
| 3118 | return ESR_Succeeded; |
| 3119 | } |
| 3120 | |
| 3121 | case Stmt::DoStmtClass: { |
| 3122 | const DoStmt *DS = cast<DoStmt>(S); |
| 3123 | bool Continue; |
| 3124 | do { |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3125 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3126 | if (ESR != ESR_Continue) |
| 3127 | return ESR; |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3128 | Case = 0; |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3129 | |
| 3130 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 3131 | return ESR_Failed; |
| 3132 | } while (Continue); |
| 3133 | return ESR_Succeeded; |
| 3134 | } |
| 3135 | |
| 3136 | case Stmt::ForStmtClass: { |
| 3137 | const ForStmt *FS = cast<ForStmt>(S); |
| 3138 | if (FS->getInit()) { |
| 3139 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 3140 | if (ESR != ESR_Succeeded) |
| 3141 | return ESR; |
| 3142 | } |
| 3143 | while (true) { |
| 3144 | bool Continue = true; |
| 3145 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 3146 | FS->getCond(), Continue)) |
| 3147 | return ESR_Failed; |
| 3148 | if (!Continue) |
| 3149 | break; |
| 3150 | |
| 3151 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3152 | if (ESR != ESR_Continue) |
| 3153 | return ESR; |
| 3154 | |
| 3155 | if (FS->getInc() && !EvaluateIgnoredValue(Info, FS->getInc())) |
| 3156 | return ESR_Failed; |
| 3157 | } |
| 3158 | return ESR_Succeeded; |
| 3159 | } |
| 3160 | |
Richard Smith | 692eafd | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3161 | case Stmt::CXXForRangeStmtClass: { |
| 3162 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
| 3163 | |
| 3164 | // Initialize the __range variable. |
| 3165 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 3166 | if (ESR != ESR_Succeeded) |
| 3167 | return ESR; |
| 3168 | |
| 3169 | // Create the __begin and __end iterators. |
| 3170 | ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt()); |
| 3171 | if (ESR != ESR_Succeeded) |
| 3172 | return ESR; |
| 3173 | |
| 3174 | while (true) { |
| 3175 | // Condition: __begin != __end. |
| 3176 | bool Continue = true; |
| 3177 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 3178 | return ESR_Failed; |
| 3179 | if (!Continue) |
| 3180 | break; |
| 3181 | |
| 3182 | // User's variable declaration, initialized by *__begin. |
| 3183 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 3184 | if (ESR != ESR_Succeeded) |
| 3185 | return ESR; |
| 3186 | |
| 3187 | // Loop body. |
| 3188 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3189 | if (ESR != ESR_Continue) |
| 3190 | return ESR; |
| 3191 | |
| 3192 | // Increment: ++__begin |
| 3193 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3194 | return ESR_Failed; |
| 3195 | } |
| 3196 | |
| 3197 | return ESR_Succeeded; |
| 3198 | } |
| 3199 | |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3200 | case Stmt::SwitchStmtClass: |
| 3201 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 3202 | |
Richard Smith | ce61715 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3203 | case Stmt::ContinueStmtClass: |
| 3204 | return ESR_Continue; |
| 3205 | |
| 3206 | case Stmt::BreakStmtClass: |
| 3207 | return ESR_Break; |
Richard Smith | 284b3cb | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3208 | |
| 3209 | case Stmt::LabelStmtClass: |
| 3210 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 3211 | |
| 3212 | case Stmt::AttributedStmtClass: |
| 3213 | // As a general principle, C++11 attributes can be ignored without |
| 3214 | // any semantic impact. |
| 3215 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 3216 | Case); |
| 3217 | |
| 3218 | case Stmt::CaseStmtClass: |
| 3219 | case Stmt::DefaultStmtClass: |
| 3220 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3221 | } |
| 3222 | } |
| 3223 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3224 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 3225 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 3226 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 3227 | /// so we need special handling. |
| 3228 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3229 | const CXXConstructorDecl *CD, |
| 3230 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3231 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 3232 | return false; |
| 3233 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3234 | // Value-initialization does not call a trivial default constructor, so such a |
| 3235 | // call is a core constant expression whether or not the constructor is |
| 3236 | // constexpr. |
| 3237 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3238 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3239 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 3240 | // we should be much more explicit about why it's not constexpr. |
| 3241 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 3242 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 3243 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3244 | } else { |
| 3245 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 3246 | } |
| 3247 | } |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3251 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 3252 | /// expression. |
| 3253 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 3254 | const FunctionDecl *Declaration, |
| 3255 | const FunctionDecl *Definition) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3256 | // Potential constant expressions can contain calls to declared, but not yet |
| 3257 | // defined, constexpr functions. |
| 3258 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 3259 | Declaration->isConstexpr()) |
| 3260 | return false; |
| 3261 | |
Richard Smith | f039e3e | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 3262 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 3263 | // We will have produced a relevant diagnostic while parsing it. |
| 3264 | if (Declaration->isInvalidDecl()) |
| 3265 | return false; |
| 3266 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3267 | // Can we evaluate this function call? |
| 3268 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 3269 | return true; |
| 3270 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3271 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3272 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3273 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 3274 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3275 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 3276 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 3277 | << DiagDecl; |
| 3278 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 3279 | } else { |
| 3280 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 3281 | } |
| 3282 | return false; |
| 3283 | } |
| 3284 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3285 | namespace { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3286 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 3290 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 3291 | EvalInfo &Info) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3292 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3293 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3294 | I != E; ++I) { |
| 3295 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 3296 | // If we're checking for a potential constant expression, evaluate all |
| 3297 | // initializers even if some of them fail. |
| 3298 | if (!Info.keepEvaluatingAfterFailure()) |
| 3299 | return false; |
| 3300 | Success = false; |
| 3301 | } |
| 3302 | } |
| 3303 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3306 | /// Evaluate a function call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3307 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 3308 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3309 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3310 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3311 | ArgVector ArgValues(Args.size()); |
| 3312 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3313 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3314 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3315 | if (!Info.CheckCallLimit(CallLoc)) |
| 3316 | return false; |
| 3317 | |
| 3318 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3319 | |
| 3320 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 3321 | // essential for unions, where the operations performed by the assignment |
| 3322 | // operator cannot be represented as statements. |
| 3323 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 3324 | if (MD && MD->isDefaulted() && MD->isTrivial()) { |
| 3325 | assert(This && |
| 3326 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 3327 | LValue RHS; |
| 3328 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 3329 | APValue RHSValue; |
| 3330 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 3331 | RHS, RHSValue)) |
| 3332 | return false; |
| 3333 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 3334 | RHSValue)) |
| 3335 | return false; |
| 3336 | This->moveInto(Result); |
| 3337 | return true; |
| 3338 | } |
| 3339 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3340 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3341 | if (ESR == ESR_Succeeded) { |
| 3342 | if (Callee->getResultType()->isVoidType()) |
| 3343 | return true; |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3344 | Info.Diag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3345 | } |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3346 | return ESR == ESR_Returned; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3349 | /// Evaluate a constructor call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3350 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3351 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3352 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3353 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3354 | ArgVector ArgValues(Args.size()); |
| 3355 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3356 | return false; |
| 3357 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3358 | if (!Info.CheckCallLimit(CallLoc)) |
| 3359 | return false; |
| 3360 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3361 | const CXXRecordDecl *RD = Definition->getParent(); |
| 3362 | if (RD->getNumVBases()) { |
| 3363 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 3364 | return false; |
| 3365 | } |
| 3366 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3367 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3368 | |
| 3369 | // If it's a delegating constructor, just delegate. |
| 3370 | if (Definition->isDelegatingConstructor()) { |
| 3371 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3372 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 3373 | return false; |
| 3374 | return EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3377 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 3378 | // essential for unions, where the operations performed by the constructor |
| 3379 | // cannot be represented by ctor-initializers. |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3380 | if (Definition->isDefaulted() && |
Douglas Gregor | f6cfe8b | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 3381 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 3382 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3383 | LValue RHS; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3384 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3385 | return handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3386 | RHS, Result); |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3390 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3391 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3392 | std::distance(RD->field_begin(), RD->field_end())); |
| 3393 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3394 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3395 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3396 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3397 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3398 | unsigned BasesSeen = 0; |
| 3399 | #ifndef NDEBUG |
| 3400 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 3401 | #endif |
| 3402 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 3403 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3404 | LValue Subobject = This; |
| 3405 | APValue *Value = &Result; |
| 3406 | |
| 3407 | // Determine the subobject to initialize. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3408 | if ((*I)->isBaseInitializer()) { |
| 3409 | QualType BaseType((*I)->getBaseClass(), 0); |
| 3410 | #ifndef NDEBUG |
| 3411 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3412 | // definition. We have already checked for virtual base classes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3413 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 3414 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 3415 | "base class initializers not in expected order"); |
| 3416 | ++BaseIt; |
| 3417 | #endif |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3418 | if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
| 3419 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 3420 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3421 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3422 | } else if (FieldDecl *FD = (*I)->getMember()) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3423 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout)) |
| 3424 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3425 | if (RD->isUnion()) { |
| 3426 | Result = APValue(FD); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3427 | Value = &Result.getUnionValue(); |
| 3428 | } else { |
| 3429 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 3430 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3431 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3432 | // Walk the indirect field decl's chain to find the object to initialize, |
| 3433 | // and make sure we've initialized every step along it. |
| 3434 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 3435 | CE = IFD->chain_end(); |
| 3436 | C != CE; ++C) { |
| 3437 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 3438 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 3439 | // Switch the union field if it differs. This happens if we had |
| 3440 | // preceding zero-initialization, and we're now initializing a union |
| 3441 | // subobject other than the first. |
| 3442 | // FIXME: In this case, the values of the other subobjects are |
| 3443 | // specified, since zero-initialization sets all padding bits to zero. |
| 3444 | if (Value->isUninit() || |
| 3445 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 3446 | if (CD->isUnion()) |
| 3447 | *Value = APValue(FD); |
| 3448 | else |
| 3449 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 3450 | std::distance(CD->field_begin(), CD->field_end())); |
| 3451 | } |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3452 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD)) |
| 3453 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3454 | if (CD->isUnion()) |
| 3455 | Value = &Value->getUnionValue(); |
| 3456 | else |
| 3457 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3458 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3459 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3460 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3461 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3462 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3463 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit())) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3464 | // If we're checking for a potential constant expression, evaluate all |
| 3465 | // initializers even if some of them fail. |
| 3466 | if (!Info.keepEvaluatingAfterFailure()) |
| 3467 | return false; |
| 3468 | Success = false; |
| 3469 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3472 | return Success && |
| 3473 | EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3476 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3477 | // Generic Evaluation |
| 3478 | //===----------------------------------------------------------------------===// |
| 3479 | namespace { |
| 3480 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3481 | // FIXME: RetTy is always bool. Remove it. |
| 3482 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3483 | class ExprEvaluatorBase |
| 3484 | : public ConstStmtVisitor<Derived, RetTy> { |
| 3485 | private: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3486 | RetTy DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3487 | return static_cast<Derived*>(this)->Success(V, E); |
| 3488 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3489 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 3490 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3491 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3492 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3493 | // Check whether a conditional operator with a non-constant condition is a |
| 3494 | // potential constant expression. If neither arm is a potential constant |
| 3495 | // expression, then the conditional operator is not either. |
| 3496 | template<typename ConditionalOperator> |
| 3497 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| 3498 | assert(Info.CheckingPotentialConstantExpression); |
| 3499 | |
| 3500 | // Speculatively evaluate both arms. |
| 3501 | { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3502 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3503 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 3504 | |
| 3505 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 3506 | if (Diag.empty()) |
| 3507 | return; |
| 3508 | |
| 3509 | Diag.clear(); |
| 3510 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 3511 | if (Diag.empty()) |
| 3512 | return; |
| 3513 | } |
| 3514 | |
| 3515 | Error(E, diag::note_constexpr_conditional_never_const); |
| 3516 | } |
| 3517 | |
| 3518 | |
| 3519 | template<typename ConditionalOperator> |
| 3520 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 3521 | bool BoolResult; |
| 3522 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| 3523 | if (Info.CheckingPotentialConstantExpression) |
| 3524 | CheckPotentialConstantConditional(E); |
| 3525 | return false; |
| 3526 | } |
| 3527 | |
| 3528 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 3529 | return StmtVisitorTy::Visit(EvalExpr); |
| 3530 | } |
| 3531 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3532 | protected: |
| 3533 | EvalInfo &Info; |
| 3534 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 3535 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 3536 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3537 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3538 | return Info.CCEDiag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3541 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
| 3542 | |
| 3543 | public: |
| 3544 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 3545 | |
| 3546 | EvalInfo &getEvalInfo() { return Info; } |
| 3547 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3548 | /// Report an evaluation error. This should only be called when an error is |
| 3549 | /// first discovered. When propagating an error, just return false. |
| 3550 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3551 | Info.Diag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3552 | return false; |
| 3553 | } |
| 3554 | bool Error(const Expr *E) { |
| 3555 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 3556 | } |
| 3557 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3558 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3559 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3560 | } |
| 3561 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3562 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | RetTy VisitParenExpr(const ParenExpr *E) |
| 3566 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3567 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 3568 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3569 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 3570 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3571 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 3572 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 3573 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 3574 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3575 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 3576 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 3577 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 3578 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3579 | RetTy VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) |
| 3580 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 3581 | // We cannot create any objects for which cleanups are required, so there is |
| 3582 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 3583 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 3584 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3585 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3586 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 3587 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 3588 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3589 | } |
| 3590 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 3591 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 3592 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3593 | } |
| 3594 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3595 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 3596 | switch (E->getOpcode()) { |
| 3597 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3598 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3599 | |
| 3600 | case BO_Comma: |
| 3601 | VisitIgnoredValue(E->getLHS()); |
| 3602 | return StmtVisitorTy::Visit(E->getRHS()); |
| 3603 | |
| 3604 | case BO_PtrMemD: |
| 3605 | case BO_PtrMemI: { |
| 3606 | LValue Obj; |
| 3607 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 3608 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3609 | APValue Result; |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3610 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3611 | return false; |
| 3612 | return DerivedSuccess(Result, E); |
| 3613 | } |
| 3614 | } |
| 3615 | } |
| 3616 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3617 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3618 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 3619 | // even though it's not quite the same thing. |
| 3620 | if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()], |
| 3621 | Info, E->getCommon())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3622 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3623 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3624 | return HandleConditionalOperator(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3628 | bool IsBcpCall = false; |
| 3629 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 3630 | // the result is a constant expression if it can be folded without |
| 3631 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 3632 | // for discussion. |
| 3633 | if (const CallExpr *CallCE = |
| 3634 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 3635 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 3636 | IsBcpCall = true; |
| 3637 | |
| 3638 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 3639 | // constant expression; we can't check whether it's potentially foldable. |
| 3640 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 3641 | return false; |
| 3642 | |
| 3643 | FoldConstant Fold(Info); |
| 3644 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3645 | if (!HandleConditionalOperator(E)) |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3646 | return false; |
| 3647 | |
| 3648 | if (IsBcpCall) |
| 3649 | Fold.Fold(Info); |
| 3650 | |
| 3651 | return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3655 | APValue &Value = Info.CurrentCall->Temporaries[E]; |
| 3656 | if (Value.isUninit()) { |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3657 | const Expr *Source = E->getSourceExpr(); |
| 3658 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3659 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3660 | if (Source == E) { // sanity checking. |
| 3661 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3662 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3663 | } |
| 3664 | return StmtVisitorTy::Visit(Source); |
| 3665 | } |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3666 | return DerivedSuccess(Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3667 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3668 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3669 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3670 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3671 | QualType CalleeType = Callee->getType(); |
| 3672 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3673 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3674 | LValue *This = 0, ThisVal; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3675 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3676 | bool HasQualifier = false; |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 3677 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3678 | // Extract function decl and 'this' pointer from the callee. |
| 3679 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3680 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3681 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 3682 | // Explicit bound member calls, such as x.f() or p->g(); |
| 3683 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3684 | return false; |
| 3685 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3686 | This = &ThisVal; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3687 | HasQualifier = ME->hasQualifier(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3688 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 3689 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3690 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 3691 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3692 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3693 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3694 | return Error(Callee); |
| 3695 | |
| 3696 | FD = dyn_cast<FunctionDecl>(Member); |
| 3697 | if (!FD) |
| 3698 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3699 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3700 | LValue Call; |
| 3701 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3702 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3703 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3704 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3705 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3706 | FD = dyn_cast_or_null<FunctionDecl>( |
| 3707 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3708 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3709 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3710 | |
| 3711 | // Overloaded operator calls to member functions are represented as normal |
| 3712 | // calls with '*this' as the first argument. |
| 3713 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 3714 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3715 | // FIXME: When selecting an implicit conversion for an overloaded |
| 3716 | // operator delete, we sometimes try to evaluate calls to conversion |
| 3717 | // operators without a 'this' parameter! |
| 3718 | if (Args.empty()) |
| 3719 | return Error(E); |
| 3720 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3721 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 3722 | return false; |
| 3723 | This = &ThisVal; |
| 3724 | Args = Args.slice(1); |
| 3725 | } |
| 3726 | |
| 3727 | // Don't call function pointers which have been cast to some other type. |
| 3728 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3729 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3730 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3731 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3732 | |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 3733 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 3734 | return false; |
| 3735 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3736 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 3737 | // calls to such functions in constant expressions. |
| 3738 | if (This && !HasQualifier && |
| 3739 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 3740 | return Error(E, diag::note_constexpr_virtual_call); |
| 3741 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3742 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3743 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3744 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3745 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3746 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3747 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 3748 | Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3749 | return false; |
| 3750 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3751 | return DerivedSuccess(Result, E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3754 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 3755 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 3756 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3757 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 3758 | if (E->getNumInits() == 0) |
| 3759 | return DerivedZeroInitialization(E); |
| 3760 | if (E->getNumInits() == 1) |
| 3761 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3762 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3763 | } |
| 3764 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3765 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3766 | } |
| 3767 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3768 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3769 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3770 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3771 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3772 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3773 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3774 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 3775 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 3776 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 3777 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3778 | APValue Val; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3779 | if (!Evaluate(Val, Info, E->getBase())) |
| 3780 | return false; |
| 3781 | |
| 3782 | QualType BaseTy = E->getBase()->getType(); |
| 3783 | |
| 3784 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3785 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3786 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3787 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3788 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 3789 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3790 | CompleteObject Obj(&Val, BaseTy); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3791 | SubobjectDesignator Designator(BaseTy); |
| 3792 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3793 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3794 | APValue Result; |
| 3795 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 3796 | DerivedSuccess(Result, E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3799 | RetTy VisitCastExpr(const CastExpr *E) { |
| 3800 | switch (E->getCastKind()) { |
| 3801 | default: |
| 3802 | break; |
| 3803 | |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 3804 | case CK_AtomicToNonAtomic: { |
| 3805 | APValue AtomicVal; |
| 3806 | if (!EvaluateAtomic(E->getSubExpr(), AtomicVal, Info)) |
| 3807 | return false; |
| 3808 | return DerivedSuccess(AtomicVal, E); |
| 3809 | } |
| 3810 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3811 | case CK_NoOp: |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 3812 | case CK_UserDefinedConversion: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3813 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 3814 | |
| 3815 | case CK_LValueToRValue: { |
| 3816 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3817 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 3818 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3819 | APValue RVal; |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 3820 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3821 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 3822 | LVal, RVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3823 | return false; |
| 3824 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3825 | } |
| 3826 | } |
| 3827 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3828 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3831 | RetTy VisitUnaryPostInc(const UnaryOperator *UO) { |
| 3832 | return VisitUnaryPostIncDec(UO); |
| 3833 | } |
| 3834 | RetTy VisitUnaryPostDec(const UnaryOperator *UO) { |
| 3835 | return VisitUnaryPostIncDec(UO); |
| 3836 | } |
| 3837 | RetTy VisitUnaryPostIncDec(const UnaryOperator *UO) { |
| 3838 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 3839 | return Error(UO); |
| 3840 | |
| 3841 | LValue LVal; |
| 3842 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 3843 | return false; |
| 3844 | APValue RVal; |
| 3845 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 3846 | UO->isIncrementOp(), &RVal)) |
| 3847 | return false; |
| 3848 | return DerivedSuccess(RVal, UO); |
| 3849 | } |
| 3850 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3851 | /// Visit a value which is evaluated, but whose value is ignored. |
| 3852 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3853 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3854 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3855 | }; |
| 3856 | |
| 3857 | } |
| 3858 | |
| 3859 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3860 | // Common base class for lvalue and temporary evaluation. |
| 3861 | //===----------------------------------------------------------------------===// |
| 3862 | namespace { |
| 3863 | template<class Derived> |
| 3864 | class LValueExprEvaluatorBase |
| 3865 | : public ExprEvaluatorBase<Derived, bool> { |
| 3866 | protected: |
| 3867 | LValue &Result; |
| 3868 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 3869 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 3870 | |
| 3871 | bool Success(APValue::LValueBase B) { |
| 3872 | Result.set(B); |
| 3873 | return true; |
| 3874 | } |
| 3875 | |
| 3876 | public: |
| 3877 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 3878 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3879 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3880 | bool Success(const APValue &V, const Expr *E) { |
| 3881 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3882 | return true; |
| 3883 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3884 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3885 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3886 | // Handle non-static data members. |
| 3887 | QualType BaseTy; |
| 3888 | if (E->isArrow()) { |
| 3889 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 3890 | return false; |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3891 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3892 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3893 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3894 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 3895 | return false; |
| 3896 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3897 | } else { |
| 3898 | if (!this->Visit(E->getBase())) |
| 3899 | return false; |
| 3900 | BaseTy = E->getBase()->getType(); |
| 3901 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3902 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3903 | const ValueDecl *MD = E->getMemberDecl(); |
| 3904 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 3905 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 3906 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 3907 | (void)BaseTy; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3908 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 3909 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3910 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3911 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 3912 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3913 | } else |
| 3914 | return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3915 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3916 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3917 | APValue RefValue; |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3918 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3919 | RefValue)) |
| 3920 | return false; |
| 3921 | return Success(RefValue, E); |
| 3922 | } |
| 3923 | return true; |
| 3924 | } |
| 3925 | |
| 3926 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 3927 | switch (E->getOpcode()) { |
| 3928 | default: |
| 3929 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 3930 | |
| 3931 | case BO_PtrMemD: |
| 3932 | case BO_PtrMemI: |
| 3933 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | bool VisitCastExpr(const CastExpr *E) { |
| 3938 | switch (E->getCastKind()) { |
| 3939 | default: |
| 3940 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3941 | |
| 3942 | case CK_DerivedToBase: |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3943 | case CK_UncheckedDerivedToBase: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3944 | if (!this->Visit(E->getSubExpr())) |
| 3945 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3946 | |
| 3947 | // Now figure out the necessary offset to add to the base LV to get from |
| 3948 | // the derived class to the base class. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3949 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 3950 | Result); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3951 | } |
| 3952 | } |
| 3953 | }; |
| 3954 | } |
| 3955 | |
| 3956 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3957 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3958 | // |
| 3959 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 3960 | // function designators (in C), decl references to void objects (in C), and |
| 3961 | // temporaries (if building with -Wno-address-of-temporary). |
| 3962 | // |
| 3963 | // LValue evaluation produces values comprising a base expression of one of the |
| 3964 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3965 | // - Declarations |
| 3966 | // * VarDecl |
| 3967 | // * FunctionDecl |
| 3968 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3969 | // * CompoundLiteralExpr in C |
| 3970 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3971 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3972 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3973 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3974 | // * ObjCEncodeExpr |
| 3975 | // * AddrLabelExpr |
| 3976 | // * BlockExpr |
| 3977 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3978 | // - Locals and temporaries |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3979 | // * MaterializeTemporaryExpr |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3980 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3981 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 3982 | // from the AST (FIXME). |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3983 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 3984 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3985 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3986 | //===----------------------------------------------------------------------===// |
| 3987 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3988 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3989 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3990 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3991 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3992 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3994 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3995 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3996 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3997 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 3998 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3999 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4000 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 4001 | bool VisitMemberExpr(const MemberExpr *E); |
| 4002 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 4003 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4004 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4005 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4006 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 4007 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4008 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4009 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4010 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 4011 | return VisitUnaryPreIncDec(UO); |
| 4012 | } |
| 4013 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 4014 | return VisitUnaryPreIncDec(UO); |
| 4015 | } |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4016 | bool VisitBinAssign(const BinaryOperator *BO); |
| 4017 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4018 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4019 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4020 | switch (E->getCastKind()) { |
| 4021 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4022 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4023 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4024 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4025 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4026 | if (!Visit(E->getSubExpr())) |
| 4027 | return false; |
| 4028 | Result.Designator.setInvalid(); |
| 4029 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4030 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4031 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4032 | if (!Visit(E->getSubExpr())) |
| 4033 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4034 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4037 | }; |
| 4038 | } // end anonymous namespace |
| 4039 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4040 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Richard Smith | a07a6c3 | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 4041 | /// expressions which are not glvalues, in two cases: |
| 4042 | /// * function designators in C, and |
| 4043 | /// * "extern void" objects |
| 4044 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 4045 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| 4046 | E->getType()->isVoidType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4047 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4050 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4051 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 4052 | return Success(FD); |
| 4053 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4054 | return VisitVarDecl(E, VD); |
| 4055 | return Error(E); |
| 4056 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 4057 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4058 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4059 | CallStackFrame *Frame = 0; |
| 4060 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) |
| 4061 | Frame = Info.CurrentCall; |
| 4062 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4063 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4064 | if (Frame) { |
| 4065 | Result.set(VD, Frame->Index); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4066 | return true; |
| 4067 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4068 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4069 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 4070 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4071 | APValue *V; |
| 4072 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4073 | return false; |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4074 | return Success(*V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4077 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 4078 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4079 | // Walk through the expression to find the materialized temporary itself. |
| 4080 | SmallVector<const Expr *, 2> CommaLHSs; |
| 4081 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 4082 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 4083 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4084 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4085 | // If we passed any comma operators, evaluate their LHSs. |
| 4086 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 4087 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 4088 | return false; |
| 4089 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4090 | // A materialized temporary with static storage duration can appear within the |
| 4091 | // result of a constant expression evaluation, so we need to preserve its |
| 4092 | // value for use outside this evaluation. |
| 4093 | APValue *Value; |
| 4094 | if (E->getStorageDuration() == SD_Static) { |
| 4095 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
| 4096 | Result.set(E); |
| 4097 | } else { |
| 4098 | Value = &Info.CurrentCall->Temporaries[E]; |
| 4099 | Result.set(E, Info.CurrentCall->Index); |
| 4100 | } |
| 4101 | |
Richard Smith | f69dd33 | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4102 | QualType Type = Inner->getType(); |
| 4103 | |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4104 | // Materialize the temporary itself. |
Richard Smith | f69dd33 | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4105 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 4106 | (E->getStorageDuration() == SD_Static && |
| 4107 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 4108 | *Value = APValue(); |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4109 | return false; |
Richard Smith | f69dd33 | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4110 | } |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4111 | |
| 4112 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4113 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 4114 | --I; |
| 4115 | switch (Adjustments[I].Kind) { |
| 4116 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 4117 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 4118 | Type, Result)) |
| 4119 | return false; |
| 4120 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 4121 | break; |
| 4122 | |
| 4123 | case SubobjectAdjustment::FieldAdjustment: |
| 4124 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 4125 | return false; |
| 4126 | Type = Adjustments[I].Field->getType(); |
| 4127 | break; |
| 4128 | |
| 4129 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 4130 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 4131 | Adjustments[I].Ptr.RHS)) |
| 4132 | return false; |
| 4133 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 4134 | break; |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4141 | bool |
| 4142 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4143 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 4144 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 4145 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4146 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4149 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 9be36ab | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4150 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4151 | return Success(E); |
Richard Smith | 9be36ab | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4152 | |
| 4153 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 4154 | << E->getExprOperand()->getType() |
| 4155 | << E->getExprOperand()->getSourceRange(); |
| 4156 | return false; |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4159 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4160 | return Success(E); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4161 | } |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4162 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4163 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4164 | // Handle static data members. |
| 4165 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 4166 | VisitIgnoredValue(E->getBase()); |
| 4167 | return VisitVarDecl(E, VD); |
| 4168 | } |
| 4169 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4170 | // Handle static member functions. |
| 4171 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 4172 | if (MD->isStatic()) { |
| 4173 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4174 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4178 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4179 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4182 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4183 | // FIXME: Deal with vectors as array subscript bases. |
| 4184 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4185 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4186 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4187 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4188 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4190 | APSInt Index; |
| 4191 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4192 | return false; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4193 | |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4194 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), |
| 4195 | getExtValue(Index)); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4196 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4197 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4198 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4199 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4202 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4203 | if (!Visit(E->getSubExpr())) |
| 4204 | return false; |
| 4205 | // __real is a no-op on scalar lvalues. |
| 4206 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 4207 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 4208 | return true; |
| 4209 | } |
| 4210 | |
| 4211 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 4212 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 4213 | "lvalue __imag__ on scalar?"); |
| 4214 | if (!Visit(E->getSubExpr())) |
| 4215 | return false; |
| 4216 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 4217 | return true; |
| 4218 | } |
| 4219 | |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4220 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
| 4221 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4222 | return Error(UO); |
| 4223 | |
| 4224 | if (!this->Visit(UO->getSubExpr())) |
| 4225 | return false; |
| 4226 | |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4227 | return handleIncDec( |
| 4228 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
| 4229 | UO->isIncrementOp(), 0); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 4233 | const CompoundAssignOperator *CAO) { |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4234 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4235 | return Error(CAO); |
| 4236 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4237 | APValue RHS; |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4238 | |
| 4239 | // The overall lvalue result is the result of evaluating the LHS. |
| 4240 | if (!this->Visit(CAO->getLHS())) { |
| 4241 | if (Info.keepEvaluatingAfterFailure()) |
| 4242 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 4243 | return false; |
| 4244 | } |
| 4245 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4246 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 4247 | return false; |
| 4248 | |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 4249 | return handleCompoundAssignment( |
| 4250 | this->Info, CAO, |
| 4251 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 4252 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4256 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 4257 | return Error(E); |
| 4258 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4259 | APValue NewVal; |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4260 | |
| 4261 | if (!this->Visit(E->getLHS())) { |
| 4262 | if (Info.keepEvaluatingAfterFailure()) |
| 4263 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 4264 | return false; |
| 4265 | } |
| 4266 | |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4267 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 4268 | return false; |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4269 | |
| 4270 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | b476a14 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4271 | NewVal); |
| 4272 | } |
| 4273 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4274 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4275 | // Pointer Evaluation |
| 4276 | //===----------------------------------------------------------------------===// |
| 4277 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4278 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4279 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4280 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4281 | LValue &Result; |
| 4282 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4283 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4284 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4285 | return true; |
| 4286 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4287 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4289 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4290 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4291 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4292 | bool Success(const APValue &V, const Expr *E) { |
| 4293 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4294 | return true; |
| 4295 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4296 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4297 | return Success((Expr*)0); |
| 4298 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4299 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4300 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4301 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4302 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4303 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4304 | { return Success(E); } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 4305 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4306 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4307 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4308 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4309 | bool VisitCallExpr(const CallExpr *E); |
| 4310 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4311 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4312 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4313 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4314 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4315 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4316 | // Can't look at 'this' when checking a potential constant expression. |
| 4317 | if (Info.CheckingPotentialConstantExpression) |
| 4318 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4319 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4320 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4321 | Result = *Info.CurrentCall->This; |
| 4322 | return true; |
| 4323 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4324 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4325 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4326 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4327 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4328 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4329 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4330 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4331 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4334 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4335 | if (E->getOpcode() != BO_Add && |
| 4336 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4337 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4339 | const Expr *PExp = E->getLHS(); |
| 4340 | const Expr *IExp = E->getRHS(); |
| 4341 | if (IExp->getType()->isPointerType()) |
| 4342 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4343 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4344 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 4345 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4346 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4348 | llvm::APSInt Offset; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4349 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4350 | return false; |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4351 | |
| 4352 | int64_t AdditionalOffset = getExtValue(Offset); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4353 | if (E->getOpcode() == BO_Sub) |
| 4354 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4355 | |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4356 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4357 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 4358 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4359 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4360 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4361 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4362 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4364 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4365 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4366 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4367 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4368 | switch (E->getCastKind()) { |
| 4369 | default: |
| 4370 | break; |
| 4371 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4372 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4373 | case CK_CPointerToObjCPointerCast: |
| 4374 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4375 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4376 | if (!Visit(SubExpr)) |
| 4377 | return false; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4378 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 4379 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 4380 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4381 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4382 | Result.Designator.setInvalid(); |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4383 | if (SubExpr->getType()->isVoidPointerType()) |
| 4384 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 4385 | << 3 << SubExpr->getType(); |
| 4386 | else |
| 4387 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4388 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4389 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4390 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4391 | case CK_DerivedToBase: |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4392 | case CK_UncheckedDerivedToBase: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4393 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4394 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4395 | if (!Result.Base && Result.Offset.isZero()) |
| 4396 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4397 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4398 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4399 | // the derived class to the base class. |
Richard Smith | 8a66bf7 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4400 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 4401 | castAs<PointerType>()->getPointeeType(), |
| 4402 | Result); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4403 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4404 | case CK_BaseToDerived: |
| 4405 | if (!Visit(E->getSubExpr())) |
| 4406 | return false; |
| 4407 | if (!Result.Base && Result.Offset.isZero()) |
| 4408 | return true; |
| 4409 | return HandleBaseToDerivedCast(Info, E, Result); |
| 4410 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4411 | case CK_NullToPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4412 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4413 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4414 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4415 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4416 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4417 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4418 | APValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4419 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4420 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4421 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4422 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4423 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 4424 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4425 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4426 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4427 | Result.CallIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4428 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4429 | return true; |
| 4430 | } else { |
| 4431 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4432 | Result.setFrom(Info.Ctx, Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4433 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4434 | } |
| 4435 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4436 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4437 | if (SubExpr->isGLValue()) { |
| 4438 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 4439 | return false; |
| 4440 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4441 | Result.set(SubExpr, Info.CurrentCall->Index); |
| 4442 | if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], |
| 4443 | Info, Result, SubExpr)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4444 | return false; |
| 4445 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4446 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4447 | if (const ConstantArrayType *CAT |
| 4448 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 4449 | Result.addArray(Info, E, CAT); |
| 4450 | else |
| 4451 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4452 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4453 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4454 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4455 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4458 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4460 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4461 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4462 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4463 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 4464 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4465 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4466 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4467 | |
| 4468 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4469 | // Member Pointer Evaluation |
| 4470 | //===----------------------------------------------------------------------===// |
| 4471 | |
| 4472 | namespace { |
| 4473 | class MemberPointerExprEvaluator |
| 4474 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 4475 | MemberPtr &Result; |
| 4476 | |
| 4477 | bool Success(const ValueDecl *D) { |
| 4478 | Result = MemberPtr(D); |
| 4479 | return true; |
| 4480 | } |
| 4481 | public: |
| 4482 | |
| 4483 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 4484 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 4485 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4486 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4487 | Result.setFrom(V); |
| 4488 | return true; |
| 4489 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4490 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4491 | return Success((const ValueDecl*)0); |
| 4492 | } |
| 4493 | |
| 4494 | bool VisitCastExpr(const CastExpr *E); |
| 4495 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 4496 | }; |
| 4497 | } // end anonymous namespace |
| 4498 | |
| 4499 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 4500 | EvalInfo &Info) { |
| 4501 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 4502 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 4503 | } |
| 4504 | |
| 4505 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4506 | switch (E->getCastKind()) { |
| 4507 | default: |
| 4508 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4509 | |
| 4510 | case CK_NullToMemberPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4511 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4512 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4513 | |
| 4514 | case CK_BaseToDerivedMemberPointer: { |
| 4515 | if (!Visit(E->getSubExpr())) |
| 4516 | return false; |
| 4517 | if (E->path_empty()) |
| 4518 | return true; |
| 4519 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 4520 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 4521 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 4522 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 4523 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 4524 | PathI != PathE; ++PathI) { |
| 4525 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 4526 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4527 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4528 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4529 | } |
| 4530 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 4531 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4532 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4533 | return true; |
| 4534 | } |
| 4535 | |
| 4536 | case CK_DerivedToBaseMemberPointer: |
| 4537 | if (!Visit(E->getSubExpr())) |
| 4538 | return false; |
| 4539 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 4540 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 4541 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 4542 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4543 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4544 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4545 | } |
| 4546 | return true; |
| 4547 | } |
| 4548 | } |
| 4549 | |
| 4550 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4551 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 4552 | // member can be formed. |
| 4553 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 4554 | } |
| 4555 | |
| 4556 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4557 | // Record Evaluation |
| 4558 | //===----------------------------------------------------------------------===// |
| 4559 | |
| 4560 | namespace { |
| 4561 | class RecordExprEvaluator |
| 4562 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 4563 | const LValue &This; |
| 4564 | APValue &Result; |
| 4565 | public: |
| 4566 | |
| 4567 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 4568 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 4569 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4570 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4571 | Result = V; |
| 4572 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4573 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4574 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4575 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4576 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4577 | bool VisitInitListExpr(const InitListExpr *E); |
| 4578 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 4579 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4580 | }; |
| 4581 | } |
| 4582 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4583 | /// Perform zero-initialization on an object of non-union class type. |
| 4584 | /// C++11 [dcl.init]p5: |
| 4585 | /// To zero-initialize an object or reference of type T means: |
| 4586 | /// [...] |
| 4587 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 4588 | /// each non-static data member and each base-class subobject is |
| 4589 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4590 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 4591 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4592 | const LValue &This, APValue &Result) { |
| 4593 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 4594 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 4595 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 4596 | std::distance(RD->field_begin(), RD->field_end())); |
| 4597 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4598 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4599 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4600 | |
| 4601 | if (CD) { |
| 4602 | unsigned Index = 0; |
| 4603 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4604 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4605 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 4606 | LValue Subobject = This; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4607 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 4608 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4609 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4610 | Result.getStructBase(Index))) |
| 4611 | return false; |
| 4612 | } |
| 4613 | } |
| 4614 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4615 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 4616 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4617 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4618 | if (I->getType()->isReferenceType()) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4619 | continue; |
| 4620 | |
| 4621 | LValue Subobject = This; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4622 | if (!HandleLValueMember(Info, E, Subobject, *I, &Layout)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4623 | return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4624 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4625 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4626 | if (!EvaluateInPlace( |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4627 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4628 | return false; |
| 4629 | } |
| 4630 | |
| 4631 | return true; |
| 4632 | } |
| 4633 | |
| 4634 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 4635 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4636 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4637 | if (RD->isUnion()) { |
| 4638 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 4639 | // object's first non-static named data member is zero-initialized |
| 4640 | RecordDecl::field_iterator I = RD->field_begin(); |
| 4641 | if (I == RD->field_end()) { |
| 4642 | Result = APValue((const FieldDecl*)0); |
| 4643 | return true; |
| 4644 | } |
| 4645 | |
| 4646 | LValue Subobject = This; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4647 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4648 | return false; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4649 | Result = APValue(*I); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4650 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4651 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4654 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4655 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
| 4658 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4659 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4662 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4663 | switch (E->getCastKind()) { |
| 4664 | default: |
| 4665 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4666 | |
| 4667 | case CK_ConstructorConversion: |
| 4668 | return Visit(E->getSubExpr()); |
| 4669 | |
| 4670 | case CK_DerivedToBase: |
| 4671 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4672 | APValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4673 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4674 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4675 | if (!DerivedObject.isStruct()) |
| 4676 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4677 | |
| 4678 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 4679 | APValue *Value = &DerivedObject; |
| 4680 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 4681 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 4682 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 4683 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 4684 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4685 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 4686 | RD = Base; |
| 4687 | } |
| 4688 | Result = *Value; |
| 4689 | return true; |
| 4690 | } |
| 4691 | } |
| 4692 | } |
| 4693 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4694 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 4695 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4696 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4697 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4698 | |
| 4699 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4700 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 4701 | Result = APValue(Field); |
| 4702 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4703 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4704 | |
| 4705 | // If the initializer list for a union does not contain any elements, the |
| 4706 | // first element of the union is value-initialized. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4707 | // FIXME: The element should be initialized from an initializer list. |
| 4708 | // Is this difference ever observable for initializer lists which |
| 4709 | // we don't build? |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4710 | ImplicitValueInitExpr VIE(Field->getType()); |
| 4711 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 4712 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4713 | LValue Subobject = This; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4714 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 4715 | return false; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4716 | |
| 4717 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 4718 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 4719 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 4720 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4721 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 4725 | "initializer list for class with base classes"); |
| 4726 | Result = APValue(APValue::UninitStruct(), 0, |
| 4727 | std::distance(RD->field_begin(), RD->field_end())); |
| 4728 | unsigned ElementNo = 0; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4729 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4730 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4731 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 4732 | // Anonymous bit-fields are not considered members of the class for |
| 4733 | // purposes of aggregate initialization. |
| 4734 | if (Field->isUnnamedBitfield()) |
| 4735 | continue; |
| 4736 | |
| 4737 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4738 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4739 | bool HaveInit = ElementNo < E->getNumInits(); |
| 4740 | |
| 4741 | // FIXME: Diagnostics here should point to the end of the initializer |
| 4742 | // list, not the start. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4743 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4744 | Subobject, *Field, &Layout)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4745 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4746 | |
| 4747 | // Perform an implicit value-initialization for members beyond the end of |
| 4748 | // the initializer list. |
| 4749 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4750 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4751 | |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4752 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 4753 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 4754 | isa<CXXDefaultInitExpr>(Init)); |
| 4755 | |
| 4756 | if (!EvaluateInPlace(Result.getStructField(Field->getFieldIndex()), Info, |
| 4757 | Subobject, Init)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4758 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4759 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4760 | Success = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4761 | } |
| 4762 | } |
| 4763 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4764 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 4768 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4769 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 4770 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4771 | bool ZeroInit = E->requiresZeroInitialization(); |
| 4772 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4773 | // If we've already performed zero-initialization, we're already done. |
| 4774 | if (!Result.isUninit()) |
| 4775 | return true; |
| 4776 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4777 | if (ZeroInit) |
| 4778 | return ZeroInitialization(E); |
| 4779 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4780 | const CXXRecordDecl *RD = FD->getParent(); |
| 4781 | if (RD->isUnion()) |
| 4782 | Result = APValue((FieldDecl*)0); |
| 4783 | else |
| 4784 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 4785 | std::distance(RD->field_begin(), RD->field_end())); |
| 4786 | return true; |
| 4787 | } |
| 4788 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4789 | const FunctionDecl *Definition = 0; |
| 4790 | FD->getBody(Definition); |
| 4791 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4792 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 4793 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4794 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4795 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4796 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4797 | if (const MaterializeTemporaryExpr *ME |
| 4798 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 4799 | return Visit(ME->GetTemporaryExpr()); |
| 4800 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4801 | if (ZeroInit && !ZeroInitialization(E)) |
| 4802 | return false; |
| 4803 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4804 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4805 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4806 | cast<CXXConstructorDecl>(Definition), Info, |
| 4807 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 4810 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 4811 | const CXXStdInitializerListExpr *E) { |
| 4812 | const ConstantArrayType *ArrayType = |
| 4813 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 4814 | |
| 4815 | LValue Array; |
| 4816 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 4817 | return false; |
| 4818 | |
| 4819 | // Get a pointer to the first element of the array. |
| 4820 | Array.addArray(Info, E, ArrayType); |
| 4821 | |
| 4822 | // FIXME: Perform the checks on the field types in SemaInit. |
| 4823 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 4824 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 4825 | if (Field == Record->field_end()) |
| 4826 | return Error(E); |
| 4827 | |
| 4828 | // Start pointer. |
| 4829 | if (!Field->getType()->isPointerType() || |
| 4830 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 4831 | ArrayType->getElementType())) |
| 4832 | return Error(E); |
| 4833 | |
| 4834 | // FIXME: What if the initializer_list type has base classes, etc? |
| 4835 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 4836 | Array.moveInto(Result.getStructField(0)); |
| 4837 | |
| 4838 | if (++Field == Record->field_end()) |
| 4839 | return Error(E); |
| 4840 | |
| 4841 | if (Field->getType()->isPointerType() && |
| 4842 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 4843 | ArrayType->getElementType())) { |
| 4844 | // End pointer. |
| 4845 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 4846 | ArrayType->getElementType(), |
| 4847 | ArrayType->getSize().getZExtValue())) |
| 4848 | return false; |
| 4849 | Array.moveInto(Result.getStructField(1)); |
| 4850 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 4851 | // Length. |
| 4852 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 4853 | else |
| 4854 | return Error(E); |
| 4855 | |
| 4856 | if (++Field != Record->field_end()) |
| 4857 | return Error(E); |
| 4858 | |
| 4859 | return true; |
| 4860 | } |
| 4861 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4862 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 4863 | APValue &Result, EvalInfo &Info) { |
| 4864 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4865 | "can't evaluate expression as a record rvalue"); |
| 4866 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 4867 | } |
| 4868 | |
| 4869 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4870 | // Temporary Evaluation |
| 4871 | // |
| 4872 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 4873 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 4874 | // materialized so that a reference can bind to it. |
| 4875 | //===----------------------------------------------------------------------===// |
| 4876 | namespace { |
| 4877 | class TemporaryExprEvaluator |
| 4878 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 4879 | public: |
| 4880 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 4881 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 4882 | |
| 4883 | /// Visit an expression which constructs the value of this temporary. |
| 4884 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4885 | Result.set(E, Info.CurrentCall->Index); |
| 4886 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | bool VisitCastExpr(const CastExpr *E) { |
| 4890 | switch (E->getCastKind()) { |
| 4891 | default: |
| 4892 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4893 | |
| 4894 | case CK_ConstructorConversion: |
| 4895 | return VisitConstructExpr(E->getSubExpr()); |
| 4896 | } |
| 4897 | } |
| 4898 | bool VisitInitListExpr(const InitListExpr *E) { |
| 4899 | return VisitConstructExpr(E); |
| 4900 | } |
| 4901 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 4902 | return VisitConstructExpr(E); |
| 4903 | } |
| 4904 | bool VisitCallExpr(const CallExpr *E) { |
| 4905 | return VisitConstructExpr(E); |
| 4906 | } |
| 4907 | }; |
| 4908 | } // end anonymous namespace |
| 4909 | |
| 4910 | /// Evaluate an expression of record type as a temporary. |
| 4911 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 4912 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4913 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 4914 | } |
| 4915 | |
| 4916 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4917 | // Vector Evaluation |
| 4918 | //===----------------------------------------------------------------------===// |
| 4919 | |
| 4920 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4921 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4922 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 4923 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4924 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4925 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4926 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 4927 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4929 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 4930 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 4931 | // FIXME: remove this APValue copy. |
| 4932 | Result = APValue(V.data(), V.size()); |
| 4933 | return true; |
| 4934 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4935 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4936 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4937 | Result = V; |
| 4938 | return true; |
| 4939 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4940 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4942 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4943 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4944 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4945 | bool VisitInitListExpr(const InitListExpr *E); |
| 4946 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4947 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 4948 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4949 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4950 | }; |
| 4951 | } // end anonymous namespace |
| 4952 | |
| 4953 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4954 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4955 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4958 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4959 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 4960 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4961 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 4962 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 4963 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4964 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4965 | switch (E->getCastKind()) { |
| 4966 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4967 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4968 | if (SETy->isIntegerType()) { |
| 4969 | APSInt IntResult; |
| 4970 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4971 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4972 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4973 | } else if (SETy->isRealFloatingType()) { |
| 4974 | APFloat F(0.0); |
| 4975 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4976 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4977 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4978 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4979 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4980 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 4981 | |
| 4982 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4983 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 4984 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 4985 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 4986 | case CK_BitCast: { |
| 4987 | // Evaluate the operand into an APInt we can extract from. |
| 4988 | llvm::APInt SValInt; |
| 4989 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 4990 | return false; |
| 4991 | // Extract the elements |
| 4992 | QualType EltTy = VTy->getElementType(); |
| 4993 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 4994 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 4995 | SmallVector<APValue, 4> Elts; |
| 4996 | if (EltTy->isRealFloatingType()) { |
| 4997 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 4998 | unsigned FloatEltSize = EltSize; |
| 4999 | if (&Sem == &APFloat::x87DoubleExtended) |
| 5000 | FloatEltSize = 80; |
| 5001 | for (unsigned i = 0; i < NElts; i++) { |
| 5002 | llvm::APInt Elt; |
| 5003 | if (BigEndian) |
| 5004 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 5005 | else |
| 5006 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 9ec55f2 | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 5007 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5008 | } |
| 5009 | } else if (EltTy->isIntegerType()) { |
| 5010 | for (unsigned i = 0; i < NElts; i++) { |
| 5011 | llvm::APInt Elt; |
| 5012 | if (BigEndian) |
| 5013 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 5014 | else |
| 5015 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 5016 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 5017 | } |
| 5018 | } else { |
| 5019 | return Error(E); |
| 5020 | } |
| 5021 | return Success(Elts, E); |
| 5022 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5023 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5024 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5025 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5028 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5029 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5030 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5031 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5032 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5033 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5034 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5035 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5036 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5037 | // The number of initializers can be less than the number of |
| 5038 | // vector elements. For OpenCL, this can be due to nested vector |
| 5039 | // initialization. For GCC compatibility, missing trailing elements |
| 5040 | // should be initialized with zeroes. |
| 5041 | unsigned CountInits = 0, CountElts = 0; |
| 5042 | while (CountElts < NumElements) { |
| 5043 | // Handle nested vector initialization. |
| 5044 | if (CountInits < NumInits |
| 5045 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 5046 | APValue v; |
| 5047 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 5048 | return Error(E); |
| 5049 | unsigned vlen = v.getVectorLength(); |
| 5050 | for (unsigned j = 0; j < vlen; j++) |
| 5051 | Elements.push_back(v.getVectorElt(j)); |
| 5052 | CountElts += vlen; |
| 5053 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5054 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5055 | if (CountInits < NumInits) { |
| 5056 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5057 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5058 | } else // trailing integer zero. |
| 5059 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 5060 | Elements.push_back(APValue(sInt)); |
| 5061 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5062 | } else { |
| 5063 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5064 | if (CountInits < NumInits) { |
| 5065 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5066 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5067 | } else // trailing float zero. |
| 5068 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 5069 | Elements.push_back(APValue(f)); |
| 5070 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 5071 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5072 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5073 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5074 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5075 | } |
| 5076 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5077 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5078 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5079 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5080 | QualType EltTy = VT->getElementType(); |
| 5081 | APValue ZeroElement; |
| 5082 | if (EltTy->isIntegerType()) |
| 5083 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 5084 | else |
| 5085 | ZeroElement = |
| 5086 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 5087 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5088 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5089 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5092 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5093 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5094 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5097 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5098 | // Array Evaluation |
| 5099 | //===----------------------------------------------------------------------===// |
| 5100 | |
| 5101 | namespace { |
| 5102 | class ArrayExprEvaluator |
| 5103 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5104 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5105 | APValue &Result; |
| 5106 | public: |
| 5107 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5108 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 5109 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5110 | |
| 5111 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5112 | assert((V.isArray() || V.isLValue()) && |
| 5113 | "expected array or string literal"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5114 | Result = V; |
| 5115 | return true; |
| 5116 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5117 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5118 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5119 | const ConstantArrayType *CAT = |
| 5120 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5121 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5122 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5123 | |
| 5124 | Result = APValue(APValue::UninitArray(), 0, |
| 5125 | CAT->getSize().getZExtValue()); |
| 5126 | if (!Result.hasArrayFiller()) return true; |
| 5127 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5128 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5129 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5130 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5131 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5132 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5135 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5136 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5137 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5138 | const LValue &Subobject, |
| 5139 | APValue *Value, QualType Type); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5140 | }; |
| 5141 | } // end anonymous namespace |
| 5142 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5143 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 5144 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5145 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5146 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5150 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5151 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5152 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5153 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5154 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 5155 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 5156 | if (E->isStringLiteralInit()) { |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5157 | LValue LV; |
| 5158 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 5159 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5160 | APValue Val; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5161 | LV.moveInto(Val); |
| 5162 | return Success(Val, E); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5165 | bool Success = true; |
| 5166 | |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5167 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 5168 | "zero-initialized array shouldn't have any initialized elts"); |
| 5169 | APValue Filler; |
| 5170 | if (Result.isArray() && Result.hasArrayFiller()) |
| 5171 | Filler = Result.getArrayFiller(); |
| 5172 | |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5173 | unsigned NumEltsToInit = E->getNumInits(); |
| 5174 | unsigned NumElts = CAT->getSize().getZExtValue(); |
| 5175 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : 0; |
| 5176 | |
| 5177 | // If the initializer might depend on the array index, run it for each |
| 5178 | // array element. For now, just whitelist non-class value-initialization. |
| 5179 | if (NumEltsToInit != NumElts && !isa<ImplicitValueInitExpr>(FillerExpr)) |
| 5180 | NumEltsToInit = NumElts; |
| 5181 | |
| 5182 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5183 | |
| 5184 | // If the array was previously zero-initialized, preserve the |
| 5185 | // zero-initialized values. |
| 5186 | if (!Filler.isUninit()) { |
| 5187 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 5188 | Result.getArrayInitializedElt(I) = Filler; |
| 5189 | if (Result.hasArrayFiller()) |
| 5190 | Result.getArrayFiller() = Filler; |
| 5191 | } |
| 5192 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5193 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5194 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5195 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 5196 | const Expr *Init = |
| 5197 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5198 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5199 | Info, Subobject, Init) || |
| 5200 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5201 | CAT->getElementType(), 1)) { |
| 5202 | if (!Info.keepEvaluatingAfterFailure()) |
| 5203 | return false; |
| 5204 | Success = false; |
| 5205 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5206 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5207 | |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5208 | if (!Result.hasArrayFiller()) |
| 5209 | return Success; |
| 5210 | |
| 5211 | // If we get here, we have a trivial filler, which we can just evaluate |
| 5212 | // once and splat over the rest of the array elements. |
| 5213 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 5214 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 5215 | FillerExpr) && Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5218 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5219 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 5220 | } |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5221 | |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5222 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5223 | const LValue &Subobject, |
| 5224 | APValue *Value, |
| 5225 | QualType Type) { |
| 5226 | bool HadZeroInit = !Value->isUninit(); |
| 5227 | |
| 5228 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 5229 | unsigned N = CAT->getSize().getZExtValue(); |
| 5230 | |
| 5231 | // Preserve the array filler if we had prior zero-initialization. |
| 5232 | APValue Filler = |
| 5233 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 5234 | : APValue(); |
| 5235 | |
| 5236 | *Value = APValue(APValue::UninitArray(), N, N); |
| 5237 | |
| 5238 | if (HadZeroInit) |
| 5239 | for (unsigned I = 0; I != N; ++I) |
| 5240 | Value->getArrayInitializedElt(I) = Filler; |
| 5241 | |
| 5242 | // Initialize the elements. |
| 5243 | LValue ArrayElt = Subobject; |
| 5244 | ArrayElt.addArray(Info, E, CAT); |
| 5245 | for (unsigned I = 0; I != N; ++I) |
| 5246 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 5247 | CAT->getElementType()) || |
| 5248 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 5249 | CAT->getElementType(), 1)) |
| 5250 | return false; |
| 5251 | |
| 5252 | return true; |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5253 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5254 | |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5255 | if (!Type->isRecordType()) |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 5256 | return Error(E); |
| 5257 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5258 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5259 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5260 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5261 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5262 | if (HadZeroInit) |
| 5263 | return true; |
| 5264 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5265 | if (ZeroInit) { |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5266 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5267 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5270 | const CXXRecordDecl *RD = FD->getParent(); |
| 5271 | if (RD->isUnion()) |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5272 | *Value = APValue((FieldDecl*)0); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5273 | else |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5274 | *Value = |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5275 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 5276 | std::distance(RD->field_begin(), RD->field_end())); |
| 5277 | return true; |
| 5278 | } |
| 5279 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5280 | const FunctionDecl *Definition = 0; |
| 5281 | FD->getBody(Definition); |
| 5282 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5283 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5284 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5285 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5286 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 99ad359 | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5287 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5288 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5289 | return false; |
| 5290 | } |
| 5291 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5292 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5293 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5294 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5295 | Info, *Value); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5298 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5299 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5300 | // |
| 5301 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 5302 | // types and back in constant folding. Integer values are thus represented |
| 5303 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5304 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5305 | |
| 5306 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5307 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5308 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5309 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5310 | public: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5311 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5312 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5313 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5314 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5315 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5316 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5317 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5318 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5319 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5320 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5321 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5322 | return true; |
| 5323 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5324 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 5325 | return Success(SI, E, Result); |
| 5326 | } |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5327 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5328 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5329 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5330 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5331 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5332 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5333 | Result = APValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5334 | Result.getInt().setIsUnsigned( |
| 5335 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5336 | return true; |
| 5337 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5338 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 5339 | return Success(I, E, Result); |
| 5340 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5341 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5342 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5343 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5344 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5345 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5346 | return true; |
| 5347 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5348 | bool Success(uint64_t Value, const Expr *E) { |
| 5349 | return Success(Value, E, Result); |
| 5350 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5351 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5352 | bool Success(CharUnits Size, const Expr *E) { |
| 5353 | return Success(Size.getQuantity(), E); |
| 5354 | } |
| 5355 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5356 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 5357 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 5358 | Result = V; |
| 5359 | return true; |
| 5360 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5361 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 5362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5364 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5365 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5366 | //===--------------------------------------------------------------------===// |
| 5367 | // Visitor Methods |
| 5368 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5369 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5370 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5371 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5372 | } |
| 5373 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5374 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5375 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5376 | |
| 5377 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 5378 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5379 | if (CheckReferencedDecl(E, E->getDecl())) |
| 5380 | return true; |
| 5381 | |
| 5382 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5383 | } |
| 5384 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5385 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5386 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5387 | return true; |
| 5388 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5389 | |
| 5390 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5393 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5394 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5395 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5396 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 5397 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5398 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5399 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5400 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5401 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5402 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5405 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 5406 | return Success(E->getValue(), E); |
| 5407 | } |
| 5408 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5409 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 5410 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5411 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5414 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 5415 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 5418 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 5419 | return Success(E->getValue(), E); |
| 5420 | } |
| 5421 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5422 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 5423 | return Success(E->getValue(), E); |
| 5424 | } |
| 5425 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5426 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 5427 | return Success(E->getValue(), E); |
| 5428 | } |
| 5429 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5430 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 5431 | return Success(E->getValue(), E); |
| 5432 | } |
| 5433 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5434 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5435 | bool VisitUnaryImag(const UnaryOperator *E); |
| 5436 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5437 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5438 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 5439 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5440 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5441 | CharUnits GetAlignOfExpr(const Expr *E); |
| 5442 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5443 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5444 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5445 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5446 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5447 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5448 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5449 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 5450 | /// produce either the integer value or a pointer. |
| 5451 | /// |
| 5452 | /// GCC has a heinous extension which folds casts between pointer types and |
| 5453 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 5454 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 5455 | /// Some simple arithmetic on such values is supported (they are treated much |
| 5456 | /// like char*). |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5457 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5458 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5459 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5460 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5461 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5462 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5463 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5464 | APValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5465 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5466 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5467 | if (!Val.isInt()) { |
| 5468 | // FIXME: It would be better to produce the diagnostic for casting |
| 5469 | // a pointer to an integer. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5470 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5471 | return false; |
| 5472 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5473 | Result = Val.getInt(); |
| 5474 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5475 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5476 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5477 | /// Check whether the given declaration can be directly converted to an integral |
| 5478 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 5479 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5480 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5481 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 5482 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5483 | // Check for signedness/width mismatches between E type and ECD value. |
| 5484 | bool SameSign = (ECD->getInitVal().isSigned() |
| 5485 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 5486 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 5487 | == Info.Ctx.getIntWidth(E->getType())); |
| 5488 | if (SameSign && SameWidth) |
| 5489 | return Success(ECD->getInitVal(), E); |
| 5490 | else { |
| 5491 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 5492 | // by computing a new value matching the type of E. |
| 5493 | llvm::APSInt Val = ECD->getInitVal(); |
| 5494 | if (!SameSign) |
| 5495 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 5496 | if (!SameWidth) |
| 5497 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 5498 | return Success(Val, E); |
| 5499 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 5500 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5501 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5504 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 5505 | /// as GCC. |
| 5506 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 5507 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 5508 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5509 | enum gcc_type_class { |
| 5510 | no_type_class = -1, |
| 5511 | void_type_class, integer_type_class, char_type_class, |
| 5512 | enumeral_type_class, boolean_type_class, |
| 5513 | pointer_type_class, reference_type_class, offset_type_class, |
| 5514 | real_type_class, complex_type_class, |
| 5515 | function_type_class, method_type_class, |
| 5516 | record_type_class, union_type_class, |
| 5517 | array_type_class, string_type_class, |
| 5518 | lang_type_class |
| 5519 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5520 | |
| 5521 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5522 | // ideal, however it is what gcc does. |
| 5523 | if (E->getNumArgs() == 0) |
| 5524 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5525 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5526 | QualType ArgTy = E->getArg(0)->getType(); |
| 5527 | if (ArgTy->isVoidType()) |
| 5528 | return void_type_class; |
| 5529 | else if (ArgTy->isEnumeralType()) |
| 5530 | return enumeral_type_class; |
| 5531 | else if (ArgTy->isBooleanType()) |
| 5532 | return boolean_type_class; |
| 5533 | else if (ArgTy->isCharType()) |
| 5534 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 5535 | else if (ArgTy->isIntegerType()) |
| 5536 | return integer_type_class; |
| 5537 | else if (ArgTy->isPointerType()) |
| 5538 | return pointer_type_class; |
| 5539 | else if (ArgTy->isReferenceType()) |
| 5540 | return reference_type_class; |
| 5541 | else if (ArgTy->isRealType()) |
| 5542 | return real_type_class; |
| 5543 | else if (ArgTy->isComplexType()) |
| 5544 | return complex_type_class; |
| 5545 | else if (ArgTy->isFunctionType()) |
| 5546 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 5547 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5548 | return record_type_class; |
| 5549 | else if (ArgTy->isUnionType()) |
| 5550 | return union_type_class; |
| 5551 | else if (ArgTy->isArrayType()) |
| 5552 | return array_type_class; |
| 5553 | else if (ArgTy->isUnionType()) |
| 5554 | return union_type_class; |
| 5555 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5556 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 5557 | } |
| 5558 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5559 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 5560 | /// __builtin_constant_p when applied to the given lvalue. |
| 5561 | /// |
| 5562 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 5563 | /// character of a string literal. |
| 5564 | template<typename LValue> |
| 5565 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | 8e55ed1 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 5566 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5567 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 5568 | } |
| 5569 | |
| 5570 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 5571 | /// GCC as we can manage. |
| 5572 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 5573 | QualType ArgType = Arg->getType(); |
| 5574 | |
| 5575 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 5576 | // are not precisely documented, but are as follows: |
| 5577 | // |
| 5578 | // - If the operand is of integral, floating, complex or enumeration type, |
| 5579 | // and can be folded to a known value of that type, it returns 1. |
| 5580 | // - If the operand and can be folded to a pointer to the first character |
| 5581 | // of a string literal (or such a pointer cast to an integral type), it |
| 5582 | // returns 1. |
| 5583 | // |
| 5584 | // Otherwise, it returns 0. |
| 5585 | // |
| 5586 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 5587 | // its support for this does not currently work. |
| 5588 | if (ArgType->isIntegralOrEnumerationType()) { |
| 5589 | Expr::EvalResult Result; |
| 5590 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 5591 | return false; |
| 5592 | |
| 5593 | APValue &V = Result.Val; |
| 5594 | if (V.getKind() == APValue::Int) |
| 5595 | return true; |
| 5596 | |
| 5597 | return EvaluateBuiltinConstantPForLValue(V); |
| 5598 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 5599 | return Arg->isEvaluatable(Ctx); |
| 5600 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 5601 | LValue LV; |
| 5602 | Expr::EvalStatus Status; |
| 5603 | EvalInfo Info(Ctx, Status); |
| 5604 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 5605 | : EvaluatePointer(Arg, LV, Info)) && |
| 5606 | !Status.HasSideEffects) |
| 5607 | return EvaluateBuiltinConstantPForLValue(LV); |
| 5608 | } |
| 5609 | |
| 5610 | // Anything else isn't considered to be sufficiently constant. |
| 5611 | return false; |
| 5612 | } |
| 5613 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5614 | /// Retrieves the "underlying object type" of the given expression, |
| 5615 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5616 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 5617 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 5618 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5619 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5620 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 5621 | if (isa<CompoundLiteralExpr>(E)) |
| 5622 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | return QualType(); |
| 5626 | } |
| 5627 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5628 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5629 | LValue Base; |
Richard Smith | c679485 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5630 | |
| 5631 | { |
| 5632 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 5633 | // If there are any, but we can determine the pointed-to object anyway, then |
| 5634 | // ignore the side-effects. |
| 5635 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 5636 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 5637 | return false; |
| 5638 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5639 | |
| 5640 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5641 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5642 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5643 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5644 | if (T.isNull() || |
| 5645 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5646 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5647 | T->isVariablyModifiedType() || |
| 5648 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5649 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5650 | |
| 5651 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 5652 | CharUnits Offset = Base.getLValueOffset(); |
| 5653 | |
| 5654 | if (!Offset.isNegative() && Offset <= Size) |
| 5655 | Size -= Offset; |
| 5656 | else |
| 5657 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5658 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5661 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5662 | switch (unsigned BuiltinOp = E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5663 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5664 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5665 | |
| 5666 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5667 | if (TryEvaluateBuiltinObjectSize(E)) |
| 5668 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5669 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 5670 | // If evaluating the argument has side-effects, we can't determine the size |
| 5671 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 5672 | // handle all cases where the expression has side-effects. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 5673 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5674 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 5675 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5676 | return Success(0, E); |
| 5677 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 5678 | |
Richard Smith | c679485 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5679 | // Expression had no side effects, but we couldn't statically determine the |
| 5680 | // size of the referenced object. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5681 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
Benjamin Kramer | d190057 | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 5684 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 70d38f3 | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 5685 | case Builtin::BI__builtin_bswap32: |
| 5686 | case Builtin::BI__builtin_bswap64: { |
| 5687 | APSInt Val; |
| 5688 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5689 | return false; |
| 5690 | |
| 5691 | return Success(Val.byteSwap(), E); |
| 5692 | } |
| 5693 | |
Richard Smith | acaf72a | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 5694 | case Builtin::BI__builtin_classify_type: |
| 5695 | return Success(EvaluateBuiltinClassifyType(E), E); |
| 5696 | |
| 5697 | // FIXME: BI__builtin_clrsb |
| 5698 | // FIXME: BI__builtin_clrsbl |
| 5699 | // FIXME: BI__builtin_clrsbll |
| 5700 | |
Richard Smith | 4dbf408 | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 5701 | case Builtin::BI__builtin_clz: |
| 5702 | case Builtin::BI__builtin_clzl: |
| 5703 | case Builtin::BI__builtin_clzll: { |
| 5704 | APSInt Val; |
| 5705 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5706 | return false; |
| 5707 | if (!Val) |
| 5708 | return Error(E); |
| 5709 | |
| 5710 | return Success(Val.countLeadingZeros(), E); |
| 5711 | } |
| 5712 | |
Richard Smith | acaf72a | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 5713 | case Builtin::BI__builtin_constant_p: |
| 5714 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 5715 | |
Richard Smith | 4dbf408 | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 5716 | case Builtin::BI__builtin_ctz: |
| 5717 | case Builtin::BI__builtin_ctzl: |
| 5718 | case Builtin::BI__builtin_ctzll: { |
| 5719 | APSInt Val; |
| 5720 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5721 | return false; |
| 5722 | if (!Val) |
| 5723 | return Error(E); |
| 5724 | |
| 5725 | return Success(Val.countTrailingZeros(), E); |
| 5726 | } |
| 5727 | |
Richard Smith | acaf72a | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 5728 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 5729 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 5730 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 5731 | return Success(Operand, E); |
| 5732 | } |
| 5733 | |
| 5734 | case Builtin::BI__builtin_expect: |
| 5735 | return Visit(E->getArg(0)); |
| 5736 | |
| 5737 | case Builtin::BI__builtin_ffs: |
| 5738 | case Builtin::BI__builtin_ffsl: |
| 5739 | case Builtin::BI__builtin_ffsll: { |
| 5740 | APSInt Val; |
| 5741 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5742 | return false; |
| 5743 | |
| 5744 | unsigned N = Val.countTrailingZeros(); |
| 5745 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 5746 | } |
| 5747 | |
| 5748 | case Builtin::BI__builtin_fpclassify: { |
| 5749 | APFloat Val(0.0); |
| 5750 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 5751 | return false; |
| 5752 | unsigned Arg; |
| 5753 | switch (Val.getCategory()) { |
| 5754 | case APFloat::fcNaN: Arg = 0; break; |
| 5755 | case APFloat::fcInfinity: Arg = 1; break; |
| 5756 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 5757 | case APFloat::fcZero: Arg = 4; break; |
| 5758 | } |
| 5759 | return Visit(E->getArg(Arg)); |
| 5760 | } |
| 5761 | |
| 5762 | case Builtin::BI__builtin_isinf_sign: { |
| 5763 | APFloat Val(0.0); |
Richard Smith | 5350ded | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 5764 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | acaf72a | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 5765 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 5766 | } |
| 5767 | |
| 5768 | case Builtin::BI__builtin_parity: |
| 5769 | case Builtin::BI__builtin_parityl: |
| 5770 | case Builtin::BI__builtin_parityll: { |
| 5771 | APSInt Val; |
| 5772 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5773 | return false; |
| 5774 | |
| 5775 | return Success(Val.countPopulation() % 2, E); |
| 5776 | } |
| 5777 | |
Richard Smith | 4dbf408 | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 5778 | case Builtin::BI__builtin_popcount: |
| 5779 | case Builtin::BI__builtin_popcountl: |
| 5780 | case Builtin::BI__builtin_popcountll: { |
| 5781 | APSInt Val; |
| 5782 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5783 | return false; |
| 5784 | |
| 5785 | return Success(Val.countPopulation(), E); |
| 5786 | } |
| 5787 | |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5788 | case Builtin::BIstrlen: |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5789 | // A call to strlen is not a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5790 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5791 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5792 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 5793 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5794 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5795 | // Fall through. |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5796 | case Builtin::BI__builtin_strlen: |
| 5797 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 5798 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5799 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5800 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 5801 | // The string literal may have embedded null characters. Find the first |
| 5802 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5803 | StringRef Str = S->getString(); |
| 5804 | StringRef::size_type Pos = Str.find(0); |
| 5805 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5806 | Str = Str.substr(0, Pos); |
| 5807 | |
| 5808 | return Success(Str.size(), E); |
| 5809 | } |
| 5810 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5811 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5812 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5813 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 5814 | case Builtin::BI__atomic_is_lock_free: |
| 5815 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5816 | APSInt SizeVal; |
| 5817 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 5818 | return false; |
| 5819 | |
| 5820 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 5821 | // of two less than the maximum inline atomic width, we know it is |
| 5822 | // lock-free. If the size isn't a power of two, or greater than the |
| 5823 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 5824 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 5825 | // the answer can only be determined at runtime; for example, 16-byte |
| 5826 | // atomics have lock-free implementations on some, but not all, |
| 5827 | // x86-64 processors. |
| 5828 | |
| 5829 | // Check power-of-two. |
| 5830 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5831 | if (Size.isPowerOfTwo()) { |
| 5832 | // Check against inlining width. |
| 5833 | unsigned InlineWidthBits = |
| 5834 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 5835 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 5836 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 5837 | Size == CharUnits::One() || |
| 5838 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 5839 | Expr::NPC_NeverValueDependent)) |
| 5840 | // OK, we will inline appropriately-aligned operations of this size, |
| 5841 | // and _Atomic(T) is appropriately-aligned. |
| 5842 | return Success(1, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5843 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5844 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 5845 | castAs<PointerType>()->getPointeeType(); |
| 5846 | if (!PointeeType->isIncompleteType() && |
| 5847 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 5848 | // OK, we will inline operations on this object. |
| 5849 | return Success(1, E); |
| 5850 | } |
| 5851 | } |
| 5852 | } |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5853 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5854 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 5855 | Success(0, E) : Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5856 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5857 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5858 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5859 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5860 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 5861 | if (!A.getLValueBase()) |
| 5862 | return !B.getLValueBase(); |
| 5863 | if (!B.getLValueBase()) |
| 5864 | return false; |
| 5865 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5866 | if (A.getLValueBase().getOpaqueValue() != |
| 5867 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5868 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 5869 | if (!ADecl) |
| 5870 | return false; |
| 5871 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 5872 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5873 | return false; |
| 5874 | } |
| 5875 | |
| 5876 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5877 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5880 | namespace { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5881 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5882 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 5883 | /// |
| 5884 | /// We use a data recursive algorithm for binary operators so that we are able |
| 5885 | /// to handle extreme cases of chained binary operators without causing stack |
| 5886 | /// overflow. |
| 5887 | class DataRecursiveIntBinOpEvaluator { |
| 5888 | struct EvalResult { |
| 5889 | APValue Val; |
| 5890 | bool Failed; |
| 5891 | |
| 5892 | EvalResult() : Failed(false) { } |
| 5893 | |
| 5894 | void swap(EvalResult &RHS) { |
| 5895 | Val.swap(RHS.Val); |
| 5896 | Failed = RHS.Failed; |
| 5897 | RHS.Failed = false; |
| 5898 | } |
| 5899 | }; |
| 5900 | |
| 5901 | struct Job { |
| 5902 | const Expr *E; |
| 5903 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 5904 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| 5905 | |
| 5906 | Job() : StoredInfo(0) { } |
| 5907 | void startSpeculativeEval(EvalInfo &Info) { |
| 5908 | OldEvalStatus = Info.EvalStatus; |
| 5909 | Info.EvalStatus.Diag = 0; |
| 5910 | StoredInfo = &Info; |
| 5911 | } |
| 5912 | ~Job() { |
| 5913 | if (StoredInfo) { |
| 5914 | StoredInfo->EvalStatus = OldEvalStatus; |
| 5915 | } |
| 5916 | } |
| 5917 | private: |
| 5918 | EvalInfo *StoredInfo; // non-null if status changed. |
| 5919 | Expr::EvalStatus OldEvalStatus; |
| 5920 | }; |
| 5921 | |
| 5922 | SmallVector<Job, 16> Queue; |
| 5923 | |
| 5924 | IntExprEvaluator &IntEval; |
| 5925 | EvalInfo &Info; |
| 5926 | APValue &FinalResult; |
| 5927 | |
| 5928 | public: |
| 5929 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 5930 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 5931 | |
| 5932 | /// \brief True if \param E is a binary operator that we are going to handle |
| 5933 | /// data recursively. |
| 5934 | /// We handle binary operators that are comma, logical, or that have operands |
| 5935 | /// with integral or enumeration type. |
| 5936 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 5937 | return E->getOpcode() == BO_Comma || |
| 5938 | E->isLogicalOp() || |
| 5939 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 5940 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5943 | bool Traverse(const BinaryOperator *E) { |
| 5944 | enqueue(E); |
| 5945 | EvalResult PrevResult; |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5946 | while (!Queue.empty()) |
| 5947 | process(PrevResult); |
| 5948 | |
| 5949 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5950 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5951 | FinalResult.swap(PrevResult.Val); |
| 5952 | return true; |
| 5953 | } |
| 5954 | |
| 5955 | private: |
| 5956 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 5957 | return IntEval.Success(Value, E, Result); |
| 5958 | } |
| 5959 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 5960 | return IntEval.Success(Value, E, Result); |
| 5961 | } |
| 5962 | bool Error(const Expr *E) { |
| 5963 | return IntEval.Error(E); |
| 5964 | } |
| 5965 | bool Error(const Expr *E, diag::kind D) { |
| 5966 | return IntEval.Error(E, D); |
| 5967 | } |
| 5968 | |
| 5969 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 5970 | return Info.CCEDiag(E, D); |
| 5971 | } |
| 5972 | |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5973 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 5974 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5975 | bool &SuppressRHSDiags); |
| 5976 | |
| 5977 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 5978 | const BinaryOperator *E, APValue &Result); |
| 5979 | |
| 5980 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 5981 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 5982 | if (Result.Failed) |
| 5983 | Result.Val = APValue(); |
| 5984 | } |
| 5985 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5986 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5987 | |
| 5988 | void enqueue(const Expr *E) { |
| 5989 | E = E->IgnoreParens(); |
| 5990 | Queue.resize(Queue.size()+1); |
| 5991 | Queue.back().E = E; |
| 5992 | Queue.back().Kind = Job::AnyExprKind; |
| 5993 | } |
| 5994 | }; |
| 5995 | |
| 5996 | } |
| 5997 | |
| 5998 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5999 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6000 | bool &SuppressRHSDiags) { |
| 6001 | if (E->getOpcode() == BO_Comma) { |
| 6002 | // Ignore LHS but note if we could not evaluate it. |
| 6003 | if (LHSResult.Failed) |
| 6004 | Info.EvalStatus.HasSideEffects = true; |
| 6005 | return true; |
| 6006 | } |
| 6007 | |
| 6008 | if (E->isLogicalOp()) { |
| 6009 | bool lhsResult; |
| 6010 | if (HandleConversionToBool(LHSResult.Val, lhsResult)) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6011 | // We were able to evaluate the LHS, see if we can get away with not |
| 6012 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6013 | if (lhsResult == (E->getOpcode() == BO_LOr)) { |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6014 | Success(lhsResult, E, LHSResult.Val); |
| 6015 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6016 | } |
| 6017 | } else { |
| 6018 | // Since we weren't able to evaluate the left hand side, it |
| 6019 | // must have had side effects. |
| 6020 | Info.EvalStatus.HasSideEffects = true; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6021 | |
| 6022 | // We can't evaluate the LHS; however, sometimes the result |
| 6023 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6024 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 6025 | SuppressRHSDiags = true; |
| 6026 | } |
| 6027 | |
| 6028 | return true; |
| 6029 | } |
| 6030 | |
| 6031 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6032 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6033 | |
| 6034 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6035 | return false; // Ignore RHS; |
| 6036 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6037 | return true; |
| 6038 | } |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6039 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6040 | bool DataRecursiveIntBinOpEvaluator:: |
| 6041 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6042 | const BinaryOperator *E, APValue &Result) { |
| 6043 | if (E->getOpcode() == BO_Comma) { |
| 6044 | if (RHSResult.Failed) |
| 6045 | return false; |
| 6046 | Result = RHSResult.Val; |
| 6047 | return true; |
| 6048 | } |
| 6049 | |
| 6050 | if (E->isLogicalOp()) { |
| 6051 | bool lhsResult, rhsResult; |
| 6052 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 6053 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 6054 | |
| 6055 | if (LHSIsOK) { |
| 6056 | if (RHSIsOK) { |
| 6057 | if (E->getOpcode() == BO_LOr) |
| 6058 | return Success(lhsResult || rhsResult, E, Result); |
| 6059 | else |
| 6060 | return Success(lhsResult && rhsResult, E, Result); |
| 6061 | } |
| 6062 | } else { |
| 6063 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6064 | // We can't evaluate the LHS; however, sometimes the result |
| 6065 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6066 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6067 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6068 | } |
| 6069 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6070 | |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6071 | return false; |
| 6072 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6073 | |
| 6074 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6075 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6076 | |
| 6077 | if (LHSResult.Failed || RHSResult.Failed) |
| 6078 | return false; |
| 6079 | |
| 6080 | const APValue &LHSVal = LHSResult.Val; |
| 6081 | const APValue &RHSVal = RHSResult.Val; |
| 6082 | |
| 6083 | // Handle cases like (unsigned long)&a + 4. |
| 6084 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 6085 | Result = LHSVal; |
| 6086 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 6087 | RHSVal.getInt().getZExtValue()); |
| 6088 | if (E->getOpcode() == BO_Add) |
| 6089 | Result.getLValueOffset() += AdditionalOffset; |
| 6090 | else |
| 6091 | Result.getLValueOffset() -= AdditionalOffset; |
| 6092 | return true; |
| 6093 | } |
| 6094 | |
| 6095 | // Handle cases like 4 + (unsigned long)&a |
| 6096 | if (E->getOpcode() == BO_Add && |
| 6097 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 6098 | Result = RHSVal; |
| 6099 | Result.getLValueOffset() += CharUnits::fromQuantity( |
| 6100 | LHSVal.getInt().getZExtValue()); |
| 6101 | return true; |
| 6102 | } |
| 6103 | |
| 6104 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 6105 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 6106 | if (!LHSVal.getLValueOffset().isZero() || |
| 6107 | !RHSVal.getLValueOffset().isZero()) |
| 6108 | return false; |
| 6109 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6110 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6111 | if (!LHSExpr || !RHSExpr) |
| 6112 | return false; |
| 6113 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6114 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6115 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6116 | return false; |
| 6117 | // Make sure both labels come from the same function. |
| 6118 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6119 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6120 | return false; |
| 6121 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 6122 | return true; |
| 6123 | } |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6124 | |
| 6125 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6126 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 6127 | return Error(E); |
Richard Smith | d20afcb | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6128 | |
| 6129 | // Set up the width and signedness manually, in case it can't be deduced |
| 6130 | // from the operation we're performing. |
| 6131 | // FIXME: Don't do this in the cases where we can deduce it. |
| 6132 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 6133 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 6134 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 6135 | RHSVal.getInt(), Value)) |
| 6136 | return false; |
| 6137 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6140 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6141 | Job &job = Queue.back(); |
| 6142 | |
| 6143 | switch (job.Kind) { |
| 6144 | case Job::AnyExprKind: { |
| 6145 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 6146 | if (shouldEnqueue(Bop)) { |
| 6147 | job.Kind = Job::BinOpKind; |
| 6148 | enqueue(Bop->getLHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6149 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6150 | } |
| 6151 | } |
| 6152 | |
| 6153 | EvaluateExpr(job.E, Result); |
| 6154 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6155 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
| 6158 | case Job::BinOpKind: { |
| 6159 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6160 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6161 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6162 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6163 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6164 | } |
| 6165 | if (SuppressRHSDiags) |
| 6166 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6167 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6168 | job.Kind = Job::BinOpVisitedLHSKind; |
| 6169 | enqueue(Bop->getRHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6170 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | case Job::BinOpVisitedLHSKind: { |
| 6174 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 6175 | EvalResult RHS; |
| 6176 | RHS.swap(Result); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6177 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6178 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6179 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6180 | } |
| 6181 | } |
| 6182 | |
| 6183 | llvm_unreachable("Invalid Job::Kind!"); |
| 6184 | } |
| 6185 | |
| 6186 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 6187 | if (E->isAssignmentOp()) |
| 6188 | return Error(E); |
| 6189 | |
| 6190 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 6191 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6192 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6193 | QualType LHSTy = E->getLHS()->getType(); |
| 6194 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6195 | |
| 6196 | if (LHSTy->isAnyComplexType()) { |
| 6197 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6198 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6199 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6200 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 6201 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6202 | return false; |
| 6203 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6204 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6205 | return false; |
| 6206 | |
| 6207 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6208 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6209 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6210 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6211 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 6212 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6213 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6214 | return Success((CR_r == APFloat::cmpEqual && |
| 6215 | CR_i == APFloat::cmpEqual), E); |
| 6216 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6217 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6218 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6219 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6220 | CR_r == APFloat::cmpLessThan || |
| 6221 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6222 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6223 | CR_i == APFloat::cmpLessThan || |
| 6224 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6225 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6226 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6227 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6228 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 6229 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 6230 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6231 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6232 | "Invalid compex comparison."); |
| 6233 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 6234 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 6235 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6236 | } |
| 6237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6238 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6239 | if (LHSTy->isRealFloatingType() && |
| 6240 | RHSTy->isRealFloatingType()) { |
| 6241 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6242 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6243 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 6244 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6245 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6246 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6247 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6248 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6249 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6250 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 6251 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6252 | switch (E->getOpcode()) { |
| 6253 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6254 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6255 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6256 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6257 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6258 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6259 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6260 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6261 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6262 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6263 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6264 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6265 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6266 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6267 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6268 | || CR == APFloat::cmpLessThan |
| 6269 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6270 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 6273 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6274 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6275 | LValue LHSValue, RHSValue; |
| 6276 | |
| 6277 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 6278 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6279 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6280 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6281 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6282 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6283 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6284 | // Reject differing bases from the normal codepath; we special-case |
| 6285 | // comparisons to null. |
| 6286 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6287 | if (E->getOpcode() == BO_Sub) { |
| 6288 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6289 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 6290 | return false; |
| 6291 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | 7b2f93c | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 6292 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6293 | if (!LHSExpr || !RHSExpr) |
| 6294 | return false; |
| 6295 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6296 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6297 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6298 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 6299 | // Make sure both labels come from the same function. |
| 6300 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6301 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6302 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6303 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6304 | return true; |
| 6305 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6306 | // Inequalities and subtractions between unrelated pointers have |
| 6307 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 6308 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6309 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 6310 | // A constant address may compare equal to the address of a symbol. |
| 6311 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 6312 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 6313 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 6314 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6315 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6316 | // It's implementation-defined whether distinct literals will have |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 6317 | // distinct addresses. In clang, the result of such a comparison is |
| 6318 | // unspecified, so it is not a constant expression. However, we do know |
| 6319 | // that the address of a literal will be non-null. |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 6320 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 6321 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6322 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6323 | // We can't tell whether weak symbols will end up pointing to the same |
| 6324 | // object. |
| 6325 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6326 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6327 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 6328 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 6329 | // lead to inconsistent results for comparisons involving the address |
| 6330 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 6331 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 6332 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6333 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6334 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 6335 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 6336 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6337 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 6338 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 6339 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6340 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6341 | // C++11 [expr.add]p6: |
| 6342 | // Unless both pointers point to elements of the same array object, or |
| 6343 | // one past the last element of the array object, the behavior is |
| 6344 | // undefined. |
| 6345 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 6346 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 6347 | LHSDesignator, RHSDesignator)) |
| 6348 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 6349 | |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 6350 | QualType Type = E->getLHS()->getType(); |
| 6351 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6352 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6353 | CharUnits ElementSize; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6354 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6355 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6356 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6357 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 6358 | // and produce incorrect results when it overflows. Such behavior |
| 6359 | // appears to be non-conforming, but is common, so perhaps we should |
| 6360 | // assume the standard intended for such cases to be undefined behavior |
| 6361 | // and check for them. |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6362 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 6363 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 6364 | // overflow in the final conversion to ptrdiff_t. |
| 6365 | APSInt LHS( |
| 6366 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 6367 | APSInt RHS( |
| 6368 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 6369 | APSInt ElemSize( |
| 6370 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 6371 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 6372 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 6373 | |
| 6374 | if (Result.extend(65) != TrueResult) |
| 6375 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 6376 | return Success(Result, E); |
| 6377 | } |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 6378 | |
| 6379 | // C++11 [expr.rel]p3: |
| 6380 | // Pointers to void (after pointer conversions) can be compared, with a |
| 6381 | // result defined as follows: If both pointers represent the same |
| 6382 | // address or are both the null pointer value, the result is true if the |
| 6383 | // operator is <= or >= and false otherwise; otherwise the result is |
| 6384 | // unspecified. |
| 6385 | // We interpret this as applying to pointers to *cv* void. |
| 6386 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6387 | E->isRelationalOp()) |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 6388 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 6389 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6390 | // C++11 [expr.rel]p2: |
| 6391 | // - If two pointers point to non-static data members of the same object, |
| 6392 | // or to subobjects or array elements fo such members, recursively, the |
| 6393 | // pointer to the later declared member compares greater provided the |
| 6394 | // two members have the same access control and provided their class is |
| 6395 | // not a union. |
| 6396 | // [...] |
| 6397 | // - Otherwise pointer comparisons are unspecified. |
| 6398 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 6399 | E->isRelationalOp()) { |
| 6400 | bool WasArrayIndex; |
| 6401 | unsigned Mismatch = |
| 6402 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 6403 | RHSDesignator, WasArrayIndex); |
| 6404 | // At the point where the designators diverge, the comparison has a |
| 6405 | // specified value if: |
| 6406 | // - we are comparing array indices |
| 6407 | // - we are comparing fields of a union, or fields with the same access |
| 6408 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 6409 | // constant expression. |
| 6410 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 6411 | Mismatch < RHSDesignator.Entries.size()) { |
| 6412 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 6413 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 6414 | if (!LF && !RF) |
| 6415 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 6416 | else if (!LF) |
| 6417 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 6418 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 6419 | << RF->getParent() << RF; |
| 6420 | else if (!RF) |
| 6421 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 6422 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 6423 | << LF->getParent() << LF; |
| 6424 | else if (!LF->getParent()->isUnion() && |
| 6425 | LF->getAccess() != RF->getAccess()) |
| 6426 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 6427 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 6428 | << LF->getParent(); |
| 6429 | } |
| 6430 | } |
| 6431 | |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6432 | // The comparison here must be unsigned, and performed with the same |
| 6433 | // width as the pointer. |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6434 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 6435 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 6436 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 6437 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 6438 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 6439 | CompareLHS &= Mask; |
| 6440 | CompareRHS &= Mask; |
| 6441 | |
Eli Friedman | 2850376 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 6442 | // If there is a base and this is a relational operator, we can only |
| 6443 | // compare pointers within the object in question; otherwise, the result |
| 6444 | // depends on where the object is located in memory. |
| 6445 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 6446 | QualType BaseTy = getType(LHSValue.Base); |
| 6447 | if (BaseTy->isIncompleteType()) |
| 6448 | return Error(E); |
| 6449 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 6450 | uint64_t OffsetLimit = Size.getQuantity(); |
| 6451 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 6452 | return Error(E); |
| 6453 | } |
| 6454 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6455 | switch (E->getOpcode()) { |
| 6456 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 6457 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 6458 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 6459 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 6460 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 6461 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 6462 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 6463 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 6464 | } |
| 6465 | } |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 6466 | |
| 6467 | if (LHSTy->isMemberPointerType()) { |
| 6468 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 6469 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 6470 | |
| 6471 | MemberPtr LHSValue, RHSValue; |
| 6472 | |
| 6473 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 6474 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 6475 | return false; |
| 6476 | |
| 6477 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 6478 | return false; |
| 6479 | |
| 6480 | // C++11 [expr.eq]p2: |
| 6481 | // If both operands are null, they compare equal. Otherwise if only one is |
| 6482 | // null, they compare unequal. |
| 6483 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 6484 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 6485 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 6486 | } |
| 6487 | |
| 6488 | // Otherwise if either is a pointer to a virtual member function, the |
| 6489 | // result is unspecified. |
| 6490 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 6491 | if (MD->isVirtual()) |
| 6492 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 6493 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 6494 | if (MD->isVirtual()) |
| 6495 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 6496 | |
| 6497 | // Otherwise they compare equal if and only if they would refer to the |
| 6498 | // same member of the same most derived object or the same subobject if |
| 6499 | // they were dereferenced with a hypothetical object of the associated |
| 6500 | // class type. |
| 6501 | bool Equal = LHSValue == RHSValue; |
| 6502 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 6503 | } |
| 6504 | |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 6505 | if (LHSTy->isNullPtrType()) { |
| 6506 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 6507 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 6508 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 6509 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 6510 | // false otherwise. |
| 6511 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 6512 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 6513 | } |
| 6514 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6515 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 6516 | !RHSTy->isIntegralOrEnumerationType()) && |
| 6517 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 6518 | // We can't continue from here for non-integral types. |
| 6519 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6520 | } |
| 6521 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6522 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 6523 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 6524 | // result shall be the alignment of the referenced type." |
| 6525 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 6526 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 6527 | |
| 6528 | // __alignof is defined to return the preferred alignment. |
| 6529 | return Info.Ctx.toCharUnitsFromBits( |
| 6530 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6531 | } |
| 6532 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6533 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6534 | E = E->IgnoreParens(); |
| 6535 | |
John McCall | 10f6f06 | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 6536 | // The kinds of expressions that we have special-case logic here for |
| 6537 | // should be kept up to date with the special checks for those |
| 6538 | // expressions in Sema. |
| 6539 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6540 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6541 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6542 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6543 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 6544 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6545 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6546 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 6547 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 6548 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 6549 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6550 | return GetAlignOfType(E->getType()); |
| 6551 | } |
| 6552 | |
| 6553 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6554 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 6555 | /// a result as the expression's type. |
| 6556 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 6557 | const UnaryExprOrTypeTraitExpr *E) { |
| 6558 | switch(E->getKind()) { |
| 6559 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6560 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 6561 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6562 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 6563 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 6564 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6565 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6566 | case UETT_VecStep: { |
| 6567 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 6568 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6569 | if (Ty->isVectorType()) { |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 6570 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 6571 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6572 | // The vec_step built-in functions that take a 3-component |
| 6573 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 6574 | if (n == 3) |
| 6575 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 6576 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6577 | return Success(n, E); |
| 6578 | } else |
| 6579 | return Success(1, E); |
| 6580 | } |
| 6581 | |
| 6582 | case UETT_SizeOf: { |
| 6583 | QualType SrcTy = E->getTypeOfArgument(); |
| 6584 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 6585 | // the result is the size of the referenced type." |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6586 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 6587 | SrcTy = Ref->getPointeeType(); |
| 6588 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6589 | CharUnits Sizeof; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6590 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6591 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6592 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6599 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6600 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6601 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6602 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6603 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6604 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6605 | for (unsigned i = 0; i != n; ++i) { |
| 6606 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 6607 | switch (ON.getKind()) { |
| 6608 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6609 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6610 | APSInt IdxResult; |
| 6611 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 6612 | return false; |
| 6613 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 6614 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6615 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6616 | CurrentType = AT->getElementType(); |
| 6617 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 6618 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 6619 | break; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6620 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6621 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6622 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 6623 | FieldDecl *MemberDecl = ON.getField(); |
| 6624 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6625 | if (!RT) |
| 6626 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6627 | RecordDecl *RD = RT->getDecl(); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6628 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6629 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 6630 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6631 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 6632 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6633 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 6634 | break; |
| 6635 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6636 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6637 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 6638 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6639 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6640 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 6641 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 6642 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6643 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6644 | |
| 6645 | // Find the layout of the class whose base we are looking into. |
| 6646 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6647 | if (!RT) |
| 6648 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6649 | RecordDecl *RD = RT->getDecl(); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6650 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6651 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 6652 | |
| 6653 | // Find the base class itself. |
| 6654 | CurrentType = BaseSpec->getType(); |
| 6655 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 6656 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6657 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6658 | |
| 6659 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 6660 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6661 | break; |
| 6662 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6663 | } |
| 6664 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6665 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6668 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6669 | switch (E->getOpcode()) { |
| 6670 | default: |
| 6671 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 6672 | // See C99 6.6p3. |
| 6673 | return Error(E); |
| 6674 | case UO_Extension: |
| 6675 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 6676 | // If so, we could clear the diagnostic ID. |
| 6677 | return Visit(E->getSubExpr()); |
| 6678 | case UO_Plus: |
| 6679 | // The result is just the value. |
| 6680 | return Visit(E->getSubExpr()); |
| 6681 | case UO_Minus: { |
| 6682 | if (!Visit(E->getSubExpr())) |
| 6683 | return false; |
| 6684 | if (!Result.isInt()) return Error(E); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 6685 | const APSInt &Value = Result.getInt(); |
| 6686 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 6687 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 6688 | E->getType()); |
| 6689 | return Success(-Value, E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6690 | } |
| 6691 | case UO_Not: { |
| 6692 | if (!Visit(E->getSubExpr())) |
| 6693 | return false; |
| 6694 | if (!Result.isInt()) return Error(E); |
| 6695 | return Success(~Result.getInt(), E); |
| 6696 | } |
| 6697 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6698 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6699 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6700 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6701 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6702 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6703 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6705 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6706 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 6707 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6708 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6709 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 6710 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 6711 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 6712 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6713 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6714 | case CK_BaseToDerived: |
| 6715 | case CK_DerivedToBase: |
| 6716 | case CK_UncheckedDerivedToBase: |
| 6717 | case CK_Dynamic: |
| 6718 | case CK_ToUnion: |
| 6719 | case CK_ArrayToPointerDecay: |
| 6720 | case CK_FunctionToPointerDecay: |
| 6721 | case CK_NullToPointer: |
| 6722 | case CK_NullToMemberPointer: |
| 6723 | case CK_BaseToDerivedMemberPointer: |
| 6724 | case CK_DerivedToBaseMemberPointer: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 6725 | case CK_ReinterpretMemberPointer: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6726 | case CK_ConstructorConversion: |
| 6727 | case CK_IntegralToPointer: |
| 6728 | case CK_ToVoid: |
| 6729 | case CK_VectorSplat: |
| 6730 | case CK_IntegralToFloating: |
| 6731 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6732 | case CK_CPointerToObjCPointerCast: |
| 6733 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6734 | case CK_AnyPointerToBlockPointerCast: |
| 6735 | case CK_ObjCObjectLValueCast: |
| 6736 | case CK_FloatingRealToComplex: |
| 6737 | case CK_FloatingComplexToReal: |
| 6738 | case CK_FloatingComplexCast: |
| 6739 | case CK_FloatingComplexToIntegralComplex: |
| 6740 | case CK_IntegralRealToComplex: |
| 6741 | case CK_IntegralComplexCast: |
| 6742 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 6743 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6744 | case CK_ZeroToOCLEvent: |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 6745 | case CK_NonAtomicToAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6746 | llvm_unreachable("invalid cast kind for integral value"); |
| 6747 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 6748 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6749 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6750 | case CK_LValueBitCast: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6751 | case CK_ARCProduceObject: |
| 6752 | case CK_ARCConsumeObject: |
| 6753 | case CK_ARCReclaimReturnedObject: |
| 6754 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 6755 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6756 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6757 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 6758 | case CK_UserDefinedConversion: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6759 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6760 | case CK_AtomicToNonAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6761 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6762 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6763 | |
| 6764 | case CK_MemberPointerToBoolean: |
| 6765 | case CK_PointerToBoolean: |
| 6766 | case CK_IntegralToBoolean: |
| 6767 | case CK_FloatingToBoolean: |
| 6768 | case CK_FloatingComplexToBoolean: |
| 6769 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6770 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6771 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6772 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6773 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6776 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6777 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6778 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 6779 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 6780 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6781 | // Allow casts of address-of-label differences if they are no-ops |
| 6782 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 6783 | // be constant-evaluatable except in some narrow cases which are hard |
| 6784 | // to detect here. We let it through on the assumption the user knows |
| 6785 | // what they are doing.) |
| 6786 | if (Result.isAddrLabelDiff()) |
| 6787 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 6788 | // Only allow casts of lvalues if they are lossless. |
| 6789 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 6790 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6791 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6792 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 6793 | Result.getInt()), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6794 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6795 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6796 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 6797 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 6798 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6799 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 6800 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6801 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6802 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6803 | if (LV.getLValueBase()) { |
| 6804 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6805 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 6806 | // narrowing back down to pointer width in subsequent integral casts. |
| 6807 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6808 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6809 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6810 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 6811 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6812 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6813 | return true; |
| 6814 | } |
| 6815 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 6816 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 6817 | SrcType); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6818 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 6819 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6820 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6821 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6822 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 6823 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 6824 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6825 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 6826 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6827 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6828 | case CK_FloatingToIntegral: { |
| 6829 | APFloat F(0.0); |
| 6830 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 6831 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6832 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6833 | APSInt Value; |
| 6834 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 6835 | return false; |
| 6836 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6837 | } |
| 6838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6839 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6840 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6841 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 6842 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6843 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 6844 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6845 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6846 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 6847 | return false; |
| 6848 | if (!LV.isComplexInt()) |
| 6849 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6850 | return Success(LV.getComplexIntReal(), E); |
| 6851 | } |
| 6852 | |
| 6853 | return Visit(E->getSubExpr()); |
| 6854 | } |
| 6855 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6856 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6857 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6858 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6859 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 6860 | return false; |
| 6861 | if (!LV.isComplexInt()) |
| 6862 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6863 | return Success(LV.getComplexIntImag(), E); |
| 6864 | } |
| 6865 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 6866 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6867 | return Success(0, E); |
| 6868 | } |
| 6869 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6870 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 6871 | return Success(E->getPackLength(), E); |
| 6872 | } |
| 6873 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 6874 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 6875 | return Success(E->getValue(), E); |
| 6876 | } |
| 6877 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6878 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6879 | // Float Evaluation |
| 6880 | //===----------------------------------------------------------------------===// |
| 6881 | |
| 6882 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6883 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6884 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6885 | APFloat &Result; |
| 6886 | public: |
| 6887 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6888 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6889 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6890 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6891 | Result = V.getFloat(); |
| 6892 | return true; |
| 6893 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6894 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6895 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 6896 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 6897 | return true; |
| 6898 | } |
| 6899 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6900 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6901 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6902 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6903 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 6904 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6905 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6906 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 6907 | bool VisitUnaryReal(const UnaryOperator *E); |
| 6908 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 6909 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6910 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6911 | }; |
| 6912 | } // end anonymous namespace |
| 6913 | |
| 6914 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6915 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6916 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6919 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6920 | QualType ResultTy, |
| 6921 | const Expr *Arg, |
| 6922 | bool SNaN, |
| 6923 | llvm::APFloat &Result) { |
| 6924 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 6925 | if (!S) return false; |
| 6926 | |
| 6927 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 6928 | |
| 6929 | llvm::APInt fill; |
| 6930 | |
| 6931 | // Treat empty strings as if they were zero. |
| 6932 | if (S->getString().empty()) |
| 6933 | fill = llvm::APInt(32, 0); |
| 6934 | else if (S->getString().getAsInteger(0, fill)) |
| 6935 | return false; |
| 6936 | |
| 6937 | if (SNaN) |
| 6938 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 6939 | else |
| 6940 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 6941 | return true; |
| 6942 | } |
| 6943 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6944 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6945 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6946 | default: |
| 6947 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 6948 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6949 | case Builtin::BI__builtin_huge_val: |
| 6950 | case Builtin::BI__builtin_huge_valf: |
| 6951 | case Builtin::BI__builtin_huge_vall: |
| 6952 | case Builtin::BI__builtin_inf: |
| 6953 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 6954 | case Builtin::BI__builtin_infl: { |
| 6955 | const llvm::fltSemantics &Sem = |
| 6956 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 6957 | Result = llvm::APFloat::getInf(Sem); |
| 6958 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 6959 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6960 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6961 | case Builtin::BI__builtin_nans: |
| 6962 | case Builtin::BI__builtin_nansf: |
| 6963 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6964 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 6965 | true, Result)) |
| 6966 | return Error(E); |
| 6967 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6968 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 6969 | case Builtin::BI__builtin_nan: |
| 6970 | case Builtin::BI__builtin_nanf: |
| 6971 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 6972 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 6973 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6974 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 6975 | false, Result)) |
| 6976 | return Error(E); |
| 6977 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6978 | |
| 6979 | case Builtin::BI__builtin_fabs: |
| 6980 | case Builtin::BI__builtin_fabsf: |
| 6981 | case Builtin::BI__builtin_fabsl: |
| 6982 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 6983 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6984 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6985 | if (Result.isNegative()) |
| 6986 | Result.changeSign(); |
| 6987 | return true; |
| 6988 | |
Richard Smith | acaf72a | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6989 | // FIXME: Builtin::BI__builtin_powi |
| 6990 | // FIXME: Builtin::BI__builtin_powif |
| 6991 | // FIXME: Builtin::BI__builtin_powil |
| 6992 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6993 | case Builtin::BI__builtin_copysign: |
| 6994 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6995 | case Builtin::BI__builtin_copysignl: { |
| 6996 | APFloat RHS(0.); |
| 6997 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 6998 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 6999 | return false; |
| 7000 | Result.copySign(RHS); |
| 7001 | return true; |
| 7002 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7003 | } |
| 7004 | } |
| 7005 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7006 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7007 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7008 | ComplexValue CV; |
| 7009 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7010 | return false; |
| 7011 | Result = CV.FloatReal; |
| 7012 | return true; |
| 7013 | } |
| 7014 | |
| 7015 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7016 | } |
| 7017 | |
| 7018 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7019 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7020 | ComplexValue CV; |
| 7021 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7022 | return false; |
| 7023 | Result = CV.FloatImag; |
| 7024 | return true; |
| 7025 | } |
| 7026 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7027 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7028 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 7029 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7030 | return true; |
| 7031 | } |
| 7032 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7033 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7034 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7035 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7036 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7037 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7038 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7039 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 7040 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7041 | Result.changeSign(); |
| 7042 | return true; |
| 7043 | } |
| 7044 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7045 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7046 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7047 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 7048 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 7049 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7050 | APFloat RHS(0.0); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7051 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 7052 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7053 | return false; |
Richard Smith | a49a7fe | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7054 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 7055 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 7059 | Result = E->getValue(); |
| 7060 | return true; |
| 7061 | } |
| 7062 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7063 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7064 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7065 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7066 | switch (E->getCastKind()) { |
| 7067 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7068 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7069 | |
| 7070 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7071 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7072 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 7073 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 7074 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7075 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7076 | |
| 7077 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7078 | if (!Visit(SubExpr)) |
| 7079 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7080 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 7081 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7082 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7083 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7084 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7085 | ComplexValue V; |
| 7086 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 7087 | return false; |
| 7088 | Result = V.getComplexFloatReal(); |
| 7089 | return true; |
| 7090 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7091 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7094 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7095 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7096 | //===----------------------------------------------------------------------===// |
| 7097 | |
| 7098 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7099 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7100 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7101 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7102 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7103 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7104 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7105 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 7106 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7107 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7108 | Result.setFrom(V); |
| 7109 | return true; |
| 7110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7111 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7112 | bool ZeroInitialization(const Expr *E); |
| 7113 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7114 | //===--------------------------------------------------------------------===// |
| 7115 | // Visitor Methods |
| 7116 | //===--------------------------------------------------------------------===// |
| 7117 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7118 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7119 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7120 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7121 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7122 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7123 | }; |
| 7124 | } // end anonymous namespace |
| 7125 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7126 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 7127 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7128 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7129 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7132 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7133 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7134 | if (ElemTy->isRealFloatingType()) { |
| 7135 | Result.makeComplexFloat(); |
| 7136 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 7137 | Result.FloatReal = Zero; |
| 7138 | Result.FloatImag = Zero; |
| 7139 | } else { |
| 7140 | Result.makeComplexInt(); |
| 7141 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 7142 | Result.IntReal = Zero; |
| 7143 | Result.IntImag = Zero; |
| 7144 | } |
| 7145 | return true; |
| 7146 | } |
| 7147 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7148 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 7149 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7150 | |
| 7151 | if (SubExpr->getType()->isRealFloatingType()) { |
| 7152 | Result.makeComplexFloat(); |
| 7153 | APFloat &Imag = Result.FloatImag; |
| 7154 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 7155 | return false; |
| 7156 | |
| 7157 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 7158 | return true; |
| 7159 | } else { |
| 7160 | assert(SubExpr->getType()->isIntegerType() && |
| 7161 | "Unexpected imaginary literal."); |
| 7162 | |
| 7163 | Result.makeComplexInt(); |
| 7164 | APSInt &Imag = Result.IntImag; |
| 7165 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 7166 | return false; |
| 7167 | |
| 7168 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 7169 | return true; |
| 7170 | } |
| 7171 | } |
| 7172 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7173 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7174 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7175 | switch (E->getCastKind()) { |
| 7176 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7177 | case CK_BaseToDerived: |
| 7178 | case CK_DerivedToBase: |
| 7179 | case CK_UncheckedDerivedToBase: |
| 7180 | case CK_Dynamic: |
| 7181 | case CK_ToUnion: |
| 7182 | case CK_ArrayToPointerDecay: |
| 7183 | case CK_FunctionToPointerDecay: |
| 7184 | case CK_NullToPointer: |
| 7185 | case CK_NullToMemberPointer: |
| 7186 | case CK_BaseToDerivedMemberPointer: |
| 7187 | case CK_DerivedToBaseMemberPointer: |
| 7188 | case CK_MemberPointerToBoolean: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7189 | case CK_ReinterpretMemberPointer: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7190 | case CK_ConstructorConversion: |
| 7191 | case CK_IntegralToPointer: |
| 7192 | case CK_PointerToIntegral: |
| 7193 | case CK_PointerToBoolean: |
| 7194 | case CK_ToVoid: |
| 7195 | case CK_VectorSplat: |
| 7196 | case CK_IntegralCast: |
| 7197 | case CK_IntegralToBoolean: |
| 7198 | case CK_IntegralToFloating: |
| 7199 | case CK_FloatingToIntegral: |
| 7200 | case CK_FloatingToBoolean: |
| 7201 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7202 | case CK_CPointerToObjCPointerCast: |
| 7203 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7204 | case CK_AnyPointerToBlockPointerCast: |
| 7205 | case CK_ObjCObjectLValueCast: |
| 7206 | case CK_FloatingComplexToReal: |
| 7207 | case CK_FloatingComplexToBoolean: |
| 7208 | case CK_IntegralComplexToReal: |
| 7209 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7210 | case CK_ARCProduceObject: |
| 7211 | case CK_ARCConsumeObject: |
| 7212 | case CK_ARCReclaimReturnedObject: |
| 7213 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7214 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7215 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7216 | case CK_ZeroToOCLEvent: |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7217 | case CK_NonAtomicToAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7218 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 7219 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7220 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7221 | case CK_AtomicToNonAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7222 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7223 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7224 | |
| 7225 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7226 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7227 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7228 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7229 | |
| 7230 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7231 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7232 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7233 | return false; |
| 7234 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7235 | Result.makeComplexFloat(); |
| 7236 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 7237 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7238 | } |
| 7239 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7240 | case CK_FloatingComplexCast: { |
| 7241 | if (!Visit(E->getSubExpr())) |
| 7242 | return false; |
| 7243 | |
| 7244 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7245 | QualType From |
| 7246 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7247 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7248 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 7249 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7250 | } |
| 7251 | |
| 7252 | case CK_FloatingComplexToIntegralComplex: { |
| 7253 | if (!Visit(E->getSubExpr())) |
| 7254 | return false; |
| 7255 | |
| 7256 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7257 | QualType From |
| 7258 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7259 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7260 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 7261 | To, Result.IntReal) && |
| 7262 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 7263 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
| 7266 | case CK_IntegralRealToComplex: { |
| 7267 | APSInt &Real = Result.IntReal; |
| 7268 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 7269 | return false; |
| 7270 | |
| 7271 | Result.makeComplexInt(); |
| 7272 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 7273 | return true; |
| 7274 | } |
| 7275 | |
| 7276 | case CK_IntegralComplexCast: { |
| 7277 | if (!Visit(E->getSubExpr())) |
| 7278 | return false; |
| 7279 | |
| 7280 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7281 | QualType From |
| 7282 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7283 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7284 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 7285 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7286 | return true; |
| 7287 | } |
| 7288 | |
| 7289 | case CK_IntegralComplexToFloatingComplex: { |
| 7290 | if (!Visit(E->getSubExpr())) |
| 7291 | return false; |
| 7292 | |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7293 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7294 | QualType From |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7295 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7296 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7297 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 7298 | To, Result.FloatReal) && |
| 7299 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 7300 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7301 | } |
| 7302 | } |
| 7303 | |
| 7304 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7307 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7308 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 7309 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 7310 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7311 | bool LHSOK = Visit(E->getLHS()); |
| 7312 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7313 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7314 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7315 | ComplexValue RHS; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7316 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7317 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7318 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7319 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 7320 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7321 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7322 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7323 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7324 | if (Result.isComplexFloat()) { |
| 7325 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 7326 | APFloat::rmNearestTiesToEven); |
| 7327 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 7328 | APFloat::rmNearestTiesToEven); |
| 7329 | } else { |
| 7330 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 7331 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 7332 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7333 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7334 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7335 | if (Result.isComplexFloat()) { |
| 7336 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 7337 | APFloat::rmNearestTiesToEven); |
| 7338 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 7339 | APFloat::rmNearestTiesToEven); |
| 7340 | } else { |
| 7341 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 7342 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 7343 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7344 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7345 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7346 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7347 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7348 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 7349 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 7350 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 7351 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7352 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7353 | APFloat Tmp = LHS_r; |
| 7354 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7355 | Result.getComplexFloatReal() = Tmp; |
| 7356 | Tmp = LHS_i; |
| 7357 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7358 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 7359 | |
| 7360 | Tmp = LHS_r; |
| 7361 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7362 | Result.getComplexFloatImag() = Tmp; |
| 7363 | Tmp = LHS_i; |
| 7364 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7365 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 7366 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7367 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7368 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7369 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 7370 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7371 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 7372 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 7373 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 7374 | } |
| 7375 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7376 | case BO_Div: |
| 7377 | if (Result.isComplexFloat()) { |
| 7378 | ComplexValue LHS = Result; |
| 7379 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 7380 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 7381 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 7382 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 7383 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 7384 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 7385 | |
| 7386 | APFloat Den = RHS_r; |
| 7387 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7388 | APFloat Tmp = RHS_i; |
| 7389 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7390 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 7391 | |
| 7392 | Res_r = LHS_r; |
| 7393 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7394 | Tmp = LHS_i; |
| 7395 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7396 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 7397 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 7398 | |
| 7399 | Res_i = LHS_i; |
| 7400 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 7401 | Tmp = LHS_r; |
| 7402 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 7403 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 7404 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 7405 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7406 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 7407 | return Error(E, diag::note_expr_divide_by_zero); |
| 7408 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7409 | ComplexValue LHS = Result; |
| 7410 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 7411 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 7412 | Result.getComplexIntReal() = |
| 7413 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 7414 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 7415 | Result.getComplexIntImag() = |
| 7416 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 7417 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 7418 | } |
| 7419 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7420 | } |
| 7421 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7422 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 7423 | } |
| 7424 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7425 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 7426 | // Get the operand value into 'Result'. |
| 7427 | if (!Visit(E->getSubExpr())) |
| 7428 | return false; |
| 7429 | |
| 7430 | switch (E->getOpcode()) { |
| 7431 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7432 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7433 | case UO_Extension: |
| 7434 | return true; |
| 7435 | case UO_Plus: |
| 7436 | // The result is always just the subexpr. |
| 7437 | return true; |
| 7438 | case UO_Minus: |
| 7439 | if (Result.isComplexFloat()) { |
| 7440 | Result.getComplexFloatReal().changeSign(); |
| 7441 | Result.getComplexFloatImag().changeSign(); |
| 7442 | } |
| 7443 | else { |
| 7444 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 7445 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 7446 | } |
| 7447 | return true; |
| 7448 | case UO_Not: |
| 7449 | if (Result.isComplexFloat()) |
| 7450 | Result.getComplexFloatImag().changeSign(); |
| 7451 | else |
| 7452 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 7453 | return true; |
| 7454 | } |
| 7455 | } |
| 7456 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7457 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 7458 | if (E->getNumInits() == 2) { |
| 7459 | if (E->getType()->isComplexType()) { |
| 7460 | Result.makeComplexFloat(); |
| 7461 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 7462 | return false; |
| 7463 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 7464 | return false; |
| 7465 | } else { |
| 7466 | Result.makeComplexInt(); |
| 7467 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 7468 | return false; |
| 7469 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 7470 | return false; |
| 7471 | } |
| 7472 | return true; |
| 7473 | } |
| 7474 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 7475 | } |
| 7476 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7477 | //===----------------------------------------------------------------------===// |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7478 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 7479 | // implicit conversion. |
| 7480 | //===----------------------------------------------------------------------===// |
| 7481 | |
| 7482 | namespace { |
| 7483 | class AtomicExprEvaluator : |
| 7484 | public ExprEvaluatorBase<AtomicExprEvaluator, bool> { |
| 7485 | APValue &Result; |
| 7486 | public: |
| 7487 | AtomicExprEvaluator(EvalInfo &Info, APValue &Result) |
| 7488 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 7489 | |
| 7490 | bool Success(const APValue &V, const Expr *E) { |
| 7491 | Result = V; |
| 7492 | return true; |
| 7493 | } |
| 7494 | |
| 7495 | bool ZeroInitialization(const Expr *E) { |
| 7496 | ImplicitValueInitExpr VIE( |
| 7497 | E->getType()->castAs<AtomicType>()->getValueType()); |
| 7498 | return Evaluate(Result, Info, &VIE); |
| 7499 | } |
| 7500 | |
| 7501 | bool VisitCastExpr(const CastExpr *E) { |
| 7502 | switch (E->getCastKind()) { |
| 7503 | default: |
| 7504 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 7505 | case CK_NonAtomicToAtomic: |
| 7506 | return Evaluate(Result, Info, E->getSubExpr()); |
| 7507 | } |
| 7508 | } |
| 7509 | }; |
| 7510 | } // end anonymous namespace |
| 7511 | |
| 7512 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info) { |
| 7513 | assert(E->isRValue() && E->getType()->isAtomicType()); |
| 7514 | return AtomicExprEvaluator(Info, Result).Visit(E); |
| 7515 | } |
| 7516 | |
| 7517 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7518 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 7519 | // comma operator |
| 7520 | //===----------------------------------------------------------------------===// |
| 7521 | |
| 7522 | namespace { |
| 7523 | class VoidExprEvaluator |
| 7524 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 7525 | public: |
| 7526 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 7527 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7528 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7529 | |
| 7530 | bool VisitCastExpr(const CastExpr *E) { |
| 7531 | switch (E->getCastKind()) { |
| 7532 | default: |
| 7533 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 7534 | case CK_ToVoid: |
| 7535 | VisitIgnoredValue(E->getSubExpr()); |
| 7536 | return true; |
| 7537 | } |
| 7538 | } |
| 7539 | }; |
| 7540 | } // end anonymous namespace |
| 7541 | |
| 7542 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 7543 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 7544 | return VoidExprEvaluator(Info).Visit(E); |
| 7545 | } |
| 7546 | |
| 7547 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7548 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7549 | //===----------------------------------------------------------------------===// |
| 7550 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7551 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7552 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 7553 | // are. |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7554 | QualType T = E->getType(); |
| 7555 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7556 | LValue LV; |
| 7557 | if (!EvaluateLValue(E, LV, Info)) |
| 7558 | return false; |
| 7559 | LV.moveInto(Result); |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7560 | } else if (T->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7561 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7562 | return false; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7563 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7564 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7565 | return false; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7566 | } else if (T->hasPointerRepresentation()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7567 | LValue LV; |
| 7568 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7569 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7570 | LV.moveInto(Result); |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7571 | } else if (T->isRealFloatingType()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7572 | llvm::APFloat F(0.0); |
| 7573 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7574 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7575 | Result = APValue(F); |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7576 | } else if (T->isAnyComplexType()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7577 | ComplexValue C; |
| 7578 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7579 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 7580 | C.moveInto(Result); |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7581 | } else if (T->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7582 | MemberPtr P; |
| 7583 | if (!EvaluateMemberPointer(E, P, Info)) |
| 7584 | return false; |
| 7585 | P.moveInto(Result); |
| 7586 | return true; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7587 | } else if (T->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7588 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7589 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7590 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7591 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7592 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7593 | } else if (T->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7594 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7595 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7596 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 7597 | return false; |
| 7598 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7599 | } else if (T->isVoidType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7600 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7601 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7602 | << E->getType(); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 7603 | if (!EvaluateVoid(E, Info)) |
| 7604 | return false; |
Richard Smith | 5705f21 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7605 | } else if (T->isAtomicType()) { |
| 7606 | if (!EvaluateAtomic(E, Result, Info)) |
| 7607 | return false; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7608 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7609 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7610 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7611 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7612 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 7613 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7614 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 7615 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 7616 | return true; |
| 7617 | } |
| 7618 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7619 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 7620 | /// cases, the in-place evaluation is essential, since later initializers for |
| 7621 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 7622 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 7623 | const Expr *E, bool AllowNonLiteralTypes) { |
| 7624 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7625 | return false; |
| 7626 | |
| 7627 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7628 | // Evaluate arrays and record types in-place, so that later initializers can |
| 7629 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7630 | if (E->getType()->isArrayType()) |
| 7631 | return EvaluateArray(E, This, Result, Info); |
| 7632 | else if (E->getType()->isRecordType()) |
| 7633 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7637 | return Evaluate(Result, Info, E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7638 | } |
| 7639 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7640 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 7641 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 7642 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7643 | if (!CheckLiteralType(Info, E)) |
| 7644 | return false; |
| 7645 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7646 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7647 | return false; |
| 7648 | |
| 7649 | if (E->isGLValue()) { |
| 7650 | LValue LV; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7651 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 5528ac9 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7652 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7653 | return false; |
| 7654 | } |
| 7655 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7656 | // Check this core constant expression is a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7657 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7658 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7659 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7660 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| 7661 | const ASTContext &Ctx, bool &IsConst) { |
| 7662 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 7663 | // containing vast quantities of these. |
| 7664 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 7665 | Result.Val = APValue(APSInt(L->getValue(), |
| 7666 | L->getType()->isUnsignedIntegerType())); |
| 7667 | IsConst = true; |
| 7668 | return true; |
| 7669 | } |
| 7670 | |
| 7671 | // FIXME: Evaluating values of large array and record types can cause |
| 7672 | // performance problems. Only do so in C++11 for now. |
| 7673 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 7674 | Exp->getType()->isRecordType()) && |
| 7675 | !Ctx.getLangOpts().CPlusPlus11) { |
| 7676 | IsConst = false; |
| 7677 | return true; |
| 7678 | } |
| 7679 | return false; |
| 7680 | } |
| 7681 | |
| 7682 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7683 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7684 | /// any crazy technique (that has nothing to do with language standards) that |
| 7685 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7686 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 7687 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7688 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7689 | bool IsConst; |
| 7690 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
| 7691 | return IsConst; |
| 7692 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7693 | EvalInfo Info(Ctx, Result); |
| 7694 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7695 | } |
| 7696 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7697 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 7698 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7699 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7700 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7701 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 7702 | } |
| 7703 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7704 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 7705 | SideEffectsKind AllowSideEffects) const { |
| 7706 | if (!getType()->isIntegralOrEnumerationType()) |
| 7707 | return false; |
| 7708 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7709 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7710 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 7711 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7712 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7713 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7714 | Result = ExprResult.Val.getInt(); |
| 7715 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7718 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 7719 | EvalInfo Info(Ctx, Result); |
| 7720 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7721 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7722 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 7723 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 7724 | Ctx.getLValueReferenceType(getType()), LV)) |
| 7725 | return false; |
| 7726 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7727 | LV.moveInto(Result.Val); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7728 | return true; |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 7729 | } |
| 7730 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7731 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 7732 | const VarDecl *VD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7733 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 7734 | // FIXME: Evaluating initializers for large array and record types can cause |
| 7735 | // performance problems. Only do so in C++11 for now. |
| 7736 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7737 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 7738 | return false; |
| 7739 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7740 | Expr::EvalStatus EStatus; |
| 7741 | EStatus.Diag = &Notes; |
| 7742 | |
| 7743 | EvalInfo InitInfo(Ctx, EStatus); |
| 7744 | InitInfo.setEvaluatingDecl(VD, Value); |
| 7745 | |
| 7746 | LValue LVal; |
| 7747 | LVal.set(VD); |
| 7748 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7749 | // C++11 [basic.start.init]p2: |
| 7750 | // Variables with static storage duration or thread storage duration shall be |
| 7751 | // zero-initialized before any other initialization takes place. |
| 7752 | // This behavior is not present in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7753 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7754 | !VD->getType()->isReferenceType()) { |
| 7755 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 7756 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7757 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7758 | return false; |
| 7759 | } |
| 7760 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 7761 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 7762 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7763 | EStatus.HasSideEffects) |
| 7764 | return false; |
| 7765 | |
| 7766 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 7767 | Value); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7770 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 7771 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7772 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 7773 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7774 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 7775 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7776 | |
Fariborz Jahanian | a18e70b | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 7777 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7778 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7779 | EvalResult EvalResult; |
Fariborz Jahanian | a18e70b | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 7780 | EvalResult.Diag = Diag; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7781 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 7782 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7783 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7784 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7785 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7786 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7787 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7788 | |
Fariborz Jahanian | ad48a50 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7789 | void Expr::EvaluateForOverflow(const ASTContext &Ctx, |
| 7790 | SmallVectorImpl<PartialDiagnosticAt> *Diags) const { |
| 7791 | bool IsConst; |
| 7792 | EvalResult EvalResult; |
| 7793 | EvalResult.Diag = Diags; |
| 7794 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
| 7795 | EvalInfo Info(Ctx, EvalResult, true); |
| 7796 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 7797 | } |
| 7798 | } |
| 7799 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7800 | bool Expr::EvalResult::isGlobalLValue() const { |
| 7801 | assert(Val.isLValue()); |
| 7802 | return IsGlobalLValue(Val.getLValueBase()); |
| 7803 | } |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 7804 | |
| 7805 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7806 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 7807 | /// an integer constant expression. |
| 7808 | |
| 7809 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 7810 | /// comma, etc |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7811 | |
| 7812 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7813 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 7814 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 7815 | // |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7816 | // Note that to reduce code duplication, this helper does no evaluation |
| 7817 | // itself; the caller checks whether the expression is evaluatable, and |
| 7818 | // in the rare cases where CheckICE actually cares about the evaluated |
| 7819 | // value, it calls into Evalute. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7820 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 7821 | namespace { |
| 7822 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7823 | enum ICEKind { |
| 7824 | /// This expression is an ICE. |
| 7825 | IK_ICE, |
| 7826 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 7827 | /// a legal subexpression for an ICE. This return value is used to handle |
| 7828 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 7829 | IK_ICEIfUnevaluated, |
| 7830 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 7831 | IK_NotICE |
| 7832 | }; |
| 7833 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7834 | struct ICEDiag { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7835 | ICEKind Kind; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7836 | SourceLocation Loc; |
| 7837 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7838 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7839 | }; |
| 7840 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 7841 | } |
| 7842 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7843 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 7844 | |
| 7845 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7846 | |
| 7847 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 7848 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7849 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7850 | !EVResult.Val.isInt()) |
| 7851 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 7852 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7853 | return NoDiag(); |
| 7854 | } |
| 7855 | |
| 7856 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 7857 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7858 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 7859 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7860 | |
| 7861 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 7862 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7863 | #define STMT(Node, Base) case Expr::Node##Class: |
| 7864 | #define EXPR(Node, Base) |
| 7865 | #include "clang/AST/StmtNodes.inc" |
| 7866 | case Expr::PredefinedExprClass: |
| 7867 | case Expr::FloatingLiteralClass: |
| 7868 | case Expr::ImaginaryLiteralClass: |
| 7869 | case Expr::StringLiteralClass: |
| 7870 | case Expr::ArraySubscriptExprClass: |
| 7871 | case Expr::MemberExprClass: |
| 7872 | case Expr::CompoundAssignOperatorClass: |
| 7873 | case Expr::CompoundLiteralExprClass: |
| 7874 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7875 | case Expr::DesignatedInitExprClass: |
| 7876 | case Expr::ImplicitValueInitExprClass: |
| 7877 | case Expr::ParenListExprClass: |
| 7878 | case Expr::VAArgExprClass: |
| 7879 | case Expr::AddrLabelExprClass: |
| 7880 | case Expr::StmtExprClass: |
| 7881 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7882 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7883 | case Expr::CXXDynamicCastExprClass: |
| 7884 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 7885 | case Expr::CXXUuidofExprClass: |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 7886 | case Expr::MSPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7887 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 7888 | case Expr::UserDefinedLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7889 | case Expr::CXXThisExprClass: |
| 7890 | case Expr::CXXThrowExprClass: |
| 7891 | case Expr::CXXNewExprClass: |
| 7892 | case Expr::CXXDeleteExprClass: |
| 7893 | case Expr::CXXPseudoDestructorExprClass: |
| 7894 | case Expr::UnresolvedLookupExprClass: |
| 7895 | case Expr::DependentScopeDeclRefExprClass: |
| 7896 | case Expr::CXXConstructExprClass: |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 7897 | case Expr::CXXStdInitializerListExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7898 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7899 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7900 | case Expr::CXXTemporaryObjectExprClass: |
| 7901 | case Expr::CXXUnresolvedConstructExprClass: |
| 7902 | case Expr::CXXDependentScopeMemberExprClass: |
| 7903 | case Expr::UnresolvedMemberExprClass: |
| 7904 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 7905 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7906 | case Expr::ObjCArrayLiteralClass: |
| 7907 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7908 | case Expr::ObjCEncodeExprClass: |
| 7909 | case Expr::ObjCMessageExprClass: |
| 7910 | case Expr::ObjCSelectorExprClass: |
| 7911 | case Expr::ObjCProtocolExprClass: |
| 7912 | case Expr::ObjCIvarRefExprClass: |
| 7913 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7914 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7915 | case Expr::ObjCIsaExprClass: |
| 7916 | case Expr::ShuffleVectorExprClass: |
| 7917 | case Expr::BlockExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7918 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 7919 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 7920 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 7921 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | 9a4db03 | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 7922 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 7923 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7924 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7925 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7926 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 7927 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7928 | case Expr::InitListExprClass: |
Douglas Gregor | 01d0801 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 7929 | case Expr::LambdaExprClass: |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7930 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7931 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7932 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7933 | case Expr::GNUNullExprClass: |
| 7934 | // GCC considers the GNU __null value to be an integral constant expression. |
| 7935 | return NoDiag(); |
| 7936 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 7937 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 7938 | return |
| 7939 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 7940 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7941 | case Expr::ParenExprClass: |
| 7942 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 7943 | case Expr::GenericSelectionExprClass: |
| 7944 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7945 | case Expr::IntegerLiteralClass: |
| 7946 | case Expr::CharacterLiteralClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7947 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7948 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 7949 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7950 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 7951 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 7952 | case Expr::TypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 7953 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 7954 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 7955 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7956 | return NoDiag(); |
| 7957 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 7958 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 7959 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 7960 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 7961 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7962 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7963 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7964 | return CheckEvalInICE(E, Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7965 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7966 | } |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7967 | case Expr::DeclRefExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7968 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 7969 | return NoDiag(); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7970 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7971 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7972 | D && IsConstNonVolatile(D->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7973 | // Parameter variables are never constants. Without this check, |
| 7974 | // getAnyInitializer() can find a default argument, which leads |
| 7975 | // to chaos. |
| 7976 | if (isa<ParmVarDecl>(D)) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7977 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7978 | |
| 7979 | // C++ 7.1.5.1p2 |
| 7980 | // A variable of non-volatile const-qualified integral or enumeration |
| 7981 | // type initialized by an ICE can be used in ICEs. |
| 7982 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 7983 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7984 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 7985 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7986 | const VarDecl *VD; |
| 7987 | // Look for a declaration of this variable that has an initializer, and |
| 7988 | // check whether it is an ICE. |
| 7989 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 7990 | return NoDiag(); |
| 7991 | else |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7992 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7993 | } |
| 7994 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7995 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7996 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7997 | case Expr::UnaryOperatorClass: { |
| 7998 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 7999 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8000 | case UO_PostInc: |
| 8001 | case UO_PostDec: |
| 8002 | case UO_PreInc: |
| 8003 | case UO_PreDec: |
| 8004 | case UO_AddrOf: |
| 8005 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8006 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 8007 | // subexpressions of constant expressions, but they can never be ICEs |
| 8008 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8009 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8010 | case UO_Extension: |
| 8011 | case UO_LNot: |
| 8012 | case UO_Plus: |
| 8013 | case UO_Minus: |
| 8014 | case UO_Not: |
| 8015 | case UO_Real: |
| 8016 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8017 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8018 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8019 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8020 | // OffsetOf falls through here. |
| 8021 | } |
| 8022 | case Expr::OffsetOfExprClass: { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8023 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 8024 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 8025 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 8026 | // compliance: we should warn earlier for offsetof expressions with |
| 8027 | // array subscripts that aren't ICEs, and if the array subscripts |
| 8028 | // are ICEs, the value of the offsetof must be an integer constant. |
| 8029 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8030 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8031 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 8032 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 8033 | if ((Exp->getKind() == UETT_SizeOf) && |
| 8034 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8035 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8036 | return NoDiag(); |
| 8037 | } |
| 8038 | case Expr::BinaryOperatorClass: { |
| 8039 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 8040 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8041 | case BO_PtrMemD: |
| 8042 | case BO_PtrMemI: |
| 8043 | case BO_Assign: |
| 8044 | case BO_MulAssign: |
| 8045 | case BO_DivAssign: |
| 8046 | case BO_RemAssign: |
| 8047 | case BO_AddAssign: |
| 8048 | case BO_SubAssign: |
| 8049 | case BO_ShlAssign: |
| 8050 | case BO_ShrAssign: |
| 8051 | case BO_AndAssign: |
| 8052 | case BO_XorAssign: |
| 8053 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8054 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 8055 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8056 | // contain an lvalue operand. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8057 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8058 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8059 | case BO_Mul: |
| 8060 | case BO_Div: |
| 8061 | case BO_Rem: |
| 8062 | case BO_Add: |
| 8063 | case BO_Sub: |
| 8064 | case BO_Shl: |
| 8065 | case BO_Shr: |
| 8066 | case BO_LT: |
| 8067 | case BO_GT: |
| 8068 | case BO_LE: |
| 8069 | case BO_GE: |
| 8070 | case BO_EQ: |
| 8071 | case BO_NE: |
| 8072 | case BO_And: |
| 8073 | case BO_Xor: |
| 8074 | case BO_Or: |
| 8075 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8076 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8077 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8078 | if (Exp->getOpcode() == BO_Div || |
| 8079 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8080 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8081 | // we don't evaluate one. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8082 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8083 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8084 | if (REval == 0) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8085 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8086 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8087 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8088 | if (LEval.isMinSignedValue()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8089 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8090 | } |
| 8091 | } |
| 8092 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8093 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8094 | if (Ctx.getLangOpts().C99) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8095 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 8096 | // if it isn't evaluated. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8097 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 8098 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8099 | } else { |
| 8100 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8101 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8102 | } |
| 8103 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8104 | return Worst(LHSResult, RHSResult); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8105 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8106 | case BO_LAnd: |
| 8107 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8108 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8109 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8110 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8111 | // Rare case where the RHS has a comma "side-effect"; we need |
| 8112 | // to actually check the condition to see whether the side |
| 8113 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8114 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8115 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8116 | return RHSResult; |
| 8117 | return NoDiag(); |
| 8118 | } |
| 8119 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8120 | return Worst(LHSResult, RHSResult); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8121 | } |
| 8122 | } |
| 8123 | } |
| 8124 | case Expr::ImplicitCastExprClass: |
| 8125 | case Expr::CStyleCastExprClass: |
| 8126 | case Expr::CXXFunctionalCastExprClass: |
| 8127 | case Expr::CXXStaticCastExprClass: |
| 8128 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 8129 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8130 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8131 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 8132 | if (isa<ExplicitCastExpr>(E)) { |
| 8133 | if (const FloatingLiteral *FL |
| 8134 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 8135 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 8136 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 8137 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 8138 | bool Ignored; |
| 8139 | // If the value does not fit in the destination type, the behavior is |
| 8140 | // undefined, so we are not required to treat it as a constant |
| 8141 | // expression. |
| 8142 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 8143 | llvm::APFloat::rmTowardZero, |
| 8144 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8145 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 8146 | return NoDiag(); |
| 8147 | } |
| 8148 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8149 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 8150 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 8151 | case CK_AtomicToNonAtomic: |
| 8152 | case CK_NonAtomicToAtomic: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8153 | case CK_NoOp: |
| 8154 | case CK_IntegralToBoolean: |
| 8155 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8156 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8157 | default: |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8158 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 8159 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8160 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8161 | case Expr::BinaryConditionalOperatorClass: { |
| 8162 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 8163 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8164 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8165 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8166 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 8167 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 8168 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 9b403c5 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 8169 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8170 | return FalseResult; |
| 8171 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8172 | case Expr::ConditionalOperatorClass: { |
| 8173 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 8174 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 8175 | // then only the true side is actually considered in an integer constant |
| 8176 | // expression, and it is fully evaluated. This is an important GNU |
| 8177 | // extension. See GCC PR38377 for discussion. |
| 8178 | if (const CallExpr *CallCE |
| 8179 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8180 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 8181 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8182 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8183 | if (CondResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8184 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8185 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8186 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 8187 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 8188 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8189 | if (TrueResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8190 | return TrueResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8191 | if (FalseResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8192 | return FalseResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8193 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8194 | return CondResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8195 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8196 | return NoDiag(); |
| 8197 | // Rare case where the diagnostics depend on which side is evaluated |
| 8198 | // Note that if we get here, CondResult is 0, and at least one of |
| 8199 | // TrueResult and FalseResult is non-zero. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8200 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8201 | return FalseResult; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8202 | return TrueResult; |
| 8203 | } |
| 8204 | case Expr::CXXDefaultArgExprClass: |
| 8205 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8206 | case Expr::CXXDefaultInitExprClass: |
| 8207 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8208 | case Expr::ChooseExprClass: { |
| 8209 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 8210 | } |
| 8211 | } |
| 8212 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 8213 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8214 | } |
| 8215 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8216 | /// Evaluate an expression as a C++11 integral constant expression. |
| 8217 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 8218 | const Expr *E, |
| 8219 | llvm::APSInt *Value, |
| 8220 | SourceLocation *Loc) { |
| 8221 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 8222 | if (Loc) *Loc = E->getExprLoc(); |
| 8223 | return false; |
| 8224 | } |
| 8225 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8226 | APValue Result; |
| 8227 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 8228 | return false; |
| 8229 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8230 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 8231 | if (Value) *Value = Result.getInt(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 8232 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8233 | } |
| 8234 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 8235 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8236 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8237 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 8238 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8239 | ICEDiag D = CheckICE(this, Ctx); |
| 8240 | if (D.Kind != IK_ICE) { |
| 8241 | if (Loc) *Loc = D.Loc; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8242 | return false; |
| 8243 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8244 | return true; |
| 8245 | } |
| 8246 | |
| 8247 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 8248 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8249 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8250 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 8251 | |
| 8252 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 8253 | return false; |
| 8254 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8255 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8256 | return true; |
| 8257 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8258 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 8259 | bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8260 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 8261 | } |
| 8262 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8263 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 8264 | SourceLocation *Loc) const { |
| 8265 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 8266 | // issues. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8267 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8268 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 8269 | // Build evaluation settings. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8270 | Expr::EvalStatus Status; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8271 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 8272 | Status.Diag = &Diags; |
| 8273 | EvalInfo Info(Ctx, Status); |
| 8274 | |
| 8275 | APValue Scratch; |
| 8276 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 8277 | |
| 8278 | if (!Diags.empty()) { |
| 8279 | IsConstExpr = false; |
| 8280 | if (Loc) *Loc = Diags[0].first; |
| 8281 | } else if (!IsConstExpr) { |
| 8282 | // FIXME: This shouldn't happen. |
| 8283 | if (Loc) *Loc = getExprLoc(); |
| 8284 | } |
| 8285 | |
| 8286 | return IsConstExpr; |
| 8287 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8288 | |
| 8289 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8290 | SmallVectorImpl< |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8291 | PartialDiagnosticAt> &Diags) { |
| 8292 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 8293 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 8294 | // ASTs which we build for dependent expressions. |
| 8295 | if (FD->isDependentContext()) |
| 8296 | return true; |
| 8297 | |
| 8298 | Expr::EvalStatus Status; |
| 8299 | Status.Diag = &Diags; |
| 8300 | |
| 8301 | EvalInfo Info(FD->getASTContext(), Status); |
| 8302 | Info.CheckingPotentialConstantExpression = true; |
| 8303 | |
| 8304 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 8305 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 8306 | |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8307 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8308 | // is a temporary being used as the 'this' pointer. |
| 8309 | LValue This; |
| 8310 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8311 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8312 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8313 | ArrayRef<const Expr*> Args; |
| 8314 | |
| 8315 | SourceLocation Loc = FD->getLocation(); |
| 8316 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8317 | APValue Scratch; |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8318 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 8319 | // Evaluate the call as a constant initializer, to allow the construction |
| 8320 | // of objects of non-literal types. |
| 8321 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8322 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 6391ea2 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8323 | } else |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8324 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 8325 | Args, FD->getBody(), Info, Scratch); |
| 8326 | |
| 8327 | return Diags.empty(); |
| 8328 | } |