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