Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c7436af | 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" |
| 16 | #include "clang/AST/Expr.h" |
Seo Sanghyeon | efddb9c | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 21 | using namespace clang; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 22 | using llvm::APSInt; |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 23 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 24 | #define USE_NEW_EVALUATOR 0 |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 25 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 26 | static bool CalcFakeICEVal(const Expr *Expr, |
| 27 | llvm::APSInt &Result, |
| 28 | ASTContext &Context) { |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 29 | // Calculate the value of an expression that has a calculatable |
| 30 | // value, but isn't an ICE. Currently, this only supports |
| 31 | // a very narrow set of extensions, but it can be expanded if needed. |
| 32 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr)) |
| 33 | return CalcFakeICEVal(PE->getSubExpr(), Result, Context); |
| 34 | |
| 35 | if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) { |
| 36 | QualType CETy = CE->getType(); |
| 37 | if ((CETy->isIntegralType() && !CETy->isBooleanType()) || |
| 38 | CETy->isPointerType()) { |
| 39 | if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) { |
| 40 | Result.extOrTrunc(Context.getTypeSize(CETy)); |
| 41 | // FIXME: This assumes pointers are signed. |
| 42 | Result.setIsSigned(CETy->isSignedIntegerType() || |
| 43 | CETy->isPointerType()); |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (Expr->getType()->isIntegralType()) |
| 50 | return Expr->isIntegerConstantExpr(Result, Context); |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 55 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 56 | /// information about a subexpression as it is folded. It retains information |
| 57 | /// about the AST context, but also maintains information about the folded |
| 58 | /// expression. |
| 59 | /// |
| 60 | /// If an expression could be evaluated, it is still possible it is not a C |
| 61 | /// "integer constant expression" or constant expression. If not, this struct |
| 62 | /// captures information about how and why not. |
| 63 | /// |
| 64 | /// One bit of information passed *into* the request for constant folding |
| 65 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 66 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 67 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 68 | /// certain things in certain situations. |
| 69 | struct EvalInfo { |
| 70 | ASTContext &Ctx; |
| 71 | |
| 72 | /// isEvaluated - True if the subexpression is required to be evaluated, false |
| 73 | /// if it is short-circuited (according to C rules). |
| 74 | bool isEvaluated; |
| 75 | |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 76 | /// ICEDiag - If the expression is unfoldable, then ICEDiag contains the |
| 77 | /// error diagnostic indicating why it is not foldable and DiagLoc indicates a |
| 78 | /// caret position for the error. If it is foldable, but the expression is |
| 79 | /// not an integer constant expression, ICEDiag contains the extension |
| 80 | /// diagnostic to emit which describes why it isn't an integer constant |
| 81 | /// expression. If this expression *is* an integer-constant-expr, then |
| 82 | /// ICEDiag is zero. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 83 | /// |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 84 | /// The caller can choose to emit this diagnostic or not, depending on whether |
| 85 | /// they require an i-c-e or a constant or not. DiagLoc indicates the caret |
| 86 | /// position for the report. |
| 87 | /// |
| 88 | /// If ICEDiag is zero, then this expression is an i-c-e. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 89 | unsigned ICEDiag; |
| 90 | SourceLocation DiagLoc; |
| 91 | |
| 92 | EvalInfo(ASTContext &ctx) : Ctx(ctx), isEvaluated(true), ICEDiag(0) {} |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info); |
| 97 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // Pointer Evaluation |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 104 | namespace { |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 105 | class VISIBILITY_HIDDEN PointerExprEvaluator |
| 106 | : public StmtVisitor<PointerExprEvaluator, APValue> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 107 | EvalInfo &Info; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 108 | public: |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 110 | PointerExprEvaluator(EvalInfo &info) : Info(info) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 111 | |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 112 | APValue VisitStmt(Stmt *S) { |
| 113 | // FIXME: Remove this when we support more expressions. |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 114 | printf("Unhandled pointer statement\n"); |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 115 | S->dump(); |
| 116 | return APValue(); |
| 117 | } |
| 118 | |
| 119 | APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 120 | |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 121 | APValue VisitBinaryOperator(const BinaryOperator *E); |
| 122 | APValue VisitCastExpr(const CastExpr* E); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 123 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 124 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 126 | static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 127 | if (!E->getType()->isPointerType()) |
| 128 | return false; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 129 | Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E)); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 130 | return Result.isLValue(); |
| 131 | } |
| 132 | |
| 133 | APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 134 | if (E->getOpcode() != BinaryOperator::Add && |
| 135 | E->getOpcode() != BinaryOperator::Sub) |
| 136 | return APValue(); |
| 137 | |
| 138 | const Expr *PExp = E->getLHS(); |
| 139 | const Expr *IExp = E->getRHS(); |
| 140 | if (IExp->getType()->isPointerType()) |
| 141 | std::swap(PExp, IExp); |
| 142 | |
| 143 | APValue ResultLValue; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 144 | if (!EvaluatePointer(PExp, ResultLValue, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 145 | return APValue(); |
| 146 | |
| 147 | llvm::APSInt AdditionalOffset(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 148 | if (!EvaluateInteger(IExp, AdditionalOffset, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 149 | return APValue(); |
| 150 | |
| 151 | uint64_t Offset = ResultLValue.getLValueOffset(); |
| 152 | if (E->getOpcode() == BinaryOperator::Add) |
| 153 | Offset += AdditionalOffset.getZExtValue(); |
| 154 | else |
| 155 | Offset -= AdditionalOffset.getZExtValue(); |
| 156 | |
| 157 | return APValue(ResultLValue.getLValueBase(), Offset); |
| 158 | } |
| 159 | |
| 160 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 161 | APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 162 | const Expr* SubExpr = E->getSubExpr(); |
| 163 | |
| 164 | // Check for pointer->pointer cast |
| 165 | if (SubExpr->getType()->isPointerType()) { |
| 166 | APValue Result; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 167 | if (EvaluatePointer(SubExpr, Result, Info)) |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 168 | return Result; |
| 169 | return APValue(); |
| 170 | } |
| 171 | |
| 172 | if (SubExpr->getType()->isArithmeticType()) { |
| 173 | llvm::APSInt Result(32); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 174 | if (EvaluateInteger(SubExpr, Result, Info)) { |
| 175 | Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType())); |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 176 | return APValue(0, Result.getZExtValue()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | assert(0 && "Unhandled cast"); |
| 181 | return APValue(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // Integer Evaluation |
| 187 | //===----------------------------------------------------------------------===// |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 188 | |
| 189 | namespace { |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 190 | class VISIBILITY_HIDDEN IntExprEvaluator |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 191 | : public StmtVisitor<IntExprEvaluator, bool> { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 192 | EvalInfo &Info; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 193 | APSInt &Result; |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 194 | public: |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 195 | IntExprEvaluator(EvalInfo &info, APSInt &result) |
| 196 | : Info(info), Result(result) {} |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 198 | unsigned getIntTypeSizeInBits(QualType T) const { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 199 | return (unsigned)Info.Ctx.getIntWidth(T); |
| 200 | } |
| 201 | |
| 202 | bool Extension(SourceLocation L, diag::kind D) { |
| 203 | Info.DiagLoc = L; |
| 204 | Info.ICEDiag = D; |
| 205 | return true; // still a constant. |
| 206 | } |
| 207 | |
| 208 | bool Error(SourceLocation L, diag::kind D) { |
| 209 | // If this is in an unevaluated portion of the subexpression, ignore the |
| 210 | // error. |
| 211 | if (!Info.isEvaluated) |
| 212 | return true; |
| 213 | |
| 214 | Info.DiagLoc = L; |
| 215 | Info.ICEDiag = D; |
| 216 | return false; |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 219 | //===--------------------------------------------------------------------===// |
| 220 | // Visitor Methods |
| 221 | //===--------------------------------------------------------------------===// |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 222 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 223 | bool VisitStmt(Stmt *S) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 224 | return Error(S->getLocStart(), diag::err_expr_not_constant); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 227 | bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 229 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| 230 | Result = E->getValue(); |
| 231 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 232 | return true; |
| 233 | } |
| 234 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| 235 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 236 | Result = E->getValue(); |
| 237 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 238 | return true; |
| 239 | } |
| 240 | bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 241 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 242 | Result = Info.Ctx.typesAreCompatible(E->getArgType1(), E->getArgType2()); |
| 243 | return true; |
| 244 | } |
| 245 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 246 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 247 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 248 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 249 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 250 | bool VisitCastExpr(CastExpr* E) { |
| 251 | return HandleCast(E->getLParenLoc(), E->getSubExpr(), E->getType()); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 253 | bool VisitImplicitCastExpr(ImplicitCastExpr* E) { |
| 254 | return HandleCast(E->getLocStart(), E->getSubExpr(), E->getType()); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 256 | bool VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 257 | return EvaluateSizeAlignOf(E->isSizeOf(), E->getArgumentType(), |
| 258 | E->getType()); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 259 | } |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 261 | private: |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 262 | bool HandleCast(SourceLocation CastLoc, Expr *SubExpr, QualType DestType); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 263 | bool EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, QualType DstTy); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 264 | }; |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 265 | } // end anonymous namespace |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 267 | static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) { |
| 268 | return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E)); |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 269 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 271 | bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| 272 | // Enums are integer constant exprs. |
| 273 | if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) { |
| 274 | Result = D->getInitVal(); |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | // Otherwise, random variable references are not constants. |
| 279 | return Error(E->getLocStart(), diag::err_expr_not_constant); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| 284 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 285 | // __builtin_type_compatible_p is a constant. |
| 286 | if (E->isBuiltinClassifyType(Result)) |
| 287 | return true; |
| 288 | |
| 289 | return Error(E->getLocStart(), diag::err_expr_not_constant); |
| 290 | } |
Anders Carlsson | c43f44b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 291 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 292 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 293 | // The LHS of a constant expr is always evaluated and needed. |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 294 | llvm::APSInt RHS(32); |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 295 | if (!Visit(E->getLHS())) |
| 296 | return false; // error in subexpression. |
| 297 | |
| 298 | bool OldEval = Info.isEvaluated; |
| 299 | |
| 300 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 301 | // RHS. Make sure to pass isEvaluated down correctly. |
| 302 | if ((E->getOpcode() == BinaryOperator::LAnd && Result == 0) || |
| 303 | (E->getOpcode() == BinaryOperator::LOr && Result != 0)) |
| 304 | Info.isEvaluated = false; |
| 305 | |
| 306 | if (!EvaluateInteger(E->getRHS(), RHS, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 307 | return false; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 308 | Info.isEvaluated = OldEval; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 309 | |
| 310 | switch (E->getOpcode()) { |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 311 | default: return Error(E->getOperatorLoc(), diag::err_expr_not_constant); |
| 312 | case BinaryOperator::Mul: Result *= RHS; return true; |
| 313 | case BinaryOperator::Add: Result += RHS; return true; |
| 314 | case BinaryOperator::Sub: Result -= RHS; return true; |
| 315 | case BinaryOperator::And: Result &= RHS; return true; |
| 316 | case BinaryOperator::Xor: Result ^= RHS; return true; |
| 317 | case BinaryOperator::Or: Result |= RHS; return true; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 318 | case BinaryOperator::Div: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 319 | if (RHS == 0) |
| 320 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 321 | Result /= RHS; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 322 | return true; |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 323 | case BinaryOperator::Rem: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 324 | if (RHS == 0) |
| 325 | return Error(E->getOperatorLoc(), diag::err_expr_divide_by_zero); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 326 | Result %= RHS; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 327 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 328 | case BinaryOperator::Shl: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 329 | // FIXME: Warn about out of range shift amounts! |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 330 | Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 331 | break; |
| 332 | case BinaryOperator::Shr: |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 333 | Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 334 | break; |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 045502c | 2008-07-11 19:29:32 +0000 | [diff] [blame] | 336 | case BinaryOperator::LT: |
| 337 | Result = Result < RHS; |
| 338 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 339 | break; |
| 340 | case BinaryOperator::GT: |
| 341 | Result = Result > RHS; |
| 342 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 343 | break; |
| 344 | case BinaryOperator::LE: |
| 345 | Result = Result <= RHS; |
| 346 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 347 | break; |
| 348 | case BinaryOperator::GE: |
| 349 | Result = Result >= RHS; |
| 350 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 351 | break; |
| 352 | case BinaryOperator::EQ: |
| 353 | Result = Result == RHS; |
| 354 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 355 | break; |
| 356 | case BinaryOperator::NE: |
| 357 | Result = Result != RHS; |
| 358 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 359 | break; |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 360 | case BinaryOperator::LAnd: |
| 361 | Result = Result != 0 && RHS != 0; |
| 362 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 363 | break; |
| 364 | case BinaryOperator::LOr: |
| 365 | Result = Result != 0 || RHS != 0; |
| 366 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 367 | break; |
| 368 | |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 370 | case BinaryOperator::Comma: |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 371 | // Result of the comma is just the result of the RHS. |
| 372 | Result = RHS; |
| 373 | |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 374 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 375 | // *except* when they are contained within a subexpression that is not |
| 376 | // evaluated". Note that Assignment can never happen due to constraints |
| 377 | // on the LHS subexpr, so we don't need to check it here. |
Chris Lattner | 82437da | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 378 | if (!Info.isEvaluated) |
| 379 | return true; |
| 380 | |
| 381 | // If the value is evaluated, we can accept it as an extension. |
| 382 | return Extension(E->getOperatorLoc(), diag::ext_comma_in_constant_expr); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 386 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 389 | /// EvaluateSizeAlignOf - Evaluate sizeof(SrcTy) or alignof(SrcTy) with a result |
| 390 | /// as a DstTy type. |
| 391 | bool IntExprEvaluator::EvaluateSizeAlignOf(bool isSizeOf, QualType SrcTy, |
| 392 | QualType DstTy) { |
| 393 | // Return the result in the right width. |
| 394 | Result.zextOrTrunc(getIntTypeSizeInBits(DstTy)); |
| 395 | Result.setIsUnsigned(DstTy->isUnsignedIntegerType()); |
| 396 | |
| 397 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 398 | if (SrcTy->isVoidType()) |
| 399 | Result = 1; |
| 400 | |
| 401 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| 402 | if (!SrcTy->isConstantSizeType()) { |
| 403 | // FIXME: Should we attempt to evaluate this? |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | // GCC extension: sizeof(function) = 1. |
| 408 | if (SrcTy->isFunctionType()) { |
| 409 | // FIXME: AlignOf shouldn't be unconditionally 4! |
| 410 | Result = isSizeOf ? 1 : 4; |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | // Get information about the size or align. |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 415 | unsigned CharSize = Info.Ctx.Target.getCharWidth(); |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 416 | if (isSizeOf) |
| 417 | Result = getIntTypeSizeInBits(SrcTy) / CharSize; |
| 418 | else |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 419 | Result = Info.Ctx.getTypeAlign(SrcTy) / CharSize; |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 420 | return true; |
| 421 | } |
| 422 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 423 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 424 | // Special case unary operators that do not need their subexpression |
| 425 | // evaluated. offsetof/sizeof/alignof are all special. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 426 | if (E->isOffsetOfOp()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 427 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 428 | Result = E->evaluateOffsetOf(Info.Ctx); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 429 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | if (E->isSizeOfAlignOfOp()) |
Chris Lattner | 265a089 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 434 | return EvaluateSizeAlignOf(E->getOpcode() == UnaryOperator::SizeOf, |
| 435 | E->getSubExpr()->getType(), E->getType()); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 437 | // Get the operand value into 'Result'. |
| 438 | if (!Visit(E->getSubExpr())) |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 439 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 440 | |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 441 | switch (E->getOpcode()) { |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 442 | default: |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 443 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 444 | // See C99 6.6p3. |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 445 | return Error(E->getOperatorLoc(), diag::err_expr_not_constant); |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 446 | case UnaryOperator::LNot: { |
| 447 | bool Val = Result == 0; |
| 448 | Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); |
| 449 | Result = Val; |
| 450 | break; |
| 451 | } |
| 452 | case UnaryOperator::Extension: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 453 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 454 | // If so, we could clear the diagnostic ID. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 455 | case UnaryOperator::Plus: |
Chris Lattner | 15e5911 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 456 | // The result is always just the subexpr. |
Chris Lattner | 400d740 | 2008-07-11 22:15:16 +0000 | [diff] [blame] | 457 | break; |
| 458 | case UnaryOperator::Minus: |
| 459 | Result = -Result; |
| 460 | break; |
| 461 | case UnaryOperator::Not: |
| 462 | Result = ~Result; |
| 463 | break; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 467 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 470 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 471 | /// result type is integer. |
| 472 | bool IntExprEvaluator::HandleCast(SourceLocation CastLoc, |
| 473 | Expr *SubExpr, QualType DestType) { |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 474 | unsigned DestWidth = getIntTypeSizeInBits(DestType); |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 475 | |
| 476 | // Handle simple integer->integer casts. |
| 477 | if (SubExpr->getType()->isIntegerType()) { |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 478 | if (!Visit(SubExpr)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 479 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 480 | |
| 481 | // Figure out if this is a truncate, extend or noop cast. |
| 482 | // If the input is signed, do a sign extend, noop, or truncate. |
| 483 | if (DestType->isBooleanType()) { |
| 484 | // Conversion to bool compares against zero. |
| 485 | Result = Result != 0; |
| 486 | Result.zextOrTrunc(DestWidth); |
Chris Lattner | 2c99c71 | 2008-07-11 19:24:49 +0000 | [diff] [blame] | 487 | } else |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 488 | Result.extOrTrunc(DestWidth); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 489 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | // FIXME: Clean this up! |
| 494 | if (SubExpr->getType()->isPointerType()) { |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 495 | APValue LV; |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 496 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 497 | return false; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 498 | if (LV.getLValueBase()) |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 499 | return false; |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 500 | |
Anders Carlsson | 8ab15c8 | 2008-07-08 16:49:00 +0000 | [diff] [blame] | 501 | Result.extOrTrunc(DestWidth); |
| 502 | Result = LV.getLValueOffset(); |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 503 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 504 | return true; |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | ff579ff | 2008-07-12 01:15:53 +0000 | [diff] [blame^] | 507 | if (!SubExpr->getType()->isRealFloatingType()) |
| 508 | return Error(CastLoc, diag::err_expr_not_constant); |
| 509 | |
| 510 | // FIXME: Generalize floating point constant folding! For now we just permit |
| 511 | // which is allowed by integer constant expressions. |
| 512 | |
| 513 | // Allow floating constants that are the immediate operands of casts or that |
| 514 | // are parenthesized. |
| 515 | const Expr *Operand = SubExpr; |
| 516 | while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand)) |
| 517 | Operand = PE->getSubExpr(); |
| 518 | |
| 519 | // If this isn't a floating literal, we can't handle it. |
| 520 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 521 | if (!FL) |
| 522 | return Error(CastLoc, diag::err_expr_not_constant); |
| 523 | |
| 524 | // If the destination is boolean, compare against zero. |
| 525 | if (DestType->isBooleanType()) { |
| 526 | Result = !FL->getValue().isZero(); |
| 527 | Result.zextOrTrunc(DestWidth); |
| 528 | Result.setIsUnsigned(DestType->isUnsignedIntegerType()); |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | // Determine whether we are converting to unsigned or signed. |
| 533 | bool DestSigned = DestType->isSignedIntegerType(); |
| 534 | |
| 535 | // FIXME: Warning for overflow. |
| 536 | uint64_t Space[4]; |
| 537 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
| 538 | llvm::APFloat::rmTowardZero); |
| 539 | Result = llvm::APInt(DestWidth, 4, Space); |
| 540 | Result.setIsUnsigned(!DestSigned); |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 541 | return true; |
Anders Carlsson | d1aa581 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 542 | } |
Anders Carlsson | 02a34c3 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 543 | |
Chris Lattner | a823ccf | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 544 | //===----------------------------------------------------------------------===// |
| 545 | // Top level TryEvaluate. |
| 546 | //===----------------------------------------------------------------------===// |
| 547 | |
Chris Lattner | a42f09a | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 548 | bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { |
Chris Lattner | 334b194 | 2008-07-11 19:19:21 +0000 | [diff] [blame] | 549 | llvm::APSInt sInt(32); |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 550 | #if USE_NEW_EVALUATOR |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 551 | EvalInfo Info(Ctx); |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 552 | if (getType()->isIntegerType()) { |
Chris Lattner | 422373c | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 553 | if (EvaluateInteger(this, sInt, Info)) { |
Anders Carlsson | c032801 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 554 | Result = APValue(sInt); |
| 555 | return true; |
| 556 | } |
| 557 | } else |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 558 | return false; |
| 559 | |
| 560 | #else |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 561 | if (CalcFakeICEVal(this, sInt, Ctx)) { |
| 562 | Result = APValue(sInt); |
| 563 | return true; |
| 564 | } |
Anders Carlsson | cad17b5 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 565 | #endif |
Anders Carlsson | c7436af | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 566 | |
| 567 | return false; |
| 568 | } |