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 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 27 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 28 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 29 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 31 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 32 | /// information about a subexpression as it is folded. It retains information |
| 33 | /// about the AST context, but also maintains information about the folded |
| 34 | /// expression. |
| 35 | /// |
| 36 | /// If an expression could be evaluated, it is still possible it is not a C |
| 37 | /// "integer constant expression" or constant expression. If not, this struct |
| 38 | /// captures information about how and why not. |
| 39 | /// |
| 40 | /// One bit of information passed *into* the request for constant folding |
| 41 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 42 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 43 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 44 | /// certain things in certain situations. |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 45 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 46 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 47 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 48 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 49 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 50 | QualType getType(APValue::LValueBase B) { |
| 51 | if (!B) return QualType(); |
| 52 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 53 | return D->getType(); |
| 54 | return B.get<const Expr*>()->getType(); |
| 55 | } |
| 56 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 57 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 58 | /// field declaration. |
| 59 | const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
| 60 | APValue::BaseOrMemberType Value; |
| 61 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 62 | return dyn_cast<FieldDecl>(Value.getPointer()); |
| 63 | } |
| 64 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 65 | /// base class declaration. |
| 66 | const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
| 67 | APValue::BaseOrMemberType Value; |
| 68 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 69 | return dyn_cast<CXXRecordDecl>(Value.getPointer()); |
| 70 | } |
| 71 | /// Determine whether this LValue path entry for a base class names a virtual |
| 72 | /// base class. |
| 73 | bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
| 74 | APValue::BaseOrMemberType Value; |
| 75 | Value.setFromOpaqueValue(E.BaseOrMember); |
| 76 | return Value.getInt(); |
| 77 | } |
| 78 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 79 | /// Determine whether the described subobject is an array element. |
| 80 | static bool SubobjectIsArrayElement(QualType Base, |
| 81 | ArrayRef<APValue::LValuePathEntry> Path) { |
| 82 | bool IsArrayElement = false; |
| 83 | const Type *T = Base.getTypePtr(); |
| 84 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 85 | IsArrayElement = T && T->isArrayType(); |
| 86 | if (IsArrayElement) |
| 87 | T = T->getBaseElementTypeUnsafe(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 88 | else if (const FieldDecl *FD = getAsField(Path[I])) |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 89 | T = FD->getType().getTypePtr(); |
| 90 | else |
| 91 | // Path[I] describes a base class. |
| 92 | T = 0; |
| 93 | } |
| 94 | return IsArrayElement; |
| 95 | } |
| 96 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 97 | /// A path from a glvalue to a subobject of that glvalue. |
| 98 | struct SubobjectDesignator { |
| 99 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 100 | /// lvalues can still be folded, but they are not core constant expressions |
| 101 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 102 | bool Invalid : 1; |
| 103 | |
| 104 | /// Whether this designates an array element. |
| 105 | bool ArrayElement : 1; |
| 106 | |
| 107 | /// Whether this designates 'one past the end' of the current subobject. |
| 108 | bool OnePastTheEnd : 1; |
| 109 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 110 | typedef APValue::LValuePathEntry PathEntry; |
| 111 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 112 | /// The entries on the path from the glvalue to the designated subobject. |
| 113 | SmallVector<PathEntry, 8> Entries; |
| 114 | |
| 115 | SubobjectDesignator() : |
| 116 | Invalid(false), ArrayElement(false), OnePastTheEnd(false) {} |
| 117 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 118 | SubobjectDesignator(const APValue &V) : |
| 119 | Invalid(!V.isLValue() || !V.hasLValuePath()), ArrayElement(false), |
| 120 | OnePastTheEnd(false) { |
| 121 | if (!Invalid) { |
| 122 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 123 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 124 | if (V.getLValueBase()) |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 125 | ArrayElement = SubobjectIsArrayElement(getType(V.getLValueBase()), |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 126 | V.getLValuePath()); |
| 127 | else |
| 128 | assert(V.getLValuePath().empty() &&"Null pointer with nonempty path"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 129 | OnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 133 | void setInvalid() { |
| 134 | Invalid = true; |
| 135 | Entries.clear(); |
| 136 | } |
| 137 | /// Update this designator to refer to the given element within this array. |
| 138 | void addIndex(uint64_t N) { |
| 139 | if (Invalid) return; |
| 140 | if (OnePastTheEnd) { |
| 141 | setInvalid(); |
| 142 | return; |
| 143 | } |
| 144 | PathEntry Entry; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 145 | Entry.ArrayIndex = N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 146 | Entries.push_back(Entry); |
| 147 | ArrayElement = true; |
| 148 | } |
| 149 | /// Update this designator to refer to the given base or member of this |
| 150 | /// object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 151 | void addDecl(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | if (Invalid) return; |
| 153 | if (OnePastTheEnd) { |
| 154 | setInvalid(); |
| 155 | return; |
| 156 | } |
| 157 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 158 | APValue::BaseOrMemberType Value(D, Virtual); |
| 159 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 160 | Entries.push_back(Entry); |
| 161 | ArrayElement = false; |
| 162 | } |
| 163 | /// Add N to the address of this subobject. |
| 164 | void adjustIndex(uint64_t N) { |
| 165 | if (Invalid) return; |
| 166 | if (ArrayElement) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 167 | // FIXME: Make sure the index stays within bounds, or one past the end. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 168 | Entries.back().ArrayIndex += N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | if (OnePastTheEnd && N == (uint64_t)-1) |
| 172 | OnePastTheEnd = false; |
| 173 | else if (!OnePastTheEnd && N == 1) |
| 174 | OnePastTheEnd = true; |
| 175 | else if (N != 0) |
| 176 | setInvalid(); |
| 177 | } |
| 178 | }; |
| 179 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 180 | /// A core constant value. This can be the value of any constant expression, |
| 181 | /// 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] | 182 | /// |
| 183 | /// For an LValue, the base and offset are stored in the APValue subobject, |
| 184 | /// but the other information is stored in the SubobjectDesignator. For all |
| 185 | /// other value kinds, the value is stored directly in the APValue subobject. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 186 | class CCValue : public APValue { |
| 187 | typedef llvm::APSInt APSInt; |
| 188 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 189 | /// If the value is a reference or pointer into a parameter or temporary, |
| 190 | /// this is the corresponding call stack frame. |
| 191 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 192 | /// If the value is a reference or pointer, this is a description of how the |
| 193 | /// subobject was specified. |
| 194 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 195 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 196 | struct GlobalValue {}; |
| 197 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 198 | CCValue() {} |
| 199 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 200 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 201 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 202 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 203 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 204 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 205 | CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F, |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 206 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 207 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 208 | CCValue(const APValue &V, GlobalValue) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 209 | APValue(V), CallFrame(0), Designator(V) {} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 210 | CCValue(const ValueDecl *D, bool IsDerivedMember, |
| 211 | ArrayRef<const CXXRecordDecl*> Path) : |
| 212 | APValue(D, IsDerivedMember, Path) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 213 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 214 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 215 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 216 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 217 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 218 | SubobjectDesignator &getLValueDesignator() { |
| 219 | assert(getKind() == LValue); |
| 220 | return Designator; |
| 221 | } |
| 222 | const SubobjectDesignator &getLValueDesignator() const { |
| 223 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 224 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 227 | /// A stack frame in the constexpr call stack. |
| 228 | struct CallStackFrame { |
| 229 | EvalInfo &Info; |
| 230 | |
| 231 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 232 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 233 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 234 | /// This - The binding for the this pointer in this call, if any. |
| 235 | const LValue *This; |
| 236 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 237 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 238 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 239 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 240 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 241 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 242 | typedef MapTy::const_iterator temp_iterator; |
| 243 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 244 | MapTy Temporaries; |
| 245 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 246 | CallStackFrame(EvalInfo &Info, const LValue *This, |
| 247 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 248 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 251 | /// A partial diagnostic which we might know in advance that we are not going |
| 252 | /// to emit. |
| 253 | class OptionalDiagnostic { |
| 254 | PartialDiagnostic *Diag; |
| 255 | |
| 256 | public: |
| 257 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 258 | |
| 259 | template<typename T> |
| 260 | OptionalDiagnostic &operator<<(const T &v) { |
| 261 | if (Diag) |
| 262 | *Diag << v; |
| 263 | return *this; |
| 264 | } |
| 265 | }; |
| 266 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 267 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 268 | ASTContext &Ctx; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 269 | |
| 270 | /// EvalStatus - Contains information about the evaluation. |
| 271 | Expr::EvalStatus &EvalStatus; |
| 272 | |
| 273 | /// CurrentCall - The top of the constexpr call stack. |
| 274 | CallStackFrame *CurrentCall; |
| 275 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 276 | /// CallStackDepth - The number of calls in the call stack right now. |
| 277 | unsigned CallStackDepth; |
| 278 | |
| 279 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 280 | /// OpaqueValues - Values used as the common expression in a |
| 281 | /// BinaryConditionalOperator. |
| 282 | MapTy OpaqueValues; |
| 283 | |
| 284 | /// BottomFrame - The frame in which evaluation started. This must be |
| 285 | /// initialized last. |
| 286 | CallStackFrame BottomFrame; |
| 287 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 288 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 289 | /// evaluated, if any. |
| 290 | const VarDecl *EvaluatingDecl; |
| 291 | |
| 292 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 293 | /// declaration whose initializer is being evaluated, if any. |
| 294 | APValue *EvaluatingDeclValue; |
| 295 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 296 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 297 | /// notes attached to it will also be stored, otherwise they will not be. |
| 298 | bool HasActiveDiagnostic; |
| 299 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 300 | |
| 301 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 302 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
| 303 | CallStackDepth(0), BottomFrame(*this, 0, 0), EvaluatingDecl(0), |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 304 | EvaluatingDeclValue(0), HasActiveDiagnostic(false) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 305 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 306 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 307 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 308 | if (i == OpaqueValues.end()) return 0; |
| 309 | return &i->second; |
| 310 | } |
| 311 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 312 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 313 | EvaluatingDecl = VD; |
| 314 | EvaluatingDeclValue = &Value; |
| 315 | } |
| 316 | |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 317 | const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } |
| 318 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 319 | bool CheckCallLimit(SourceLocation Loc) { |
| 320 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 321 | return true; |
| 322 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 323 | << getLangOpts().ConstexprCallDepth; |
| 324 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 325 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 326 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 327 | private: |
| 328 | /// Add a diagnostic to the diagnostics list. |
| 329 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 330 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 331 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 332 | return EvalStatus.Diag->back().second; |
| 333 | } |
| 334 | |
| 335 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 336 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 337 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId, |
| 338 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 339 | // If we have a prior diagnostic, it will be noting that the expression |
| 340 | // isn't a constant expression. This diagnostic is more important. |
| 341 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 342 | if (EvalStatus.Diag) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 343 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 344 | EvalStatus.Diag->clear(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 345 | EvalStatus.Diag->reserve(1 + ExtraNotes); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 346 | // FIXME: Add a call stack for constexpr evaluation. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 347 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 348 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 349 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 350 | return OptionalDiagnostic(); |
| 351 | } |
| 352 | |
| 353 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 354 | /// expression. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 355 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId, |
| 356 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 357 | // Don't override a previous diagnostic. |
| 358 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) |
| 359 | return OptionalDiagnostic(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 360 | return Diag(Loc, DiagId, ExtraNotes); |
| 361 | } |
| 362 | |
| 363 | /// Add a note to a prior diagnostic. |
| 364 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 365 | if (!HasActiveDiagnostic) |
| 366 | return OptionalDiagnostic(); |
| 367 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 368 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 371 | CallStackFrame::CallStackFrame(EvalInfo &Info, const LValue *This, |
| 372 | const CCValue *Arguments) |
| 373 | : Info(Info), Caller(Info.CurrentCall), This(This), Arguments(Arguments) { |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 374 | Info.CurrentCall = this; |
| 375 | ++Info.CallStackDepth; |
| 376 | } |
| 377 | |
| 378 | CallStackFrame::~CallStackFrame() { |
| 379 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 380 | --Info.CallStackDepth; |
| 381 | Info.CurrentCall = Caller; |
| 382 | } |
| 383 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 384 | struct ComplexValue { |
| 385 | private: |
| 386 | bool IsInt; |
| 387 | |
| 388 | public: |
| 389 | APSInt IntReal, IntImag; |
| 390 | APFloat FloatReal, FloatImag; |
| 391 | |
| 392 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 393 | |
| 394 | void makeComplexFloat() { IsInt = false; } |
| 395 | bool isComplexFloat() const { return !IsInt; } |
| 396 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 397 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 398 | |
| 399 | void makeComplexInt() { IsInt = true; } |
| 400 | bool isComplexInt() const { return IsInt; } |
| 401 | APSInt &getComplexIntReal() { return IntReal; } |
| 402 | APSInt &getComplexIntImag() { return IntImag; } |
| 403 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 404 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 405 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 406 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 407 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 408 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 409 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 410 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 411 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 412 | if (v.isComplexFloat()) { |
| 413 | makeComplexFloat(); |
| 414 | FloatReal = v.getComplexFloatReal(); |
| 415 | FloatImag = v.getComplexFloatImag(); |
| 416 | } else { |
| 417 | makeComplexInt(); |
| 418 | IntReal = v.getComplexIntReal(); |
| 419 | IntImag = v.getComplexIntImag(); |
| 420 | } |
| 421 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 422 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 423 | |
| 424 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 425 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 426 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 427 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 428 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 429 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 430 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 431 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 432 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 433 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 434 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 435 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 436 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 437 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 438 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 439 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 440 | void setFrom(const CCValue &V) { |
| 441 | assert(V.isLValue()); |
| 442 | Base = V.getLValueBase(); |
| 443 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 444 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 445 | Designator = V.getLValueDesignator(); |
| 446 | } |
| 447 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 448 | void set(APValue::LValueBase B, CallStackFrame *F = 0) { |
| 449 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 450 | Offset = CharUnits::Zero(); |
| 451 | Frame = F; |
| 452 | Designator = SubobjectDesignator(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 453 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 454 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 455 | |
| 456 | struct MemberPtr { |
| 457 | MemberPtr() {} |
| 458 | explicit MemberPtr(const ValueDecl *Decl) : |
| 459 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 460 | |
| 461 | /// The member or (direct or indirect) field referred to by this member |
| 462 | /// pointer, or 0 if this is a null member pointer. |
| 463 | const ValueDecl *getDecl() const { |
| 464 | return DeclAndIsDerivedMember.getPointer(); |
| 465 | } |
| 466 | /// Is this actually a member of some type derived from the relevant class? |
| 467 | bool isDerivedMember() const { |
| 468 | return DeclAndIsDerivedMember.getInt(); |
| 469 | } |
| 470 | /// Get the class which the declaration actually lives in. |
| 471 | const CXXRecordDecl *getContainingRecord() const { |
| 472 | return cast<CXXRecordDecl>( |
| 473 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 474 | } |
| 475 | |
| 476 | void moveInto(CCValue &V) const { |
| 477 | V = CCValue(getDecl(), isDerivedMember(), Path); |
| 478 | } |
| 479 | void setFrom(const CCValue &V) { |
| 480 | assert(V.isMemberPointer()); |
| 481 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 482 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 483 | Path.clear(); |
| 484 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 485 | Path.insert(Path.end(), P.begin(), P.end()); |
| 486 | } |
| 487 | |
| 488 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 489 | /// whether the member is a member of some class derived from the class type |
| 490 | /// of the member pointer. |
| 491 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 492 | /// Path - The path of base/derived classes from the member declaration's |
| 493 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 494 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 495 | |
| 496 | /// Perform a cast towards the class of the Decl (either up or down the |
| 497 | /// hierarchy). |
| 498 | bool castBack(const CXXRecordDecl *Class) { |
| 499 | assert(!Path.empty()); |
| 500 | const CXXRecordDecl *Expected; |
| 501 | if (Path.size() >= 2) |
| 502 | Expected = Path[Path.size() - 2]; |
| 503 | else |
| 504 | Expected = getContainingRecord(); |
| 505 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 506 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 507 | // if B does not contain the original member and is not a base or |
| 508 | // derived class of the class containing the original member, the result |
| 509 | // of the cast is undefined. |
| 510 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 511 | // (D::*). We consider that to be a language defect. |
| 512 | return false; |
| 513 | } |
| 514 | Path.pop_back(); |
| 515 | return true; |
| 516 | } |
| 517 | /// Perform a base-to-derived member pointer cast. |
| 518 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 519 | if (!getDecl()) |
| 520 | return true; |
| 521 | if (!isDerivedMember()) { |
| 522 | Path.push_back(Derived); |
| 523 | return true; |
| 524 | } |
| 525 | if (!castBack(Derived)) |
| 526 | return false; |
| 527 | if (Path.empty()) |
| 528 | DeclAndIsDerivedMember.setInt(false); |
| 529 | return true; |
| 530 | } |
| 531 | /// Perform a derived-to-base member pointer cast. |
| 532 | bool castToBase(const CXXRecordDecl *Base) { |
| 533 | if (!getDecl()) |
| 534 | return true; |
| 535 | if (Path.empty()) |
| 536 | DeclAndIsDerivedMember.setInt(true); |
| 537 | if (isDerivedMember()) { |
| 538 | Path.push_back(Base); |
| 539 | return true; |
| 540 | } |
| 541 | return castBack(Base); |
| 542 | } |
| 543 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 544 | |
| 545 | /// Kinds of constant expression checking, for diagnostics. |
| 546 | enum CheckConstantExpressionKind { |
| 547 | CCEK_Constant, ///< A normal constant. |
| 548 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 549 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 550 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 552 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 553 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 554 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 555 | const LValue &This, const Expr *E, |
| 556 | CheckConstantExpressionKind CCEK |
| 557 | = CCEK_Constant); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 558 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 559 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 560 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 561 | EvalInfo &Info); |
| 562 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 563 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 564 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 565 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 566 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 567 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 568 | |
| 569 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 570 | // Misc utilities |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 573 | /// Should this call expression be treated as a string literal? |
| 574 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 575 | unsigned Builtin = E->isBuiltinCall(); |
| 576 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 577 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 578 | } |
| 579 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 580 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 581 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 582 | // constant expression of pointer type that evaluates to... |
| 583 | |
| 584 | // ... a null pointer value, or a prvalue core constant expression of type |
| 585 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 586 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 587 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 588 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 589 | // ... the address of an object with static storage duration, |
| 590 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 591 | return VD->hasGlobalStorage(); |
| 592 | // ... the address of a function, |
| 593 | return isa<FunctionDecl>(D); |
| 594 | } |
| 595 | |
| 596 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 597 | switch (E->getStmtClass()) { |
| 598 | default: |
| 599 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 600 | case Expr::CompoundLiteralExprClass: |
| 601 | return cast<CompoundLiteralExpr>(E)->isFileScope(); |
| 602 | // A string literal has static storage duration. |
| 603 | case Expr::StringLiteralClass: |
| 604 | case Expr::PredefinedExprClass: |
| 605 | case Expr::ObjCStringLiteralClass: |
| 606 | case Expr::ObjCEncodeExprClass: |
| 607 | return true; |
| 608 | case Expr::CallExprClass: |
| 609 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 610 | // For GCC compatibility, &&label has static storage duration. |
| 611 | case Expr::AddrLabelExprClass: |
| 612 | return true; |
| 613 | // A Block literal expression may be used as the initialization value for |
| 614 | // Block variables at global or local static scope. |
| 615 | case Expr::BlockExprClass: |
| 616 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
| 617 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 620 | /// Check that this reference or pointer core constant expression is a valid |
| 621 | /// value for a constant expression. Type T should be either LValue or CCValue. |
| 622 | template<typename T> |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 623 | static bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 624 | const T &LVal, APValue &Value, |
| 625 | CheckConstantExpressionKind CCEK) { |
| 626 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 627 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 628 | |
| 629 | if (!IsGlobalLValue(Base)) { |
| 630 | if (Info.getLangOpts().CPlusPlus0x) { |
| 631 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 632 | Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1) |
| 633 | << E->isGLValue() << !Designator.Entries.empty() |
| 634 | << !!VD << CCEK << VD; |
| 635 | if (VD) |
| 636 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 637 | else |
| 638 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 639 | diag::note_constexpr_temporary_here); |
| 640 | } else { |
| 641 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 642 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 643 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 644 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 645 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 646 | // A constant expression must refer to an object or be a null pointer. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 647 | if (Designator.Invalid || |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 648 | (!LVal.getLValueBase() && !Designator.Entries.empty())) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 649 | // FIXME: This is not a core constant expression. We should have already |
| 650 | // produced a CCE diagnostic. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 651 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 652 | APValue::NoLValuePath()); |
| 653 | return true; |
| 654 | } |
| 655 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 656 | // Does this refer one past the end of some object? |
| 657 | // This is technically not an address constant expression nor a reference |
| 658 | // constant expression, but we allow it for address constant expressions. |
| 659 | if (E->isGLValue() && Base && Designator.OnePastTheEnd) { |
| 660 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 661 | Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1) |
| 662 | << !Designator.Entries.empty() << !!VD << VD; |
| 663 | if (VD) |
| 664 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 665 | else |
| 666 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 667 | diag::note_constexpr_temporary_here); |
| 668 | return false; |
| 669 | } |
| 670 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 671 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 672 | Designator.Entries, Designator.OnePastTheEnd); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 673 | return true; |
| 674 | } |
| 675 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 676 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 677 | /// constant expression, and if it is, produce the corresponding constant value. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 678 | /// If not, report an appropriate diagnostic. |
| 679 | static bool CheckConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 680 | const CCValue &CCValue, APValue &Value, |
| 681 | CheckConstantExpressionKind CCEK |
| 682 | = CCEK_Constant) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 683 | if (!CCValue.isLValue()) { |
| 684 | Value = CCValue; |
| 685 | return true; |
| 686 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 687 | return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 690 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 691 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 695 | return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 698 | static bool IsWeakLValue(const LValue &Value) { |
| 699 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 700 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 703 | static bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 704 | // A null base expression indicates a null pointer. These are always |
| 705 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 706 | if (!Value.getLValueBase()) { |
| 707 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 708 | return true; |
| 709 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 710 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 711 | // Require the base expression to be a global l-value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 712 | // FIXME: C++11 requires such conversions. Remove this check. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 713 | if (!IsGlobalLValue(Value.getLValueBase())) return false; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 714 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 715 | // We have a non-null base. These are generally known to be true, but if it's |
| 716 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 717 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 718 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 719 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 722 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 723 | switch (Val.getKind()) { |
| 724 | case APValue::Uninitialized: |
| 725 | return false; |
| 726 | case APValue::Int: |
| 727 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 728 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 729 | case APValue::Float: |
| 730 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 731 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 732 | case APValue::ComplexInt: |
| 733 | Result = Val.getComplexIntReal().getBoolValue() || |
| 734 | Val.getComplexIntImag().getBoolValue(); |
| 735 | return true; |
| 736 | case APValue::ComplexFloat: |
| 737 | Result = !Val.getComplexFloatReal().isZero() || |
| 738 | !Val.getComplexFloatImag().isZero(); |
| 739 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 740 | case APValue::LValue: |
| 741 | return EvalPointerValueAsBool(Val, Result); |
| 742 | case APValue::MemberPointer: |
| 743 | Result = Val.getMemberPointerDecl(); |
| 744 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 745 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 746 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 747 | case APValue::Struct: |
| 748 | case APValue::Union: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 749 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 752 | llvm_unreachable("unknown APValue kind"); |
| 753 | } |
| 754 | |
| 755 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 756 | EvalInfo &Info) { |
| 757 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 758 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 759 | if (!Evaluate(Val, Info, E)) |
| 760 | return false; |
| 761 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 764 | template<typename T> |
| 765 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| 766 | const T &SrcValue, QualType DestType) { |
| 767 | llvm::SmallVector<char, 32> Buffer; |
| 768 | SrcValue.toString(Buffer); |
| 769 | Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) |
| 770 | << StringRef(Buffer.data(), Buffer.size()) << DestType; |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 775 | QualType SrcType, const APFloat &Value, |
| 776 | QualType DestType, APSInt &Result) { |
| 777 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 778 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 779 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 781 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 782 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 783 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 784 | & APFloat::opInvalidOp) |
| 785 | return HandleOverflow(Info, E, Value, DestType); |
| 786 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 789 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 790 | QualType SrcType, QualType DestType, |
| 791 | APFloat &Result) { |
| 792 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 793 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 794 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 795 | APFloat::rmNearestTiesToEven, &ignored) |
| 796 | & APFloat::opOverflow) |
| 797 | return HandleOverflow(Info, E, Value, DestType); |
| 798 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 802 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 803 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 804 | APSInt Result = Value; |
| 805 | // Figure out if this is a truncate, extend or noop cast. |
| 806 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 807 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 808 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 809 | return Result; |
| 810 | } |
| 811 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 812 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 813 | QualType SrcType, const APSInt &Value, |
| 814 | QualType DestType, APFloat &Result) { |
| 815 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 816 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 817 | APFloat::rmNearestTiesToEven) |
| 818 | & APFloat::opOverflow) |
| 819 | return HandleOverflow(Info, E, Value, DestType); |
| 820 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 823 | static bool FindMostDerivedObject(EvalInfo &Info, const LValue &LVal, |
| 824 | const CXXRecordDecl *&MostDerivedType, |
| 825 | unsigned &MostDerivedPathLength, |
| 826 | bool &MostDerivedIsArrayElement) { |
| 827 | const SubobjectDesignator &D = LVal.Designator; |
| 828 | if (D.Invalid || !LVal.Base) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 829 | return false; |
| 830 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 831 | const Type *T = getType(LVal.Base).getTypePtr(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 832 | |
| 833 | // Find path prefix which leads to the most-derived subobject. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 834 | MostDerivedType = T->getAsCXXRecordDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 835 | MostDerivedPathLength = 0; |
| 836 | MostDerivedIsArrayElement = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 837 | |
| 838 | for (unsigned I = 0, N = D.Entries.size(); I != N; ++I) { |
| 839 | bool IsArray = T && T->isArrayType(); |
| 840 | if (IsArray) |
| 841 | T = T->getBaseElementTypeUnsafe(); |
| 842 | else if (const FieldDecl *FD = getAsField(D.Entries[I])) |
| 843 | T = FD->getType().getTypePtr(); |
| 844 | else |
| 845 | T = 0; |
| 846 | |
| 847 | if (T) { |
| 848 | MostDerivedType = T->getAsCXXRecordDecl(); |
| 849 | MostDerivedPathLength = I + 1; |
| 850 | MostDerivedIsArrayElement = IsArray; |
| 851 | } |
| 852 | } |
| 853 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 854 | // (B*)&d + 1 has no most-derived object. |
| 855 | if (D.OnePastTheEnd && MostDerivedPathLength != D.Entries.size()) |
| 856 | return false; |
| 857 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 858 | return MostDerivedType != 0; |
| 859 | } |
| 860 | |
| 861 | static void TruncateLValueBasePath(EvalInfo &Info, LValue &Result, |
| 862 | const RecordDecl *TruncatedType, |
| 863 | unsigned TruncatedElements, |
| 864 | bool IsArrayElement) { |
| 865 | SubobjectDesignator &D = Result.Designator; |
| 866 | const RecordDecl *RD = TruncatedType; |
| 867 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 868 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 869 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 870 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 871 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 872 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 873 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 874 | RD = Base; |
| 875 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 876 | D.Entries.resize(TruncatedElements); |
| 877 | D.ArrayElement = IsArrayElement; |
| 878 | } |
| 879 | |
| 880 | /// If the given LValue refers to a base subobject of some object, find the most |
| 881 | /// derived object and the corresponding complete record type. This is necessary |
| 882 | /// in order to find the offset of a virtual base class. |
| 883 | static bool ExtractMostDerivedObject(EvalInfo &Info, LValue &Result, |
| 884 | const CXXRecordDecl *&MostDerivedType) { |
| 885 | unsigned MostDerivedPathLength; |
| 886 | bool MostDerivedIsArrayElement; |
| 887 | if (!FindMostDerivedObject(Info, Result, MostDerivedType, |
| 888 | MostDerivedPathLength, MostDerivedIsArrayElement)) |
| 889 | return false; |
| 890 | |
| 891 | // Remove the trailing base class path entries and their offsets. |
| 892 | TruncateLValueBasePath(Info, Result, MostDerivedType, MostDerivedPathLength, |
| 893 | MostDerivedIsArrayElement); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 894 | return true; |
| 895 | } |
| 896 | |
| 897 | static void HandleLValueDirectBase(EvalInfo &Info, LValue &Obj, |
| 898 | const CXXRecordDecl *Derived, |
| 899 | const CXXRecordDecl *Base, |
| 900 | const ASTRecordLayout *RL = 0) { |
| 901 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 902 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
| 903 | Obj.Designator.addDecl(Base, /*Virtual*/ false); |
| 904 | } |
| 905 | |
| 906 | static bool HandleLValueBase(EvalInfo &Info, LValue &Obj, |
| 907 | const CXXRecordDecl *DerivedDecl, |
| 908 | const CXXBaseSpecifier *Base) { |
| 909 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 910 | |
| 911 | if (!Base->isVirtual()) { |
| 912 | HandleLValueDirectBase(Info, Obj, DerivedDecl, BaseDecl); |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | // Extract most-derived object and corresponding type. |
| 917 | if (!ExtractMostDerivedObject(Info, Obj, DerivedDecl)) |
| 918 | return false; |
| 919 | |
| 920 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 921 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
| 922 | Obj.Designator.addDecl(BaseDecl, /*Virtual*/ true); |
| 923 | return true; |
| 924 | } |
| 925 | |
| 926 | /// Update LVal to refer to the given field, which must be a member of the type |
| 927 | /// currently described by LVal. |
| 928 | static void HandleLValueMember(EvalInfo &Info, LValue &LVal, |
| 929 | const FieldDecl *FD, |
| 930 | const ASTRecordLayout *RL = 0) { |
| 931 | if (!RL) |
| 932 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 933 | |
| 934 | unsigned I = FD->getFieldIndex(); |
| 935 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
| 936 | LVal.Designator.addDecl(FD); |
| 937 | } |
| 938 | |
| 939 | /// Get the size of the given type in char units. |
| 940 | static bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { |
| 941 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 942 | // extension. |
| 943 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 944 | Size = CharUnits::One(); |
| 945 | return true; |
| 946 | } |
| 947 | |
| 948 | if (!Type->isConstantSizeType()) { |
| 949 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 950 | return false; |
| 951 | } |
| 952 | |
| 953 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 954 | return true; |
| 955 | } |
| 956 | |
| 957 | /// Update a pointer value to model pointer arithmetic. |
| 958 | /// \param Info - Information about the ongoing evaluation. |
| 959 | /// \param LVal - The pointer value to be updated. |
| 960 | /// \param EltTy - The pointee type represented by LVal. |
| 961 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
| 962 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, LValue &LVal, |
| 963 | QualType EltTy, int64_t Adjustment) { |
| 964 | CharUnits SizeOfPointee; |
| 965 | if (!HandleSizeof(Info, EltTy, SizeOfPointee)) |
| 966 | return false; |
| 967 | |
| 968 | // Compute the new offset in the appropriate width. |
| 969 | LVal.Offset += Adjustment * SizeOfPointee; |
| 970 | LVal.Designator.adjustIndex(Adjustment); |
| 971 | return true; |
| 972 | } |
| 973 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 974 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 975 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 976 | const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 977 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 978 | // If this is a parameter to an active constexpr function call, perform |
| 979 | // argument substitution. |
| 980 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 981 | if (!Frame || !Frame->Arguments) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 982 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 983 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 984 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 985 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 986 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 987 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 988 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 989 | // If we're currently evaluating the initializer of this declaration, use that |
| 990 | // in-flight value. |
| 991 | if (Info.EvaluatingDecl == VD) { |
| 992 | Result = CCValue(*Info.EvaluatingDeclValue, CCValue::GlobalValue()); |
| 993 | return !Result.isUninit(); |
| 994 | } |
| 995 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 996 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 997 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 998 | if (VD->isWeak()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 999 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1000 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1001 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1002 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1003 | const Expr *Init = VD->getAnyInitializer(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1004 | if (!Init || Init->isValueDependent()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1005 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1006 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1007 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1008 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1009 | if (APValue *V = VD->getEvaluatedValue()) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1010 | Result = CCValue(*V, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1011 | return !Result.isUninit(); |
| 1012 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1013 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1014 | if (VD->isEvaluatingValue()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1015 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1016 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1017 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1018 | |
| 1019 | VD->setEvaluatingValue(); |
| 1020 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1021 | Expr::EvalStatus EStatus; |
| 1022 | EvalInfo InitInfo(Info.Ctx, EStatus); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1023 | APValue EvalResult; |
| 1024 | InitInfo.setEvaluatingDecl(VD, EvalResult); |
| 1025 | LValue LVal; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1026 | LVal.set(VD); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1027 | // FIXME: The caller will need to know whether the value was a constant |
| 1028 | // expression. If not, we should propagate up a diagnostic. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1029 | if (!EvaluateConstantExpression(EvalResult, InitInfo, LVal, Init)) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1030 | // FIXME: If the evaluation failure was not permanent (for instance, if we |
| 1031 | // hit a variable with no declaration yet, or a constexpr function with no |
| 1032 | // definition yet), the standard is unclear as to how we should behave. |
| 1033 | // |
| 1034 | // Either the initializer should be evaluated when the variable is defined, |
| 1035 | // or a failed evaluation of the initializer should be reattempted each time |
| 1036 | // it is used. |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1037 | VD->setEvaluatedValue(APValue()); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1038 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1041 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1042 | VD->setEvaluatedValue(EvalResult); |
| 1043 | Result = CCValue(EvalResult, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1044 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1047 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1048 | Qualifiers Quals = T.getQualifiers(); |
| 1049 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1050 | } |
| 1051 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1052 | /// Get the base index of the given base class within an APValue representing |
| 1053 | /// the given derived class. |
| 1054 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1055 | const CXXRecordDecl *Base) { |
| 1056 | Base = Base->getCanonicalDecl(); |
| 1057 | unsigned Index = 0; |
| 1058 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1059 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1060 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1061 | return Index; |
| 1062 | } |
| 1063 | |
| 1064 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1065 | } |
| 1066 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1067 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1068 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
| 1069 | CCValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1070 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1071 | if (Sub.Invalid || Sub.OnePastTheEnd) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1072 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1073 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1074 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1075 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1076 | return true; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1077 | |
| 1078 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 1079 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1080 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1081 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1082 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1083 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1084 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1085 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1086 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1087 | if (CAT->getSize().ule(Index)) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1088 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1089 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1090 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1091 | if (O->getArrayInitializedElts() > Index) |
| 1092 | O = &O->getArrayInitializedElt(Index); |
| 1093 | else |
| 1094 | O = &O->getArrayFiller(); |
| 1095 | ObjType = CAT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1096 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| 1097 | // Next subobject is a class, struct or union field. |
| 1098 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1099 | if (RD->isUnion()) { |
| 1100 | const FieldDecl *UnionField = O->getUnionField(); |
| 1101 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1102 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1103 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1104 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1105 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1106 | O = &O->getUnionValue(); |
| 1107 | } else |
| 1108 | O = &O->getStructField(Field->getFieldIndex()); |
| 1109 | ObjType = Field->getType(); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1110 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1111 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1112 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1113 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1114 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1115 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1116 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1117 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1118 | if (O->isUninit()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1119 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1120 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1121 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1124 | Obj = CCValue(*O, CCValue::GlobalValue()); |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1128 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1129 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1130 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1131 | /// |
| 1132 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1133 | /// \param Conv - The expression for which we are performing the conversion. |
| 1134 | /// Used for diagnostics. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1135 | /// \param Type - The type we expect this conversion to produce. |
| 1136 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1137 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1138 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1139 | QualType Type, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1140 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1141 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1142 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1143 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1144 | if (!LVal.Base) { |
| 1145 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1146 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1147 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1148 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1149 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1150 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1151 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1152 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1153 | // expressions are constant expressions too. Inside constexpr functions, |
| 1154 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1155 | // In C, such things can also be folded, although they are not ICEs. |
| 1156 | // |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1157 | // FIXME: volatile-qualified ParmVarDecls need special handling. A literal |
| 1158 | // interpretation of C++11 suggests that volatile parameters are OK if |
| 1159 | // they're never read (there's no prohibition against constructing volatile |
| 1160 | // objects in constant expressions), but lvalue-to-rvalue conversions on |
| 1161 | // them are not permitted. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1162 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1163 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1164 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1165 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1168 | QualType VT = VD->getType(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1169 | if (!isa<ParmVarDecl>(VD)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1170 | if (!IsConstNonVolatile(VT)) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1171 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1172 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1173 | } |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 1174 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1175 | if (!VT->isIntegralOrEnumerationType() && !VT->isRealFloatingType() && |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1176 | !VD->isConstexpr()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1177 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1178 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1179 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1180 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1181 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1184 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1185 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1186 | |
| 1187 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1188 | // conversion. This happens when the declaration and the lvalue should be |
| 1189 | // considered synonymous, for instance when initializing an array of char |
| 1190 | // from a string literal. Continue as if the initializer lvalue was the |
| 1191 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1192 | assert(RVal.getLValueOffset().isZero() && |
| 1193 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1194 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1195 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1198 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1199 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 1200 | const SubobjectDesignator &Designator = LVal.Designator; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1201 | if (Designator.Invalid || Designator.Entries.size() != 1) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1202 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1203 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1204 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1205 | |
| 1206 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1207 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1208 | if (Index > S->getLength()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1209 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1210 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1211 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1212 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1213 | Type->isUnsignedIntegerType()); |
| 1214 | if (Index < S->getLength()) |
| 1215 | Value = S->getCodeUnit(Index); |
| 1216 | RVal = CCValue(Value); |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1220 | if (Frame) { |
| 1221 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1222 | // value from the relevant stack frame. |
| 1223 | RVal = Frame->Temporaries[Base]; |
| 1224 | } else if (const CompoundLiteralExpr *CLE |
| 1225 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1226 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1227 | // initializer until now for such expressions. Such an expression can't be |
| 1228 | // an ICE in C, so this only matters for fold. |
| 1229 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1230 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1231 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1232 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1233 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1234 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1235 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1236 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1237 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1238 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1241 | /// Build an lvalue for the object argument of a member function call. |
| 1242 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1243 | LValue &This) { |
| 1244 | if (Object->getType()->isPointerType()) |
| 1245 | return EvaluatePointer(Object, This, Info); |
| 1246 | |
| 1247 | if (Object->isGLValue()) |
| 1248 | return EvaluateLValue(Object, This, Info); |
| 1249 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1250 | if (Object->getType()->isLiteralType()) |
| 1251 | return EvaluateTemporary(Object, This, Info); |
| 1252 | |
| 1253 | return false; |
| 1254 | } |
| 1255 | |
| 1256 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1257 | /// lvalue referring to the result. |
| 1258 | /// |
| 1259 | /// \param Info - Information about the ongoing evaluation. |
| 1260 | /// \param BO - The member pointer access operation. |
| 1261 | /// \param LV - Filled in with a reference to the resulting object. |
| 1262 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1263 | /// the resulting LValue subobject designator. This is not possible when |
| 1264 | /// creating a bound member function. |
| 1265 | /// \return The field or method declaration to which the member pointer refers, |
| 1266 | /// or 0 if evaluation fails. |
| 1267 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1268 | const BinaryOperator *BO, |
| 1269 | LValue &LV, |
| 1270 | bool IncludeMember = true) { |
| 1271 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1272 | |
| 1273 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) |
| 1274 | return 0; |
| 1275 | |
| 1276 | MemberPtr MemPtr; |
| 1277 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1278 | return 0; |
| 1279 | |
| 1280 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1281 | // member value, the behavior is undefined. |
| 1282 | if (!MemPtr.getDecl()) |
| 1283 | return 0; |
| 1284 | |
| 1285 | if (MemPtr.isDerivedMember()) { |
| 1286 | // This is a member of some derived class. Truncate LV appropriately. |
| 1287 | const CXXRecordDecl *MostDerivedType; |
| 1288 | unsigned MostDerivedPathLength; |
| 1289 | bool MostDerivedIsArrayElement; |
| 1290 | if (!FindMostDerivedObject(Info, LV, MostDerivedType, MostDerivedPathLength, |
| 1291 | MostDerivedIsArrayElement)) |
| 1292 | return 0; |
| 1293 | |
| 1294 | // The end of the derived-to-base path for the base object must match the |
| 1295 | // derived-to-base path for the member pointer. |
| 1296 | if (MostDerivedPathLength + MemPtr.Path.size() > |
| 1297 | LV.Designator.Entries.size()) |
| 1298 | return 0; |
| 1299 | unsigned PathLengthToMember = |
| 1300 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1301 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1302 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1303 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1304 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1305 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | // Truncate the lvalue to the appropriate derived class. |
| 1310 | bool ResultIsArray = false; |
| 1311 | if (PathLengthToMember == MostDerivedPathLength) |
| 1312 | ResultIsArray = MostDerivedIsArrayElement; |
| 1313 | TruncateLValueBasePath(Info, LV, MemPtr.getContainingRecord(), |
| 1314 | PathLengthToMember, ResultIsArray); |
| 1315 | } else if (!MemPtr.Path.empty()) { |
| 1316 | // Extend the LValue path with the member pointer's path. |
| 1317 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1318 | MemPtr.Path.size() + IncludeMember); |
| 1319 | |
| 1320 | // Walk down to the appropriate base class. |
| 1321 | QualType LVType = BO->getLHS()->getType(); |
| 1322 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1323 | LVType = PT->getPointeeType(); |
| 1324 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1325 | assert(RD && "member pointer access on non-class-type expression"); |
| 1326 | // The first class in the path is that of the lvalue. |
| 1327 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1328 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
| 1329 | HandleLValueDirectBase(Info, LV, RD, Base); |
| 1330 | RD = Base; |
| 1331 | } |
| 1332 | // Finally cast to the class containing the member. |
| 1333 | HandleLValueDirectBase(Info, LV, RD, MemPtr.getContainingRecord()); |
| 1334 | } |
| 1335 | |
| 1336 | // Add the member. Note that we cannot build bound member functions here. |
| 1337 | if (IncludeMember) { |
| 1338 | // FIXME: Deal with IndirectFieldDecls. |
| 1339 | const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()); |
| 1340 | if (!FD) return 0; |
| 1341 | HandleLValueMember(Info, LV, FD); |
| 1342 | } |
| 1343 | |
| 1344 | return MemPtr.getDecl(); |
| 1345 | } |
| 1346 | |
| 1347 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1348 | /// the provided lvalue, which currently refers to the base object. |
| 1349 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1350 | LValue &Result) { |
| 1351 | const CXXRecordDecl *MostDerivedType; |
| 1352 | unsigned MostDerivedPathLength; |
| 1353 | bool MostDerivedIsArrayElement; |
| 1354 | |
| 1355 | // Check this cast doesn't take us outside the object. |
| 1356 | if (!FindMostDerivedObject(Info, Result, MostDerivedType, |
| 1357 | MostDerivedPathLength, |
| 1358 | MostDerivedIsArrayElement)) |
| 1359 | return false; |
| 1360 | SubobjectDesignator &D = Result.Designator; |
| 1361 | if (MostDerivedPathLength + E->path_size() > D.Entries.size()) |
| 1362 | return false; |
| 1363 | |
| 1364 | // Check the type of the final cast. We don't need to check the path, |
| 1365 | // since a cast can only be formed if the path is unique. |
| 1366 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
| 1367 | bool ResultIsArray = false; |
| 1368 | QualType TargetQT = E->getType(); |
| 1369 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 1370 | TargetQT = PT->getPointeeType(); |
| 1371 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 1372 | const CXXRecordDecl *FinalType; |
| 1373 | if (NewEntriesSize == MostDerivedPathLength) { |
| 1374 | ResultIsArray = MostDerivedIsArrayElement; |
| 1375 | FinalType = MostDerivedType; |
| 1376 | } else |
| 1377 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
| 1378 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) |
| 1379 | return false; |
| 1380 | |
| 1381 | // Truncate the lvalue to the appropriate derived class. |
| 1382 | TruncateLValueBasePath(Info, Result, TargetType, NewEntriesSize, |
| 1383 | ResultIsArray); |
| 1384 | return true; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1387 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1388 | enum EvalStmtResult { |
| 1389 | /// Evaluation failed. |
| 1390 | ESR_Failed, |
| 1391 | /// Hit a 'return' statement. |
| 1392 | ESR_Returned, |
| 1393 | /// Evaluation succeeded. |
| 1394 | ESR_Succeeded |
| 1395 | }; |
| 1396 | } |
| 1397 | |
| 1398 | // Evaluate a statement. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1399 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1400 | const Stmt *S) { |
| 1401 | switch (S->getStmtClass()) { |
| 1402 | default: |
| 1403 | return ESR_Failed; |
| 1404 | |
| 1405 | case Stmt::NullStmtClass: |
| 1406 | case Stmt::DeclStmtClass: |
| 1407 | return ESR_Succeeded; |
| 1408 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1409 | case Stmt::ReturnStmtClass: { |
| 1410 | CCValue CCResult; |
| 1411 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
| 1412 | if (!Evaluate(CCResult, Info, RetExpr) || |
| 1413 | !CheckConstantExpression(Info, RetExpr, CCResult, Result, |
| 1414 | CCEK_ReturnValue)) |
| 1415 | return ESR_Failed; |
| 1416 | return ESR_Returned; |
| 1417 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1418 | |
| 1419 | case Stmt::CompoundStmtClass: { |
| 1420 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 1421 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 1422 | BE = CS->body_end(); BI != BE; ++BI) { |
| 1423 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 1424 | if (ESR != ESR_Succeeded) |
| 1425 | return ESR; |
| 1426 | } |
| 1427 | return ESR_Succeeded; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1432 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 1433 | /// expression. |
| 1434 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 1435 | const FunctionDecl *Declaration, |
| 1436 | const FunctionDecl *Definition) { |
| 1437 | // Can we evaluate this function call? |
| 1438 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 1439 | return true; |
| 1440 | |
| 1441 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1442 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
| 1443 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 1444 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 1445 | << DiagDecl; |
| 1446 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 1447 | } else { |
| 1448 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 1449 | } |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1453 | namespace { |
Richard Smith | cd99b07 | 2011-11-11 05:48:57 +0000 | [diff] [blame] | 1454 | typedef SmallVector<CCValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 1458 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 1459 | EvalInfo &Info) { |
| 1460 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 1461 | I != E; ++I) |
| 1462 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| 1463 | return false; |
| 1464 | return true; |
| 1465 | } |
| 1466 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1467 | /// Evaluate a function call. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1468 | static bool HandleFunctionCall(const Expr *CallExpr, const LValue *This, |
| 1469 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1470 | EvalInfo &Info, APValue &Result) { |
| 1471 | if (!Info.CheckCallLimit(CallExpr->getExprLoc())) |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1474 | ArgVector ArgValues(Args.size()); |
| 1475 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1476 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1477 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1478 | CallStackFrame Frame(Info, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1479 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 1480 | } |
| 1481 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1482 | /// Evaluate a constructor call. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1483 | static bool HandleConstructorCall(const Expr *CallExpr, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1484 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1485 | const CXXConstructorDecl *Definition, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1486 | EvalInfo &Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1487 | APValue &Result) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1488 | if (!Info.CheckCallLimit(CallExpr->getExprLoc())) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1489 | return false; |
| 1490 | |
| 1491 | ArgVector ArgValues(Args.size()); |
| 1492 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 1493 | return false; |
| 1494 | |
| 1495 | CallStackFrame Frame(Info, &This, ArgValues.data()); |
| 1496 | |
| 1497 | // If it's a delegating constructor, just delegate. |
| 1498 | if (Definition->isDelegatingConstructor()) { |
| 1499 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| 1500 | return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); |
| 1501 | } |
| 1502 | |
| 1503 | // Reserve space for the struct members. |
| 1504 | const CXXRecordDecl *RD = Definition->getParent(); |
| 1505 | if (!RD->isUnion()) |
| 1506 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 1507 | std::distance(RD->field_begin(), RD->field_end())); |
| 1508 | |
| 1509 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1510 | |
| 1511 | unsigned BasesSeen = 0; |
| 1512 | #ifndef NDEBUG |
| 1513 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 1514 | #endif |
| 1515 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 1516 | E = Definition->init_end(); I != E; ++I) { |
| 1517 | if ((*I)->isBaseInitializer()) { |
| 1518 | QualType BaseType((*I)->getBaseClass(), 0); |
| 1519 | #ifndef NDEBUG |
| 1520 | // Non-virtual base classes are initialized in the order in the class |
| 1521 | // definition. We cannot have a virtual base class for a literal type. |
| 1522 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 1523 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 1524 | "base class initializers not in expected order"); |
| 1525 | ++BaseIt; |
| 1526 | #endif |
| 1527 | LValue Subobject = This; |
| 1528 | HandleLValueDirectBase(Info, Subobject, RD, |
| 1529 | BaseType->getAsCXXRecordDecl(), &Layout); |
| 1530 | if (!EvaluateConstantExpression(Result.getStructBase(BasesSeen++), Info, |
| 1531 | Subobject, (*I)->getInit())) |
| 1532 | return false; |
| 1533 | } else if (FieldDecl *FD = (*I)->getMember()) { |
| 1534 | LValue Subobject = This; |
| 1535 | HandleLValueMember(Info, Subobject, FD, &Layout); |
| 1536 | if (RD->isUnion()) { |
| 1537 | Result = APValue(FD); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1538 | if (!EvaluateConstantExpression(Result.getUnionValue(), Info, Subobject, |
| 1539 | (*I)->getInit(), CCEK_MemberInit)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1540 | return false; |
| 1541 | } else if (!EvaluateConstantExpression( |
| 1542 | Result.getStructField(FD->getFieldIndex()), |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1543 | Info, Subobject, (*I)->getInit(), CCEK_MemberInit)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1544 | return false; |
| 1545 | } else { |
| 1546 | // FIXME: handle indirect field initializers |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1547 | Info.Diag((*I)->getInit()->getExprLoc(), |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1548 | diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | return true; |
| 1554 | } |
| 1555 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1556 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1557 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1558 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1559 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1560 | public: |
| 1561 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1562 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1565 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1566 | return true; |
| 1567 | } |
| 1568 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1569 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1570 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1571 | return Visit(E->getResultExpr()); |
| 1572 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1573 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1574 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1575 | return true; |
| 1576 | return false; |
| 1577 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1578 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1579 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1580 | return true; |
| 1581 | return false; |
| 1582 | } |
| 1583 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1584 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1585 | return true; |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1589 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 1590 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1591 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 1592 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 1593 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1594 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1595 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 1596 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 1597 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 1598 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 1599 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 1600 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1601 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1602 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1603 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1604 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1605 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1606 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 1607 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 1608 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 1609 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1610 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1611 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 1612 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 1613 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 1614 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 1615 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1616 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1617 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 1618 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1619 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1620 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1623 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1624 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 1625 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1626 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 1627 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 1628 | return false; |
| 1629 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1630 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1631 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1632 | }; |
| 1633 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1634 | class OpaqueValueEvaluation { |
| 1635 | EvalInfo &info; |
| 1636 | OpaqueValueExpr *opaqueValue; |
| 1637 | |
| 1638 | public: |
| 1639 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 1640 | Expr *value) |
| 1641 | : info(info), opaqueValue(opaqueValue) { |
| 1642 | |
| 1643 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1644 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1645 | this->opaqueValue = 0; |
| 1646 | return; |
| 1647 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | bool hasError() const { return opaqueValue == 0; } |
| 1651 | |
| 1652 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1653 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 1654 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1655 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 1656 | } |
| 1657 | }; |
| 1658 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1659 | } // end anonymous namespace |
| 1660 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1661 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1662 | // Generic Evaluation |
| 1663 | //===----------------------------------------------------------------------===// |
| 1664 | namespace { |
| 1665 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1666 | // FIXME: RetTy is always bool. Remove it. |
| 1667 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1668 | class ExprEvaluatorBase |
| 1669 | : public ConstStmtVisitor<Derived, RetTy> { |
| 1670 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1671 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1672 | return static_cast<Derived*>(this)->Success(V, E); |
| 1673 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1674 | RetTy DerivedValueInitialization(const Expr *E) { |
| 1675 | return static_cast<Derived*>(this)->ValueInitialization(E); |
| 1676 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1677 | |
| 1678 | protected: |
| 1679 | EvalInfo &Info; |
| 1680 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 1681 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 1682 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1683 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | d509342 | 2011-12-12 09:41:58 +0000 | [diff] [blame] | 1684 | return Info.CCEDiag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /// Report an evaluation error. This should only be called when an error is |
| 1688 | /// first discovered. When propagating an error, just return false. |
| 1689 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1690 | Info.Diag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1691 | return false; |
| 1692 | } |
| 1693 | bool Error(const Expr *E) { |
| 1694 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 1695 | } |
| 1696 | |
| 1697 | RetTy ValueInitialization(const Expr *E) { return Error(E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1698 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1699 | public: |
| 1700 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 1701 | |
| 1702 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1703 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1704 | } |
| 1705 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1706 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | RetTy VisitParenExpr(const ParenExpr *E) |
| 1710 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1711 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 1712 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1713 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 1714 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 1715 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 1716 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1717 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 1718 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 1719 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 1720 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 1721 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 1722 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1723 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 1724 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 1725 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 1726 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 1727 | } |
| 1728 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 1729 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 1730 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 1731 | } |
| 1732 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1733 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 1734 | switch (E->getOpcode()) { |
| 1735 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1736 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1737 | |
| 1738 | case BO_Comma: |
| 1739 | VisitIgnoredValue(E->getLHS()); |
| 1740 | return StmtVisitorTy::Visit(E->getRHS()); |
| 1741 | |
| 1742 | case BO_PtrMemD: |
| 1743 | case BO_PtrMemI: { |
| 1744 | LValue Obj; |
| 1745 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 1746 | return false; |
| 1747 | CCValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1748 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1749 | return false; |
| 1750 | return DerivedSuccess(Result, E); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1755 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 1756 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 1757 | if (opaque.hasError()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1758 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1759 | |
| 1760 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1761 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1762 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1763 | |
| 1764 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 1765 | } |
| 1766 | |
| 1767 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
| 1768 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1769 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1770 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1771 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1772 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1773 | return StmtVisitorTy::Visit(EvalExpr); |
| 1774 | } |
| 1775 | |
| 1776 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1777 | const CCValue *Value = Info.getOpaqueValue(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 1778 | if (!Value) { |
| 1779 | const Expr *Source = E->getSourceExpr(); |
| 1780 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1781 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 1782 | if (Source == E) { // sanity checking. |
| 1783 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1784 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 1785 | } |
| 1786 | return StmtVisitorTy::Visit(Source); |
| 1787 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1788 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1789 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1790 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1791 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1792 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1793 | QualType CalleeType = Callee->getType(); |
| 1794 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1795 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1796 | LValue *This = 0, ThisVal; |
| 1797 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 1798 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1799 | // Extract function decl and 'this' pointer from the callee. |
| 1800 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1801 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1802 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 1803 | // Explicit bound member calls, such as x.f() or p->g(); |
| 1804 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1805 | return false; |
| 1806 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1807 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1808 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 1809 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1810 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 1811 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1812 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1813 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1814 | return Error(Callee); |
| 1815 | |
| 1816 | FD = dyn_cast<FunctionDecl>(Member); |
| 1817 | if (!FD) |
| 1818 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1819 | } else if (CalleeType->isFunctionPointerType()) { |
| 1820 | CCValue Call; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1821 | if (!Evaluate(Call, Info, Callee)) |
| 1822 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1823 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1824 | if (!Call.isLValue() || !Call.getLValueOffset().isZero()) |
| 1825 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1826 | FD = dyn_cast_or_null<FunctionDecl>( |
| 1827 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1828 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1829 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1830 | |
| 1831 | // Overloaded operator calls to member functions are represented as normal |
| 1832 | // calls with '*this' as the first argument. |
| 1833 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 1834 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1835 | // FIXME: When selecting an implicit conversion for an overloaded |
| 1836 | // operator delete, we sometimes try to evaluate calls to conversion |
| 1837 | // operators without a 'this' parameter! |
| 1838 | if (Args.empty()) |
| 1839 | return Error(E); |
| 1840 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1841 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 1842 | return false; |
| 1843 | This = &ThisVal; |
| 1844 | Args = Args.slice(1); |
| 1845 | } |
| 1846 | |
| 1847 | // Don't call function pointers which have been cast to some other type. |
| 1848 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1849 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1850 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1851 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1852 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1853 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1854 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1855 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1856 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1857 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
| 1858 | !HandleFunctionCall(E, This, Args, Body, Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1859 | return false; |
| 1860 | |
| 1861 | return DerivedSuccess(CCValue(Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1864 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 1865 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 1866 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1867 | RetTy VisitInitListExpr(const InitListExpr *E) { |
| 1868 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1869 | if (E->getNumInits() == 0) |
| 1870 | return DerivedValueInitialization(E); |
| 1871 | if (E->getNumInits() == 1) |
| 1872 | return StmtVisitorTy::Visit(E->getInit(0)); |
| 1873 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1874 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1875 | } |
| 1876 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 1877 | return DerivedValueInitialization(E); |
| 1878 | } |
| 1879 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
| 1880 | return DerivedValueInitialization(E); |
| 1881 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1882 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
| 1883 | return DerivedValueInitialization(E); |
| 1884 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1885 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1886 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 1887 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 1888 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 1889 | |
| 1890 | CCValue Val; |
| 1891 | if (!Evaluate(Val, Info, E->getBase())) |
| 1892 | return false; |
| 1893 | |
| 1894 | QualType BaseTy = E->getBase()->getType(); |
| 1895 | |
| 1896 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1897 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1898 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 1899 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1900 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1901 | |
| 1902 | SubobjectDesignator Designator; |
| 1903 | Designator.addDecl(FD); |
| 1904 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1905 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1906 | DerivedSuccess(Val, E); |
| 1907 | } |
| 1908 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1909 | RetTy VisitCastExpr(const CastExpr *E) { |
| 1910 | switch (E->getCastKind()) { |
| 1911 | default: |
| 1912 | break; |
| 1913 | |
| 1914 | case CK_NoOp: |
| 1915 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 1916 | |
| 1917 | case CK_LValueToRValue: { |
| 1918 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1919 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 1920 | return false; |
| 1921 | CCValue RVal; |
| 1922 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LVal, RVal)) |
| 1923 | return false; |
| 1924 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1928 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1931 | /// Visit a value which is evaluated, but whose value is ignored. |
| 1932 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1933 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1934 | if (!Evaluate(Scratch, Info, E)) |
| 1935 | Info.EvalStatus.HasSideEffects = true; |
| 1936 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1937 | }; |
| 1938 | |
| 1939 | } |
| 1940 | |
| 1941 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1942 | // Common base class for lvalue and temporary evaluation. |
| 1943 | //===----------------------------------------------------------------------===// |
| 1944 | namespace { |
| 1945 | template<class Derived> |
| 1946 | class LValueExprEvaluatorBase |
| 1947 | : public ExprEvaluatorBase<Derived, bool> { |
| 1948 | protected: |
| 1949 | LValue &Result; |
| 1950 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 1951 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 1952 | |
| 1953 | bool Success(APValue::LValueBase B) { |
| 1954 | Result.set(B); |
| 1955 | return true; |
| 1956 | } |
| 1957 | |
| 1958 | public: |
| 1959 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 1960 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 1961 | |
| 1962 | bool Success(const CCValue &V, const Expr *E) { |
| 1963 | Result.setFrom(V); |
| 1964 | return true; |
| 1965 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1966 | |
| 1967 | bool CheckValidLValue() { |
| 1968 | // C++11 [basic.lval]p1: An lvalue designates a function or an object. Hence |
| 1969 | // there are no null references, nor once-past-the-end references. |
| 1970 | // FIXME: Check for one-past-the-end array indices |
| 1971 | return Result.Base && !Result.Designator.Invalid && |
| 1972 | !Result.Designator.OnePastTheEnd; |
| 1973 | } |
| 1974 | |
| 1975 | bool VisitMemberExpr(const MemberExpr *E) { |
| 1976 | // Handle non-static data members. |
| 1977 | QualType BaseTy; |
| 1978 | if (E->isArrow()) { |
| 1979 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 1980 | return false; |
| 1981 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1982 | } else if (E->getBase()->isRValue()) { |
| 1983 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 1984 | return false; |
| 1985 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1986 | } else { |
| 1987 | if (!this->Visit(E->getBase())) |
| 1988 | return false; |
| 1989 | BaseTy = E->getBase()->getType(); |
| 1990 | } |
| 1991 | // FIXME: In C++11, require the result to be a valid lvalue. |
| 1992 | |
| 1993 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 1994 | // FIXME: Handle IndirectFieldDecls |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1995 | if (!FD) return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1996 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 1997 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 1998 | (void)BaseTy; |
| 1999 | |
| 2000 | HandleLValueMember(this->Info, Result, FD); |
| 2001 | |
| 2002 | if (FD->getType()->isReferenceType()) { |
| 2003 | CCValue RefValue; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2004 | if (!HandleLValueToRValueConversion(this->Info, E, FD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2005 | RefValue)) |
| 2006 | return false; |
| 2007 | return Success(RefValue, E); |
| 2008 | } |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
| 2012 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2013 | switch (E->getOpcode()) { |
| 2014 | default: |
| 2015 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2016 | |
| 2017 | case BO_PtrMemD: |
| 2018 | case BO_PtrMemI: |
| 2019 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | bool VisitCastExpr(const CastExpr *E) { |
| 2024 | switch (E->getCastKind()) { |
| 2025 | default: |
| 2026 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2027 | |
| 2028 | case CK_DerivedToBase: |
| 2029 | case CK_UncheckedDerivedToBase: { |
| 2030 | if (!this->Visit(E->getSubExpr())) |
| 2031 | return false; |
| 2032 | if (!CheckValidLValue()) |
| 2033 | return false; |
| 2034 | |
| 2035 | // Now figure out the necessary offset to add to the base LV to get from |
| 2036 | // the derived class to the base class. |
| 2037 | QualType Type = E->getSubExpr()->getType(); |
| 2038 | |
| 2039 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2040 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2041 | if (!HandleLValueBase(this->Info, Result, Type->getAsCXXRecordDecl(), |
| 2042 | *PathI)) |
| 2043 | return false; |
| 2044 | Type = (*PathI)->getType(); |
| 2045 | } |
| 2046 | |
| 2047 | return true; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | }; |
| 2052 | } |
| 2053 | |
| 2054 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2055 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2056 | // |
| 2057 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2058 | // function designators (in C), decl references to void objects (in C), and |
| 2059 | // temporaries (if building with -Wno-address-of-temporary). |
| 2060 | // |
| 2061 | // LValue evaluation produces values comprising a base expression of one of the |
| 2062 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2063 | // - Declarations |
| 2064 | // * VarDecl |
| 2065 | // * FunctionDecl |
| 2066 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2067 | // * CompoundLiteralExpr in C |
| 2068 | // * StringLiteral |
| 2069 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2070 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2071 | // * ObjCEncodeExpr |
| 2072 | // * AddrLabelExpr |
| 2073 | // * BlockExpr |
| 2074 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2075 | // - Locals and temporaries |
| 2076 | // * Any Expr, with a Frame indicating the function in which the temporary was |
| 2077 | // evaluated. |
| 2078 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2079 | //===----------------------------------------------------------------------===// |
| 2080 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2081 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2082 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2083 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2084 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2085 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2087 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2088 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2089 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2090 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2091 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2092 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2093 | bool VisitMemberExpr(const MemberExpr *E); |
| 2094 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2095 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
| 2096 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2097 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2098 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2099 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2100 | switch (E->getCastKind()) { |
| 2101 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2102 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2103 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2104 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2105 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2106 | if (!Visit(E->getSubExpr())) |
| 2107 | return false; |
| 2108 | Result.Designator.setInvalid(); |
| 2109 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2110 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2111 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2112 | if (!Visit(E->getSubExpr())) |
| 2113 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2114 | if (!CheckValidLValue()) |
| 2115 | return false; |
| 2116 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2117 | } |
| 2118 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2119 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2120 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2121 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2122 | }; |
| 2123 | } // end anonymous namespace |
| 2124 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2125 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2126 | /// expressions which are not glvalues, in a few cases: |
| 2127 | /// * function designators in C, |
| 2128 | /// * "extern void" objects, |
| 2129 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2130 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2131 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2132 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2133 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2134 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2137 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2138 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2139 | return Success(FD); |
| 2140 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2141 | return VisitVarDecl(E, VD); |
| 2142 | return Error(E); |
| 2143 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2144 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2145 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2146 | if (!VD->getType()->isReferenceType()) { |
| 2147 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2148 | Result.set(VD, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2149 | return true; |
| 2150 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2151 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2152 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2153 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2154 | CCValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2155 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2156 | return false; |
| 2157 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2160 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 2161 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2162 | if (E->GetTemporaryExpr()->isRValue()) { |
| 2163 | if (E->getType()->isRecordType() && E->getType()->isLiteralType()) |
| 2164 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 2165 | |
| 2166 | Result.set(E, Info.CurrentCall); |
| 2167 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 2168 | Result, E->GetTemporaryExpr()); |
| 2169 | } |
| 2170 | |
| 2171 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 2172 | // (for instance, if it's a bitfield). |
| 2173 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 2174 | if (!Visit(E->GetTemporaryExpr())) |
| 2175 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2176 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2177 | Info.CurrentCall->Temporaries[E])) |
| 2178 | return false; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2179 | Result.set(E, Info.CurrentCall); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2180 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2183 | bool |
| 2184 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2185 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2186 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 2187 | // 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] | 2188 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2191 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2192 | // Handle static data members. |
| 2193 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 2194 | VisitIgnoredValue(E->getBase()); |
| 2195 | return VisitVarDecl(E, VD); |
| 2196 | } |
| 2197 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2198 | // Handle static member functions. |
| 2199 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 2200 | if (MD->isStatic()) { |
| 2201 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2202 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2206 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2207 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2210 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2211 | // FIXME: Deal with vectors as array subscript bases. |
| 2212 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2213 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2214 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2215 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2216 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2218 | APSInt Index; |
| 2219 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2220 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2221 | int64_t IndexValue |
| 2222 | = Index.isSigned() ? Index.getSExtValue() |
| 2223 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2224 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2225 | // FIXME: In C++11, require the result to be a valid lvalue. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2226 | return HandleLValueArrayAdjustment(Info, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2227 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2228 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2229 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2230 | // FIXME: In C++11, require the result to be a valid lvalue. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2231 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2234 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2235 | // Pointer Evaluation |
| 2236 | //===----------------------------------------------------------------------===// |
| 2237 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2238 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2239 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2240 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2241 | LValue &Result; |
| 2242 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2243 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2244 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2245 | return true; |
| 2246 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2247 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2249 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2250 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2251 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2252 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2253 | Result.setFrom(V); |
| 2254 | return true; |
| 2255 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2256 | bool ValueInitialization(const Expr *E) { |
| 2257 | return Success((Expr*)0); |
| 2258 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2259 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2260 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2261 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2262 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2263 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2264 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2265 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2266 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2267 | bool VisitCallExpr(const CallExpr *E); |
| 2268 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2269 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2270 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2271 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 2272 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2273 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 2274 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2275 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2276 | Result = *Info.CurrentCall->This; |
| 2277 | return true; |
| 2278 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2279 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2280 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2281 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2282 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2283 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2284 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2285 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2286 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2289 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2290 | if (E->getOpcode() != BO_Add && |
| 2291 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2292 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2294 | const Expr *PExp = E->getLHS(); |
| 2295 | const Expr *IExp = E->getRHS(); |
| 2296 | if (IExp->getType()->isPointerType()) |
| 2297 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2299 | if (!EvaluatePointer(PExp, Result, Info)) |
| 2300 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2302 | llvm::APSInt Offset; |
| 2303 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 2304 | return false; |
| 2305 | int64_t AdditionalOffset |
| 2306 | = Offset.isSigned() ? Offset.getSExtValue() |
| 2307 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2308 | if (E->getOpcode() == BO_Sub) |
| 2309 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2310 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2311 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2312 | // FIXME: In C++11, require the result to be a valid lvalue. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2313 | return HandleLValueArrayAdjustment(Info, Result, Pointee, AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2314 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2315 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2316 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2317 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2320 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2321 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2322 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2323 | switch (E->getCastKind()) { |
| 2324 | default: |
| 2325 | break; |
| 2326 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2327 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2328 | case CK_CPointerToObjCPointerCast: |
| 2329 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2330 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2331 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 2332 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 2333 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 2334 | if (!E->getType()->isVoidPointerType()) { |
| 2335 | if (SubExpr->getType()->isVoidPointerType()) |
| 2336 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 2337 | << 3 << SubExpr->getType(); |
| 2338 | else |
| 2339 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 2340 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2341 | if (!Visit(SubExpr)) |
| 2342 | return false; |
| 2343 | Result.Designator.setInvalid(); |
| 2344 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2345 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2346 | case CK_DerivedToBase: |
| 2347 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2348 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2349 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2350 | if (!Result.Base && Result.Offset.isZero()) |
| 2351 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2352 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2353 | // 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] | 2354 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2355 | QualType Type = |
| 2356 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2357 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2358 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2359 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2360 | if (!HandleLValueBase(Info, Result, Type->getAsCXXRecordDecl(), *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2361 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2362 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2365 | return true; |
| 2366 | } |
| 2367 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2368 | case CK_BaseToDerived: |
| 2369 | if (!Visit(E->getSubExpr())) |
| 2370 | return false; |
| 2371 | if (!Result.Base && Result.Offset.isZero()) |
| 2372 | return true; |
| 2373 | return HandleBaseToDerivedCast(Info, E, Result); |
| 2374 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2375 | case CK_NullToPointer: |
| 2376 | return ValueInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2377 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2378 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2379 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 2380 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2381 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2382 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2383 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 2384 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2385 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2386 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 2387 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2388 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2389 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2390 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2391 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2392 | return true; |
| 2393 | } else { |
| 2394 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2395 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2396 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2399 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2400 | if (SubExpr->isGLValue()) { |
| 2401 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 2402 | return false; |
| 2403 | } else { |
| 2404 | Result.set(SubExpr, Info.CurrentCall); |
| 2405 | if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], |
| 2406 | Info, Result, SubExpr)) |
| 2407 | return false; |
| 2408 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2409 | // The result is a pointer to the first element of the array. |
| 2410 | Result.Designator.addIndex(0); |
| 2411 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2412 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2413 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 2414 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2417 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2419 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2420 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2421 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2422 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 2423 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2424 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2425 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2426 | |
| 2427 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2428 | // Member Pointer Evaluation |
| 2429 | //===----------------------------------------------------------------------===// |
| 2430 | |
| 2431 | namespace { |
| 2432 | class MemberPointerExprEvaluator |
| 2433 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 2434 | MemberPtr &Result; |
| 2435 | |
| 2436 | bool Success(const ValueDecl *D) { |
| 2437 | Result = MemberPtr(D); |
| 2438 | return true; |
| 2439 | } |
| 2440 | public: |
| 2441 | |
| 2442 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 2443 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2444 | |
| 2445 | bool Success(const CCValue &V, const Expr *E) { |
| 2446 | Result.setFrom(V); |
| 2447 | return true; |
| 2448 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2449 | bool ValueInitialization(const Expr *E) { |
| 2450 | return Success((const ValueDecl*)0); |
| 2451 | } |
| 2452 | |
| 2453 | bool VisitCastExpr(const CastExpr *E); |
| 2454 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 2455 | }; |
| 2456 | } // end anonymous namespace |
| 2457 | |
| 2458 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 2459 | EvalInfo &Info) { |
| 2460 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 2461 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 2462 | } |
| 2463 | |
| 2464 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2465 | switch (E->getCastKind()) { |
| 2466 | default: |
| 2467 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2468 | |
| 2469 | case CK_NullToMemberPointer: |
| 2470 | return ValueInitialization(E); |
| 2471 | |
| 2472 | case CK_BaseToDerivedMemberPointer: { |
| 2473 | if (!Visit(E->getSubExpr())) |
| 2474 | return false; |
| 2475 | if (E->path_empty()) |
| 2476 | return true; |
| 2477 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 2478 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 2479 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 2480 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 2481 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 2482 | PathI != PathE; ++PathI) { |
| 2483 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2484 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2485 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2486 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2487 | } |
| 2488 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 2489 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2490 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2491 | return true; |
| 2492 | } |
| 2493 | |
| 2494 | case CK_DerivedToBaseMemberPointer: |
| 2495 | if (!Visit(E->getSubExpr())) |
| 2496 | return false; |
| 2497 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2498 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2499 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 2500 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2501 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2502 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2503 | } |
| 2504 | return true; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2509 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 2510 | // member can be formed. |
| 2511 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 2512 | } |
| 2513 | |
| 2514 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2515 | // Record Evaluation |
| 2516 | //===----------------------------------------------------------------------===// |
| 2517 | |
| 2518 | namespace { |
| 2519 | class RecordExprEvaluator |
| 2520 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 2521 | const LValue &This; |
| 2522 | APValue &Result; |
| 2523 | public: |
| 2524 | |
| 2525 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 2526 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 2527 | |
| 2528 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2529 | return CheckConstantExpression(Info, E, V, Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2530 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2531 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2532 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2533 | bool VisitInitListExpr(const InitListExpr *E); |
| 2534 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 2535 | }; |
| 2536 | } |
| 2537 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2538 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2539 | switch (E->getCastKind()) { |
| 2540 | default: |
| 2541 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2542 | |
| 2543 | case CK_ConstructorConversion: |
| 2544 | return Visit(E->getSubExpr()); |
| 2545 | |
| 2546 | case CK_DerivedToBase: |
| 2547 | case CK_UncheckedDerivedToBase: { |
| 2548 | CCValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2549 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2550 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2551 | if (!DerivedObject.isStruct()) |
| 2552 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2553 | |
| 2554 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 2555 | APValue *Value = &DerivedObject; |
| 2556 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 2557 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2558 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 2559 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 2560 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 2561 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 2562 | RD = Base; |
| 2563 | } |
| 2564 | Result = *Value; |
| 2565 | return true; |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2570 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 2571 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 2572 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2573 | |
| 2574 | if (RD->isUnion()) { |
| 2575 | Result = APValue(E->getInitializedFieldInUnion()); |
| 2576 | if (!E->getNumInits()) |
| 2577 | return true; |
| 2578 | LValue Subobject = This; |
| 2579 | HandleLValueMember(Info, Subobject, E->getInitializedFieldInUnion(), |
| 2580 | &Layout); |
| 2581 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 2582 | Subobject, E->getInit(0)); |
| 2583 | } |
| 2584 | |
| 2585 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 2586 | "initializer list for class with base classes"); |
| 2587 | Result = APValue(APValue::UninitStruct(), 0, |
| 2588 | std::distance(RD->field_begin(), RD->field_end())); |
| 2589 | unsigned ElementNo = 0; |
| 2590 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 2591 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 2592 | // Anonymous bit-fields are not considered members of the class for |
| 2593 | // purposes of aggregate initialization. |
| 2594 | if (Field->isUnnamedBitfield()) |
| 2595 | continue; |
| 2596 | |
| 2597 | LValue Subobject = This; |
| 2598 | HandleLValueMember(Info, Subobject, *Field, &Layout); |
| 2599 | |
| 2600 | if (ElementNo < E->getNumInits()) { |
| 2601 | if (!EvaluateConstantExpression( |
| 2602 | Result.getStructField((*Field)->getFieldIndex()), |
| 2603 | Info, Subobject, E->getInit(ElementNo++))) |
| 2604 | return false; |
| 2605 | } else { |
| 2606 | // Perform an implicit value-initialization for members beyond the end of |
| 2607 | // the initializer list. |
| 2608 | ImplicitValueInitExpr VIE(Field->getType()); |
| 2609 | if (!EvaluateConstantExpression( |
| 2610 | Result.getStructField((*Field)->getFieldIndex()), |
| 2611 | Info, Subobject, &VIE)) |
| 2612 | return false; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | return true; |
| 2617 | } |
| 2618 | |
| 2619 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2620 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 2621 | const FunctionDecl *Definition = 0; |
| 2622 | FD->getBody(Definition); |
| 2623 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2624 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 2625 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2626 | |
| 2627 | // FIXME: Elide the copy/move construction wherever we can. |
| 2628 | if (E->isElidable()) |
| 2629 | if (const MaterializeTemporaryExpr *ME |
| 2630 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 2631 | return Visit(ME->GetTemporaryExpr()); |
| 2632 | |
| 2633 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2634 | return HandleConstructorCall(E, This, Args, |
| 2635 | cast<CXXConstructorDecl>(Definition), Info, |
| 2636 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 2640 | APValue &Result, EvalInfo &Info) { |
| 2641 | assert(E->isRValue() && E->getType()->isRecordType() && |
| 2642 | E->getType()->isLiteralType() && |
| 2643 | "can't evaluate expression as a record rvalue"); |
| 2644 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 2645 | } |
| 2646 | |
| 2647 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2648 | // Temporary Evaluation |
| 2649 | // |
| 2650 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 2651 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 2652 | // materialized so that a reference can bind to it. |
| 2653 | //===----------------------------------------------------------------------===// |
| 2654 | namespace { |
| 2655 | class TemporaryExprEvaluator |
| 2656 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 2657 | public: |
| 2658 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2659 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 2660 | |
| 2661 | /// Visit an expression which constructs the value of this temporary. |
| 2662 | bool VisitConstructExpr(const Expr *E) { |
| 2663 | Result.set(E, Info.CurrentCall); |
| 2664 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 2665 | Result, E); |
| 2666 | } |
| 2667 | |
| 2668 | bool VisitCastExpr(const CastExpr *E) { |
| 2669 | switch (E->getCastKind()) { |
| 2670 | default: |
| 2671 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2672 | |
| 2673 | case CK_ConstructorConversion: |
| 2674 | return VisitConstructExpr(E->getSubExpr()); |
| 2675 | } |
| 2676 | } |
| 2677 | bool VisitInitListExpr(const InitListExpr *E) { |
| 2678 | return VisitConstructExpr(E); |
| 2679 | } |
| 2680 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2681 | return VisitConstructExpr(E); |
| 2682 | } |
| 2683 | bool VisitCallExpr(const CallExpr *E) { |
| 2684 | return VisitConstructExpr(E); |
| 2685 | } |
| 2686 | }; |
| 2687 | } // end anonymous namespace |
| 2688 | |
| 2689 | /// Evaluate an expression of record type as a temporary. |
| 2690 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 2691 | assert(E->isRValue() && E->getType()->isRecordType() && |
| 2692 | E->getType()->isLiteralType()); |
| 2693 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 2694 | } |
| 2695 | |
| 2696 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2697 | // Vector Evaluation |
| 2698 | //===----------------------------------------------------------------------===// |
| 2699 | |
| 2700 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2701 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2702 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 2703 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2704 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2706 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 2707 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2709 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 2710 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 2711 | // FIXME: remove this APValue copy. |
| 2712 | Result = APValue(V.data(), V.size()); |
| 2713 | return true; |
| 2714 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 2715 | bool Success(const CCValue &V, const Expr *E) { |
| 2716 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2717 | Result = V; |
| 2718 | return true; |
| 2719 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2720 | bool ValueInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2722 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2723 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2724 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2725 | bool VisitInitListExpr(const InitListExpr *E); |
| 2726 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2727 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 2728 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2729 | // shufflevector, ExtVectorElementExpr |
| 2730 | // (Note that these require implementing conversions |
| 2731 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2732 | }; |
| 2733 | } // end anonymous namespace |
| 2734 | |
| 2735 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2736 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2737 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2740 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2741 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2742 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2744 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2745 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2746 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2747 | switch (E->getCastKind()) { |
| 2748 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2749 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2750 | if (SETy->isIntegerType()) { |
| 2751 | APSInt IntResult; |
| 2752 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2753 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2754 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2755 | } else if (SETy->isRealFloatingType()) { |
| 2756 | APFloat F(0.0); |
| 2757 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2758 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2759 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2760 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2761 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2762 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 2763 | |
| 2764 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2765 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 2766 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 2767 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2768 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2769 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2770 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2773 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2774 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2775 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2776 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2777 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2779 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2780 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2781 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2782 | // If a vector is initialized with a single element, that value |
| 2783 | // becomes every element of the vector, not just the first. |
| 2784 | // This is the behavior described in the IBM AltiVec documentation. |
| 2785 | if (NumInits == 1) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2786 | |
| 2787 | // Handle the case where the vector is initialized by another |
Tanya Lattner | b92ae0e | 2011-04-15 22:42:59 +0000 | [diff] [blame] | 2788 | // vector (OpenCL 6.1.6). |
| 2789 | if (E->getInit(0)->getType()->isVectorType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2790 | return Visit(E->getInit(0)); |
| 2791 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2792 | APValue InitValue; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2793 | if (EltTy->isIntegerType()) { |
| 2794 | llvm::APSInt sInt(32); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2795 | if (!EvaluateInteger(E->getInit(0), sInt, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2796 | return false; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2797 | InitValue = APValue(sInt); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2798 | } else { |
| 2799 | llvm::APFloat f(0.0); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2800 | if (!EvaluateFloat(E->getInit(0), f, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2801 | return false; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2802 | InitValue = APValue(f); |
| 2803 | } |
| 2804 | for (unsigned i = 0; i < NumElements; i++) { |
| 2805 | Elements.push_back(InitValue); |
| 2806 | } |
| 2807 | } else { |
| 2808 | for (unsigned i = 0; i < NumElements; i++) { |
| 2809 | if (EltTy->isIntegerType()) { |
| 2810 | llvm::APSInt sInt(32); |
| 2811 | if (i < NumInits) { |
| 2812 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2813 | return false; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2814 | } else { |
| 2815 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 2816 | } |
| 2817 | Elements.push_back(APValue(sInt)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2818 | } else { |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2819 | llvm::APFloat f(0.0); |
| 2820 | if (i < NumInits) { |
| 2821 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2822 | return false; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 2823 | } else { |
| 2824 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 2825 | } |
| 2826 | Elements.push_back(APValue(f)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2827 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2830 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2833 | bool |
| 2834 | VectorExprEvaluator::ValueInitialization(const Expr *E) { |
| 2835 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2836 | QualType EltTy = VT->getElementType(); |
| 2837 | APValue ZeroElement; |
| 2838 | if (EltTy->isIntegerType()) |
| 2839 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 2840 | else |
| 2841 | ZeroElement = |
| 2842 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 2843 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2844 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2845 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2848 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2849 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 2850 | return ValueInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2853 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2854 | // Array Evaluation |
| 2855 | //===----------------------------------------------------------------------===// |
| 2856 | |
| 2857 | namespace { |
| 2858 | class ArrayExprEvaluator |
| 2859 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2860 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2861 | APValue &Result; |
| 2862 | public: |
| 2863 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2864 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 2865 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2866 | |
| 2867 | bool Success(const APValue &V, const Expr *E) { |
| 2868 | assert(V.isArray() && "Expected array type"); |
| 2869 | Result = V; |
| 2870 | return true; |
| 2871 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2872 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2873 | bool ValueInitialization(const Expr *E) { |
| 2874 | const ConstantArrayType *CAT = |
| 2875 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2876 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2877 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2878 | |
| 2879 | Result = APValue(APValue::UninitArray(), 0, |
| 2880 | CAT->getSize().getZExtValue()); |
| 2881 | if (!Result.hasArrayFiller()) return true; |
| 2882 | |
| 2883 | // Value-initialize all elements. |
| 2884 | LValue Subobject = This; |
| 2885 | Subobject.Designator.addIndex(0); |
| 2886 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 2887 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 2888 | Subobject, &VIE); |
| 2889 | } |
| 2890 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2891 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2892 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2893 | }; |
| 2894 | } // end anonymous namespace |
| 2895 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2896 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 2897 | APValue &Result, EvalInfo &Info) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2898 | assert(E->isRValue() && E->getType()->isArrayType() && |
| 2899 | E->getType()->isLiteralType() && "not a literal array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2900 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 2904 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2905 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2906 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2907 | |
| 2908 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 2909 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2910 | LValue Subobject = This; |
| 2911 | Subobject.Designator.addIndex(0); |
| 2912 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2913 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2914 | I != End; ++I, ++Index) { |
| 2915 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), |
| 2916 | Info, Subobject, cast<Expr>(*I))) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2917 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2918 | if (!HandleLValueArrayAdjustment(Info, Subobject, CAT->getElementType(), 1)) |
| 2919 | return false; |
| 2920 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2921 | |
| 2922 | if (!Result.hasArrayFiller()) return true; |
| 2923 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2924 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 2925 | // but sometimes does: |
| 2926 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 2927 | // S s[10] = {}; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2928 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2929 | Subobject, E->getArrayFiller()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2932 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 2933 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 2934 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2935 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2936 | |
| 2937 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
| 2938 | if (!Result.hasArrayFiller()) |
| 2939 | return true; |
| 2940 | |
| 2941 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 2942 | const FunctionDecl *Definition = 0; |
| 2943 | FD->getBody(Definition); |
| 2944 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2945 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 2946 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2947 | |
| 2948 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 2949 | // but sometimes does: |
| 2950 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 2951 | // S s[10]; |
| 2952 | LValue Subobject = This; |
| 2953 | Subobject.Designator.addIndex(0); |
| 2954 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2955 | return HandleConstructorCall(E, Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2956 | cast<CXXConstructorDecl>(Definition), |
| 2957 | Info, Result.getArrayFiller()); |
| 2958 | } |
| 2959 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2960 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2961 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2962 | // |
| 2963 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 2964 | // types and back in constant folding. Integer values are thus represented |
| 2965 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2966 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2967 | |
| 2968 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2969 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2970 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2971 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2972 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2973 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2974 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2975 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2976 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 2977 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2978 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2979 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2980 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 2981 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2982 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2983 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2984 | return true; |
| 2985 | } |
| 2986 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2987 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2988 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 2989 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2990 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2991 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2992 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2993 | Result.getInt().setIsUnsigned( |
| 2994 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2995 | return true; |
| 2996 | } |
| 2997 | |
| 2998 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2999 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3000 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3001 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3002 | return true; |
| 3003 | } |
| 3004 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3005 | bool Success(CharUnits Size, const Expr *E) { |
| 3006 | return Success(Size.getQuantity(), E); |
| 3007 | } |
| 3008 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3009 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 3010 | if (V.isLValue()) { |
| 3011 | Result = V; |
| 3012 | return true; |
| 3013 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3014 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3016 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3017 | bool ValueInitialization(const Expr *E) { return Success(0, E); } |
| 3018 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3019 | //===--------------------------------------------------------------------===// |
| 3020 | // Visitor Methods |
| 3021 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3022 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3023 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3024 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3025 | } |
| 3026 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3027 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3028 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3029 | |
| 3030 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 3031 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3032 | if (CheckReferencedDecl(E, E->getDecl())) |
| 3033 | return true; |
| 3034 | |
| 3035 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3036 | } |
| 3037 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3038 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3039 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3040 | return true; |
| 3041 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3042 | |
| 3043 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3046 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3047 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3048 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3049 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 3050 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3051 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3052 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3053 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3054 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3055 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3058 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 3059 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3060 | return ValueInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3063 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3064 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3067 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 3068 | return Success(E->getValue(), E); |
| 3069 | } |
| 3070 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3071 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 3072 | return Success(E->getValue(), E); |
| 3073 | } |
| 3074 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3075 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 3076 | return Success(E->getValue(), E); |
| 3077 | } |
| 3078 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3079 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3080 | bool VisitUnaryImag(const UnaryOperator *E); |
| 3081 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 3082 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3083 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 3084 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3085 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3086 | CharUnits GetAlignOfExpr(const Expr *E); |
| 3087 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3088 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3089 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3090 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3091 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3092 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3093 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3094 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 3095 | /// produce either the integer value or a pointer. |
| 3096 | /// |
| 3097 | /// GCC has a heinous extension which folds casts between pointer types and |
| 3098 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 3099 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 3100 | /// Some simple arithmetic on such values is supported (they are treated much |
| 3101 | /// like char*). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3102 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3103 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3104 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3105 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3106 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3107 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3108 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3109 | CCValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3110 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3111 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3112 | if (!Val.isInt()) { |
| 3113 | // FIXME: It would be better to produce the diagnostic for casting |
| 3114 | // a pointer to an integer. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3115 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3116 | return false; |
| 3117 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3118 | Result = Val.getInt(); |
| 3119 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3120 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3121 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3122 | /// Check whether the given declaration can be directly converted to an integral |
| 3123 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 3124 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3125 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3126 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3127 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3128 | // Check for signedness/width mismatches between E type and ECD value. |
| 3129 | bool SameSign = (ECD->getInitVal().isSigned() |
| 3130 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 3131 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 3132 | == Info.Ctx.getIntWidth(E->getType())); |
| 3133 | if (SameSign && SameWidth) |
| 3134 | return Success(ECD->getInitVal(), E); |
| 3135 | else { |
| 3136 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 3137 | // by computing a new value matching the type of E. |
| 3138 | llvm::APSInt Val = ECD->getInitVal(); |
| 3139 | if (!SameSign) |
| 3140 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 3141 | if (!SameWidth) |
| 3142 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 3143 | return Success(Val, E); |
| 3144 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3145 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3146 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3149 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 3150 | /// as GCC. |
| 3151 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 3152 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3153 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3154 | enum gcc_type_class { |
| 3155 | no_type_class = -1, |
| 3156 | void_type_class, integer_type_class, char_type_class, |
| 3157 | enumeral_type_class, boolean_type_class, |
| 3158 | pointer_type_class, reference_type_class, offset_type_class, |
| 3159 | real_type_class, complex_type_class, |
| 3160 | function_type_class, method_type_class, |
| 3161 | record_type_class, union_type_class, |
| 3162 | array_type_class, string_type_class, |
| 3163 | lang_type_class |
| 3164 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | |
| 3166 | // 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] | 3167 | // ideal, however it is what gcc does. |
| 3168 | if (E->getNumArgs() == 0) |
| 3169 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3171 | QualType ArgTy = E->getArg(0)->getType(); |
| 3172 | if (ArgTy->isVoidType()) |
| 3173 | return void_type_class; |
| 3174 | else if (ArgTy->isEnumeralType()) |
| 3175 | return enumeral_type_class; |
| 3176 | else if (ArgTy->isBooleanType()) |
| 3177 | return boolean_type_class; |
| 3178 | else if (ArgTy->isCharType()) |
| 3179 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 3180 | else if (ArgTy->isIntegerType()) |
| 3181 | return integer_type_class; |
| 3182 | else if (ArgTy->isPointerType()) |
| 3183 | return pointer_type_class; |
| 3184 | else if (ArgTy->isReferenceType()) |
| 3185 | return reference_type_class; |
| 3186 | else if (ArgTy->isRealType()) |
| 3187 | return real_type_class; |
| 3188 | else if (ArgTy->isComplexType()) |
| 3189 | return complex_type_class; |
| 3190 | else if (ArgTy->isFunctionType()) |
| 3191 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 3192 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3193 | return record_type_class; |
| 3194 | else if (ArgTy->isUnionType()) |
| 3195 | return union_type_class; |
| 3196 | else if (ArgTy->isArrayType()) |
| 3197 | return array_type_class; |
| 3198 | else if (ArgTy->isUnionType()) |
| 3199 | return union_type_class; |
| 3200 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3201 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3202 | return -1; |
| 3203 | } |
| 3204 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3205 | /// Retrieves the "underlying object type" of the given expression, |
| 3206 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3207 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 3208 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 3209 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3210 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3211 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 3212 | if (isa<CompoundLiteralExpr>(E)) |
| 3213 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | return QualType(); |
| 3217 | } |
| 3218 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3219 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3220 | // TODO: Perhaps we should let LLVM lower this? |
| 3221 | LValue Base; |
| 3222 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 3223 | return false; |
| 3224 | |
| 3225 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3226 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3227 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3228 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3229 | if (T.isNull() || |
| 3230 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3231 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3232 | T->isVariablyModifiedType() || |
| 3233 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3234 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3235 | |
| 3236 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 3237 | CharUnits Offset = Base.getLValueOffset(); |
| 3238 | |
| 3239 | if (!Offset.isNegative() && Offset <= Size) |
| 3240 | Size -= Offset; |
| 3241 | else |
| 3242 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3243 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3246 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3247 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3248 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3249 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3250 | |
| 3251 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 3252 | if (TryEvaluateBuiltinObjectSize(E)) |
| 3253 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3254 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 3255 | // If evaluating the argument has side-effects we can't determine |
| 3256 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 3257 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3258 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 3259 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3260 | return Success(0, E); |
| 3261 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3262 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3263 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3266 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3267 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 3269 | case Builtin::BI__builtin_constant_p: { |
| 3270 | const Expr *Arg = E->getArg(0); |
| 3271 | QualType ArgType = Arg->getType(); |
| 3272 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 3273 | // are not precisely documented, but are as follows: |
| 3274 | // |
| 3275 | // - If the operand is of integral, floating, complex or enumeration type, |
| 3276 | // and can be folded to a known value of that type, it returns 1. |
| 3277 | // - If the operand and can be folded to a pointer to the first character |
| 3278 | // of a string literal (or such a pointer cast to an integral type), it |
| 3279 | // returns 1. |
| 3280 | // |
| 3281 | // Otherwise, it returns 0. |
| 3282 | // |
| 3283 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 3284 | // its support for this does not currently work. |
| 3285 | int IsConstant = 0; |
| 3286 | if (ArgType->isIntegralOrEnumerationType()) { |
| 3287 | // Note, a pointer cast to an integral type is only a constant if it is |
| 3288 | // a pointer to the first character of a string literal. |
| 3289 | Expr::EvalResult Result; |
| 3290 | if (Arg->EvaluateAsRValue(Result, Info.Ctx) && !Result.HasSideEffects) { |
| 3291 | APValue &V = Result.Val; |
| 3292 | if (V.getKind() == APValue::LValue) { |
| 3293 | if (const Expr *E = V.getLValueBase().dyn_cast<const Expr*>()) |
| 3294 | IsConstant = isa<StringLiteral>(E) && V.getLValueOffset().isZero(); |
| 3295 | } else { |
| 3296 | IsConstant = 1; |
| 3297 | } |
| 3298 | } |
| 3299 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 3300 | IsConstant = Arg->isEvaluatable(Info.Ctx); |
| 3301 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 3302 | LValue LV; |
| 3303 | // Use a separate EvalInfo: ignore constexpr parameter and 'this' bindings |
| 3304 | // during the check. |
| 3305 | Expr::EvalStatus Status; |
| 3306 | EvalInfo SubInfo(Info.Ctx, Status); |
| 3307 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, SubInfo) |
| 3308 | : EvaluatePointer(Arg, LV, SubInfo)) && |
| 3309 | !Status.HasSideEffects) |
| 3310 | if (const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>()) |
| 3311 | IsConstant = isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 3312 | } |
| 3313 | |
| 3314 | return Success(IsConstant, E); |
| 3315 | } |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3316 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3317 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3318 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 3319 | return Success(Operand, E); |
| 3320 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 3321 | |
| 3322 | case Builtin::BI__builtin_expect: |
| 3323 | return Visit(E->getArg(0)); |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3324 | |
| 3325 | case Builtin::BIstrlen: |
| 3326 | case Builtin::BI__builtin_strlen: |
| 3327 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 3328 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3329 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3330 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 3331 | // The string literal may have embedded null characters. Find the first |
| 3332 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3333 | StringRef Str = S->getString(); |
| 3334 | StringRef::size_type Pos = Str.find(0); |
| 3335 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 3336 | Str = Str.substr(0, Pos); |
| 3337 | |
| 3338 | return Success(Str.size(), E); |
| 3339 | } |
| 3340 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3341 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3342 | |
| 3343 | case Builtin::BI__atomic_is_lock_free: { |
| 3344 | APSInt SizeVal; |
| 3345 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 3346 | return false; |
| 3347 | |
| 3348 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 3349 | // of two less than the maximum inline atomic width, we know it is |
| 3350 | // lock-free. If the size isn't a power of two, or greater than the |
| 3351 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 3352 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 3353 | // the answer can only be determined at runtime; for example, 16-byte |
| 3354 | // atomics have lock-free implementations on some, but not all, |
| 3355 | // x86-64 processors. |
| 3356 | |
| 3357 | // Check power-of-two. |
| 3358 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 3359 | if (!Size.isPowerOfTwo()) |
| 3360 | #if 0 |
| 3361 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3362 | // settles. |
| 3363 | return Success(0, E); |
| 3364 | #else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3365 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3366 | #endif |
| 3367 | |
| 3368 | #if 0 |
| 3369 | // Check against promotion width. |
| 3370 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 3371 | // settles. |
| 3372 | unsigned PromoteWidthBits = |
| 3373 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 3374 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 3375 | return Success(0, E); |
| 3376 | #endif |
| 3377 | |
| 3378 | // Check against inlining width. |
| 3379 | unsigned InlineWidthBits = |
| 3380 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 3381 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 3382 | return Success(1, E); |
| 3383 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3384 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 3385 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 3386 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3387 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3388 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3389 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 3390 | if (!A.getLValueBase()) |
| 3391 | return !B.getLValueBase(); |
| 3392 | if (!B.getLValueBase()) |
| 3393 | return false; |
| 3394 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3395 | if (A.getLValueBase().getOpaqueValue() != |
| 3396 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3397 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 3398 | if (!ADecl) |
| 3399 | return false; |
| 3400 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 3401 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3402 | return false; |
| 3403 | } |
| 3404 | |
| 3405 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3406 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3409 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3410 | if (E->isAssignmentOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3411 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3412 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3413 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3414 | VisitIgnoredValue(E->getLHS()); |
| 3415 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | if (E->isLogicalOp()) { |
| 3419 | // These need to be handled specially because the operands aren't |
| 3420 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 3421 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3423 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3424 | // We were able to evaluate the LHS, see if we can get away with not |
| 3425 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3426 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3427 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3428 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3429 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3430 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3431 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3432 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3433 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3434 | } |
| 3435 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3436 | // FIXME: If both evaluations fail, we should produce the diagnostic from |
| 3437 | // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's |
| 3438 | // less clear how to diagnose this. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3439 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3440 | // We can't evaluate the LHS; however, sometimes the result |
| 3441 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3442 | if (rhsResult == (E->getOpcode() == BO_LOr)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3443 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 3444 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3445 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3446 | |
| 3447 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3450 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3451 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3452 | return false; |
| 3453 | } |
| 3454 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3455 | QualType LHSTy = E->getLHS()->getType(); |
| 3456 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3457 | |
| 3458 | if (LHSTy->isAnyComplexType()) { |
| 3459 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3460 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3461 | |
| 3462 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 3463 | return false; |
| 3464 | |
| 3465 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 3466 | return false; |
| 3467 | |
| 3468 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3470 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3472 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 3473 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3474 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3475 | return Success((CR_r == APFloat::cmpEqual && |
| 3476 | CR_i == APFloat::cmpEqual), E); |
| 3477 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3478 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3479 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3481 | CR_r == APFloat::cmpLessThan || |
| 3482 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3484 | CR_i == APFloat::cmpLessThan || |
| 3485 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3486 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3487 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3488 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3489 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 3490 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 3491 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3492 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3493 | "Invalid compex comparison."); |
| 3494 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 3495 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 3496 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 3497 | } |
| 3498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3500 | if (LHSTy->isRealFloatingType() && |
| 3501 | RHSTy->isRealFloatingType()) { |
| 3502 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3504 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 3505 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3506 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3507 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 3508 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3510 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 3511 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3512 | switch (E->getOpcode()) { |
| 3513 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3514 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3515 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3516 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3517 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3518 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3519 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3520 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3521 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3522 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3523 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3524 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3525 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3526 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 3528 | || CR == APFloat::cmpLessThan |
| 3529 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3530 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 3531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3533 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3534 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3535 | LValue LHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3536 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 3537 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3538 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3539 | LValue RHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3540 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 3541 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3542 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3543 | // Reject differing bases from the normal codepath; we special-case |
| 3544 | // comparisons to null. |
| 3545 | if (!HasSameBase(LHSValue, RHSValue)) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3546 | // Inequalities and subtractions between unrelated pointers have |
| 3547 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3548 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3549 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 3550 | // A constant address may compare equal to the address of a symbol. |
| 3551 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3552 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 3553 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 3554 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3555 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3556 | // It's implementation-defined whether distinct literals will have |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3557 | // distinct addresses. In clang, we do not guarantee the addresses are |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 3558 | // distinct. However, we do know that the address of a literal will be |
| 3559 | // non-null. |
| 3560 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 3561 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3562 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3563 | // We can't tell whether weak symbols will end up pointing to the same |
| 3564 | // object. |
| 3565 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3566 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3567 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 3568 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 3569 | // lead to inconsistent results for comparisons involving the address |
| 3570 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 3571 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 3572 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3573 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3574 | // FIXME: Implement the C++11 restrictions: |
| 3575 | // - Pointer subtractions must be on elements of the same array. |
| 3576 | // - Pointer comparisons must be between members with the same access. |
| 3577 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3578 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 3579 | QualType Type = E->getLHS()->getType(); |
| 3580 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3581 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3582 | CharUnits ElementSize; |
| 3583 | if (!HandleSizeof(Info, ElementType, ElementSize)) |
| 3584 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3585 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3586 | CharUnits Diff = LHSValue.getLValueOffset() - |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3587 | RHSValue.getLValueOffset(); |
| 3588 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3589 | } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 3590 | |
| 3591 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 3592 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 3593 | switch (E->getOpcode()) { |
| 3594 | default: llvm_unreachable("missing comparison operator"); |
| 3595 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 3596 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 3597 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 3598 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 3599 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 3600 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 3601 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3602 | } |
| 3603 | } |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3604 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 3605 | !RHSTy->isIntegralOrEnumerationType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3606 | // We can't continue from here for non-integral types. |
| 3607 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3610 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3611 | CCValue LHSVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3612 | if (!EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3613 | return false; |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 3614 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3615 | if (!Visit(E->getRHS())) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3616 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3617 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3618 | |
| 3619 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3620 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3621 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 3622 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3623 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3624 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3625 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3626 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 3627 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3628 | return true; |
| 3629 | } |
| 3630 | |
| 3631 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3632 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3633 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3634 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 3635 | LHSVal.getInt().getZExtValue()); |
| 3636 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3637 | return true; |
| 3638 | } |
| 3639 | |
| 3640 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3641 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3642 | return Error(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3643 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3644 | APSInt &LHS = LHSVal.getInt(); |
| 3645 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 3646 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3647 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3648 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3649 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3650 | case BO_Mul: return Success(LHS * RHS, E); |
| 3651 | case BO_Add: return Success(LHS + RHS, E); |
| 3652 | case BO_Sub: return Success(LHS - RHS, E); |
| 3653 | case BO_And: return Success(LHS & RHS, E); |
| 3654 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 3655 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3656 | case BO_Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 3657 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3658 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3659 | return Success(LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3660 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 3661 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3662 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3663 | return Success(LHS % RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3664 | case BO_Shl: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3665 | // During constant-folding, a negative shift is an opposite shift. |
| 3666 | if (RHS.isSigned() && RHS.isNegative()) { |
| 3667 | RHS = -RHS; |
| 3668 | goto shift_right; |
| 3669 | } |
| 3670 | |
| 3671 | shift_left: |
| 3672 | unsigned SA |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3673 | = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 3674 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3675 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3676 | case BO_Shr: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 3677 | // During constant-folding, a negative shift is an opposite shift. |
| 3678 | if (RHS.isSigned() && RHS.isNegative()) { |
| 3679 | RHS = -RHS; |
| 3680 | goto shift_left; |
| 3681 | } |
| 3682 | |
| 3683 | shift_right: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | unsigned SA = |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3685 | (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 3686 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3689 | case BO_LT: return Success(LHS < RHS, E); |
| 3690 | case BO_GT: return Success(LHS > RHS, E); |
| 3691 | case BO_LE: return Success(LHS <= RHS, E); |
| 3692 | case BO_GE: return Success(LHS >= RHS, E); |
| 3693 | case BO_EQ: return Success(LHS == RHS, E); |
| 3694 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 3695 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3698 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3699 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3700 | // the result is the size of the referenced type." |
| 3701 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3702 | // result shall be the alignment of the referenced type." |
| 3703 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 3704 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 3705 | |
| 3706 | // __alignof is defined to return the preferred alignment. |
| 3707 | return Info.Ctx.toCharUnitsFromBits( |
| 3708 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3711 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3712 | E = E->IgnoreParens(); |
| 3713 | |
| 3714 | // 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] | 3715 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3716 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3717 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 3718 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3719 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3720 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3721 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 3722 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 3723 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3724 | return GetAlignOfType(E->getType()); |
| 3725 | } |
| 3726 | |
| 3727 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3728 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 3729 | /// a result as the expression's type. |
| 3730 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 3731 | const UnaryExprOrTypeTraitExpr *E) { |
| 3732 | switch(E->getKind()) { |
| 3733 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3734 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3735 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3736 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3737 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 3738 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3739 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3740 | case UETT_VecStep: { |
| 3741 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3742 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3743 | if (Ty->isVectorType()) { |
| 3744 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 3745 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3746 | // The vec_step built-in functions that take a 3-component |
| 3747 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 3748 | if (n == 3) |
| 3749 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 3750 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3751 | return Success(n, E); |
| 3752 | } else |
| 3753 | return Success(1, E); |
| 3754 | } |
| 3755 | |
| 3756 | case UETT_SizeOf: { |
| 3757 | QualType SrcTy = E->getTypeOfArgument(); |
| 3758 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3759 | // the result is the size of the referenced type." |
| 3760 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3761 | // result shall be the alignment of the referenced type." |
| 3762 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 3763 | SrcTy = Ref->getPointeeType(); |
| 3764 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3765 | CharUnits Sizeof; |
| 3766 | if (!HandleSizeof(Info, SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3767 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3768 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
| 3772 | llvm_unreachable("unknown expr/type trait"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3773 | return Error(E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3776 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3777 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3778 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3779 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3780 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3781 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3782 | for (unsigned i = 0; i != n; ++i) { |
| 3783 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 3784 | switch (ON.getKind()) { |
| 3785 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3786 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3787 | APSInt IdxResult; |
| 3788 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 3789 | return false; |
| 3790 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 3791 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3792 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3793 | CurrentType = AT->getElementType(); |
| 3794 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 3795 | Result += IdxResult.getSExtValue() * ElementSize; |
| 3796 | break; |
| 3797 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3799 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 3800 | FieldDecl *MemberDecl = ON.getField(); |
| 3801 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3802 | if (!RT) |
| 3803 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3804 | RecordDecl *RD = RT->getDecl(); |
| 3805 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3806 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3807 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 3808 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3809 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 3810 | break; |
| 3811 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3812 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3813 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 3814 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3815 | return Error(OOE); |
| 3816 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3817 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 3818 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 3819 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3820 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3821 | |
| 3822 | // Find the layout of the class whose base we are looking into. |
| 3823 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3824 | if (!RT) |
| 3825 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3826 | RecordDecl *RD = RT->getDecl(); |
| 3827 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 3828 | |
| 3829 | // Find the base class itself. |
| 3830 | CurrentType = BaseSpec->getType(); |
| 3831 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 3832 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3833 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3834 | |
| 3835 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 3836 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 3837 | break; |
| 3838 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3839 | } |
| 3840 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3841 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3844 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3845 | switch (E->getOpcode()) { |
| 3846 | default: |
| 3847 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 3848 | // See C99 6.6p3. |
| 3849 | return Error(E); |
| 3850 | case UO_Extension: |
| 3851 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 3852 | // If so, we could clear the diagnostic ID. |
| 3853 | return Visit(E->getSubExpr()); |
| 3854 | case UO_Plus: |
| 3855 | // The result is just the value. |
| 3856 | return Visit(E->getSubExpr()); |
| 3857 | case UO_Minus: { |
| 3858 | if (!Visit(E->getSubExpr())) |
| 3859 | return false; |
| 3860 | if (!Result.isInt()) return Error(E); |
| 3861 | return Success(-Result.getInt(), E); |
| 3862 | } |
| 3863 | case UO_Not: { |
| 3864 | if (!Visit(E->getSubExpr())) |
| 3865 | return false; |
| 3866 | if (!Result.isInt()) return Error(E); |
| 3867 | return Success(~Result.getInt(), E); |
| 3868 | } |
| 3869 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3870 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3871 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3872 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3873 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 3874 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3875 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3878 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 3879 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3880 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3881 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3882 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 3883 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 3884 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3885 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3886 | case CK_BaseToDerived: |
| 3887 | case CK_DerivedToBase: |
| 3888 | case CK_UncheckedDerivedToBase: |
| 3889 | case CK_Dynamic: |
| 3890 | case CK_ToUnion: |
| 3891 | case CK_ArrayToPointerDecay: |
| 3892 | case CK_FunctionToPointerDecay: |
| 3893 | case CK_NullToPointer: |
| 3894 | case CK_NullToMemberPointer: |
| 3895 | case CK_BaseToDerivedMemberPointer: |
| 3896 | case CK_DerivedToBaseMemberPointer: |
| 3897 | case CK_ConstructorConversion: |
| 3898 | case CK_IntegralToPointer: |
| 3899 | case CK_ToVoid: |
| 3900 | case CK_VectorSplat: |
| 3901 | case CK_IntegralToFloating: |
| 3902 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3903 | case CK_CPointerToObjCPointerCast: |
| 3904 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3905 | case CK_AnyPointerToBlockPointerCast: |
| 3906 | case CK_ObjCObjectLValueCast: |
| 3907 | case CK_FloatingRealToComplex: |
| 3908 | case CK_FloatingComplexToReal: |
| 3909 | case CK_FloatingComplexCast: |
| 3910 | case CK_FloatingComplexToIntegralComplex: |
| 3911 | case CK_IntegralRealToComplex: |
| 3912 | case CK_IntegralComplexCast: |
| 3913 | case CK_IntegralComplexToFloatingComplex: |
| 3914 | llvm_unreachable("invalid cast kind for integral value"); |
| 3915 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 3916 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3917 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3918 | case CK_LValueBitCast: |
| 3919 | case CK_UserDefinedConversion: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3920 | case CK_ARCProduceObject: |
| 3921 | case CK_ARCConsumeObject: |
| 3922 | case CK_ARCReclaimReturnedObject: |
| 3923 | case CK_ARCExtendBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3924 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3925 | |
| 3926 | case CK_LValueToRValue: |
| 3927 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3928 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3929 | |
| 3930 | case CK_MemberPointerToBoolean: |
| 3931 | case CK_PointerToBoolean: |
| 3932 | case CK_IntegralToBoolean: |
| 3933 | case CK_FloatingToBoolean: |
| 3934 | case CK_FloatingComplexToBoolean: |
| 3935 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3936 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3937 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3938 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3939 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3942 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3943 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3944 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 3945 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 3946 | if (!Result.isInt()) { |
| 3947 | // Only allow casts of lvalues if they are lossless. |
| 3948 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 3949 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3950 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3951 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3952 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3955 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3956 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3957 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3958 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 3959 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3960 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3961 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3962 | if (LV.getLValueBase()) { |
| 3963 | // Only allow based lvalue casts if they are lossless. |
| 3964 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3965 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3966 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 3967 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3968 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3969 | return true; |
| 3970 | } |
| 3971 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 3972 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 3973 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 3974 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3975 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3976 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3977 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3978 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3979 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 3980 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3981 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 3982 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3983 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3984 | case CK_FloatingToIntegral: { |
| 3985 | APFloat F(0.0); |
| 3986 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 3987 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 3988 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3989 | APSInt Value; |
| 3990 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 3991 | return false; |
| 3992 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3996 | llvm_unreachable("unknown cast resulting in integral value"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3997 | return Error(E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3998 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3999 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4000 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4001 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4002 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4003 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 4004 | return false; |
| 4005 | if (!LV.isComplexInt()) |
| 4006 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4007 | return Success(LV.getComplexIntReal(), E); |
| 4008 | } |
| 4009 | |
| 4010 | return Visit(E->getSubExpr()); |
| 4011 | } |
| 4012 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4013 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4014 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4015 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4016 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 4017 | return false; |
| 4018 | if (!LV.isComplexInt()) |
| 4019 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4020 | return Success(LV.getComplexIntImag(), E); |
| 4021 | } |
| 4022 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4023 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4024 | return Success(0, E); |
| 4025 | } |
| 4026 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4027 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 4028 | return Success(E->getPackLength(), E); |
| 4029 | } |
| 4030 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4031 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 4032 | return Success(E->getValue(), E); |
| 4033 | } |
| 4034 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4035 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4036 | // Float Evaluation |
| 4037 | //===----------------------------------------------------------------------===// |
| 4038 | |
| 4039 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4040 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4041 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4042 | APFloat &Result; |
| 4043 | public: |
| 4044 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4045 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4046 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4047 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4048 | Result = V.getFloat(); |
| 4049 | return true; |
| 4050 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4051 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4052 | bool ValueInitialization(const Expr *E) { |
| 4053 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 4054 | return true; |
| 4055 | } |
| 4056 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4057 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4058 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4059 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4060 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 4061 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4062 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 4063 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4064 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4065 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4066 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4067 | // FIXME: Missing: array subscript of vector, member of vector, |
| 4068 | // ImplicitValueInitExpr |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4069 | }; |
| 4070 | } // end anonymous namespace |
| 4071 | |
| 4072 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4073 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4074 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4077 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4078 | QualType ResultTy, |
| 4079 | const Expr *Arg, |
| 4080 | bool SNaN, |
| 4081 | llvm::APFloat &Result) { |
| 4082 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 4083 | if (!S) return false; |
| 4084 | |
| 4085 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 4086 | |
| 4087 | llvm::APInt fill; |
| 4088 | |
| 4089 | // Treat empty strings as if they were zero. |
| 4090 | if (S->getString().empty()) |
| 4091 | fill = llvm::APInt(32, 0); |
| 4092 | else if (S->getString().getAsInteger(0, fill)) |
| 4093 | return false; |
| 4094 | |
| 4095 | if (SNaN) |
| 4096 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 4097 | else |
| 4098 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 4099 | return true; |
| 4100 | } |
| 4101 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4102 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4103 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4104 | default: |
| 4105 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 4106 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4107 | case Builtin::BI__builtin_huge_val: |
| 4108 | case Builtin::BI__builtin_huge_valf: |
| 4109 | case Builtin::BI__builtin_huge_vall: |
| 4110 | case Builtin::BI__builtin_inf: |
| 4111 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 4112 | case Builtin::BI__builtin_infl: { |
| 4113 | const llvm::fltSemantics &Sem = |
| 4114 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 4115 | Result = llvm::APFloat::getInf(Sem); |
| 4116 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 4117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4119 | case Builtin::BI__builtin_nans: |
| 4120 | case Builtin::BI__builtin_nansf: |
| 4121 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4122 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 4123 | true, Result)) |
| 4124 | return Error(E); |
| 4125 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 4127 | case Builtin::BI__builtin_nan: |
| 4128 | case Builtin::BI__builtin_nanf: |
| 4129 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 4130 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 4131 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4132 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 4133 | false, Result)) |
| 4134 | return Error(E); |
| 4135 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4136 | |
| 4137 | case Builtin::BI__builtin_fabs: |
| 4138 | case Builtin::BI__builtin_fabsf: |
| 4139 | case Builtin::BI__builtin_fabsl: |
| 4140 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 4141 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4142 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4143 | if (Result.isNegative()) |
| 4144 | Result.changeSign(); |
| 4145 | return true; |
| 4146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4147 | case Builtin::BI__builtin_copysign: |
| 4148 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4149 | case Builtin::BI__builtin_copysignl: { |
| 4150 | APFloat RHS(0.); |
| 4151 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 4152 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 4153 | return false; |
| 4154 | Result.copySign(RHS); |
| 4155 | return true; |
| 4156 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4160 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4161 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 4162 | ComplexValue CV; |
| 4163 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 4164 | return false; |
| 4165 | Result = CV.FloatReal; |
| 4166 | return true; |
| 4167 | } |
| 4168 | |
| 4169 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4173 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 4174 | ComplexValue CV; |
| 4175 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 4176 | return false; |
| 4177 | Result = CV.FloatImag; |
| 4178 | return true; |
| 4179 | } |
| 4180 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4181 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 4182 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 4183 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 4184 | return true; |
| 4185 | } |
| 4186 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4187 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4188 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4189 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4190 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 4191 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4192 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 4193 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 4194 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4195 | Result.changeSign(); |
| 4196 | return true; |
| 4197 | } |
| 4198 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4199 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4200 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4201 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 4202 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 4203 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 4204 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4205 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 4206 | return false; |
| 4207 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 4208 | return false; |
| 4209 | |
| 4210 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4211 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4212 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4213 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 4214 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4215 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4216 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 4217 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4218 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4219 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 4220 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4221 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4222 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 4223 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 4228 | Result = E->getValue(); |
| 4229 | return true; |
| 4230 | } |
| 4231 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4232 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4233 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4235 | switch (E->getCastKind()) { |
| 4236 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4237 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4238 | |
| 4239 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4240 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4241 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 4242 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 4243 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4244 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4245 | |
| 4246 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4247 | if (!Visit(SubExpr)) |
| 4248 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4249 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 4250 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4251 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4252 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4253 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4254 | ComplexValue V; |
| 4255 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 4256 | return false; |
| 4257 | Result = V.getComplexFloatReal(); |
| 4258 | return true; |
| 4259 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 4260 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4261 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4262 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 4265 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4266 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4267 | //===----------------------------------------------------------------------===// |
| 4268 | |
| 4269 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4270 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4271 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4272 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4274 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4275 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4276 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 4277 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4278 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4279 | Result.setFrom(V); |
| 4280 | return true; |
| 4281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4283 | //===--------------------------------------------------------------------===// |
| 4284 | // Visitor Methods |
| 4285 | //===--------------------------------------------------------------------===// |
| 4286 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4287 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4289 | bool VisitCastExpr(const CastExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4291 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4292 | bool VisitUnaryOperator(const UnaryOperator *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4293 | // FIXME Missing: ImplicitValueInitExpr, InitListExpr |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4294 | }; |
| 4295 | } // end anonymous namespace |
| 4296 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4297 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 4298 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4299 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4300 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4303 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 4304 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4305 | |
| 4306 | if (SubExpr->getType()->isRealFloatingType()) { |
| 4307 | Result.makeComplexFloat(); |
| 4308 | APFloat &Imag = Result.FloatImag; |
| 4309 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 4310 | return false; |
| 4311 | |
| 4312 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 4313 | return true; |
| 4314 | } else { |
| 4315 | assert(SubExpr->getType()->isIntegerType() && |
| 4316 | "Unexpected imaginary literal."); |
| 4317 | |
| 4318 | Result.makeComplexInt(); |
| 4319 | APSInt &Imag = Result.IntImag; |
| 4320 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 4321 | return false; |
| 4322 | |
| 4323 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 4324 | return true; |
| 4325 | } |
| 4326 | } |
| 4327 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4328 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4329 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4330 | switch (E->getCastKind()) { |
| 4331 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4332 | case CK_BaseToDerived: |
| 4333 | case CK_DerivedToBase: |
| 4334 | case CK_UncheckedDerivedToBase: |
| 4335 | case CK_Dynamic: |
| 4336 | case CK_ToUnion: |
| 4337 | case CK_ArrayToPointerDecay: |
| 4338 | case CK_FunctionToPointerDecay: |
| 4339 | case CK_NullToPointer: |
| 4340 | case CK_NullToMemberPointer: |
| 4341 | case CK_BaseToDerivedMemberPointer: |
| 4342 | case CK_DerivedToBaseMemberPointer: |
| 4343 | case CK_MemberPointerToBoolean: |
| 4344 | case CK_ConstructorConversion: |
| 4345 | case CK_IntegralToPointer: |
| 4346 | case CK_PointerToIntegral: |
| 4347 | case CK_PointerToBoolean: |
| 4348 | case CK_ToVoid: |
| 4349 | case CK_VectorSplat: |
| 4350 | case CK_IntegralCast: |
| 4351 | case CK_IntegralToBoolean: |
| 4352 | case CK_IntegralToFloating: |
| 4353 | case CK_FloatingToIntegral: |
| 4354 | case CK_FloatingToBoolean: |
| 4355 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4356 | case CK_CPointerToObjCPointerCast: |
| 4357 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4358 | case CK_AnyPointerToBlockPointerCast: |
| 4359 | case CK_ObjCObjectLValueCast: |
| 4360 | case CK_FloatingComplexToReal: |
| 4361 | case CK_FloatingComplexToBoolean: |
| 4362 | case CK_IntegralComplexToReal: |
| 4363 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4364 | case CK_ARCProduceObject: |
| 4365 | case CK_ARCConsumeObject: |
| 4366 | case CK_ARCReclaimReturnedObject: |
| 4367 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4368 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 4369 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4370 | case CK_LValueToRValue: |
| 4371 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4372 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4373 | |
| 4374 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4375 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4376 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4377 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4378 | |
| 4379 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4380 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4381 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4382 | return false; |
| 4383 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4384 | Result.makeComplexFloat(); |
| 4385 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 4386 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4389 | case CK_FloatingComplexCast: { |
| 4390 | if (!Visit(E->getSubExpr())) |
| 4391 | return false; |
| 4392 | |
| 4393 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4394 | QualType From |
| 4395 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4396 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4397 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 4398 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | case CK_FloatingComplexToIntegralComplex: { |
| 4402 | if (!Visit(E->getSubExpr())) |
| 4403 | return false; |
| 4404 | |
| 4405 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4406 | QualType From |
| 4407 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4408 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4409 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 4410 | To, Result.IntReal) && |
| 4411 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 4412 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | case CK_IntegralRealToComplex: { |
| 4416 | APSInt &Real = Result.IntReal; |
| 4417 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 4418 | return false; |
| 4419 | |
| 4420 | Result.makeComplexInt(); |
| 4421 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 4422 | return true; |
| 4423 | } |
| 4424 | |
| 4425 | case CK_IntegralComplexCast: { |
| 4426 | if (!Visit(E->getSubExpr())) |
| 4427 | return false; |
| 4428 | |
| 4429 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4430 | QualType From |
| 4431 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4432 | |
| 4433 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 4434 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 4435 | return true; |
| 4436 | } |
| 4437 | |
| 4438 | case CK_IntegralComplexToFloatingComplex: { |
| 4439 | if (!Visit(E->getSubExpr())) |
| 4440 | return false; |
| 4441 | |
| 4442 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 4443 | QualType From |
| 4444 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 4445 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4446 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 4447 | To, Result.FloatReal) && |
| 4448 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 4449 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | llvm_unreachable("unknown cast resulting in complex value"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4454 | return Error(E); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4457 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4458 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 4459 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 4460 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4461 | if (!Visit(E->getLHS())) |
| 4462 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4464 | ComplexValue RHS; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4465 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4466 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4467 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4468 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 4469 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4470 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4471 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4472 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4473 | if (Result.isComplexFloat()) { |
| 4474 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 4475 | APFloat::rmNearestTiesToEven); |
| 4476 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 4477 | APFloat::rmNearestTiesToEven); |
| 4478 | } else { |
| 4479 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 4480 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 4481 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4482 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4483 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 4484 | if (Result.isComplexFloat()) { |
| 4485 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 4486 | APFloat::rmNearestTiesToEven); |
| 4487 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 4488 | APFloat::rmNearestTiesToEven); |
| 4489 | } else { |
| 4490 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 4491 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 4492 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4493 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4494 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4495 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4496 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4497 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 4498 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 4499 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 4500 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4502 | APFloat Tmp = LHS_r; |
| 4503 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4504 | Result.getComplexFloatReal() = Tmp; |
| 4505 | Tmp = LHS_i; |
| 4506 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4507 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 4508 | |
| 4509 | Tmp = LHS_r; |
| 4510 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4511 | Result.getComplexFloatImag() = Tmp; |
| 4512 | Tmp = LHS_i; |
| 4513 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4514 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 4515 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4516 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4517 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4518 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 4519 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 4521 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 4522 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 4523 | } |
| 4524 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4525 | case BO_Div: |
| 4526 | if (Result.isComplexFloat()) { |
| 4527 | ComplexValue LHS = Result; |
| 4528 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 4529 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 4530 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 4531 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 4532 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 4533 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 4534 | |
| 4535 | APFloat Den = RHS_r; |
| 4536 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4537 | APFloat Tmp = RHS_i; |
| 4538 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4539 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 4540 | |
| 4541 | Res_r = LHS_r; |
| 4542 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4543 | Tmp = LHS_i; |
| 4544 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4545 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 4546 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 4547 | |
| 4548 | Res_i = LHS_i; |
| 4549 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 4550 | Tmp = LHS_r; |
| 4551 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 4552 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 4553 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 4554 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4555 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 4556 | return Error(E, diag::note_expr_divide_by_zero); |
| 4557 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4558 | ComplexValue LHS = Result; |
| 4559 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 4560 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 4561 | Result.getComplexIntReal() = |
| 4562 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 4563 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 4564 | Result.getComplexIntImag() = |
| 4565 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 4566 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 4567 | } |
| 4568 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4571 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4574 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 4575 | // Get the operand value into 'Result'. |
| 4576 | if (!Visit(E->getSubExpr())) |
| 4577 | return false; |
| 4578 | |
| 4579 | switch (E->getOpcode()) { |
| 4580 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4581 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 4582 | case UO_Extension: |
| 4583 | return true; |
| 4584 | case UO_Plus: |
| 4585 | // The result is always just the subexpr. |
| 4586 | return true; |
| 4587 | case UO_Minus: |
| 4588 | if (Result.isComplexFloat()) { |
| 4589 | Result.getComplexFloatReal().changeSign(); |
| 4590 | Result.getComplexFloatImag().changeSign(); |
| 4591 | } |
| 4592 | else { |
| 4593 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 4594 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 4595 | } |
| 4596 | return true; |
| 4597 | case UO_Not: |
| 4598 | if (Result.isComplexFloat()) |
| 4599 | Result.getComplexFloatImag().changeSign(); |
| 4600 | else |
| 4601 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 4602 | return true; |
| 4603 | } |
| 4604 | } |
| 4605 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 4606 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4607 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 4608 | // comma operator |
| 4609 | //===----------------------------------------------------------------------===// |
| 4610 | |
| 4611 | namespace { |
| 4612 | class VoidExprEvaluator |
| 4613 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 4614 | public: |
| 4615 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 4616 | |
| 4617 | bool Success(const CCValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4618 | |
| 4619 | bool VisitCastExpr(const CastExpr *E) { |
| 4620 | switch (E->getCastKind()) { |
| 4621 | default: |
| 4622 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4623 | case CK_ToVoid: |
| 4624 | VisitIgnoredValue(E->getSubExpr()); |
| 4625 | return true; |
| 4626 | } |
| 4627 | } |
| 4628 | }; |
| 4629 | } // end anonymous namespace |
| 4630 | |
| 4631 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 4632 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 4633 | return VoidExprEvaluator(Info).Visit(E); |
| 4634 | } |
| 4635 | |
| 4636 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4637 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4638 | //===----------------------------------------------------------------------===// |
| 4639 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4640 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4641 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 4642 | // are. |
| 4643 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 4644 | LValue LV; |
| 4645 | if (!EvaluateLValue(E, LV, Info)) |
| 4646 | return false; |
| 4647 | LV.moveInto(Result); |
| 4648 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4649 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4650 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4651 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4652 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4653 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4654 | } else if (E->getType()->hasPointerRepresentation()) { |
| 4655 | LValue LV; |
| 4656 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4657 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4658 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4659 | } else if (E->getType()->isRealFloatingType()) { |
| 4660 | llvm::APFloat F(0.0); |
| 4661 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4662 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4663 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4664 | } else if (E->getType()->isAnyComplexType()) { |
| 4665 | ComplexValue C; |
| 4666 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4667 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4668 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4669 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4670 | MemberPtr P; |
| 4671 | if (!EvaluateMemberPointer(E, P, Info)) |
| 4672 | return false; |
| 4673 | P.moveInto(Result); |
| 4674 | return true; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4675 | } else if (E->getType()->isArrayType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4676 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4677 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4678 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4679 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4680 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4681 | } else if (E->getType()->isRecordType() && E->getType()->isLiteralType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4682 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4683 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4684 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 4685 | return false; |
| 4686 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4687 | } else if (E->getType()->isVoidType()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4688 | if (Info.getLangOpts().CPlusPlus0x) |
| 4689 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 4690 | << E->getType(); |
| 4691 | else |
| 4692 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 4693 | if (!EvaluateVoid(E, Info)) |
| 4694 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4695 | } else if (Info.getLangOpts().CPlusPlus0x) { |
| 4696 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); |
| 4697 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4698 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4699 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 4700 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4701 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 4702 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 4703 | return true; |
| 4704 | } |
| 4705 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4706 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 4707 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 4708 | /// since later initializers for an object can indirectly refer to subobjects |
| 4709 | /// which were initialized earlier. |
| 4710 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4711 | const LValue &This, const Expr *E, |
| 4712 | CheckConstantExpressionKind CCEK) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4713 | if (E->isRValue() && E->getType()->isLiteralType()) { |
| 4714 | // Evaluate arrays and record types in-place, so that later initializers can |
| 4715 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4716 | if (E->getType()->isArrayType()) |
| 4717 | return EvaluateArray(E, This, Result, Info); |
| 4718 | else if (E->getType()->isRecordType()) |
| 4719 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | // For any other type, in-place evaluation is unimportant. |
| 4723 | CCValue CoreConstResult; |
| 4724 | return Evaluate(CoreConstResult, Info, E) && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4725 | CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4728 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 4729 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 4730 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
| 4731 | CCValue Value; |
| 4732 | if (!::Evaluate(Value, Info, E)) |
| 4733 | return false; |
| 4734 | |
| 4735 | if (E->isGLValue()) { |
| 4736 | LValue LV; |
| 4737 | LV.setFrom(Value); |
| 4738 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) |
| 4739 | return false; |
| 4740 | } |
| 4741 | |
| 4742 | // Check this core constant expression is a constant expression, and if so, |
| 4743 | // convert it to one. |
| 4744 | return CheckConstantExpression(Info, E, Value, Result); |
| 4745 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4746 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4747 | /// 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] | 4748 | /// any crazy technique (that has nothing to do with language standards) that |
| 4749 | /// 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] | 4750 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 4751 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4752 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 4753 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 4754 | // containing vast quantities of these. |
| 4755 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 4756 | Result.Val = APValue(APSInt(L->getValue(), |
| 4757 | L->getType()->isUnsignedIntegerType())); |
| 4758 | return true; |
| 4759 | } |
| 4760 | |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 4761 | // FIXME: Evaluating initializers for large arrays can cause performance |
| 4762 | // problems, and we don't use such values yet. Once we have a more efficient |
| 4763 | // array representation, this should be reinstated, and used by CodeGen. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4764 | // The same problem affects large records. |
| 4765 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 4766 | !Ctx.getLangOptions().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 4767 | return false; |
| 4768 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4769 | // FIXME: If this is the initializer for an lvalue, pass that in. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4770 | EvalInfo Info(Ctx, Result); |
| 4771 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4774 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 4775 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4776 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4777 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4778 | HandleConversionToBool(CCValue(Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4779 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4782 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4783 | EvalResult ExprResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4784 | if (!EvaluateAsRValue(ExprResult, Ctx) || ExprResult.HasSideEffects || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4785 | !ExprResult.Val.isInt()) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4786 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4787 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4788 | Result = ExprResult.Val.getInt(); |
| 4789 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4792 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 4793 | EvalInfo Info(Ctx, Result); |
| 4794 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4795 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4796 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4797 | CheckLValueConstantExpression(Info, this, LV, Result.Val, |
| 4798 | CCEK_Constant); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4801 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 4802 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4803 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 4804 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4805 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 4806 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4807 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4808 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4809 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4812 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4813 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4814 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 4815 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4816 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4817 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4818 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 4819 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4820 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4821 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 4822 | bool Expr::EvalResult::isGlobalLValue() const { |
| 4823 | assert(Val.isLValue()); |
| 4824 | return IsGlobalLValue(Val.getLValueBase()); |
| 4825 | } |
| 4826 | |
| 4827 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4828 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 4829 | /// an integer constant expression. |
| 4830 | |
| 4831 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 4832 | /// comma, etc |
| 4833 | /// |
| 4834 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 4835 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 4836 | /// cast+dereference. |
| 4837 | |
| 4838 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 4839 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 4840 | // Note that to reduce code duplication, this helper does no evaluation |
| 4841 | // itself; the caller checks whether the expression is evaluatable, and |
| 4842 | // in the rare cases where CheckICE actually cares about the evaluated |
| 4843 | // value, it calls into Evalute. |
| 4844 | // |
| 4845 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4846 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4847 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 4848 | // a legal subexpression for an ICE. This return value is used to handle |
| 4849 | // the comma operator in C99 mode. |
| 4850 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 4851 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4852 | namespace { |
| 4853 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4854 | struct ICEDiag { |
| 4855 | unsigned Val; |
| 4856 | SourceLocation Loc; |
| 4857 | |
| 4858 | public: |
| 4859 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 4860 | ICEDiag() : Val(0) {} |
| 4861 | }; |
| 4862 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4866 | |
| 4867 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 4868 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 4869 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4870 | !EVResult.Val.isInt()) { |
| 4871 | return ICEDiag(2, E->getLocStart()); |
| 4872 | } |
| 4873 | return NoDiag(); |
| 4874 | } |
| 4875 | |
| 4876 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 4877 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4878 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4879 | return ICEDiag(2, E->getLocStart()); |
| 4880 | } |
| 4881 | |
| 4882 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 4883 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4884 | #define STMT(Node, Base) case Expr::Node##Class: |
| 4885 | #define EXPR(Node, Base) |
| 4886 | #include "clang/AST/StmtNodes.inc" |
| 4887 | case Expr::PredefinedExprClass: |
| 4888 | case Expr::FloatingLiteralClass: |
| 4889 | case Expr::ImaginaryLiteralClass: |
| 4890 | case Expr::StringLiteralClass: |
| 4891 | case Expr::ArraySubscriptExprClass: |
| 4892 | case Expr::MemberExprClass: |
| 4893 | case Expr::CompoundAssignOperatorClass: |
| 4894 | case Expr::CompoundLiteralExprClass: |
| 4895 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4896 | case Expr::DesignatedInitExprClass: |
| 4897 | case Expr::ImplicitValueInitExprClass: |
| 4898 | case Expr::ParenListExprClass: |
| 4899 | case Expr::VAArgExprClass: |
| 4900 | case Expr::AddrLabelExprClass: |
| 4901 | case Expr::StmtExprClass: |
| 4902 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4903 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4904 | case Expr::CXXDynamicCastExprClass: |
| 4905 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 4906 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4907 | case Expr::CXXNullPtrLiteralExprClass: |
| 4908 | case Expr::CXXThisExprClass: |
| 4909 | case Expr::CXXThrowExprClass: |
| 4910 | case Expr::CXXNewExprClass: |
| 4911 | case Expr::CXXDeleteExprClass: |
| 4912 | case Expr::CXXPseudoDestructorExprClass: |
| 4913 | case Expr::UnresolvedLookupExprClass: |
| 4914 | case Expr::DependentScopeDeclRefExprClass: |
| 4915 | case Expr::CXXConstructExprClass: |
| 4916 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4917 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4918 | case Expr::CXXTemporaryObjectExprClass: |
| 4919 | case Expr::CXXUnresolvedConstructExprClass: |
| 4920 | case Expr::CXXDependentScopeMemberExprClass: |
| 4921 | case Expr::UnresolvedMemberExprClass: |
| 4922 | case Expr::ObjCStringLiteralClass: |
| 4923 | case Expr::ObjCEncodeExprClass: |
| 4924 | case Expr::ObjCMessageExprClass: |
| 4925 | case Expr::ObjCSelectorExprClass: |
| 4926 | case Expr::ObjCProtocolExprClass: |
| 4927 | case Expr::ObjCIvarRefExprClass: |
| 4928 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4929 | case Expr::ObjCIsaExprClass: |
| 4930 | case Expr::ShuffleVectorExprClass: |
| 4931 | case Expr::BlockExprClass: |
| 4932 | case Expr::BlockDeclRefExprClass: |
| 4933 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 4934 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4935 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 4936 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 4937 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4938 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4939 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 4940 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 4941 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4942 | case Expr::InitListExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4943 | return ICEDiag(2, E->getLocStart()); |
| 4944 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4945 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4946 | case Expr::GNUNullExprClass: |
| 4947 | // GCC considers the GNU __null value to be an integral constant expression. |
| 4948 | return NoDiag(); |
| 4949 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4950 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 4951 | return |
| 4952 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 4953 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4954 | case Expr::ParenExprClass: |
| 4955 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 4956 | case Expr::GenericSelectionExprClass: |
| 4957 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4958 | case Expr::IntegerLiteralClass: |
| 4959 | case Expr::CharacterLiteralClass: |
| 4960 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 4961 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4962 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4963 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4964 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4965 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4966 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4967 | return NoDiag(); |
| 4968 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 4969 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 4970 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 4971 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 4972 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4973 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4974 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4975 | return CheckEvalInICE(E, Ctx); |
| 4976 | return ICEDiag(2, E->getLocStart()); |
| 4977 | } |
| 4978 | case Expr::DeclRefExprClass: |
| 4979 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 4980 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 4981 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4982 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 4983 | |
| 4984 | // Parameter variables are never constants. Without this check, |
| 4985 | // getAnyInitializer() can find a default argument, which leads |
| 4986 | // to chaos. |
| 4987 | if (isa<ParmVarDecl>(D)) |
| 4988 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4989 | |
| 4990 | // C++ 7.1.5.1p2 |
| 4991 | // A variable of non-volatile const-qualified integral or enumeration |
| 4992 | // type initialized by an ICE can be used in ICEs. |
| 4993 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 4994 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 4995 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 4996 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 4997 | // Look for a declaration of this variable that has an initializer. |
| 4998 | const VarDecl *ID = 0; |
| 4999 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 5000 | if (Init) { |
| 5001 | if (ID->isInitKnownICE()) { |
| 5002 | // We have already checked whether this subexpression is an |
| 5003 | // integral constant expression. |
| 5004 | if (ID->isInitICE()) |
| 5005 | return NoDiag(); |
| 5006 | else |
| 5007 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 5008 | } |
| 5009 | |
| 5010 | // It's an ICE whether or not the definition we found is |
| 5011 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 5012 | // 6206 for details. |
| 5013 | |
| 5014 | if (Dcl->isCheckingICE()) { |
| 5015 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 5016 | } |
| 5017 | |
| 5018 | Dcl->setCheckingICE(); |
| 5019 | ICEDiag Result = CheckICE(Init, Ctx); |
| 5020 | // Cache the result of the ICE test. |
| 5021 | Dcl->setInitKnownICE(Result.Val == 0); |
| 5022 | return Result; |
| 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | return ICEDiag(2, E->getLocStart()); |
| 5027 | case Expr::UnaryOperatorClass: { |
| 5028 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 5029 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5030 | case UO_PostInc: |
| 5031 | case UO_PostDec: |
| 5032 | case UO_PreInc: |
| 5033 | case UO_PreDec: |
| 5034 | case UO_AddrOf: |
| 5035 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5036 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 5037 | // subexpressions of constant expressions, but they can never be ICEs |
| 5038 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5039 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5040 | case UO_Extension: |
| 5041 | case UO_LNot: |
| 5042 | case UO_Plus: |
| 5043 | case UO_Minus: |
| 5044 | case UO_Not: |
| 5045 | case UO_Real: |
| 5046 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5047 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | // OffsetOf falls through here. |
| 5051 | } |
| 5052 | case Expr::OffsetOfExprClass: { |
| 5053 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5054 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5055 | // "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] | 5056 | // compliance: we should warn earlier for offsetof expressions with |
| 5057 | // array subscripts that aren't ICEs, and if the array subscripts |
| 5058 | // are ICEs, the value of the offsetof must be an integer constant. |
| 5059 | return CheckEvalInICE(E, Ctx); |
| 5060 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5061 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 5062 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 5063 | if ((Exp->getKind() == UETT_SizeOf) && |
| 5064 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5065 | return ICEDiag(2, E->getLocStart()); |
| 5066 | return NoDiag(); |
| 5067 | } |
| 5068 | case Expr::BinaryOperatorClass: { |
| 5069 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 5070 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5071 | case BO_PtrMemD: |
| 5072 | case BO_PtrMemI: |
| 5073 | case BO_Assign: |
| 5074 | case BO_MulAssign: |
| 5075 | case BO_DivAssign: |
| 5076 | case BO_RemAssign: |
| 5077 | case BO_AddAssign: |
| 5078 | case BO_SubAssign: |
| 5079 | case BO_ShlAssign: |
| 5080 | case BO_ShrAssign: |
| 5081 | case BO_AndAssign: |
| 5082 | case BO_XorAssign: |
| 5083 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 5084 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 5085 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 5086 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5087 | return ICEDiag(2, E->getLocStart()); |
| 5088 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5089 | case BO_Mul: |
| 5090 | case BO_Div: |
| 5091 | case BO_Rem: |
| 5092 | case BO_Add: |
| 5093 | case BO_Sub: |
| 5094 | case BO_Shl: |
| 5095 | case BO_Shr: |
| 5096 | case BO_LT: |
| 5097 | case BO_GT: |
| 5098 | case BO_LE: |
| 5099 | case BO_GE: |
| 5100 | case BO_EQ: |
| 5101 | case BO_NE: |
| 5102 | case BO_And: |
| 5103 | case BO_Xor: |
| 5104 | case BO_Or: |
| 5105 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5106 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 5107 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5108 | if (Exp->getOpcode() == BO_Div || |
| 5109 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5110 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5111 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 5112 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5113 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5114 | if (REval == 0) |
| 5115 | return ICEDiag(1, E->getLocStart()); |
| 5116 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5117 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5118 | if (LEval.isMinSignedValue()) |
| 5119 | return ICEDiag(1, E->getLocStart()); |
| 5120 | } |
| 5121 | } |
| 5122 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5123 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5124 | if (Ctx.getLangOptions().C99) { |
| 5125 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 5126 | // if it isn't evaluated. |
| 5127 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 5128 | return ICEDiag(1, E->getLocStart()); |
| 5129 | } else { |
| 5130 | // In both C89 and C++, commas in ICEs are illegal. |
| 5131 | return ICEDiag(2, E->getLocStart()); |
| 5132 | } |
| 5133 | } |
| 5134 | if (LHSResult.Val >= RHSResult.Val) |
| 5135 | return LHSResult; |
| 5136 | return RHSResult; |
| 5137 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5138 | case BO_LAnd: |
| 5139 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5140 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 5141 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 5142 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 5143 | // Rare case where the RHS has a comma "side-effect"; we need |
| 5144 | // to actually check the condition to see whether the side |
| 5145 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5146 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5147 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5148 | return RHSResult; |
| 5149 | return NoDiag(); |
| 5150 | } |
| 5151 | |
| 5152 | if (LHSResult.Val >= RHSResult.Val) |
| 5153 | return LHSResult; |
| 5154 | return RHSResult; |
| 5155 | } |
| 5156 | } |
| 5157 | } |
| 5158 | case Expr::ImplicitCastExprClass: |
| 5159 | case Expr::CStyleCastExprClass: |
| 5160 | case Expr::CXXFunctionalCastExprClass: |
| 5161 | case Expr::CXXStaticCastExprClass: |
| 5162 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 5163 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5164 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5165 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 98326ed | 2011-10-25 00:21:54 +0000 | [diff] [blame] | 5166 | if (isa<ExplicitCastExpr>(E) && |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 5167 | isa<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) |
| 5168 | return NoDiag(); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5169 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 5170 | case CK_LValueToRValue: |
| 5171 | case CK_NoOp: |
| 5172 | case CK_IntegralToBoolean: |
| 5173 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5174 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5175 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 5176 | return ICEDiag(2, E->getLocStart()); |
| 5177 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5178 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5179 | case Expr::BinaryConditionalOperatorClass: { |
| 5180 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 5181 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 5182 | if (CommonResult.Val == 2) return CommonResult; |
| 5183 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 5184 | if (FalseResult.Val == 2) return FalseResult; |
| 5185 | if (CommonResult.Val == 1) return CommonResult; |
| 5186 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5187 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5188 | return FalseResult; |
| 5189 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5190 | case Expr::ConditionalOperatorClass: { |
| 5191 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 5192 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 5193 | // then only the true side is actually considered in an integer constant |
| 5194 | // expression, and it is fully evaluated. This is an important GNU |
| 5195 | // extension. See GCC PR38377 for discussion. |
| 5196 | if (const CallExpr *CallCE |
| 5197 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5198 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5199 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5200 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5201 | !EVResult.Val.isInt()) { |
| 5202 | return ICEDiag(2, E->getLocStart()); |
| 5203 | } |
| 5204 | return NoDiag(); |
| 5205 | } |
| 5206 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5207 | if (CondResult.Val == 2) |
| 5208 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 5209 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5210 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 5211 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 5212 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5213 | if (TrueResult.Val == 2) |
| 5214 | return TrueResult; |
| 5215 | if (FalseResult.Val == 2) |
| 5216 | return FalseResult; |
| 5217 | if (CondResult.Val == 1) |
| 5218 | return CondResult; |
| 5219 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 5220 | return NoDiag(); |
| 5221 | // Rare case where the diagnostics depend on which side is evaluated |
| 5222 | // Note that if we get here, CondResult is 0, and at least one of |
| 5223 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5224 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5225 | return FalseResult; |
| 5226 | } |
| 5227 | return TrueResult; |
| 5228 | } |
| 5229 | case Expr::CXXDefaultArgExprClass: |
| 5230 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 5231 | case Expr::ChooseExprClass: { |
| 5232 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | // Silence a GCC warning |
| 5237 | return ICEDiag(2, E->getLocStart()); |
| 5238 | } |
| 5239 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5240 | /// Evaluate an expression as a C++11 integral constant expression. |
| 5241 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 5242 | const Expr *E, |
| 5243 | llvm::APSInt *Value, |
| 5244 | SourceLocation *Loc) { |
| 5245 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 5246 | if (Loc) *Loc = E->getExprLoc(); |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
| 5250 | Expr::EvalResult Result; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5251 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 5252 | Result.Diag = &Diags; |
| 5253 | EvalInfo Info(Ctx, Result); |
| 5254 | |
| 5255 | bool IsICE = EvaluateAsRValue(Info, E, Result.Val); |
| 5256 | if (!Diags.empty()) { |
| 5257 | IsICE = false; |
| 5258 | if (Loc) *Loc = Diags[0].first; |
| 5259 | } else if (!IsICE && Loc) { |
| 5260 | *Loc = E->getExprLoc(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5261 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5262 | |
| 5263 | if (!IsICE) |
| 5264 | return false; |
| 5265 | |
| 5266 | assert(Result.Val.isInt() && "pointer cast to int is not an ICE"); |
| 5267 | if (Value) *Value = Result.Val.getInt(); |
| 5268 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5271 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5272 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 5273 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 5274 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5275 | ICEDiag d = CheckICE(this, Ctx); |
| 5276 | if (d.Val != 0) { |
| 5277 | if (Loc) *Loc = d.Loc; |
| 5278 | return false; |
| 5279 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5280 | return true; |
| 5281 | } |
| 5282 | |
| 5283 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 5284 | SourceLocation *Loc, bool isEvaluated) const { |
| 5285 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 5286 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 5287 | |
| 5288 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 5289 | return false; |
| 5290 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5291 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5292 | return true; |
| 5293 | } |