Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/APValue.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTDiagnostic.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 22 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 23 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 25 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 26 | /// information about a subexpression as it is folded. It retains information |
| 27 | /// about the AST context, but also maintains information about the folded |
| 28 | /// expression. |
| 29 | /// |
| 30 | /// If an expression could be evaluated, it is still possible it is not a C |
| 31 | /// "integer constant expression" or constant expression. If not, this struct |
| 32 | /// captures information about how and why not. |
| 33 | /// |
| 34 | /// One bit of information passed *into* the request for constant folding |
| 35 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 36 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 37 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 38 | /// certain things in certain situations. |
| 39 | struct EvalInfo { |
| 40 | ASTContext &Ctx; |
| 41 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 42 | /// EvalResult - Contains information about the evaluation. |
| 43 | Expr::EvalResult &EvalResult; |
Anders Carlsson | f0c1e4b | 2008-11-30 18:26:25 +0000 | [diff] [blame] | 44 | |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 45 | EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx), |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 46 | EvalResult(evalresult) {} |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 50 | static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 51 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 52 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 53 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 54 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 55 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 56 | |
| 57 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 58 | // Misc utilities |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) { |
| 62 | if (E->getType()->isIntegralType()) { |
| 63 | APSInt IntResult; |
| 64 | if (!EvaluateInteger(E, IntResult, Info)) |
| 65 | return false; |
| 66 | Result = IntResult != 0; |
| 67 | return true; |
| 68 | } else if (E->getType()->isRealFloatingType()) { |
| 69 | APFloat FloatResult(0.0); |
| 70 | if (!EvaluateFloat(E, FloatResult, Info)) |
| 71 | return false; |
| 72 | Result = !FloatResult.isZero(); |
| 73 | return true; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 74 | } else if (E->getType()->hasPointerRepresentation()) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 75 | APValue PointerResult; |
| 76 | if (!EvaluatePointer(E, PointerResult, Info)) |
| 77 | return false; |
| 78 | // FIXME: Is this accurate for all kinds of bases? If not, what would |
| 79 | // the check look like? |
| 80 | Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset(); |
| 81 | return true; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 82 | } else if (E->getType()->isAnyComplexType()) { |
| 83 | APValue ComplexResult; |
| 84 | if (!EvaluateComplex(E, ComplexResult, Info)) |
| 85 | return false; |
| 86 | if (ComplexResult.isComplexFloat()) { |
| 87 | Result = !ComplexResult.getComplexFloatReal().isZero() || |
| 88 | !ComplexResult.getComplexFloatImag().isZero(); |
| 89 | } else { |
| 90 | Result = ComplexResult.getComplexIntReal().getBoolValue() || |
| 91 | ComplexResult.getComplexIntImag().getBoolValue(); |
| 92 | } |
| 93 | return true; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 99 | static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType, |
| 100 | APFloat &Value, ASTContext &Ctx) { |
| 101 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 102 | // Determine whether we are converting to unsigned or signed. |
| 103 | bool DestSigned = DestType->isSignedIntegerType(); |
| 104 | |
| 105 | // FIXME: Warning for overflow. |
| 106 | uint64_t Space[4]; |
| 107 | bool ignored; |
| 108 | (void)Value.convertToInteger(Space, DestWidth, DestSigned, |
| 109 | llvm::APFloat::rmTowardZero, &ignored); |
| 110 | return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned); |
| 111 | } |
| 112 | |
| 113 | static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType, |
| 114 | APFloat &Value, ASTContext &Ctx) { |
| 115 | bool ignored; |
| 116 | APFloat Result = Value; |
| 117 | Result.convert(Ctx.getFloatTypeSemantics(DestType), |
| 118 | APFloat::rmNearestTiesToEven, &ignored); |
| 119 | return Result; |
| 120 | } |
| 121 | |
| 122 | static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType, |
| 123 | APSInt &Value, ASTContext &Ctx) { |
| 124 | unsigned DestWidth = Ctx.getIntWidth(DestType); |
| 125 | APSInt Result = Value; |
| 126 | // Figure out if this is a truncate, extend or noop cast. |
| 127 | // If the input is signed, do a sign extend, noop, or truncate. |
| 128 | Result.extOrTrunc(DestWidth); |
| 129 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 130 | return Result; |
| 131 | } |
| 132 | |
| 133 | static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType, |
| 134 | APSInt &Value, ASTContext &Ctx) { |
| 135 | |
| 136 | APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1); |
| 137 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 138 | APFloat::rmNearestTiesToEven); |
| 139 | return Result; |
| 140 | } |
| 141 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // LValue Evaluation |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | namespace { |
| 146 | class VISIBILITY_HIDDEN LValueExprEvaluator |
| 147 | : public StmtVisitor<LValueExprEvaluator, APValue> { |
| 148 | EvalInfo &Info; |
| 149 | public: |
| 150 | |
| 151 | LValueExprEvaluator(EvalInfo &info) : Info(info) {} |
| 152 | |
| 153 | APValue VisitStmt(Stmt *S) { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 154 | return APValue(); |
| 155 | } |
| 156 | |
| 157 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 158 | APValue VisitDeclRefExpr(DeclRefExpr *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 159 | APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); } |
| 160 | APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E); |
| 161 | APValue VisitMemberExpr(MemberExpr *E); |
| 162 | APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); } |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 163 | APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 164 | APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 165 | APValue VisitUnaryDeref(UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 166 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 167 | { return Visit(E->getSubExpr()); } |
| 168 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 169 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 170 | // FIXME: Missing: __real__, __imag__ |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 171 | }; |
| 172 | } // end anonymous namespace |
| 173 | |
| 174 | static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 175 | Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 176 | return Result.isLValue(); |
| 177 | } |
| 178 | |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 179 | APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) |
| 180 | { |
| 181 | if (!E->hasGlobalStorage()) |
| 182 | return APValue(); |
| 183 | |
| 184 | return APValue(E, 0); |
| 185 | } |
| 186 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 187 | APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 188 | if (E->isFileScope()) |
| 189 | return APValue(E, 0); |
| 190 | return APValue(); |
| 191 | } |
| 192 | |
| 193 | APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) { |
| 194 | APValue result; |
| 195 | QualType Ty; |
| 196 | if (E->isArrow()) { |
| 197 | if (!EvaluatePointer(E->getBase(), result, Info)) |
| 198 | return APValue(); |
| 199 | Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType(); |
| 200 | } else { |
| 201 | result = Visit(E->getBase()); |
| 202 | if (result.isUninit()) |
| 203 | return APValue(); |
| 204 | Ty = E->getBase()->getType(); |
| 205 | } |
| 206 | |
| 207 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
| 208 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 209 | |
| 210 | FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| 211 | if (!FD) // FIXME: deal with other kinds of member expressions |
| 212 | return APValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 213 | |
| 214 | // FIXME: This is linear time. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 215 | unsigned i = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 216 | for (RecordDecl::field_iterator Field = RD->field_begin(Info.Ctx), |
| 217 | FieldEnd = RD->field_end(Info.Ctx); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 218 | Field != FieldEnd; (void)++Field, ++i) { |
| 219 | if (*Field == FD) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | |
| 223 | result.setLValue(result.getLValueBase(), |
| 224 | result.getLValueOffset() + RL.getFieldOffset(i) / 8); |
| 225 | |
| 226 | return result; |
| 227 | } |
| 228 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 229 | APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) |
| 230 | { |
| 231 | APValue Result; |
| 232 | |
| 233 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
| 234 | return APValue(); |
| 235 | |
| 236 | APSInt Index; |
| 237 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| 238 | return APValue(); |
| 239 | |
| 240 | uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8; |
| 241 | |
| 242 | uint64_t Offset = Index.getSExtValue() * ElementSize; |
| 243 | Result.setLValue(Result.getLValueBase(), |
| 244 | Result.getLValueOffset() + Offset); |
| 245 | return Result; |
| 246 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 247 | |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 248 | APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) |
| 249 | { |
| 250 | APValue Result; |
| 251 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
| 252 | return APValue(); |
| 253 | return Result; |
| 254 | } |
| 255 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 256 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 257 | // Pointer Evaluation |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 260 | namespace { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 261 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 262 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 263 | EvalInfo &Info; |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 264 | public: |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 266 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 267 | |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 268 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 269 | return APValue(); |
| 270 | } |
| 271 | |
| 272 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 273 | |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 274 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 275 | APValue VisitCastExpr(const CastExpr* E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 276 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 277 | { return Visit(E->getSubExpr()); } |
| 278 | APValue VisitUnaryAddrOf(const UnaryOperator *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 279 | APValue VisitObjCStringLiteral(ObjCStringLiteral *E) |
| 280 | { return APValue(E, 0); } |
Eli Friedman | f011589 | 2009-01-25 01:21:06 +0000 | [diff] [blame] | 281 | APValue VisitAddrLabelExpr(AddrLabelExpr *E) |
| 282 | { return APValue(E, 0); } |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 283 | APValue VisitCallExpr(CallExpr *E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 284 | APValue VisitBlockExpr(BlockExpr *E) { |
| 285 | if (!E->hasBlockDeclRefExprs()) |
| 286 | return APValue(E, 0); |
| 287 | return APValue(); |
| 288 | } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 289 | APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) |
| 290 | { return APValue((Expr*)0, 0); } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 291 | APValue VisitConditionalOperator(ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 292 | APValue VisitChooseExpr(ChooseExpr *E) |
| 293 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 294 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 295 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 296 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 298 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 299 | if (!E->getType()->hasPointerRepresentation()) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 300 | return false; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 301 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 302 | return Result.isLValue(); |
| 303 | } |
| 304 | |
| 305 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 306 | if (E->getOpcode() != BinaryOperator::Add && |
| 307 | E->getOpcode() != BinaryOperator::Sub) |
| 308 | return APValue(); |
| 309 | |
| 310 | const Expr *PExp = E->getLHS(); |
| 311 | const Expr *IExp = E->getRHS(); |
| 312 | if (IExp->getType()->isPointerType()) |
| 313 | std::swap(PExp, IExp); |
| 314 | |
| 315 | APValue ResultLValue; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 316 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 317 | return APValue(); |
| 318 | |
| 319 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 320 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 321 | return APValue(); |
| 322 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 323 | QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType(); |
Anders Carlsson | 4d4c50d | 2009-02-19 04:55:58 +0000 | [diff] [blame] | 324 | uint64_t SizeOfPointee; |
| 325 | |
| 326 | // Explicitly handle GNU void* and function pointer arithmetic extensions. |
| 327 | if (PointeeType->isVoidType() || PointeeType->isFunctionType()) |
| 328 | SizeOfPointee = 1; |
| 329 | else |
| 330 | SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 331 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 332 | uint64_t Offset = ResultLValue.getLValueOffset(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 333 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 334 | if (E->getOpcode() == BinaryOperator::Add) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 335 | Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 336 | else |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 337 | Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee; |
| 338 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 339 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 340 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 341 | |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 342 | APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 343 | APValue result; |
| 344 | if (EvaluateLValue(E->getSubExpr(), result, Info)) |
| 345 | return result; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 346 | return APValue(); |
| 347 | } |
Anders Carlsson | d407a76 | 2008-12-05 05:24:13 +0000 | [diff] [blame] | 348 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 349 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 350 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 351 | const Expr* SubExpr = E->getSubExpr(); |
| 352 | |
| 353 | // Check for pointer->pointer cast |
| 354 | if (SubExpr->getType()->isPointerType()) { |
| 355 | APValue Result; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 356 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 357 | return Result; |
| 358 | return APValue(); |
| 359 | } |
| 360 | |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 361 | if (SubExpr->getType()->isIntegralType()) { |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 362 | APValue Result; |
| 363 | if (!EvaluateIntegerOrLValue(SubExpr, Result, Info)) |
| 364 | return APValue(); |
| 365 | |
| 366 | if (Result.isInt()) { |
| 367 | Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
| 368 | return APValue(0, Result.getInt().getZExtValue()); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 369 | } |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 370 | |
| 371 | // Cast is of an lvalue, no need to change value. |
| 372 | return Result; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 373 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 374 | |
| 375 | if (SubExpr->getType()->isFunctionType() || |
| 376 | SubExpr->getType()->isArrayType()) { |
| 377 | APValue Result; |
| 378 | if (EvaluateLValue(SubExpr, Result, Info)) |
| 379 | return Result; |
| 380 | return APValue(); |
| 381 | } |
| 382 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 383 | return APValue(); |
| 384 | } |
| 385 | |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 386 | APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 387 | if (E->isBuiltinCall(Info.Ctx) == |
| 388 | Builtin::BI__builtin___CFStringMakeConstantString) |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 389 | return APValue(E, 0); |
| 390 | return APValue(); |
| 391 | } |
| 392 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 393 | APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) { |
| 394 | bool BoolResult; |
| 395 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 396 | return APValue(); |
| 397 | |
| 398 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 399 | |
| 400 | APValue Result; |
| 401 | if (EvaluatePointer(EvalExpr, Result, Info)) |
| 402 | return Result; |
| 403 | return APValue(); |
| 404 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 405 | |
| 406 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 407 | // Vector Evaluation |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | |
| 410 | namespace { |
| 411 | class VISIBILITY_HIDDEN VectorExprEvaluator |
| 412 | : public StmtVisitor<VectorExprEvaluator, APValue> { |
| 413 | EvalInfo &Info; |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 414 | APValue GetZeroVector(QualType VecType); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 415 | public: |
| 416 | |
| 417 | VectorExprEvaluator(EvalInfo &info) : Info(info) {} |
| 418 | |
| 419 | APValue VisitStmt(Stmt *S) { |
| 420 | return APValue(); |
| 421 | } |
| 422 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 423 | APValue VisitParenExpr(ParenExpr *E) |
| 424 | { return Visit(E->getSubExpr()); } |
| 425 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 426 | { return Visit(E->getSubExpr()); } |
| 427 | APValue VisitUnaryPlus(const UnaryOperator *E) |
| 428 | { return Visit(E->getSubExpr()); } |
| 429 | APValue VisitUnaryReal(const UnaryOperator *E) |
| 430 | { return Visit(E->getSubExpr()); } |
| 431 | APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) |
| 432 | { return GetZeroVector(E->getType()); } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 433 | APValue VisitCastExpr(const CastExpr* E); |
| 434 | APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 435 | APValue VisitInitListExpr(const InitListExpr *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 436 | APValue VisitConditionalOperator(const ConditionalOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 437 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 438 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 439 | APValue VisitUnaryImag(const UnaryOperator *E); |
| 440 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 441 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 442 | // shufflevector, ExtVectorElementExpr |
| 443 | // (Note that these require implementing conversions |
| 444 | // between vector types.) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 445 | }; |
| 446 | } // end anonymous namespace |
| 447 | |
| 448 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| 449 | if (!E->getType()->isVectorType()) |
| 450 | return false; |
| 451 | Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 452 | return !Result.isUninit(); |
| 453 | } |
| 454 | |
| 455 | APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 456 | const Expr* SE = E->getSubExpr(); |
| 457 | |
| 458 | // Check for vector->vector bitcast. |
| 459 | if (SE->getType()->isVectorType()) |
| 460 | return this->Visit(const_cast<Expr*>(SE)); |
| 461 | |
| 462 | return APValue(); |
| 463 | } |
| 464 | |
| 465 | APValue |
| 466 | VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 467 | return this->Visit(const_cast<Expr*>(E->getInitializer())); |
| 468 | } |
| 469 | |
| 470 | APValue |
| 471 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 472 | const VectorType *VT = E->getType()->getAsVectorType(); |
| 473 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 474 | unsigned NumElements = VT->getNumElements(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 475 | |
| 476 | QualType EltTy = VT->getElementType(); |
| 477 | llvm::SmallVector<APValue, 4> Elements; |
| 478 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 479 | for (unsigned i = 0; i < NumElements; i++) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 480 | if (EltTy->isIntegerType()) { |
| 481 | llvm::APSInt sInt(32); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 482 | if (i < NumInits) { |
| 483 | if (!EvaluateInteger(E->getInit(i), sInt, Info)) |
| 484 | return APValue(); |
| 485 | } else { |
| 486 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 487 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 488 | Elements.push_back(APValue(sInt)); |
| 489 | } else { |
| 490 | llvm::APFloat f(0.0); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 491 | if (i < NumInits) { |
| 492 | if (!EvaluateFloat(E->getInit(i), f, Info)) |
| 493 | return APValue(); |
| 494 | } else { |
| 495 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 496 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 497 | Elements.push_back(APValue(f)); |
| 498 | } |
| 499 | } |
| 500 | return APValue(&Elements[0], Elements.size()); |
| 501 | } |
| 502 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 503 | APValue |
| 504 | VectorExprEvaluator::GetZeroVector(QualType T) { |
| 505 | const VectorType *VT = T->getAsVectorType(); |
| 506 | QualType EltTy = VT->getElementType(); |
| 507 | APValue ZeroElement; |
| 508 | if (EltTy->isIntegerType()) |
| 509 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 510 | else |
| 511 | ZeroElement = |
| 512 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 513 | |
| 514 | llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| 515 | return APValue(&Elements[0], Elements.size()); |
| 516 | } |
| 517 | |
| 518 | APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
| 519 | bool BoolResult; |
| 520 | if (!HandleConversionToBool(E->getCond(), BoolResult, Info)) |
| 521 | return APValue(); |
| 522 | |
| 523 | Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 524 | |
| 525 | APValue Result; |
| 526 | if (EvaluateVector(EvalExpr, Result, Info)) |
| 527 | return Result; |
| 528 | return APValue(); |
| 529 | } |
| 530 | |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 531 | APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 532 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 533 | Info.EvalResult.HasSideEffects = true; |
| 534 | return GetZeroVector(E->getType()); |
| 535 | } |
| 536 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 537 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 538 | // Integer Evaluation |
| 539 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 540 | |
| 541 | namespace { |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 542 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 543 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 544 | EvalInfo &Info; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 545 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 546 | public: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 547 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 548 | : Info(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 550 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 551 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 552 | assert(SI.isSigned() == E->getType()->isSignedIntegerType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 553 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 554 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 555 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 556 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 560 | bool Success(const llvm::APInt &I, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 561 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 562 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 563 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 564 | Result = APValue(APSInt(I)); |
| 565 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 566 | return true; |
| 567 | } |
| 568 | |
| 569 | bool Success(uint64_t Value, const Expr *E) { |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 570 | assert(E->getType()->isIntegralType() && "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 571 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 575 | bool Error(SourceLocation L, diag::kind D, const Expr *E) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 576 | // Take the first error. |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 577 | if (Info.EvalResult.Diag == 0) { |
| 578 | Info.EvalResult.DiagLoc = L; |
| 579 | Info.EvalResult.Diag = D; |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 580 | Info.EvalResult.DiagExpr = E; |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 581 | } |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 582 | return false; |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 585 | //===--------------------------------------------------------------------===// |
| 586 | // Visitor Methods |
| 587 | //===--------------------------------------------------------------------===// |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 588 | |
| 589 | bool VisitStmt(Stmt *) { |
| 590 | assert(0 && "This should be called on integers, stmts are not integers"); |
| 591 | return false; |
| 592 | } |
Chris Lattner | 7a76778 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 594 | bool VisitExpr(Expr *E) { |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 595 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 598 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 600 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 601 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 602 | } |
| 603 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 604 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 605 | } |
| 606 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 607 | // Per gcc docs "this built-in function ignores top level |
| 608 | // qualifiers". We need to use the canonical version to properly |
| 609 | // be able to strip CRV qualifiers from the type. |
| 610 | QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1()); |
| 611 | QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 612 | return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 613 | T1.getUnqualifiedType()), |
| 614 | E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 615 | } |
| 616 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 617 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 618 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 619 | bool VisitUnaryOperator(const UnaryOperator *E); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 620 | bool VisitConditionalOperator(const ConditionalOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 621 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 622 | bool VisitCastExpr(CastExpr* E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 623 | bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
| 624 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 625 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 626 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 629 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 630 | return Success(0, E); |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 633 | bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 634 | return Success(0, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 637 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| 638 | return Success(0, E); |
| 639 | } |
| 640 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 641 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 642 | return Success(E->EvaluateTrait(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 645 | bool VisitChooseExpr(const ChooseExpr *E) { |
| 646 | return Visit(E->getChosenSubExpr(Info.Ctx)); |
| 647 | } |
| 648 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 649 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 650 | bool VisitUnaryImag(const UnaryOperator *E); |
| 651 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 652 | private: |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 653 | unsigned GetAlignOfExpr(const Expr *E); |
| 654 | unsigned GetAlignOfType(QualType T); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 655 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 656 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 657 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 658 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 659 | static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) { |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 660 | if (!E->getType()->isIntegralType()) |
| 661 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 663 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 664 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 665 | |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 666 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 667 | APValue Val; |
| 668 | if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt()) |
| 669 | return false; |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 670 | Result = Val.getInt(); |
| 671 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 672 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 674 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 675 | // Enums are integer constant exprs. |
| 676 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
Eli Friedman | e9a0f43 | 2008-12-08 02:21:03 +0000 | [diff] [blame] | 677 | // FIXME: This is an ugly hack around the fact that enums don't set their |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 678 | // signedness consistently; see PR3173. |
| 679 | APSInt SI = D->getInitVal(); |
| 680 | SI.setIsUnsigned(!E->getType()->isSignedIntegerType()); |
| 681 | // FIXME: This is an ugly hack around the fact that enums don't |
| 682 | // set their width (!?!) consistently; see PR3173. |
| 683 | SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 684 | return Success(SI, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 685 | } |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 686 | |
| 687 | // In C++, const, non-volatile integers initialized with ICEs are ICEs. |
Eli Friedman | e1646da | 2009-03-30 23:39:01 +0000 | [diff] [blame] | 688 | // In C, they can also be folded, although they are not ICEs. |
| 689 | if (E->getType().getCVRQualifiers() == QualType::Const) { |
Sebastian Redl | b2bc62b | 2009-02-08 15:51:17 +0000 | [diff] [blame] | 690 | if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) { |
| 691 | if (const Expr *Init = D->getInit()) |
| 692 | return Visit(const_cast<Expr*>(Init)); |
| 693 | } |
| 694 | } |
| 695 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 696 | // Otherwise, random variable references are not constants. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 697 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 700 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 701 | /// as GCC. |
| 702 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 703 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 704 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 705 | enum gcc_type_class { |
| 706 | no_type_class = -1, |
| 707 | void_type_class, integer_type_class, char_type_class, |
| 708 | enumeral_type_class, boolean_type_class, |
| 709 | pointer_type_class, reference_type_class, offset_type_class, |
| 710 | real_type_class, complex_type_class, |
| 711 | function_type_class, method_type_class, |
| 712 | record_type_class, union_type_class, |
| 713 | array_type_class, string_type_class, |
| 714 | lang_type_class |
| 715 | }; |
| 716 | |
| 717 | // If no argument was supplied, default to "no_type_class". This isn't |
| 718 | // ideal, however it is what gcc does. |
| 719 | if (E->getNumArgs() == 0) |
| 720 | return no_type_class; |
| 721 | |
| 722 | QualType ArgTy = E->getArg(0)->getType(); |
| 723 | if (ArgTy->isVoidType()) |
| 724 | return void_type_class; |
| 725 | else if (ArgTy->isEnumeralType()) |
| 726 | return enumeral_type_class; |
| 727 | else if (ArgTy->isBooleanType()) |
| 728 | return boolean_type_class; |
| 729 | else if (ArgTy->isCharType()) |
| 730 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 731 | else if (ArgTy->isIntegerType()) |
| 732 | return integer_type_class; |
| 733 | else if (ArgTy->isPointerType()) |
| 734 | return pointer_type_class; |
| 735 | else if (ArgTy->isReferenceType()) |
| 736 | return reference_type_class; |
| 737 | else if (ArgTy->isRealType()) |
| 738 | return real_type_class; |
| 739 | else if (ArgTy->isComplexType()) |
| 740 | return complex_type_class; |
| 741 | else if (ArgTy->isFunctionType()) |
| 742 | return function_type_class; |
| 743 | else if (ArgTy->isStructureType()) |
| 744 | return record_type_class; |
| 745 | else if (ArgTy->isUnionType()) |
| 746 | return union_type_class; |
| 747 | else if (ArgTy->isArrayType()) |
| 748 | return array_type_class; |
| 749 | else if (ArgTy->isUnionType()) |
| 750 | return union_type_class; |
| 751 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
| 752 | assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 753 | return -1; |
| 754 | } |
| 755 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 756 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 757 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 758 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 759 | return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 760 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 761 | return Success(EvaluateBuiltinClassifyType(E), E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 762 | |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 763 | case Builtin::BI__builtin_constant_p: |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 764 | // __builtin_constant_p always has one operand: it returns true if that |
| 765 | // operand can be folded, false otherwise. |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 766 | return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E); |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 768 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 769 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 770 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 771 | if (E->getOpcode() == BinaryOperator::Comma) { |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 772 | if (!Visit(E->getRHS())) |
| 773 | return false; |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 774 | |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 775 | // If we can't evaluate the LHS, it might have side effects; |
| 776 | // conservatively mark it. |
| 777 | if (!E->getLHS()->isEvaluatable(Info.Ctx)) |
| 778 | Info.EvalResult.HasSideEffects = true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 779 | |
Anders Carlsson | 027f62e | 2008-12-01 02:07:06 +0000 | [diff] [blame] | 780 | return true; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if (E->isLogicalOp()) { |
| 784 | // These need to be handled specially because the operands aren't |
| 785 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 786 | bool lhsResult, rhsResult; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 787 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 788 | if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 789 | // We were able to evaluate the LHS, see if we can get away with not |
| 790 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Eli Friedman | 33ef145 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 791 | if (lhsResult == (E->getOpcode() == BinaryOperator::LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 792 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 793 | |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 794 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 795 | if (E->getOpcode() == BinaryOperator::LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 796 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 797 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 798 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 799 | } |
| 800 | } else { |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 801 | if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 802 | // We can't evaluate the LHS; however, sometimes the result |
| 803 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 804 | if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) || |
| 805 | !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 806 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 807 | // must have had side effects. |
| 808 | Info.EvalResult.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 809 | |
| 810 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 813 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 814 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 818 | QualType LHSTy = E->getLHS()->getType(); |
| 819 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 820 | |
| 821 | if (LHSTy->isAnyComplexType()) { |
| 822 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
| 823 | APValue LHS, RHS; |
| 824 | |
| 825 | if (!EvaluateComplex(E->getLHS(), LHS, Info)) |
| 826 | return false; |
| 827 | |
| 828 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
| 829 | return false; |
| 830 | |
| 831 | if (LHS.isComplexFloat()) { |
| 832 | APFloat::cmpResult CR_r = |
| 833 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
| 834 | APFloat::cmpResult CR_i = |
| 835 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 836 | |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 837 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 838 | return Success((CR_r == APFloat::cmpEqual && |
| 839 | CR_i == APFloat::cmpEqual), E); |
| 840 | else { |
| 841 | assert(E->getOpcode() == BinaryOperator::NE && |
| 842 | "Invalid complex comparison."); |
| 843 | return Success(((CR_r == APFloat::cmpGreaterThan || |
| 844 | CR_r == APFloat::cmpLessThan) && |
| 845 | (CR_i == APFloat::cmpGreaterThan || |
| 846 | CR_i == APFloat::cmpLessThan)), E); |
| 847 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 848 | } else { |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 849 | if (E->getOpcode() == BinaryOperator::EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 850 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 851 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 852 | else { |
| 853 | assert(E->getOpcode() == BinaryOperator::NE && |
| 854 | "Invalid compex comparison."); |
| 855 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 856 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 857 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 860 | |
| 861 | if (LHSTy->isRealFloatingType() && |
| 862 | RHSTy->isRealFloatingType()) { |
| 863 | APFloat RHS(0.0), LHS(0.0); |
| 864 | |
| 865 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 866 | return false; |
| 867 | |
| 868 | if (!EvaluateFloat(E->getLHS(), LHS, Info)) |
| 869 | return false; |
| 870 | |
| 871 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 872 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 873 | switch (E->getOpcode()) { |
| 874 | default: |
| 875 | assert(0 && "Invalid binary operator!"); |
| 876 | case BinaryOperator::LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 877 | return Success(CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 878 | case BinaryOperator::GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 879 | return Success(CR == APFloat::cmpGreaterThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 880 | case BinaryOperator::LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 881 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 882 | case BinaryOperator::GE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 883 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
| 884 | E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 885 | case BinaryOperator::EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 886 | return Success(CR == APFloat::cmpEqual, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 887 | case BinaryOperator::NE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 888 | return Success(CR == APFloat::cmpGreaterThan |
| 889 | || CR == APFloat::cmpLessThan, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 890 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 893 | if (E->getOpcode() == BinaryOperator::Sub) { |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 894 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 895 | APValue LHSValue; |
| 896 | if (!EvaluatePointer(E->getLHS(), LHSValue, Info)) |
| 897 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 898 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 899 | APValue RHSValue; |
| 900 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info)) |
| 901 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 902 | |
| 903 | // Reject any bases; this is conservative, but good enough for |
| 904 | // common uses |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 905 | if (LHSValue.getLValueBase() || RHSValue.getLValueBase()) |
| 906 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 907 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 908 | const QualType Type = E->getLHS()->getType(); |
| 909 | const QualType ElementType = Type->getAsPointerType()->getPointeeType(); |
| 910 | |
| 911 | uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset(); |
| 912 | D /= Info.Ctx.getTypeSize(ElementType) / 8; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 913 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 914 | return Success(D, E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 917 | if (!LHSTy->isIntegralType() || |
| 918 | !RHSTy->isIntegralType()) { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 919 | // We can't continue from here for non-integral types, and they |
| 920 | // could potentially confuse the following operations. |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 921 | return false; |
| 922 | } |
| 923 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 924 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 925 | if (!Visit(E->getLHS())) |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 926 | return false; // error in subexpression. |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 927 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 928 | APValue RHSVal; |
| 929 | if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info)) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 930 | return false; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 931 | |
| 932 | // Handle cases like (unsigned long)&a + 4. |
| 933 | if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) { |
| 934 | uint64_t offset = Result.getLValueOffset(); |
| 935 | if (E->getOpcode() == BinaryOperator::Add) |
| 936 | offset += RHSVal.getInt().getZExtValue(); |
| 937 | else |
| 938 | offset -= RHSVal.getInt().getZExtValue(); |
| 939 | Result = APValue(Result.getLValueBase(), offset); |
| 940 | return true; |
| 941 | } |
| 942 | |
| 943 | // Handle cases like 4 + (unsigned long)&a |
| 944 | if (E->getOpcode() == BinaryOperator::Add && |
| 945 | RHSVal.isLValue() && Result.isInt()) { |
| 946 | uint64_t offset = RHSVal.getLValueOffset(); |
| 947 | offset += Result.getInt().getZExtValue(); |
| 948 | Result = APValue(RHSVal.getLValueBase(), offset); |
| 949 | return true; |
| 950 | } |
| 951 | |
| 952 | // All the following cases expect both operands to be an integer |
| 953 | if (!Result.isInt() || !RHSVal.isInt()) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 954 | return false; |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 955 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 956 | APSInt& RHS = RHSVal.getInt(); |
| 957 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 958 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 959 | default: |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 960 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 961 | case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E); |
| 962 | case BinaryOperator::Add: return Success(Result.getInt() + RHS, E); |
| 963 | case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E); |
| 964 | case BinaryOperator::And: return Success(Result.getInt() & RHS, E); |
| 965 | case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E); |
| 966 | case BinaryOperator::Or: return Success(Result.getInt() | RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 967 | case BinaryOperator::Div: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 968 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 969 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 970 | return Success(Result.getInt() / RHS, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 971 | case BinaryOperator::Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 972 | if (RHS == 0) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 973 | return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 974 | return Success(Result.getInt() % RHS, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 975 | case BinaryOperator::Shl: { |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 976 | // FIXME: Warn about out of range shift amounts! |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 977 | unsigned SA = |
| 978 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 979 | return Success(Result.getInt() << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 980 | } |
| 981 | case BinaryOperator::Shr: { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 982 | unsigned SA = |
| 983 | (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1); |
| 984 | return Success(Result.getInt() >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 985 | } |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 986 | |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 987 | case BinaryOperator::LT: return Success(Result.getInt() < RHS, E); |
| 988 | case BinaryOperator::GT: return Success(Result.getInt() > RHS, E); |
| 989 | case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E); |
| 990 | case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E); |
| 991 | case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E); |
| 992 | case BinaryOperator::NE: return Success(Result.getInt() != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 993 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 996 | bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) { |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 997 | bool Cond; |
| 998 | if (!HandleConversionToBool(E->getCond(), Cond, Info)) |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | |
Nuno Lopes | a25bd55 | 2008-11-16 22:06:39 +0000 | [diff] [blame] | 1001 | return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr()); |
Nuno Lopes | ca7c2ea | 2008-11-16 19:28:31 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1004 | unsigned IntExprEvaluator::GetAlignOfType(QualType T) { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1005 | const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr(); |
| 1006 | |
| 1007 | // __alignof__(void) = 1 as a gcc extension. |
| 1008 | if (Ty->isVoidType()) |
| 1009 | return 1; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1011 | // GCC extension: alignof(function) = 4. |
| 1012 | // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the |
| 1013 | // attribute(align) directive. |
| 1014 | if (Ty->isFunctionType()) |
| 1015 | return 4; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1016 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1017 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) |
| 1018 | return GetAlignOfType(QualType(EXTQT->getBaseType(), 0)); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1019 | |
| 1020 | // alignof VLA/incomplete array. |
| 1021 | if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty)) |
| 1022 | return GetAlignOfType(VAT->getElementType()); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1024 | // sizeof (objc class)? |
| 1025 | if (isa<ObjCInterfaceType>(Ty)) |
| 1026 | return 1; // FIXME: This probably isn't right. |
| 1027 | |
| 1028 | // Get information about the alignment. |
| 1029 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Eli Friedman | c508203 | 2009-02-22 03:31:23 +0000 | [diff] [blame] | 1030 | return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize; |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1033 | unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
| 1034 | E = E->IgnoreParens(); |
| 1035 | |
| 1036 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 1037 | // to 1 in those cases. |
| 1038 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1039 | return Info.Ctx.getDeclAlignInBytes(DRE->getDecl()); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1041 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 1042 | return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl()); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1044 | return GetAlignOfType(E->getType()); |
| 1045 | } |
| 1046 | |
| 1047 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1048 | /// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the |
| 1049 | /// expression's type. |
| 1050 | bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
| 1051 | QualType DstTy = E->getType(); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1053 | // Handle alignof separately. |
| 1054 | if (!E->isSizeOf()) { |
| 1055 | if (E->isArgumentType()) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1056 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1057 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1058 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1059 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1060 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1061 | QualType SrcTy = E->getTypeOfArgument(); |
| 1062 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1063 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1064 | // extension. |
| 1065 | if (SrcTy->isVoidType() || SrcTy->isFunctionType()) |
| 1066 | return Success(1, E); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1068 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1069 | if (!SrcTy->isConstantSizeType()) |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1070 | return false; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (SrcTy->isObjCInterfaceType()) { |
| 1073 | // Slightly unusual case: the size of an ObjC interface type is the |
| 1074 | // size of the class. This code intentionally falls through to the normal |
| 1075 | // case. |
| 1076 | ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl(); |
| 1077 | RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI)); |
| 1078 | SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD)); |
| 1079 | } |
| 1080 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 1081 | // Get information about the size. |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1082 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1083 | return Success(Info.Ctx.getTypeSize(SrcTy) / CharSize, E); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1086 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1087 | // Special case unary operators that do not need their subexpression |
| 1088 | // evaluated. offsetof/sizeof/alignof are all special. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1089 | if (E->isOffsetOfOp()) { |
| 1090 | // The AST for offsetof is defined in such a way that we can just |
| 1091 | // directly Evaluate it as an l-value. |
| 1092 | APValue LV; |
| 1093 | if (!EvaluateLValue(E->getSubExpr(), LV, Info)) |
| 1094 | return false; |
| 1095 | if (LV.getLValueBase()) |
| 1096 | return false; |
| 1097 | return Success(LV.getLValueOffset(), E); |
| 1098 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1099 | |
| 1100 | if (E->getOpcode() == UnaryOperator::LNot) { |
| 1101 | // LNot's operand isn't necessarily an integer, so we handle it specially. |
| 1102 | bool bres; |
| 1103 | if (!HandleConversionToBool(E->getSubExpr(), bres, Info)) |
| 1104 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1105 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Daniel Dunbar | 4fff481 | 2009-02-21 18:14:20 +0000 | [diff] [blame] | 1108 | // Only handle integral operations... |
| 1109 | if (!E->getSubExpr()->getType()->isIntegralType()) |
| 1110 | return false; |
| 1111 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1112 | // Get the operand value into 'Result'. |
| 1113 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1114 | return false; |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1116 | switch (E->getOpcode()) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1117 | default: |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1118 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 1119 | // See C99 6.6p3. |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1120 | return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1121 | case UnaryOperator::Extension: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1122 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 1123 | // If so, we could clear the diagnostic ID. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1124 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1125 | case UnaryOperator::Plus: |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 1126 | // The result is always just the subexpr. |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1127 | return true; |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1128 | case UnaryOperator::Minus: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1129 | if (!Result.isInt()) return false; |
| 1130 | return Success(-Result.getInt(), E); |
Chris Lattner | 75a4881 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 1131 | case UnaryOperator::Not: |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1132 | if (!Result.isInt()) return false; |
| 1133 | return Success(~Result.getInt(), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1134 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1137 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 1138 | /// result type is integer. |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1139 | bool IntExprEvaluator::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1140 | Expr *SubExpr = E->getSubExpr(); |
| 1141 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1142 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 1143 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1144 | if (DestType->isBooleanType()) { |
| 1145 | bool BoolResult; |
| 1146 | if (!HandleConversionToBool(SubExpr, BoolResult, Info)) |
| 1147 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 1148 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1151 | // Handle simple integer->integer casts. |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1152 | if (SrcType->isIntegralType()) { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1153 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1154 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1155 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1156 | if (!Result.isInt()) { |
| 1157 | // Only allow casts of lvalues if they are lossless. |
| 1158 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 1159 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1160 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1161 | return Success(HandleIntToIntCast(DestType, SrcType, |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1162 | Result.getInt(), Info.Ctx), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | // FIXME: Clean this up! |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1166 | if (SrcType->isPointerType()) { |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1167 | APValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1168 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1169 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1170 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1171 | if (LV.getLValueBase()) { |
| 1172 | // Only allow based lvalue casts if they are lossless. |
| 1173 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| 1174 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1175 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 1176 | Result = LV; |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType); |
| 1181 | return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1182 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1183 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 1184 | if (SrcType->isArrayType() || SrcType->isFunctionType()) { |
| 1185 | // This handles double-conversion cases, where there's both |
| 1186 | // an l-value promotion and an implicit conversion to int. |
| 1187 | APValue LV; |
| 1188 | if (!EvaluateLValue(SubExpr, LV, Info)) |
| 1189 | return false; |
| 1190 | |
| 1191 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy)) |
| 1192 | return false; |
| 1193 | |
| 1194 | Result = LV; |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1198 | // FIXME: Handle complex types |
| 1199 | // FIXME: Handle vectors |
| 1200 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1201 | if (!SrcType->isRealFloatingType()) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1202 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1203 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1204 | APFloat F(0.0); |
| 1205 | if (!EvaluateFloat(SubExpr, F, Info)) |
Anders Carlsson | 0e8acbb | 2008-11-30 18:37:00 +0000 | [diff] [blame] | 1206 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 1207 | |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 1208 | return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 1209 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 1210 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1211 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 1212 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 1213 | APValue LV; |
| 1214 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1215 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1216 | return Success(LV.getComplexIntReal(), E); |
| 1217 | } |
| 1218 | |
| 1219 | return Visit(E->getSubExpr()); |
| 1220 | } |
| 1221 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1222 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 1223 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| 1224 | APValue LV; |
| 1225 | if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt()) |
| 1226 | return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E); |
| 1227 | return Success(LV.getComplexIntImag(), E); |
| 1228 | } |
| 1229 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 1230 | if (!E->getSubExpr()->isEvaluatable(Info.Ctx)) |
| 1231 | Info.EvalResult.HasSideEffects = true; |
| 1232 | return Success(0, E); |
| 1233 | } |
| 1234 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1235 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1236 | // Float Evaluation |
| 1237 | //===----------------------------------------------------------------------===// |
| 1238 | |
| 1239 | namespace { |
| 1240 | class VISIBILITY_HIDDEN FloatExprEvaluator |
| 1241 | : public StmtVisitor<FloatExprEvaluator, bool> { |
| 1242 | EvalInfo &Info; |
| 1243 | APFloat &Result; |
| 1244 | public: |
| 1245 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| 1246 | : Info(info), Result(result) {} |
| 1247 | |
| 1248 | bool VisitStmt(Stmt *S) { |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1253 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1254 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1255 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1256 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 1257 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1258 | bool VisitCastExpr(CastExpr *E); |
| 1259 | bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1260 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1261 | bool VisitChooseExpr(const ChooseExpr *E) |
| 1262 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1263 | bool VisitUnaryExtension(const UnaryOperator *E) |
| 1264 | { return Visit(E->getSubExpr()); } |
| 1265 | |
| 1266 | // FIXME: Missing: __real__/__imag__, array subscript of vector, |
| 1267 | // member of vector, ImplicitValueInitExpr, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1268 | // conditional ?:, comma |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1269 | }; |
| 1270 | } // end anonymous namespace |
| 1271 | |
| 1272 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| 1273 | return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
| 1274 | } |
| 1275 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1276 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1277 | switch (E->isBuiltinCall(Info.Ctx)) { |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1278 | default: return false; |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1279 | case Builtin::BI__builtin_huge_val: |
| 1280 | case Builtin::BI__builtin_huge_valf: |
| 1281 | case Builtin::BI__builtin_huge_vall: |
| 1282 | case Builtin::BI__builtin_inf: |
| 1283 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1284 | case Builtin::BI__builtin_infl: { |
| 1285 | const llvm::fltSemantics &Sem = |
| 1286 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 1287 | Result = llvm::APFloat::getInf(Sem); |
| 1288 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1289 | } |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1290 | |
| 1291 | case Builtin::BI__builtin_nan: |
| 1292 | case Builtin::BI__builtin_nanf: |
| 1293 | case Builtin::BI__builtin_nanl: |
| 1294 | // If this is __builtin_nan("") turn this into a simple nan, otherwise we |
| 1295 | // can't constant fold it. |
| 1296 | if (const StringLiteral *S = |
| 1297 | dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) { |
| 1298 | if (!S->isWide() && S->getByteLength() == 0) { // empty string. |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 1299 | const llvm::fltSemantics &Sem = |
| 1300 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 1301 | Result = llvm::APFloat::getNaN(Sem); |
| 1302 | return true; |
| 1303 | } |
| 1304 | } |
| 1305 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1306 | |
| 1307 | case Builtin::BI__builtin_fabs: |
| 1308 | case Builtin::BI__builtin_fabsf: |
| 1309 | case Builtin::BI__builtin_fabsl: |
| 1310 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 1311 | return false; |
| 1312 | |
| 1313 | if (Result.isNegative()) |
| 1314 | Result.changeSign(); |
| 1315 | return true; |
| 1316 | |
| 1317 | case Builtin::BI__builtin_copysign: |
| 1318 | case Builtin::BI__builtin_copysignf: |
| 1319 | case Builtin::BI__builtin_copysignl: { |
| 1320 | APFloat RHS(0.); |
| 1321 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 1322 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 1323 | return false; |
| 1324 | Result.copySign(RHS); |
| 1325 | return true; |
| 1326 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1330 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Nuno Lopes | a468d34 | 2008-11-19 17:44:31 +0000 | [diff] [blame] | 1331 | if (E->getOpcode() == UnaryOperator::Deref) |
| 1332 | return false; |
| 1333 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1334 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 1335 | return false; |
| 1336 | |
| 1337 | switch (E->getOpcode()) { |
| 1338 | default: return false; |
| 1339 | case UnaryOperator::Plus: |
| 1340 | return true; |
| 1341 | case UnaryOperator::Minus: |
| 1342 | Result.changeSign(); |
| 1343 | return true; |
| 1344 | } |
| 1345 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1346 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1347 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 1348 | // FIXME: Diagnostics? I really don't understand how the warnings |
| 1349 | // and errors are supposed to work. |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 1350 | APFloat RHS(0.0); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1351 | if (!EvaluateFloat(E->getLHS(), Result, Info)) |
| 1352 | return false; |
| 1353 | if (!EvaluateFloat(E->getRHS(), RHS, Info)) |
| 1354 | return false; |
| 1355 | |
| 1356 | switch (E->getOpcode()) { |
| 1357 | default: return false; |
| 1358 | case BinaryOperator::Mul: |
| 1359 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1360 | return true; |
| 1361 | case BinaryOperator::Add: |
| 1362 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
| 1363 | return true; |
| 1364 | case BinaryOperator::Sub: |
| 1365 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1366 | return true; |
| 1367 | case BinaryOperator::Div: |
| 1368 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1369 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 1374 | Result = E->getValue(); |
| 1375 | return true; |
| 1376 | } |
| 1377 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1378 | bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) { |
| 1379 | Expr* SubExpr = E->getSubExpr(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1380 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1381 | if (SubExpr->getType()->isIntegralType()) { |
| 1382 | APSInt IntResult; |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 1383 | if (!EvaluateInteger(SubExpr, IntResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1384 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1385 | Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(), |
| 1386 | IntResult, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
| 1389 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1390 | if (!Visit(SubExpr)) |
| 1391 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1392 | Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(), |
| 1393 | Result, Info.Ctx); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1394 | return true; |
| 1395 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1396 | // FIXME: Handle complex types |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1397 | |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
| 1401 | bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 1402 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 1403 | return true; |
| 1404 | } |
| 1405 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1406 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1407 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1408 | //===----------------------------------------------------------------------===// |
| 1409 | |
| 1410 | namespace { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1411 | class VISIBILITY_HIDDEN ComplexExprEvaluator |
| 1412 | : public StmtVisitor<ComplexExprEvaluator, APValue> { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1413 | EvalInfo &Info; |
| 1414 | |
| 1415 | public: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1416 | ComplexExprEvaluator(EvalInfo &info) : Info(info) {} |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1417 | |
| 1418 | //===--------------------------------------------------------------------===// |
| 1419 | // Visitor Methods |
| 1420 | //===--------------------------------------------------------------------===// |
| 1421 | |
| 1422 | APValue VisitStmt(Stmt *S) { |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1423 | return APValue(); |
| 1424 | } |
| 1425 | |
| 1426 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 1427 | |
| 1428 | APValue VisitImaginaryLiteral(ImaginaryLiteral *E) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1429 | Expr* SubExpr = E->getSubExpr(); |
| 1430 | |
| 1431 | if (SubExpr->getType()->isRealFloatingType()) { |
| 1432 | APFloat Result(0.0); |
| 1433 | |
| 1434 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1435 | return APValue(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1436 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1437 | return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false), |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1438 | Result); |
| 1439 | } else { |
| 1440 | assert(SubExpr->getType()->isIntegerType() && |
| 1441 | "Unexpected imaginary literal."); |
| 1442 | |
| 1443 | llvm::APSInt Result; |
| 1444 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1445 | return APValue(); |
| 1446 | |
| 1447 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1448 | Zero = 0; |
| 1449 | return APValue(Zero, Result); |
| 1450 | } |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1453 | APValue VisitCastExpr(CastExpr *E) { |
| 1454 | Expr* SubExpr = E->getSubExpr(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1455 | QualType EltType = E->getType()->getAsComplexType()->getElementType(); |
| 1456 | QualType SubType = SubExpr->getType(); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1457 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1458 | if (SubType->isRealFloatingType()) { |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1459 | APFloat Result(0.0); |
| 1460 | |
| 1461 | if (!EvaluateFloat(SubExpr, Result, Info)) |
| 1462 | return APValue(); |
| 1463 | |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1464 | // Apply float conversion if necessary. |
| 1465 | Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | 8f826f0 | 2009-01-24 19:08:01 +0000 | [diff] [blame] | 1466 | return APValue(Result, |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1467 | APFloat(Result.getSemantics(), APFloat::fcZero, false)); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1468 | } else if (SubType->isIntegerType()) { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1469 | APSInt Result; |
| 1470 | |
| 1471 | if (!EvaluateInteger(SubExpr, Result, Info)) |
| 1472 | return APValue(); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Apply integer conversion if necessary. |
| 1475 | Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1476 | llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned()); |
| 1477 | Zero = 0; |
| 1478 | return APValue(Result, Zero); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1479 | } else if (const ComplexType *CT = SubType->getAsComplexType()) { |
| 1480 | APValue Src; |
| 1481 | |
| 1482 | if (!EvaluateComplex(SubExpr, Src, Info)) |
| 1483 | return APValue(); |
| 1484 | |
| 1485 | QualType SrcType = CT->getElementType(); |
| 1486 | |
| 1487 | if (Src.isComplexFloat()) { |
| 1488 | if (EltType->isRealFloatingType()) { |
| 1489 | return APValue(HandleFloatToFloatCast(EltType, SrcType, |
| 1490 | Src.getComplexFloatReal(), |
| 1491 | Info.Ctx), |
| 1492 | HandleFloatToFloatCast(EltType, SrcType, |
| 1493 | Src.getComplexFloatImag(), |
| 1494 | Info.Ctx)); |
| 1495 | } else { |
| 1496 | return APValue(HandleFloatToIntCast(EltType, SrcType, |
| 1497 | Src.getComplexFloatReal(), |
| 1498 | Info.Ctx), |
| 1499 | HandleFloatToIntCast(EltType, SrcType, |
| 1500 | Src.getComplexFloatImag(), |
| 1501 | Info.Ctx)); |
| 1502 | } |
| 1503 | } else { |
| 1504 | assert(Src.isComplexInt() && "Invalid evaluate result."); |
| 1505 | if (EltType->isRealFloatingType()) { |
| 1506 | return APValue(HandleIntToFloatCast(EltType, SrcType, |
| 1507 | Src.getComplexIntReal(), |
| 1508 | Info.Ctx), |
| 1509 | HandleIntToFloatCast(EltType, SrcType, |
| 1510 | Src.getComplexIntImag(), |
| 1511 | Info.Ctx)); |
| 1512 | } else { |
| 1513 | return APValue(HandleIntToIntCast(EltType, SrcType, |
| 1514 | Src.getComplexIntReal(), |
| 1515 | Info.Ctx), |
| 1516 | HandleIntToIntCast(EltType, SrcType, |
| 1517 | Src.getComplexIntImag(), |
| 1518 | Info.Ctx)); |
| 1519 | } |
| 1520 | } |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | // FIXME: Handle more casts. |
| 1524 | return APValue(); |
| 1525 | } |
| 1526 | |
| 1527 | APValue VisitBinaryOperator(const BinaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 1528 | APValue VisitChooseExpr(const ChooseExpr *E) |
| 1529 | { return Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 1530 | APValue VisitUnaryExtension(const UnaryOperator *E) |
| 1531 | { return Visit(E->getSubExpr()); } |
| 1532 | // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 1533 | // conditional ?:, comma |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1534 | }; |
| 1535 | } // end anonymous namespace |
| 1536 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1537 | static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1538 | { |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1539 | Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
| 1540 | assert((!Result.isComplexFloat() || |
| 1541 | (&Result.getComplexFloatReal().getSemantics() == |
| 1542 | &Result.getComplexFloatImag().getSemantics())) && |
| 1543 | "Invalid complex evaluation."); |
| 1544 | return Result.isComplexFloat() || Result.isComplexInt(); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1547 | APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1548 | { |
| 1549 | APValue Result, RHS; |
| 1550 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1551 | if (!EvaluateComplex(E->getLHS(), Result, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1552 | return APValue(); |
| 1553 | |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1554 | if (!EvaluateComplex(E->getRHS(), RHS, Info)) |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1555 | return APValue(); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1556 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1557 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 1558 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1559 | switch (E->getOpcode()) { |
| 1560 | default: return APValue(); |
| 1561 | case BinaryOperator::Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1562 | if (Result.isComplexFloat()) { |
| 1563 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 1564 | APFloat::rmNearestTiesToEven); |
| 1565 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 1566 | APFloat::rmNearestTiesToEven); |
| 1567 | } else { |
| 1568 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 1569 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 1570 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1571 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1572 | case BinaryOperator::Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1573 | if (Result.isComplexFloat()) { |
| 1574 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 1575 | APFloat::rmNearestTiesToEven); |
| 1576 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 1577 | APFloat::rmNearestTiesToEven); |
| 1578 | } else { |
| 1579 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 1580 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 1581 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | case BinaryOperator::Mul: |
| 1584 | if (Result.isComplexFloat()) { |
| 1585 | APValue LHS = Result; |
| 1586 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 1587 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 1588 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 1589 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 1590 | |
| 1591 | APFloat Tmp = LHS_r; |
| 1592 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1593 | Result.getComplexFloatReal() = Tmp; |
| 1594 | Tmp = LHS_i; |
| 1595 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1596 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 1597 | |
| 1598 | Tmp = LHS_r; |
| 1599 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 1600 | Result.getComplexFloatImag() = Tmp; |
| 1601 | Tmp = LHS_i; |
| 1602 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 1603 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 1604 | } else { |
| 1605 | APValue LHS = Result; |
| 1606 | Result.getComplexIntReal() = |
| 1607 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 1608 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
| 1609 | Result.getComplexIntImag() = |
| 1610 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 1611 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 1612 | } |
| 1613 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | return Result; |
| 1617 | } |
| 1618 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 1619 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1620 | // Top level Expr::Evaluate method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1621 | //===----------------------------------------------------------------------===// |
| 1622 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1623 | /// Evaluate - Return true if this is a constant which we can fold using |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 1624 | /// any crazy technique (that has nothing to do with language standards) that |
| 1625 | /// we want to. If this function returns true, it returns the folded constant |
| 1626 | /// in Result. |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1627 | bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const { |
| 1628 | EvalInfo Info(Ctx, Result); |
Anders Carlsson | 54da049 | 2008-11-30 16:38:33 +0000 | [diff] [blame] | 1629 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1630 | if (getType()->isVectorType()) { |
| 1631 | if (!EvaluateVector(this, Result.Val, Info)) |
| 1632 | return false; |
| 1633 | } else if (getType()->isIntegerType()) { |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 1634 | if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this))) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1635 | return false; |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 1636 | } else if (getType()->hasPointerRepresentation()) { |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1637 | if (!EvaluatePointer(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1638 | return false; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1639 | } else if (getType()->isRealFloatingType()) { |
| 1640 | llvm::APFloat f(0.0); |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1641 | if (!EvaluateFloat(this, f, Info)) |
| 1642 | return false; |
| 1643 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1644 | Result.Val = APValue(f); |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1645 | } else if (getType()->isAnyComplexType()) { |
| 1646 | if (!EvaluateComplex(this, Result.Val, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1647 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 1648 | } else |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 1649 | return false; |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 1650 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 1651 | return true; |
| 1652 | } |
| 1653 | |
Chris Lattner | 6ee7aa1 | 2008-11-16 21:24:15 +0000 | [diff] [blame] | 1654 | /// isEvaluatable - Call Evaluate to see if this expression can be constant |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1655 | /// folded, but discard the result. |
| 1656 | bool Expr::isEvaluatable(ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 1657 | EvalResult Result; |
| 1658 | return Evaluate(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 1659 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1660 | |
| 1661 | APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1662 | EvalResult EvalResult; |
| 1663 | bool Result = Evaluate(EvalResult, Ctx); |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 1664 | Result = Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1665 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1666 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1667 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 1668 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 1669 | } |