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