blob: 8f5ff7284b7787a4e4ce8618c650df6d02779cb4 [file] [log] [blame]
Chris Lattnerb542afe2008-07-11 19:10:17 +00001//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
Anders Carlssonc44eec62008-07-03 04:20:39 +00002//
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"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000016#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000017#include "clang/AST/StmtVisitor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/AST/ASTDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000019#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000020#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000021#include "llvm/ADT/SmallString.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000022#include "llvm/Support/Compiler.h"
Mike Stump4572bab2009-05-30 03:56:50 +000023#include <cstring>
24
Anders Carlssonc44eec62008-07-03 04:20:39 +000025using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000026using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000027using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000028
Chris Lattner87eae5e2008-07-11 22:52:41 +000029/// EvalInfo - This is a private struct used by the evaluator to capture
30/// information about a subexpression as it is folded. It retains information
31/// about the AST context, but also maintains information about the folded
32/// expression.
33///
34/// If an expression could be evaluated, it is still possible it is not a C
35/// "integer constant expression" or constant expression. If not, this struct
36/// captures information about how and why not.
37///
38/// One bit of information passed *into* the request for constant folding
39/// indicates whether the subexpression is "evaluated" or not according to C
40/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
41/// evaluate the expression regardless of what the RHS is, but C only allows
42/// certain things in certain situations.
43struct EvalInfo {
44 ASTContext &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Anders Carlsson54da0492008-11-30 16:38:33 +000046 /// EvalResult - Contains information about the evaluation.
47 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000048
Eli Friedmanb2f295c2009-09-13 10:17:44 +000049 /// AnyLValue - Stack based LValue results are not discarded.
50 bool AnyLValue;
51
52 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
53 bool anylvalue = false)
54 : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000055};
56
57
Eli Friedman4efaa272008-11-12 09:44:48 +000058static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000059static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
60static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000062static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000063static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000064
65//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000066// Misc utilities
67//===----------------------------------------------------------------------===//
68
Eli Friedman5bc86102009-06-14 02:17:33 +000069static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
70 // FIXME: Is this accurate for all kinds of bases? If not, what would
71 // the check look like?
72 Result = Value.getLValueBase() || Value.getLValueOffset();
73 return true;
74}
75
Eli Friedman4efaa272008-11-12 09:44:48 +000076static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
77 if (E->getType()->isIntegralType()) {
78 APSInt IntResult;
79 if (!EvaluateInteger(E, IntResult, Info))
80 return false;
81 Result = IntResult != 0;
82 return true;
83 } else if (E->getType()->isRealFloatingType()) {
84 APFloat FloatResult(0.0);
85 if (!EvaluateFloat(E, FloatResult, Info))
86 return false;
87 Result = !FloatResult.isZero();
88 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000089 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000090 APValue PointerResult;
91 if (!EvaluatePointer(E, PointerResult, Info))
92 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000093 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000094 } else if (E->getType()->isAnyComplexType()) {
95 APValue ComplexResult;
96 if (!EvaluateComplex(E, ComplexResult, Info))
97 return false;
98 if (ComplexResult.isComplexFloat()) {
99 Result = !ComplexResult.getComplexFloatReal().isZero() ||
100 !ComplexResult.getComplexFloatImag().isZero();
101 } else {
102 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
103 ComplexResult.getComplexIntImag().getBoolValue();
104 }
105 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000106 }
107
108 return false;
109}
110
Mike Stump1eb44332009-09-09 15:08:12 +0000111static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000112 APFloat &Value, ASTContext &Ctx) {
113 unsigned DestWidth = Ctx.getIntWidth(DestType);
114 // Determine whether we are converting to unsigned or signed.
115 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000117 // FIXME: Warning for overflow.
118 uint64_t Space[4];
119 bool ignored;
120 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
121 llvm::APFloat::rmTowardZero, &ignored);
122 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000126 APFloat &Value, ASTContext &Ctx) {
127 bool ignored;
128 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000130 APFloat::rmNearestTiesToEven, &ignored);
131 return Result;
132}
133
Mike Stump1eb44332009-09-09 15:08:12 +0000134static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000135 APSInt &Value, ASTContext &Ctx) {
136 unsigned DestWidth = Ctx.getIntWidth(DestType);
137 APSInt Result = Value;
138 // Figure out if this is a truncate, extend or noop cast.
139 // If the input is signed, do a sign extend, noop, or truncate.
140 Result.extOrTrunc(DestWidth);
141 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
142 return Result;
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000146 APSInt &Value, ASTContext &Ctx) {
147
148 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
149 Result.convertFromAPInt(Value, Value.isSigned(),
150 APFloat::rmNearestTiesToEven);
151 return Result;
152}
153
Eli Friedman4efaa272008-11-12 09:44:48 +0000154//===----------------------------------------------------------------------===//
155// LValue Evaluation
156//===----------------------------------------------------------------------===//
157namespace {
158class VISIBILITY_HIDDEN LValueExprEvaluator
159 : public StmtVisitor<LValueExprEvaluator, APValue> {
160 EvalInfo &Info;
161public:
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Eli Friedman4efaa272008-11-12 09:44:48 +0000163 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
164
165 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000166 return APValue();
167 }
168
169 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000170 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000171 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000172 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
173 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
174 APValue VisitMemberExpr(MemberExpr *E);
175 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000176 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000177 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000178 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000179 APValue VisitUnaryExtension(const UnaryOperator *E)
180 { return Visit(E->getSubExpr()); }
181 APValue VisitChooseExpr(const ChooseExpr *E)
182 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
183 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000184};
185} // end anonymous namespace
186
187static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
188 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
189 return Result.isLValue();
190}
191
Mike Stump1eb44332009-09-09 15:08:12 +0000192APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000193 if (isa<FunctionDecl>(E->getDecl())) {
194 return APValue(E, 0);
195 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000196 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000197 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000198 if (!VD->getType()->isReferenceType())
199 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000200 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000201 if (VD->getInit())
202 return Visit(VD->getInit());
203 }
204
205 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000209 if (E->hasBlockDeclRefExprs())
210 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Steve Naroff3aaa4822009-04-16 19:02:57 +0000212 return APValue(E, 0);
213}
214
Eli Friedman4efaa272008-11-12 09:44:48 +0000215APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000216 if (!Info.AnyLValue && !E->isFileScope())
217 return APValue();
218 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000219}
220
221APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
222 APValue result;
223 QualType Ty;
224 if (E->isArrow()) {
225 if (!EvaluatePointer(E->getBase(), result, Info))
226 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000227 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 } else {
229 result = Visit(E->getBase());
230 if (result.isUninit())
231 return APValue();
232 Ty = E->getBase()->getType();
233 }
234
Ted Kremenek6217b802009-07-29 21:53:49 +0000235 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000236 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000237
238 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
239 if (!FD) // FIXME: deal with other kinds of member expressions
240 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000241
242 if (FD->getType()->isReferenceType())
243 return APValue();
244
Eli Friedman4efaa272008-11-12 09:44:48 +0000245 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000246 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000247 for (RecordDecl::field_iterator Field = RD->field_begin(),
248 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000249 Field != FieldEnd; (void)++Field, ++i) {
250 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000251 break;
252 }
253
254 result.setLValue(result.getLValueBase(),
255 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
256
257 return result;
258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000261 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson3068d112008-11-16 19:01:22 +0000263 if (!EvaluatePointer(E->getBase(), Result, Info))
264 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Anders Carlsson3068d112008-11-16 19:01:22 +0000266 APSInt Index;
267 if (!EvaluateInteger(E->getIdx(), Index, Info))
268 return APValue();
269
270 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
271
272 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000274 Result.getLValueOffset() + Offset);
275 return Result;
276}
Eli Friedman4efaa272008-11-12 09:44:48 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000279 APValue Result;
280 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
281 return APValue();
282 return Result;
283}
284
Eli Friedman4efaa272008-11-12 09:44:48 +0000285//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000286// Pointer Evaluation
287//===----------------------------------------------------------------------===//
288
Anders Carlssonc754aa62008-07-08 05:13:58 +0000289namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000290class VISIBILITY_HIDDEN PointerExprEvaluator
291 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000292 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000293public:
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner87eae5e2008-07-11 22:52:41 +0000295 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000296
Anders Carlsson2bad1682008-07-08 14:30:00 +0000297 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000298 return APValue();
299 }
300
301 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
302
Anders Carlsson650c92f2008-07-08 15:34:11 +0000303 APValue VisitBinaryOperator(const BinaryOperator *E);
304 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000305 APValue VisitUnaryExtension(const UnaryOperator *E)
306 { return Visit(E->getSubExpr()); }
307 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000308 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
309 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000310 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
311 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000312 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000313 APValue VisitBlockExpr(BlockExpr *E) {
314 if (!E->hasBlockDeclRefExprs())
315 return APValue(E, 0);
316 return APValue();
317 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000318 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
319 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000320 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000321 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000322 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
323 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
324 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000325 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000326};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000327} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000328
Chris Lattner87eae5e2008-07-11 22:52:41 +0000329static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000330 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000331 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000332 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000333 return Result.isLValue();
334}
335
336APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
337 if (E->getOpcode() != BinaryOperator::Add &&
338 E->getOpcode() != BinaryOperator::Sub)
339 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 const Expr *PExp = E->getLHS();
342 const Expr *IExp = E->getRHS();
343 if (IExp->getType()->isPointerType())
344 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000346 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000347 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000348 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000350 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000351 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000352 return APValue();
353
Ted Kremenek6217b802009-07-29 21:53:49 +0000354 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000355 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000357 // Explicitly handle GNU void* and function pointer arithmetic extensions.
358 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
359 SizeOfPointee = 1;
360 else
361 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000362
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000363 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000364
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000365 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000366 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000367 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000368 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
369
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000370 return APValue(ResultLValue.getLValueBase(), Offset);
371}
Eli Friedman4efaa272008-11-12 09:44:48 +0000372
Eli Friedman2217c872009-02-22 11:46:18 +0000373APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
374 APValue result;
375 if (EvaluateLValue(E->getSubExpr(), result, Info))
376 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000377 return APValue();
378}
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000380
Chris Lattnerb542afe2008-07-11 19:10:17 +0000381APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000382 const Expr* SubExpr = E->getSubExpr();
383
384 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000385 if (SubExpr->getType()->isPointerType() ||
386 SubExpr->getType()->isObjCObjectPointerType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000387 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000388 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000389 return Result;
390 return APValue();
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000393 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000394 APValue Result;
395 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
396 return APValue();
397
398 if (Result.isInt()) {
399 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
400 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000403 // Cast is of an lvalue, no need to change value.
404 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000405 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000406
407 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000408 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000409 SubExpr->getType()->isArrayType()) {
410 APValue Result;
411 if (EvaluateLValue(SubExpr, Result, Info))
412 return Result;
413 return APValue();
414 }
415
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000417}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000418
Eli Friedman3941b182009-01-25 01:54:01 +0000419APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000420 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000421 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000422 return APValue(E, 0);
423 return APValue();
424}
425
Eli Friedman4efaa272008-11-12 09:44:48 +0000426APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
427 bool BoolResult;
428 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
429 return APValue();
430
431 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
432
433 APValue Result;
434 if (EvaluatePointer(EvalExpr, Result, Info))
435 return Result;
436 return APValue();
437}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000438
439//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000440// Vector Evaluation
441//===----------------------------------------------------------------------===//
442
443namespace {
444 class VISIBILITY_HIDDEN VectorExprEvaluator
445 : public StmtVisitor<VectorExprEvaluator, APValue> {
446 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000447 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000448 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Nate Begeman59b5da62009-01-18 03:20:47 +0000450 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Nate Begeman59b5da62009-01-18 03:20:47 +0000452 APValue VisitStmt(Stmt *S) {
453 return APValue();
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Eli Friedman91110ee2009-02-23 04:23:56 +0000456 APValue VisitParenExpr(ParenExpr *E)
457 { return Visit(E->getSubExpr()); }
458 APValue VisitUnaryExtension(const UnaryOperator *E)
459 { return Visit(E->getSubExpr()); }
460 APValue VisitUnaryPlus(const UnaryOperator *E)
461 { return Visit(E->getSubExpr()); }
462 APValue VisitUnaryReal(const UnaryOperator *E)
463 { return Visit(E->getSubExpr()); }
464 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
465 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000466 APValue VisitCastExpr(const CastExpr* E);
467 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
468 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000469 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000470 APValue VisitChooseExpr(const ChooseExpr *E)
471 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000472 APValue VisitUnaryImag(const UnaryOperator *E);
473 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000474 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000475 // shufflevector, ExtVectorElementExpr
476 // (Note that these require implementing conversions
477 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000478 };
479} // end anonymous namespace
480
481static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
482 if (!E->getType()->isVectorType())
483 return false;
484 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
485 return !Result.isUninit();
486}
487
488APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
Nate Begemanc0b8b192009-07-01 07:50:47 +0000489 const VectorType *VTy = E->getType()->getAsVectorType();
490 QualType EltTy = VTy->getElementType();
491 unsigned NElts = VTy->getNumElements();
492 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Nate Begeman59b5da62009-01-18 03:20:47 +0000494 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000495 QualType SETy = SE->getType();
496 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000497
Nate Begemane8c9e922009-06-26 18:22:18 +0000498 // Check for vector->vector bitcast and scalar->vector splat.
499 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000500 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000501 } else if (SETy->isIntegerType()) {
502 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000503 if (!EvaluateInteger(SE, IntResult, Info))
504 return APValue();
505 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000506 } else if (SETy->isRealFloatingType()) {
507 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000508 if (!EvaluateFloat(SE, F, Info))
509 return APValue();
510 Result = APValue(F);
511 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000512 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000513
Nate Begemanc0b8b192009-07-01 07:50:47 +0000514 // For casts of a scalar to ExtVector, convert the scalar to the element type
515 // and splat it to all elements.
516 if (E->getType()->isExtVectorType()) {
517 if (EltTy->isIntegerType() && Result.isInt())
518 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
519 Info.Ctx));
520 else if (EltTy->isIntegerType())
521 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
522 Info.Ctx));
523 else if (EltTy->isRealFloatingType() && Result.isInt())
524 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
525 Info.Ctx));
526 else if (EltTy->isRealFloatingType())
527 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
528 Info.Ctx));
529 else
530 return APValue();
531
532 // Splat and create vector APValue.
533 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
534 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000535 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000536
537 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
538 // to the vector. To construct the APValue vector initializer, bitcast the
539 // initializing value to an APInt, and shift out the bits pertaining to each
540 // element.
541 APSInt Init;
542 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Nate Begemanc0b8b192009-07-01 07:50:47 +0000544 llvm::SmallVector<APValue, 4> Elts;
545 for (unsigned i = 0; i != NElts; ++i) {
546 APSInt Tmp = Init;
547 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Nate Begemanc0b8b192009-07-01 07:50:47 +0000549 if (EltTy->isIntegerType())
550 Elts.push_back(APValue(Tmp));
551 else if (EltTy->isRealFloatingType())
552 Elts.push_back(APValue(APFloat(Tmp)));
553 else
554 return APValue();
555
556 Init >>= EltWidth;
557 }
558 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000559}
560
Mike Stump1eb44332009-09-09 15:08:12 +0000561APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000562VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
563 return this->Visit(const_cast<Expr*>(E->getInitializer()));
564}
565
Mike Stump1eb44332009-09-09 15:08:12 +0000566APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000567VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
568 const VectorType *VT = E->getType()->getAsVectorType();
569 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000570 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Nate Begeman59b5da62009-01-18 03:20:47 +0000572 QualType EltTy = VT->getElementType();
573 llvm::SmallVector<APValue, 4> Elements;
574
Eli Friedman91110ee2009-02-23 04:23:56 +0000575 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000576 if (EltTy->isIntegerType()) {
577 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000578 if (i < NumInits) {
579 if (!EvaluateInteger(E->getInit(i), sInt, Info))
580 return APValue();
581 } else {
582 sInt = Info.Ctx.MakeIntValue(0, EltTy);
583 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000584 Elements.push_back(APValue(sInt));
585 } else {
586 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000587 if (i < NumInits) {
588 if (!EvaluateFloat(E->getInit(i), f, Info))
589 return APValue();
590 } else {
591 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
592 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000593 Elements.push_back(APValue(f));
594 }
595 }
596 return APValue(&Elements[0], Elements.size());
597}
598
Mike Stump1eb44332009-09-09 15:08:12 +0000599APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000600VectorExprEvaluator::GetZeroVector(QualType T) {
601 const VectorType *VT = T->getAsVectorType();
602 QualType EltTy = VT->getElementType();
603 APValue ZeroElement;
604 if (EltTy->isIntegerType())
605 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
606 else
607 ZeroElement =
608 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
609
610 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
611 return APValue(&Elements[0], Elements.size());
612}
613
614APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
615 bool BoolResult;
616 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
617 return APValue();
618
619 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
620
621 APValue Result;
622 if (EvaluateVector(EvalExpr, Result, Info))
623 return Result;
624 return APValue();
625}
626
Eli Friedman91110ee2009-02-23 04:23:56 +0000627APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
628 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
629 Info.EvalResult.HasSideEffects = true;
630 return GetZeroVector(E->getType());
631}
632
Nate Begeman59b5da62009-01-18 03:20:47 +0000633//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000634// Integer Evaluation
635//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000636
637namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000638class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000639 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000640 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000641 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000642public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000643 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000644 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000645
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000646 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000647 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000648 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000649 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000650 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000651 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000652 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000653 return true;
654 }
655
Daniel Dunbar131eb432009-02-19 09:06:44 +0000656 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000657 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000658 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000659 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000660 Result = APValue(APSInt(I));
661 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000662 return true;
663 }
664
665 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000666 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000667 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000668 return true;
669 }
670
Anders Carlsson82206e22008-11-30 18:14:57 +0000671 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000672 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000673 if (Info.EvalResult.Diag == 0) {
674 Info.EvalResult.DiagLoc = L;
675 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000676 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000677 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000678 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlssonc754aa62008-07-08 05:13:58 +0000681 //===--------------------------------------------------------------------===//
682 // Visitor Methods
683 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Chris Lattner32fea9d2008-11-12 07:43:42 +0000685 bool VisitStmt(Stmt *) {
686 assert(0 && "This should be called on integers, stmts are not integers");
687 return false;
688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Chris Lattner32fea9d2008-11-12 07:43:42 +0000690 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000691 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattnerb542afe2008-07-11 19:10:17 +0000694 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000695
Chris Lattner4c4867e2008-07-12 00:38:25 +0000696 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000697 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000698 }
699 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000700 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000701 }
702 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000703 // Per gcc docs "this built-in function ignores top level
704 // qualifiers". We need to use the canonical version to properly
705 // be able to strip CRV qualifiers from the type.
706 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
707 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000708 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000709 T1.getUnqualifiedType()),
710 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000711 }
712 bool VisitDeclRefExpr(const DeclRefExpr *E);
713 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000714 bool VisitBinaryOperator(const BinaryOperator *E);
715 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000716 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000717
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000718 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000719 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
720
Anders Carlsson3068d112008-11-16 19:01:22 +0000721 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000722 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Anders Carlsson3f704562008-12-21 22:39:40 +0000725 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000726 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Anders Carlsson3068d112008-11-16 19:01:22 +0000729 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000730 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000731 }
732
Eli Friedman664a1042009-02-27 04:45:43 +0000733 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
734 return Success(0, E);
735 }
736
Sebastian Redl64b45f72009-01-05 20:52:13 +0000737 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000738 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000739 }
740
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000741 bool VisitChooseExpr(const ChooseExpr *E) {
742 return Visit(E->getChosenSubExpr(Info.Ctx));
743 }
744
Eli Friedman722c7172009-02-28 03:59:05 +0000745 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000746 bool VisitUnaryImag(const UnaryOperator *E);
747
Chris Lattnerfcee0012008-07-11 21:24:13 +0000748private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000749 unsigned GetAlignOfExpr(const Expr *E);
750 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000751 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000752};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000753} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000754
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000755static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000756 if (!E->getType()->isIntegralType())
757 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000759 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
760}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000761
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000762static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
763 APValue Val;
764 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
765 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000766 Result = Val.getInt();
767 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000768}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000769
Chris Lattner4c4867e2008-07-12 00:38:25 +0000770bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
771 // Enums are integer constant exprs.
772 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000773 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000774 // signedness consistently; see PR3173.
775 APSInt SI = D->getInitVal();
776 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
777 // FIXME: This is an ugly hack around the fact that enums don't
778 // set their width (!?!) consistently; see PR3173.
779 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
780 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000781 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000782
783 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000784 // In C, they can also be folded, although they are not ICEs.
785 if (E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000786 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000787 if (APValue *V = D->getEvaluatedValue())
788 return Success(V->getInt(), E);
789 if (const Expr *Init = D->getInit()) {
790 if (Visit(const_cast<Expr*>(Init))) {
791 // Cache the evaluated value in the variable declaration.
792 D->setEvaluatedValue(Info.Ctx, Result);
793 return true;
794 }
795
796 return false;
797 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000798 }
799 }
800
Chris Lattner4c4867e2008-07-12 00:38:25 +0000801 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000802 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000803}
804
Chris Lattnera4d55d82008-10-06 06:40:35 +0000805/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
806/// as GCC.
807static int EvaluateBuiltinClassifyType(const CallExpr *E) {
808 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000809 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000810 enum gcc_type_class {
811 no_type_class = -1,
812 void_type_class, integer_type_class, char_type_class,
813 enumeral_type_class, boolean_type_class,
814 pointer_type_class, reference_type_class, offset_type_class,
815 real_type_class, complex_type_class,
816 function_type_class, method_type_class,
817 record_type_class, union_type_class,
818 array_type_class, string_type_class,
819 lang_type_class
820 };
Mike Stump1eb44332009-09-09 15:08:12 +0000821
822 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000823 // ideal, however it is what gcc does.
824 if (E->getNumArgs() == 0)
825 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Chris Lattnera4d55d82008-10-06 06:40:35 +0000827 QualType ArgTy = E->getArg(0)->getType();
828 if (ArgTy->isVoidType())
829 return void_type_class;
830 else if (ArgTy->isEnumeralType())
831 return enumeral_type_class;
832 else if (ArgTy->isBooleanType())
833 return boolean_type_class;
834 else if (ArgTy->isCharType())
835 return string_type_class; // gcc doesn't appear to use char_type_class
836 else if (ArgTy->isIntegerType())
837 return integer_type_class;
838 else if (ArgTy->isPointerType())
839 return pointer_type_class;
840 else if (ArgTy->isReferenceType())
841 return reference_type_class;
842 else if (ArgTy->isRealType())
843 return real_type_class;
844 else if (ArgTy->isComplexType())
845 return complex_type_class;
846 else if (ArgTy->isFunctionType())
847 return function_type_class;
848 else if (ArgTy->isStructureType())
849 return record_type_class;
850 else if (ArgTy->isUnionType())
851 return union_type_class;
852 else if (ArgTy->isArrayType())
853 return array_type_class;
854 else if (ArgTy->isUnionType())
855 return union_type_class;
856 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
857 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
858 return -1;
859}
860
Chris Lattner4c4867e2008-07-12 00:38:25 +0000861bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000862 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000863 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000864 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000865 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000866 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000868 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000869 // __builtin_constant_p always has one operand: it returns true if that
870 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000871 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000872 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000873}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000874
Chris Lattnerb542afe2008-07-11 19:10:17 +0000875bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000876 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000877 if (!Visit(E->getRHS()))
878 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000879
Eli Friedman33ef1452009-02-26 10:19:36 +0000880 // If we can't evaluate the LHS, it might have side effects;
881 // conservatively mark it.
882 if (!E->getLHS()->isEvaluatable(Info.Ctx))
883 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000884
Anders Carlsson027f62e2008-12-01 02:07:06 +0000885 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000886 }
887
888 if (E->isLogicalOp()) {
889 // These need to be handled specially because the operands aren't
890 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000891 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000893 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000894 // We were able to evaluate the LHS, see if we can get away with not
895 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000896 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000897 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000898
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000899 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000900 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000901 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000902 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000903 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000904 }
905 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000906 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000907 // We can't evaluate the LHS; however, sometimes the result
908 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +0000909 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000910 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000911 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000912 // must have had side effects.
913 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000914
915 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000916 }
917 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000918 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000919
Eli Friedmana6afa762008-11-13 06:09:17 +0000920 return false;
921 }
922
Anders Carlsson286f85e2008-11-16 07:17:21 +0000923 QualType LHSTy = E->getLHS()->getType();
924 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000925
926 if (LHSTy->isAnyComplexType()) {
927 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
928 APValue LHS, RHS;
929
930 if (!EvaluateComplex(E->getLHS(), LHS, Info))
931 return false;
932
933 if (!EvaluateComplex(E->getRHS(), RHS, Info))
934 return false;
935
936 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000937 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000938 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +0000939 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +0000940 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
941
Daniel Dunbar4087e242009-01-29 06:43:41 +0000942 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000943 return Success((CR_r == APFloat::cmpEqual &&
944 CR_i == APFloat::cmpEqual), E);
945 else {
946 assert(E->getOpcode() == BinaryOperator::NE &&
947 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +0000948 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000949 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000950 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +0000951 CR_i == APFloat::cmpLessThan)), E);
952 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000953 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000954 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000955 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
956 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
957 else {
958 assert(E->getOpcode() == BinaryOperator::NE &&
959 "Invalid compex comparison.");
960 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
961 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
962 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000963 }
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Anders Carlsson286f85e2008-11-16 07:17:21 +0000966 if (LHSTy->isRealFloatingType() &&
967 RHSTy->isRealFloatingType()) {
968 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Anders Carlsson286f85e2008-11-16 07:17:21 +0000970 if (!EvaluateFloat(E->getRHS(), RHS, Info))
971 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Anders Carlsson286f85e2008-11-16 07:17:21 +0000973 if (!EvaluateFloat(E->getLHS(), LHS, Info))
974 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Anders Carlsson286f85e2008-11-16 07:17:21 +0000976 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000977
Anders Carlsson286f85e2008-11-16 07:17:21 +0000978 switch (E->getOpcode()) {
979 default:
980 assert(0 && "Invalid binary operator!");
981 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000982 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000983 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000984 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000985 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000986 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000987 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000988 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +0000989 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000990 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000991 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000992 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +0000993 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +0000994 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000995 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000998 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
999 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001000 APValue LHSValue;
1001 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1002 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001003
Anders Carlsson3068d112008-11-16 19:01:22 +00001004 APValue RHSValue;
1005 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1006 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001007
Eli Friedman5bc86102009-06-14 02:17:33 +00001008 // Reject any bases from the normal codepath; we special-case comparisons
1009 // to null.
1010 if (LHSValue.getLValueBase()) {
1011 if (!E->isEqualityOp())
1012 return false;
1013 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1014 return false;
1015 bool bres;
1016 if (!EvalPointerValueAsBool(LHSValue, bres))
1017 return false;
1018 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1019 } else if (RHSValue.getLValueBase()) {
1020 if (!E->isEqualityOp())
1021 return false;
1022 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1023 return false;
1024 bool bres;
1025 if (!EvalPointerValueAsBool(RHSValue, bres))
1026 return false;
1027 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1028 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001029
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001030 if (E->getOpcode() == BinaryOperator::Sub) {
1031 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001032 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001033
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001034 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001035 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1036 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001037
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001038 return Success(D, E);
1039 }
1040 bool Result;
1041 if (E->getOpcode() == BinaryOperator::EQ) {
1042 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001043 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001044 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1045 }
1046 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001047 }
1048 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001049 if (!LHSTy->isIntegralType() ||
1050 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001051 // We can't continue from here for non-integral types, and they
1052 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001053 return false;
1054 }
1055
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001056 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001057 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001058 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001059
Eli Friedman42edd0d2009-03-24 01:14:50 +00001060 APValue RHSVal;
1061 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001062 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001063
1064 // Handle cases like (unsigned long)&a + 4.
1065 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1066 uint64_t offset = Result.getLValueOffset();
1067 if (E->getOpcode() == BinaryOperator::Add)
1068 offset += RHSVal.getInt().getZExtValue();
1069 else
1070 offset -= RHSVal.getInt().getZExtValue();
1071 Result = APValue(Result.getLValueBase(), offset);
1072 return true;
1073 }
1074
1075 // Handle cases like 4 + (unsigned long)&a
1076 if (E->getOpcode() == BinaryOperator::Add &&
1077 RHSVal.isLValue() && Result.isInt()) {
1078 uint64_t offset = RHSVal.getLValueOffset();
1079 offset += Result.getInt().getZExtValue();
1080 Result = APValue(RHSVal.getLValueBase(), offset);
1081 return true;
1082 }
1083
1084 // All the following cases expect both operands to be an integer
1085 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001086 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001087
Eli Friedman42edd0d2009-03-24 01:14:50 +00001088 APSInt& RHS = RHSVal.getInt();
1089
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001090 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001091 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001092 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001093 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1094 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1095 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1096 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1097 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1098 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001099 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001100 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001101 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001102 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001103 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001104 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001105 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001106 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001107 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001108 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001109 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001110 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1111 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001112 }
1113 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001114 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001115 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1116 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001119 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1120 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1121 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1122 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1123 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1124 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001125 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001126}
1127
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001128bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001129 bool Cond;
1130 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001131 return false;
1132
Nuno Lopesa25bd552008-11-16 22:06:39 +00001133 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001134}
1135
Chris Lattneraf707ab2009-01-24 21:53:27 +00001136unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001137 // Get information about the alignment.
1138 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001139
Eli Friedman2be58612009-05-30 21:09:44 +00001140 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001141 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001142}
1143
Chris Lattneraf707ab2009-01-24 21:53:27 +00001144unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1145 E = E->IgnoreParens();
1146
1147 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001148 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001149 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001150 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001151
Chris Lattneraf707ab2009-01-24 21:53:27 +00001152 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001153 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001154
Chris Lattnere9feb472009-01-24 21:09:06 +00001155 return GetAlignOfType(E->getType());
1156}
1157
1158
Sebastian Redl05189992008-11-11 17:56:53 +00001159/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1160/// expression's type.
1161bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1162 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001163
Chris Lattnere9feb472009-01-24 21:09:06 +00001164 // Handle alignof separately.
1165 if (!E->isSizeOf()) {
1166 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001167 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001168 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001169 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001170 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001171
Sebastian Redl05189992008-11-11 17:56:53 +00001172 QualType SrcTy = E->getTypeOfArgument();
1173
Daniel Dunbar131eb432009-02-19 09:06:44 +00001174 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1175 // extension.
1176 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1177 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001178
Chris Lattnerfcee0012008-07-11 21:24:13 +00001179 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001180 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001181 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001182
Chris Lattnere9feb472009-01-24 21:09:06 +00001183 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001184 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001185 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001186}
1187
Chris Lattnerb542afe2008-07-11 19:10:17 +00001188bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001189 // Special case unary operators that do not need their subexpression
1190 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001191 if (E->isOffsetOfOp()) {
1192 // The AST for offsetof is defined in such a way that we can just
1193 // directly Evaluate it as an l-value.
1194 APValue LV;
1195 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1196 return false;
1197 if (LV.getLValueBase())
1198 return false;
1199 return Success(LV.getLValueOffset(), E);
1200 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001201
1202 if (E->getOpcode() == UnaryOperator::LNot) {
1203 // LNot's operand isn't necessarily an integer, so we handle it specially.
1204 bool bres;
1205 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1206 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001207 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001208 }
1209
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001210 // Only handle integral operations...
1211 if (!E->getSubExpr()->getType()->isIntegralType())
1212 return false;
1213
Chris Lattner87eae5e2008-07-11 22:52:41 +00001214 // Get the operand value into 'Result'.
1215 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001216 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001217
Chris Lattner75a48812008-07-11 22:15:16 +00001218 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001219 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001220 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1221 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001222 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001223 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001224 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1225 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001226 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001227 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001228 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001229 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001230 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001231 if (!Result.isInt()) return false;
1232 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001233 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001234 if (!Result.isInt()) return false;
1235 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001236 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001237}
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Chris Lattner732b2232008-07-12 01:15:53 +00001239/// HandleCast - This is used to evaluate implicit or explicit casts where the
1240/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001241bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001242 Expr *SubExpr = E->getSubExpr();
1243 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001244 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001245
Eli Friedman4efaa272008-11-12 09:44:48 +00001246 if (DestType->isBooleanType()) {
1247 bool BoolResult;
1248 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1249 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001250 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001251 }
1252
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001253 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001254 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001255 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001256 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001257
Eli Friedmanbe265702009-02-20 01:15:07 +00001258 if (!Result.isInt()) {
1259 // Only allow casts of lvalues if they are lossless.
1260 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1261 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001262
Daniel Dunbardd211642009-02-19 22:24:01 +00001263 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001264 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner732b2232008-07-12 01:15:53 +00001267 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001268 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001269 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001270 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001271 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001272
Daniel Dunbardd211642009-02-19 22:24:01 +00001273 if (LV.getLValueBase()) {
1274 // Only allow based lvalue casts if they are lossless.
1275 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1276 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001277
Daniel Dunbardd211642009-02-19 22:24:01 +00001278 Result = LV;
1279 return true;
1280 }
1281
1282 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1283 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001284 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001285
Eli Friedmanbe265702009-02-20 01:15:07 +00001286 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1287 // This handles double-conversion cases, where there's both
1288 // an l-value promotion and an implicit conversion to int.
1289 APValue LV;
1290 if (!EvaluateLValue(SubExpr, LV, Info))
1291 return false;
1292
1293 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1294 return false;
1295
1296 Result = LV;
1297 return true;
1298 }
1299
Eli Friedman1725f682009-04-22 19:23:09 +00001300 if (SrcType->isAnyComplexType()) {
1301 APValue C;
1302 if (!EvaluateComplex(SubExpr, C, Info))
1303 return false;
1304 if (C.isComplexFloat())
1305 return Success(HandleFloatToIntCast(DestType, SrcType,
1306 C.getComplexFloatReal(), Info.Ctx),
1307 E);
1308 else
1309 return Success(HandleIntToIntCast(DestType, SrcType,
1310 C.getComplexIntReal(), Info.Ctx), E);
1311 }
Eli Friedman2217c872009-02-22 11:46:18 +00001312 // FIXME: Handle vectors
1313
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001314 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001315 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001316
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001317 APFloat F(0.0);
1318 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001319 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001321 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001322}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001323
Eli Friedman722c7172009-02-28 03:59:05 +00001324bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1325 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1326 APValue LV;
1327 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1328 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1329 return Success(LV.getComplexIntReal(), E);
1330 }
1331
1332 return Visit(E->getSubExpr());
1333}
1334
Eli Friedman664a1042009-02-27 04:45:43 +00001335bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001336 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1337 APValue LV;
1338 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1339 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1340 return Success(LV.getComplexIntImag(), E);
1341 }
1342
Eli Friedman664a1042009-02-27 04:45:43 +00001343 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1344 Info.EvalResult.HasSideEffects = true;
1345 return Success(0, E);
1346}
1347
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001348//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001349// Float Evaluation
1350//===----------------------------------------------------------------------===//
1351
1352namespace {
1353class VISIBILITY_HIDDEN FloatExprEvaluator
1354 : public StmtVisitor<FloatExprEvaluator, bool> {
1355 EvalInfo &Info;
1356 APFloat &Result;
1357public:
1358 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1359 : Info(info), Result(result) {}
1360
1361 bool VisitStmt(Stmt *S) {
1362 return false;
1363 }
1364
1365 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001366 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001367
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001368 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001369 bool VisitBinaryOperator(const BinaryOperator *E);
1370 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001371 bool VisitCastExpr(CastExpr *E);
1372 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001373
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001374 bool VisitChooseExpr(const ChooseExpr *E)
1375 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1376 bool VisitUnaryExtension(const UnaryOperator *E)
1377 { return Visit(E->getSubExpr()); }
1378
1379 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1380 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001381 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001382};
1383} // end anonymous namespace
1384
1385static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1386 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1387}
1388
Chris Lattner019f4e82008-10-06 05:28:25 +00001389bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001390 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001391 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001392 case Builtin::BI__builtin_huge_val:
1393 case Builtin::BI__builtin_huge_valf:
1394 case Builtin::BI__builtin_huge_vall:
1395 case Builtin::BI__builtin_inf:
1396 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001397 case Builtin::BI__builtin_infl: {
1398 const llvm::fltSemantics &Sem =
1399 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001400 Result = llvm::APFloat::getInf(Sem);
1401 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Chris Lattner9e621712008-10-06 06:31:58 +00001404 case Builtin::BI__builtin_nan:
1405 case Builtin::BI__builtin_nanf:
1406 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001407 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001408 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001409 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001410 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001411 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001412 const llvm::fltSemantics &Sem =
1413 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001414 llvm::SmallString<16> s;
1415 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1416 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001417 long l;
1418 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001419 l = strtol(&s[0], &endp, 0);
1420 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001421 return false;
1422 unsigned type = (unsigned int)l;;
1423 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001424 return true;
1425 }
1426 }
1427 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001428
1429 case Builtin::BI__builtin_fabs:
1430 case Builtin::BI__builtin_fabsf:
1431 case Builtin::BI__builtin_fabsl:
1432 if (!EvaluateFloat(E->getArg(0), Result, Info))
1433 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001435 if (Result.isNegative())
1436 Result.changeSign();
1437 return true;
1438
Mike Stump1eb44332009-09-09 15:08:12 +00001439 case Builtin::BI__builtin_copysign:
1440 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001441 case Builtin::BI__builtin_copysignl: {
1442 APFloat RHS(0.);
1443 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1444 !EvaluateFloat(E->getArg(1), RHS, Info))
1445 return false;
1446 Result.copySign(RHS);
1447 return true;
1448 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001449 }
1450}
1451
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001452bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001453 if (E->getOpcode() == UnaryOperator::Deref)
1454 return false;
1455
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001456 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1457 return false;
1458
1459 switch (E->getOpcode()) {
1460 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001461 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001462 return true;
1463 case UnaryOperator::Minus:
1464 Result.changeSign();
1465 return true;
1466 }
1467}
Chris Lattner019f4e82008-10-06 05:28:25 +00001468
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001469bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1470 // FIXME: Diagnostics? I really don't understand how the warnings
1471 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001472 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001473 if (!EvaluateFloat(E->getLHS(), Result, Info))
1474 return false;
1475 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1476 return false;
1477
1478 switch (E->getOpcode()) {
1479 default: return false;
1480 case BinaryOperator::Mul:
1481 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1482 return true;
1483 case BinaryOperator::Add:
1484 Result.add(RHS, APFloat::rmNearestTiesToEven);
1485 return true;
1486 case BinaryOperator::Sub:
1487 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1488 return true;
1489 case BinaryOperator::Div:
1490 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1491 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001492 }
1493}
1494
1495bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1496 Result = E->getValue();
1497 return true;
1498}
1499
Eli Friedman4efaa272008-11-12 09:44:48 +00001500bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1501 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Eli Friedman4efaa272008-11-12 09:44:48 +00001503 if (SubExpr->getType()->isIntegralType()) {
1504 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001505 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001506 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001507 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001508 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001509 return true;
1510 }
1511 if (SubExpr->getType()->isRealFloatingType()) {
1512 if (!Visit(SubExpr))
1513 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001514 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1515 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001516 return true;
1517 }
Eli Friedman2217c872009-02-22 11:46:18 +00001518 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001519
1520 return false;
1521}
1522
1523bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1524 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1525 return true;
1526}
1527
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001528//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001529// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001530//===----------------------------------------------------------------------===//
1531
1532namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001533class VISIBILITY_HIDDEN ComplexExprEvaluator
1534 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001535 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001537public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001538 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001540 //===--------------------------------------------------------------------===//
1541 // Visitor Methods
1542 //===--------------------------------------------------------------------===//
1543
1544 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001545 return APValue();
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001548 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1549
1550 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001551 Expr* SubExpr = E->getSubExpr();
1552
1553 if (SubExpr->getType()->isRealFloatingType()) {
1554 APFloat Result(0.0);
1555
1556 if (!EvaluateFloat(SubExpr, Result, Info))
1557 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001558
1559 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001560 Result);
1561 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001562 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001563 "Unexpected imaginary literal.");
1564
1565 llvm::APSInt Result;
1566 if (!EvaluateInteger(SubExpr, Result, Info))
1567 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001569 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1570 Zero = 0;
1571 return APValue(Zero, Result);
1572 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001573 }
1574
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001575 APValue VisitCastExpr(CastExpr *E) {
1576 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001577 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1578 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001579
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001580 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001581 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001582
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001583 if (!EvaluateFloat(SubExpr, Result, Info))
1584 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001585
1586 if (EltType->isRealFloatingType()) {
1587 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001588 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001589 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1590 } else {
1591 llvm::APSInt IResult;
1592 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1593 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1594 Zero = 0;
1595 return APValue(IResult, Zero);
1596 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001597 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001598 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001599
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001600 if (!EvaluateInteger(SubExpr, Result, Info))
1601 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001602
Eli Friedman1725f682009-04-22 19:23:09 +00001603 if (EltType->isRealFloatingType()) {
1604 APFloat FResult =
1605 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001606 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001607 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1608 } else {
1609 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1610 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1611 Zero = 0;
1612 return APValue(Result, Zero);
1613 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001614 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1615 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001616
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001617 if (!EvaluateComplex(SubExpr, Src, Info))
1618 return APValue();
1619
1620 QualType SrcType = CT->getElementType();
1621
1622 if (Src.isComplexFloat()) {
1623 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001624 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001625 Src.getComplexFloatReal(),
1626 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001627 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001628 Src.getComplexFloatImag(),
1629 Info.Ctx));
1630 } else {
1631 return APValue(HandleFloatToIntCast(EltType, SrcType,
1632 Src.getComplexFloatReal(),
1633 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001634 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001635 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001636 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001637 }
1638 } else {
1639 assert(Src.isComplexInt() && "Invalid evaluate result.");
1640 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001641 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001642 Src.getComplexIntReal(),
1643 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001644 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001645 Src.getComplexIntImag(),
1646 Info.Ctx));
1647 } else {
1648 return APValue(HandleIntToIntCast(EltType, SrcType,
1649 Src.getComplexIntReal(),
1650 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001651 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001652 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001653 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001654 }
1655 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001656 }
1657
1658 // FIXME: Handle more casts.
1659 return APValue();
1660 }
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001662 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001663 APValue VisitChooseExpr(const ChooseExpr *E)
1664 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1665 APValue VisitUnaryExtension(const UnaryOperator *E)
1666 { return Visit(E->getSubExpr()); }
1667 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001668 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001669};
1670} // end anonymous namespace
1671
Mike Stump1eb44332009-09-09 15:08:12 +00001672static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001673 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1674 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001675 (&Result.getComplexFloatReal().getSemantics() ==
1676 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001677 "Invalid complex evaluation.");
1678 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001679}
1680
Mike Stump1eb44332009-09-09 15:08:12 +00001681APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001682 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001684 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001685 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001687 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001688 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001689
Daniel Dunbar3f279872009-01-29 01:32:56 +00001690 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1691 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001692 switch (E->getOpcode()) {
1693 default: return APValue();
1694 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001695 if (Result.isComplexFloat()) {
1696 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1697 APFloat::rmNearestTiesToEven);
1698 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1699 APFloat::rmNearestTiesToEven);
1700 } else {
1701 Result.getComplexIntReal() += RHS.getComplexIntReal();
1702 Result.getComplexIntImag() += RHS.getComplexIntImag();
1703 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001704 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001705 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001706 if (Result.isComplexFloat()) {
1707 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1708 APFloat::rmNearestTiesToEven);
1709 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1710 APFloat::rmNearestTiesToEven);
1711 } else {
1712 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1713 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1714 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001715 break;
1716 case BinaryOperator::Mul:
1717 if (Result.isComplexFloat()) {
1718 APValue LHS = Result;
1719 APFloat &LHS_r = LHS.getComplexFloatReal();
1720 APFloat &LHS_i = LHS.getComplexFloatImag();
1721 APFloat &RHS_r = RHS.getComplexFloatReal();
1722 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Daniel Dunbar3f279872009-01-29 01:32:56 +00001724 APFloat Tmp = LHS_r;
1725 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1726 Result.getComplexFloatReal() = Tmp;
1727 Tmp = LHS_i;
1728 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1729 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1730
1731 Tmp = LHS_r;
1732 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1733 Result.getComplexFloatImag() = Tmp;
1734 Tmp = LHS_i;
1735 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1736 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1737 } else {
1738 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001739 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001740 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1741 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001742 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001743 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1744 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1745 }
1746 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001747 }
1748
1749 return Result;
1750}
1751
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001752//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001753// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001754//===----------------------------------------------------------------------===//
1755
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001756/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001757/// any crazy technique (that has nothing to do with language standards) that
1758/// we want to. If this function returns true, it returns the folded constant
1759/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001760bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1761 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001762
Nate Begeman59b5da62009-01-18 03:20:47 +00001763 if (getType()->isVectorType()) {
1764 if (!EvaluateVector(this, Result.Val, Info))
1765 return false;
1766 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001767 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001768 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001769 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001770 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001771 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001772 } else if (getType()->isRealFloatingType()) {
1773 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001774 if (!EvaluateFloat(this, f, Info))
1775 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001777 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001778 } else if (getType()->isAnyComplexType()) {
1779 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001780 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001781 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001782 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001783
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001784 return true;
1785}
1786
Anders Carlsson1b782762009-04-10 04:54:13 +00001787bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1788 EvalInfo Info(Ctx, Result);
1789
1790 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1791}
1792
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001793bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1794 EvalInfo Info(Ctx, Result, true);
1795
1796 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1797}
1798
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001799/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001800/// folded, but discard the result.
1801bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001802 EvalResult Result;
1803 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001804}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001805
1806APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001807 EvalResult EvalResult;
1808 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001809 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001810 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001811 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001812
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001813 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001814}