blob: fce59133c105371429cd1e8d6db7877ed3bea548 [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"
Eli Friedman4efaa272008-11-12 09:44:48 +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"
Anders Carlsson06a36752008-07-08 05:49:43 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlssonc754aa62008-07-08 05:13:58 +000020#include "llvm/Support/Compiler.h"
Anders Carlssonc44eec62008-07-03 04:20:39 +000021using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000022using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000023using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000024
Chris Lattner87eae5e2008-07-11 22:52:41 +000025/// EvalInfo - This is a private struct used by the evaluator to capture
26/// information about a subexpression as it is folded. It retains information
27/// about the AST context, but also maintains information about the folded
28/// expression.
29///
30/// If an expression could be evaluated, it is still possible it is not a C
31/// "integer constant expression" or constant expression. If not, this struct
32/// captures information about how and why not.
33///
34/// One bit of information passed *into* the request for constant folding
35/// indicates whether the subexpression is "evaluated" or not according to C
36/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
37/// evaluate the expression regardless of what the RHS is, but C only allows
38/// certain things in certain situations.
39struct EvalInfo {
40 ASTContext &Ctx;
41
Anders Carlsson54da0492008-11-30 16:38:33 +000042 /// EvalResult - Contains information about the evaluation.
43 Expr::EvalResult &EvalResult;
Anders Carlssonf0c1e4b2008-11-30 18:26:25 +000044
Anders Carlsson54da0492008-11-30 16:38:33 +000045 EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
Eli Friedman33ef1452009-02-26 10:19:36 +000046 EvalResult(evalresult) {}
Chris Lattner87eae5e2008-07-11 22:52:41 +000047};
48
49
Eli Friedman4efaa272008-11-12 09:44:48 +000050static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +000051static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
52static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000053static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000054static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000055static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000056
57//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000058// Misc utilities
59//===----------------------------------------------------------------------===//
60
61static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
62 if (E->getType()->isIntegralType()) {
63 APSInt IntResult;
64 if (!EvaluateInteger(E, IntResult, Info))
65 return false;
66 Result = IntResult != 0;
67 return true;
68 } else if (E->getType()->isRealFloatingType()) {
69 APFloat FloatResult(0.0);
70 if (!EvaluateFloat(E, FloatResult, Info))
71 return false;
72 Result = !FloatResult.isZero();
73 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000074 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000075 APValue PointerResult;
76 if (!EvaluatePointer(E, PointerResult, Info))
77 return false;
78 // FIXME: Is this accurate for all kinds of bases? If not, what would
79 // the check look like?
80 Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
81 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000082 } else if (E->getType()->isAnyComplexType()) {
83 APValue ComplexResult;
84 if (!EvaluateComplex(E, ComplexResult, Info))
85 return false;
86 if (ComplexResult.isComplexFloat()) {
87 Result = !ComplexResult.getComplexFloatReal().isZero() ||
88 !ComplexResult.getComplexFloatImag().isZero();
89 } else {
90 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
91 ComplexResult.getComplexIntImag().getBoolValue();
92 }
93 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +000094 }
95
96 return false;
97}
98
Daniel Dunbara2cfd342009-01-29 06:16:07 +000099static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
100 APFloat &Value, ASTContext &Ctx) {
101 unsigned DestWidth = Ctx.getIntWidth(DestType);
102 // Determine whether we are converting to unsigned or signed.
103 bool DestSigned = DestType->isSignedIntegerType();
104
105 // FIXME: Warning for overflow.
106 uint64_t Space[4];
107 bool ignored;
108 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
109 llvm::APFloat::rmTowardZero, &ignored);
110 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
111}
112
113static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
114 APFloat &Value, ASTContext &Ctx) {
115 bool ignored;
116 APFloat Result = Value;
117 Result.convert(Ctx.getFloatTypeSemantics(DestType),
118 APFloat::rmNearestTiesToEven, &ignored);
119 return Result;
120}
121
122static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
123 APSInt &Value, ASTContext &Ctx) {
124 unsigned DestWidth = Ctx.getIntWidth(DestType);
125 APSInt Result = Value;
126 // Figure out if this is a truncate, extend or noop cast.
127 // If the input is signed, do a sign extend, noop, or truncate.
128 Result.extOrTrunc(DestWidth);
129 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
130 return Result;
131}
132
133static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
134 APSInt &Value, ASTContext &Ctx) {
135
136 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
137 Result.convertFromAPInt(Value, Value.isSigned(),
138 APFloat::rmNearestTiesToEven);
139 return Result;
140}
141
Eli Friedman4efaa272008-11-12 09:44:48 +0000142//===----------------------------------------------------------------------===//
143// LValue Evaluation
144//===----------------------------------------------------------------------===//
145namespace {
146class VISIBILITY_HIDDEN LValueExprEvaluator
147 : public StmtVisitor<LValueExprEvaluator, APValue> {
148 EvalInfo &Info;
149public:
150
151 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
152
153 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000154 return APValue();
155 }
156
157 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000158 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000159 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000160 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
161 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
162 APValue VisitMemberExpr(MemberExpr *E);
163 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000164 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000165 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000166 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000167 APValue VisitUnaryExtension(const UnaryOperator *E)
168 { return Visit(E->getSubExpr()); }
169 APValue VisitChooseExpr(const ChooseExpr *E)
170 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
171 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000172};
173} // end anonymous namespace
174
175static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
176 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
177 return Result.isLValue();
178}
179
Anders Carlsson35873c42008-11-24 04:41:22 +0000180APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
181{
182 if (!E->hasGlobalStorage())
183 return APValue();
184
185 return APValue(E, 0);
186}
187
Steve Naroff3aaa4822009-04-16 19:02:57 +0000188APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E)
189{
190 if (E->hasBlockDeclRefExprs())
191 return APValue();
192
193 return APValue(E, 0);
194}
195
Eli Friedman4efaa272008-11-12 09:44:48 +0000196APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
197 if (E->isFileScope())
198 return APValue(E, 0);
199 return APValue();
200}
201
202APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
203 APValue result;
204 QualType Ty;
205 if (E->isArrow()) {
206 if (!EvaluatePointer(E->getBase(), result, Info))
207 return APValue();
208 Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
209 } else {
210 result = Visit(E->getBase());
211 if (result.isUninit())
212 return APValue();
213 Ty = E->getBase()->getType();
214 }
215
216 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
217 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000218
219 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
220 if (!FD) // FIXME: deal with other kinds of member expressions
221 return APValue();
Eli Friedman4efaa272008-11-12 09:44:48 +0000222
223 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000224 unsigned i = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000225 for (RecordDecl::field_iterator Field = RD->field_begin(Info.Ctx),
226 FieldEnd = RD->field_end(Info.Ctx);
Douglas Gregor44b43212008-12-11 16:49:14 +0000227 Field != FieldEnd; (void)++Field, ++i) {
228 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000229 break;
230 }
231
232 result.setLValue(result.getLValueBase(),
233 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
234
235 return result;
236}
237
Anders Carlsson3068d112008-11-16 19:01:22 +0000238APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
239{
240 APValue Result;
241
242 if (!EvaluatePointer(E->getBase(), Result, Info))
243 return APValue();
244
245 APSInt Index;
246 if (!EvaluateInteger(E->getIdx(), Index, Info))
247 return APValue();
248
249 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
250
251 uint64_t Offset = Index.getSExtValue() * ElementSize;
252 Result.setLValue(Result.getLValueBase(),
253 Result.getLValueOffset() + Offset);
254 return Result;
255}
Eli Friedman4efaa272008-11-12 09:44:48 +0000256
Eli Friedmane8761c82009-02-20 01:57:15 +0000257APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E)
258{
259 APValue Result;
260 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
261 return APValue();
262 return Result;
263}
264
Eli Friedman4efaa272008-11-12 09:44:48 +0000265//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000266// Pointer Evaluation
267//===----------------------------------------------------------------------===//
268
Anders Carlssonc754aa62008-07-08 05:13:58 +0000269namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000270class VISIBILITY_HIDDEN PointerExprEvaluator
271 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000272 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000273public:
Anders Carlsson2bad1682008-07-08 14:30:00 +0000274
Chris Lattner87eae5e2008-07-11 22:52:41 +0000275 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000276
Anders Carlsson2bad1682008-07-08 14:30:00 +0000277 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000278 return APValue();
279 }
280
281 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
282
Anders Carlsson650c92f2008-07-08 15:34:11 +0000283 APValue VisitBinaryOperator(const BinaryOperator *E);
284 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000285 APValue VisitUnaryExtension(const UnaryOperator *E)
286 { return Visit(E->getSubExpr()); }
287 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000288 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
289 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000290 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
291 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000292 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000293 APValue VisitBlockExpr(BlockExpr *E) {
294 if (!E->hasBlockDeclRefExprs())
295 return APValue(E, 0);
296 return APValue();
297 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000298 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
299 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000300 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000301 APValue VisitChooseExpr(ChooseExpr *E)
302 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
303 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000304};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000305} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000306
Chris Lattner87eae5e2008-07-11 22:52:41 +0000307static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000308 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000309 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000310 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000311 return Result.isLValue();
312}
313
314APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
315 if (E->getOpcode() != BinaryOperator::Add &&
316 E->getOpcode() != BinaryOperator::Sub)
317 return APValue();
318
319 const Expr *PExp = E->getLHS();
320 const Expr *IExp = E->getRHS();
321 if (IExp->getType()->isPointerType())
322 std::swap(PExp, IExp);
323
324 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000325 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000326 return APValue();
327
328 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000329 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000330 return APValue();
331
Eli Friedman4efaa272008-11-12 09:44:48 +0000332 QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000333 uint64_t SizeOfPointee;
334
335 // Explicitly handle GNU void* and function pointer arithmetic extensions.
336 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
337 SizeOfPointee = 1;
338 else
339 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000340
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000341 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000342
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000343 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000344 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000345 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000346 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
347
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000348 return APValue(ResultLValue.getLValueBase(), Offset);
349}
Eli Friedman4efaa272008-11-12 09:44:48 +0000350
Eli Friedman2217c872009-02-22 11:46:18 +0000351APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
352 APValue result;
353 if (EvaluateLValue(E->getSubExpr(), result, Info))
354 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000355 return APValue();
356}
Anders Carlssond407a762008-12-05 05:24:13 +0000357
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000358
Chris Lattnerb542afe2008-07-11 19:10:17 +0000359APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000360 const Expr* SubExpr = E->getSubExpr();
361
362 // Check for pointer->pointer cast
363 if (SubExpr->getType()->isPointerType()) {
364 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000365 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000366 return Result;
367 return APValue();
368 }
369
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000370 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000371 APValue Result;
372 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
373 return APValue();
374
375 if (Result.isInt()) {
376 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
377 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000378 }
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000379
380 // Cast is of an lvalue, no need to change value.
381 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000382 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000383
384 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000385 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000386 SubExpr->getType()->isArrayType()) {
387 APValue Result;
388 if (EvaluateLValue(SubExpr, Result, Info))
389 return Result;
390 return APValue();
391 }
392
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393 return APValue();
394}
395
Eli Friedman3941b182009-01-25 01:54:01 +0000396APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000397 if (E->isBuiltinCall(Info.Ctx) ==
398 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000399 return APValue(E, 0);
400 return APValue();
401}
402
Eli Friedman4efaa272008-11-12 09:44:48 +0000403APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
404 bool BoolResult;
405 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
406 return APValue();
407
408 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
409
410 APValue Result;
411 if (EvaluatePointer(EvalExpr, Result, Info))
412 return Result;
413 return APValue();
414}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000415
416//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000417// Vector Evaluation
418//===----------------------------------------------------------------------===//
419
420namespace {
421 class VISIBILITY_HIDDEN VectorExprEvaluator
422 : public StmtVisitor<VectorExprEvaluator, APValue> {
423 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000424 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000425 public:
426
427 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
428
429 APValue VisitStmt(Stmt *S) {
430 return APValue();
431 }
432
Eli Friedman91110ee2009-02-23 04:23:56 +0000433 APValue VisitParenExpr(ParenExpr *E)
434 { return Visit(E->getSubExpr()); }
435 APValue VisitUnaryExtension(const UnaryOperator *E)
436 { return Visit(E->getSubExpr()); }
437 APValue VisitUnaryPlus(const UnaryOperator *E)
438 { return Visit(E->getSubExpr()); }
439 APValue VisitUnaryReal(const UnaryOperator *E)
440 { return Visit(E->getSubExpr()); }
441 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
442 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000443 APValue VisitCastExpr(const CastExpr* E);
444 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
445 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000446 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000447 APValue VisitChooseExpr(const ChooseExpr *E)
448 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000449 APValue VisitUnaryImag(const UnaryOperator *E);
450 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000451 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000452 // shufflevector, ExtVectorElementExpr
453 // (Note that these require implementing conversions
454 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000455 };
456} // end anonymous namespace
457
458static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
459 if (!E->getType()->isVectorType())
460 return false;
461 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
462 return !Result.isUninit();
463}
464
465APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
466 const Expr* SE = E->getSubExpr();
467
468 // Check for vector->vector bitcast.
469 if (SE->getType()->isVectorType())
470 return this->Visit(const_cast<Expr*>(SE));
471
472 return APValue();
473}
474
475APValue
476VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
477 return this->Visit(const_cast<Expr*>(E->getInitializer()));
478}
479
480APValue
481VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
482 const VectorType *VT = E->getType()->getAsVectorType();
483 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000484 unsigned NumElements = VT->getNumElements();
Nate Begeman59b5da62009-01-18 03:20:47 +0000485
486 QualType EltTy = VT->getElementType();
487 llvm::SmallVector<APValue, 4> Elements;
488
Eli Friedman91110ee2009-02-23 04:23:56 +0000489 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000490 if (EltTy->isIntegerType()) {
491 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000492 if (i < NumInits) {
493 if (!EvaluateInteger(E->getInit(i), sInt, Info))
494 return APValue();
495 } else {
496 sInt = Info.Ctx.MakeIntValue(0, EltTy);
497 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000498 Elements.push_back(APValue(sInt));
499 } else {
500 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000501 if (i < NumInits) {
502 if (!EvaluateFloat(E->getInit(i), f, Info))
503 return APValue();
504 } else {
505 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
506 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000507 Elements.push_back(APValue(f));
508 }
509 }
510 return APValue(&Elements[0], Elements.size());
511}
512
Eli Friedman91110ee2009-02-23 04:23:56 +0000513APValue
514VectorExprEvaluator::GetZeroVector(QualType T) {
515 const VectorType *VT = T->getAsVectorType();
516 QualType EltTy = VT->getElementType();
517 APValue ZeroElement;
518 if (EltTy->isIntegerType())
519 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
520 else
521 ZeroElement =
522 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
523
524 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
525 return APValue(&Elements[0], Elements.size());
526}
527
528APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
529 bool BoolResult;
530 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
531 return APValue();
532
533 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
534
535 APValue Result;
536 if (EvaluateVector(EvalExpr, Result, Info))
537 return Result;
538 return APValue();
539}
540
Eli Friedman91110ee2009-02-23 04:23:56 +0000541APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
542 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
543 Info.EvalResult.HasSideEffects = true;
544 return GetZeroVector(E->getType());
545}
546
Nate Begeman59b5da62009-01-18 03:20:47 +0000547//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000548// Integer Evaluation
549//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000550
551namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000552class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000553 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000554 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000555 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000556public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000557 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000558 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000559
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000560 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000561 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000562 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000563 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000564 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000565 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000566 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000567 return true;
568 }
569
Daniel Dunbar131eb432009-02-19 09:06:44 +0000570 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000571 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000572 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000573 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000574 Result = APValue(APSInt(I));
575 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000576 return true;
577 }
578
579 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000580 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000581 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000582 return true;
583 }
584
Anders Carlsson82206e22008-11-30 18:14:57 +0000585 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000586 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000587 if (Info.EvalResult.Diag == 0) {
588 Info.EvalResult.DiagLoc = L;
589 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000590 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000591 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000592 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000593 }
594
Anders Carlssonc754aa62008-07-08 05:13:58 +0000595 //===--------------------------------------------------------------------===//
596 // Visitor Methods
597 //===--------------------------------------------------------------------===//
Chris Lattner32fea9d2008-11-12 07:43:42 +0000598
599 bool VisitStmt(Stmt *) {
600 assert(0 && "This should be called on integers, stmts are not integers");
601 return false;
602 }
Chris Lattner7a767782008-07-11 19:24:49 +0000603
Chris Lattner32fea9d2008-11-12 07:43:42 +0000604 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000605 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000606 }
607
Chris Lattnerb542afe2008-07-11 19:10:17 +0000608 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000609
Chris Lattner4c4867e2008-07-12 00:38:25 +0000610 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000611 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000612 }
613 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000614 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000615 }
616 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000617 // Per gcc docs "this built-in function ignores top level
618 // qualifiers". We need to use the canonical version to properly
619 // be able to strip CRV qualifiers from the type.
620 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
621 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000622 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
623 T1.getUnqualifiedType()),
624 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000625 }
626 bool VisitDeclRefExpr(const DeclRefExpr *E);
627 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000628 bool VisitBinaryOperator(const BinaryOperator *E);
629 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000630 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000631
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000632 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000633 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
634
Anders Carlsson3068d112008-11-16 19:01:22 +0000635 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000636 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000637 }
638
Anders Carlsson3f704562008-12-21 22:39:40 +0000639 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000640 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000641 }
642
Anders Carlsson3068d112008-11-16 19:01:22 +0000643 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000644 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000645 }
646
Eli Friedman664a1042009-02-27 04:45:43 +0000647 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
648 return Success(0, E);
649 }
650
Sebastian Redl64b45f72009-01-05 20:52:13 +0000651 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000652 return Success(E->EvaluateTrait(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000653 }
654
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000655 bool VisitChooseExpr(const ChooseExpr *E) {
656 return Visit(E->getChosenSubExpr(Info.Ctx));
657 }
658
Eli Friedman722c7172009-02-28 03:59:05 +0000659 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000660 bool VisitUnaryImag(const UnaryOperator *E);
661
Chris Lattnerfcee0012008-07-11 21:24:13 +0000662private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000663 unsigned GetAlignOfExpr(const Expr *E);
664 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000665 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000666};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000667} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000668
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000669static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000670 if (!E->getType()->isIntegralType())
671 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000672
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000673 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
674}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000675
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000676static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
677 APValue Val;
678 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
679 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000680 Result = Val.getInt();
681 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000682}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000683
Chris Lattner4c4867e2008-07-12 00:38:25 +0000684bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
685 // Enums are integer constant exprs.
686 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000687 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000688 // signedness consistently; see PR3173.
689 APSInt SI = D->getInitVal();
690 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
691 // FIXME: This is an ugly hack around the fact that enums don't
692 // set their width (!?!) consistently; see PR3173.
693 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
694 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000695 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000696
697 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000698 // In C, they can also be folded, although they are not ICEs.
699 if (E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000700 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
701 if (const Expr *Init = D->getInit())
702 return Visit(const_cast<Expr*>(Init));
703 }
704 }
705
Chris Lattner4c4867e2008-07-12 00:38:25 +0000706 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000707 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000708}
709
Chris Lattnera4d55d82008-10-06 06:40:35 +0000710/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
711/// as GCC.
712static int EvaluateBuiltinClassifyType(const CallExpr *E) {
713 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000714 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000715 enum gcc_type_class {
716 no_type_class = -1,
717 void_type_class, integer_type_class, char_type_class,
718 enumeral_type_class, boolean_type_class,
719 pointer_type_class, reference_type_class, offset_type_class,
720 real_type_class, complex_type_class,
721 function_type_class, method_type_class,
722 record_type_class, union_type_class,
723 array_type_class, string_type_class,
724 lang_type_class
725 };
726
727 // If no argument was supplied, default to "no_type_class". This isn't
728 // ideal, however it is what gcc does.
729 if (E->getNumArgs() == 0)
730 return no_type_class;
731
732 QualType ArgTy = E->getArg(0)->getType();
733 if (ArgTy->isVoidType())
734 return void_type_class;
735 else if (ArgTy->isEnumeralType())
736 return enumeral_type_class;
737 else if (ArgTy->isBooleanType())
738 return boolean_type_class;
739 else if (ArgTy->isCharType())
740 return string_type_class; // gcc doesn't appear to use char_type_class
741 else if (ArgTy->isIntegerType())
742 return integer_type_class;
743 else if (ArgTy->isPointerType())
744 return pointer_type_class;
745 else if (ArgTy->isReferenceType())
746 return reference_type_class;
747 else if (ArgTy->isRealType())
748 return real_type_class;
749 else if (ArgTy->isComplexType())
750 return complex_type_class;
751 else if (ArgTy->isFunctionType())
752 return function_type_class;
753 else if (ArgTy->isStructureType())
754 return record_type_class;
755 else if (ArgTy->isUnionType())
756 return union_type_class;
757 else if (ArgTy->isArrayType())
758 return array_type_class;
759 else if (ArgTy->isUnionType())
760 return union_type_class;
761 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
762 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
763 return -1;
764}
765
Chris Lattner4c4867e2008-07-12 00:38:25 +0000766bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000767 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000768 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000769 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000770 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000771 return Success(EvaluateBuiltinClassifyType(E), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000772
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000773 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000774 // __builtin_constant_p always has one operand: it returns true if that
775 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000776 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner019f4e82008-10-06 05:28:25 +0000777 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000778}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000779
Chris Lattnerb542afe2008-07-11 19:10:17 +0000780bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000781 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000782 if (!Visit(E->getRHS()))
783 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000784
Eli Friedman33ef1452009-02-26 10:19:36 +0000785 // If we can't evaluate the LHS, it might have side effects;
786 // conservatively mark it.
787 if (!E->getLHS()->isEvaluatable(Info.Ctx))
788 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000789
Anders Carlsson027f62e2008-12-01 02:07:06 +0000790 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000791 }
792
793 if (E->isLogicalOp()) {
794 // These need to be handled specially because the operands aren't
795 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000796 bool lhsResult, rhsResult;
Anders Carlsson51fe9962008-11-22 21:04:56 +0000797
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000798 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +0000799 // We were able to evaluate the LHS, see if we can get away with not
800 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +0000801 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000802 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000803
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000804 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000805 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000806 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000807 else
Daniel Dunbar131eb432009-02-19 09:06:44 +0000808 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000809 }
810 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000811 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000812 // We can't evaluate the LHS; however, sometimes the result
813 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000814 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
815 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000816 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +0000817 // must have had side effects.
818 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +0000819
820 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000821 }
822 }
Anders Carlsson51fe9962008-11-22 21:04:56 +0000823 }
Eli Friedmana6afa762008-11-13 06:09:17 +0000824
Eli Friedmana6afa762008-11-13 06:09:17 +0000825 return false;
826 }
827
Anders Carlsson286f85e2008-11-16 07:17:21 +0000828 QualType LHSTy = E->getLHS()->getType();
829 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +0000830
831 if (LHSTy->isAnyComplexType()) {
832 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
833 APValue LHS, RHS;
834
835 if (!EvaluateComplex(E->getLHS(), LHS, Info))
836 return false;
837
838 if (!EvaluateComplex(E->getRHS(), RHS, Info))
839 return false;
840
841 if (LHS.isComplexFloat()) {
842 APFloat::cmpResult CR_r =
843 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
844 APFloat::cmpResult CR_i =
845 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
846
Daniel Dunbar4087e242009-01-29 06:43:41 +0000847 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000848 return Success((CR_r == APFloat::cmpEqual &&
849 CR_i == APFloat::cmpEqual), E);
850 else {
851 assert(E->getOpcode() == BinaryOperator::NE &&
852 "Invalid complex comparison.");
853 return Success(((CR_r == APFloat::cmpGreaterThan ||
854 CR_r == APFloat::cmpLessThan) &&
855 (CR_i == APFloat::cmpGreaterThan ||
856 CR_i == APFloat::cmpLessThan)), E);
857 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000858 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +0000859 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +0000860 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
861 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
862 else {
863 assert(E->getOpcode() == BinaryOperator::NE &&
864 "Invalid compex comparison.");
865 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
866 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
867 }
Daniel Dunbar4087e242009-01-29 06:43:41 +0000868 }
869 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000870
871 if (LHSTy->isRealFloatingType() &&
872 RHSTy->isRealFloatingType()) {
873 APFloat RHS(0.0), LHS(0.0);
874
875 if (!EvaluateFloat(E->getRHS(), RHS, Info))
876 return false;
877
878 if (!EvaluateFloat(E->getLHS(), LHS, Info))
879 return false;
880
881 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +0000882
Anders Carlsson286f85e2008-11-16 07:17:21 +0000883 switch (E->getOpcode()) {
884 default:
885 assert(0 && "Invalid binary operator!");
886 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000887 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000888 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000889 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000890 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000891 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000892 case BinaryOperator::GE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000893 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
894 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000895 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000896 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000897 case BinaryOperator::NE:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000898 return Success(CR == APFloat::cmpGreaterThan
899 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +0000900 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000901 }
902
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000903 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
904 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000905 APValue LHSValue;
906 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
907 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000908
Anders Carlsson3068d112008-11-16 19:01:22 +0000909 APValue RHSValue;
910 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
911 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000912
913 // Reject any bases; this is conservative, but good enough for
914 // common uses
Anders Carlsson3068d112008-11-16 19:01:22 +0000915 if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
916 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000917
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000918 if (E->getOpcode() == BinaryOperator::Sub) {
919 const QualType Type = E->getLHS()->getType();
920 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +0000921
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000922 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
923 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000924
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000925 return Success(D, E);
926 }
927 bool Result;
928 if (E->getOpcode() == BinaryOperator::EQ) {
929 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +0000930 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +0000931 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
932 }
933 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000934 }
935 }
Anders Carlsson286f85e2008-11-16 07:17:21 +0000936 if (!LHSTy->isIntegralType() ||
937 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000938 // We can't continue from here for non-integral types, and they
939 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +0000940 return false;
941 }
942
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000943 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000944 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +0000945 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000946
Eli Friedman42edd0d2009-03-24 01:14:50 +0000947 APValue RHSVal;
948 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000949 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +0000950
951 // Handle cases like (unsigned long)&a + 4.
952 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
953 uint64_t offset = Result.getLValueOffset();
954 if (E->getOpcode() == BinaryOperator::Add)
955 offset += RHSVal.getInt().getZExtValue();
956 else
957 offset -= RHSVal.getInt().getZExtValue();
958 Result = APValue(Result.getLValueBase(), offset);
959 return true;
960 }
961
962 // Handle cases like 4 + (unsigned long)&a
963 if (E->getOpcode() == BinaryOperator::Add &&
964 RHSVal.isLValue() && Result.isInt()) {
965 uint64_t offset = RHSVal.getLValueOffset();
966 offset += Result.getInt().getZExtValue();
967 Result = APValue(RHSVal.getLValueBase(), offset);
968 return true;
969 }
970
971 // All the following cases expect both operands to be an integer
972 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +0000973 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +0000974
Eli Friedman42edd0d2009-03-24 01:14:50 +0000975 APSInt& RHS = RHSVal.getInt();
976
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000977 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000978 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000979 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000980 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
981 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
982 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
983 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
984 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
985 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000986 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +0000987 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000988 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000989 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +0000990 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +0000991 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000992 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000993 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000994 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +0000995 // FIXME: Warn about out of range shift amounts!
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000996 unsigned SA =
997 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
998 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000999 }
1000 case BinaryOperator::Shr: {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001001 unsigned SA =
1002 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1003 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001004 }
Chris Lattnerb542afe2008-07-11 19:10:17 +00001005
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001006 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1007 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1008 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1009 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1010 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1011 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001012 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001013}
1014
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001015bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001016 bool Cond;
1017 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001018 return false;
1019
Nuno Lopesa25bd552008-11-16 22:06:39 +00001020 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001021}
1022
Chris Lattneraf707ab2009-01-24 21:53:27 +00001023unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001024 const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
1025
1026 // __alignof__(void) = 1 as a gcc extension.
1027 if (Ty->isVoidType())
1028 return 1;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001029
Chris Lattnere9feb472009-01-24 21:09:06 +00001030 // GCC extension: alignof(function) = 4.
1031 // FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
1032 // attribute(align) directive.
1033 if (Ty->isFunctionType())
1034 return 4;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001035
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001036 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty))
1037 return GetAlignOfType(QualType(EXTQT->getBaseType(), 0));
Chris Lattnere9feb472009-01-24 21:09:06 +00001038
1039 // alignof VLA/incomplete array.
1040 if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
1041 return GetAlignOfType(VAT->getElementType());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001042
Chris Lattnere9feb472009-01-24 21:09:06 +00001043 // sizeof (objc class)?
1044 if (isa<ObjCInterfaceType>(Ty))
1045 return 1; // FIXME: This probably isn't right.
1046
1047 // Get information about the alignment.
1048 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Eli Friedmanc5082032009-02-22 03:31:23 +00001049 return Info.Ctx.getPreferredTypeAlign(Ty) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001050}
1051
Chris Lattneraf707ab2009-01-24 21:53:27 +00001052unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1053 E = E->IgnoreParens();
1054
1055 // alignof decl is always accepted, even if it doesn't make sense: we default
1056 // to 1 in those cases.
1057 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001058 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001059
Chris Lattneraf707ab2009-01-24 21:53:27 +00001060 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001061 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001062
Chris Lattnere9feb472009-01-24 21:09:06 +00001063 return GetAlignOfType(E->getType());
1064}
1065
1066
Sebastian Redl05189992008-11-11 17:56:53 +00001067/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1068/// expression's type.
1069bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1070 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001071
Chris Lattnere9feb472009-01-24 21:09:06 +00001072 // Handle alignof separately.
1073 if (!E->isSizeOf()) {
1074 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001075 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001076 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001077 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001078 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001079
Sebastian Redl05189992008-11-11 17:56:53 +00001080 QualType SrcTy = E->getTypeOfArgument();
1081
Daniel Dunbar131eb432009-02-19 09:06:44 +00001082 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1083 // extension.
1084 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1085 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001086
Chris Lattnerfcee0012008-07-11 21:24:13 +00001087 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001088 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001089 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001090
Daniel Dunbarff896662009-04-21 15:48:54 +00001091 unsigned BitWidth = 0;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001092 if (SrcTy->isObjCInterfaceType()) {
1093 // Slightly unusual case: the size of an ObjC interface type is the
Daniel Dunbarff896662009-04-21 15:48:54 +00001094 // size of the class.
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001095 ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbarff896662009-04-21 15:48:54 +00001096 const ASTRecordLayout &Layout = Info.Ctx.getASTObjCInterfaceLayout(OI);
1097 BitWidth = Layout.getSize();
1098 } else
1099 BitWidth = Info.Ctx.getTypeSize(SrcTy);
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001100
Chris Lattnere9feb472009-01-24 21:09:06 +00001101 // Get information about the size.
Daniel Dunbarff896662009-04-21 15:48:54 +00001102 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001103}
1104
Chris Lattnerb542afe2008-07-11 19:10:17 +00001105bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001106 // Special case unary operators that do not need their subexpression
1107 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001108 if (E->isOffsetOfOp()) {
1109 // The AST for offsetof is defined in such a way that we can just
1110 // directly Evaluate it as an l-value.
1111 APValue LV;
1112 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1113 return false;
1114 if (LV.getLValueBase())
1115 return false;
1116 return Success(LV.getLValueOffset(), E);
1117 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001118
1119 if (E->getOpcode() == UnaryOperator::LNot) {
1120 // LNot's operand isn't necessarily an integer, so we handle it specially.
1121 bool bres;
1122 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1123 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001124 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001125 }
1126
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001127 // Only handle integral operations...
1128 if (!E->getSubExpr()->getType()->isIntegralType())
1129 return false;
1130
Chris Lattner87eae5e2008-07-11 22:52:41 +00001131 // Get the operand value into 'Result'.
1132 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001133 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001134
Chris Lattner75a48812008-07-11 22:15:16 +00001135 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001136 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001137 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1138 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001139 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001140 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001141 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1142 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001143 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001144 case UnaryOperator::Plus:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001145 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001146 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001147 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001148 if (!Result.isInt()) return false;
1149 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001150 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001151 if (!Result.isInt()) return false;
1152 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001153 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001154}
1155
Chris Lattner732b2232008-07-12 01:15:53 +00001156/// HandleCast - This is used to evaluate implicit or explicit casts where the
1157/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001158bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001159 Expr *SubExpr = E->getSubExpr();
1160 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001161 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001162
Eli Friedman4efaa272008-11-12 09:44:48 +00001163 if (DestType->isBooleanType()) {
1164 bool BoolResult;
1165 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1166 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001167 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001168 }
1169
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001170 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001171 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001172 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001173 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001174
Eli Friedmanbe265702009-02-20 01:15:07 +00001175 if (!Result.isInt()) {
1176 // Only allow casts of lvalues if they are lossless.
1177 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1178 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001179
Daniel Dunbardd211642009-02-19 22:24:01 +00001180 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001181 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001182 }
1183
1184 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001185 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001186 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001187 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001188 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001189
Daniel Dunbardd211642009-02-19 22:24:01 +00001190 if (LV.getLValueBase()) {
1191 // Only allow based lvalue casts if they are lossless.
1192 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1193 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001194
Daniel Dunbardd211642009-02-19 22:24:01 +00001195 Result = LV;
1196 return true;
1197 }
1198
1199 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1200 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001201 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001202
Eli Friedmanbe265702009-02-20 01:15:07 +00001203 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1204 // This handles double-conversion cases, where there's both
1205 // an l-value promotion and an implicit conversion to int.
1206 APValue LV;
1207 if (!EvaluateLValue(SubExpr, LV, Info))
1208 return false;
1209
1210 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1211 return false;
1212
1213 Result = LV;
1214 return true;
1215 }
1216
Eli Friedman1725f682009-04-22 19:23:09 +00001217 if (SrcType->isAnyComplexType()) {
1218 APValue C;
1219 if (!EvaluateComplex(SubExpr, C, Info))
1220 return false;
1221 if (C.isComplexFloat())
1222 return Success(HandleFloatToIntCast(DestType, SrcType,
1223 C.getComplexFloatReal(), Info.Ctx),
1224 E);
1225 else
1226 return Success(HandleIntToIntCast(DestType, SrcType,
1227 C.getComplexIntReal(), Info.Ctx), E);
1228 }
Eli Friedman2217c872009-02-22 11:46:18 +00001229 // FIXME: Handle vectors
1230
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001231 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001232 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001233
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001234 APFloat F(0.0);
1235 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001236 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001237
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001238 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001239}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001240
Eli Friedman722c7172009-02-28 03:59:05 +00001241bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1242 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1243 APValue LV;
1244 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1245 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1246 return Success(LV.getComplexIntReal(), E);
1247 }
1248
1249 return Visit(E->getSubExpr());
1250}
1251
Eli Friedman664a1042009-02-27 04:45:43 +00001252bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001253 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1254 APValue LV;
1255 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1256 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1257 return Success(LV.getComplexIntImag(), E);
1258 }
1259
Eli Friedman664a1042009-02-27 04:45:43 +00001260 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1261 Info.EvalResult.HasSideEffects = true;
1262 return Success(0, E);
1263}
1264
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001265//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001266// Float Evaluation
1267//===----------------------------------------------------------------------===//
1268
1269namespace {
1270class VISIBILITY_HIDDEN FloatExprEvaluator
1271 : public StmtVisitor<FloatExprEvaluator, bool> {
1272 EvalInfo &Info;
1273 APFloat &Result;
1274public:
1275 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1276 : Info(info), Result(result) {}
1277
1278 bool VisitStmt(Stmt *S) {
1279 return false;
1280 }
1281
1282 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001283 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001284
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001285 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001286 bool VisitBinaryOperator(const BinaryOperator *E);
1287 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001288 bool VisitCastExpr(CastExpr *E);
1289 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001290
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001291 bool VisitChooseExpr(const ChooseExpr *E)
1292 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1293 bool VisitUnaryExtension(const UnaryOperator *E)
1294 { return Visit(E->getSubExpr()); }
1295
1296 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1297 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001298 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001299};
1300} // end anonymous namespace
1301
1302static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1303 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1304}
1305
Chris Lattner019f4e82008-10-06 05:28:25 +00001306bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001307 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001308 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001309 case Builtin::BI__builtin_huge_val:
1310 case Builtin::BI__builtin_huge_valf:
1311 case Builtin::BI__builtin_huge_vall:
1312 case Builtin::BI__builtin_inf:
1313 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001314 case Builtin::BI__builtin_infl: {
1315 const llvm::fltSemantics &Sem =
1316 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001317 Result = llvm::APFloat::getInf(Sem);
1318 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001319 }
Chris Lattner9e621712008-10-06 06:31:58 +00001320
1321 case Builtin::BI__builtin_nan:
1322 case Builtin::BI__builtin_nanf:
1323 case Builtin::BI__builtin_nanl:
1324 // If this is __builtin_nan("") turn this into a simple nan, otherwise we
1325 // can't constant fold it.
1326 if (const StringLiteral *S =
1327 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
1328 if (!S->isWide() && S->getByteLength() == 0) { // empty string.
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001329 const llvm::fltSemantics &Sem =
1330 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner9e621712008-10-06 06:31:58 +00001331 Result = llvm::APFloat::getNaN(Sem);
1332 return true;
1333 }
1334 }
1335 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001336
1337 case Builtin::BI__builtin_fabs:
1338 case Builtin::BI__builtin_fabsf:
1339 case Builtin::BI__builtin_fabsl:
1340 if (!EvaluateFloat(E->getArg(0), Result, Info))
1341 return false;
1342
1343 if (Result.isNegative())
1344 Result.changeSign();
1345 return true;
1346
1347 case Builtin::BI__builtin_copysign:
1348 case Builtin::BI__builtin_copysignf:
1349 case Builtin::BI__builtin_copysignl: {
1350 APFloat RHS(0.);
1351 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1352 !EvaluateFloat(E->getArg(1), RHS, Info))
1353 return false;
1354 Result.copySign(RHS);
1355 return true;
1356 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001357 }
1358}
1359
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001360bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001361 if (E->getOpcode() == UnaryOperator::Deref)
1362 return false;
1363
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001364 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1365 return false;
1366
1367 switch (E->getOpcode()) {
1368 default: return false;
1369 case UnaryOperator::Plus:
1370 return true;
1371 case UnaryOperator::Minus:
1372 Result.changeSign();
1373 return true;
1374 }
1375}
Chris Lattner019f4e82008-10-06 05:28:25 +00001376
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001377bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1378 // FIXME: Diagnostics? I really don't understand how the warnings
1379 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001380 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001381 if (!EvaluateFloat(E->getLHS(), Result, Info))
1382 return false;
1383 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1384 return false;
1385
1386 switch (E->getOpcode()) {
1387 default: return false;
1388 case BinaryOperator::Mul:
1389 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1390 return true;
1391 case BinaryOperator::Add:
1392 Result.add(RHS, APFloat::rmNearestTiesToEven);
1393 return true;
1394 case BinaryOperator::Sub:
1395 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1396 return true;
1397 case BinaryOperator::Div:
1398 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1399 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001400 }
1401}
1402
1403bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1404 Result = E->getValue();
1405 return true;
1406}
1407
Eli Friedman4efaa272008-11-12 09:44:48 +00001408bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1409 Expr* SubExpr = E->getSubExpr();
Nate Begeman59b5da62009-01-18 03:20:47 +00001410
Eli Friedman4efaa272008-11-12 09:44:48 +00001411 if (SubExpr->getType()->isIntegralType()) {
1412 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001413 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001414 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001415 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
1416 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001417 return true;
1418 }
1419 if (SubExpr->getType()->isRealFloatingType()) {
1420 if (!Visit(SubExpr))
1421 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001422 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1423 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001424 return true;
1425 }
Eli Friedman2217c872009-02-22 11:46:18 +00001426 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001427
1428 return false;
1429}
1430
1431bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1432 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1433 return true;
1434}
1435
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001436//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001437// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001438//===----------------------------------------------------------------------===//
1439
1440namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001441class VISIBILITY_HIDDEN ComplexExprEvaluator
1442 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001443 EvalInfo &Info;
1444
1445public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001446 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001447
1448 //===--------------------------------------------------------------------===//
1449 // Visitor Methods
1450 //===--------------------------------------------------------------------===//
1451
1452 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001453 return APValue();
1454 }
1455
1456 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1457
1458 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001459 Expr* SubExpr = E->getSubExpr();
1460
1461 if (SubExpr->getType()->isRealFloatingType()) {
1462 APFloat Result(0.0);
1463
1464 if (!EvaluateFloat(SubExpr, Result, Info))
1465 return APValue();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001466
Daniel Dunbar3f279872009-01-29 01:32:56 +00001467 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001468 Result);
1469 } else {
1470 assert(SubExpr->getType()->isIntegerType() &&
1471 "Unexpected imaginary literal.");
1472
1473 llvm::APSInt Result;
1474 if (!EvaluateInteger(SubExpr, Result, Info))
1475 return APValue();
1476
1477 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1478 Zero = 0;
1479 return APValue(Zero, Result);
1480 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001481 }
1482
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001483 APValue VisitCastExpr(CastExpr *E) {
1484 Expr* SubExpr = E->getSubExpr();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001485 QualType EltType = E->getType()->getAsComplexType()->getElementType();
1486 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001487
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001488 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001489 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001490
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001491 if (!EvaluateFloat(SubExpr, Result, Info))
1492 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001493
1494 if (EltType->isRealFloatingType()) {
1495 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
1496 return APValue(Result,
1497 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1498 } else {
1499 llvm::APSInt IResult;
1500 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1501 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1502 Zero = 0;
1503 return APValue(IResult, Zero);
1504 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001505 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001506 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001507
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001508 if (!EvaluateInteger(SubExpr, Result, Info))
1509 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001510
Eli Friedman1725f682009-04-22 19:23:09 +00001511 if (EltType->isRealFloatingType()) {
1512 APFloat FResult =
1513 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
1514 return APValue(FResult,
1515 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1516 } else {
1517 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1518 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1519 Zero = 0;
1520 return APValue(Result, Zero);
1521 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001522 } else if (const ComplexType *CT = SubType->getAsComplexType()) {
1523 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001524
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001525 if (!EvaluateComplex(SubExpr, Src, Info))
1526 return APValue();
1527
1528 QualType SrcType = CT->getElementType();
1529
1530 if (Src.isComplexFloat()) {
1531 if (EltType->isRealFloatingType()) {
1532 return APValue(HandleFloatToFloatCast(EltType, SrcType,
1533 Src.getComplexFloatReal(),
1534 Info.Ctx),
1535 HandleFloatToFloatCast(EltType, SrcType,
1536 Src.getComplexFloatImag(),
1537 Info.Ctx));
1538 } else {
1539 return APValue(HandleFloatToIntCast(EltType, SrcType,
1540 Src.getComplexFloatReal(),
1541 Info.Ctx),
1542 HandleFloatToIntCast(EltType, SrcType,
1543 Src.getComplexFloatImag(),
1544 Info.Ctx));
1545 }
1546 } else {
1547 assert(Src.isComplexInt() && "Invalid evaluate result.");
1548 if (EltType->isRealFloatingType()) {
1549 return APValue(HandleIntToFloatCast(EltType, SrcType,
1550 Src.getComplexIntReal(),
1551 Info.Ctx),
1552 HandleIntToFloatCast(EltType, SrcType,
1553 Src.getComplexIntImag(),
1554 Info.Ctx));
1555 } else {
1556 return APValue(HandleIntToIntCast(EltType, SrcType,
1557 Src.getComplexIntReal(),
1558 Info.Ctx),
1559 HandleIntToIntCast(EltType, SrcType,
1560 Src.getComplexIntImag(),
1561 Info.Ctx));
1562 }
1563 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001564 }
1565
1566 // FIXME: Handle more casts.
1567 return APValue();
1568 }
1569
1570 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001571 APValue VisitChooseExpr(const ChooseExpr *E)
1572 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1573 APValue VisitUnaryExtension(const UnaryOperator *E)
1574 { return Visit(E->getSubExpr()); }
1575 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001576 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001577};
1578} // end anonymous namespace
1579
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001580static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001581{
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001582 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1583 assert((!Result.isComplexFloat() ||
1584 (&Result.getComplexFloatReal().getSemantics() ==
1585 &Result.getComplexFloatImag().getSemantics())) &&
1586 "Invalid complex evaluation.");
1587 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001588}
1589
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001590APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001591{
1592 APValue Result, RHS;
1593
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001594 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001595 return APValue();
1596
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001597 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001598 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001599
Daniel Dunbar3f279872009-01-29 01:32:56 +00001600 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1601 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001602 switch (E->getOpcode()) {
1603 default: return APValue();
1604 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001605 if (Result.isComplexFloat()) {
1606 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1607 APFloat::rmNearestTiesToEven);
1608 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1609 APFloat::rmNearestTiesToEven);
1610 } else {
1611 Result.getComplexIntReal() += RHS.getComplexIntReal();
1612 Result.getComplexIntImag() += RHS.getComplexIntImag();
1613 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001614 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001615 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001616 if (Result.isComplexFloat()) {
1617 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1618 APFloat::rmNearestTiesToEven);
1619 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1620 APFloat::rmNearestTiesToEven);
1621 } else {
1622 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1623 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1624 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001625 break;
1626 case BinaryOperator::Mul:
1627 if (Result.isComplexFloat()) {
1628 APValue LHS = Result;
1629 APFloat &LHS_r = LHS.getComplexFloatReal();
1630 APFloat &LHS_i = LHS.getComplexFloatImag();
1631 APFloat &RHS_r = RHS.getComplexFloatReal();
1632 APFloat &RHS_i = RHS.getComplexFloatImag();
1633
1634 APFloat Tmp = LHS_r;
1635 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1636 Result.getComplexFloatReal() = Tmp;
1637 Tmp = LHS_i;
1638 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1639 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1640
1641 Tmp = LHS_r;
1642 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1643 Result.getComplexFloatImag() = Tmp;
1644 Tmp = LHS_i;
1645 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1646 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1647 } else {
1648 APValue LHS = Result;
1649 Result.getComplexIntReal() =
1650 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1651 LHS.getComplexIntImag() * RHS.getComplexIntImag());
1652 Result.getComplexIntImag() =
1653 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1654 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1655 }
1656 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001657 }
1658
1659 return Result;
1660}
1661
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001662//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001663// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001664//===----------------------------------------------------------------------===//
1665
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001666/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001667/// any crazy technique (that has nothing to do with language standards) that
1668/// we want to. If this function returns true, it returns the folded constant
1669/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001670bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1671 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001672
Nate Begeman59b5da62009-01-18 03:20:47 +00001673 if (getType()->isVectorType()) {
1674 if (!EvaluateVector(this, Result.Val, Info))
1675 return false;
1676 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001677 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001678 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001679 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001680 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001681 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001682 } else if (getType()->isRealFloatingType()) {
1683 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001684 if (!EvaluateFloat(this, f, Info))
1685 return false;
1686
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001687 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001688 } else if (getType()->isAnyComplexType()) {
1689 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001690 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001691 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001692 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001693
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001694 return true;
1695}
1696
Anders Carlsson1b782762009-04-10 04:54:13 +00001697bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1698 EvalInfo Info(Ctx, Result);
1699
1700 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1701}
1702
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001703/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001704/// folded, but discard the result.
1705bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001706 EvalResult Result;
1707 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001708}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001709
1710APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001711 EvalResult EvalResult;
1712 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001713 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001714 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001715 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001716
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001717 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001718}