Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
| 26 | // (under the C++11 rules only, at the moment), or, if folding failed too, |
| 27 | // why the expression could not be folded. |
| 28 | // |
| 29 | // If we are checking for a potential constant expression, failure to constant |
| 30 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 31 | // return value (the expression could not be folded) and no diagnostic (the |
| 32 | // expression is not necessarily non-constant). |
| 33 | // |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 38 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 39 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 40 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 41 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 42 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Argyrios Kyrtzidis | b2c60b0 | 2012-03-01 19:45:56 +0000 | [diff] [blame^] | 47 | #include "llvm/Support/SaveAndRestore.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 63 | if (!B) return QualType(); |
| 64 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 65 | return D->getType(); |
| 66 | return B.get<const Expr*>()->getType(); |
| 67 | } |
| 68 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 69 | /// Get an LValue path entry, which is known to not be an array index, as a |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 70 | /// field or base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 71 | static |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 72 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 73 | APValue::BaseOrMemberType Value; |
| 74 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 75 | return Value; |
| 76 | } |
| 77 | |
| 78 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 79 | /// field declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 80 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 81 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 82 | } |
| 83 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 84 | /// base class declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 85 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 86 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 87 | } |
| 88 | /// Determine whether this LValue path entry for a base class names a virtual |
| 89 | /// base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 90 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 91 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 94 | /// Find the path length and type of the most-derived subobject in the given |
| 95 | /// path, and find the size of the containing array, if any. |
| 96 | static |
| 97 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 98 | ArrayRef<APValue::LValuePathEntry> Path, |
| 99 | uint64_t &ArraySize, QualType &Type) { |
| 100 | unsigned MostDerivedLength = 0; |
| 101 | Type = Base; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 102 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 103 | if (Type->isArrayType()) { |
| 104 | const ConstantArrayType *CAT = |
| 105 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 106 | Type = CAT->getElementType(); |
| 107 | ArraySize = CAT->getSize().getZExtValue(); |
| 108 | MostDerivedLength = I + 1; |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 109 | } else if (Type->isAnyComplexType()) { |
| 110 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 111 | Type = CT->getElementType(); |
| 112 | ArraySize = 2; |
| 113 | MostDerivedLength = I + 1; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 114 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 115 | Type = FD->getType(); |
| 116 | ArraySize = 0; |
| 117 | MostDerivedLength = I + 1; |
| 118 | } else { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 119 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 120 | ArraySize = 0; |
| 121 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 122 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 123 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 126 | // The order of this enum is important for diagnostics. |
| 127 | enum CheckSubobjectKind { |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 128 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 129 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 132 | /// A path from a glvalue to a subobject of that glvalue. |
| 133 | struct SubobjectDesignator { |
| 134 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 135 | /// lvalues can still be folded, but they are not core constant expressions |
| 136 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 137 | bool Invalid : 1; |
| 138 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 139 | /// Is this a pointer one past the end of an object? |
| 140 | bool IsOnePastTheEnd : 1; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 141 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 142 | /// The length of the path to the most-derived object of which this is a |
| 143 | /// subobject. |
| 144 | unsigned MostDerivedPathLength : 30; |
| 145 | |
| 146 | /// The size of the array of which the most-derived object is an element, or |
| 147 | /// 0 if the most-derived object is not an array element. |
| 148 | uint64_t MostDerivedArraySize; |
| 149 | |
| 150 | /// The type of the most derived object referred to by this address. |
| 151 | QualType MostDerivedType; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 153 | typedef APValue::LValuePathEntry PathEntry; |
| 154 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 155 | /// The entries on the path from the glvalue to the designated subobject. |
| 156 | SmallVector<PathEntry, 8> Entries; |
| 157 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 158 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 160 | explicit SubobjectDesignator(QualType T) |
| 161 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 162 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 163 | |
| 164 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 165 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 166 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 167 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 168 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 169 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 170 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 171 | if (V.getLValueBase()) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 172 | MostDerivedPathLength = |
| 173 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 174 | V.getLValuePath(), MostDerivedArraySize, |
| 175 | MostDerivedType); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 179 | void setInvalid() { |
| 180 | Invalid = true; |
| 181 | Entries.clear(); |
| 182 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 183 | |
| 184 | /// Determine whether this is a one-past-the-end pointer. |
| 185 | bool isOnePastTheEnd() const { |
| 186 | if (IsOnePastTheEnd) |
| 187 | return true; |
| 188 | if (MostDerivedArraySize && |
| 189 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 190 | return true; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /// Check that this refers to a valid subobject. |
| 195 | bool isValidSubobject() const { |
| 196 | if (Invalid) |
| 197 | return false; |
| 198 | return !isOnePastTheEnd(); |
| 199 | } |
| 200 | /// Check that this refers to a valid subobject, and if not, produce a |
| 201 | /// relevant diagnostic and set the designator as invalid. |
| 202 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 203 | |
| 204 | /// Update this designator to refer to the first element within this array. |
| 205 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 206 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 207 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 208 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 209 | |
| 210 | // This is a most-derived object. |
| 211 | MostDerivedType = CAT->getElementType(); |
| 212 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 213 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 214 | } |
| 215 | /// Update this designator to refer to the given base or member of this |
| 216 | /// object. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 217 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 218 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 219 | APValue::BaseOrMemberType Value(D, Virtual); |
| 220 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 221 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 222 | |
| 223 | // If this isn't a base class, it's a new most-derived object. |
| 224 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 225 | MostDerivedType = FD->getType(); |
| 226 | MostDerivedArraySize = 0; |
| 227 | MostDerivedPathLength = Entries.size(); |
| 228 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 229 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 230 | /// Update this designator to refer to the given complex component. |
| 231 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 232 | PathEntry Entry; |
| 233 | Entry.ArrayIndex = Imag; |
| 234 | Entries.push_back(Entry); |
| 235 | |
| 236 | // This is technically a most-derived object, though in practice this |
| 237 | // is unlikely to matter. |
| 238 | MostDerivedType = EltTy; |
| 239 | MostDerivedArraySize = 2; |
| 240 | MostDerivedPathLength = Entries.size(); |
| 241 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 242 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 243 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 244 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 245 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 246 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 247 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 248 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 249 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 250 | setInvalid(); |
| 251 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 254 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 255 | // nonarray object behaves the same as a pointer to the first element of |
| 256 | // an array of length one with the type of the object as its element type. |
| 257 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 258 | IsOnePastTheEnd = false; |
| 259 | else if (!IsOnePastTheEnd && N == 1) |
| 260 | IsOnePastTheEnd = true; |
| 261 | else if (N != 0) { |
| 262 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 263 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 264 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 268 | /// A core constant value. This can be the value of any constant expression, |
| 269 | /// or a pointer or reference to a non-static object or function parameter. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 270 | /// |
| 271 | /// For an LValue, the base and offset are stored in the APValue subobject, |
| 272 | /// but the other information is stored in the SubobjectDesignator. For all |
| 273 | /// other value kinds, the value is stored directly in the APValue subobject. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 274 | class CCValue : public APValue { |
| 275 | typedef llvm::APSInt APSInt; |
| 276 | typedef llvm::APFloat APFloat; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 277 | /// If the value is a reference or pointer, this is a description of how the |
| 278 | /// subobject was specified. |
| 279 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 280 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 281 | struct GlobalValue {}; |
| 282 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 283 | CCValue() {} |
| 284 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 285 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 286 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 287 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 288 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 289 | CCValue(const CCValue &V) : APValue(V), Designator(V.Designator) {} |
| 290 | CCValue(LValueBase B, const CharUnits &O, unsigned I, |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 291 | const SubobjectDesignator &D) : |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 292 | APValue(B, O, APValue::NoLValuePath(), I), Designator(D) {} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 293 | CCValue(ASTContext &Ctx, const APValue &V, GlobalValue) : |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 294 | APValue(V), Designator(Ctx, V) { |
| 295 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 296 | CCValue(const ValueDecl *D, bool IsDerivedMember, |
| 297 | ArrayRef<const CXXRecordDecl*> Path) : |
| 298 | APValue(D, IsDerivedMember, Path) {} |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 299 | CCValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) : |
| 300 | APValue(LHSExpr, RHSExpr) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 301 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 302 | SubobjectDesignator &getLValueDesignator() { |
| 303 | assert(getKind() == LValue); |
| 304 | return Designator; |
| 305 | } |
| 306 | const SubobjectDesignator &getLValueDesignator() const { |
| 307 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 308 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 309 | APValue toAPValue() const { |
| 310 | if (!isLValue()) |
| 311 | return *this; |
| 312 | |
| 313 | if (Designator.Invalid) { |
| 314 | // This is not a core constant expression. An appropriate diagnostic |
| 315 | // will have already been produced. |
| 316 | return APValue(getLValueBase(), getLValueOffset(), |
| 317 | APValue::NoLValuePath(), getLValueCallIndex()); |
| 318 | } |
| 319 | |
| 320 | return APValue(getLValueBase(), getLValueOffset(), |
| 321 | Designator.Entries, Designator.IsOnePastTheEnd, |
| 322 | getLValueCallIndex()); |
| 323 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 326 | /// A stack frame in the constexpr call stack. |
| 327 | struct CallStackFrame { |
| 328 | EvalInfo &Info; |
| 329 | |
| 330 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 331 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 332 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 333 | /// CallLoc - The location of the call expression for this call. |
| 334 | SourceLocation CallLoc; |
| 335 | |
| 336 | /// Callee - The function which was called. |
| 337 | const FunctionDecl *Callee; |
| 338 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 339 | /// Index - The call index of this call. |
| 340 | unsigned Index; |
| 341 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 342 | /// This - The binding for the this pointer in this call, if any. |
| 343 | const LValue *This; |
| 344 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 345 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 346 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 347 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 348 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 349 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 350 | typedef MapTy::const_iterator temp_iterator; |
| 351 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 352 | MapTy Temporaries; |
| 353 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 354 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 355 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 356 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 357 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 360 | /// A partial diagnostic which we might know in advance that we are not going |
| 361 | /// to emit. |
| 362 | class OptionalDiagnostic { |
| 363 | PartialDiagnostic *Diag; |
| 364 | |
| 365 | public: |
| 366 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 367 | |
| 368 | template<typename T> |
| 369 | OptionalDiagnostic &operator<<(const T &v) { |
| 370 | if (Diag) |
| 371 | *Diag << v; |
| 372 | return *this; |
| 373 | } |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 374 | |
| 375 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 376 | if (Diag) { |
| 377 | llvm::SmallVector<char, 32> Buffer; |
| 378 | I.toString(Buffer); |
| 379 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 380 | } |
| 381 | return *this; |
| 382 | } |
| 383 | |
| 384 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 385 | if (Diag) { |
| 386 | llvm::SmallVector<char, 32> Buffer; |
| 387 | F.toString(Buffer); |
| 388 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 389 | } |
| 390 | return *this; |
| 391 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 394 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 395 | /// information about a subexpression as it is folded. It retains information |
| 396 | /// about the AST context, but also maintains information about the folded |
| 397 | /// expression. |
| 398 | /// |
| 399 | /// If an expression could be evaluated, it is still possible it is not a C |
| 400 | /// "integer constant expression" or constant expression. If not, this struct |
| 401 | /// captures information about how and why not. |
| 402 | /// |
| 403 | /// One bit of information passed *into* the request for constant folding |
| 404 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 405 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 406 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 407 | /// certain things in certain situations. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 408 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 409 | ASTContext &Ctx; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 410 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 411 | /// EvalStatus - Contains information about the evaluation. |
| 412 | Expr::EvalStatus &EvalStatus; |
| 413 | |
| 414 | /// CurrentCall - The top of the constexpr call stack. |
| 415 | CallStackFrame *CurrentCall; |
| 416 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 417 | /// CallStackDepth - The number of calls in the call stack right now. |
| 418 | unsigned CallStackDepth; |
| 419 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 420 | /// NextCallIndex - The next call index to assign. |
| 421 | unsigned NextCallIndex; |
| 422 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 423 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 424 | /// OpaqueValues - Values used as the common expression in a |
| 425 | /// BinaryConditionalOperator. |
| 426 | MapTy OpaqueValues; |
| 427 | |
| 428 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 429 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 430 | CallStackFrame BottomFrame; |
| 431 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 432 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 433 | /// evaluated, if any. |
| 434 | const VarDecl *EvaluatingDecl; |
| 435 | |
| 436 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 437 | /// declaration whose initializer is being evaluated, if any. |
| 438 | APValue *EvaluatingDeclValue; |
| 439 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 440 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 441 | /// notes attached to it will also be stored, otherwise they will not be. |
| 442 | bool HasActiveDiagnostic; |
| 443 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 444 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 445 | /// expression is a potential constant expression? If so, some diagnostics |
| 446 | /// are suppressed. |
| 447 | bool CheckingPotentialConstantExpression; |
| 448 | |
Argyrios Kyrtzidis | c1b66e6 | 2012-02-27 23:18:37 +0000 | [diff] [blame] | 449 | /// \brief Stack depth of IntExprEvaluator. |
| 450 | /// We check this against a maximum value to avoid stack overflow, see |
| 451 | /// test case in test/Sema/many-logical-ops.c. |
| 452 | // FIXME: This is a hack; handle properly unlimited logical ops. |
| 453 | unsigned IntExprEvaluatorDepth; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 454 | |
| 455 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 456 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 457 | CallStackDepth(0), NextCallIndex(1), |
| 458 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 459 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), |
Argyrios Kyrtzidis | c1b66e6 | 2012-02-27 23:18:37 +0000 | [diff] [blame] | 460 | CheckingPotentialConstantExpression(false), IntExprEvaluatorDepth(0) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 461 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 462 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 463 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 464 | if (i == OpaqueValues.end()) return 0; |
| 465 | return &i->second; |
| 466 | } |
| 467 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 468 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 469 | EvaluatingDecl = VD; |
| 470 | EvaluatingDeclValue = &Value; |
| 471 | } |
| 472 | |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 473 | const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } |
| 474 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 475 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 476 | // Don't perform any constexpr calls (other than the call we're checking) |
| 477 | // when checking a potential constant expression. |
| 478 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 479 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 480 | if (NextCallIndex == 0) { |
| 481 | // NextCallIndex has wrapped around. |
| 482 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 483 | return false; |
| 484 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 485 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 486 | return true; |
| 487 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 488 | << getLangOpts().ConstexprCallDepth; |
| 489 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 490 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 491 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 492 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 493 | assert(CallIndex && "no call index in getCallFrame"); |
| 494 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 495 | // be null in this loop. |
| 496 | CallStackFrame *Frame = CurrentCall; |
| 497 | while (Frame->Index > CallIndex) |
| 498 | Frame = Frame->Caller; |
| 499 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 500 | } |
| 501 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 502 | private: |
| 503 | /// Add a diagnostic to the diagnostics list. |
| 504 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 505 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 506 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 507 | return EvalStatus.Diag->back().second; |
| 508 | } |
| 509 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 510 | /// Add notes containing a call stack to the current point of evaluation. |
| 511 | void addCallStack(unsigned Limit); |
| 512 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 513 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 514 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 515 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 516 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 517 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 518 | // If we have a prior diagnostic, it will be noting that the expression |
| 519 | // isn't a constant expression. This diagnostic is more important. |
| 520 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 521 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 522 | unsigned CallStackNotes = CallStackDepth - 1; |
| 523 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 524 | if (Limit) |
| 525 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 526 | if (CheckingPotentialConstantExpression) |
| 527 | CallStackNotes = 0; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 528 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 529 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 530 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 531 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 532 | addDiag(Loc, DiagId); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 533 | if (!CheckingPotentialConstantExpression) |
| 534 | addCallStack(Limit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 535 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 536 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 537 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 538 | return OptionalDiagnostic(); |
| 539 | } |
| 540 | |
| 541 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 542 | /// expression. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 543 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
| 544 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 545 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 546 | // Don't override a previous diagnostic. |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 547 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
| 548 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 549 | return OptionalDiagnostic(); |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 550 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 551 | return Diag(Loc, DiagId, ExtraNotes); |
| 552 | } |
| 553 | |
| 554 | /// Add a note to a prior diagnostic. |
| 555 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 556 | if (!HasActiveDiagnostic) |
| 557 | return OptionalDiagnostic(); |
| 558 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 559 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 560 | |
| 561 | /// Add a stack of notes to a prior diagnostic. |
| 562 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 563 | if (HasActiveDiagnostic) { |
| 564 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 565 | Diags.begin(), Diags.end()); |
| 566 | } |
| 567 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 568 | |
| 569 | /// Should we continue evaluation as much as possible after encountering a |
| 570 | /// construct which can't be folded? |
| 571 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 572 | return CheckingPotentialConstantExpression && |
| 573 | EvalStatus.Diag && EvalStatus.Diag->empty(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 574 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 575 | }; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 576 | |
| 577 | /// Object used to treat all foldable expressions as constant expressions. |
| 578 | struct FoldConstant { |
| 579 | bool Enabled; |
| 580 | |
| 581 | explicit FoldConstant(EvalInfo &Info) |
| 582 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 583 | !Info.EvalStatus.HasSideEffects) { |
| 584 | } |
| 585 | // Treat the value we've computed since this object was created as constant. |
| 586 | void Fold(EvalInfo &Info) { |
| 587 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 588 | !Info.EvalStatus.HasSideEffects) |
| 589 | Info.EvalStatus.Diag->clear(); |
| 590 | } |
| 591 | }; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 592 | |
| 593 | /// RAII object used to suppress diagnostics and side-effects from a |
| 594 | /// speculative evaluation. |
| 595 | class SpeculativeEvaluationRAII { |
| 596 | EvalInfo &Info; |
| 597 | Expr::EvalStatus Old; |
| 598 | |
| 599 | public: |
| 600 | SpeculativeEvaluationRAII(EvalInfo &Info, |
| 601 | llvm::SmallVectorImpl<PartialDiagnosticAt> |
| 602 | *NewDiag = 0) |
| 603 | : Info(Info), Old(Info.EvalStatus) { |
| 604 | Info.EvalStatus.Diag = NewDiag; |
| 605 | } |
| 606 | ~SpeculativeEvaluationRAII() { |
| 607 | Info.EvalStatus = Old; |
| 608 | } |
| 609 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 610 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 611 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 612 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 613 | CheckSubobjectKind CSK) { |
| 614 | if (Invalid) |
| 615 | return false; |
| 616 | if (isOnePastTheEnd()) { |
| 617 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_past_end_subobject) |
| 618 | << CSK; |
| 619 | setInvalid(); |
| 620 | return false; |
| 621 | } |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 626 | const Expr *E, uint64_t N) { |
| 627 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
| 628 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 629 | << static_cast<int>(N) << /*array*/ 0 |
| 630 | << static_cast<unsigned>(MostDerivedArraySize); |
| 631 | else |
| 632 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 633 | << static_cast<int>(N) << /*non-array*/ 1; |
| 634 | setInvalid(); |
| 635 | } |
| 636 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 637 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 638 | const FunctionDecl *Callee, const LValue *This, |
| 639 | const CCValue *Arguments) |
| 640 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 641 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 642 | Info.CurrentCall = this; |
| 643 | ++Info.CallStackDepth; |
| 644 | } |
| 645 | |
| 646 | CallStackFrame::~CallStackFrame() { |
| 647 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 648 | --Info.CallStackDepth; |
| 649 | Info.CurrentCall = Caller; |
| 650 | } |
| 651 | |
| 652 | /// Produce a string describing the given constexpr call. |
| 653 | static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { |
| 654 | unsigned ArgIndex = 0; |
| 655 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 656 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 657 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 658 | |
| 659 | if (!IsMemberCall) |
| 660 | Out << *Frame->Callee << '('; |
| 661 | |
| 662 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 663 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
NAKAMURA Takumi | 5fe3122 | 2012-01-26 09:37:36 +0000 | [diff] [blame] | 664 | if (ArgIndex > (unsigned)IsMemberCall) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 665 | Out << ", "; |
| 666 | |
| 667 | const ParmVarDecl *Param = *I; |
| 668 | const CCValue &Arg = Frame->Arguments[ArgIndex]; |
| 669 | if (!Arg.isLValue() || Arg.getLValueDesignator().Invalid) |
| 670 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 671 | else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 672 | // Convert the CCValue to an APValue without checking for constantness. |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 673 | APValue Value(Arg.getLValueBase(), Arg.getLValueOffset(), |
| 674 | Arg.getLValueDesignator().Entries, |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 675 | Arg.getLValueDesignator().IsOnePastTheEnd, |
| 676 | Arg.getLValueCallIndex()); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 677 | Value.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 678 | } |
| 679 | |
| 680 | if (ArgIndex == 0 && IsMemberCall) |
| 681 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 684 | Out << ')'; |
| 685 | } |
| 686 | |
| 687 | void EvalInfo::addCallStack(unsigned Limit) { |
| 688 | // Determine which calls to skip, if any. |
| 689 | unsigned ActiveCalls = CallStackDepth - 1; |
| 690 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 691 | if (Limit && Limit < ActiveCalls) { |
| 692 | SkipStart = Limit / 2 + Limit % 2; |
| 693 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 696 | // Walk the call stack and add the diagnostics. |
| 697 | unsigned CallIdx = 0; |
| 698 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 699 | Frame = Frame->Caller, ++CallIdx) { |
| 700 | // Skip this call? |
| 701 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 702 | if (CallIdx == SkipStart) { |
| 703 | // Note that we're skipping calls. |
| 704 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 705 | << unsigned(ActiveCalls - Limit); |
| 706 | } |
| 707 | continue; |
| 708 | } |
| 709 | |
| 710 | llvm::SmallVector<char, 128> Buffer; |
| 711 | llvm::raw_svector_ostream Out(Buffer); |
| 712 | describeCall(Frame, Out); |
| 713 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 718 | struct ComplexValue { |
| 719 | private: |
| 720 | bool IsInt; |
| 721 | |
| 722 | public: |
| 723 | APSInt IntReal, IntImag; |
| 724 | APFloat FloatReal, FloatImag; |
| 725 | |
| 726 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 727 | |
| 728 | void makeComplexFloat() { IsInt = false; } |
| 729 | bool isComplexFloat() const { return !IsInt; } |
| 730 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 731 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 732 | |
| 733 | void makeComplexInt() { IsInt = true; } |
| 734 | bool isComplexInt() const { return IsInt; } |
| 735 | APSInt &getComplexIntReal() { return IntReal; } |
| 736 | APSInt &getComplexIntImag() { return IntImag; } |
| 737 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 738 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 739 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 740 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 741 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 742 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 743 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 744 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 745 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 746 | if (v.isComplexFloat()) { |
| 747 | makeComplexFloat(); |
| 748 | FloatReal = v.getComplexFloatReal(); |
| 749 | FloatImag = v.getComplexFloatImag(); |
| 750 | } else { |
| 751 | makeComplexInt(); |
| 752 | IntReal = v.getComplexIntReal(); |
| 753 | IntImag = v.getComplexIntImag(); |
| 754 | } |
| 755 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 756 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 757 | |
| 758 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 759 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 760 | CharUnits Offset; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 761 | unsigned CallIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 762 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 763 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 764 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 765 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 766 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 767 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 768 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 769 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 770 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 771 | void moveInto(CCValue &V) const { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 772 | V = CCValue(Base, Offset, CallIndex, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 773 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 774 | void setFrom(const CCValue &V) { |
| 775 | assert(V.isLValue()); |
| 776 | Base = V.getLValueBase(); |
| 777 | Offset = V.getLValueOffset(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 778 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 779 | Designator = V.getLValueDesignator(); |
| 780 | } |
| 781 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 782 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 783 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 784 | Offset = CharUnits::Zero(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 785 | CallIndex = I; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 786 | Designator = SubobjectDesignator(getType(B)); |
| 787 | } |
| 788 | |
| 789 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 790 | // a diagnostic and mark the designator as invalid. |
| 791 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 792 | CheckSubobjectKind CSK) { |
| 793 | if (Designator.Invalid) |
| 794 | return false; |
| 795 | if (!Base) { |
| 796 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_null_subobject) |
| 797 | << CSK; |
| 798 | Designator.setInvalid(); |
| 799 | return false; |
| 800 | } |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | // Check this LValue refers to an object. If not, set the designator to be |
| 805 | // invalid and emit a diagnostic. |
| 806 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
| 807 | return checkNullPointer(Info, E, CSK) && |
| 808 | Designator.checkSubobject(Info, E, CSK); |
| 809 | } |
| 810 | |
| 811 | void addDecl(EvalInfo &Info, const Expr *E, |
| 812 | const Decl *D, bool Virtual = false) { |
| 813 | checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base); |
| 814 | Designator.addDeclUnchecked(D, Virtual); |
| 815 | } |
| 816 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
| 817 | checkSubobject(Info, E, CSK_ArrayToPointer); |
| 818 | Designator.addArrayUnchecked(CAT); |
| 819 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 820 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
| 821 | checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real); |
| 822 | Designator.addComplexUnchecked(EltTy, Imag); |
| 823 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 824 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
| 825 | if (!checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 826 | return; |
| 827 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 828 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 829 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 830 | |
| 831 | struct MemberPtr { |
| 832 | MemberPtr() {} |
| 833 | explicit MemberPtr(const ValueDecl *Decl) : |
| 834 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 835 | |
| 836 | /// The member or (direct or indirect) field referred to by this member |
| 837 | /// pointer, or 0 if this is a null member pointer. |
| 838 | const ValueDecl *getDecl() const { |
| 839 | return DeclAndIsDerivedMember.getPointer(); |
| 840 | } |
| 841 | /// Is this actually a member of some type derived from the relevant class? |
| 842 | bool isDerivedMember() const { |
| 843 | return DeclAndIsDerivedMember.getInt(); |
| 844 | } |
| 845 | /// Get the class which the declaration actually lives in. |
| 846 | const CXXRecordDecl *getContainingRecord() const { |
| 847 | return cast<CXXRecordDecl>( |
| 848 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 849 | } |
| 850 | |
| 851 | void moveInto(CCValue &V) const { |
| 852 | V = CCValue(getDecl(), isDerivedMember(), Path); |
| 853 | } |
| 854 | void setFrom(const CCValue &V) { |
| 855 | assert(V.isMemberPointer()); |
| 856 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 857 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 858 | Path.clear(); |
| 859 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 860 | Path.insert(Path.end(), P.begin(), P.end()); |
| 861 | } |
| 862 | |
| 863 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 864 | /// whether the member is a member of some class derived from the class type |
| 865 | /// of the member pointer. |
| 866 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 867 | /// Path - The path of base/derived classes from the member declaration's |
| 868 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 869 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 870 | |
| 871 | /// Perform a cast towards the class of the Decl (either up or down the |
| 872 | /// hierarchy). |
| 873 | bool castBack(const CXXRecordDecl *Class) { |
| 874 | assert(!Path.empty()); |
| 875 | const CXXRecordDecl *Expected; |
| 876 | if (Path.size() >= 2) |
| 877 | Expected = Path[Path.size() - 2]; |
| 878 | else |
| 879 | Expected = getContainingRecord(); |
| 880 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 881 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 882 | // if B does not contain the original member and is not a base or |
| 883 | // derived class of the class containing the original member, the result |
| 884 | // of the cast is undefined. |
| 885 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 886 | // (D::*). We consider that to be a language defect. |
| 887 | return false; |
| 888 | } |
| 889 | Path.pop_back(); |
| 890 | return true; |
| 891 | } |
| 892 | /// Perform a base-to-derived member pointer cast. |
| 893 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 894 | if (!getDecl()) |
| 895 | return true; |
| 896 | if (!isDerivedMember()) { |
| 897 | Path.push_back(Derived); |
| 898 | return true; |
| 899 | } |
| 900 | if (!castBack(Derived)) |
| 901 | return false; |
| 902 | if (Path.empty()) |
| 903 | DeclAndIsDerivedMember.setInt(false); |
| 904 | return true; |
| 905 | } |
| 906 | /// Perform a derived-to-base member pointer cast. |
| 907 | bool castToBase(const CXXRecordDecl *Base) { |
| 908 | if (!getDecl()) |
| 909 | return true; |
| 910 | if (Path.empty()) |
| 911 | DeclAndIsDerivedMember.setInt(true); |
| 912 | if (isDerivedMember()) { |
| 913 | Path.push_back(Base); |
| 914 | return true; |
| 915 | } |
| 916 | return castBack(Base); |
| 917 | } |
| 918 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 919 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 920 | /// Compare two member pointers, which are assumed to be of the same type. |
| 921 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 922 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 923 | return !LHS.getDecl() && !RHS.getDecl(); |
| 924 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 925 | return false; |
| 926 | return LHS.Path == RHS.Path; |
| 927 | } |
| 928 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 929 | /// Kinds of constant expression checking, for diagnostics. |
| 930 | enum CheckConstantExpressionKind { |
| 931 | CCEK_Constant, ///< A normal constant. |
| 932 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 933 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 934 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 935 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 936 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 937 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 938 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 939 | const LValue &This, const Expr *E, |
| 940 | CheckConstantExpressionKind CCEK = CCEK_Constant, |
| 941 | bool AllowNonLiteralTypes = false); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 942 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 943 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 944 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 945 | EvalInfo &Info); |
| 946 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 947 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 948 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 949 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 950 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 951 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 952 | |
| 953 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 954 | // Misc utilities |
| 955 | //===----------------------------------------------------------------------===// |
| 956 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 957 | /// Should this call expression be treated as a string literal? |
| 958 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 959 | unsigned Builtin = E->isBuiltinCall(); |
| 960 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 961 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 962 | } |
| 963 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 964 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 965 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 966 | // constant expression of pointer type that evaluates to... |
| 967 | |
| 968 | // ... a null pointer value, or a prvalue core constant expression of type |
| 969 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 970 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 971 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 972 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 973 | // ... the address of an object with static storage duration, |
| 974 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 975 | return VD->hasGlobalStorage(); |
| 976 | // ... the address of a function, |
| 977 | return isa<FunctionDecl>(D); |
| 978 | } |
| 979 | |
| 980 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 981 | switch (E->getStmtClass()) { |
| 982 | default: |
| 983 | return false; |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 984 | case Expr::CompoundLiteralExprClass: { |
| 985 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 986 | return CLE->isFileScope() && CLE->isLValue(); |
| 987 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 988 | // A string literal has static storage duration. |
| 989 | case Expr::StringLiteralClass: |
| 990 | case Expr::PredefinedExprClass: |
| 991 | case Expr::ObjCStringLiteralClass: |
| 992 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 993 | case Expr::CXXTypeidExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 994 | return true; |
| 995 | case Expr::CallExprClass: |
| 996 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 997 | // For GCC compatibility, &&label has static storage duration. |
| 998 | case Expr::AddrLabelExprClass: |
| 999 | return true; |
| 1000 | // A Block literal expression may be used as the initialization value for |
| 1001 | // Block variables at global or local static scope. |
| 1002 | case Expr::BlockExprClass: |
| 1003 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1004 | case Expr::ImplicitValueInitExprClass: |
| 1005 | // FIXME: |
| 1006 | // We can never form an lvalue with an implicit value initialization as its |
| 1007 | // base through expression evaluation, so these only appear in one case: the |
| 1008 | // implicit variable declaration we invent when checking whether a constexpr |
| 1009 | // constructor can produce a constant expression. We must assume that such |
| 1010 | // an expression might be a global lvalue. |
| 1011 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1012 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1015 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1016 | assert(Base && "no location for a null lvalue"); |
| 1017 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1018 | if (VD) |
| 1019 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1020 | else |
| 1021 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 1022 | diag::note_constexpr_temporary_here); |
| 1023 | } |
| 1024 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1025 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1026 | /// value for an address or reference constant expression. Type T should be |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1027 | /// either LValue or CCValue. Return true if we can fold this expression, |
| 1028 | /// whether or not it's a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1029 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1030 | QualType Type, const LValue &LVal) { |
| 1031 | bool IsReferenceType = Type->isReferenceType(); |
| 1032 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1033 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1034 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1035 | |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1036 | // Check that the object is a global. Note that the fake 'this' object we |
| 1037 | // manufacture when checking potential constant expressions is conservatively |
| 1038 | // assumed to be global here. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1039 | if (!IsGlobalLValue(Base)) { |
| 1040 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1041 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1042 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1043 | << IsReferenceType << !Designator.Entries.empty() |
| 1044 | << !!VD << VD; |
| 1045 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1046 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1047 | Info.Diag(Loc); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1048 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1049 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1050 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1051 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1052 | assert((Info.CheckingPotentialConstantExpression || |
| 1053 | LVal.getLValueCallIndex() == 0) && |
| 1054 | "have call index for global lvalue"); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1057 | // an extension: the standard requires them to point to an object. |
| 1058 | if (!IsReferenceType) |
| 1059 | return true; |
| 1060 | |
| 1061 | // A reference constant expression must refer to an object. |
| 1062 | if (!Base) { |
| 1063 | // FIXME: diagnostic |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1064 | Info.CCEDiag(Loc); |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1065 | return true; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1068 | // Does this refer one past the end of some object? |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1069 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1070 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1071 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1072 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1073 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1079 | /// Check that this core constant expression is of literal type, and if not, |
| 1080 | /// produce an appropriate diagnostic. |
| 1081 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E) { |
| 1082 | if (!E->isRValue() || E->getType()->isLiteralType()) |
| 1083 | return true; |
| 1084 | |
| 1085 | // Prvalue constant expressions must be of literal types. |
| 1086 | if (Info.getLangOpts().CPlusPlus0x) |
| 1087 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 1088 | << E->getType(); |
| 1089 | else |
| 1090 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1094 | /// 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] | 1095 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1096 | /// check that the expression is of literal type. |
| 1097 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1098 | QualType Type, const APValue &Value) { |
| 1099 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1100 | // each subobject of its value shall have been initialized by a constant |
| 1101 | // expression. |
| 1102 | if (Value.isArray()) { |
| 1103 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1104 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1105 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1106 | Value.getArrayInitializedElt(I))) |
| 1107 | return false; |
| 1108 | } |
| 1109 | if (!Value.hasArrayFiller()) |
| 1110 | return true; |
| 1111 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1112 | Value.getArrayFiller()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1113 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1114 | if (Value.isUnion() && Value.getUnionField()) { |
| 1115 | return CheckConstantExpression(Info, DiagLoc, |
| 1116 | Value.getUnionField()->getType(), |
| 1117 | Value.getUnionValue()); |
| 1118 | } |
| 1119 | if (Value.isStruct()) { |
| 1120 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1121 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1122 | unsigned BaseIndex = 0; |
| 1123 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1124 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1125 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1126 | Value.getStructBase(BaseIndex))) |
| 1127 | return false; |
| 1128 | } |
| 1129 | } |
| 1130 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1131 | I != E; ++I) { |
| 1132 | if (!CheckConstantExpression(Info, DiagLoc, (*I)->getType(), |
| 1133 | Value.getStructField((*I)->getFieldIndex()))) |
| 1134 | return false; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | if (Value.isLValue()) { |
| 1139 | CCValue Val(Info.Ctx, Value, CCValue::GlobalValue()); |
| 1140 | LValue LVal; |
| 1141 | LVal.setFrom(Val); |
| 1142 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1143 | } |
| 1144 | |
| 1145 | // Everything else is fine. |
| 1146 | return true; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1149 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1150 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1154 | return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1157 | static bool IsWeakLValue(const LValue &Value) { |
| 1158 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1159 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1162 | static bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1163 | // A null base expression indicates a null pointer. These are always |
| 1164 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1165 | if (!Value.getLValueBase()) { |
| 1166 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1167 | return true; |
| 1168 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1169 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1170 | // We have a non-null base. These are generally known to be true, but if it's |
| 1171 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1172 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1173 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1174 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1177 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1178 | switch (Val.getKind()) { |
| 1179 | case APValue::Uninitialized: |
| 1180 | return false; |
| 1181 | case APValue::Int: |
| 1182 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1183 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1184 | case APValue::Float: |
| 1185 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1186 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1187 | case APValue::ComplexInt: |
| 1188 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1189 | Val.getComplexIntImag().getBoolValue(); |
| 1190 | return true; |
| 1191 | case APValue::ComplexFloat: |
| 1192 | Result = !Val.getComplexFloatReal().isZero() || |
| 1193 | !Val.getComplexFloatImag().isZero(); |
| 1194 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1195 | case APValue::LValue: |
| 1196 | return EvalPointerValueAsBool(Val, Result); |
| 1197 | case APValue::MemberPointer: |
| 1198 | Result = Val.getMemberPointerDecl(); |
| 1199 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1200 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1201 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1202 | case APValue::Struct: |
| 1203 | case APValue::Union: |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1204 | case APValue::AddrLabelDiff: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1205 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1208 | llvm_unreachable("unknown APValue kind"); |
| 1209 | } |
| 1210 | |
| 1211 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1212 | EvalInfo &Info) { |
| 1213 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1214 | CCValue Val; |
| 1215 | if (!Evaluate(Val, Info, E)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1216 | return false; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1217 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1220 | template<typename T> |
| 1221 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| 1222 | const T &SrcValue, QualType DestType) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1223 | Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1224 | << SrcValue << DestType; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1229 | QualType SrcType, const APFloat &Value, |
| 1230 | QualType DestType, APSInt &Result) { |
| 1231 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1232 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1233 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1235 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1236 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1237 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1238 | & APFloat::opInvalidOp) |
| 1239 | return HandleOverflow(Info, E, Value, DestType); |
| 1240 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1243 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1244 | QualType SrcType, QualType DestType, |
| 1245 | APFloat &Result) { |
| 1246 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1247 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1248 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1249 | APFloat::rmNearestTiesToEven, &ignored) |
| 1250 | & APFloat::opOverflow) |
| 1251 | return HandleOverflow(Info, E, Value, DestType); |
| 1252 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1255 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1256 | QualType DestType, QualType SrcType, |
| 1257 | APSInt &Value) { |
| 1258 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1259 | APSInt Result = Value; |
| 1260 | // Figure out if this is a truncate, extend or noop cast. |
| 1261 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1262 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1263 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1264 | return Result; |
| 1265 | } |
| 1266 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1267 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1268 | QualType SrcType, const APSInt &Value, |
| 1269 | QualType DestType, APFloat &Result) { |
| 1270 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1271 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1272 | APFloat::rmNearestTiesToEven) |
| 1273 | & APFloat::opOverflow) |
| 1274 | return HandleOverflow(Info, E, Value, DestType); |
| 1275 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1278 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1279 | llvm::APInt &Res) { |
| 1280 | CCValue SVal; |
| 1281 | if (!Evaluate(SVal, Info, E)) |
| 1282 | return false; |
| 1283 | if (SVal.isInt()) { |
| 1284 | Res = SVal.getInt(); |
| 1285 | return true; |
| 1286 | } |
| 1287 | if (SVal.isFloat()) { |
| 1288 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1289 | return true; |
| 1290 | } |
| 1291 | if (SVal.isVector()) { |
| 1292 | QualType VecTy = E->getType(); |
| 1293 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1294 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1295 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1296 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1297 | Res = llvm::APInt::getNullValue(VecSize); |
| 1298 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1299 | APValue &Elt = SVal.getVectorElt(i); |
| 1300 | llvm::APInt EltAsInt; |
| 1301 | if (Elt.isInt()) { |
| 1302 | EltAsInt = Elt.getInt(); |
| 1303 | } else if (Elt.isFloat()) { |
| 1304 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1305 | } else { |
| 1306 | // Don't try to handle vectors of anything other than int or float |
| 1307 | // (not sure if it's possible to hit this case). |
| 1308 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1309 | return false; |
| 1310 | } |
| 1311 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1312 | if (BigEndian) |
| 1313 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1314 | else |
| 1315 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1316 | } |
| 1317 | return true; |
| 1318 | } |
| 1319 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1320 | // reject "(v4i16)(intptr_t)&a". |
| 1321 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1325 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1326 | /// truncating the lvalue's path to the given length. |
| 1327 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1328 | const RecordDecl *TruncatedType, |
| 1329 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1330 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Check we actually point to a derived class object. |
| 1333 | if (TruncatedElements == D.Entries.size()) |
| 1334 | return true; |
| 1335 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1336 | "not casting to a derived class"); |
| 1337 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1338 | return false; |
| 1339 | |
| 1340 | // 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] | 1341 | const RecordDecl *RD = TruncatedType; |
| 1342 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1343 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1344 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1345 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1346 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1347 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1348 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1349 | RD = Base; |
| 1350 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1351 | D.Entries.resize(TruncatedElements); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1352 | return true; |
| 1353 | } |
| 1354 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1355 | static void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1356 | const CXXRecordDecl *Derived, |
| 1357 | const CXXRecordDecl *Base, |
| 1358 | const ASTRecordLayout *RL = 0) { |
| 1359 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1360 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1361 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1364 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1365 | const CXXRecordDecl *DerivedDecl, |
| 1366 | const CXXBaseSpecifier *Base) { |
| 1367 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1368 | |
| 1369 | if (!Base->isVirtual()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1370 | HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1374 | SubobjectDesignator &D = Obj.Designator; |
| 1375 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1376 | return false; |
| 1377 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1378 | // Extract most-derived object and corresponding type. |
| 1379 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1380 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1381 | return false; |
| 1382 | |
| 1383 | // Find the virtual base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1384 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1385 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1386 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
| 1389 | |
| 1390 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1391 | /// currently described by LVal. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1392 | static void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1393 | const FieldDecl *FD, |
| 1394 | const ASTRecordLayout *RL = 0) { |
| 1395 | if (!RL) |
| 1396 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 1397 | |
| 1398 | unsigned I = FD->getFieldIndex(); |
| 1399 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1400 | LVal.addDecl(Info, E, FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1403 | /// Update LVal to refer to the given indirect field. |
| 1404 | static void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
| 1405 | LValue &LVal, |
| 1406 | const IndirectFieldDecl *IFD) { |
| 1407 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1408 | CE = IFD->chain_end(); C != CE; ++C) |
| 1409 | HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)); |
| 1410 | } |
| 1411 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1412 | /// Get the size of the given type in char units. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1413 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1414 | QualType Type, CharUnits &Size) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1415 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1416 | // extension. |
| 1417 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1418 | Size = CharUnits::One(); |
| 1419 | return true; |
| 1420 | } |
| 1421 | |
| 1422 | if (!Type->isConstantSizeType()) { |
| 1423 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1424 | // FIXME: Better diagnostic. |
| 1425 | Info.Diag(Loc); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
| 1433 | /// Update a pointer value to model pointer arithmetic. |
| 1434 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1435 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1436 | /// \param LVal - The pointer value to be updated. |
| 1437 | /// \param EltTy - The pointee type represented by LVal. |
| 1438 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1439 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1440 | LValue &LVal, QualType EltTy, |
| 1441 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1442 | CharUnits SizeOfPointee; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1443 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | |
| 1446 | // Compute the new offset in the appropriate width. |
| 1447 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1448 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1449 | return true; |
| 1450 | } |
| 1451 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1452 | /// Update an lvalue to refer to a component of a complex number. |
| 1453 | /// \param Info - Information about the ongoing evaluation. |
| 1454 | /// \param LVal - The lvalue to be updated. |
| 1455 | /// \param EltTy - The complex number's component type. |
| 1456 | /// \param Imag - False for the real component, true for the imaginary. |
| 1457 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1458 | LValue &LVal, QualType EltTy, |
| 1459 | bool Imag) { |
| 1460 | if (Imag) { |
| 1461 | CharUnits SizeOfComponent; |
| 1462 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1463 | return false; |
| 1464 | LVal.Offset += SizeOfComponent; |
| 1465 | } |
| 1466 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1470 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1471 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1472 | const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1473 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1474 | // If this is a parameter to an active constexpr function call, perform |
| 1475 | // argument substitution. |
| 1476 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1477 | // Assume arguments of a potential constant expression are unknown |
| 1478 | // constant expressions. |
| 1479 | if (Info.CheckingPotentialConstantExpression) |
| 1480 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1481 | if (!Frame || !Frame->Arguments) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1482 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1483 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1484 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1485 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 1486 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1487 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1488 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1489 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1490 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1491 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1492 | // If we're checking a potential constant expression, the variable could be |
| 1493 | // initialized later. |
| 1494 | if (!Info.CheckingPotentialConstantExpression) |
| 1495 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| 1498 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1499 | // If we're currently evaluating the initializer of this declaration, use that |
| 1500 | // in-flight value. |
| 1501 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1502 | Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue, |
| 1503 | CCValue::GlobalValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1504 | return !Result.isUninit(); |
| 1505 | } |
| 1506 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1507 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1508 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1509 | if (VD->isWeak()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1510 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1511 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1512 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1513 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1514 | // Check that we can fold the initializer. In C++, we will have already done |
| 1515 | // this in the cases where it matters for conformance. |
| 1516 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1517 | if (!VD->evaluateValue(Notes)) { |
| 1518 | Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1519 | Notes.size() + 1) << VD; |
| 1520 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1521 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1522 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1523 | } else if (!VD->checkInitIsICE()) { |
| 1524 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1525 | Notes.size() + 1) << VD; |
| 1526 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1527 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1528 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1529 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1530 | Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1531 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1534 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1535 | Qualifiers Quals = T.getQualifiers(); |
| 1536 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1537 | } |
| 1538 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1539 | /// Get the base index of the given base class within an APValue representing |
| 1540 | /// the given derived class. |
| 1541 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1542 | const CXXRecordDecl *Base) { |
| 1543 | Base = Base->getCanonicalDecl(); |
| 1544 | unsigned Index = 0; |
| 1545 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1546 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1547 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1548 | return Index; |
| 1549 | } |
| 1550 | |
| 1551 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1552 | } |
| 1553 | |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1554 | /// Extract the value of a character from a string literal. |
| 1555 | static APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 1556 | uint64_t Index) { |
| 1557 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1558 | const StringLiteral *S = dyn_cast<StringLiteral>(Lit); |
| 1559 | assert(S && "unexpected string literal expression kind"); |
| 1560 | |
| 1561 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1562 | Lit->getType()->getArrayElementTypeNoTypeQual()->isUnsignedIntegerType()); |
| 1563 | if (Index < S->getLength()) |
| 1564 | Value = S->getCodeUnit(Index); |
| 1565 | return Value; |
| 1566 | } |
| 1567 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1568 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1569 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
| 1570 | CCValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1571 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1572 | if (Sub.Invalid) |
| 1573 | // A diagnostic will have already been produced. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1574 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1575 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1576 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1577 | (unsigned)diag::note_constexpr_read_past_end : |
| 1578 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1581 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1582 | return true; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1583 | if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) |
| 1584 | // This object might be initialized later. |
| 1585 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1586 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1587 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1588 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1589 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1590 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1591 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1592 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1593 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1594 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1595 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1596 | // Note, it should not be possible to form a pointer with a valid |
| 1597 | // designator which points more than one past the end of the array. |
| 1598 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1599 | (unsigned)diag::note_constexpr_read_past_end : |
| 1600 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1601 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1602 | } |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1603 | // An array object is represented as either an Array APValue or as an |
| 1604 | // LValue which refers to a string literal. |
| 1605 | if (O->isLValue()) { |
| 1606 | assert(I == N - 1 && "extracting subobject of character?"); |
| 1607 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
| 1608 | Obj = CCValue(ExtractStringLiteralCharacter( |
| 1609 | Info, O->getLValueBase().get<const Expr*>(), Index)); |
| 1610 | return true; |
| 1611 | } else if (O->getArrayInitializedElts() > Index) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1612 | O = &O->getArrayInitializedElt(Index); |
| 1613 | else |
| 1614 | O = &O->getArrayFiller(); |
| 1615 | ObjType = CAT->getElementType(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1616 | } else if (ObjType->isAnyComplexType()) { |
| 1617 | // Next subobject is a complex number. |
| 1618 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 1619 | if (Index > 1) { |
| 1620 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
| 1621 | (unsigned)diag::note_constexpr_read_past_end : |
| 1622 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
| 1623 | return false; |
| 1624 | } |
| 1625 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 1626 | if (O->isComplexInt()) { |
| 1627 | Obj = CCValue(Index ? O->getComplexIntImag() |
| 1628 | : O->getComplexIntReal()); |
| 1629 | } else { |
| 1630 | assert(O->isComplexFloat()); |
| 1631 | Obj = CCValue(Index ? O->getComplexFloatImag() |
| 1632 | : O->getComplexFloatReal()); |
| 1633 | } |
| 1634 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1635 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1636 | if (Field->isMutable()) { |
| 1637 | Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_mutable, 1) |
| 1638 | << Field; |
| 1639 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1640 | return false; |
| 1641 | } |
| 1642 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1643 | // Next subobject is a class, struct or union field. |
| 1644 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1645 | if (RD->isUnion()) { |
| 1646 | const FieldDecl *UnionField = O->getUnionField(); |
| 1647 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1648 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1649 | Info.Diag(E->getExprLoc(), |
| 1650 | diag::note_constexpr_read_inactive_union_member) |
| 1651 | << Field << !UnionField << UnionField; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1652 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1653 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1654 | O = &O->getUnionValue(); |
| 1655 | } else |
| 1656 | O = &O->getStructField(Field->getFieldIndex()); |
| 1657 | ObjType = Field->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (ObjType.isVolatileQualified()) { |
| 1660 | if (Info.getLangOpts().CPlusPlus) { |
| 1661 | // FIXME: Include a description of the path to the volatile subobject. |
| 1662 | Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1) |
| 1663 | << 2 << Field; |
| 1664 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1665 | } else { |
| 1666 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1667 | } |
| 1668 | return false; |
| 1669 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1670 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1671 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1672 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1673 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1674 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1675 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1676 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1677 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1678 | if (O->isUninit()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1679 | if (!Info.CheckingPotentialConstantExpression) |
| 1680 | Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1681 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1682 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1685 | Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1686 | return true; |
| 1687 | } |
| 1688 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1689 | /// Find the position where two subobject designators diverge, or equivalently |
| 1690 | /// the length of the common initial subsequence. |
| 1691 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 1692 | const SubobjectDesignator &A, |
| 1693 | const SubobjectDesignator &B, |
| 1694 | bool &WasArrayIndex) { |
| 1695 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 1696 | for (/**/; I != N; ++I) { |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1697 | if (!ObjType.isNull() && |
| 1698 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1699 | // Next subobject is an array element. |
| 1700 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 1701 | WasArrayIndex = true; |
| 1702 | return I; |
| 1703 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1704 | if (ObjType->isAnyComplexType()) |
| 1705 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1706 | else |
| 1707 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1708 | } else { |
| 1709 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 1710 | WasArrayIndex = false; |
| 1711 | return I; |
| 1712 | } |
| 1713 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 1714 | // Next subobject is a field. |
| 1715 | ObjType = FD->getType(); |
| 1716 | else |
| 1717 | // Next subobject is a base class. |
| 1718 | ObjType = QualType(); |
| 1719 | } |
| 1720 | } |
| 1721 | WasArrayIndex = false; |
| 1722 | return I; |
| 1723 | } |
| 1724 | |
| 1725 | /// Determine whether the given subobject designators refer to elements of the |
| 1726 | /// same array object. |
| 1727 | static bool AreElementsOfSameArray(QualType ObjType, |
| 1728 | const SubobjectDesignator &A, |
| 1729 | const SubobjectDesignator &B) { |
| 1730 | if (A.Entries.size() != B.Entries.size()) |
| 1731 | return false; |
| 1732 | |
| 1733 | bool IsArray = A.MostDerivedArraySize != 0; |
| 1734 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 1735 | // A is a subobject of the array element. |
| 1736 | return false; |
| 1737 | |
| 1738 | // If A (and B) designates an array element, the last entry will be the array |
| 1739 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 1740 | // of length 1' case, and the entire path must match. |
| 1741 | bool WasArrayIndex; |
| 1742 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 1743 | return CommonLength >= A.Entries.size() - IsArray; |
| 1744 | } |
| 1745 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1746 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1747 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1748 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1749 | /// |
| 1750 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1751 | /// \param Conv - The expression for which we are performing the conversion. |
| 1752 | /// Used for diagnostics. |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1753 | /// \param Type - The type we expect this conversion to produce, before |
| 1754 | /// stripping cv-qualifiers in the case of a non-clas type. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1755 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1756 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1757 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1758 | QualType Type, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1759 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1760 | // In C, an lvalue-to-rvalue conversion is never a constant expression. |
| 1761 | if (!Info.getLangOpts().CPlusPlus) |
| 1762 | Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1763 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1764 | if (LVal.Designator.Invalid) |
| 1765 | // A diagnostic will have already been produced. |
| 1766 | return false; |
| 1767 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1768 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1769 | SourceLocation Loc = Conv->getExprLoc(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1770 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1771 | if (!LVal.Base) { |
| 1772 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1773 | Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 1774 | return false; |
| 1775 | } |
| 1776 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1777 | CallStackFrame *Frame = 0; |
| 1778 | if (LVal.CallIndex) { |
| 1779 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 1780 | if (!Frame) { |
| 1781 | Info.Diag(Loc, diag::note_constexpr_lifetime_ended, 1) << !Base; |
| 1782 | NoteLValueLocation(Info, LVal.Base); |
| 1783 | return false; |
| 1784 | } |
| 1785 | } |
| 1786 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1787 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1788 | // is not a constant expression (even if the object is non-volatile). We also |
| 1789 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1790 | // semantics. |
| 1791 | if (Type.isVolatileQualified()) { |
| 1792 | if (Info.getLangOpts().CPlusPlus) |
| 1793 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type; |
| 1794 | else |
| 1795 | Info.Diag(Loc); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1796 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1797 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1798 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1799 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1800 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1801 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1802 | // expressions are constant expressions too. Inside constexpr functions, |
| 1803 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1804 | // In C, such things can also be folded, although they are not ICEs. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1805 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1806 | if (const VarDecl *VDef = VD->getDefinition()) |
| 1807 | VD = VDef; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1808 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1809 | Info.Diag(Loc); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1810 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1813 | // DR1313: If the object is volatile-qualified but the glvalue was not, |
| 1814 | // behavior is undefined so the result is not a constant expression. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1815 | QualType VT = VD->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1816 | if (VT.isVolatileQualified()) { |
| 1817 | if (Info.getLangOpts().CPlusPlus) { |
| 1818 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; |
| 1819 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1820 | } else { |
| 1821 | Info.Diag(Loc); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1822 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1823 | return false; |
| 1824 | } |
| 1825 | |
| 1826 | if (!isa<ParmVarDecl>(VD)) { |
| 1827 | if (VD->isConstexpr()) { |
| 1828 | // OK, we can read this variable. |
| 1829 | } else if (VT->isIntegralOrEnumerationType()) { |
| 1830 | if (!VT.isConstQualified()) { |
| 1831 | if (Info.getLangOpts().CPlusPlus) { |
| 1832 | Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 1833 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1834 | } else { |
| 1835 | Info.Diag(Loc); |
| 1836 | } |
| 1837 | return false; |
| 1838 | } |
| 1839 | } else if (VT->isFloatingType() && VT.isConstQualified()) { |
| 1840 | // We support folding of const floating-point types, in order to make |
| 1841 | // static const data members of such types (supported as an extension) |
| 1842 | // more useful. |
| 1843 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1844 | Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1845 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1846 | } else { |
| 1847 | Info.CCEDiag(Loc); |
| 1848 | } |
| 1849 | } else { |
| 1850 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1851 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1852 | Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1853 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1854 | } else { |
| 1855 | Info.Diag(Loc); |
| 1856 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1857 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1858 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1859 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1860 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1861 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1862 | return false; |
| 1863 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1864 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1865 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1866 | |
| 1867 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1868 | // conversion. This happens when the declaration and the lvalue should be |
| 1869 | // considered synonymous, for instance when initializing an array of char |
| 1870 | // from a string literal. Continue as if the initializer lvalue was the |
| 1871 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1872 | assert(RVal.getLValueOffset().isZero() && |
| 1873 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1874 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1875 | |
| 1876 | if (unsigned CallIndex = RVal.getLValueCallIndex()) { |
| 1877 | Frame = Info.getCallFrame(CallIndex); |
| 1878 | if (!Frame) { |
| 1879 | Info.Diag(Loc, diag::note_constexpr_lifetime_ended, 1) << !Base; |
| 1880 | NoteLValueLocation(Info, RVal.getLValueBase()); |
| 1881 | return false; |
| 1882 | } |
| 1883 | } else { |
| 1884 | Frame = 0; |
| 1885 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1888 | // Volatile temporary objects cannot be read in constant expressions. |
| 1889 | if (Base->getType().isVolatileQualified()) { |
| 1890 | if (Info.getLangOpts().CPlusPlus) { |
| 1891 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0; |
| 1892 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 1893 | } else { |
| 1894 | Info.Diag(Loc); |
| 1895 | } |
| 1896 | return false; |
| 1897 | } |
| 1898 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1899 | if (Frame) { |
| 1900 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1901 | // value from the relevant stack frame. |
| 1902 | RVal = Frame->Temporaries[Base]; |
| 1903 | } else if (const CompoundLiteralExpr *CLE |
| 1904 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1905 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1906 | // initializer until now for such expressions. Such an expression can't be |
| 1907 | // an ICE in C, so this only matters for fold. |
| 1908 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1909 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1910 | return false; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1911 | } else if (isa<StringLiteral>(Base)) { |
| 1912 | // We represent a string literal array as an lvalue pointing at the |
| 1913 | // corresponding expression, rather than building an array of chars. |
| 1914 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1915 | RVal = CCValue(Info.Ctx, |
| 1916 | APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0), |
| 1917 | CCValue::GlobalValue()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1918 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1919 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1920 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1921 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1922 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1923 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1924 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1927 | /// Build an lvalue for the object argument of a member function call. |
| 1928 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1929 | LValue &This) { |
| 1930 | if (Object->getType()->isPointerType()) |
| 1931 | return EvaluatePointer(Object, This, Info); |
| 1932 | |
| 1933 | if (Object->isGLValue()) |
| 1934 | return EvaluateLValue(Object, This, Info); |
| 1935 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1936 | if (Object->getType()->isLiteralType()) |
| 1937 | return EvaluateTemporary(Object, This, Info); |
| 1938 | |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
| 1942 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1943 | /// lvalue referring to the result. |
| 1944 | /// |
| 1945 | /// \param Info - Information about the ongoing evaluation. |
| 1946 | /// \param BO - The member pointer access operation. |
| 1947 | /// \param LV - Filled in with a reference to the resulting object. |
| 1948 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1949 | /// the resulting LValue subobject designator. This is not possible when |
| 1950 | /// creating a bound member function. |
| 1951 | /// \return The field or method declaration to which the member pointer refers, |
| 1952 | /// or 0 if evaluation fails. |
| 1953 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1954 | const BinaryOperator *BO, |
| 1955 | LValue &LV, |
| 1956 | bool IncludeMember = true) { |
| 1957 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1958 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1959 | bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); |
| 1960 | if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1961 | return 0; |
| 1962 | |
| 1963 | MemberPtr MemPtr; |
| 1964 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1965 | return 0; |
| 1966 | |
| 1967 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1968 | // member value, the behavior is undefined. |
| 1969 | if (!MemPtr.getDecl()) |
| 1970 | return 0; |
| 1971 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1972 | if (!EvalObjOK) |
| 1973 | return 0; |
| 1974 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1975 | if (MemPtr.isDerivedMember()) { |
| 1976 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1977 | // The end of the derived-to-base path for the base object must match the |
| 1978 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1979 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1980 | LV.Designator.Entries.size()) |
| 1981 | return 0; |
| 1982 | unsigned PathLengthToMember = |
| 1983 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1984 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1985 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1986 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1987 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1988 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
| 1992 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1993 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 1994 | PathLengthToMember)) |
| 1995 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1996 | } else if (!MemPtr.Path.empty()) { |
| 1997 | // Extend the LValue path with the member pointer's path. |
| 1998 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1999 | MemPtr.Path.size() + IncludeMember); |
| 2000 | |
| 2001 | // Walk down to the appropriate base class. |
| 2002 | QualType LVType = BO->getLHS()->getType(); |
| 2003 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 2004 | LVType = PT->getPointeeType(); |
| 2005 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 2006 | assert(RD && "member pointer access on non-class-type expression"); |
| 2007 | // The first class in the path is that of the lvalue. |
| 2008 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 2009 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2010 | HandleLValueDirectBase(Info, BO, LV, RD, Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2011 | RD = Base; |
| 2012 | } |
| 2013 | // Finally cast to the class containing the member. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2014 | HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | // Add the member. Note that we cannot build bound member functions here. |
| 2018 | if (IncludeMember) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2019 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) |
| 2020 | HandleLValueMember(Info, BO, LV, FD); |
| 2021 | else if (const IndirectFieldDecl *IFD = |
| 2022 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) |
| 2023 | HandleLValueIndirectMember(Info, BO, LV, IFD); |
| 2024 | else |
| 2025 | llvm_unreachable("can't construct reference to bound member function"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | return MemPtr.getDecl(); |
| 2029 | } |
| 2030 | |
| 2031 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 2032 | /// the provided lvalue, which currently refers to the base object. |
| 2033 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 2034 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2035 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2036 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2037 | return false; |
| 2038 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2039 | QualType TargetQT = E->getType(); |
| 2040 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 2041 | TargetQT = PT->getPointeeType(); |
| 2042 | |
| 2043 | // Check this cast lands within the final derived-to-base subobject path. |
| 2044 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
| 2045 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 2046 | << D.MostDerivedType << TargetQT; |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2050 | // Check the type of the final cast. We don't need to check the path, |
| 2051 | // since a cast can only be formed if the path is unique. |
| 2052 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2053 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 2054 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2055 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 2056 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2057 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2058 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2059 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
| 2060 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 2061 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2062 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2063 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2066 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2069 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2070 | enum EvalStmtResult { |
| 2071 | /// Evaluation failed. |
| 2072 | ESR_Failed, |
| 2073 | /// Hit a 'return' statement. |
| 2074 | ESR_Returned, |
| 2075 | /// Evaluation succeeded. |
| 2076 | ESR_Succeeded |
| 2077 | }; |
| 2078 | } |
| 2079 | |
| 2080 | // Evaluate a statement. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2081 | static EvalStmtResult EvaluateStmt(CCValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2082 | const Stmt *S) { |
| 2083 | switch (S->getStmtClass()) { |
| 2084 | default: |
| 2085 | return ESR_Failed; |
| 2086 | |
| 2087 | case Stmt::NullStmtClass: |
| 2088 | case Stmt::DeclStmtClass: |
| 2089 | return ESR_Succeeded; |
| 2090 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2091 | case Stmt::ReturnStmtClass: { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2092 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2093 | if (!Evaluate(Result, Info, RetExpr)) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2094 | return ESR_Failed; |
| 2095 | return ESR_Returned; |
| 2096 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2097 | |
| 2098 | case Stmt::CompoundStmtClass: { |
| 2099 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 2100 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 2101 | BE = CS->body_end(); BI != BE; ++BI) { |
| 2102 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 2103 | if (ESR != ESR_Succeeded) |
| 2104 | return ESR; |
| 2105 | } |
| 2106 | return ESR_Succeeded; |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2111 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 2112 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 2113 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 2114 | /// so we need special handling. |
| 2115 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2116 | const CXXConstructorDecl *CD, |
| 2117 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2118 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 2119 | return false; |
| 2120 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2121 | // Value-initialization does not call a trivial default constructor, so such a |
| 2122 | // call is a core constant expression whether or not the constructor is |
| 2123 | // constexpr. |
| 2124 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2125 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2126 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 2127 | // we should be much more explicit about why it's not constexpr. |
| 2128 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 2129 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 2130 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2131 | } else { |
| 2132 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 2133 | } |
| 2134 | } |
| 2135 | return true; |
| 2136 | } |
| 2137 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2138 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 2139 | /// expression. |
| 2140 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 2141 | const FunctionDecl *Declaration, |
| 2142 | const FunctionDecl *Definition) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2143 | // Potential constant expressions can contain calls to declared, but not yet |
| 2144 | // defined, constexpr functions. |
| 2145 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 2146 | Declaration->isConstexpr()) |
| 2147 | return false; |
| 2148 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2149 | // Can we evaluate this function call? |
| 2150 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 2151 | return true; |
| 2152 | |
| 2153 | if (Info.getLangOpts().CPlusPlus0x) { |
| 2154 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2155 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 2156 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2157 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 2158 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 2159 | << DiagDecl; |
| 2160 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 2161 | } else { |
| 2162 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 2163 | } |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2167 | namespace { |
Richard Smith | cd99b07 | 2011-11-11 05:48:57 +0000 | [diff] [blame] | 2168 | typedef SmallVector<CCValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 2172 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 2173 | EvalInfo &Info) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2174 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2175 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2176 | I != E; ++I) { |
| 2177 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 2178 | // If we're checking for a potential constant expression, evaluate all |
| 2179 | // initializers even if some of them fail. |
| 2180 | if (!Info.keepEvaluatingAfterFailure()) |
| 2181 | return false; |
| 2182 | Success = false; |
| 2183 | } |
| 2184 | } |
| 2185 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2188 | /// Evaluate a function call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2189 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 2190 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2191 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2192 | EvalInfo &Info, CCValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2193 | ArgVector ArgValues(Args.size()); |
| 2194 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2195 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2196 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2197 | if (!Info.CheckCallLimit(CallLoc)) |
| 2198 | return false; |
| 2199 | |
| 2200 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2201 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 2202 | } |
| 2203 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2204 | /// Evaluate a constructor call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2205 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2206 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2207 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2208 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2209 | ArgVector ArgValues(Args.size()); |
| 2210 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2211 | return false; |
| 2212 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2213 | if (!Info.CheckCallLimit(CallLoc)) |
| 2214 | return false; |
| 2215 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2216 | const CXXRecordDecl *RD = Definition->getParent(); |
| 2217 | if (RD->getNumVBases()) { |
| 2218 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 2219 | return false; |
| 2220 | } |
| 2221 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2222 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2223 | |
| 2224 | // If it's a delegating constructor, just delegate. |
| 2225 | if (Definition->isDelegatingConstructor()) { |
| 2226 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2227 | return EvaluateInPlace(Result, Info, This, (*I)->getInit()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2230 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 2231 | // essential for unions, where the operations performed by the constructor |
| 2232 | // cannot be represented by ctor-initializers. |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2233 | if (Definition->isDefaulted() && |
Douglas Gregor | f6cfe8b | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 2234 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 2235 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2236 | LValue RHS; |
| 2237 | RHS.setFrom(ArgValues[0]); |
| 2238 | CCValue Value; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2239 | if (!HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 2240 | RHS, Value)) |
| 2241 | return false; |
| 2242 | assert((Value.isStruct() || Value.isUnion()) && |
| 2243 | "trivial copy/move from non-class type?"); |
| 2244 | // Any CCValue of class type must already be a constant expression. |
| 2245 | Result = Value; |
| 2246 | return true; |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2250 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2251 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 2252 | std::distance(RD->field_begin(), RD->field_end())); |
| 2253 | |
| 2254 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2255 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2256 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2257 | unsigned BasesSeen = 0; |
| 2258 | #ifndef NDEBUG |
| 2259 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 2260 | #endif |
| 2261 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 2262 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2263 | LValue Subobject = This; |
| 2264 | APValue *Value = &Result; |
| 2265 | |
| 2266 | // Determine the subobject to initialize. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2267 | if ((*I)->isBaseInitializer()) { |
| 2268 | QualType BaseType((*I)->getBaseClass(), 0); |
| 2269 | #ifndef NDEBUG |
| 2270 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2271 | // definition. We have already checked for virtual base classes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2272 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 2273 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 2274 | "base class initializers not in expected order"); |
| 2275 | ++BaseIt; |
| 2276 | #endif |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2277 | HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2278 | BaseType->getAsCXXRecordDecl(), &Layout); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2279 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2280 | } else if (FieldDecl *FD = (*I)->getMember()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2281 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2282 | if (RD->isUnion()) { |
| 2283 | Result = APValue(FD); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2284 | Value = &Result.getUnionValue(); |
| 2285 | } else { |
| 2286 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 2287 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2288 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2289 | // Walk the indirect field decl's chain to find the object to initialize, |
| 2290 | // and make sure we've initialized every step along it. |
| 2291 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 2292 | CE = IFD->chain_end(); |
| 2293 | C != CE; ++C) { |
| 2294 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 2295 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 2296 | // Switch the union field if it differs. This happens if we had |
| 2297 | // preceding zero-initialization, and we're now initializing a union |
| 2298 | // subobject other than the first. |
| 2299 | // FIXME: In this case, the values of the other subobjects are |
| 2300 | // specified, since zero-initialization sets all padding bits to zero. |
| 2301 | if (Value->isUninit() || |
| 2302 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 2303 | if (CD->isUnion()) |
| 2304 | *Value = APValue(FD); |
| 2305 | else |
| 2306 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 2307 | std::distance(CD->field_begin(), CD->field_end())); |
| 2308 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2309 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2310 | if (CD->isUnion()) |
| 2311 | Value = &Value->getUnionValue(); |
| 2312 | else |
| 2313 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2314 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2315 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2316 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2317 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2318 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2319 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(), |
| 2320 | (*I)->isBaseInitializer() |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2321 | ? CCEK_Constant : CCEK_MemberInit)) { |
| 2322 | // If we're checking for a potential constant expression, evaluate all |
| 2323 | // initializers even if some of them fail. |
| 2324 | if (!Info.keepEvaluatingAfterFailure()) |
| 2325 | return false; |
| 2326 | Success = false; |
| 2327 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2330 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2333 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2334 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2335 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2336 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2337 | public: |
| 2338 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2339 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2340 | |
| 2341 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2342 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2343 | return true; |
| 2344 | } |
| 2345 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2346 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 2347 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2348 | return Visit(E->getResultExpr()); |
| 2349 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2350 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2351 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2352 | return true; |
| 2353 | return false; |
| 2354 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2355 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2356 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2357 | return true; |
| 2358 | return false; |
| 2359 | } |
| 2360 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2361 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2362 | return true; |
| 2363 | return false; |
| 2364 | } |
| 2365 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2366 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 2367 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2368 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 2369 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 2370 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2371 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2372 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 2373 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 2374 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 2375 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 2376 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 2377 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2378 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2379 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2380 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2381 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2382 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2383 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 2384 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 2385 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 2386 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2387 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2388 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 2389 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 2390 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 2391 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 2392 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2393 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2394 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2395 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2396 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2397 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2398 | |
| 2399 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2400 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2401 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 2402 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2403 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 2404 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2405 | return false; |
| 2406 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2407 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2408 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2409 | }; |
| 2410 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2411 | class OpaqueValueEvaluation { |
| 2412 | EvalInfo &info; |
| 2413 | OpaqueValueExpr *opaqueValue; |
| 2414 | |
| 2415 | public: |
| 2416 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 2417 | Expr *value) |
| 2418 | : info(info), opaqueValue(opaqueValue) { |
| 2419 | |
| 2420 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2421 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2422 | this->opaqueValue = 0; |
| 2423 | return; |
| 2424 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | bool hasError() const { return opaqueValue == 0; } |
| 2428 | |
| 2429 | ~OpaqueValueEvaluation() { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2430 | // FIXME: For a recursive constexpr call, an outer stack frame might have |
| 2431 | // been using this opaque value too, and will now have to re-evaluate the |
| 2432 | // source expression. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2433 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 2434 | } |
| 2435 | }; |
| 2436 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2437 | } // end anonymous namespace |
| 2438 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2439 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2440 | // Generic Evaluation |
| 2441 | //===----------------------------------------------------------------------===// |
| 2442 | namespace { |
| 2443 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2444 | // FIXME: RetTy is always bool. Remove it. |
| 2445 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2446 | class ExprEvaluatorBase |
| 2447 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2448 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2449 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2450 | return static_cast<Derived*>(this)->Success(V, E); |
| 2451 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2452 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2453 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2454 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2455 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2456 | // Check whether a conditional operator with a non-constant condition is a |
| 2457 | // potential constant expression. If neither arm is a potential constant |
| 2458 | // expression, then the conditional operator is not either. |
| 2459 | template<typename ConditionalOperator> |
| 2460 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| 2461 | assert(Info.CheckingPotentialConstantExpression); |
| 2462 | |
| 2463 | // Speculatively evaluate both arms. |
| 2464 | { |
| 2465 | llvm::SmallVector<PartialDiagnosticAt, 8> Diag; |
| 2466 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 2467 | |
| 2468 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 2469 | if (Diag.empty()) |
| 2470 | return; |
| 2471 | |
| 2472 | Diag.clear(); |
| 2473 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 2474 | if (Diag.empty()) |
| 2475 | return; |
| 2476 | } |
| 2477 | |
| 2478 | Error(E, diag::note_constexpr_conditional_never_const); |
| 2479 | } |
| 2480 | |
| 2481 | |
| 2482 | template<typename ConditionalOperator> |
| 2483 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 2484 | bool BoolResult; |
| 2485 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| 2486 | if (Info.CheckingPotentialConstantExpression) |
| 2487 | CheckPotentialConstantConditional(E); |
| 2488 | return false; |
| 2489 | } |
| 2490 | |
| 2491 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 2492 | return StmtVisitorTy::Visit(EvalExpr); |
| 2493 | } |
| 2494 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2495 | protected: |
| 2496 | EvalInfo &Info; |
| 2497 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2498 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2499 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2500 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | d509342 | 2011-12-12 09:41:58 +0000 | [diff] [blame] | 2501 | return Info.CCEDiag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | /// Report an evaluation error. This should only be called when an error is |
| 2505 | /// first discovered. When propagating an error, just return false. |
| 2506 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2507 | Info.Diag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2508 | return false; |
| 2509 | } |
| 2510 | bool Error(const Expr *E) { |
| 2511 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 2512 | } |
| 2513 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2514 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2515 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2516 | public: |
| 2517 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 2518 | |
| 2519 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2520 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2521 | } |
| 2522 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2523 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | RetTy VisitParenExpr(const ParenExpr *E) |
| 2527 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2528 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 2529 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2530 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 2531 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2532 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 2533 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 2534 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 2535 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 2536 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 2537 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 2538 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 2539 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 2540 | // We cannot create any objects for which cleanups are required, so there is |
| 2541 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 2542 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 2543 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2544 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2545 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 2546 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 2547 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2548 | } |
| 2549 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 2550 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 2551 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2552 | } |
| 2553 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2554 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 2555 | switch (E->getOpcode()) { |
| 2556 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2557 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2558 | |
| 2559 | case BO_Comma: |
| 2560 | VisitIgnoredValue(E->getLHS()); |
| 2561 | return StmtVisitorTy::Visit(E->getRHS()); |
| 2562 | |
| 2563 | case BO_PtrMemD: |
| 2564 | case BO_PtrMemI: { |
| 2565 | LValue Obj; |
| 2566 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 2567 | return false; |
| 2568 | CCValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2569 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2570 | return false; |
| 2571 | return DerivedSuccess(Result, E); |
| 2572 | } |
| 2573 | } |
| 2574 | } |
| 2575 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2576 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2577 | // Cache the value of the common expression. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2578 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 2579 | if (opaque.hasError()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2580 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2581 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2582 | return HandleConditionalOperator(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2586 | bool IsBcpCall = false; |
| 2587 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 2588 | // the result is a constant expression if it can be folded without |
| 2589 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 2590 | // for discussion. |
| 2591 | if (const CallExpr *CallCE = |
| 2592 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 2593 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 2594 | IsBcpCall = true; |
| 2595 | |
| 2596 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 2597 | // constant expression; we can't check whether it's potentially foldable. |
| 2598 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 2599 | return false; |
| 2600 | |
| 2601 | FoldConstant Fold(Info); |
| 2602 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2603 | if (!HandleConditionalOperator(E)) |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2604 | return false; |
| 2605 | |
| 2606 | if (IsBcpCall) |
| 2607 | Fold.Fold(Info); |
| 2608 | |
| 2609 | return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2613 | const CCValue *Value = Info.getOpaqueValue(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2614 | if (!Value) { |
| 2615 | const Expr *Source = E->getSourceExpr(); |
| 2616 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2617 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2618 | if (Source == E) { // sanity checking. |
| 2619 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2620 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2621 | } |
| 2622 | return StmtVisitorTy::Visit(Source); |
| 2623 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2624 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2625 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2626 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2627 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2628 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2629 | QualType CalleeType = Callee->getType(); |
| 2630 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2631 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2632 | LValue *This = 0, ThisVal; |
| 2633 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2634 | bool HasQualifier = false; |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 2635 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2636 | // Extract function decl and 'this' pointer from the callee. |
| 2637 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2638 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2639 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 2640 | // Explicit bound member calls, such as x.f() or p->g(); |
| 2641 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2642 | return false; |
| 2643 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2644 | This = &ThisVal; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2645 | HasQualifier = ME->hasQualifier(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2646 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 2647 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2648 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 2649 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2650 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2651 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2652 | return Error(Callee); |
| 2653 | |
| 2654 | FD = dyn_cast<FunctionDecl>(Member); |
| 2655 | if (!FD) |
| 2656 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2657 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2658 | LValue Call; |
| 2659 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2660 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2661 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2662 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2663 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2664 | FD = dyn_cast_or_null<FunctionDecl>( |
| 2665 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2666 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2667 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Overloaded operator calls to member functions are represented as normal |
| 2670 | // calls with '*this' as the first argument. |
| 2671 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 2672 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2673 | // FIXME: When selecting an implicit conversion for an overloaded |
| 2674 | // operator delete, we sometimes try to evaluate calls to conversion |
| 2675 | // operators without a 'this' parameter! |
| 2676 | if (Args.empty()) |
| 2677 | return Error(E); |
| 2678 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2679 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 2680 | return false; |
| 2681 | This = &ThisVal; |
| 2682 | Args = Args.slice(1); |
| 2683 | } |
| 2684 | |
| 2685 | // Don't call function pointers which have been cast to some other type. |
| 2686 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2687 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2688 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2689 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2690 | |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 2691 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 2692 | return false; |
| 2693 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2694 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 2695 | // calls to such functions in constant expressions. |
| 2696 | if (This && !HasQualifier && |
| 2697 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 2698 | return Error(E, diag::note_constexpr_virtual_call); |
| 2699 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2700 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2701 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2702 | CCValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2703 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2704 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2705 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 2706 | Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2707 | return false; |
| 2708 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2709 | return DerivedSuccess(Result, E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2712 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 2713 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 2714 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2715 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 2716 | if (E->getNumInits() == 0) |
| 2717 | return DerivedZeroInitialization(E); |
| 2718 | if (E->getNumInits() == 1) |
| 2719 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2720 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2721 | } |
| 2722 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2723 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2724 | } |
| 2725 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2726 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2727 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2728 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2729 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2730 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2731 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2732 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 2733 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 2734 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 2735 | |
| 2736 | CCValue Val; |
| 2737 | if (!Evaluate(Val, Info, E->getBase())) |
| 2738 | return false; |
| 2739 | |
| 2740 | QualType BaseTy = E->getBase()->getType(); |
| 2741 | |
| 2742 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2743 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2744 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 2745 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2746 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2747 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2748 | SubobjectDesignator Designator(BaseTy); |
| 2749 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2750 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2751 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2752 | DerivedSuccess(Val, E); |
| 2753 | } |
| 2754 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2755 | RetTy VisitCastExpr(const CastExpr *E) { |
| 2756 | switch (E->getCastKind()) { |
| 2757 | default: |
| 2758 | break; |
| 2759 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2760 | case CK_AtomicToNonAtomic: |
| 2761 | case CK_NonAtomicToAtomic: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2762 | case CK_NoOp: |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 2763 | case CK_UserDefinedConversion: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2764 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 2765 | |
| 2766 | case CK_LValueToRValue: { |
| 2767 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2768 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 2769 | return false; |
| 2770 | CCValue RVal; |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 2771 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 2772 | if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 2773 | LVal, RVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2774 | return false; |
| 2775 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2779 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2782 | /// Visit a value which is evaluated, but whose value is ignored. |
| 2783 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2784 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2785 | if (!Evaluate(Scratch, Info, E)) |
| 2786 | Info.EvalStatus.HasSideEffects = true; |
| 2787 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2788 | }; |
| 2789 | |
| 2790 | } |
| 2791 | |
| 2792 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2793 | // Common base class for lvalue and temporary evaluation. |
| 2794 | //===----------------------------------------------------------------------===// |
| 2795 | namespace { |
| 2796 | template<class Derived> |
| 2797 | class LValueExprEvaluatorBase |
| 2798 | : public ExprEvaluatorBase<Derived, bool> { |
| 2799 | protected: |
| 2800 | LValue &Result; |
| 2801 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 2802 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 2803 | |
| 2804 | bool Success(APValue::LValueBase B) { |
| 2805 | Result.set(B); |
| 2806 | return true; |
| 2807 | } |
| 2808 | |
| 2809 | public: |
| 2810 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 2811 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2812 | |
| 2813 | bool Success(const CCValue &V, const Expr *E) { |
| 2814 | Result.setFrom(V); |
| 2815 | return true; |
| 2816 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2817 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2818 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2819 | // Handle non-static data members. |
| 2820 | QualType BaseTy; |
| 2821 | if (E->isArrow()) { |
| 2822 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 2823 | return false; |
| 2824 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2825 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2826 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2827 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 2828 | return false; |
| 2829 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2830 | } else { |
| 2831 | if (!this->Visit(E->getBase())) |
| 2832 | return false; |
| 2833 | BaseTy = E->getBase()->getType(); |
| 2834 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2835 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2836 | const ValueDecl *MD = E->getMemberDecl(); |
| 2837 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 2838 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2839 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2840 | (void)BaseTy; |
| 2841 | HandleLValueMember(this->Info, E, Result, FD); |
| 2842 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
| 2843 | HandleLValueIndirectMember(this->Info, E, Result, IFD); |
| 2844 | } else |
| 2845 | return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2846 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2847 | if (MD->getType()->isReferenceType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2848 | CCValue RefValue; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2849 | if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2850 | RefValue)) |
| 2851 | return false; |
| 2852 | return Success(RefValue, E); |
| 2853 | } |
| 2854 | return true; |
| 2855 | } |
| 2856 | |
| 2857 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2858 | switch (E->getOpcode()) { |
| 2859 | default: |
| 2860 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2861 | |
| 2862 | case BO_PtrMemD: |
| 2863 | case BO_PtrMemI: |
| 2864 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | bool VisitCastExpr(const CastExpr *E) { |
| 2869 | switch (E->getCastKind()) { |
| 2870 | default: |
| 2871 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2872 | |
| 2873 | case CK_DerivedToBase: |
| 2874 | case CK_UncheckedDerivedToBase: { |
| 2875 | if (!this->Visit(E->getSubExpr())) |
| 2876 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2877 | |
| 2878 | // Now figure out the necessary offset to add to the base LV to get from |
| 2879 | // the derived class to the base class. |
| 2880 | QualType Type = E->getSubExpr()->getType(); |
| 2881 | |
| 2882 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2883 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2884 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2885 | *PathI)) |
| 2886 | return false; |
| 2887 | Type = (*PathI)->getType(); |
| 2888 | } |
| 2889 | |
| 2890 | return true; |
| 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | }; |
| 2895 | } |
| 2896 | |
| 2897 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2898 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2899 | // |
| 2900 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2901 | // function designators (in C), decl references to void objects (in C), and |
| 2902 | // temporaries (if building with -Wno-address-of-temporary). |
| 2903 | // |
| 2904 | // LValue evaluation produces values comprising a base expression of one of the |
| 2905 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2906 | // - Declarations |
| 2907 | // * VarDecl |
| 2908 | // * FunctionDecl |
| 2909 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2910 | // * CompoundLiteralExpr in C |
| 2911 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2912 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2913 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2914 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2915 | // * ObjCEncodeExpr |
| 2916 | // * AddrLabelExpr |
| 2917 | // * BlockExpr |
| 2918 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2919 | // - Locals and temporaries |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2920 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
| 2921 | // was evaluated. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2922 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2923 | //===----------------------------------------------------------------------===// |
| 2924 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2925 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2926 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2927 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2928 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2929 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2931 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2932 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2933 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2934 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2935 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2936 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2937 | bool VisitMemberExpr(const MemberExpr *E); |
| 2938 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2939 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2940 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2941 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2942 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2943 | bool VisitUnaryReal(const UnaryOperator *E); |
| 2944 | bool VisitUnaryImag(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2945 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2946 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2947 | switch (E->getCastKind()) { |
| 2948 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2949 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2950 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2951 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2952 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2953 | if (!Visit(E->getSubExpr())) |
| 2954 | return false; |
| 2955 | Result.Designator.setInvalid(); |
| 2956 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2957 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2958 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2959 | if (!Visit(E->getSubExpr())) |
| 2960 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2961 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2962 | } |
| 2963 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2964 | }; |
| 2965 | } // end anonymous namespace |
| 2966 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2967 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2968 | /// expressions which are not glvalues, in a few cases: |
| 2969 | /// * function designators in C, |
| 2970 | /// * "extern void" objects, |
| 2971 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2972 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2973 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2974 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2975 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2976 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2979 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2980 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2981 | return Success(FD); |
| 2982 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2983 | return VisitVarDecl(E, VD); |
| 2984 | return Error(E); |
| 2985 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2986 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2987 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2988 | if (!VD->getType()->isReferenceType()) { |
| 2989 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2990 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2991 | return true; |
| 2992 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2993 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2994 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2995 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2996 | CCValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2997 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2998 | return false; |
| 2999 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3002 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 3003 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3004 | if (E->GetTemporaryExpr()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3005 | if (E->getType()->isRecordType()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3006 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 3007 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3008 | Result.set(E, Info.CurrentCall->Index); |
| 3009 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, |
| 3010 | Result, E->GetTemporaryExpr()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 3014 | // (for instance, if it's a bitfield). |
| 3015 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 3016 | if (!Visit(E->GetTemporaryExpr())) |
| 3017 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3018 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3019 | Info.CurrentCall->Temporaries[E])) |
| 3020 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3021 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3022 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3025 | bool |
| 3026 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3027 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 3028 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 3029 | // 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] | 3030 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3033 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 3034 | if (E->isTypeOperand()) |
| 3035 | return Success(E); |
| 3036 | CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); |
| 3037 | if (RD && RD->isPolymorphic()) { |
| 3038 | Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic) |
| 3039 | << E->getExprOperand()->getType() |
| 3040 | << E->getExprOperand()->getSourceRange(); |
| 3041 | return false; |
| 3042 | } |
| 3043 | return Success(E); |
| 3044 | } |
| 3045 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3046 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3047 | // Handle static data members. |
| 3048 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 3049 | VisitIgnoredValue(E->getBase()); |
| 3050 | return VisitVarDecl(E, VD); |
| 3051 | } |
| 3052 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3053 | // Handle static member functions. |
| 3054 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 3055 | if (MD->isStatic()) { |
| 3056 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3057 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3061 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3062 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3065 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3066 | // FIXME: Deal with vectors as array subscript bases. |
| 3067 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3068 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3069 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3070 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3071 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3073 | APSInt Index; |
| 3074 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3075 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3076 | int64_t IndexValue |
| 3077 | = Index.isSigned() ? Index.getSExtValue() |
| 3078 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3079 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3080 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3081 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3082 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3083 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3084 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3087 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 3088 | if (!Visit(E->getSubExpr())) |
| 3089 | return false; |
| 3090 | // __real is a no-op on scalar lvalues. |
| 3091 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 3092 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 3093 | return true; |
| 3094 | } |
| 3095 | |
| 3096 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 3097 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 3098 | "lvalue __imag__ on scalar?"); |
| 3099 | if (!Visit(E->getSubExpr())) |
| 3100 | return false; |
| 3101 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 3102 | return true; |
| 3103 | } |
| 3104 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3105 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3106 | // Pointer Evaluation |
| 3107 | //===----------------------------------------------------------------------===// |
| 3108 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3109 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3110 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3111 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3112 | LValue &Result; |
| 3113 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3114 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3115 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3116 | return true; |
| 3117 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3118 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3120 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3121 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3122 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3123 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3124 | Result.setFrom(V); |
| 3125 | return true; |
| 3126 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3127 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3128 | return Success((Expr*)0); |
| 3129 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3130 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3131 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3132 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3133 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3134 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3135 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3136 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3137 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3138 | bool VisitCallExpr(const CallExpr *E); |
| 3139 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3140 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3141 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3142 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 3143 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3144 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 3145 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3146 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3147 | Result = *Info.CurrentCall->This; |
| 3148 | return true; |
| 3149 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3150 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3151 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3152 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3153 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3154 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3155 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3156 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3157 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3160 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3161 | if (E->getOpcode() != BO_Add && |
| 3162 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3163 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3165 | const Expr *PExp = E->getLHS(); |
| 3166 | const Expr *IExp = E->getRHS(); |
| 3167 | if (IExp->getType()->isPointerType()) |
| 3168 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3170 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 3171 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3172 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3174 | llvm::APSInt Offset; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3175 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3176 | return false; |
| 3177 | int64_t AdditionalOffset |
| 3178 | = Offset.isSigned() ? Offset.getSExtValue() |
| 3179 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3180 | if (E->getOpcode() == BO_Sub) |
| 3181 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3182 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3183 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3184 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 3185 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3186 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3187 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3188 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3189 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3192 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3193 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3194 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3195 | switch (E->getCastKind()) { |
| 3196 | default: |
| 3197 | break; |
| 3198 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3199 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3200 | case CK_CPointerToObjCPointerCast: |
| 3201 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3202 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3203 | if (!Visit(SubExpr)) |
| 3204 | return false; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3205 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 3206 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 3207 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3208 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3209 | Result.Designator.setInvalid(); |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3210 | if (SubExpr->getType()->isVoidPointerType()) |
| 3211 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 3212 | << 3 << SubExpr->getType(); |
| 3213 | else |
| 3214 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3215 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3216 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3217 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3218 | case CK_DerivedToBase: |
| 3219 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3220 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3221 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3222 | if (!Result.Base && Result.Offset.isZero()) |
| 3223 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3224 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3225 | // 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] | 3226 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3227 | QualType Type = |
| 3228 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3229 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3230 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3231 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3232 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 3233 | *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3234 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3235 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3238 | return true; |
| 3239 | } |
| 3240 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3241 | case CK_BaseToDerived: |
| 3242 | if (!Visit(E->getSubExpr())) |
| 3243 | return false; |
| 3244 | if (!Result.Base && Result.Offset.isZero()) |
| 3245 | return true; |
| 3246 | return HandleBaseToDerivedCast(Info, E, Result); |
| 3247 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3248 | case CK_NullToPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3249 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3250 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3251 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3252 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3253 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3254 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3255 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3256 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3257 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3258 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3259 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 3260 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3261 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3262 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3263 | Result.CallIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3264 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3265 | return true; |
| 3266 | } else { |
| 3267 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3268 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3269 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3270 | } |
| 3271 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3272 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3273 | if (SubExpr->isGLValue()) { |
| 3274 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 3275 | return false; |
| 3276 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3277 | Result.set(SubExpr, Info.CurrentCall->Index); |
| 3278 | if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], |
| 3279 | Info, Result, SubExpr)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3280 | return false; |
| 3281 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3282 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3283 | if (const ConstantArrayType *CAT |
| 3284 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 3285 | Result.addArray(Info, E, CAT); |
| 3286 | else |
| 3287 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3288 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3289 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3290 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3291 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3294 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3296 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3297 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3298 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3299 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 3300 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3301 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3302 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3303 | |
| 3304 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3305 | // Member Pointer Evaluation |
| 3306 | //===----------------------------------------------------------------------===// |
| 3307 | |
| 3308 | namespace { |
| 3309 | class MemberPointerExprEvaluator |
| 3310 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 3311 | MemberPtr &Result; |
| 3312 | |
| 3313 | bool Success(const ValueDecl *D) { |
| 3314 | Result = MemberPtr(D); |
| 3315 | return true; |
| 3316 | } |
| 3317 | public: |
| 3318 | |
| 3319 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 3320 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3321 | |
| 3322 | bool Success(const CCValue &V, const Expr *E) { |
| 3323 | Result.setFrom(V); |
| 3324 | return true; |
| 3325 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3326 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3327 | return Success((const ValueDecl*)0); |
| 3328 | } |
| 3329 | |
| 3330 | bool VisitCastExpr(const CastExpr *E); |
| 3331 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 3332 | }; |
| 3333 | } // end anonymous namespace |
| 3334 | |
| 3335 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 3336 | EvalInfo &Info) { |
| 3337 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 3338 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 3339 | } |
| 3340 | |
| 3341 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3342 | switch (E->getCastKind()) { |
| 3343 | default: |
| 3344 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3345 | |
| 3346 | case CK_NullToMemberPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3347 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3348 | |
| 3349 | case CK_BaseToDerivedMemberPointer: { |
| 3350 | if (!Visit(E->getSubExpr())) |
| 3351 | return false; |
| 3352 | if (E->path_empty()) |
| 3353 | return true; |
| 3354 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 3355 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 3356 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 3357 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 3358 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 3359 | PathI != PathE; ++PathI) { |
| 3360 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3361 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3362 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3363 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3364 | } |
| 3365 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 3366 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3367 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3368 | return true; |
| 3369 | } |
| 3370 | |
| 3371 | case CK_DerivedToBaseMemberPointer: |
| 3372 | if (!Visit(E->getSubExpr())) |
| 3373 | return false; |
| 3374 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3375 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3376 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3377 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3378 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3379 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3380 | } |
| 3381 | return true; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3386 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 3387 | // member can be formed. |
| 3388 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 3389 | } |
| 3390 | |
| 3391 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3392 | // Record Evaluation |
| 3393 | //===----------------------------------------------------------------------===// |
| 3394 | |
| 3395 | namespace { |
| 3396 | class RecordExprEvaluator |
| 3397 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 3398 | const LValue &This; |
| 3399 | APValue &Result; |
| 3400 | public: |
| 3401 | |
| 3402 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 3403 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 3404 | |
| 3405 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3406 | Result = V; |
| 3407 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3408 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3409 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3410 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3411 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3412 | bool VisitInitListExpr(const InitListExpr *E); |
| 3413 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 3414 | }; |
| 3415 | } |
| 3416 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3417 | /// Perform zero-initialization on an object of non-union class type. |
| 3418 | /// C++11 [dcl.init]p5: |
| 3419 | /// To zero-initialize an object or reference of type T means: |
| 3420 | /// [...] |
| 3421 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 3422 | /// each non-static data member and each base-class subobject is |
| 3423 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3424 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 3425 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3426 | const LValue &This, APValue &Result) { |
| 3427 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 3428 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 3429 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 3430 | std::distance(RD->field_begin(), RD->field_end())); |
| 3431 | |
| 3432 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3433 | |
| 3434 | if (CD) { |
| 3435 | unsigned Index = 0; |
| 3436 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3437 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3438 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 3439 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3440 | HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); |
| 3441 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3442 | Result.getStructBase(Index))) |
| 3443 | return false; |
| 3444 | } |
| 3445 | } |
| 3446 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3447 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 3448 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3449 | // -- if T is a reference type, no initialization is performed. |
| 3450 | if ((*I)->getType()->isReferenceType()) |
| 3451 | continue; |
| 3452 | |
| 3453 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3454 | HandleLValueMember(Info, E, Subobject, *I, &Layout); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3455 | |
| 3456 | ImplicitValueInitExpr VIE((*I)->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3457 | if (!EvaluateInPlace( |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3458 | Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) |
| 3459 | return false; |
| 3460 | } |
| 3461 | |
| 3462 | return true; |
| 3463 | } |
| 3464 | |
| 3465 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 3466 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 3467 | if (RD->isUnion()) { |
| 3468 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 3469 | // object's first non-static named data member is zero-initialized |
| 3470 | RecordDecl::field_iterator I = RD->field_begin(); |
| 3471 | if (I == RD->field_end()) { |
| 3472 | Result = APValue((const FieldDecl*)0); |
| 3473 | return true; |
| 3474 | } |
| 3475 | |
| 3476 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3477 | HandleLValueMember(Info, E, Subobject, *I); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3478 | Result = APValue(*I); |
| 3479 | ImplicitValueInitExpr VIE((*I)->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3480 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 3483 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
| 3484 | Info.Diag(E->getExprLoc(), diag::note_constexpr_virtual_base) << RD; |
| 3485 | return false; |
| 3486 | } |
| 3487 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3488 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3491 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3492 | switch (E->getCastKind()) { |
| 3493 | default: |
| 3494 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3495 | |
| 3496 | case CK_ConstructorConversion: |
| 3497 | return Visit(E->getSubExpr()); |
| 3498 | |
| 3499 | case CK_DerivedToBase: |
| 3500 | case CK_UncheckedDerivedToBase: { |
| 3501 | CCValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3502 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3503 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3504 | if (!DerivedObject.isStruct()) |
| 3505 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3506 | |
| 3507 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 3508 | APValue *Value = &DerivedObject; |
| 3509 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 3510 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3511 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3512 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 3513 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3514 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 3515 | RD = Base; |
| 3516 | } |
| 3517 | Result = *Value; |
| 3518 | return true; |
| 3519 | } |
| 3520 | } |
| 3521 | } |
| 3522 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3523 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Sebastian Redl | 24fe798 | 2012-02-19 14:53:49 +0000 | [diff] [blame] | 3524 | // Cannot constant-evaluate std::initializer_list inits. |
| 3525 | if (E->initializesStdInitializerList()) |
| 3526 | return false; |
| 3527 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3528 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 3529 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3530 | |
| 3531 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3532 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 3533 | Result = APValue(Field); |
| 3534 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3535 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3536 | |
| 3537 | // If the initializer list for a union does not contain any elements, the |
| 3538 | // first element of the union is value-initialized. |
| 3539 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3540 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 3541 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3542 | LValue Subobject = This; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3543 | HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3544 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 3548 | "initializer list for class with base classes"); |
| 3549 | Result = APValue(APValue::UninitStruct(), 0, |
| 3550 | std::distance(RD->field_begin(), RD->field_end())); |
| 3551 | unsigned ElementNo = 0; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3552 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3553 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3554 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 3555 | // Anonymous bit-fields are not considered members of the class for |
| 3556 | // purposes of aggregate initialization. |
| 3557 | if (Field->isUnnamedBitfield()) |
| 3558 | continue; |
| 3559 | |
| 3560 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3561 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3562 | bool HaveInit = ElementNo < E->getNumInits(); |
| 3563 | |
| 3564 | // FIXME: Diagnostics here should point to the end of the initializer |
| 3565 | // list, not the start. |
| 3566 | HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject, |
| 3567 | *Field, &Layout); |
| 3568 | |
| 3569 | // Perform an implicit value-initialization for members beyond the end of |
| 3570 | // the initializer list. |
| 3571 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
| 3572 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3573 | if (!EvaluateInPlace( |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3574 | Result.getStructField((*Field)->getFieldIndex()), |
| 3575 | Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { |
| 3576 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3577 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3578 | Success = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3582 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3586 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3587 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3588 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3589 | // If we've already performed zero-initialization, we're already done. |
| 3590 | if (!Result.isUninit()) |
| 3591 | return true; |
| 3592 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3593 | if (ZeroInit) |
| 3594 | return ZeroInitialization(E); |
| 3595 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3596 | const CXXRecordDecl *RD = FD->getParent(); |
| 3597 | if (RD->isUnion()) |
| 3598 | Result = APValue((FieldDecl*)0); |
| 3599 | else |
| 3600 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3601 | std::distance(RD->field_begin(), RD->field_end())); |
| 3602 | return true; |
| 3603 | } |
| 3604 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3605 | const FunctionDecl *Definition = 0; |
| 3606 | FD->getBody(Definition); |
| 3607 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3608 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3609 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3610 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3611 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3612 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3613 | if (const MaterializeTemporaryExpr *ME |
| 3614 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 3615 | return Visit(ME->GetTemporaryExpr()); |
| 3616 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3617 | if (ZeroInit && !ZeroInitialization(E)) |
| 3618 | return false; |
| 3619 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3620 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3621 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3622 | cast<CXXConstructorDecl>(Definition), Info, |
| 3623 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 3627 | APValue &Result, EvalInfo &Info) { |
| 3628 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3629 | "can't evaluate expression as a record rvalue"); |
| 3630 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 3631 | } |
| 3632 | |
| 3633 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3634 | // Temporary Evaluation |
| 3635 | // |
| 3636 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 3637 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 3638 | // materialized so that a reference can bind to it. |
| 3639 | //===----------------------------------------------------------------------===// |
| 3640 | namespace { |
| 3641 | class TemporaryExprEvaluator |
| 3642 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 3643 | public: |
| 3644 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3645 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 3646 | |
| 3647 | /// Visit an expression which constructs the value of this temporary. |
| 3648 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3649 | Result.set(E, Info.CurrentCall->Index); |
| 3650 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | bool VisitCastExpr(const CastExpr *E) { |
| 3654 | switch (E->getCastKind()) { |
| 3655 | default: |
| 3656 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3657 | |
| 3658 | case CK_ConstructorConversion: |
| 3659 | return VisitConstructExpr(E->getSubExpr()); |
| 3660 | } |
| 3661 | } |
| 3662 | bool VisitInitListExpr(const InitListExpr *E) { |
| 3663 | return VisitConstructExpr(E); |
| 3664 | } |
| 3665 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3666 | return VisitConstructExpr(E); |
| 3667 | } |
| 3668 | bool VisitCallExpr(const CallExpr *E) { |
| 3669 | return VisitConstructExpr(E); |
| 3670 | } |
| 3671 | }; |
| 3672 | } // end anonymous namespace |
| 3673 | |
| 3674 | /// Evaluate an expression of record type as a temporary. |
| 3675 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3676 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3677 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 3678 | } |
| 3679 | |
| 3680 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3681 | // Vector Evaluation |
| 3682 | //===----------------------------------------------------------------------===// |
| 3683 | |
| 3684 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3685 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3686 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 3687 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3688 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3690 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 3691 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3693 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 3694 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 3695 | // FIXME: remove this APValue copy. |
| 3696 | Result = APValue(V.data(), V.size()); |
| 3697 | return true; |
| 3698 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3699 | bool Success(const CCValue &V, const Expr *E) { |
| 3700 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3701 | Result = V; |
| 3702 | return true; |
| 3703 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3704 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3706 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3707 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3708 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3709 | bool VisitInitListExpr(const InitListExpr *E); |
| 3710 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3711 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3712 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3713 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3714 | }; |
| 3715 | } // end anonymous namespace |
| 3716 | |
| 3717 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3718 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3719 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3722 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3723 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3724 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 3726 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3727 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3728 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3729 | switch (E->getCastKind()) { |
| 3730 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3731 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3732 | if (SETy->isIntegerType()) { |
| 3733 | APSInt IntResult; |
| 3734 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3735 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3736 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3737 | } else if (SETy->isRealFloatingType()) { |
| 3738 | APFloat F(0.0); |
| 3739 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3740 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3741 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3742 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3743 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3744 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3745 | |
| 3746 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3747 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 3748 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3749 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 3750 | case CK_BitCast: { |
| 3751 | // Evaluate the operand into an APInt we can extract from. |
| 3752 | llvm::APInt SValInt; |
| 3753 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 3754 | return false; |
| 3755 | // Extract the elements |
| 3756 | QualType EltTy = VTy->getElementType(); |
| 3757 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 3758 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 3759 | SmallVector<APValue, 4> Elts; |
| 3760 | if (EltTy->isRealFloatingType()) { |
| 3761 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| 3762 | bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; |
| 3763 | unsigned FloatEltSize = EltSize; |
| 3764 | if (&Sem == &APFloat::x87DoubleExtended) |
| 3765 | FloatEltSize = 80; |
| 3766 | for (unsigned i = 0; i < NElts; i++) { |
| 3767 | llvm::APInt Elt; |
| 3768 | if (BigEndian) |
| 3769 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 3770 | else |
| 3771 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| 3772 | Elts.push_back(APValue(APFloat(Elt, isIEESem))); |
| 3773 | } |
| 3774 | } else if (EltTy->isIntegerType()) { |
| 3775 | for (unsigned i = 0; i < NElts; i++) { |
| 3776 | llvm::APInt Elt; |
| 3777 | if (BigEndian) |
| 3778 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 3779 | else |
| 3780 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 3781 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 3782 | } |
| 3783 | } else { |
| 3784 | return Error(E); |
| 3785 | } |
| 3786 | return Success(Elts, E); |
| 3787 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3788 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3789 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3790 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3793 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3794 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3795 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3796 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3797 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3799 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3800 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3801 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3802 | // The number of initializers can be less than the number of |
| 3803 | // vector elements. For OpenCL, this can be due to nested vector |
| 3804 | // initialization. For GCC compatibility, missing trailing elements |
| 3805 | // should be initialized with zeroes. |
| 3806 | unsigned CountInits = 0, CountElts = 0; |
| 3807 | while (CountElts < NumElements) { |
| 3808 | // Handle nested vector initialization. |
| 3809 | if (CountInits < NumInits |
| 3810 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 3811 | APValue v; |
| 3812 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 3813 | return Error(E); |
| 3814 | unsigned vlen = v.getVectorLength(); |
| 3815 | for (unsigned j = 0; j < vlen; j++) |
| 3816 | Elements.push_back(v.getVectorElt(j)); |
| 3817 | CountElts += vlen; |
| 3818 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3819 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3820 | if (CountInits < NumInits) { |
| 3821 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
| 3822 | return Error(E); |
| 3823 | } else // trailing integer zero. |
| 3824 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 3825 | Elements.push_back(APValue(sInt)); |
| 3826 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3827 | } else { |
| 3828 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3829 | if (CountInits < NumInits) { |
| 3830 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
| 3831 | return Error(E); |
| 3832 | } else // trailing float zero. |
| 3833 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 3834 | Elements.push_back(APValue(f)); |
| 3835 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 3836 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3837 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3838 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3839 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3842 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3843 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3844 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3845 | QualType EltTy = VT->getElementType(); |
| 3846 | APValue ZeroElement; |
| 3847 | if (EltTy->isIntegerType()) |
| 3848 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 3849 | else |
| 3850 | ZeroElement = |
| 3851 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 3852 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3853 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3854 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3857 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3858 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3859 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3862 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3863 | // Array Evaluation |
| 3864 | //===----------------------------------------------------------------------===// |
| 3865 | |
| 3866 | namespace { |
| 3867 | class ArrayExprEvaluator |
| 3868 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3869 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3870 | APValue &Result; |
| 3871 | public: |
| 3872 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3873 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 3874 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3875 | |
| 3876 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3877 | assert((V.isArray() || V.isLValue()) && |
| 3878 | "expected array or string literal"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3879 | Result = V; |
| 3880 | return true; |
| 3881 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3882 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3883 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3884 | const ConstantArrayType *CAT = |
| 3885 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3886 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3887 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3888 | |
| 3889 | Result = APValue(APValue::UninitArray(), 0, |
| 3890 | CAT->getSize().getZExtValue()); |
| 3891 | if (!Result.hasArrayFiller()) return true; |
| 3892 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3893 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3894 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3895 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3896 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3897 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3900 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3901 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3902 | }; |
| 3903 | } // end anonymous namespace |
| 3904 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3905 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 3906 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3907 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3908 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3912 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3913 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3914 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3915 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3916 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 3917 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3918 | if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() && |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3919 | Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) { |
| 3920 | LValue LV; |
| 3921 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 3922 | return false; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3923 | CCValue Val; |
| 3924 | LV.moveInto(Val); |
| 3925 | return Success(Val, E); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3928 | bool Success = true; |
| 3929 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3930 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 3931 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3932 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3933 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3934 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3935 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3936 | I != End; ++I, ++Index) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3937 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 3938 | Info, Subobject, cast<Expr>(*I)) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3939 | !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, |
| 3940 | CAT->getElementType(), 1)) { |
| 3941 | if (!Info.keepEvaluatingAfterFailure()) |
| 3942 | return false; |
| 3943 | Success = false; |
| 3944 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3945 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3946 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3947 | if (!Result.hasArrayFiller()) return Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3948 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3949 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3950 | // but sometimes does: |
| 3951 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3952 | // S s[10] = {}; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3953 | return EvaluateInPlace(Result.getArrayFiller(), Info, |
| 3954 | Subobject, E->getArrayFiller()) && Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3957 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3958 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3959 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3960 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3961 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3962 | bool HadZeroInit = !Result.isUninit(); |
| 3963 | if (!HadZeroInit) |
| 3964 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3965 | if (!Result.hasArrayFiller()) |
| 3966 | return true; |
| 3967 | |
| 3968 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3969 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3970 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3971 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3972 | if (HadZeroInit) |
| 3973 | return true; |
| 3974 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3975 | if (ZeroInit) { |
| 3976 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3977 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3978 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3979 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3982 | const CXXRecordDecl *RD = FD->getParent(); |
| 3983 | if (RD->isUnion()) |
| 3984 | Result.getArrayFiller() = APValue((FieldDecl*)0); |
| 3985 | else |
| 3986 | Result.getArrayFiller() = |
| 3987 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3988 | std::distance(RD->field_begin(), RD->field_end())); |
| 3989 | return true; |
| 3990 | } |
| 3991 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3992 | const FunctionDecl *Definition = 0; |
| 3993 | FD->getBody(Definition); |
| 3994 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3995 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3996 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3997 | |
| 3998 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3999 | // but sometimes does: |
| 4000 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 4001 | // S s[10]; |
| 4002 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4003 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4004 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4005 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4006 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4007 | if (!EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4008 | return false; |
| 4009 | } |
| 4010 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4011 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4012 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4013 | cast<CXXConstructorDecl>(Definition), |
| 4014 | Info, Result.getArrayFiller()); |
| 4015 | } |
| 4016 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4017 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4018 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4019 | // |
| 4020 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 4021 | // types and back in constant folding. Integer values are thus represented |
| 4022 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4023 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4024 | |
| 4025 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4026 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4027 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4028 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4029 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4030 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4031 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4032 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4033 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 4034 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4035 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4036 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4037 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4038 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4039 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4040 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4041 | return true; |
| 4042 | } |
| 4043 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4044 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4045 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 4046 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4047 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4048 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4049 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4050 | Result.getInt().setIsUnsigned( |
| 4051 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4052 | return true; |
| 4053 | } |
| 4054 | |
| 4055 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4056 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 4057 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4058 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4059 | return true; |
| 4060 | } |
| 4061 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4062 | bool Success(CharUnits Size, const Expr *E) { |
| 4063 | return Success(Size.getQuantity(), E); |
| 4064 | } |
| 4065 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4066 | bool Success(const CCValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4067 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 4068 | Result = V; |
| 4069 | return true; |
| 4070 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4071 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4074 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4075 | |
Argyrios Kyrtzidis | c1b66e6 | 2012-02-27 23:18:37 +0000 | [diff] [blame] | 4076 | // FIXME: See EvalInfo::IntExprEvaluatorDepth. |
| 4077 | bool Visit(const Expr *E) { |
| 4078 | SaveAndRestore<unsigned> Depth(Info.IntExprEvaluatorDepth, |
| 4079 | Info.IntExprEvaluatorDepth+1); |
| 4080 | const unsigned MaxDepth = 512; |
| 4081 | if (Depth.get() > MaxDepth) { |
| 4082 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 4083 | diag::err_intexpr_depth_limit_exceeded); |
| 4084 | return false; |
| 4085 | } |
| 4086 | |
| 4087 | return ExprEvaluatorBaseTy::Visit(E); |
| 4088 | } |
| 4089 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4090 | //===--------------------------------------------------------------------===// |
| 4091 | // Visitor Methods |
| 4092 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4094 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4095 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4096 | } |
| 4097 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4098 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4099 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4100 | |
| 4101 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 4102 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4103 | if (CheckReferencedDecl(E, E->getDecl())) |
| 4104 | return true; |
| 4105 | |
| 4106 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4107 | } |
| 4108 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4109 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4110 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4111 | return true; |
| 4112 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4113 | |
| 4114 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4117 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4118 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4119 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4120 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 4121 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4122 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4123 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4124 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4125 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4126 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4129 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 4130 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4131 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4134 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4135 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4138 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 4139 | return Success(E->getValue(), E); |
| 4140 | } |
| 4141 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4142 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 4143 | return Success(E->getValue(), E); |
| 4144 | } |
| 4145 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4146 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 4147 | return Success(E->getValue(), E); |
| 4148 | } |
| 4149 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4150 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4151 | return Success(E->getValue(), E); |
| 4152 | } |
| 4153 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4154 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4155 | bool VisitUnaryImag(const UnaryOperator *E); |
| 4156 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4157 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4158 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4160 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4161 | CharUnits GetAlignOfExpr(const Expr *E); |
| 4162 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4163 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4164 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4165 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4166 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4167 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4168 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4169 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 4170 | /// produce either the integer value or a pointer. |
| 4171 | /// |
| 4172 | /// GCC has a heinous extension which folds casts between pointer types and |
| 4173 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 4174 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 4175 | /// Some simple arithmetic on such values is supported (they are treated much |
| 4176 | /// like char*). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4177 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4178 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4179 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4180 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4181 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4182 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4183 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4184 | CCValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4185 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4186 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4187 | if (!Val.isInt()) { |
| 4188 | // FIXME: It would be better to produce the diagnostic for casting |
| 4189 | // a pointer to an integer. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4190 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4191 | return false; |
| 4192 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4193 | Result = Val.getInt(); |
| 4194 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4195 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4196 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4197 | /// Check whether the given declaration can be directly converted to an integral |
| 4198 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 4199 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4200 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4201 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4202 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4203 | // Check for signedness/width mismatches between E type and ECD value. |
| 4204 | bool SameSign = (ECD->getInitVal().isSigned() |
| 4205 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 4206 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 4207 | == Info.Ctx.getIntWidth(E->getType())); |
| 4208 | if (SameSign && SameWidth) |
| 4209 | return Success(ECD->getInitVal(), E); |
| 4210 | else { |
| 4211 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 4212 | // by computing a new value matching the type of E. |
| 4213 | llvm::APSInt Val = ECD->getInitVal(); |
| 4214 | if (!SameSign) |
| 4215 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 4216 | if (!SameWidth) |
| 4217 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 4218 | return Success(Val, E); |
| 4219 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4220 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4221 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4224 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 4225 | /// as GCC. |
| 4226 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 4227 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4228 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4229 | enum gcc_type_class { |
| 4230 | no_type_class = -1, |
| 4231 | void_type_class, integer_type_class, char_type_class, |
| 4232 | enumeral_type_class, boolean_type_class, |
| 4233 | pointer_type_class, reference_type_class, offset_type_class, |
| 4234 | real_type_class, complex_type_class, |
| 4235 | function_type_class, method_type_class, |
| 4236 | record_type_class, union_type_class, |
| 4237 | array_type_class, string_type_class, |
| 4238 | lang_type_class |
| 4239 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 | |
| 4241 | // 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] | 4242 | // ideal, however it is what gcc does. |
| 4243 | if (E->getNumArgs() == 0) |
| 4244 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4246 | QualType ArgTy = E->getArg(0)->getType(); |
| 4247 | if (ArgTy->isVoidType()) |
| 4248 | return void_type_class; |
| 4249 | else if (ArgTy->isEnumeralType()) |
| 4250 | return enumeral_type_class; |
| 4251 | else if (ArgTy->isBooleanType()) |
| 4252 | return boolean_type_class; |
| 4253 | else if (ArgTy->isCharType()) |
| 4254 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 4255 | else if (ArgTy->isIntegerType()) |
| 4256 | return integer_type_class; |
| 4257 | else if (ArgTy->isPointerType()) |
| 4258 | return pointer_type_class; |
| 4259 | else if (ArgTy->isReferenceType()) |
| 4260 | return reference_type_class; |
| 4261 | else if (ArgTy->isRealType()) |
| 4262 | return real_type_class; |
| 4263 | else if (ArgTy->isComplexType()) |
| 4264 | return complex_type_class; |
| 4265 | else if (ArgTy->isFunctionType()) |
| 4266 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 4267 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4268 | return record_type_class; |
| 4269 | else if (ArgTy->isUnionType()) |
| 4270 | return union_type_class; |
| 4271 | else if (ArgTy->isArrayType()) |
| 4272 | return array_type_class; |
| 4273 | else if (ArgTy->isUnionType()) |
| 4274 | return union_type_class; |
| 4275 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4276 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4279 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 4280 | /// __builtin_constant_p when applied to the given lvalue. |
| 4281 | /// |
| 4282 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 4283 | /// character of a string literal. |
| 4284 | template<typename LValue> |
| 4285 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
| 4286 | const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>(); |
| 4287 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 4288 | } |
| 4289 | |
| 4290 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 4291 | /// GCC as we can manage. |
| 4292 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 4293 | QualType ArgType = Arg->getType(); |
| 4294 | |
| 4295 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 4296 | // are not precisely documented, but are as follows: |
| 4297 | // |
| 4298 | // - If the operand is of integral, floating, complex or enumeration type, |
| 4299 | // and can be folded to a known value of that type, it returns 1. |
| 4300 | // - If the operand and can be folded to a pointer to the first character |
| 4301 | // of a string literal (or such a pointer cast to an integral type), it |
| 4302 | // returns 1. |
| 4303 | // |
| 4304 | // Otherwise, it returns 0. |
| 4305 | // |
| 4306 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 4307 | // its support for this does not currently work. |
| 4308 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4309 | Expr::EvalResult Result; |
| 4310 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 4311 | return false; |
| 4312 | |
| 4313 | APValue &V = Result.Val; |
| 4314 | if (V.getKind() == APValue::Int) |
| 4315 | return true; |
| 4316 | |
| 4317 | return EvaluateBuiltinConstantPForLValue(V); |
| 4318 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 4319 | return Arg->isEvaluatable(Ctx); |
| 4320 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 4321 | LValue LV; |
| 4322 | Expr::EvalStatus Status; |
| 4323 | EvalInfo Info(Ctx, Status); |
| 4324 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 4325 | : EvaluatePointer(Arg, LV, Info)) && |
| 4326 | !Status.HasSideEffects) |
| 4327 | return EvaluateBuiltinConstantPForLValue(LV); |
| 4328 | } |
| 4329 | |
| 4330 | // Anything else isn't considered to be sufficiently constant. |
| 4331 | return false; |
| 4332 | } |
| 4333 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4334 | /// Retrieves the "underlying object type" of the given expression, |
| 4335 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4336 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 4337 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 4338 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4339 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4340 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 4341 | if (isa<CompoundLiteralExpr>(E)) |
| 4342 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | return QualType(); |
| 4346 | } |
| 4347 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4348 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4349 | // TODO: Perhaps we should let LLVM lower this? |
| 4350 | LValue Base; |
| 4351 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 4352 | return false; |
| 4353 | |
| 4354 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4355 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4356 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4357 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4358 | if (T.isNull() || |
| 4359 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4360 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4361 | T->isVariablyModifiedType() || |
| 4362 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4363 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4364 | |
| 4365 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 4366 | CharUnits Offset = Base.getLValueOffset(); |
| 4367 | |
| 4368 | if (!Offset.isNegative() && Offset <= Size) |
| 4369 | Size -= Offset; |
| 4370 | else |
| 4371 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4372 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4375 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4376 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4377 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4378 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4379 | |
| 4380 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4381 | if (TryEvaluateBuiltinObjectSize(E)) |
| 4382 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4383 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 4384 | // If evaluating the argument has side-effects we can't determine |
| 4385 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4386 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4387 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 4388 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4389 | return Success(0, E); |
| 4390 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 4391 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4392 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4395 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4396 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4398 | case Builtin::BI__builtin_constant_p: |
| 4399 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 4400 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4401 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4402 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4403 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4404 | return Success(Operand, E); |
| 4405 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 4406 | |
| 4407 | case Builtin::BI__builtin_expect: |
| 4408 | return Visit(E->getArg(0)); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4410 | case Builtin::BIstrlen: |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4411 | // A call to strlen is not a constant expression. |
| 4412 | if (Info.getLangOpts().CPlusPlus0x) |
| 4413 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function) |
| 4414 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 4415 | else |
| 4416 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 4417 | // Fall through. |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4418 | case Builtin::BI__builtin_strlen: |
| 4419 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 4420 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4421 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4422 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 4423 | // The string literal may have embedded null characters. Find the first |
| 4424 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4425 | StringRef Str = S->getString(); |
| 4426 | StringRef::size_type Pos = Str.find(0); |
| 4427 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4428 | Str = Str.substr(0, Pos); |
| 4429 | |
| 4430 | return Success(Str.size(), E); |
| 4431 | } |
| 4432 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4433 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4434 | |
| 4435 | case Builtin::BI__atomic_is_lock_free: { |
| 4436 | APSInt SizeVal; |
| 4437 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 4438 | return false; |
| 4439 | |
| 4440 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 4441 | // of two less than the maximum inline atomic width, we know it is |
| 4442 | // lock-free. If the size isn't a power of two, or greater than the |
| 4443 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 4444 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 4445 | // the answer can only be determined at runtime; for example, 16-byte |
| 4446 | // atomics have lock-free implementations on some, but not all, |
| 4447 | // x86-64 processors. |
| 4448 | |
| 4449 | // Check power-of-two. |
| 4450 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 4451 | if (!Size.isPowerOfTwo()) |
| 4452 | #if 0 |
| 4453 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 4454 | // settles. |
| 4455 | return Success(0, E); |
| 4456 | #else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4457 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4458 | #endif |
| 4459 | |
| 4460 | #if 0 |
| 4461 | // Check against promotion width. |
| 4462 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 4463 | // settles. |
| 4464 | unsigned PromoteWidthBits = |
| 4465 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 4466 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 4467 | return Success(0, E); |
| 4468 | #endif |
| 4469 | |
| 4470 | // Check against inlining width. |
| 4471 | unsigned InlineWidthBits = |
| 4472 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 4473 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 4474 | return Success(1, E); |
| 4475 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4476 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4477 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4478 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4479 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4480 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4481 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 4482 | if (!A.getLValueBase()) |
| 4483 | return !B.getLValueBase(); |
| 4484 | if (!B.getLValueBase()) |
| 4485 | return false; |
| 4486 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4487 | if (A.getLValueBase().getOpaqueValue() != |
| 4488 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4489 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 4490 | if (!ADecl) |
| 4491 | return false; |
| 4492 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4493 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4494 | return false; |
| 4495 | } |
| 4496 | |
| 4497 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4498 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4501 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 4502 | /// bits, and check for overflow in the original type (if that type was not an |
| 4503 | /// unsigned type). |
| 4504 | template<typename Operation> |
| 4505 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 4506 | const APSInt &LHS, const APSInt &RHS, |
| 4507 | unsigned BitWidth, Operation Op) { |
| 4508 | if (LHS.isUnsigned()) |
| 4509 | return Op(LHS, RHS); |
| 4510 | |
| 4511 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 4512 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 4513 | if (Result.extend(BitWidth) != Value) |
| 4514 | HandleOverflow(Info, E, Value, E->getType()); |
| 4515 | return Result; |
| 4516 | } |
| 4517 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4518 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4519 | if (E->isAssignmentOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4520 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4521 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4522 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4523 | VisitIgnoredValue(E->getLHS()); |
| 4524 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4527 | if (E->isLogicalOp()) { |
| 4528 | // These need to be handled specially because the operands aren't |
| 4529 | // necessarily integral nor evaluated. |
| 4530 | bool lhsResult, rhsResult; |
| 4531 | |
| 4532 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
| 4533 | // We were able to evaluate the LHS, see if we can get away with not |
| 4534 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
| 4535 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
| 4536 | return Success(lhsResult, E); |
| 4537 | |
| 4538 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
| 4539 | if (E->getOpcode() == BO_LOr) |
| 4540 | return Success(lhsResult || rhsResult, E); |
| 4541 | else |
| 4542 | return Success(lhsResult && rhsResult, E); |
| 4543 | } |
| 4544 | } else { |
| 4545 | // Since we weren't able to evaluate the left hand side, it |
| 4546 | // must have had side effects. |
| 4547 | Info.EvalStatus.HasSideEffects = true; |
| 4548 | |
| 4549 | // Suppress diagnostics from this arm. |
| 4550 | SpeculativeEvaluationRAII Speculative(Info); |
| 4551 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
| 4552 | // We can't evaluate the LHS; however, sometimes the result |
| 4553 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 4554 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
| 4555 | return Success(rhsResult, E); |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | return false; |
| 4560 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4561 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4562 | QualType LHSTy = E->getLHS()->getType(); |
| 4563 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4564 | |
| 4565 | if (LHSTy->isAnyComplexType()) { |
| 4566 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4567 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4568 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4569 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 4570 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4571 | return false; |
| 4572 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4573 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4574 | return false; |
| 4575 | |
| 4576 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4578 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4579 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4580 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 4581 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4582 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4583 | return Success((CR_r == APFloat::cmpEqual && |
| 4584 | CR_i == APFloat::cmpEqual), E); |
| 4585 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4586 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4587 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4589 | CR_r == APFloat::cmpLessThan || |
| 4590 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4592 | CR_i == APFloat::cmpLessThan || |
| 4593 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4594 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4595 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4596 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4597 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 4598 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 4599 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4600 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4601 | "Invalid compex comparison."); |
| 4602 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 4603 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 4604 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4608 | if (LHSTy->isRealFloatingType() && |
| 4609 | RHSTy->isRealFloatingType()) { |
| 4610 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4612 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 4613 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4614 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4616 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4617 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4618 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4619 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 4620 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4621 | switch (E->getOpcode()) { |
| 4622 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4623 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4624 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4625 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4626 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4627 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4628 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4629 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4630 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4632 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4633 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4634 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4635 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4637 | || CR == APFloat::cmpLessThan |
| 4638 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4639 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4642 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4643 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4644 | LValue LHSValue, RHSValue; |
| 4645 | |
| 4646 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 4647 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4648 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4649 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4650 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4651 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4652 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4653 | // Reject differing bases from the normal codepath; we special-case |
| 4654 | // comparisons to null. |
| 4655 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4656 | if (E->getOpcode() == BO_Sub) { |
| 4657 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4658 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 4659 | return false; |
| 4660 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4661 | const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4662 | if (!LHSExpr || !RHSExpr) |
| 4663 | return false; |
| 4664 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4665 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4666 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4667 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4668 | // Make sure both labels come from the same function. |
| 4669 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4670 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4671 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4672 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4673 | return true; |
| 4674 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4675 | // Inequalities and subtractions between unrelated pointers have |
| 4676 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4677 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4678 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4679 | // A constant address may compare equal to the address of a symbol. |
| 4680 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4681 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4682 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 4683 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4684 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4685 | // It's implementation-defined whether distinct literals will have |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4686 | // distinct addresses. In clang, the result of such a comparison is |
| 4687 | // unspecified, so it is not a constant expression. However, we do know |
| 4688 | // that the address of a literal will be non-null. |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 4689 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 4690 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4691 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4692 | // We can't tell whether weak symbols will end up pointing to the same |
| 4693 | // object. |
| 4694 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4695 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4696 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4697 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 4698 | // lead to inconsistent results for comparisons involving the address |
| 4699 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4700 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4701 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4702 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4703 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 4704 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 4705 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4706 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 4707 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 4708 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4709 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4710 | // C++11 [expr.add]p6: |
| 4711 | // Unless both pointers point to elements of the same array object, or |
| 4712 | // one past the last element of the array object, the behavior is |
| 4713 | // undefined. |
| 4714 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 4715 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 4716 | LHSDesignator, RHSDesignator)) |
| 4717 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 4718 | |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 4719 | QualType Type = E->getLHS()->getType(); |
| 4720 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4721 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4722 | CharUnits ElementSize; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4723 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4724 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4725 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4726 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 4727 | // and produce incorrect results when it overflows. Such behavior |
| 4728 | // appears to be non-conforming, but is common, so perhaps we should |
| 4729 | // assume the standard intended for such cases to be undefined behavior |
| 4730 | // and check for them. |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4731 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4732 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 4733 | // overflow in the final conversion to ptrdiff_t. |
| 4734 | APSInt LHS( |
| 4735 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 4736 | APSInt RHS( |
| 4737 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 4738 | APSInt ElemSize( |
| 4739 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 4740 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 4741 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 4742 | |
| 4743 | if (Result.extend(65) != TrueResult) |
| 4744 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 4745 | return Success(Result, E); |
| 4746 | } |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 4747 | |
| 4748 | // C++11 [expr.rel]p3: |
| 4749 | // Pointers to void (after pointer conversions) can be compared, with a |
| 4750 | // result defined as follows: If both pointers represent the same |
| 4751 | // address or are both the null pointer value, the result is true if the |
| 4752 | // operator is <= or >= and false otherwise; otherwise the result is |
| 4753 | // unspecified. |
| 4754 | // We interpret this as applying to pointers to *cv* void. |
| 4755 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4756 | E->isRelationalOp()) |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 4757 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 4758 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4759 | // C++11 [expr.rel]p2: |
| 4760 | // - If two pointers point to non-static data members of the same object, |
| 4761 | // or to subobjects or array elements fo such members, recursively, the |
| 4762 | // pointer to the later declared member compares greater provided the |
| 4763 | // two members have the same access control and provided their class is |
| 4764 | // not a union. |
| 4765 | // [...] |
| 4766 | // - Otherwise pointer comparisons are unspecified. |
| 4767 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 4768 | E->isRelationalOp()) { |
| 4769 | bool WasArrayIndex; |
| 4770 | unsigned Mismatch = |
| 4771 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 4772 | RHSDesignator, WasArrayIndex); |
| 4773 | // At the point where the designators diverge, the comparison has a |
| 4774 | // specified value if: |
| 4775 | // - we are comparing array indices |
| 4776 | // - we are comparing fields of a union, or fields with the same access |
| 4777 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 4778 | // constant expression. |
| 4779 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 4780 | Mismatch < RHSDesignator.Entries.size()) { |
| 4781 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 4782 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 4783 | if (!LF && !RF) |
| 4784 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 4785 | else if (!LF) |
| 4786 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 4787 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 4788 | << RF->getParent() << RF; |
| 4789 | else if (!RF) |
| 4790 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 4791 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 4792 | << LF->getParent() << LF; |
| 4793 | else if (!LF->getParent()->isUnion() && |
| 4794 | LF->getAccess() != RF->getAccess()) |
| 4795 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 4796 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 4797 | << LF->getParent(); |
| 4798 | } |
| 4799 | } |
| 4800 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4801 | switch (E->getOpcode()) { |
| 4802 | default: llvm_unreachable("missing comparison operator"); |
| 4803 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 4804 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 4805 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 4806 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 4807 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 4808 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4809 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4810 | } |
| 4811 | } |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4812 | |
| 4813 | if (LHSTy->isMemberPointerType()) { |
| 4814 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 4815 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 4816 | |
| 4817 | MemberPtr LHSValue, RHSValue; |
| 4818 | |
| 4819 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 4820 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 4821 | return false; |
| 4822 | |
| 4823 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 4824 | return false; |
| 4825 | |
| 4826 | // C++11 [expr.eq]p2: |
| 4827 | // If both operands are null, they compare equal. Otherwise if only one is |
| 4828 | // null, they compare unequal. |
| 4829 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 4830 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 4831 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 4832 | } |
| 4833 | |
| 4834 | // Otherwise if either is a pointer to a virtual member function, the |
| 4835 | // result is unspecified. |
| 4836 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 4837 | if (MD->isVirtual()) |
| 4838 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 4839 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 4840 | if (MD->isVirtual()) |
| 4841 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 4842 | |
| 4843 | // Otherwise they compare equal if and only if they would refer to the |
| 4844 | // same member of the same most derived object or the same subobject if |
| 4845 | // they were dereferenced with a hypothetical object of the associated |
| 4846 | // class type. |
| 4847 | bool Equal = LHSValue == RHSValue; |
| 4848 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 4849 | } |
| 4850 | |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 4851 | if (LHSTy->isNullPtrType()) { |
| 4852 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 4853 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 4854 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 4855 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 4856 | // false otherwise. |
| 4857 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 4858 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 4859 | } |
| 4860 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4861 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 4862 | !RHSTy->isIntegralOrEnumerationType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4863 | // We can't continue from here for non-integral types. |
| 4864 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4867 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4868 | CCValue LHSVal; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4869 | |
| 4870 | bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info); |
| 4871 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4872 | return false; |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 4873 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4874 | if (!Visit(E->getRHS()) || !LHSOK) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4875 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4876 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4877 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4880 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 4881 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 4882 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4883 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4884 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4885 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4886 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 4887 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4888 | return true; |
| 4889 | } |
| 4890 | |
| 4891 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4892 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4893 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4894 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 4895 | LHSVal.getInt().getZExtValue()); |
| 4896 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4897 | return true; |
| 4898 | } |
| 4899 | |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4900 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 4901 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4902 | if (!LHSVal.getLValueOffset().isZero() || |
| 4903 | !RHSVal.getLValueOffset().isZero()) |
| 4904 | return false; |
| 4905 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4906 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4907 | if (!LHSExpr || !RHSExpr) |
| 4908 | return false; |
| 4909 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4910 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4911 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4912 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4913 | // Make sure both labels come from the same function. |
| 4914 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4915 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4916 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4917 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4918 | return true; |
| 4919 | } |
| 4920 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4921 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4922 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4923 | return Error(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4924 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4925 | APSInt &LHS = LHSVal.getInt(); |
| 4926 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4927 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4928 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4929 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4930 | return Error(E); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4931 | case BO_Mul: |
| 4932 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4933 | LHS.getBitWidth() * 2, |
| 4934 | std::multiplies<APSInt>()), E); |
| 4935 | case BO_Add: |
| 4936 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4937 | LHS.getBitWidth() + 1, |
| 4938 | std::plus<APSInt>()), E); |
| 4939 | case BO_Sub: |
| 4940 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4941 | LHS.getBitWidth() + 1, |
| 4942 | std::minus<APSInt>()), E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4943 | case BO_And: return Success(LHS & RHS, E); |
| 4944 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 4945 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4946 | case BO_Div: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4947 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 4948 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4949 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | 3df6130 | 2012-01-31 23:24:19 +0000 | [diff] [blame] | 4950 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not |
| 4951 | // actually undefined behavior in C++11 due to a language defect. |
| 4952 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 4953 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 4954 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 4955 | return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4956 | case BO_Shl: { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4957 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4958 | // shift is not a constant expression. |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4959 | if (RHS.isSigned() && RHS.isNegative()) { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4960 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4961 | RHS = -RHS; |
| 4962 | goto shift_right; |
| 4963 | } |
| 4964 | |
| 4965 | shift_left: |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4966 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4967 | // shifted type. |
| 4968 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4969 | if (SA != RHS) { |
| 4970 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4971 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4972 | } else if (LHS.isSigned()) { |
| 4973 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
Richard Smith | 925d8e7 | 2012-02-08 06:14:53 +0000 | [diff] [blame] | 4974 | // operand, and must not overflow the corresponding unsigned type. |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4975 | if (LHS.isNegative()) |
| 4976 | CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
Richard Smith | 925d8e7 | 2012-02-08 06:14:53 +0000 | [diff] [blame] | 4977 | else if (LHS.countLeadingZeros() < SA) |
| 4978 | CCEDiag(E, diag::note_constexpr_lshift_discards); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4981 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4982 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4983 | case BO_Shr: { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4984 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4985 | // shift is not a constant expression. |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4986 | if (RHS.isSigned() && RHS.isNegative()) { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4987 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4988 | RHS = -RHS; |
| 4989 | goto shift_left; |
| 4990 | } |
| 4991 | |
| 4992 | shift_right: |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4993 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4994 | // shifted type. |
| 4995 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4996 | if (SA != RHS) |
| 4997 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4998 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4999 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5000 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5003 | case BO_LT: return Success(LHS < RHS, E); |
| 5004 | case BO_GT: return Success(LHS > RHS, E); |
| 5005 | case BO_LE: return Success(LHS <= RHS, E); |
| 5006 | case BO_GE: return Success(LHS >= RHS, E); |
| 5007 | case BO_EQ: return Success(LHS == RHS, E); |
| 5008 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 5009 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5012 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 5013 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 5014 | // result shall be the alignment of the referenced type." |
| 5015 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5016 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 5017 | |
| 5018 | // __alignof is defined to return the preferred alignment. |
| 5019 | return Info.Ctx.toCharUnitsFromBits( |
| 5020 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5023 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5024 | E = E->IgnoreParens(); |
| 5025 | |
| 5026 | // 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] | 5027 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5028 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5029 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5030 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5031 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5032 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5033 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5034 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5035 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5036 | return GetAlignOfType(E->getType()); |
| 5037 | } |
| 5038 | |
| 5039 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5040 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 5041 | /// a result as the expression's type. |
| 5042 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 5043 | const UnaryExprOrTypeTraitExpr *E) { |
| 5044 | switch(E->getKind()) { |
| 5045 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5046 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5047 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5048 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5049 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5050 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5051 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5052 | case UETT_VecStep: { |
| 5053 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5054 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5055 | if (Ty->isVectorType()) { |
| 5056 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5057 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5058 | // The vec_step built-in functions that take a 3-component |
| 5059 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 5060 | if (n == 3) |
| 5061 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 5062 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5063 | return Success(n, E); |
| 5064 | } else |
| 5065 | return Success(1, E); |
| 5066 | } |
| 5067 | |
| 5068 | case UETT_SizeOf: { |
| 5069 | QualType SrcTy = E->getTypeOfArgument(); |
| 5070 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 5071 | // the result is the size of the referenced type." |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5072 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 5073 | SrcTy = Ref->getPointeeType(); |
| 5074 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5075 | CharUnits Sizeof; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5076 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5077 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5078 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5085 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5086 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5087 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5088 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5089 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5090 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5091 | for (unsigned i = 0; i != n; ++i) { |
| 5092 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 5093 | switch (ON.getKind()) { |
| 5094 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5095 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5096 | APSInt IdxResult; |
| 5097 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 5098 | return false; |
| 5099 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 5100 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5101 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5102 | CurrentType = AT->getElementType(); |
| 5103 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 5104 | Result += IdxResult.getSExtValue() * ElementSize; |
| 5105 | break; |
| 5106 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5107 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5108 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 5109 | FieldDecl *MemberDecl = ON.getField(); |
| 5110 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5111 | if (!RT) |
| 5112 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5113 | RecordDecl *RD = RT->getDecl(); |
| 5114 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 5115 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5116 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 5117 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5118 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 5119 | break; |
| 5120 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5121 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5122 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 5123 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5125 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 5126 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 5127 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5128 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5129 | |
| 5130 | // Find the layout of the class whose base we are looking into. |
| 5131 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5132 | if (!RT) |
| 5133 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5134 | RecordDecl *RD = RT->getDecl(); |
| 5135 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 5136 | |
| 5137 | // Find the base class itself. |
| 5138 | CurrentType = BaseSpec->getType(); |
| 5139 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 5140 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5141 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 5144 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5145 | break; |
| 5146 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5147 | } |
| 5148 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5149 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5152 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5153 | switch (E->getOpcode()) { |
| 5154 | default: |
| 5155 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 5156 | // See C99 6.6p3. |
| 5157 | return Error(E); |
| 5158 | case UO_Extension: |
| 5159 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 5160 | // If so, we could clear the diagnostic ID. |
| 5161 | return Visit(E->getSubExpr()); |
| 5162 | case UO_Plus: |
| 5163 | // The result is just the value. |
| 5164 | return Visit(E->getSubExpr()); |
| 5165 | case UO_Minus: { |
| 5166 | if (!Visit(E->getSubExpr())) |
| 5167 | return false; |
| 5168 | if (!Result.isInt()) return Error(E); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 5169 | const APSInt &Value = Result.getInt(); |
| 5170 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 5171 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 5172 | E->getType()); |
| 5173 | return Success(-Value, E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5174 | } |
| 5175 | case UO_Not: { |
| 5176 | if (!Visit(E->getSubExpr())) |
| 5177 | return false; |
| 5178 | if (!Result.isInt()) return Error(E); |
| 5179 | return Success(~Result.getInt(), E); |
| 5180 | } |
| 5181 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5182 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5183 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5184 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5185 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5186 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5187 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5189 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5190 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 5191 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5192 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5193 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5194 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 5195 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5196 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5197 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5198 | case CK_BaseToDerived: |
| 5199 | case CK_DerivedToBase: |
| 5200 | case CK_UncheckedDerivedToBase: |
| 5201 | case CK_Dynamic: |
| 5202 | case CK_ToUnion: |
| 5203 | case CK_ArrayToPointerDecay: |
| 5204 | case CK_FunctionToPointerDecay: |
| 5205 | case CK_NullToPointer: |
| 5206 | case CK_NullToMemberPointer: |
| 5207 | case CK_BaseToDerivedMemberPointer: |
| 5208 | case CK_DerivedToBaseMemberPointer: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5209 | case CK_ReinterpretMemberPointer: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5210 | case CK_ConstructorConversion: |
| 5211 | case CK_IntegralToPointer: |
| 5212 | case CK_ToVoid: |
| 5213 | case CK_VectorSplat: |
| 5214 | case CK_IntegralToFloating: |
| 5215 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5216 | case CK_CPointerToObjCPointerCast: |
| 5217 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5218 | case CK_AnyPointerToBlockPointerCast: |
| 5219 | case CK_ObjCObjectLValueCast: |
| 5220 | case CK_FloatingRealToComplex: |
| 5221 | case CK_FloatingComplexToReal: |
| 5222 | case CK_FloatingComplexCast: |
| 5223 | case CK_FloatingComplexToIntegralComplex: |
| 5224 | case CK_IntegralRealToComplex: |
| 5225 | case CK_IntegralComplexCast: |
| 5226 | case CK_IntegralComplexToFloatingComplex: |
| 5227 | llvm_unreachable("invalid cast kind for integral value"); |
| 5228 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 5229 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5230 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5231 | case CK_LValueBitCast: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5232 | case CK_ARCProduceObject: |
| 5233 | case CK_ARCConsumeObject: |
| 5234 | case CK_ARCReclaimReturnedObject: |
| 5235 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5236 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5237 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5238 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 5239 | case CK_UserDefinedConversion: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5240 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5241 | case CK_AtomicToNonAtomic: |
| 5242 | case CK_NonAtomicToAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5243 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5244 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5245 | |
| 5246 | case CK_MemberPointerToBoolean: |
| 5247 | case CK_PointerToBoolean: |
| 5248 | case CK_IntegralToBoolean: |
| 5249 | case CK_FloatingToBoolean: |
| 5250 | case CK_FloatingComplexToBoolean: |
| 5251 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5252 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5253 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5254 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5255 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5258 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5259 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5260 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 5261 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5262 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5263 | // Allow casts of address-of-label differences if they are no-ops |
| 5264 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 5265 | // be constant-evaluatable except in some narrow cases which are hard |
| 5266 | // to detect here. We let it through on the assumption the user knows |
| 5267 | // what they are doing.) |
| 5268 | if (Result.isAddrLabelDiff()) |
| 5269 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5270 | // Only allow casts of lvalues if they are lossless. |
| 5271 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 5272 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5273 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5274 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 5275 | Result.getInt()), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5278 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5279 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5280 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5281 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 5282 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5283 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5284 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5285 | if (LV.getLValueBase()) { |
| 5286 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5287 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 5288 | // narrowing back down to pointer width in subsequent integral casts. |
| 5289 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5290 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5291 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5292 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 5293 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5294 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5295 | return true; |
| 5296 | } |
| 5297 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 5298 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 5299 | SrcType); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5300 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5301 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5302 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5303 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5304 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5305 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 5306 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5307 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5308 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5309 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5310 | case CK_FloatingToIntegral: { |
| 5311 | APFloat F(0.0); |
| 5312 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 5313 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5314 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5315 | APSInt Value; |
| 5316 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 5317 | return false; |
| 5318 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5319 | } |
| 5320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5322 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5323 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5324 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5325 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5326 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5327 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5328 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5329 | return false; |
| 5330 | if (!LV.isComplexInt()) |
| 5331 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5332 | return Success(LV.getComplexIntReal(), E); |
| 5333 | } |
| 5334 | |
| 5335 | return Visit(E->getSubExpr()); |
| 5336 | } |
| 5337 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5338 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5339 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5340 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5341 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5342 | return false; |
| 5343 | if (!LV.isComplexInt()) |
| 5344 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5345 | return Success(LV.getComplexIntImag(), E); |
| 5346 | } |
| 5347 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5348 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5349 | return Success(0, E); |
| 5350 | } |
| 5351 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5352 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 5353 | return Success(E->getPackLength(), E); |
| 5354 | } |
| 5355 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5356 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 5357 | return Success(E->getValue(), E); |
| 5358 | } |
| 5359 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5360 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5361 | // Float Evaluation |
| 5362 | //===----------------------------------------------------------------------===// |
| 5363 | |
| 5364 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5365 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5366 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5367 | APFloat &Result; |
| 5368 | public: |
| 5369 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5370 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5371 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5372 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5373 | Result = V.getFloat(); |
| 5374 | return true; |
| 5375 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5376 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5377 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5378 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 5379 | return true; |
| 5380 | } |
| 5381 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5382 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5383 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5384 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5385 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 5386 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5387 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5388 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5389 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5390 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5391 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5392 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5393 | }; |
| 5394 | } // end anonymous namespace |
| 5395 | |
| 5396 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5397 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5398 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5401 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5402 | QualType ResultTy, |
| 5403 | const Expr *Arg, |
| 5404 | bool SNaN, |
| 5405 | llvm::APFloat &Result) { |
| 5406 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 5407 | if (!S) return false; |
| 5408 | |
| 5409 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 5410 | |
| 5411 | llvm::APInt fill; |
| 5412 | |
| 5413 | // Treat empty strings as if they were zero. |
| 5414 | if (S->getString().empty()) |
| 5415 | fill = llvm::APInt(32, 0); |
| 5416 | else if (S->getString().getAsInteger(0, fill)) |
| 5417 | return false; |
| 5418 | |
| 5419 | if (SNaN) |
| 5420 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 5421 | else |
| 5422 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 5423 | return true; |
| 5424 | } |
| 5425 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5426 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5427 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5428 | default: |
| 5429 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5430 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5431 | case Builtin::BI__builtin_huge_val: |
| 5432 | case Builtin::BI__builtin_huge_valf: |
| 5433 | case Builtin::BI__builtin_huge_vall: |
| 5434 | case Builtin::BI__builtin_inf: |
| 5435 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5436 | case Builtin::BI__builtin_infl: { |
| 5437 | const llvm::fltSemantics &Sem = |
| 5438 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 5439 | Result = llvm::APFloat::getInf(Sem); |
| 5440 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5443 | case Builtin::BI__builtin_nans: |
| 5444 | case Builtin::BI__builtin_nansf: |
| 5445 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5446 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5447 | true, Result)) |
| 5448 | return Error(E); |
| 5449 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5450 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5451 | case Builtin::BI__builtin_nan: |
| 5452 | case Builtin::BI__builtin_nanf: |
| 5453 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 5454 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5455 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5456 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5457 | false, Result)) |
| 5458 | return Error(E); |
| 5459 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5460 | |
| 5461 | case Builtin::BI__builtin_fabs: |
| 5462 | case Builtin::BI__builtin_fabsf: |
| 5463 | case Builtin::BI__builtin_fabsl: |
| 5464 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 5465 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5467 | if (Result.isNegative()) |
| 5468 | Result.changeSign(); |
| 5469 | return true; |
| 5470 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | case Builtin::BI__builtin_copysign: |
| 5472 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5473 | case Builtin::BI__builtin_copysignl: { |
| 5474 | APFloat RHS(0.); |
| 5475 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 5476 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 5477 | return false; |
| 5478 | Result.copySign(RHS); |
| 5479 | return true; |
| 5480 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5484 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5485 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5486 | ComplexValue CV; |
| 5487 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5488 | return false; |
| 5489 | Result = CV.FloatReal; |
| 5490 | return true; |
| 5491 | } |
| 5492 | |
| 5493 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5497 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5498 | ComplexValue CV; |
| 5499 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5500 | return false; |
| 5501 | Result = CV.FloatImag; |
| 5502 | return true; |
| 5503 | } |
| 5504 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5505 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5506 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 5507 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5508 | return true; |
| 5509 | } |
| 5510 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5511 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5512 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5513 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5514 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5515 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5516 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5517 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 5518 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5519 | Result.changeSign(); |
| 5520 | return true; |
| 5521 | } |
| 5522 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5523 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5524 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5525 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 5526 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 5527 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5528 | APFloat RHS(0.0); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5529 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 5530 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5531 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5532 | if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5533 | return false; |
| 5534 | |
| 5535 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5536 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5537 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5538 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5539 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5540 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5541 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5542 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5543 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5544 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5545 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5546 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5547 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5548 | break; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5549 | } |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5550 | |
| 5551 | if (Result.isInfinity() || Result.isNaN()) |
| 5552 | CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); |
| 5553 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 5557 | Result = E->getValue(); |
| 5558 | return true; |
| 5559 | } |
| 5560 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5561 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5562 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5564 | switch (E->getCastKind()) { |
| 5565 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5566 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5567 | |
| 5568 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5569 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5570 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 5571 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 5572 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5573 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5574 | |
| 5575 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5576 | if (!Visit(SubExpr)) |
| 5577 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5578 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 5579 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5580 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5581 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5582 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5583 | ComplexValue V; |
| 5584 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 5585 | return false; |
| 5586 | Result = V.getComplexFloatReal(); |
| 5587 | return true; |
| 5588 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5589 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5592 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5593 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5594 | //===----------------------------------------------------------------------===// |
| 5595 | |
| 5596 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5597 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5598 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5599 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5601 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5602 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5603 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 5604 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5605 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5606 | Result.setFrom(V); |
| 5607 | return true; |
| 5608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5609 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5610 | bool ZeroInitialization(const Expr *E); |
| 5611 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5612 | //===--------------------------------------------------------------------===// |
| 5613 | // Visitor Methods |
| 5614 | //===--------------------------------------------------------------------===// |
| 5615 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5616 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5617 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5618 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5619 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5620 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5621 | }; |
| 5622 | } // end anonymous namespace |
| 5623 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5624 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 5625 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5626 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5627 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5630 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 5631 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5632 | if (ElemTy->isRealFloatingType()) { |
| 5633 | Result.makeComplexFloat(); |
| 5634 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 5635 | Result.FloatReal = Zero; |
| 5636 | Result.FloatImag = Zero; |
| 5637 | } else { |
| 5638 | Result.makeComplexInt(); |
| 5639 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 5640 | Result.IntReal = Zero; |
| 5641 | Result.IntImag = Zero; |
| 5642 | } |
| 5643 | return true; |
| 5644 | } |
| 5645 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5646 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 5647 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5648 | |
| 5649 | if (SubExpr->getType()->isRealFloatingType()) { |
| 5650 | Result.makeComplexFloat(); |
| 5651 | APFloat &Imag = Result.FloatImag; |
| 5652 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 5653 | return false; |
| 5654 | |
| 5655 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 5656 | return true; |
| 5657 | } else { |
| 5658 | assert(SubExpr->getType()->isIntegerType() && |
| 5659 | "Unexpected imaginary literal."); |
| 5660 | |
| 5661 | Result.makeComplexInt(); |
| 5662 | APSInt &Imag = Result.IntImag; |
| 5663 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 5664 | return false; |
| 5665 | |
| 5666 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 5667 | return true; |
| 5668 | } |
| 5669 | } |
| 5670 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5671 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5672 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5673 | switch (E->getCastKind()) { |
| 5674 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5675 | case CK_BaseToDerived: |
| 5676 | case CK_DerivedToBase: |
| 5677 | case CK_UncheckedDerivedToBase: |
| 5678 | case CK_Dynamic: |
| 5679 | case CK_ToUnion: |
| 5680 | case CK_ArrayToPointerDecay: |
| 5681 | case CK_FunctionToPointerDecay: |
| 5682 | case CK_NullToPointer: |
| 5683 | case CK_NullToMemberPointer: |
| 5684 | case CK_BaseToDerivedMemberPointer: |
| 5685 | case CK_DerivedToBaseMemberPointer: |
| 5686 | case CK_MemberPointerToBoolean: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5687 | case CK_ReinterpretMemberPointer: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5688 | case CK_ConstructorConversion: |
| 5689 | case CK_IntegralToPointer: |
| 5690 | case CK_PointerToIntegral: |
| 5691 | case CK_PointerToBoolean: |
| 5692 | case CK_ToVoid: |
| 5693 | case CK_VectorSplat: |
| 5694 | case CK_IntegralCast: |
| 5695 | case CK_IntegralToBoolean: |
| 5696 | case CK_IntegralToFloating: |
| 5697 | case CK_FloatingToIntegral: |
| 5698 | case CK_FloatingToBoolean: |
| 5699 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5700 | case CK_CPointerToObjCPointerCast: |
| 5701 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5702 | case CK_AnyPointerToBlockPointerCast: |
| 5703 | case CK_ObjCObjectLValueCast: |
| 5704 | case CK_FloatingComplexToReal: |
| 5705 | case CK_FloatingComplexToBoolean: |
| 5706 | case CK_IntegralComplexToReal: |
| 5707 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5708 | case CK_ARCProduceObject: |
| 5709 | case CK_ARCConsumeObject: |
| 5710 | case CK_ARCReclaimReturnedObject: |
| 5711 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5712 | case CK_CopyAndAutoreleaseBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5713 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 5714 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5715 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5716 | case CK_AtomicToNonAtomic: |
| 5717 | case CK_NonAtomicToAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5718 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5719 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5720 | |
| 5721 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5722 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5723 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5724 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5725 | |
| 5726 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5727 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5728 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5729 | return false; |
| 5730 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5731 | Result.makeComplexFloat(); |
| 5732 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 5733 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5736 | case CK_FloatingComplexCast: { |
| 5737 | if (!Visit(E->getSubExpr())) |
| 5738 | return false; |
| 5739 | |
| 5740 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5741 | QualType From |
| 5742 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5743 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5744 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 5745 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | case CK_FloatingComplexToIntegralComplex: { |
| 5749 | if (!Visit(E->getSubExpr())) |
| 5750 | return false; |
| 5751 | |
| 5752 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5753 | QualType From |
| 5754 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5755 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5756 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 5757 | To, Result.IntReal) && |
| 5758 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 5759 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | case CK_IntegralRealToComplex: { |
| 5763 | APSInt &Real = Result.IntReal; |
| 5764 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 5765 | return false; |
| 5766 | |
| 5767 | Result.makeComplexInt(); |
| 5768 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 5769 | return true; |
| 5770 | } |
| 5771 | |
| 5772 | case CK_IntegralComplexCast: { |
| 5773 | if (!Visit(E->getSubExpr())) |
| 5774 | return false; |
| 5775 | |
| 5776 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5777 | QualType From |
| 5778 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5779 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5780 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 5781 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5782 | return true; |
| 5783 | } |
| 5784 | |
| 5785 | case CK_IntegralComplexToFloatingComplex: { |
| 5786 | if (!Visit(E->getSubExpr())) |
| 5787 | return false; |
| 5788 | |
| 5789 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5790 | QualType From |
| 5791 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5792 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5793 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 5794 | To, Result.FloatReal) && |
| 5795 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 5796 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5797 | } |
| 5798 | } |
| 5799 | |
| 5800 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5803 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5804 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 5805 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5806 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5807 | bool LHSOK = Visit(E->getLHS()); |
| 5808 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5809 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5810 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5811 | ComplexValue RHS; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5812 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5813 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5814 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5815 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 5816 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5817 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5818 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5819 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5820 | if (Result.isComplexFloat()) { |
| 5821 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 5822 | APFloat::rmNearestTiesToEven); |
| 5823 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 5824 | APFloat::rmNearestTiesToEven); |
| 5825 | } else { |
| 5826 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 5827 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 5828 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5829 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5830 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5831 | if (Result.isComplexFloat()) { |
| 5832 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 5833 | APFloat::rmNearestTiesToEven); |
| 5834 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 5835 | APFloat::rmNearestTiesToEven); |
| 5836 | } else { |
| 5837 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 5838 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 5839 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5840 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5841 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5842 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5843 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5844 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5845 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5846 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5847 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5848 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5849 | APFloat Tmp = LHS_r; |
| 5850 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5851 | Result.getComplexFloatReal() = Tmp; |
| 5852 | Tmp = LHS_i; |
| 5853 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5854 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5855 | |
| 5856 | Tmp = LHS_r; |
| 5857 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5858 | Result.getComplexFloatImag() = Tmp; |
| 5859 | Tmp = LHS_i; |
| 5860 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5861 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 5862 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5863 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5864 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5865 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 5866 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5868 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 5869 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 5870 | } |
| 5871 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5872 | case BO_Div: |
| 5873 | if (Result.isComplexFloat()) { |
| 5874 | ComplexValue LHS = Result; |
| 5875 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5876 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5877 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5878 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 5879 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 5880 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 5881 | |
| 5882 | APFloat Den = RHS_r; |
| 5883 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5884 | APFloat Tmp = RHS_i; |
| 5885 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5886 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5887 | |
| 5888 | Res_r = LHS_r; |
| 5889 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5890 | Tmp = LHS_i; |
| 5891 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5892 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5893 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 5894 | |
| 5895 | Res_i = LHS_i; |
| 5896 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5897 | Tmp = LHS_r; |
| 5898 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5899 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5900 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 5901 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5902 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 5903 | return Error(E, diag::note_expr_divide_by_zero); |
| 5904 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5905 | ComplexValue LHS = Result; |
| 5906 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5907 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 5908 | Result.getComplexIntReal() = |
| 5909 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5910 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 5911 | Result.getComplexIntImag() = |
| 5912 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 5913 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 5914 | } |
| 5915 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5918 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5921 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 5922 | // Get the operand value into 'Result'. |
| 5923 | if (!Visit(E->getSubExpr())) |
| 5924 | return false; |
| 5925 | |
| 5926 | switch (E->getOpcode()) { |
| 5927 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5928 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5929 | case UO_Extension: |
| 5930 | return true; |
| 5931 | case UO_Plus: |
| 5932 | // The result is always just the subexpr. |
| 5933 | return true; |
| 5934 | case UO_Minus: |
| 5935 | if (Result.isComplexFloat()) { |
| 5936 | Result.getComplexFloatReal().changeSign(); |
| 5937 | Result.getComplexFloatImag().changeSign(); |
| 5938 | } |
| 5939 | else { |
| 5940 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 5941 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5942 | } |
| 5943 | return true; |
| 5944 | case UO_Not: |
| 5945 | if (Result.isComplexFloat()) |
| 5946 | Result.getComplexFloatImag().changeSign(); |
| 5947 | else |
| 5948 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5949 | return true; |
| 5950 | } |
| 5951 | } |
| 5952 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5953 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5954 | if (E->getNumInits() == 2) { |
| 5955 | if (E->getType()->isComplexType()) { |
| 5956 | Result.makeComplexFloat(); |
| 5957 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 5958 | return false; |
| 5959 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 5960 | return false; |
| 5961 | } else { |
| 5962 | Result.makeComplexInt(); |
| 5963 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 5964 | return false; |
| 5965 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 5966 | return false; |
| 5967 | } |
| 5968 | return true; |
| 5969 | } |
| 5970 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 5971 | } |
| 5972 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5973 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5974 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 5975 | // comma operator |
| 5976 | //===----------------------------------------------------------------------===// |
| 5977 | |
| 5978 | namespace { |
| 5979 | class VoidExprEvaluator |
| 5980 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 5981 | public: |
| 5982 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 5983 | |
| 5984 | bool Success(const CCValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5985 | |
| 5986 | bool VisitCastExpr(const CastExpr *E) { |
| 5987 | switch (E->getCastKind()) { |
| 5988 | default: |
| 5989 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5990 | case CK_ToVoid: |
| 5991 | VisitIgnoredValue(E->getSubExpr()); |
| 5992 | return true; |
| 5993 | } |
| 5994 | } |
| 5995 | }; |
| 5996 | } // end anonymous namespace |
| 5997 | |
| 5998 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 5999 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 6000 | return VoidExprEvaluator(Info).Visit(E); |
| 6001 | } |
| 6002 | |
| 6003 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6004 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6005 | //===----------------------------------------------------------------------===// |
| 6006 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 6007 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6008 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 6009 | // are. |
| 6010 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 6011 | LValue LV; |
| 6012 | if (!EvaluateLValue(E, LV, Info)) |
| 6013 | return false; |
| 6014 | LV.moveInto(Result); |
| 6015 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6016 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6017 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6018 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6019 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6020 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6021 | } else if (E->getType()->hasPointerRepresentation()) { |
| 6022 | LValue LV; |
| 6023 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6024 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6025 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6026 | } else if (E->getType()->isRealFloatingType()) { |
| 6027 | llvm::APFloat F(0.0); |
| 6028 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6029 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 6030 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6031 | } else if (E->getType()->isAnyComplexType()) { |
| 6032 | ComplexValue C; |
| 6033 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6034 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6035 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6036 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6037 | MemberPtr P; |
| 6038 | if (!EvaluateMemberPointer(E, P, Info)) |
| 6039 | return false; |
| 6040 | P.moveInto(Result); |
| 6041 | return true; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6042 | } else if (E->getType()->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6043 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6044 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6045 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6046 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6047 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6048 | } else if (E->getType()->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6049 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6050 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6051 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 6052 | return false; |
| 6053 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6054 | } else if (E->getType()->isVoidType()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6055 | if (Info.getLangOpts().CPlusPlus0x) |
| 6056 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 6057 | << E->getType(); |
| 6058 | else |
| 6059 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6060 | if (!EvaluateVoid(E, Info)) |
| 6061 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6062 | } else if (Info.getLangOpts().CPlusPlus0x) { |
| 6063 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); |
| 6064 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6065 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6066 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 6067 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6068 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6069 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 6070 | return true; |
| 6071 | } |
| 6072 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6073 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 6074 | /// cases, the in-place evaluation is essential, since later initializers for |
| 6075 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 6076 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
| 6077 | const Expr *E, CheckConstantExpressionKind CCEK, |
| 6078 | bool AllowNonLiteralTypes) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 6079 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6080 | return false; |
| 6081 | |
| 6082 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6083 | // Evaluate arrays and record types in-place, so that later initializers can |
| 6084 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6085 | if (E->getType()->isArrayType()) |
| 6086 | return EvaluateArray(E, This, Result, Info); |
| 6087 | else if (E->getType()->isRecordType()) |
| 6088 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | // For any other type, in-place evaluation is unimportant. |
| 6092 | CCValue CoreConstResult; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6093 | if (!Evaluate(CoreConstResult, Info, E)) |
| 6094 | return false; |
| 6095 | Result = CoreConstResult.toAPValue(); |
| 6096 | return true; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6099 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 6100 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 6101 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6102 | if (!CheckLiteralType(Info, E)) |
| 6103 | return false; |
| 6104 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6105 | CCValue Value; |
| 6106 | if (!::Evaluate(Value, Info, E)) |
| 6107 | return false; |
| 6108 | |
| 6109 | if (E->isGLValue()) { |
| 6110 | LValue LV; |
| 6111 | LV.setFrom(Value); |
| 6112 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) |
| 6113 | return false; |
| 6114 | } |
| 6115 | |
| 6116 | // Check this core constant expression is a constant expression, and if so, |
| 6117 | // convert it to one. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6118 | Result = Value.toAPValue(); |
| 6119 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6120 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6121 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6122 | /// 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] | 6123 | /// any crazy technique (that has nothing to do with language standards) that |
| 6124 | /// 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] | 6125 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 6126 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6127 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 6128 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 6129 | // containing vast quantities of these. |
| 6130 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 6131 | Result.Val = APValue(APSInt(L->getValue(), |
| 6132 | L->getType()->isUnsignedIntegerType())); |
| 6133 | return true; |
| 6134 | } |
| 6135 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6136 | // FIXME: Evaluating values of large array and record types can cause |
| 6137 | // performance problems. Only do so in C++11 for now. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6138 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 6139 | !Ctx.getLangOptions().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 6140 | return false; |
| 6141 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6142 | EvalInfo Info(Ctx, Result); |
| 6143 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6144 | } |
| 6145 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6146 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 6147 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6148 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6149 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6150 | HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx), |
| 6151 | Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 6152 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 6153 | } |
| 6154 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6155 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 6156 | SideEffectsKind AllowSideEffects) const { |
| 6157 | if (!getType()->isIntegralOrEnumerationType()) |
| 6158 | return false; |
| 6159 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6160 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6161 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 6162 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6163 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6164 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6165 | Result = ExprResult.Val.getInt(); |
| 6166 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6169 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 6170 | EvalInfo Info(Ctx, Result); |
| 6171 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6172 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6173 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 6174 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 6175 | Ctx.getLValueReferenceType(getType()), LV)) |
| 6176 | return false; |
| 6177 | |
| 6178 | CCValue Tmp; |
| 6179 | LV.moveInto(Tmp); |
| 6180 | Result.Val = Tmp.toAPValue(); |
| 6181 | return true; |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6184 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 6185 | const VarDecl *VD, |
| 6186 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6187 | // FIXME: Evaluating initializers for large array and record types can cause |
| 6188 | // performance problems. Only do so in C++11 for now. |
| 6189 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 6190 | !Ctx.getLangOptions().CPlusPlus0x) |
| 6191 | return false; |
| 6192 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6193 | Expr::EvalStatus EStatus; |
| 6194 | EStatus.Diag = &Notes; |
| 6195 | |
| 6196 | EvalInfo InitInfo(Ctx, EStatus); |
| 6197 | InitInfo.setEvaluatingDecl(VD, Value); |
| 6198 | |
| 6199 | LValue LVal; |
| 6200 | LVal.set(VD); |
| 6201 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6202 | // C++11 [basic.start.init]p2: |
| 6203 | // Variables with static storage duration or thread storage duration shall be |
| 6204 | // zero-initialized before any other initialization takes place. |
| 6205 | // This behavior is not present in C. |
| 6206 | if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() && |
| 6207 | !VD->getType()->isReferenceType()) { |
| 6208 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6209 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant, |
| 6210 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6211 | return false; |
| 6212 | } |
| 6213 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6214 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant, |
| 6215 | /*AllowNonLiteralTypes=*/true) || |
| 6216 | EStatus.HasSideEffects) |
| 6217 | return false; |
| 6218 | |
| 6219 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 6220 | Value); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6223 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 6224 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6225 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 6226 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6227 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 6228 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6229 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6230 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6231 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6234 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6235 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6236 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 6237 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6238 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6239 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6240 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6241 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6242 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6243 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 6244 | bool Expr::EvalResult::isGlobalLValue() const { |
| 6245 | assert(Val.isLValue()); |
| 6246 | return IsGlobalLValue(Val.getLValueBase()); |
| 6247 | } |
| 6248 | |
| 6249 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6250 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 6251 | /// an integer constant expression. |
| 6252 | |
| 6253 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 6254 | /// comma, etc |
| 6255 | /// |
| 6256 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 6257 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 6258 | /// cast+dereference. |
| 6259 | |
| 6260 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 6261 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 6262 | // Note that to reduce code duplication, this helper does no evaluation |
| 6263 | // itself; the caller checks whether the expression is evaluatable, and |
| 6264 | // in the rare cases where CheckICE actually cares about the evaluated |
| 6265 | // value, it calls into Evalute. |
| 6266 | // |
| 6267 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6268 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6269 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 6270 | // a legal subexpression for an ICE. This return value is used to handle |
| 6271 | // the comma operator in C99 mode. |
| 6272 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 6273 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6274 | namespace { |
| 6275 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6276 | struct ICEDiag { |
| 6277 | unsigned Val; |
| 6278 | SourceLocation Loc; |
| 6279 | |
| 6280 | public: |
| 6281 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 6282 | ICEDiag() : Val(0) {} |
| 6283 | }; |
| 6284 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6288 | |
| 6289 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 6290 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6291 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6292 | !EVResult.Val.isInt()) { |
| 6293 | return ICEDiag(2, E->getLocStart()); |
| 6294 | } |
| 6295 | return NoDiag(); |
| 6296 | } |
| 6297 | |
| 6298 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 6299 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6300 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6301 | return ICEDiag(2, E->getLocStart()); |
| 6302 | } |
| 6303 | |
| 6304 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 6305 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6306 | #define STMT(Node, Base) case Expr::Node##Class: |
| 6307 | #define EXPR(Node, Base) |
| 6308 | #include "clang/AST/StmtNodes.inc" |
| 6309 | case Expr::PredefinedExprClass: |
| 6310 | case Expr::FloatingLiteralClass: |
| 6311 | case Expr::ImaginaryLiteralClass: |
| 6312 | case Expr::StringLiteralClass: |
| 6313 | case Expr::ArraySubscriptExprClass: |
| 6314 | case Expr::MemberExprClass: |
| 6315 | case Expr::CompoundAssignOperatorClass: |
| 6316 | case Expr::CompoundLiteralExprClass: |
| 6317 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6318 | case Expr::DesignatedInitExprClass: |
| 6319 | case Expr::ImplicitValueInitExprClass: |
| 6320 | case Expr::ParenListExprClass: |
| 6321 | case Expr::VAArgExprClass: |
| 6322 | case Expr::AddrLabelExprClass: |
| 6323 | case Expr::StmtExprClass: |
| 6324 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 6325 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6326 | case Expr::CXXDynamicCastExprClass: |
| 6327 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 6328 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6329 | case Expr::CXXNullPtrLiteralExprClass: |
| 6330 | case Expr::CXXThisExprClass: |
| 6331 | case Expr::CXXThrowExprClass: |
| 6332 | case Expr::CXXNewExprClass: |
| 6333 | case Expr::CXXDeleteExprClass: |
| 6334 | case Expr::CXXPseudoDestructorExprClass: |
| 6335 | case Expr::UnresolvedLookupExprClass: |
| 6336 | case Expr::DependentScopeDeclRefExprClass: |
| 6337 | case Expr::CXXConstructExprClass: |
| 6338 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6339 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6340 | case Expr::CXXTemporaryObjectExprClass: |
| 6341 | case Expr::CXXUnresolvedConstructExprClass: |
| 6342 | case Expr::CXXDependentScopeMemberExprClass: |
| 6343 | case Expr::UnresolvedMemberExprClass: |
| 6344 | case Expr::ObjCStringLiteralClass: |
| 6345 | case Expr::ObjCEncodeExprClass: |
| 6346 | case Expr::ObjCMessageExprClass: |
| 6347 | case Expr::ObjCSelectorExprClass: |
| 6348 | case Expr::ObjCProtocolExprClass: |
| 6349 | case Expr::ObjCIvarRefExprClass: |
| 6350 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6351 | case Expr::ObjCIsaExprClass: |
| 6352 | case Expr::ShuffleVectorExprClass: |
| 6353 | case Expr::BlockExprClass: |
| 6354 | case Expr::BlockDeclRefExprClass: |
| 6355 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 6356 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 6357 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 6358 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 6359 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6360 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6361 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6362 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 6363 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6364 | case Expr::InitListExprClass: |
Douglas Gregor | 01d0801 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 6365 | case Expr::LambdaExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6366 | return ICEDiag(2, E->getLocStart()); |
| 6367 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6368 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6369 | case Expr::GNUNullExprClass: |
| 6370 | // GCC considers the GNU __null value to be an integral constant expression. |
| 6371 | return NoDiag(); |
| 6372 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6373 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 6374 | return |
| 6375 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 6376 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6377 | case Expr::ParenExprClass: |
| 6378 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 6379 | case Expr::GenericSelectionExprClass: |
| 6380 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6381 | case Expr::IntegerLiteralClass: |
| 6382 | case Expr::CharacterLiteralClass: |
| 6383 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 6384 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6385 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 6386 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 6387 | case Expr::TypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 6388 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 6389 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6390 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6391 | return NoDiag(); |
| 6392 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 6393 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6394 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 6395 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6396 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6397 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6398 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6399 | return CheckEvalInICE(E, Ctx); |
| 6400 | return ICEDiag(2, E->getLocStart()); |
| 6401 | } |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6402 | case Expr::DeclRefExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6403 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 6404 | return NoDiag(); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6405 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 6406 | if (Ctx.getLangOptions().CPlusPlus && |
| 6407 | D && IsConstNonVolatile(D->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6408 | // Parameter variables are never constants. Without this check, |
| 6409 | // getAnyInitializer() can find a default argument, which leads |
| 6410 | // to chaos. |
| 6411 | if (isa<ParmVarDecl>(D)) |
| 6412 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6413 | |
| 6414 | // C++ 7.1.5.1p2 |
| 6415 | // A variable of non-volatile const-qualified integral or enumeration |
| 6416 | // type initialized by an ICE can be used in ICEs. |
| 6417 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 6418 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 6419 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6420 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6421 | const VarDecl *VD; |
| 6422 | // Look for a declaration of this variable that has an initializer, and |
| 6423 | // check whether it is an ICE. |
| 6424 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 6425 | return NoDiag(); |
| 6426 | else |
| 6427 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6428 | } |
| 6429 | } |
| 6430 | return ICEDiag(2, E->getLocStart()); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6431 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6432 | case Expr::UnaryOperatorClass: { |
| 6433 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 6434 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6435 | case UO_PostInc: |
| 6436 | case UO_PostDec: |
| 6437 | case UO_PreInc: |
| 6438 | case UO_PreDec: |
| 6439 | case UO_AddrOf: |
| 6440 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6441 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 6442 | // subexpressions of constant expressions, but they can never be ICEs |
| 6443 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6444 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6445 | case UO_Extension: |
| 6446 | case UO_LNot: |
| 6447 | case UO_Plus: |
| 6448 | case UO_Minus: |
| 6449 | case UO_Not: |
| 6450 | case UO_Real: |
| 6451 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6452 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | // OffsetOf falls through here. |
| 6456 | } |
| 6457 | case Expr::OffsetOfExprClass: { |
| 6458 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6459 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6460 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6461 | // compliance: we should warn earlier for offsetof expressions with |
| 6462 | // array subscripts that aren't ICEs, and if the array subscripts |
| 6463 | // are ICEs, the value of the offsetof must be an integer constant. |
| 6464 | return CheckEvalInICE(E, Ctx); |
| 6465 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6466 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 6467 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 6468 | if ((Exp->getKind() == UETT_SizeOf) && |
| 6469 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6470 | return ICEDiag(2, E->getLocStart()); |
| 6471 | return NoDiag(); |
| 6472 | } |
| 6473 | case Expr::BinaryOperatorClass: { |
| 6474 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 6475 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6476 | case BO_PtrMemD: |
| 6477 | case BO_PtrMemI: |
| 6478 | case BO_Assign: |
| 6479 | case BO_MulAssign: |
| 6480 | case BO_DivAssign: |
| 6481 | case BO_RemAssign: |
| 6482 | case BO_AddAssign: |
| 6483 | case BO_SubAssign: |
| 6484 | case BO_ShlAssign: |
| 6485 | case BO_ShrAssign: |
| 6486 | case BO_AndAssign: |
| 6487 | case BO_XorAssign: |
| 6488 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6489 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 6490 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6491 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6492 | return ICEDiag(2, E->getLocStart()); |
| 6493 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6494 | case BO_Mul: |
| 6495 | case BO_Div: |
| 6496 | case BO_Rem: |
| 6497 | case BO_Add: |
| 6498 | case BO_Sub: |
| 6499 | case BO_Shl: |
| 6500 | case BO_Shr: |
| 6501 | case BO_LT: |
| 6502 | case BO_GT: |
| 6503 | case BO_LE: |
| 6504 | case BO_GE: |
| 6505 | case BO_EQ: |
| 6506 | case BO_NE: |
| 6507 | case BO_And: |
| 6508 | case BO_Xor: |
| 6509 | case BO_Or: |
| 6510 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6511 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6512 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6513 | if (Exp->getOpcode() == BO_Div || |
| 6514 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6515 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6516 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 6517 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6518 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6519 | if (REval == 0) |
| 6520 | return ICEDiag(1, E->getLocStart()); |
| 6521 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6522 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6523 | if (LEval.isMinSignedValue()) |
| 6524 | return ICEDiag(1, E->getLocStart()); |
| 6525 | } |
| 6526 | } |
| 6527 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6528 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6529 | if (Ctx.getLangOptions().C99) { |
| 6530 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 6531 | // if it isn't evaluated. |
| 6532 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 6533 | return ICEDiag(1, E->getLocStart()); |
| 6534 | } else { |
| 6535 | // In both C89 and C++, commas in ICEs are illegal. |
| 6536 | return ICEDiag(2, E->getLocStart()); |
| 6537 | } |
| 6538 | } |
| 6539 | if (LHSResult.Val >= RHSResult.Val) |
| 6540 | return LHSResult; |
| 6541 | return RHSResult; |
| 6542 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6543 | case BO_LAnd: |
| 6544 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6545 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6546 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 6547 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 6548 | // Rare case where the RHS has a comma "side-effect"; we need |
| 6549 | // to actually check the condition to see whether the side |
| 6550 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6551 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6552 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6553 | return RHSResult; |
| 6554 | return NoDiag(); |
| 6555 | } |
| 6556 | |
| 6557 | if (LHSResult.Val >= RHSResult.Val) |
| 6558 | return LHSResult; |
| 6559 | return RHSResult; |
| 6560 | } |
| 6561 | } |
| 6562 | } |
| 6563 | case Expr::ImplicitCastExprClass: |
| 6564 | case Expr::CStyleCastExprClass: |
| 6565 | case Expr::CXXFunctionalCastExprClass: |
| 6566 | case Expr::CXXStaticCastExprClass: |
| 6567 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 6568 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6569 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6570 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 6571 | if (isa<ExplicitCastExpr>(E)) { |
| 6572 | if (const FloatingLiteral *FL |
| 6573 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 6574 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 6575 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 6576 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 6577 | bool Ignored; |
| 6578 | // If the value does not fit in the destination type, the behavior is |
| 6579 | // undefined, so we are not required to treat it as a constant |
| 6580 | // expression. |
| 6581 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 6582 | llvm::APFloat::rmTowardZero, |
| 6583 | &Ignored) & APFloat::opInvalidOp) |
| 6584 | return ICEDiag(2, E->getLocStart()); |
| 6585 | return NoDiag(); |
| 6586 | } |
| 6587 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6588 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 6589 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6590 | case CK_AtomicToNonAtomic: |
| 6591 | case CK_NonAtomicToAtomic: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6592 | case CK_NoOp: |
| 6593 | case CK_IntegralToBoolean: |
| 6594 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6595 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6596 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6597 | return ICEDiag(2, E->getLocStart()); |
| 6598 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6599 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6600 | case Expr::BinaryConditionalOperatorClass: { |
| 6601 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 6602 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 6603 | if (CommonResult.Val == 2) return CommonResult; |
| 6604 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 6605 | if (FalseResult.Val == 2) return FalseResult; |
| 6606 | if (CommonResult.Val == 1) return CommonResult; |
| 6607 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6608 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6609 | return FalseResult; |
| 6610 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6611 | case Expr::ConditionalOperatorClass: { |
| 6612 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 6613 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 6614 | // then only the true side is actually considered in an integer constant |
| 6615 | // expression, and it is fully evaluated. This is an important GNU |
| 6616 | // extension. See GCC PR38377 for discussion. |
| 6617 | if (const CallExpr *CallCE |
| 6618 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6619 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 6620 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6621 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6622 | if (CondResult.Val == 2) |
| 6623 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6624 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6625 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 6626 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6627 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6628 | if (TrueResult.Val == 2) |
| 6629 | return TrueResult; |
| 6630 | if (FalseResult.Val == 2) |
| 6631 | return FalseResult; |
| 6632 | if (CondResult.Val == 1) |
| 6633 | return CondResult; |
| 6634 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 6635 | return NoDiag(); |
| 6636 | // Rare case where the diagnostics depend on which side is evaluated |
| 6637 | // Note that if we get here, CondResult is 0, and at least one of |
| 6638 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6639 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6640 | return FalseResult; |
| 6641 | } |
| 6642 | return TrueResult; |
| 6643 | } |
| 6644 | case Expr::CXXDefaultArgExprClass: |
| 6645 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 6646 | case Expr::ChooseExprClass: { |
| 6647 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 6648 | } |
| 6649 | } |
| 6650 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 6651 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6654 | /// Evaluate an expression as a C++11 integral constant expression. |
| 6655 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 6656 | const Expr *E, |
| 6657 | llvm::APSInt *Value, |
| 6658 | SourceLocation *Loc) { |
| 6659 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 6660 | if (Loc) *Loc = E->getExprLoc(); |
| 6661 | return false; |
| 6662 | } |
| 6663 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6664 | APValue Result; |
| 6665 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6666 | return false; |
| 6667 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6668 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 6669 | if (Value) *Value = Result.getInt(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6670 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6673 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6674 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 6675 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 6676 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6677 | ICEDiag d = CheckICE(this, Ctx); |
| 6678 | if (d.Val != 0) { |
| 6679 | if (Loc) *Loc = d.Loc; |
| 6680 | return false; |
| 6681 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6682 | return true; |
| 6683 | } |
| 6684 | |
| 6685 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 6686 | SourceLocation *Loc, bool isEvaluated) const { |
| 6687 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 6688 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 6689 | |
| 6690 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 6691 | return false; |
| 6692 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6693 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6694 | return true; |
| 6695 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6696 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6697 | bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { |
| 6698 | return CheckICE(this, Ctx).Val == 0; |
| 6699 | } |
| 6700 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6701 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 6702 | SourceLocation *Loc) const { |
| 6703 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 6704 | // issues. |
| 6705 | assert(Ctx.getLangOptions().CPlusPlus); |
| 6706 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6707 | // Build evaluation settings. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6708 | Expr::EvalStatus Status; |
| 6709 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 6710 | Status.Diag = &Diags; |
| 6711 | EvalInfo Info(Ctx, Status); |
| 6712 | |
| 6713 | APValue Scratch; |
| 6714 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 6715 | |
| 6716 | if (!Diags.empty()) { |
| 6717 | IsConstExpr = false; |
| 6718 | if (Loc) *Loc = Diags[0].first; |
| 6719 | } else if (!IsConstExpr) { |
| 6720 | // FIXME: This shouldn't happen. |
| 6721 | if (Loc) *Loc = getExprLoc(); |
| 6722 | } |
| 6723 | |
| 6724 | return IsConstExpr; |
| 6725 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6726 | |
| 6727 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
| 6728 | llvm::SmallVectorImpl< |
| 6729 | PartialDiagnosticAt> &Diags) { |
| 6730 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 6731 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 6732 | // ASTs which we build for dependent expressions. |
| 6733 | if (FD->isDependentContext()) |
| 6734 | return true; |
| 6735 | |
| 6736 | Expr::EvalStatus Status; |
| 6737 | Status.Diag = &Diags; |
| 6738 | |
| 6739 | EvalInfo Info(FD->getASTContext(), Status); |
| 6740 | Info.CheckingPotentialConstantExpression = true; |
| 6741 | |
| 6742 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6743 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 6744 | |
| 6745 | // FIXME: Fabricate an arbitrary expression on the stack and pretend that it |
| 6746 | // is a temporary being used as the 'this' pointer. |
| 6747 | LValue This; |
| 6748 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6749 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6750 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6751 | ArrayRef<const Expr*> Args; |
| 6752 | |
| 6753 | SourceLocation Loc = FD->getLocation(); |
| 6754 | |
| 6755 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6756 | APValue Scratch; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6757 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6758 | } else { |
| 6759 | CCValue Scratch; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6760 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 6761 | Args, FD->getBody(), Info, Scratch); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6762 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6763 | |
| 6764 | return Diags.empty(); |
| 6765 | } |