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