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 | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 46 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 47 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 48 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 49 | /// Determine whether the described subobject is an array element. |
| 50 | static bool SubobjectIsArrayElement(QualType Base, |
| 51 | ArrayRef<APValue::LValuePathEntry> Path) { |
| 52 | bool IsArrayElement = false; |
| 53 | const Type *T = Base.getTypePtr(); |
| 54 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 55 | IsArrayElement = T && T->isArrayType(); |
| 56 | if (IsArrayElement) |
| 57 | T = T->getBaseElementTypeUnsafe(); |
| 58 | else if (const FieldDecl *FD = dyn_cast<FieldDecl>(Path[I].BaseOrMember)) |
| 59 | T = FD->getType().getTypePtr(); |
| 60 | else |
| 61 | // Path[I] describes a base class. |
| 62 | T = 0; |
| 63 | } |
| 64 | return IsArrayElement; |
| 65 | } |
| 66 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 67 | /// A path from a glvalue to a subobject of that glvalue. |
| 68 | struct SubobjectDesignator { |
| 69 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 70 | /// lvalues can still be folded, but they are not core constant expressions |
| 71 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 72 | bool Invalid : 1; |
| 73 | |
| 74 | /// Whether this designates an array element. |
| 75 | bool ArrayElement : 1; |
| 76 | |
| 77 | /// Whether this designates 'one past the end' of the current subobject. |
| 78 | bool OnePastTheEnd : 1; |
| 79 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 80 | typedef APValue::LValuePathEntry PathEntry; |
| 81 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 82 | /// The entries on the path from the glvalue to the designated subobject. |
| 83 | SmallVector<PathEntry, 8> Entries; |
| 84 | |
| 85 | SubobjectDesignator() : |
| 86 | Invalid(false), ArrayElement(false), OnePastTheEnd(false) {} |
| 87 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 88 | SubobjectDesignator(const APValue &V) : |
| 89 | Invalid(!V.isLValue() || !V.hasLValuePath()), ArrayElement(false), |
| 90 | OnePastTheEnd(false) { |
| 91 | if (!Invalid) { |
| 92 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 93 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 94 | if (V.getLValueBase()) |
| 95 | ArrayElement = SubobjectIsArrayElement(V.getLValueBase()->getType(), |
| 96 | V.getLValuePath()); |
| 97 | else |
| 98 | assert(V.getLValuePath().empty() &&"Null pointer with nonempty path"); |
| 99 | } |
| 100 | } |
| 101 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 102 | void setInvalid() { |
| 103 | Invalid = true; |
| 104 | Entries.clear(); |
| 105 | } |
| 106 | /// Update this designator to refer to the given element within this array. |
| 107 | void addIndex(uint64_t N) { |
| 108 | if (Invalid) return; |
| 109 | if (OnePastTheEnd) { |
| 110 | setInvalid(); |
| 111 | return; |
| 112 | } |
| 113 | PathEntry Entry; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 114 | Entry.ArrayIndex = N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 115 | Entries.push_back(Entry); |
| 116 | ArrayElement = true; |
| 117 | } |
| 118 | /// Update this designator to refer to the given base or member of this |
| 119 | /// object. |
| 120 | void addDecl(const Decl *D) { |
| 121 | if (Invalid) return; |
| 122 | if (OnePastTheEnd) { |
| 123 | setInvalid(); |
| 124 | return; |
| 125 | } |
| 126 | PathEntry Entry; |
| 127 | Entry.BaseOrMember = D; |
| 128 | Entries.push_back(Entry); |
| 129 | ArrayElement = false; |
| 130 | } |
| 131 | /// Add N to the address of this subobject. |
| 132 | void adjustIndex(uint64_t N) { |
| 133 | if (Invalid) return; |
| 134 | if (ArrayElement) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 135 | // 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] | 136 | Entries.back().ArrayIndex += N; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | if (OnePastTheEnd && N == (uint64_t)-1) |
| 140 | OnePastTheEnd = false; |
| 141 | else if (!OnePastTheEnd && N == 1) |
| 142 | OnePastTheEnd = true; |
| 143 | else if (N != 0) |
| 144 | setInvalid(); |
| 145 | } |
| 146 | }; |
| 147 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 148 | /// A core constant value. This can be the value of any constant expression, |
| 149 | /// or a pointer or reference to a non-static object or function parameter. |
| 150 | class CCValue : public APValue { |
| 151 | typedef llvm::APSInt APSInt; |
| 152 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 153 | /// If the value is a reference or pointer into a parameter or temporary, |
| 154 | /// this is the corresponding call stack frame. |
| 155 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 156 | /// If the value is a reference or pointer, this is a description of how the |
| 157 | /// subobject was specified. |
| 158 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 159 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 160 | struct GlobalValue {}; |
| 161 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 162 | CCValue() {} |
| 163 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 164 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 165 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 166 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 167 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 168 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 169 | CCValue(const Expr *B, const CharUnits &O, CallStackFrame *F, |
| 170 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 172 | CCValue(const APValue &V, GlobalValue) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 173 | APValue(V), CallFrame(0), Designator(V) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 174 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 175 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 176 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 177 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 178 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 179 | SubobjectDesignator &getLValueDesignator() { |
| 180 | assert(getKind() == LValue); |
| 181 | return Designator; |
| 182 | } |
| 183 | const SubobjectDesignator &getLValueDesignator() const { |
| 184 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 185 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 188 | /// A stack frame in the constexpr call stack. |
| 189 | struct CallStackFrame { |
| 190 | EvalInfo &Info; |
| 191 | |
| 192 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 193 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 194 | |
| 195 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 196 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 197 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 198 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 199 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 200 | typedef MapTy::const_iterator temp_iterator; |
| 201 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 202 | MapTy Temporaries; |
| 203 | |
| 204 | CallStackFrame(EvalInfo &Info, const CCValue *Arguments); |
| 205 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 208 | struct EvalInfo { |
| 209 | const ASTContext &Ctx; |
| 210 | |
| 211 | /// EvalStatus - Contains information about the evaluation. |
| 212 | Expr::EvalStatus &EvalStatus; |
| 213 | |
| 214 | /// CurrentCall - The top of the constexpr call stack. |
| 215 | CallStackFrame *CurrentCall; |
| 216 | |
| 217 | /// NumCalls - The number of calls we've evaluated so far. |
| 218 | unsigned NumCalls; |
| 219 | |
| 220 | /// CallStackDepth - The number of calls in the call stack right now. |
| 221 | unsigned CallStackDepth; |
| 222 | |
| 223 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 224 | /// OpaqueValues - Values used as the common expression in a |
| 225 | /// BinaryConditionalOperator. |
| 226 | MapTy OpaqueValues; |
| 227 | |
| 228 | /// BottomFrame - The frame in which evaluation started. This must be |
| 229 | /// initialized last. |
| 230 | CallStackFrame BottomFrame; |
| 231 | |
| 232 | |
| 233 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
| 234 | : Ctx(C), EvalStatus(S), CurrentCall(0), NumCalls(0), CallStackDepth(0), |
| 235 | BottomFrame(*this, 0) {} |
| 236 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 237 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 238 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 239 | if (i == OpaqueValues.end()) return 0; |
| 240 | return &i->second; |
| 241 | } |
| 242 | |
| 243 | const LangOptions &getLangOpts() { return Ctx.getLangOptions(); } |
| 244 | }; |
| 245 | |
| 246 | CallStackFrame::CallStackFrame(EvalInfo &Info, const CCValue *Arguments) |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 247 | : Info(Info), Caller(Info.CurrentCall), Arguments(Arguments) { |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 248 | Info.CurrentCall = this; |
| 249 | ++Info.CallStackDepth; |
| 250 | } |
| 251 | |
| 252 | CallStackFrame::~CallStackFrame() { |
| 253 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 254 | --Info.CallStackDepth; |
| 255 | Info.CurrentCall = Caller; |
| 256 | } |
| 257 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 258 | struct ComplexValue { |
| 259 | private: |
| 260 | bool IsInt; |
| 261 | |
| 262 | public: |
| 263 | APSInt IntReal, IntImag; |
| 264 | APFloat FloatReal, FloatImag; |
| 265 | |
| 266 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 267 | |
| 268 | void makeComplexFloat() { IsInt = false; } |
| 269 | bool isComplexFloat() const { return !IsInt; } |
| 270 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 271 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 272 | |
| 273 | void makeComplexInt() { IsInt = true; } |
| 274 | bool isComplexInt() const { return IsInt; } |
| 275 | APSInt &getComplexIntReal() { return IntReal; } |
| 276 | APSInt &getComplexIntImag() { return IntImag; } |
| 277 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 278 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 279 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 280 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 281 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 282 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 283 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 284 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 285 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 286 | if (v.isComplexFloat()) { |
| 287 | makeComplexFloat(); |
| 288 | FloatReal = v.getComplexFloatReal(); |
| 289 | FloatImag = v.getComplexFloatImag(); |
| 290 | } else { |
| 291 | makeComplexInt(); |
| 292 | IntReal = v.getComplexIntReal(); |
| 293 | IntImag = v.getComplexIntImag(); |
| 294 | } |
| 295 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 296 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 297 | |
| 298 | struct LValue { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 299 | const Expr *Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 300 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 301 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 302 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 303 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 304 | const Expr *getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 305 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 306 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 307 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 308 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 309 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 310 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 311 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 312 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 313 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 314 | void setFrom(const CCValue &V) { |
| 315 | assert(V.isLValue()); |
| 316 | Base = V.getLValueBase(); |
| 317 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 318 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 319 | Designator = V.getLValueDesignator(); |
| 320 | } |
| 321 | |
| 322 | void setExpr(const Expr *E, CallStackFrame *F = 0) { |
| 323 | Base = E; |
| 324 | Offset = CharUnits::Zero(); |
| 325 | Frame = F; |
| 326 | Designator = SubobjectDesignator(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 327 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 328 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 330 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 331 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 332 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
| 333 | const Expr *E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 334 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 335 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 336 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 337 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 338 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 339 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 340 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 341 | |
| 342 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 343 | // Misc utilities |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 346 | static bool IsGlobalLValue(const Expr* E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 347 | if (!E) return true; |
| 348 | |
| 349 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 350 | if (isa<FunctionDecl>(DRE->getDecl())) |
| 351 | return true; |
| 352 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 353 | return VD->hasGlobalStorage(); |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E)) |
| 358 | return CLE->isFileScope(); |
| 359 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 360 | if (isa<MemberExpr>(E) || isa<MaterializeTemporaryExpr>(E)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 361 | return false; |
| 362 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 366 | /// Check that this reference or pointer core constant expression is a valid |
| 367 | /// value for a constant expression. Type T should be either LValue or CCValue. |
| 368 | template<typename T> |
| 369 | static bool CheckLValueConstantExpression(const T &LVal, APValue &Value) { |
| 370 | if (!IsGlobalLValue(LVal.getLValueBase())) |
| 371 | return false; |
| 372 | |
| 373 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 374 | // A constant expression must refer to an object or be a null pointer. |
| 375 | if (Designator.Invalid || Designator.OnePastTheEnd || |
| 376 | (!LVal.getLValueBase() && !Designator.Entries.empty())) { |
| 377 | // FIXME: Check for out-of-bounds array indices. |
| 378 | // FIXME: This is not a constant expression. |
| 379 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 380 | APValue::NoLValuePath()); |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 385 | Designator.Entries); |
| 386 | return true; |
| 387 | } |
| 388 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 389 | /// 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] | 390 | /// constant expression, and if it is, produce the corresponding constant value. |
| 391 | static bool CheckConstantExpression(const CCValue &CCValue, APValue &Value) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 392 | if (!CCValue.isLValue()) { |
| 393 | Value = CCValue; |
| 394 | return true; |
| 395 | } |
| 396 | return CheckLValueConstantExpression(CCValue, Value); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 399 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
| 400 | if (!LVal.Base) |
| 401 | return 0; |
| 402 | |
| 403 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVal.Base)) |
| 404 | return DRE->getDecl(); |
| 405 | |
| 406 | // FIXME: Static data members accessed via a MemberExpr are represented as |
| 407 | // that MemberExpr. We should use the Decl directly instead. |
| 408 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(LVal.Base)) { |
| 409 | assert(!isa<FieldDecl>(ME->getMemberDecl()) && "shouldn't see fields here"); |
| 410 | return ME->getMemberDecl(); |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static bool IsLiteralLValue(const LValue &Value) { |
| 417 | return Value.Base && |
| 418 | !isa<DeclRefExpr>(Value.Base) && |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 419 | !isa<MemberExpr>(Value.Base) && |
| 420 | !isa<MaterializeTemporaryExpr>(Value.Base); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 423 | static bool IsWeakDecl(const ValueDecl *Decl) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 424 | return Decl->hasAttr<WeakAttr>() || |
| 425 | Decl->hasAttr<WeakRefAttr>() || |
| 426 | Decl->isWeakImported(); |
| 427 | } |
| 428 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 429 | static bool IsWeakLValue(const LValue &Value) { |
| 430 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 431 | return Decl && IsWeakDecl(Decl); |
| 432 | } |
| 433 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 434 | static bool EvalPointerValueAsBool(const LValue &Value, bool &Result) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 435 | const Expr* Base = Value.Base; |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 436 | |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 437 | // A null base expression indicates a null pointer. These are always |
| 438 | // evaluatable, and they are false unless the offset is zero. |
| 439 | if (!Base) { |
| 440 | Result = !Value.Offset.isZero(); |
| 441 | return true; |
| 442 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 443 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 444 | // Require the base expression to be a global l-value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 445 | // FIXME: C++11 requires such conversions. Remove this check. |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 446 | if (!IsGlobalLValue(Base)) return false; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 447 | |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 448 | // We have a non-null base expression. These are generally known to |
| 449 | // be true, but if it'a decl-ref to a weak symbol it can be null at |
| 450 | // runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 451 | Result = true; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 452 | return !IsWeakLValue(Value); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 455 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 456 | switch (Val.getKind()) { |
| 457 | case APValue::Uninitialized: |
| 458 | return false; |
| 459 | case APValue::Int: |
| 460 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 461 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 462 | case APValue::Float: |
| 463 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 464 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 465 | case APValue::ComplexInt: |
| 466 | Result = Val.getComplexIntReal().getBoolValue() || |
| 467 | Val.getComplexIntImag().getBoolValue(); |
| 468 | return true; |
| 469 | case APValue::ComplexFloat: |
| 470 | Result = !Val.getComplexFloatReal().isZero() || |
| 471 | !Val.getComplexFloatImag().isZero(); |
| 472 | return true; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 473 | case APValue::LValue: { |
| 474 | LValue PointerResult; |
| 475 | PointerResult.setFrom(Val); |
| 476 | return EvalPointerValueAsBool(PointerResult, Result); |
| 477 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 478 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 479 | case APValue::Array: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 480 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 483 | llvm_unreachable("unknown APValue kind"); |
| 484 | } |
| 485 | |
| 486 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 487 | EvalInfo &Info) { |
| 488 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 489 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 490 | if (!Evaluate(Val, Info, E)) |
| 491 | return false; |
| 492 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 496 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 497 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 498 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 499 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 501 | // FIXME: Warning for overflow. |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 502 | APSInt Result(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 503 | bool ignored; |
Jeffrey Yasskin | 3e1ef78 | 2011-07-15 17:03:07 +0000 | [diff] [blame] | 504 | (void)Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored); |
| 505 | return Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 509 | APFloat &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 510 | bool ignored; |
| 511 | APFloat Result = Value; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 513 | APFloat::rmNearestTiesToEven, &ignored); |
| 514 | return Result; |
| 515 | } |
| 516 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 518 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 519 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 520 | APSInt Result = Value; |
| 521 | // Figure out if this is a truncate, extend or noop cast. |
| 522 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 523 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 524 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 525 | return Result; |
| 526 | } |
| 527 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 529 | APSInt &Value, const ASTContext &Ctx) { |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 530 | |
| 531 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 532 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 533 | APFloat::rmNearestTiesToEven); |
| 534 | return Result; |
| 535 | } |
| 536 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 537 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 538 | static bool EvaluateVarDeclInit(EvalInfo &Info, const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 539 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 540 | // If this is a parameter to an active constexpr function call, perform |
| 541 | // argument substitution. |
| 542 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 543 | if (!Frame || !Frame->Arguments) |
| 544 | return false; |
| 545 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 546 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 547 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 548 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 549 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 550 | // this is the definition which will be used. |
| 551 | if (IsWeakDecl(VD)) |
| 552 | return false; |
| 553 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 554 | const Expr *Init = VD->getAnyInitializer(); |
| 555 | if (!Init) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 556 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 557 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 558 | if (APValue *V = VD->getEvaluatedValue()) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 559 | Result = CCValue(*V, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 560 | return !Result.isUninit(); |
| 561 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 562 | |
| 563 | if (VD->isEvaluatingValue()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 564 | return false; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 565 | |
| 566 | VD->setEvaluatingValue(); |
| 567 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 568 | Expr::EvalStatus EStatus; |
| 569 | EvalInfo InitInfo(Info.Ctx, EStatus); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 570 | // FIXME: The caller will need to know whether the value was a constant |
| 571 | // expression. If not, we should propagate up a diagnostic. |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 572 | APValue EvalResult; |
| 573 | if (!EvaluateConstantExpression(EvalResult, InitInfo, Init)) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 574 | // FIXME: If the evaluation failure was not permanent (for instance, if we |
| 575 | // hit a variable with no declaration yet, or a constexpr function with no |
| 576 | // definition yet), the standard is unclear as to how we should behave. |
| 577 | // |
| 578 | // Either the initializer should be evaluated when the variable is defined, |
| 579 | // or a failed evaluation of the initializer should be reattempted each time |
| 580 | // it is used. |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 581 | VD->setEvaluatedValue(APValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 582 | return false; |
| 583 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 584 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 585 | VD->setEvaluatedValue(EvalResult); |
| 586 | Result = CCValue(EvalResult, CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 587 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 590 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 591 | Qualifiers Quals = T.getQualifiers(); |
| 592 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 593 | } |
| 594 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 595 | /// Extract the designated sub-object of an rvalue. |
| 596 | static bool ExtractSubobject(EvalInfo &Info, CCValue &Obj, QualType ObjType, |
| 597 | const SubobjectDesignator &Sub, QualType SubType) { |
| 598 | if (Sub.Invalid || Sub.OnePastTheEnd) |
| 599 | return false; |
| 600 | if (Sub.Entries.empty()) { |
| 601 | assert(Info.Ctx.hasSameUnqualifiedType(ObjType, SubType) && |
| 602 | "Unexpected subobject type"); |
| 603 | return true; |
| 604 | } |
| 605 | |
| 606 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 607 | const APValue *O = &Obj; |
| 608 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
| 609 | if (O->isUninit()) |
| 610 | return false; |
| 611 | if (ObjType->isArrayType()) { |
| 612 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
| 613 | if (!CAT) |
| 614 | return false; |
| 615 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 616 | if (CAT->getSize().ule(Index)) |
| 617 | return false; |
| 618 | if (O->getArrayInitializedElts() > Index) |
| 619 | O = &O->getArrayInitializedElt(Index); |
| 620 | else |
| 621 | O = &O->getArrayFiller(); |
| 622 | ObjType = CAT->getElementType(); |
| 623 | } else { |
| 624 | // FIXME: Support handling of subobjects of structs and unions. Also |
| 625 | // for vector elements, if we want to support those? |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | assert(Info.Ctx.hasSameUnqualifiedType(ObjType, SubType) && |
| 630 | "Unexpected subobject type"); |
| 631 | Obj = CCValue(*O, CCValue::GlobalValue()); |
| 632 | return true; |
| 633 | } |
| 634 | |
| 635 | static bool HandleLValueToRValueConversion(EvalInfo &Info, QualType Type, |
| 636 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 637 | const Expr *Base = LVal.Base; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 638 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 639 | |
| 640 | // FIXME: Indirection through a null pointer deserves a diagnostic. |
| 641 | if (!Base) |
| 642 | return false; |
| 643 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 644 | if (const ValueDecl *D = GetLValueBaseDecl(LVal)) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 645 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 646 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 647 | // expressions are constant expressions too. Inside constexpr functions, |
| 648 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 649 | // In C, such things can also be folded, although they are not ICEs. |
| 650 | // |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 651 | // FIXME: volatile-qualified ParmVarDecls need special handling. A literal |
| 652 | // interpretation of C++11 suggests that volatile parameters are OK if |
| 653 | // they're never read (there's no prohibition against constructing volatile |
| 654 | // objects in constant expressions), but lvalue-to-rvalue conversions on |
| 655 | // them are not permitted. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 656 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 657 | QualType VT = VD->getType(); |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 658 | if (!VD || VD->isInvalidDecl()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 659 | return false; |
| 660 | if (!isa<ParmVarDecl>(VD)) { |
| 661 | if (!IsConstNonVolatile(VT)) |
| 662 | return false; |
Richard Smith | cd68992 | 2011-11-07 03:22:51 +0000 | [diff] [blame] | 663 | // FIXME: Allow folding of values of any literal type in all languages. |
| 664 | if (!VT->isIntegralOrEnumerationType() && !VT->isRealFloatingType() && |
| 665 | !VD->isConstexpr()) |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | if (!EvaluateVarDeclInit(Info, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 669 | return false; |
| 670 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 671 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 672 | return ExtractSubobject(Info, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 673 | |
| 674 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 675 | // conversion. This happens when the declaration and the lvalue should be |
| 676 | // considered synonymous, for instance when initializing an array of char |
| 677 | // from a string literal. Continue as if the initializer lvalue was the |
| 678 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 679 | assert(RVal.getLValueOffset().isZero() && |
| 680 | "offset for lvalue init of non-reference"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 681 | Base = RVal.getLValueBase(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 682 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 685 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 686 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 687 | const SubobjectDesignator &Designator = LVal.Designator; |
| 688 | if (Designator.Invalid || Designator.Entries.size() != 1) |
| 689 | return false; |
| 690 | |
| 691 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 692 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 693 | if (Index > S->getLength()) |
| 694 | return false; |
| 695 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 696 | Type->isUnsignedIntegerType()); |
| 697 | if (Index < S->getLength()) |
| 698 | Value = S->getCodeUnit(Index); |
| 699 | RVal = CCValue(Value); |
| 700 | return true; |
| 701 | } |
| 702 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 703 | if (Frame) { |
| 704 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 705 | // value from the relevant stack frame. |
| 706 | RVal = Frame->Temporaries[Base]; |
| 707 | } else if (const CompoundLiteralExpr *CLE |
| 708 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 709 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 710 | // initializer until now for such expressions. Such an expression can't be |
| 711 | // an ICE in C, so this only matters for fold. |
| 712 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 713 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 714 | return false; |
| 715 | } else |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 716 | return false; |
| 717 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 718 | return ExtractSubobject(Info, RVal, Base->getType(), LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 721 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 722 | enum EvalStmtResult { |
| 723 | /// Evaluation failed. |
| 724 | ESR_Failed, |
| 725 | /// Hit a 'return' statement. |
| 726 | ESR_Returned, |
| 727 | /// Evaluation succeeded. |
| 728 | ESR_Succeeded |
| 729 | }; |
| 730 | } |
| 731 | |
| 732 | // Evaluate a statement. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 733 | static EvalStmtResult EvaluateStmt(CCValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 734 | const Stmt *S) { |
| 735 | switch (S->getStmtClass()) { |
| 736 | default: |
| 737 | return ESR_Failed; |
| 738 | |
| 739 | case Stmt::NullStmtClass: |
| 740 | case Stmt::DeclStmtClass: |
| 741 | return ESR_Succeeded; |
| 742 | |
| 743 | case Stmt::ReturnStmtClass: |
| 744 | if (Evaluate(Result, Info, cast<ReturnStmt>(S)->getRetValue())) |
| 745 | return ESR_Returned; |
| 746 | return ESR_Failed; |
| 747 | |
| 748 | case Stmt::CompoundStmtClass: { |
| 749 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 750 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 751 | BE = CS->body_end(); BI != BE; ++BI) { |
| 752 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 753 | if (ESR != ESR_Succeeded) |
| 754 | return ESR; |
| 755 | } |
| 756 | return ESR_Succeeded; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /// Evaluate a function call. |
| 762 | static bool HandleFunctionCall(ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 763 | EvalInfo &Info, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 764 | // FIXME: Implement a proper call limit, along with a command-line flag. |
| 765 | if (Info.NumCalls >= 1000000 || Info.CallStackDepth >= 512) |
| 766 | return false; |
| 767 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 768 | SmallVector<CCValue, 16> ArgValues(Args.size()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 769 | // FIXME: Deal with default arguments and 'this'. |
| 770 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 771 | I != E; ++I) |
| 772 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| 773 | return false; |
| 774 | |
| 775 | CallStackFrame Frame(Info, ArgValues.data()); |
| 776 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 777 | } |
| 778 | |
| 779 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 780 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 781 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 782 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 783 | public: |
| 784 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 785 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 786 | |
| 787 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 788 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 792 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 793 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 794 | return Visit(E->getResultExpr()); |
| 795 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 796 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 797 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 798 | return true; |
| 799 | return false; |
| 800 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 801 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 802 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 803 | return true; |
| 804 | return false; |
| 805 | } |
| 806 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 807 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 808 | return true; |
| 809 | return false; |
| 810 | } |
| 811 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 812 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 813 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 814 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 815 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 816 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 817 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 818 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 819 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 820 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 821 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 822 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 823 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 824 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 825 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 826 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 827 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 828 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 829 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 830 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 831 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 832 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 833 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 834 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 835 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 836 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 837 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 838 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 839 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 840 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 841 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 842 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 843 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 844 | |
| 845 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 846 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 847 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 848 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 849 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 850 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 851 | return false; |
| 852 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 853 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 854 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 855 | }; |
| 856 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 857 | class OpaqueValueEvaluation { |
| 858 | EvalInfo &info; |
| 859 | OpaqueValueExpr *opaqueValue; |
| 860 | |
| 861 | public: |
| 862 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 863 | Expr *value) |
| 864 | : info(info), opaqueValue(opaqueValue) { |
| 865 | |
| 866 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 867 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 868 | this->opaqueValue = 0; |
| 869 | return; |
| 870 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | bool hasError() const { return opaqueValue == 0; } |
| 874 | |
| 875 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 876 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 877 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 878 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 879 | } |
| 880 | }; |
| 881 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 882 | } // end anonymous namespace |
| 883 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 884 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 885 | // Generic Evaluation |
| 886 | //===----------------------------------------------------------------------===// |
| 887 | namespace { |
| 888 | |
| 889 | template <class Derived, typename RetTy=void> |
| 890 | class ExprEvaluatorBase |
| 891 | : public ConstStmtVisitor<Derived, RetTy> { |
| 892 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 893 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 894 | return static_cast<Derived*>(this)->Success(V, E); |
| 895 | } |
| 896 | RetTy DerivedError(const Expr *E) { |
| 897 | return static_cast<Derived*>(this)->Error(E); |
| 898 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 899 | RetTy DerivedValueInitialization(const Expr *E) { |
| 900 | return static_cast<Derived*>(this)->ValueInitialization(E); |
| 901 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 902 | |
| 903 | protected: |
| 904 | EvalInfo &Info; |
| 905 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 906 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 907 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 908 | RetTy ValueInitialization(const Expr *E) { return DerivedError(E); } |
| 909 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 910 | bool MakeTemporary(const Expr *Key, const Expr *Value, LValue &Result) { |
| 911 | if (!Evaluate(Info.CurrentCall->Temporaries[Key], Info, Value)) |
| 912 | return false; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 913 | Result.setExpr(Key, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 914 | return true; |
| 915 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 916 | public: |
| 917 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 918 | |
| 919 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 920 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 921 | } |
| 922 | RetTy VisitExpr(const Expr *E) { |
| 923 | return DerivedError(E); |
| 924 | } |
| 925 | |
| 926 | RetTy VisitParenExpr(const ParenExpr *E) |
| 927 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 928 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 929 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 930 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 931 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 932 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 933 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 934 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 935 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 936 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 937 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 938 | |
| 939 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 940 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 941 | if (opaque.hasError()) |
| 942 | return DerivedError(E); |
| 943 | |
| 944 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 945 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 946 | return DerivedError(E); |
| 947 | |
| 948 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 949 | } |
| 950 | |
| 951 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
| 952 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 953 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 954 | return DerivedError(E); |
| 955 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 956 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 957 | return StmtVisitorTy::Visit(EvalExpr); |
| 958 | } |
| 959 | |
| 960 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 961 | const CCValue *Value = Info.getOpaqueValue(E); |
| 962 | if (!Value) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 963 | return (E->getSourceExpr() ? StmtVisitorTy::Visit(E->getSourceExpr()) |
| 964 | : DerivedError(E)); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 965 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 966 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 967 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 968 | RetTy VisitCallExpr(const CallExpr *E) { |
| 969 | const Expr *Callee = E->getCallee(); |
| 970 | QualType CalleeType = Callee->getType(); |
| 971 | |
| 972 | // FIXME: Handle the case where Callee is a (parenthesized) MemberExpr for a |
| 973 | // non-static member function. |
| 974 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) |
| 975 | return DerivedError(E); |
| 976 | |
| 977 | if (!CalleeType->isFunctionType() && !CalleeType->isFunctionPointerType()) |
| 978 | return DerivedError(E); |
| 979 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 980 | CCValue Call; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 981 | if (!Evaluate(Call, Info, Callee) || !Call.isLValue() || |
| 982 | !Call.getLValueBase() || !Call.getLValueOffset().isZero()) |
| 983 | return DerivedError(Callee); |
| 984 | |
| 985 | const FunctionDecl *FD = 0; |
| 986 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Call.getLValueBase())) |
| 987 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 988 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(Call.getLValueBase())) |
| 989 | FD = dyn_cast<FunctionDecl>(ME->getMemberDecl()); |
| 990 | if (!FD) |
| 991 | return DerivedError(Callee); |
| 992 | |
| 993 | // Don't call function pointers which have been cast to some other type. |
| 994 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
| 995 | return DerivedError(E); |
| 996 | |
| 997 | const FunctionDecl *Definition; |
| 998 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 999 | CCValue CCResult; |
| 1000 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1001 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
| 1002 | |
| 1003 | if (Body && Definition->isConstexpr() && !Definition->isInvalidDecl() && |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1004 | HandleFunctionCall(Args, Body, Info, CCResult) && |
| 1005 | CheckConstantExpression(CCResult, Result)) |
| 1006 | return DerivedSuccess(CCValue(Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1007 | |
| 1008 | return DerivedError(E); |
| 1009 | } |
| 1010 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1011 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 1012 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 1013 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1014 | RetTy VisitInitListExpr(const InitListExpr *E) { |
| 1015 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1016 | if (E->getNumInits() == 0) |
| 1017 | return DerivedValueInitialization(E); |
| 1018 | if (E->getNumInits() == 1) |
| 1019 | return StmtVisitorTy::Visit(E->getInit(0)); |
| 1020 | } |
| 1021 | return DerivedError(E); |
| 1022 | } |
| 1023 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 1024 | return DerivedValueInitialization(E); |
| 1025 | } |
| 1026 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
| 1027 | return DerivedValueInitialization(E); |
| 1028 | } |
| 1029 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1030 | RetTy VisitCastExpr(const CastExpr *E) { |
| 1031 | switch (E->getCastKind()) { |
| 1032 | default: |
| 1033 | break; |
| 1034 | |
| 1035 | case CK_NoOp: |
| 1036 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 1037 | |
| 1038 | case CK_LValueToRValue: { |
| 1039 | LValue LVal; |
| 1040 | if (EvaluateLValue(E->getSubExpr(), LVal, Info)) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1041 | CCValue RVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1042 | if (HandleLValueToRValueConversion(Info, E->getType(), LVal, RVal)) |
| 1043 | return DerivedSuccess(RVal, E); |
| 1044 | } |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return DerivedError(E); |
| 1050 | } |
| 1051 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1052 | /// Visit a value which is evaluated, but whose value is ignored. |
| 1053 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1054 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1055 | if (!Evaluate(Scratch, Info, E)) |
| 1056 | Info.EvalStatus.HasSideEffects = true; |
| 1057 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1058 | }; |
| 1059 | |
| 1060 | } |
| 1061 | |
| 1062 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1063 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1064 | // |
| 1065 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 1066 | // function designators (in C), decl references to void objects (in C), and |
| 1067 | // temporaries (if building with -Wno-address-of-temporary). |
| 1068 | // |
| 1069 | // LValue evaluation produces values comprising a base expression of one of the |
| 1070 | // following types: |
| 1071 | // * DeclRefExpr |
| 1072 | // * MemberExpr for a static member |
| 1073 | // * CompoundLiteralExpr in C |
| 1074 | // * StringLiteral |
| 1075 | // * PredefinedExpr |
| 1076 | // * ObjCEncodeExpr |
| 1077 | // * AddrLabelExpr |
| 1078 | // * BlockExpr |
| 1079 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1080 | // plus an offset in bytes. It can also produce lvalues referring to locals. In |
| 1081 | // that case, the Frame will point to a stack frame, and the Expr is used as a |
| 1082 | // key to find the relevant temporary's value. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1083 | //===----------------------------------------------------------------------===// |
| 1084 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1085 | class LValueExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1086 | : public ExprEvaluatorBase<LValueExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1087 | LValue &Result; |
Chandler Carruth | 0124839 | 2011-08-22 17:24:56 +0000 | [diff] [blame] | 1088 | const Decl *PrevDecl; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1089 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1090 | bool Success(const Expr *E) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1091 | Result.setExpr(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1094 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1096 | LValueExprEvaluator(EvalInfo &info, LValue &Result) : |
Chandler Carruth | 0124839 | 2011-08-22 17:24:56 +0000 | [diff] [blame] | 1097 | ExprEvaluatorBaseTy(info), Result(Result), PrevDecl(0) {} |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1098 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1099 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1100 | Result.setFrom(V); |
| 1101 | return true; |
| 1102 | } |
| 1103 | bool Error(const Expr *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1104 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1105 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1106 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1107 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 1108 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1109 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 1110 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1111 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1112 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 1113 | bool VisitMemberExpr(const MemberExpr *E); |
| 1114 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 1115 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
| 1116 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 1117 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1118 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1119 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1120 | switch (E->getCastKind()) { |
| 1121 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1122 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1123 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1124 | case CK_LValueBitCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1125 | if (!Visit(E->getSubExpr())) |
| 1126 | return false; |
| 1127 | Result.Designator.setInvalid(); |
| 1128 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 1129 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1130 | // FIXME: Support CK_DerivedToBase and CK_UncheckedDerivedToBase. |
| 1131 | // Reuse PointerExprEvaluator::VisitCastExpr for these. |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 1132 | } |
| 1133 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 1134 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1135 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1136 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1137 | }; |
| 1138 | } // end anonymous namespace |
| 1139 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1140 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 1141 | /// expressions which are not glvalues, in a few cases: |
| 1142 | /// * function designators in C, |
| 1143 | /// * "extern void" objects, |
| 1144 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1145 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1146 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 1147 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 1148 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1149 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1152 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1153 | if (isa<FunctionDecl>(E->getDecl())) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1154 | return Success(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1155 | if (const VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) |
| 1156 | return VisitVarDecl(E, VD); |
| 1157 | return Error(E); |
| 1158 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 1159 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1160 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1161 | if (!VD->getType()->isReferenceType()) { |
| 1162 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1163 | Result.setExpr(E, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1164 | return true; |
| 1165 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1166 | return Success(E); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1167 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 1168 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1169 | CCValue V; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1170 | if (EvaluateVarDeclInit(Info, VD, Info.CurrentCall, V)) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1171 | return Success(V, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1172 | |
| 1173 | return Error(E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1176 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 1177 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1178 | return MakeTemporary(E, E->GetTemporaryExpr(), Result); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1181 | bool |
| 1182 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1183 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1184 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 1185 | // 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] | 1186 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1189 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1190 | // Handle static data members. |
| 1191 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 1192 | VisitIgnoredValue(E->getBase()); |
| 1193 | return VisitVarDecl(E, VD); |
| 1194 | } |
| 1195 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1196 | // Handle static member functions. |
| 1197 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 1198 | if (MD->isStatic()) { |
| 1199 | VisitIgnoredValue(E->getBase()); |
| 1200 | return Success(E); |
| 1201 | } |
| 1202 | } |
| 1203 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1204 | QualType Ty; |
| 1205 | if (E->isArrow()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1206 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 1207 | return false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1208 | Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1209 | } else { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1210 | if (!Visit(E->getBase())) |
| 1211 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1212 | Ty = E->getBase()->getType(); |
| 1213 | } |
| 1214 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1215 | const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1216 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1217 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1218 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1219 | if (!FD) // FIXME: deal with other kinds of member expressions |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1220 | return false; |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (FD->getType()->isReferenceType()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1223 | return false; |
Eli Friedman | 2be5861 | 2009-05-30 21:09:44 +0000 | [diff] [blame] | 1224 | |
Eli Friedman | 8290574 | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 1225 | unsigned i = FD->getFieldIndex(); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 1226 | Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1227 | Result.Designator.addDecl(FD); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1228 | return true; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1231 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1232 | // FIXME: Deal with vectors as array subscript bases. |
| 1233 | if (E->getBase()->getType()->isVectorType()) |
| 1234 | return false; |
| 1235 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1236 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1237 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1239 | APSInt Index; |
| 1240 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1241 | return false; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1242 | uint64_t IndexValue |
| 1243 | = Index.isSigned() ? static_cast<uint64_t>(Index.getSExtValue()) |
| 1244 | : Index.getZExtValue(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1245 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1246 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1247 | Result.Offset += IndexValue * ElementSize; |
| 1248 | Result.Designator.adjustIndex(IndexValue); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1249 | return true; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1250 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1251 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1252 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1253 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1256 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1257 | // Pointer Evaluation |
| 1258 | //===----------------------------------------------------------------------===// |
| 1259 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 1260 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1261 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1262 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1263 | LValue &Result; |
| 1264 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1265 | bool Success(const Expr *E) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1266 | Result.setExpr(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1267 | return true; |
| 1268 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1269 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1271 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1272 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1273 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1274 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1275 | Result.setFrom(V); |
| 1276 | return true; |
| 1277 | } |
| 1278 | bool Error(const Stmt *S) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1279 | return false; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1280 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1281 | bool ValueInitialization(const Expr *E) { |
| 1282 | return Success((Expr*)0); |
| 1283 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1284 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1285 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1286 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1287 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1288 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1289 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1290 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1291 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1292 | bool VisitCallExpr(const CallExpr *E); |
| 1293 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1294 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1295 | return Success(E); |
| 1296 | return false; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 1297 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1298 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1299 | { return ValueInitialization(E); } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1300 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1301 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1302 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1303 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1304 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1305 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1306 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1307 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1310 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1311 | if (E->getOpcode() != BO_Add && |
| 1312 | E->getOpcode() != BO_Sub) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1313 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1315 | const Expr *PExp = E->getLHS(); |
| 1316 | const Expr *IExp = E->getRHS(); |
| 1317 | if (IExp->getType()->isPointerType()) |
| 1318 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1320 | if (!EvaluatePointer(PExp, Result, Info)) |
| 1321 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1323 | llvm::APSInt Offset; |
| 1324 | if (!EvaluateInteger(IExp, Offset, Info)) |
| 1325 | return false; |
| 1326 | int64_t AdditionalOffset |
| 1327 | = Offset.isSigned() ? Offset.getSExtValue() |
| 1328 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1329 | if (E->getOpcode() == BO_Sub) |
| 1330 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1331 | |
Daniel Dunbar | e0cdb4e | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 1332 | // Compute the new offset in the appropriate width. |
Daniel Dunbar | e0cdb4e | 2010-03-20 05:53:45 +0000 | [diff] [blame] | 1333 | QualType PointeeType = |
| 1334 | PExp->getType()->getAs<PointerType>()->getPointeeType(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1335 | CharUnits SizeOfPointee; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 1337 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 1338 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1339 | SizeOfPointee = CharUnits::One(); |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 1340 | else |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1341 | SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1342 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1343 | Result.Offset += AdditionalOffset * SizeOfPointee; |
| 1344 | Result.Designator.adjustIndex(AdditionalOffset); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1345 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1346 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1347 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1348 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 1349 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1350 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1352 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1353 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 1354 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1355 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1356 | switch (E->getCastKind()) { |
| 1357 | default: |
| 1358 | break; |
| 1359 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1360 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1361 | case CK_CPointerToObjCPointerCast: |
| 1362 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1363 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1364 | if (!Visit(SubExpr)) |
| 1365 | return false; |
| 1366 | Result.Designator.setInvalid(); |
| 1367 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1368 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1369 | case CK_DerivedToBase: |
| 1370 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1371 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1372 | return false; |
| 1373 | |
| 1374 | // Now figure out the necessary offset to add to the baseLV to get from |
| 1375 | // the derived class to the base class. |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1376 | QualType Ty = E->getSubExpr()->getType(); |
| 1377 | const CXXRecordDecl *DerivedDecl = |
| 1378 | Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl(); |
| 1379 | |
| 1380 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1381 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 1382 | const CXXBaseSpecifier *Base = *PathI; |
| 1383 | |
| 1384 | // FIXME: If the base is virtual, we'd need to determine the type of the |
| 1385 | // most derived class and we don't support that right now. |
| 1386 | if (Base->isVirtual()) |
| 1387 | return false; |
| 1388 | |
| 1389 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1390 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1391 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1392 | Result.getLValueOffset() += Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1393 | DerivedDecl = BaseDecl; |
| 1394 | } |
| 1395 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1396 | // FIXME |
| 1397 | Result.Designator.setInvalid(); |
| 1398 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 1399 | return true; |
| 1400 | } |
| 1401 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1402 | case CK_NullToPointer: |
| 1403 | return ValueInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1404 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1405 | case CK_IntegralToPointer: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1406 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1407 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 1408 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1409 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1410 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1411 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 1412 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1413 | Result.Base = 0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1414 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1415 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1416 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1417 | return true; |
| 1418 | } else { |
| 1419 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1420 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1421 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1424 | case CK_ArrayToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1425 | // FIXME: Support array-to-pointer decay on array rvalues. |
| 1426 | if (!SubExpr->isGLValue()) |
| 1427 | return Error(E); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1428 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 1429 | return false; |
| 1430 | // The result is a pointer to the first element of the array. |
| 1431 | Result.Designator.addIndex(0); |
| 1432 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1433 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1434 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 1435 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1438 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1440 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1441 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | if (E->isBuiltinCall(Info.Ctx) == |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1443 | Builtin::BI__builtin___CFStringMakeConstantString || |
| 1444 | E->isBuiltinCall(Info.Ctx) == |
| 1445 | Builtin::BI__builtin___NSStringMakeConstantString) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1446 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 1447 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1448 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1449 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1450 | |
| 1451 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1452 | // Vector Evaluation |
| 1453 | //===----------------------------------------------------------------------===// |
| 1454 | |
| 1455 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1456 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1457 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 1458 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1459 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1461 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 1462 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1464 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 1465 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 1466 | // FIXME: remove this APValue copy. |
| 1467 | Result = APValue(V.data(), V.size()); |
| 1468 | return true; |
| 1469 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 1470 | bool Success(const CCValue &V, const Expr *E) { |
| 1471 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1472 | Result = V; |
| 1473 | return true; |
| 1474 | } |
| 1475 | bool Error(const Expr *E) { return false; } |
| 1476 | bool ValueInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1478 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1479 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1480 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1481 | bool VisitInitListExpr(const InitListExpr *E); |
| 1482 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1483 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1484 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1485 | // shufflevector, ExtVectorElementExpr |
| 1486 | // (Note that these require implementing conversions |
| 1487 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1488 | }; |
| 1489 | } // end anonymous namespace |
| 1490 | |
| 1491 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1492 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1493 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1496 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 1497 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 1498 | QualType EltTy = VTy->getElementType(); |
| 1499 | unsigned NElts = VTy->getNumElements(); |
| 1500 | unsigned EltWidth = Info.Ctx.getTypeSize(EltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1502 | const Expr* SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 1503 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1504 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1505 | switch (E->getCastKind()) { |
| 1506 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1507 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1508 | if (SETy->isIntegerType()) { |
| 1509 | APSInt IntResult; |
| 1510 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1511 | return Error(E); |
| 1512 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1513 | } else if (SETy->isRealFloatingType()) { |
| 1514 | APFloat F(0.0); |
| 1515 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1516 | return Error(E); |
| 1517 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1518 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1519 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1520 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1523 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 1524 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 1525 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1526 | case CK_BitCast: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1527 | // FIXME: this is wrong for any cast other than a no-op cast. |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1528 | if (SETy->isVectorType()) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1529 | return Visit(SE); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 1530 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1531 | if (!SETy->isIntegerType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1532 | return Error(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1534 | APSInt Init; |
| 1535 | if (!EvaluateInteger(SE, Init, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1536 | return Error(E); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 1537 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1538 | assert((EltTy->isIntegerType() || EltTy->isRealFloatingType()) && |
| 1539 | "Vectors must be composed of ints or floats"); |
| 1540 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1541 | SmallVector<APValue, 4> Elts; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1542 | for (unsigned i = 0; i != NElts; ++i) { |
| 1543 | APSInt Tmp = Init.extOrTrunc(EltWidth); |
| 1544 | |
| 1545 | if (EltTy->isIntegerType()) |
| 1546 | Elts.push_back(APValue(Tmp)); |
| 1547 | else |
| 1548 | Elts.push_back(APValue(APFloat(Tmp))); |
| 1549 | |
| 1550 | Init >>= EltWidth; |
| 1551 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1552 | return Success(Elts, E); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 1553 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1554 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1555 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 1556 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1559 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1560 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1561 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1562 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1563 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1565 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1566 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1567 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1568 | // If a vector is initialized with a single element, that value |
| 1569 | // becomes every element of the vector, not just the first. |
| 1570 | // This is the behavior described in the IBM AltiVec documentation. |
| 1571 | if (NumInits == 1) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1572 | |
| 1573 | // Handle the case where the vector is initialized by another |
Tanya Lattner | b92ae0e | 2011-04-15 22:42:59 +0000 | [diff] [blame] | 1574 | // vector (OpenCL 6.1.6). |
| 1575 | if (E->getInit(0)->getType()->isVectorType()) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1576 | return Visit(E->getInit(0)); |
| 1577 | |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1578 | APValue InitValue; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1579 | if (EltTy->isIntegerType()) { |
| 1580 | llvm::APSInt sInt(32); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1581 | if (!EvaluateInteger(E->getInit(0), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1582 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1583 | InitValue = APValue(sInt); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1584 | } else { |
| 1585 | llvm::APFloat f(0.0); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1586 | if (!EvaluateFloat(E->getInit(0), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1587 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1588 | InitValue = APValue(f); |
| 1589 | } |
| 1590 | for (unsigned i = 0; i < NumElements; i++) { |
| 1591 | Elements.push_back(InitValue); |
| 1592 | } |
| 1593 | } else { |
| 1594 | for (unsigned i = 0; i < NumElements; i++) { |
| 1595 | if (EltTy->isIntegerType()) { |
| 1596 | llvm::APSInt sInt(32); |
| 1597 | if (i < NumInits) { |
| 1598 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1599 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1600 | } else { |
| 1601 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 1602 | } |
| 1603 | Elements.push_back(APValue(sInt)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1604 | } else { |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1605 | llvm::APFloat f(0.0); |
| 1606 | if (i < NumInits) { |
| 1607 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1608 | return Error(E); |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 1609 | } else { |
| 1610 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 1611 | } |
| 1612 | Elements.push_back(APValue(f)); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1613 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1616 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1619 | bool |
| 1620 | VectorExprEvaluator::ValueInitialization(const Expr *E) { |
| 1621 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1622 | QualType EltTy = VT->getElementType(); |
| 1623 | APValue ZeroElement; |
| 1624 | if (EltTy->isIntegerType()) |
| 1625 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 1626 | else |
| 1627 | ZeroElement = |
| 1628 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 1629 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1630 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1631 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1634 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 1635 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 1636 | return ValueInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1639 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1640 | // Array Evaluation |
| 1641 | //===----------------------------------------------------------------------===// |
| 1642 | |
| 1643 | namespace { |
| 1644 | class ArrayExprEvaluator |
| 1645 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
| 1646 | APValue &Result; |
| 1647 | public: |
| 1648 | |
| 1649 | ArrayExprEvaluator(EvalInfo &Info, APValue &Result) |
| 1650 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 1651 | |
| 1652 | bool Success(const APValue &V, const Expr *E) { |
| 1653 | assert(V.isArray() && "Expected array type"); |
| 1654 | Result = V; |
| 1655 | return true; |
| 1656 | } |
| 1657 | bool Error(const Expr *E) { return false; } |
| 1658 | |
| 1659 | bool VisitInitListExpr(const InitListExpr *E); |
| 1660 | }; |
| 1661 | } // end anonymous namespace |
| 1662 | |
| 1663 | static bool EvaluateArray(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 1664 | assert(E->isRValue() && E->getType()->isArrayType() && |
| 1665 | E->getType()->isLiteralType() && "not a literal array rvalue"); |
| 1666 | return ArrayExprEvaluator(Info, Result).Visit(E); |
| 1667 | } |
| 1668 | |
| 1669 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 1670 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 1671 | if (!CAT) |
| 1672 | return false; |
| 1673 | |
| 1674 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 1675 | CAT->getSize().getZExtValue()); |
| 1676 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
| 1677 | I != End; ++I) |
| 1678 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(I-E->begin()), |
| 1679 | Info, cast<Expr>(*I))) |
| 1680 | return false; |
| 1681 | |
| 1682 | if (!Result.hasArrayFiller()) return true; |
| 1683 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
| 1684 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 1685 | E->getArrayFiller()); |
| 1686 | } |
| 1687 | |
| 1688 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1689 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1690 | // |
| 1691 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 1692 | // types and back in constant folding. Integer values are thus represented |
| 1693 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1694 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1695 | |
| 1696 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 1697 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1698 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1699 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 1700 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1701 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1702 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1703 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 1704 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 1705 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1706 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 1707 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1708 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 1709 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1710 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1711 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1712 | return true; |
| 1713 | } |
| 1714 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1715 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1716 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 1717 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1718 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1719 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1720 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1721 | Result.getInt().setIsUnsigned( |
| 1722 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1727 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 1728 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1729 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1730 | return true; |
| 1731 | } |
| 1732 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1733 | bool Success(CharUnits Size, const Expr *E) { |
| 1734 | return Success(Size.getQuantity(), E); |
| 1735 | } |
| 1736 | |
| 1737 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1738 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1739 | // Take the first error. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 1740 | if (Info.EvalStatus.Diag == 0) { |
| 1741 | Info.EvalStatus.DiagLoc = L; |
| 1742 | Info.EvalStatus.Diag = D; |
| 1743 | Info.EvalStatus.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1744 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 1745 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 1746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1748 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 1749 | if (V.isLValue()) { |
| 1750 | Result = V; |
| 1751 | return true; |
| 1752 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1753 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 1754 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1755 | bool Error(const Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1756 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 1757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1759 | bool ValueInitialization(const Expr *E) { return Success(0, E); } |
| 1760 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1761 | //===--------------------------------------------------------------------===// |
| 1762 | // Visitor Methods |
| 1763 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 1764 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1765 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1766 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1767 | } |
| 1768 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1769 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1770 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1771 | |
| 1772 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 1773 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1774 | if (CheckReferencedDecl(E, E->getDecl())) |
| 1775 | return true; |
| 1776 | |
| 1777 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1778 | } |
| 1779 | bool VisitMemberExpr(const MemberExpr *E) { |
| 1780 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1781 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1782 | return true; |
| 1783 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1784 | |
| 1785 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1788 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1789 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1790 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1791 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 1792 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1793 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1794 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1795 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1796 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1797 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 1798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1800 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 1801 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 1802 | return ValueInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1805 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 1806 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 1809 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 1810 | return Success(E->getValue(), E); |
| 1811 | } |
| 1812 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 1813 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 1814 | return Success(E->getValue(), E); |
| 1815 | } |
| 1816 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 1817 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 1818 | return Success(E->getValue(), E); |
| 1819 | } |
| 1820 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1821 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1822 | bool VisitUnaryImag(const UnaryOperator *E); |
| 1823 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1824 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 1825 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1827 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 1828 | CharUnits GetAlignOfExpr(const Expr *E); |
| 1829 | CharUnits GetAlignOfType(QualType T); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1830 | static QualType GetObjectType(const Expr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1831 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1832 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1833 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1834 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1835 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1836 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 1837 | /// produce either the integer value or a pointer. |
| 1838 | /// |
| 1839 | /// GCC has a heinous extension which folds casts between pointer types and |
| 1840 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 1841 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 1842 | /// Some simple arithmetic on such values is supported (they are treated much |
| 1843 | /// like char*). |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1844 | static bool EvaluateIntegerOrLValue(const Expr* E, CCValue &Result, |
| 1845 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1846 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1847 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1848 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1849 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1850 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1851 | CCValue Val; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 1852 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 1853 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1854 | Result = Val.getInt(); |
| 1855 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1856 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 1857 | |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 1858 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1859 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 1860 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 1861 | // Check for signedness/width mismatches between E type and ECD value. |
| 1862 | bool SameSign = (ECD->getInitVal().isSigned() |
| 1863 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 1864 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 1865 | == Info.Ctx.getIntWidth(E->getType())); |
| 1866 | if (SameSign && SameWidth) |
| 1867 | return Success(ECD->getInitVal(), E); |
| 1868 | else { |
| 1869 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 1870 | // by computing a new value matching the type of E. |
| 1871 | llvm::APSInt Val = ECD->getInitVal(); |
| 1872 | if (!SameSign) |
| 1873 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 1874 | if (!SameWidth) |
| 1875 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 1876 | return Success(Val, E); |
| 1877 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 1878 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1879 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1882 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 1883 | /// as GCC. |
| 1884 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 1885 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1886 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1887 | enum gcc_type_class { |
| 1888 | no_type_class = -1, |
| 1889 | void_type_class, integer_type_class, char_type_class, |
| 1890 | enumeral_type_class, boolean_type_class, |
| 1891 | pointer_type_class, reference_type_class, offset_type_class, |
| 1892 | real_type_class, complex_type_class, |
| 1893 | function_type_class, method_type_class, |
| 1894 | record_type_class, union_type_class, |
| 1895 | array_type_class, string_type_class, |
| 1896 | lang_type_class |
| 1897 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
| 1899 | // 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] | 1900 | // ideal, however it is what gcc does. |
| 1901 | if (E->getNumArgs() == 0) |
| 1902 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1904 | QualType ArgTy = E->getArg(0)->getType(); |
| 1905 | if (ArgTy->isVoidType()) |
| 1906 | return void_type_class; |
| 1907 | else if (ArgTy->isEnumeralType()) |
| 1908 | return enumeral_type_class; |
| 1909 | else if (ArgTy->isBooleanType()) |
| 1910 | return boolean_type_class; |
| 1911 | else if (ArgTy->isCharType()) |
| 1912 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 1913 | else if (ArgTy->isIntegerType()) |
| 1914 | return integer_type_class; |
| 1915 | else if (ArgTy->isPointerType()) |
| 1916 | return pointer_type_class; |
| 1917 | else if (ArgTy->isReferenceType()) |
| 1918 | return reference_type_class; |
| 1919 | else if (ArgTy->isRealType()) |
| 1920 | return real_type_class; |
| 1921 | else if (ArgTy->isComplexType()) |
| 1922 | return complex_type_class; |
| 1923 | else if (ArgTy->isFunctionType()) |
| 1924 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 1925 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1926 | return record_type_class; |
| 1927 | else if (ArgTy->isUnionType()) |
| 1928 | return union_type_class; |
| 1929 | else if (ArgTy->isArrayType()) |
| 1930 | return array_type_class; |
| 1931 | else if (ArgTy->isUnionType()) |
| 1932 | return union_type_class; |
| 1933 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1934 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 1935 | return -1; |
| 1936 | } |
| 1937 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1938 | /// Retrieves the "underlying object type" of the given expression, |
| 1939 | /// as used by __builtin_object_size. |
| 1940 | QualType IntExprEvaluator::GetObjectType(const Expr *E) { |
| 1941 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 1942 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1943 | return VD->getType(); |
| 1944 | } else if (isa<CompoundLiteralExpr>(E)) { |
| 1945 | return E->getType(); |
| 1946 | } |
| 1947 | |
| 1948 | return QualType(); |
| 1949 | } |
| 1950 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1951 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1952 | // TODO: Perhaps we should let LLVM lower this? |
| 1953 | LValue Base; |
| 1954 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 1955 | return false; |
| 1956 | |
| 1957 | // If we can prove the base is null, lower to zero now. |
| 1958 | const Expr *LVBase = Base.getLValueBase(); |
| 1959 | if (!LVBase) return Success(0, E); |
| 1960 | |
| 1961 | QualType T = GetObjectType(LVBase); |
| 1962 | if (T.isNull() || |
| 1963 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1964 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1965 | T->isVariablyModifiedType() || |
| 1966 | T->isDependentType()) |
| 1967 | return false; |
| 1968 | |
| 1969 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 1970 | CharUnits Offset = Base.getLValueOffset(); |
| 1971 | |
| 1972 | if (!Offset.isNegative() && Offset <= Size) |
| 1973 | Size -= Offset; |
| 1974 | else |
| 1975 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 1976 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1979 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1980 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1981 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 1982 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1983 | |
| 1984 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1985 | if (TryEvaluateBuiltinObjectSize(E)) |
| 1986 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1987 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 1988 | // If evaluating the argument has side-effects we can't determine |
| 1989 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 1990 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1991 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 1992 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1993 | return Success(0, E); |
| 1994 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1995 | |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 1996 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 1997 | } |
| 1998 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1999 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2000 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2002 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2003 | // __builtin_constant_p always has one operand: it returns true if that |
| 2004 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2005 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 2006 | |
| 2007 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2008 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2009 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 2010 | return Success(Operand, E); |
| 2011 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 2012 | |
| 2013 | case Builtin::BI__builtin_expect: |
| 2014 | return Visit(E->getArg(0)); |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2015 | |
| 2016 | case Builtin::BIstrlen: |
| 2017 | case Builtin::BI__builtin_strlen: |
| 2018 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 2019 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2020 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2021 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 2022 | // The string literal may have embedded null characters. Find the first |
| 2023 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2024 | StringRef Str = S->getString(); |
| 2025 | StringRef::size_type Pos = Str.find(0); |
| 2026 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 2027 | Str = Str.substr(0, Pos); |
| 2028 | |
| 2029 | return Success(Str.size(), E); |
| 2030 | } |
| 2031 | |
| 2032 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 2033 | |
| 2034 | case Builtin::BI__atomic_is_lock_free: { |
| 2035 | APSInt SizeVal; |
| 2036 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 2037 | return false; |
| 2038 | |
| 2039 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 2040 | // of two less than the maximum inline atomic width, we know it is |
| 2041 | // lock-free. If the size isn't a power of two, or greater than the |
| 2042 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 2043 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 2044 | // the answer can only be determined at runtime; for example, 16-byte |
| 2045 | // atomics have lock-free implementations on some, but not all, |
| 2046 | // x86-64 processors. |
| 2047 | |
| 2048 | // Check power-of-two. |
| 2049 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 2050 | if (!Size.isPowerOfTwo()) |
| 2051 | #if 0 |
| 2052 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 2053 | // settles. |
| 2054 | return Success(0, E); |
| 2055 | #else |
| 2056 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 2057 | #endif |
| 2058 | |
| 2059 | #if 0 |
| 2060 | // Check against promotion width. |
| 2061 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 2062 | // settles. |
| 2063 | unsigned PromoteWidthBits = |
| 2064 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 2065 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 2066 | return Success(0, E); |
| 2067 | #endif |
| 2068 | |
| 2069 | // Check against inlining width. |
| 2070 | unsigned InlineWidthBits = |
| 2071 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 2072 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 2073 | return Success(1, E); |
| 2074 | |
| 2075 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
| 2076 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2077 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2078 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2079 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2080 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 2081 | if (!A.getLValueBase()) |
| 2082 | return !B.getLValueBase(); |
| 2083 | if (!B.getLValueBase()) |
| 2084 | return false; |
| 2085 | |
| 2086 | if (A.getLValueBase() != B.getLValueBase()) { |
| 2087 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 2088 | if (!ADecl) |
| 2089 | return false; |
| 2090 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 2091 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2092 | return false; |
| 2093 | } |
| 2094 | |
| 2095 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2096 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2099 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2100 | if (E->isAssignmentOp()) |
| 2101 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 2102 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2103 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2104 | VisitIgnoredValue(E->getLHS()); |
| 2105 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | if (E->isLogicalOp()) { |
| 2109 | // These need to be handled specially because the operands aren't |
| 2110 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 2111 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2112 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2113 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2114 | // We were able to evaluate the LHS, see if we can get away with not |
| 2115 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2116 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2117 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2118 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2119 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2120 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2121 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2122 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2123 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } else { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2126 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2127 | // We can't evaluate the LHS; however, sometimes the result |
| 2128 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2129 | if (rhsResult == (E->getOpcode() == BO_LOr) || |
| 2130 | !rhsResult == (E->getOpcode() == BO_LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2131 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 2132 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2133 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2134 | |
| 2135 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 2138 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2139 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2140 | return false; |
| 2141 | } |
| 2142 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2143 | QualType LHSTy = E->getLHS()->getType(); |
| 2144 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2145 | |
| 2146 | if (LHSTy->isAnyComplexType()) { |
| 2147 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2148 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 2151 | return false; |
| 2152 | |
| 2153 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 2154 | return false; |
| 2155 | |
| 2156 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2158 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2160 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 2161 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2162 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2163 | return Success((CR_r == APFloat::cmpEqual && |
| 2164 | CR_i == APFloat::cmpEqual), E); |
| 2165 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2166 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2167 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2169 | CR_r == APFloat::cmpLessThan || |
| 2170 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2172 | CR_i == APFloat::cmpLessThan || |
| 2173 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2174 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2175 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2176 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2177 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 2178 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 2179 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2180 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2181 | "Invalid compex comparison."); |
| 2182 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 2183 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 2184 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 2185 | } |
| 2186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2188 | if (LHSTy->isRealFloatingType() && |
| 2189 | RHSTy->isRealFloatingType()) { |
| 2190 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2192 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 2193 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2195 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 2196 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2198 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 2199 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2200 | switch (E->getOpcode()) { |
| 2201 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2202 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2203 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2204 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2205 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2206 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2207 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2208 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2209 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2211 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2212 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2213 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2214 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 2216 | || CR == APFloat::cmpLessThan |
| 2217 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2218 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 2219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2221 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2222 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2223 | LValue LHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2224 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 2225 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2226 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2227 | LValue RHSValue; |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2228 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 2229 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2230 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2231 | // Reject differing bases from the normal codepath; we special-case |
| 2232 | // comparisons to null. |
| 2233 | if (!HasSameBase(LHSValue, RHSValue)) { |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2234 | // Inequalities and subtractions between unrelated pointers have |
| 2235 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2236 | if (!E->isEqualityOp()) |
| 2237 | return false; |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 2238 | // A constant address may compare equal to the address of a symbol. |
| 2239 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2240 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 2241 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 2242 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 2243 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2244 | // It's implementation-defined whether distinct literals will have |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2245 | // distinct addresses. In clang, we do not guarantee the addresses are |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 2246 | // distinct. However, we do know that the address of a literal will be |
| 2247 | // non-null. |
| 2248 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 2249 | LHSValue.Base && RHSValue.Base) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2250 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2251 | // We can't tell whether weak symbols will end up pointing to the same |
| 2252 | // object. |
| 2253 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2254 | return false; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2255 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 2256 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 2257 | // lead to inconsistent results for comparisons involving the address |
| 2258 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 2259 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2260 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2261 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2262 | // FIXME: Implement the C++11 restrictions: |
| 2263 | // - Pointer subtractions must be on elements of the same array. |
| 2264 | // - Pointer comparisons must be between members with the same access. |
| 2265 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2266 | if (E->getOpcode() == BO_Sub) { |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 2267 | QualType Type = E->getLHS()->getType(); |
| 2268 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2269 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2270 | CharUnits ElementSize = CharUnits::One(); |
Eli Friedman | ce1bca7 | 2009-06-04 20:23:20 +0000 | [diff] [blame] | 2271 | if (!ElementType->isVoidType() && !ElementType->isFunctionType()) |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2272 | ElementSize = Info.Ctx.getTypeSizeInChars(ElementType); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2273 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2274 | CharUnits Diff = LHSValue.getLValueOffset() - |
| 2275 | RHSValue.getLValueOffset(); |
| 2276 | return Success(Diff / ElementSize, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2277 | } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 2278 | |
| 2279 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 2280 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 2281 | switch (E->getOpcode()) { |
| 2282 | default: llvm_unreachable("missing comparison operator"); |
| 2283 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 2284 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 2285 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 2286 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 2287 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 2288 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 2289 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2292 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 2293 | !RHSTy->isIntegralOrEnumerationType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2294 | // We can't continue from here for non-integral types, and they |
| 2295 | // could potentially confuse the following operations. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2296 | return false; |
| 2297 | } |
| 2298 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2299 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2300 | CCValue LHSVal; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2301 | if (!EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info)) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2302 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 2303 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2304 | if (!Visit(E->getRHS())) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2305 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2306 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2309 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2310 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 2311 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2312 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2313 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2314 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2315 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 2316 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2317 | return true; |
| 2318 | } |
| 2319 | |
| 2320 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2321 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2322 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2323 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 2324 | LHSVal.getInt().getZExtValue()); |
| 2325 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2326 | return true; |
| 2327 | } |
| 2328 | |
| 2329 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2330 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2331 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2332 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2333 | APSInt &LHS = LHSVal.getInt(); |
| 2334 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 2335 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2336 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 2337 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2338 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2339 | case BO_Mul: return Success(LHS * RHS, E); |
| 2340 | case BO_Add: return Success(LHS + RHS, E); |
| 2341 | case BO_Sub: return Success(LHS - RHS, E); |
| 2342 | case BO_And: return Success(LHS & RHS, E); |
| 2343 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 2344 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2345 | case BO_Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2346 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2347 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2348 | return Success(LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2349 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 2350 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2351 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2352 | return Success(LHS % RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2353 | case BO_Shl: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2354 | // During constant-folding, a negative shift is an opposite shift. |
| 2355 | if (RHS.isSigned() && RHS.isNegative()) { |
| 2356 | RHS = -RHS; |
| 2357 | goto shift_right; |
| 2358 | } |
| 2359 | |
| 2360 | shift_left: |
| 2361 | unsigned SA |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2362 | = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2363 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2364 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2365 | case BO_Shr: { |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 2366 | // During constant-folding, a negative shift is an opposite shift. |
| 2367 | if (RHS.isSigned() && RHS.isNegative()) { |
| 2368 | RHS = -RHS; |
| 2369 | goto shift_left; |
| 2370 | } |
| 2371 | |
| 2372 | shift_right: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | unsigned SA = |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2374 | (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2375 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2378 | case BO_LT: return Success(LHS < RHS, E); |
| 2379 | case BO_GT: return Success(LHS > RHS, E); |
| 2380 | case BO_LE: return Success(LHS <= RHS, E); |
| 2381 | case BO_GE: return Success(LHS >= RHS, E); |
| 2382 | case BO_EQ: return Success(LHS == RHS, E); |
| 2383 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 2384 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2387 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2388 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2389 | // the result is the size of the referenced type." |
| 2390 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2391 | // result shall be the alignment of the referenced type." |
| 2392 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 2393 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 2394 | |
| 2395 | // __alignof is defined to return the preferred alignment. |
| 2396 | return Info.Ctx.toCharUnitsFromBits( |
| 2397 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2400 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2401 | E = E->IgnoreParens(); |
| 2402 | |
| 2403 | // 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] | 2404 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2405 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2406 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 2407 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2409 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 2410 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 2411 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 2412 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2413 | return GetAlignOfType(E->getType()); |
| 2414 | } |
| 2415 | |
| 2416 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2417 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 2418 | /// a result as the expression's type. |
| 2419 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 2420 | const UnaryExprOrTypeTraitExpr *E) { |
| 2421 | switch(E->getKind()) { |
| 2422 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2423 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2424 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2425 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 2426 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 2427 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2428 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2429 | case UETT_VecStep: { |
| 2430 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2431 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2432 | if (Ty->isVectorType()) { |
| 2433 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 2434 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2435 | // The vec_step built-in functions that take a 3-component |
| 2436 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 2437 | if (n == 3) |
| 2438 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 2439 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2440 | return Success(n, E); |
| 2441 | } else |
| 2442 | return Success(1, E); |
| 2443 | } |
| 2444 | |
| 2445 | case UETT_SizeOf: { |
| 2446 | QualType SrcTy = E->getTypeOfArgument(); |
| 2447 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2448 | // the result is the size of the referenced type." |
| 2449 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2450 | // result shall be the alignment of the referenced type." |
| 2451 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 2452 | SrcTy = Ref->getPointeeType(); |
| 2453 | |
| 2454 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 2455 | // extension. |
| 2456 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 2457 | return Success(1, E); |
| 2458 | |
| 2459 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 2460 | if (!SrcTy->isConstantSizeType()) |
| 2461 | return false; |
| 2462 | |
| 2463 | // Get information about the size. |
| 2464 | return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | llvm_unreachable("unknown expr/type trait"); |
| 2469 | return false; |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2472 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2473 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2474 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2475 | if (n == 0) |
| 2476 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2477 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2478 | for (unsigned i = 0; i != n; ++i) { |
| 2479 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 2480 | switch (ON.getKind()) { |
| 2481 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2482 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2483 | APSInt IdxResult; |
| 2484 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 2485 | return false; |
| 2486 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 2487 | if (!AT) |
| 2488 | return false; |
| 2489 | CurrentType = AT->getElementType(); |
| 2490 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 2491 | Result += IdxResult.getSExtValue() * ElementSize; |
| 2492 | break; |
| 2493 | } |
| 2494 | |
| 2495 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 2496 | FieldDecl *MemberDecl = ON.getField(); |
| 2497 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 2498 | if (!RT) |
| 2499 | return false; |
| 2500 | RecordDecl *RD = RT->getDecl(); |
| 2501 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2502 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 2503 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 2504 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2505 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 2506 | break; |
| 2507 | } |
| 2508 | |
| 2509 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 2510 | llvm_unreachable("dependent __builtin_offsetof"); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | |
| 2513 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 2514 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 2515 | if (BaseSpec->isVirtual()) |
| 2516 | return false; |
| 2517 | |
| 2518 | // Find the layout of the class whose base we are looking into. |
| 2519 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| 2520 | if (!RT) |
| 2521 | return false; |
| 2522 | RecordDecl *RD = RT->getDecl(); |
| 2523 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 2524 | |
| 2525 | // Find the base class itself. |
| 2526 | CurrentType = BaseSpec->getType(); |
| 2527 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 2528 | if (!BaseRT) |
| 2529 | return false; |
| 2530 | |
| 2531 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 2532 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 2533 | break; |
| 2534 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2535 | } |
| 2536 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2537 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2540 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2541 | if (E->getOpcode() == UO_LNot) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2542 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 2543 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2544 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2545 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2546 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 2549 | // Only handle integral operations... |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2550 | if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType()) |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 2551 | return false; |
| 2552 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2553 | // Get the operand value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2554 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2555 | if (!Evaluate(Val, Info, E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 2556 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 2558 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2559 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 2560 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 2561 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 2562 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2563 | case UO_Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 2564 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 2565 | // If so, we could clear the diagnostic ID. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2566 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2567 | case UO_Plus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2568 | // The result is just the value. |
| 2569 | return Success(Val, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2570 | case UO_Minus: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2571 | if (!Val.isInt()) return false; |
| 2572 | return Success(-Val.getInt(), E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2573 | case UO_Not: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2574 | if (!Val.isInt()) return false; |
| 2575 | return Success(~Val.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2576 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 2579 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 2580 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2581 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2582 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 2583 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 2584 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 2585 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2586 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2587 | case CK_BaseToDerived: |
| 2588 | case CK_DerivedToBase: |
| 2589 | case CK_UncheckedDerivedToBase: |
| 2590 | case CK_Dynamic: |
| 2591 | case CK_ToUnion: |
| 2592 | case CK_ArrayToPointerDecay: |
| 2593 | case CK_FunctionToPointerDecay: |
| 2594 | case CK_NullToPointer: |
| 2595 | case CK_NullToMemberPointer: |
| 2596 | case CK_BaseToDerivedMemberPointer: |
| 2597 | case CK_DerivedToBaseMemberPointer: |
| 2598 | case CK_ConstructorConversion: |
| 2599 | case CK_IntegralToPointer: |
| 2600 | case CK_ToVoid: |
| 2601 | case CK_VectorSplat: |
| 2602 | case CK_IntegralToFloating: |
| 2603 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2604 | case CK_CPointerToObjCPointerCast: |
| 2605 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2606 | case CK_AnyPointerToBlockPointerCast: |
| 2607 | case CK_ObjCObjectLValueCast: |
| 2608 | case CK_FloatingRealToComplex: |
| 2609 | case CK_FloatingComplexToReal: |
| 2610 | case CK_FloatingComplexCast: |
| 2611 | case CK_FloatingComplexToIntegralComplex: |
| 2612 | case CK_IntegralRealToComplex: |
| 2613 | case CK_IntegralComplexCast: |
| 2614 | case CK_IntegralComplexToFloatingComplex: |
| 2615 | llvm_unreachable("invalid cast kind for integral value"); |
| 2616 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 2617 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2618 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2619 | case CK_LValueBitCast: |
| 2620 | case CK_UserDefinedConversion: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2621 | case CK_ARCProduceObject: |
| 2622 | case CK_ARCConsumeObject: |
| 2623 | case CK_ARCReclaimReturnedObject: |
| 2624 | case CK_ARCExtendBlockObject: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2625 | return false; |
| 2626 | |
| 2627 | case CK_LValueToRValue: |
| 2628 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2629 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2630 | |
| 2631 | case CK_MemberPointerToBoolean: |
| 2632 | case CK_PointerToBoolean: |
| 2633 | case CK_IntegralToBoolean: |
| 2634 | case CK_FloatingToBoolean: |
| 2635 | case CK_FloatingComplexToBoolean: |
| 2636 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2637 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2638 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2639 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 2640 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2643 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 2644 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2645 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2646 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 2647 | if (!Result.isInt()) { |
| 2648 | // Only allow casts of lvalues if they are lossless. |
| 2649 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 2650 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2651 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 2652 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 2653 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 2654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2656 | case CK_PointerToIntegral: { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2657 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 2658 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 2659 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2660 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 2661 | if (LV.getLValueBase()) { |
| 2662 | // Only allow based lvalue casts if they are lossless. |
| 2663 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 2664 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2665 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2666 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 2667 | return true; |
| 2668 | } |
| 2669 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 2670 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 2671 | SrcType); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 2672 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2673 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2674 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2675 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2676 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 2677 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 2678 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2679 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 2680 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 2681 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2682 | case CK_FloatingToIntegral: { |
| 2683 | APFloat F(0.0); |
| 2684 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 2685 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 2686 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2687 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
| 2688 | } |
| 2689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 2691 | llvm_unreachable("unknown cast resulting in integral value"); |
| 2692 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 2693 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2694 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2695 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 2696 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2697 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2698 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 2699 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 2700 | return Success(LV.getComplexIntReal(), E); |
| 2701 | } |
| 2702 | |
| 2703 | return Visit(E->getSubExpr()); |
| 2704 | } |
| 2705 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2706 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2707 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2708 | ComplexValue LV; |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 2709 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 2710 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 2711 | return Success(LV.getComplexIntImag(), E); |
| 2712 | } |
| 2713 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2714 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 2715 | return Success(0, E); |
| 2716 | } |
| 2717 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2718 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 2719 | return Success(E->getPackLength(), E); |
| 2720 | } |
| 2721 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 2722 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 2723 | return Success(E->getValue(), E); |
| 2724 | } |
| 2725 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2726 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2727 | // Float Evaluation |
| 2728 | //===----------------------------------------------------------------------===// |
| 2729 | |
| 2730 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2731 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2732 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2733 | APFloat &Result; |
| 2734 | public: |
| 2735 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2736 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2737 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2738 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2739 | Result = V.getFloat(); |
| 2740 | return true; |
| 2741 | } |
| 2742 | bool Error(const Stmt *S) { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2743 | return false; |
| 2744 | } |
| 2745 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2746 | bool ValueInitialization(const Expr *E) { |
| 2747 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 2748 | return true; |
| 2749 | } |
| 2750 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2751 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2752 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2753 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2754 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 2755 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2756 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 2757 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2758 | bool VisitUnaryReal(const UnaryOperator *E); |
| 2759 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2760 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2761 | // FIXME: Missing: array subscript of vector, member of vector, |
| 2762 | // ImplicitValueInitExpr |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2763 | }; |
| 2764 | } // end anonymous namespace |
| 2765 | |
| 2766 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2767 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2768 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2771 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 2772 | QualType ResultTy, |
| 2773 | const Expr *Arg, |
| 2774 | bool SNaN, |
| 2775 | llvm::APFloat &Result) { |
| 2776 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 2777 | if (!S) return false; |
| 2778 | |
| 2779 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 2780 | |
| 2781 | llvm::APInt fill; |
| 2782 | |
| 2783 | // Treat empty strings as if they were zero. |
| 2784 | if (S->getString().empty()) |
| 2785 | fill = llvm::APInt(32, 0); |
| 2786 | else if (S->getString().getAsInteger(0, fill)) |
| 2787 | return false; |
| 2788 | |
| 2789 | if (SNaN) |
| 2790 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 2791 | else |
| 2792 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2796 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2797 | switch (E->isBuiltinCall(Info.Ctx)) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2798 | default: |
| 2799 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 2800 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2801 | case Builtin::BI__builtin_huge_val: |
| 2802 | case Builtin::BI__builtin_huge_valf: |
| 2803 | case Builtin::BI__builtin_huge_vall: |
| 2804 | case Builtin::BI__builtin_inf: |
| 2805 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 2806 | case Builtin::BI__builtin_infl: { |
| 2807 | const llvm::fltSemantics &Sem = |
| 2808 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 2809 | Result = llvm::APFloat::getInf(Sem); |
| 2810 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 2811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 2813 | case Builtin::BI__builtin_nans: |
| 2814 | case Builtin::BI__builtin_nansf: |
| 2815 | case Builtin::BI__builtin_nansl: |
| 2816 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 2817 | true, Result); |
| 2818 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 2819 | case Builtin::BI__builtin_nan: |
| 2820 | case Builtin::BI__builtin_nanf: |
| 2821 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 2822 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 2823 | // can't constant fold it. |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 2824 | return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 2825 | false, Result); |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2826 | |
| 2827 | case Builtin::BI__builtin_fabs: |
| 2828 | case Builtin::BI__builtin_fabsf: |
| 2829 | case Builtin::BI__builtin_fabsl: |
| 2830 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 2831 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2833 | if (Result.isNegative()) |
| 2834 | Result.changeSign(); |
| 2835 | return true; |
| 2836 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | case Builtin::BI__builtin_copysign: |
| 2838 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2839 | case Builtin::BI__builtin_copysignl: { |
| 2840 | APFloat RHS(0.); |
| 2841 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 2842 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 2843 | return false; |
| 2844 | Result.copySign(RHS); |
| 2845 | return true; |
| 2846 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2850 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 2851 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 2852 | ComplexValue CV; |
| 2853 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 2854 | return false; |
| 2855 | Result = CV.FloatReal; |
| 2856 | return true; |
| 2857 | } |
| 2858 | |
| 2859 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 2863 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 2864 | ComplexValue CV; |
| 2865 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 2866 | return false; |
| 2867 | Result = CV.FloatImag; |
| 2868 | return true; |
| 2869 | } |
| 2870 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2871 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 2872 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 2873 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 2874 | return true; |
| 2875 | } |
| 2876 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2877 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2878 | switch (E->getOpcode()) { |
| 2879 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2880 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 2881 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2882 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 2883 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 2884 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2885 | Result.changeSign(); |
| 2886 | return true; |
| 2887 | } |
| 2888 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 2889 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2890 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2891 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2892 | VisitIgnoredValue(E->getLHS()); |
| 2893 | return Visit(E->getRHS()); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Richard Smith | ee591a9 | 2011-10-28 23:26:52 +0000 | [diff] [blame] | 2896 | // We can't evaluate pointer-to-member operations or assignments. |
| 2897 | if (E->isPtrMemOp() || E->isAssignmentOp()) |
Anders Carlsson | 96e9366 | 2010-10-31 01:21:47 +0000 | [diff] [blame] | 2898 | return false; |
| 2899 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2900 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 2901 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 2902 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2903 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 2904 | return false; |
| 2905 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 2906 | return false; |
| 2907 | |
| 2908 | switch (E->getOpcode()) { |
| 2909 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2910 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2911 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2912 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2913 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2914 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 2915 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2916 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2917 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2918 | return true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2919 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2920 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2921 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 2926 | Result = E->getValue(); |
| 2927 | return true; |
| 2928 | } |
| 2929 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2930 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 2931 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 2933 | switch (E->getCastKind()) { |
| 2934 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2935 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 2936 | |
| 2937 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2938 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 2939 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2940 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2942 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2943 | return true; |
| 2944 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 2945 | |
| 2946 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2947 | if (!Visit(SubExpr)) |
| 2948 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2949 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 2950 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2951 | return true; |
| 2952 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2953 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 2954 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 2955 | ComplexValue V; |
| 2956 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 2957 | return false; |
| 2958 | Result = V.getComplexFloatReal(); |
| 2959 | return true; |
| 2960 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 2961 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2962 | |
| 2963 | return false; |
| 2964 | } |
| 2965 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 2966 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 2967 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2968 | //===----------------------------------------------------------------------===// |
| 2969 | |
| 2970 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2971 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2972 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2973 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2975 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2976 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2977 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 2978 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2979 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2980 | Result.setFrom(V); |
| 2981 | return true; |
| 2982 | } |
| 2983 | bool Error(const Expr *E) { |
| 2984 | return false; |
| 2985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2987 | //===--------------------------------------------------------------------===// |
| 2988 | // Visitor Methods |
| 2989 | //===--------------------------------------------------------------------===// |
| 2990 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2991 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2993 | bool VisitCastExpr(const CastExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 2995 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 2996 | bool VisitUnaryOperator(const UnaryOperator *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2997 | // FIXME Missing: ImplicitValueInitExpr, InitListExpr |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 2998 | }; |
| 2999 | } // end anonymous namespace |
| 3000 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3001 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 3002 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3003 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3004 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3007 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 3008 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3009 | |
| 3010 | if (SubExpr->getType()->isRealFloatingType()) { |
| 3011 | Result.makeComplexFloat(); |
| 3012 | APFloat &Imag = Result.FloatImag; |
| 3013 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 3014 | return false; |
| 3015 | |
| 3016 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 3017 | return true; |
| 3018 | } else { |
| 3019 | assert(SubExpr->getType()->isIntegerType() && |
| 3020 | "Unexpected imaginary literal."); |
| 3021 | |
| 3022 | Result.makeComplexInt(); |
| 3023 | APSInt &Imag = Result.IntImag; |
| 3024 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 3025 | return false; |
| 3026 | |
| 3027 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 3028 | return true; |
| 3029 | } |
| 3030 | } |
| 3031 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3032 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3033 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3034 | switch (E->getCastKind()) { |
| 3035 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3036 | case CK_BaseToDerived: |
| 3037 | case CK_DerivedToBase: |
| 3038 | case CK_UncheckedDerivedToBase: |
| 3039 | case CK_Dynamic: |
| 3040 | case CK_ToUnion: |
| 3041 | case CK_ArrayToPointerDecay: |
| 3042 | case CK_FunctionToPointerDecay: |
| 3043 | case CK_NullToPointer: |
| 3044 | case CK_NullToMemberPointer: |
| 3045 | case CK_BaseToDerivedMemberPointer: |
| 3046 | case CK_DerivedToBaseMemberPointer: |
| 3047 | case CK_MemberPointerToBoolean: |
| 3048 | case CK_ConstructorConversion: |
| 3049 | case CK_IntegralToPointer: |
| 3050 | case CK_PointerToIntegral: |
| 3051 | case CK_PointerToBoolean: |
| 3052 | case CK_ToVoid: |
| 3053 | case CK_VectorSplat: |
| 3054 | case CK_IntegralCast: |
| 3055 | case CK_IntegralToBoolean: |
| 3056 | case CK_IntegralToFloating: |
| 3057 | case CK_FloatingToIntegral: |
| 3058 | case CK_FloatingToBoolean: |
| 3059 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3060 | case CK_CPointerToObjCPointerCast: |
| 3061 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3062 | case CK_AnyPointerToBlockPointerCast: |
| 3063 | case CK_ObjCObjectLValueCast: |
| 3064 | case CK_FloatingComplexToReal: |
| 3065 | case CK_FloatingComplexToBoolean: |
| 3066 | case CK_IntegralComplexToReal: |
| 3067 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3068 | case CK_ARCProduceObject: |
| 3069 | case CK_ARCConsumeObject: |
| 3070 | case CK_ARCReclaimReturnedObject: |
| 3071 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3072 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 3073 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3074 | case CK_LValueToRValue: |
| 3075 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3076 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3077 | |
| 3078 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3079 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3080 | case CK_UserDefinedConversion: |
| 3081 | return false; |
| 3082 | |
| 3083 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3084 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3085 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3086 | return false; |
| 3087 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3088 | Result.makeComplexFloat(); |
| 3089 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 3090 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3093 | case CK_FloatingComplexCast: { |
| 3094 | if (!Visit(E->getSubExpr())) |
| 3095 | return false; |
| 3096 | |
| 3097 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3098 | QualType From |
| 3099 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3100 | |
| 3101 | Result.FloatReal |
| 3102 | = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx); |
| 3103 | Result.FloatImag |
| 3104 | = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx); |
| 3105 | return true; |
| 3106 | } |
| 3107 | |
| 3108 | case CK_FloatingComplexToIntegralComplex: { |
| 3109 | if (!Visit(E->getSubExpr())) |
| 3110 | return false; |
| 3111 | |
| 3112 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3113 | QualType From |
| 3114 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3115 | Result.makeComplexInt(); |
| 3116 | Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx); |
| 3117 | Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx); |
| 3118 | return true; |
| 3119 | } |
| 3120 | |
| 3121 | case CK_IntegralRealToComplex: { |
| 3122 | APSInt &Real = Result.IntReal; |
| 3123 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 3124 | return false; |
| 3125 | |
| 3126 | Result.makeComplexInt(); |
| 3127 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 3128 | return true; |
| 3129 | } |
| 3130 | |
| 3131 | case CK_IntegralComplexCast: { |
| 3132 | if (!Visit(E->getSubExpr())) |
| 3133 | return false; |
| 3134 | |
| 3135 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3136 | QualType From |
| 3137 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3138 | |
| 3139 | Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx); |
| 3140 | Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx); |
| 3141 | return true; |
| 3142 | } |
| 3143 | |
| 3144 | case CK_IntegralComplexToFloatingComplex: { |
| 3145 | if (!Visit(E->getSubExpr())) |
| 3146 | return false; |
| 3147 | |
| 3148 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 3149 | QualType From |
| 3150 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 3151 | Result.makeComplexFloat(); |
| 3152 | Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx); |
| 3153 | Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx); |
| 3154 | return true; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 3159 | return false; |
| 3160 | } |
| 3161 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3162 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3163 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3164 | VisitIgnoredValue(E->getLHS()); |
| 3165 | return Visit(E->getRHS()); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3166 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3167 | if (!Visit(E->getLHS())) |
| 3168 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3170 | ComplexValue RHS; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3171 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3172 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3173 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3174 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 3175 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3176 | switch (E->getOpcode()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3177 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3178 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3179 | if (Result.isComplexFloat()) { |
| 3180 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 3181 | APFloat::rmNearestTiesToEven); |
| 3182 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 3183 | APFloat::rmNearestTiesToEven); |
| 3184 | } else { |
| 3185 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 3186 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 3187 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3188 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3189 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3190 | if (Result.isComplexFloat()) { |
| 3191 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 3192 | APFloat::rmNearestTiesToEven); |
| 3193 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 3194 | APFloat::rmNearestTiesToEven); |
| 3195 | } else { |
| 3196 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 3197 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 3198 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3199 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3200 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3201 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3202 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3203 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 3204 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 3205 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 3206 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3208 | APFloat Tmp = LHS_r; |
| 3209 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3210 | Result.getComplexFloatReal() = Tmp; |
| 3211 | Tmp = LHS_i; |
| 3212 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3213 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 3214 | |
| 3215 | Tmp = LHS_r; |
| 3216 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3217 | Result.getComplexFloatImag() = Tmp; |
| 3218 | Tmp = LHS_i; |
| 3219 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3220 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 3221 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3222 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3224 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 3225 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 3227 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 3228 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 3229 | } |
| 3230 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3231 | case BO_Div: |
| 3232 | if (Result.isComplexFloat()) { |
| 3233 | ComplexValue LHS = Result; |
| 3234 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 3235 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 3236 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 3237 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 3238 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 3239 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 3240 | |
| 3241 | APFloat Den = RHS_r; |
| 3242 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3243 | APFloat Tmp = RHS_i; |
| 3244 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3245 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 3246 | |
| 3247 | Res_r = LHS_r; |
| 3248 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3249 | Tmp = LHS_i; |
| 3250 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3251 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 3252 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 3253 | |
| 3254 | Res_i = LHS_i; |
| 3255 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 3256 | Tmp = LHS_r; |
| 3257 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 3258 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 3259 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 3260 | } else { |
| 3261 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) { |
| 3262 | // FIXME: what about diagnostics? |
| 3263 | return false; |
| 3264 | } |
| 3265 | ComplexValue LHS = Result; |
| 3266 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 3267 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 3268 | Result.getComplexIntReal() = |
| 3269 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 3270 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 3271 | Result.getComplexIntImag() = |
| 3272 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 3273 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 3274 | } |
| 3275 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 3278 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 3281 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 3282 | // Get the operand value into 'Result'. |
| 3283 | if (!Visit(E->getSubExpr())) |
| 3284 | return false; |
| 3285 | |
| 3286 | switch (E->getOpcode()) { |
| 3287 | default: |
| 3288 | // FIXME: what about diagnostics? |
| 3289 | return false; |
| 3290 | case UO_Extension: |
| 3291 | return true; |
| 3292 | case UO_Plus: |
| 3293 | // The result is always just the subexpr. |
| 3294 | return true; |
| 3295 | case UO_Minus: |
| 3296 | if (Result.isComplexFloat()) { |
| 3297 | Result.getComplexFloatReal().changeSign(); |
| 3298 | Result.getComplexFloatImag().changeSign(); |
| 3299 | } |
| 3300 | else { |
| 3301 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 3302 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 3303 | } |
| 3304 | return true; |
| 3305 | case UO_Not: |
| 3306 | if (Result.isComplexFloat()) |
| 3307 | Result.getComplexFloatImag().changeSign(); |
| 3308 | else |
| 3309 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 3310 | return true; |
| 3311 | } |
| 3312 | } |
| 3313 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 3314 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3315 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3316 | //===----------------------------------------------------------------------===// |
| 3317 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3318 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3319 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 3320 | // are. |
| 3321 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 3322 | LValue LV; |
| 3323 | if (!EvaluateLValue(E, LV, Info)) |
| 3324 | return false; |
| 3325 | LV.moveInto(Result); |
| 3326 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3327 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3328 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3329 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3330 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3331 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3332 | } else if (E->getType()->hasPointerRepresentation()) { |
| 3333 | LValue LV; |
| 3334 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3335 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3336 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3337 | } else if (E->getType()->isRealFloatingType()) { |
| 3338 | llvm::APFloat F(0.0); |
| 3339 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3340 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3341 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3342 | } else if (E->getType()->isAnyComplexType()) { |
| 3343 | ComplexValue C; |
| 3344 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3345 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3346 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3347 | } else if (E->getType()->isMemberPointerType()) { |
| 3348 | // FIXME: Implement evaluation of pointer-to-member types. |
| 3349 | return false; |
| 3350 | } else if (E->getType()->isArrayType() && E->getType()->isLiteralType()) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3351 | if (!EvaluateArray(E, Result, Info)) |
| 3352 | return false; |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3353 | } else if (E->getType()->isRecordType() && E->getType()->isLiteralType()) { |
| 3354 | // FIXME: Implement evaluation of record rvalues. |
| 3355 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 3356 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 3357 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 3358 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 3359 | return true; |
| 3360 | } |
| 3361 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3362 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 3363 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 3364 | /// since later initializers for an object can indirectly refer to subobjects |
| 3365 | /// which were initialized earlier. |
| 3366 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
| 3367 | const Expr *E) { |
| 3368 | if (E->isRValue() && E->getType()->isLiteralType()) { |
| 3369 | // Evaluate arrays and record types in-place, so that later initializers can |
| 3370 | // refer to earlier-initialized members of the object. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3371 | if (E->getType()->isArrayType()) { |
| 3372 | if (!EvaluateArray(E, Result, Info)) |
| 3373 | return false; |
| 3374 | } else if (E->getType()->isRecordType()) |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3375 | // FIXME: Implement evaluation of record rvalues. |
| 3376 | return false; |
| 3377 | } |
| 3378 | |
| 3379 | // For any other type, in-place evaluation is unimportant. |
| 3380 | CCValue CoreConstResult; |
| 3381 | return Evaluate(CoreConstResult, Info, E) && |
| 3382 | CheckConstantExpression(CoreConstResult, Result); |
| 3383 | } |
| 3384 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3385 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3386 | /// 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] | 3387 | /// any crazy technique (that has nothing to do with language standards) that |
| 3388 | /// 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] | 3389 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 3390 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3391 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3392 | EvalInfo Info(Ctx, Result); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3393 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3394 | CCValue Value; |
| 3395 | if (!::Evaluate(Value, Info, this)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3396 | return false; |
| 3397 | |
| 3398 | if (isGLValue()) { |
| 3399 | LValue LV; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3400 | LV.setFrom(Value); |
| 3401 | if (!HandleLValueToRValueConversion(Info, getType(), LV, Value)) |
| 3402 | return false; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3405 | // Don't produce array constants until CodeGen is taught to handle them. |
| 3406 | if (Value.isArray()) |
| 3407 | return false; |
| 3408 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3409 | // Check this core constant expression is a constant expression, and if so, |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3410 | // convert it to one. |
| 3411 | return CheckConstantExpression(Value, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3414 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 3415 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3416 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3417 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3418 | HandleConversionToBool(CCValue(Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3419 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3422 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3423 | EvalResult ExprResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3424 | if (!EvaluateAsRValue(ExprResult, Ctx) || ExprResult.HasSideEffects || |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3425 | !ExprResult.Val.isInt()) { |
| 3426 | return false; |
| 3427 | } |
| 3428 | Result = ExprResult.Val.getInt(); |
| 3429 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3432 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 3433 | EvalInfo Info(Ctx, Result); |
| 3434 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3435 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 3436 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
| 3437 | CheckLValueConstantExpression(LV, Result.Val); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3440 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 3441 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3442 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 3443 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3444 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 3445 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3446 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3447 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 3448 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3451 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3452 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3453 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 3454 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3455 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3456 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3457 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 3458 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 3459 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3460 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 3461 | bool Expr::EvalResult::isGlobalLValue() const { |
| 3462 | assert(Val.isLValue()); |
| 3463 | return IsGlobalLValue(Val.getLValueBase()); |
| 3464 | } |
| 3465 | |
| 3466 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3467 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 3468 | /// an integer constant expression. |
| 3469 | |
| 3470 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 3471 | /// comma, etc |
| 3472 | /// |
| 3473 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 3474 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 3475 | /// cast+dereference. |
| 3476 | |
| 3477 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 3478 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 3479 | // Note that to reduce code duplication, this helper does no evaluation |
| 3480 | // itself; the caller checks whether the expression is evaluatable, and |
| 3481 | // in the rare cases where CheckICE actually cares about the evaluated |
| 3482 | // value, it calls into Evalute. |
| 3483 | // |
| 3484 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3485 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3486 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 3487 | // a legal subexpression for an ICE. This return value is used to handle |
| 3488 | // the comma operator in C99 mode. |
| 3489 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 3490 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3491 | namespace { |
| 3492 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3493 | struct ICEDiag { |
| 3494 | unsigned Val; |
| 3495 | SourceLocation Loc; |
| 3496 | |
| 3497 | public: |
| 3498 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 3499 | ICEDiag() : Val(0) {} |
| 3500 | }; |
| 3501 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3505 | |
| 3506 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 3507 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3508 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3509 | !EVResult.Val.isInt()) { |
| 3510 | return ICEDiag(2, E->getLocStart()); |
| 3511 | } |
| 3512 | return NoDiag(); |
| 3513 | } |
| 3514 | |
| 3515 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 3516 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3517 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3518 | return ICEDiag(2, E->getLocStart()); |
| 3519 | } |
| 3520 | |
| 3521 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3522 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3523 | #define STMT(Node, Base) case Expr::Node##Class: |
| 3524 | #define EXPR(Node, Base) |
| 3525 | #include "clang/AST/StmtNodes.inc" |
| 3526 | case Expr::PredefinedExprClass: |
| 3527 | case Expr::FloatingLiteralClass: |
| 3528 | case Expr::ImaginaryLiteralClass: |
| 3529 | case Expr::StringLiteralClass: |
| 3530 | case Expr::ArraySubscriptExprClass: |
| 3531 | case Expr::MemberExprClass: |
| 3532 | case Expr::CompoundAssignOperatorClass: |
| 3533 | case Expr::CompoundLiteralExprClass: |
| 3534 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3535 | case Expr::DesignatedInitExprClass: |
| 3536 | case Expr::ImplicitValueInitExprClass: |
| 3537 | case Expr::ParenListExprClass: |
| 3538 | case Expr::VAArgExprClass: |
| 3539 | case Expr::AddrLabelExprClass: |
| 3540 | case Expr::StmtExprClass: |
| 3541 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3542 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3543 | case Expr::CXXDynamicCastExprClass: |
| 3544 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 3545 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3546 | case Expr::CXXNullPtrLiteralExprClass: |
| 3547 | case Expr::CXXThisExprClass: |
| 3548 | case Expr::CXXThrowExprClass: |
| 3549 | case Expr::CXXNewExprClass: |
| 3550 | case Expr::CXXDeleteExprClass: |
| 3551 | case Expr::CXXPseudoDestructorExprClass: |
| 3552 | case Expr::UnresolvedLookupExprClass: |
| 3553 | case Expr::DependentScopeDeclRefExprClass: |
| 3554 | case Expr::CXXConstructExprClass: |
| 3555 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3556 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3557 | case Expr::CXXTemporaryObjectExprClass: |
| 3558 | case Expr::CXXUnresolvedConstructExprClass: |
| 3559 | case Expr::CXXDependentScopeMemberExprClass: |
| 3560 | case Expr::UnresolvedMemberExprClass: |
| 3561 | case Expr::ObjCStringLiteralClass: |
| 3562 | case Expr::ObjCEncodeExprClass: |
| 3563 | case Expr::ObjCMessageExprClass: |
| 3564 | case Expr::ObjCSelectorExprClass: |
| 3565 | case Expr::ObjCProtocolExprClass: |
| 3566 | case Expr::ObjCIvarRefExprClass: |
| 3567 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3568 | case Expr::ObjCIsaExprClass: |
| 3569 | case Expr::ShuffleVectorExprClass: |
| 3570 | case Expr::BlockExprClass: |
| 3571 | case Expr::BlockDeclRefExprClass: |
| 3572 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 3573 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 3574 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 3575 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3576 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3577 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3578 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3579 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 3580 | case Expr::AtomicExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3581 | return ICEDiag(2, E->getLocStart()); |
| 3582 | |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 3583 | case Expr::InitListExprClass: |
| 3584 | if (Ctx.getLangOptions().CPlusPlus0x) { |
| 3585 | const InitListExpr *ILE = cast<InitListExpr>(E); |
| 3586 | if (ILE->getNumInits() == 0) |
| 3587 | return NoDiag(); |
| 3588 | if (ILE->getNumInits() == 1) |
| 3589 | return CheckICE(ILE->getInit(0), Ctx); |
| 3590 | // Fall through for more than 1 expression. |
| 3591 | } |
| 3592 | return ICEDiag(2, E->getLocStart()); |
| 3593 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3594 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3595 | case Expr::GNUNullExprClass: |
| 3596 | // GCC considers the GNU __null value to be an integral constant expression. |
| 3597 | return NoDiag(); |
| 3598 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3599 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 3600 | return |
| 3601 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 3602 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3603 | case Expr::ParenExprClass: |
| 3604 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3605 | case Expr::GenericSelectionExprClass: |
| 3606 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3607 | case Expr::IntegerLiteralClass: |
| 3608 | case Expr::CharacterLiteralClass: |
| 3609 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3610 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3611 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3612 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3613 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3614 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 3615 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3616 | return NoDiag(); |
| 3617 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 3618 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 3619 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 3620 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 3621 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3622 | const CallExpr *CE = cast<CallExpr>(E); |
| 3623 | if (CE->isBuiltinCall(Ctx)) |
| 3624 | return CheckEvalInICE(E, Ctx); |
| 3625 | return ICEDiag(2, E->getLocStart()); |
| 3626 | } |
| 3627 | case Expr::DeclRefExprClass: |
| 3628 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 3629 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 3630 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3631 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 3632 | |
| 3633 | // Parameter variables are never constants. Without this check, |
| 3634 | // getAnyInitializer() can find a default argument, which leads |
| 3635 | // to chaos. |
| 3636 | if (isa<ParmVarDecl>(D)) |
| 3637 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 3638 | |
| 3639 | // C++ 7.1.5.1p2 |
| 3640 | // A variable of non-volatile const-qualified integral or enumeration |
| 3641 | // type initialized by an ICE can be used in ICEs. |
| 3642 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3643 | // Look for a declaration of this variable that has an initializer. |
| 3644 | const VarDecl *ID = 0; |
| 3645 | const Expr *Init = Dcl->getAnyInitializer(ID); |
| 3646 | if (Init) { |
| 3647 | if (ID->isInitKnownICE()) { |
| 3648 | // We have already checked whether this subexpression is an |
| 3649 | // integral constant expression. |
| 3650 | if (ID->isInitICE()) |
| 3651 | return NoDiag(); |
| 3652 | else |
| 3653 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 3654 | } |
| 3655 | |
| 3656 | // It's an ICE whether or not the definition we found is |
| 3657 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 3658 | // 6206 for details. |
| 3659 | |
| 3660 | if (Dcl->isCheckingICE()) { |
| 3661 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 3662 | } |
| 3663 | |
| 3664 | Dcl->setCheckingICE(); |
| 3665 | ICEDiag Result = CheckICE(Init, Ctx); |
| 3666 | // Cache the result of the ICE test. |
| 3667 | Dcl->setInitKnownICE(Result.Val == 0); |
| 3668 | return Result; |
| 3669 | } |
| 3670 | } |
| 3671 | } |
| 3672 | return ICEDiag(2, E->getLocStart()); |
| 3673 | case Expr::UnaryOperatorClass: { |
| 3674 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 3675 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3676 | case UO_PostInc: |
| 3677 | case UO_PostDec: |
| 3678 | case UO_PreInc: |
| 3679 | case UO_PreDec: |
| 3680 | case UO_AddrOf: |
| 3681 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 3682 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 3683 | // subexpressions of constant expressions, but they can never be ICEs |
| 3684 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3685 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3686 | case UO_Extension: |
| 3687 | case UO_LNot: |
| 3688 | case UO_Plus: |
| 3689 | case UO_Minus: |
| 3690 | case UO_Not: |
| 3691 | case UO_Real: |
| 3692 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3693 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | // OffsetOf falls through here. |
| 3697 | } |
| 3698 | case Expr::OffsetOfExprClass: { |
| 3699 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3700 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 3701 | // "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] | 3702 | // compliance: we should warn earlier for offsetof expressions with |
| 3703 | // array subscripts that aren't ICEs, and if the array subscripts |
| 3704 | // are ICEs, the value of the offsetof must be an integer constant. |
| 3705 | return CheckEvalInICE(E, Ctx); |
| 3706 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3707 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 3708 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 3709 | if ((Exp->getKind() == UETT_SizeOf) && |
| 3710 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3711 | return ICEDiag(2, E->getLocStart()); |
| 3712 | return NoDiag(); |
| 3713 | } |
| 3714 | case Expr::BinaryOperatorClass: { |
| 3715 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 3716 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3717 | case BO_PtrMemD: |
| 3718 | case BO_PtrMemI: |
| 3719 | case BO_Assign: |
| 3720 | case BO_MulAssign: |
| 3721 | case BO_DivAssign: |
| 3722 | case BO_RemAssign: |
| 3723 | case BO_AddAssign: |
| 3724 | case BO_SubAssign: |
| 3725 | case BO_ShlAssign: |
| 3726 | case BO_ShrAssign: |
| 3727 | case BO_AndAssign: |
| 3728 | case BO_XorAssign: |
| 3729 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 3730 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 3731 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 3732 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3733 | return ICEDiag(2, E->getLocStart()); |
| 3734 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3735 | case BO_Mul: |
| 3736 | case BO_Div: |
| 3737 | case BO_Rem: |
| 3738 | case BO_Add: |
| 3739 | case BO_Sub: |
| 3740 | case BO_Shl: |
| 3741 | case BO_Shr: |
| 3742 | case BO_LT: |
| 3743 | case BO_GT: |
| 3744 | case BO_LE: |
| 3745 | case BO_GE: |
| 3746 | case BO_EQ: |
| 3747 | case BO_NE: |
| 3748 | case BO_And: |
| 3749 | case BO_Xor: |
| 3750 | case BO_Or: |
| 3751 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3752 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 3753 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3754 | if (Exp->getOpcode() == BO_Div || |
| 3755 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3756 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3757 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 3758 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3759 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3760 | if (REval == 0) |
| 3761 | return ICEDiag(1, E->getLocStart()); |
| 3762 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3763 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3764 | if (LEval.isMinSignedValue()) |
| 3765 | return ICEDiag(1, E->getLocStart()); |
| 3766 | } |
| 3767 | } |
| 3768 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3769 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3770 | if (Ctx.getLangOptions().C99) { |
| 3771 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 3772 | // if it isn't evaluated. |
| 3773 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 3774 | return ICEDiag(1, E->getLocStart()); |
| 3775 | } else { |
| 3776 | // In both C89 and C++, commas in ICEs are illegal. |
| 3777 | return ICEDiag(2, E->getLocStart()); |
| 3778 | } |
| 3779 | } |
| 3780 | if (LHSResult.Val >= RHSResult.Val) |
| 3781 | return LHSResult; |
| 3782 | return RHSResult; |
| 3783 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3784 | case BO_LAnd: |
| 3785 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3786 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 3787 | |
| 3788 | // C++0x [expr.const]p2: |
| 3789 | // [...] subexpressions of logical AND (5.14), logical OR |
| 3790 | // (5.15), and condi- tional (5.16) operations that are not |
| 3791 | // evaluated are not considered. |
| 3792 | if (Ctx.getLangOptions().CPlusPlus0x && LHSResult.Val == 0) { |
| 3793 | if (Exp->getOpcode() == BO_LAnd && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3794 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 3795 | return LHSResult; |
| 3796 | |
| 3797 | if (Exp->getOpcode() == BO_LOr && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3798 | Exp->getLHS()->EvaluateKnownConstInt(Ctx) != 0) |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 3799 | return LHSResult; |
| 3800 | } |
| 3801 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3802 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 3803 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 3804 | // Rare case where the RHS has a comma "side-effect"; we need |
| 3805 | // to actually check the condition to see whether the side |
| 3806 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3807 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3808 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3809 | return RHSResult; |
| 3810 | return NoDiag(); |
| 3811 | } |
| 3812 | |
| 3813 | if (LHSResult.Val >= RHSResult.Val) |
| 3814 | return LHSResult; |
| 3815 | return RHSResult; |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | case Expr::ImplicitCastExprClass: |
| 3820 | case Expr::CStyleCastExprClass: |
| 3821 | case Expr::CXXFunctionalCastExprClass: |
| 3822 | case Expr::CXXStaticCastExprClass: |
| 3823 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 3824 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3825 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3826 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 98326ed | 2011-10-25 00:21:54 +0000 | [diff] [blame] | 3827 | if (isa<ExplicitCastExpr>(E) && |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 3828 | isa<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) |
| 3829 | return NoDiag(); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 3830 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 3831 | case CK_LValueToRValue: |
| 3832 | case CK_NoOp: |
| 3833 | case CK_IntegralToBoolean: |
| 3834 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3835 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 3836 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 3837 | return ICEDiag(2, E->getLocStart()); |
| 3838 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3839 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3840 | case Expr::BinaryConditionalOperatorClass: { |
| 3841 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 3842 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 3843 | if (CommonResult.Val == 2) return CommonResult; |
| 3844 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 3845 | if (FalseResult.Val == 2) return FalseResult; |
| 3846 | if (CommonResult.Val == 1) return CommonResult; |
| 3847 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3848 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3849 | return FalseResult; |
| 3850 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3851 | case Expr::ConditionalOperatorClass: { |
| 3852 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 3853 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 3854 | // then only the true side is actually considered in an integer constant |
| 3855 | // expression, and it is fully evaluated. This is an important GNU |
| 3856 | // extension. See GCC PR38377 for discussion. |
| 3857 | if (const CallExpr *CallCE |
| 3858 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
| 3859 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
| 3860 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 3861 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3862 | !EVResult.Val.isInt()) { |
| 3863 | return ICEDiag(2, E->getLocStart()); |
| 3864 | } |
| 3865 | return NoDiag(); |
| 3866 | } |
| 3867 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3868 | if (CondResult.Val == 2) |
| 3869 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 3870 | |
| 3871 | // C++0x [expr.const]p2: |
| 3872 | // subexpressions of [...] conditional (5.16) operations that |
| 3873 | // are not evaluated are not considered |
| 3874 | bool TrueBranch = Ctx.getLangOptions().CPlusPlus0x |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3875 | ? Exp->getCond()->EvaluateKnownConstInt(Ctx) != 0 |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 3876 | : false; |
| 3877 | ICEDiag TrueResult = NoDiag(); |
| 3878 | if (!Ctx.getLangOptions().CPlusPlus0x || TrueBranch) |
| 3879 | TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 3880 | ICEDiag FalseResult = NoDiag(); |
| 3881 | if (!Ctx.getLangOptions().CPlusPlus0x || !TrueBranch) |
| 3882 | FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 3883 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3884 | if (TrueResult.Val == 2) |
| 3885 | return TrueResult; |
| 3886 | if (FalseResult.Val == 2) |
| 3887 | return FalseResult; |
| 3888 | if (CondResult.Val == 1) |
| 3889 | return CondResult; |
| 3890 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 3891 | return NoDiag(); |
| 3892 | // Rare case where the diagnostics depend on which side is evaluated |
| 3893 | // Note that if we get here, CondResult is 0, and at least one of |
| 3894 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3895 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3896 | return FalseResult; |
| 3897 | } |
| 3898 | return TrueResult; |
| 3899 | } |
| 3900 | case Expr::CXXDefaultArgExprClass: |
| 3901 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 3902 | case Expr::ChooseExprClass: { |
| 3903 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | // Silence a GCC warning |
| 3908 | return ICEDiag(2, E->getLocStart()); |
| 3909 | } |
| 3910 | |
| 3911 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 3912 | SourceLocation *Loc, bool isEvaluated) const { |
| 3913 | ICEDiag d = CheckICE(this, Ctx); |
| 3914 | if (d.Val != 0) { |
| 3915 | if (Loc) *Loc = d.Loc; |
| 3916 | return false; |
| 3917 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3918 | if (!EvaluateAsInt(Result, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3919 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 3920 | return true; |
| 3921 | } |