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