blob: ed55df3a6267cae8ccc64cf06b5ac1ff631fa72d [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);
Chris Lattnerd9becd12009-10-28 23:59:40 +000061#ifndef USEINDIRECTBRANCH
Daniel Dunbar69ab26a2009-02-20 18:22:23 +000062static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +000063#else
64static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
65 EvalInfo &Info);
66#endif
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000067static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000068static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000069
70//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000071// Misc utilities
72//===----------------------------------------------------------------------===//
73
Eli Friedman5bc86102009-06-14 02:17:33 +000074static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
75 // FIXME: Is this accurate for all kinds of bases? If not, what would
76 // the check look like?
77 Result = Value.getLValueBase() || Value.getLValueOffset();
78 return true;
79}
80
Eli Friedman4efaa272008-11-12 09:44:48 +000081static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
82 if (E->getType()->isIntegralType()) {
83 APSInt IntResult;
84 if (!EvaluateInteger(E, IntResult, Info))
85 return false;
86 Result = IntResult != 0;
87 return true;
88 } else if (E->getType()->isRealFloatingType()) {
89 APFloat FloatResult(0.0);
90 if (!EvaluateFloat(E, FloatResult, Info))
91 return false;
92 Result = !FloatResult.isZero();
93 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000094 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000095 APValue PointerResult;
96 if (!EvaluatePointer(E, PointerResult, Info))
97 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000098 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000099 } else if (E->getType()->isAnyComplexType()) {
100 APValue ComplexResult;
101 if (!EvaluateComplex(E, ComplexResult, Info))
102 return false;
103 if (ComplexResult.isComplexFloat()) {
104 Result = !ComplexResult.getComplexFloatReal().isZero() ||
105 !ComplexResult.getComplexFloatImag().isZero();
106 } else {
107 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
108 ComplexResult.getComplexIntImag().getBoolValue();
109 }
110 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000111 }
112
113 return false;
114}
115
Mike Stump1eb44332009-09-09 15:08:12 +0000116static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000117 APFloat &Value, ASTContext &Ctx) {
118 unsigned DestWidth = Ctx.getIntWidth(DestType);
119 // Determine whether we are converting to unsigned or signed.
120 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000122 // FIXME: Warning for overflow.
123 uint64_t Space[4];
124 bool ignored;
125 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
126 llvm::APFloat::rmTowardZero, &ignored);
127 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
128}
129
Mike Stump1eb44332009-09-09 15:08:12 +0000130static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000131 APFloat &Value, ASTContext &Ctx) {
132 bool ignored;
133 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000134 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000135 APFloat::rmNearestTiesToEven, &ignored);
136 return Result;
137}
138
Mike Stump1eb44332009-09-09 15:08:12 +0000139static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000140 APSInt &Value, ASTContext &Ctx) {
141 unsigned DestWidth = Ctx.getIntWidth(DestType);
142 APSInt Result = Value;
143 // Figure out if this is a truncate, extend or noop cast.
144 // If the input is signed, do a sign extend, noop, or truncate.
145 Result.extOrTrunc(DestWidth);
146 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
147 return Result;
148}
149
Mike Stump1eb44332009-09-09 15:08:12 +0000150static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000151 APSInt &Value, ASTContext &Ctx) {
152
153 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
154 Result.convertFromAPInt(Value, Value.isSigned(),
155 APFloat::rmNearestTiesToEven);
156 return Result;
157}
158
Mike Stumpc4c90452009-10-27 22:09:17 +0000159namespace {
160class VISIBILITY_HIDDEN HasSideEffect
161 : public StmtVisitor<HasSideEffect, bool> {
162 EvalInfo &Info;
163public:
164
165 HasSideEffect(EvalInfo &info) : Info(info) {}
166
167 // Unhandled nodes conservatively default to having side effects.
168 bool VisitStmt(Stmt *S) {
169 return true;
170 }
171
172 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
173 bool VisitDeclRefExpr(DeclRefExpr *E) {
174 if (E->getType().isVolatileQualified())
175 return true;
176 return false;
177 }
178 // We don't want to evaluate BlockExprs multiple times, as they generate
179 // a ton of code.
180 bool VisitBlockExpr(BlockExpr *E) { return true; }
181 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
182 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
183 { return Visit(E->getInitializer()); }
184 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
185 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
186 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
187 bool VisitStringLiteral(StringLiteral *E) { return false; }
188 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
189 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
190 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
191 { return Visit(E->getLHS()) && Visit(E->getRHS()); }
192 bool VisitChooseExpr(ChooseExpr *E)
193 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
194 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
195 bool VisitBinAssign(BinaryOperator *E) { return true; }
196 bool VisitCompoundAssign(BinaryOperator *E) { return true; }
197 bool VisitBinaryOperator(BinaryOperator *E) { return false; }
198 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
199 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
200 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
201 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
202 bool VisitUnaryDeref(UnaryOperator *E) {
203 if (E->getType().isVolatileQualified())
204 return true;
205 return false;
206 }
207 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
208};
209
210bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
211 Expr::EvalResult Result;
212 EvalInfo Info(Ctx, Result);
213
214 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
215}
216
217} // end anonymous namespace
218
Eli Friedman4efaa272008-11-12 09:44:48 +0000219//===----------------------------------------------------------------------===//
220// LValue Evaluation
221//===----------------------------------------------------------------------===//
222namespace {
223class VISIBILITY_HIDDEN LValueExprEvaluator
224 : public StmtVisitor<LValueExprEvaluator, APValue> {
225 EvalInfo &Info;
226public:
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
229
230 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000231 return APValue();
232 }
233
234 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000235 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000236 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000237 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
238 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
239 APValue VisitMemberExpr(MemberExpr *E);
240 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000241 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000242 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000243 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000244 APValue VisitUnaryExtension(const UnaryOperator *E)
245 { return Visit(E->getSubExpr()); }
246 APValue VisitChooseExpr(const ChooseExpr *E)
247 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000248
249 APValue VisitCastExpr(CastExpr *E) {
250 switch (E->getCastKind()) {
251 default:
252 return APValue();
253
254 case CastExpr::CK_NoOp:
255 return Visit(E->getSubExpr());
256 }
257 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000258 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000259};
260} // end anonymous namespace
261
262static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
263 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
264 return Result.isLValue();
265}
266
Mike Stump1eb44332009-09-09 15:08:12 +0000267APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000268 if (isa<FunctionDecl>(E->getDecl())) {
269 return APValue(E, 0);
270 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000271 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000272 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000273 if (!VD->getType()->isReferenceType())
274 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000275 // FIXME: Check whether VD might be overridden!
Eli Friedman50c39ea2009-05-27 06:04:58 +0000276 if (VD->getInit())
277 return Visit(VD->getInit());
278 }
279
280 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000281}
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000284 if (E->hasBlockDeclRefExprs())
285 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Steve Naroff3aaa4822009-04-16 19:02:57 +0000287 return APValue(E, 0);
288}
289
Eli Friedman4efaa272008-11-12 09:44:48 +0000290APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000291 if (!Info.AnyLValue && !E->isFileScope())
292 return APValue();
293 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000294}
295
296APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
297 APValue result;
298 QualType Ty;
299 if (E->isArrow()) {
300 if (!EvaluatePointer(E->getBase(), result, Info))
301 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000302 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000303 } else {
304 result = Visit(E->getBase());
305 if (result.isUninit())
306 return APValue();
307 Ty = E->getBase()->getType();
308 }
309
Ted Kremenek6217b802009-07-29 21:53:49 +0000310 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000311 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000312
313 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
314 if (!FD) // FIXME: deal with other kinds of member expressions
315 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000316
317 if (FD->getType()->isReferenceType())
318 return APValue();
319
Eli Friedman4efaa272008-11-12 09:44:48 +0000320 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000321 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000322 for (RecordDecl::field_iterator Field = RD->field_begin(),
323 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000324 Field != FieldEnd; (void)++Field, ++i) {
325 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000326 break;
327 }
328
329 result.setLValue(result.getLValueBase(),
330 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
331
332 return result;
333}
334
Mike Stump1eb44332009-09-09 15:08:12 +0000335APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000336 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Anders Carlsson3068d112008-11-16 19:01:22 +0000338 if (!EvaluatePointer(E->getBase(), Result, Info))
339 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlsson3068d112008-11-16 19:01:22 +0000341 APSInt Index;
342 if (!EvaluateInteger(E->getIdx(), Index, Info))
343 return APValue();
344
345 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
346
347 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000348 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000349 Result.getLValueOffset() + Offset);
350 return Result;
351}
Eli Friedman4efaa272008-11-12 09:44:48 +0000352
Mike Stump1eb44332009-09-09 15:08:12 +0000353APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000354 APValue Result;
355 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
356 return APValue();
357 return Result;
358}
359
Eli Friedman4efaa272008-11-12 09:44:48 +0000360//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000361// Pointer Evaluation
362//===----------------------------------------------------------------------===//
363
Anders Carlssonc754aa62008-07-08 05:13:58 +0000364namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000365class VISIBILITY_HIDDEN PointerExprEvaluator
366 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000367 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000368public:
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner87eae5e2008-07-11 22:52:41 +0000370 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000371
Anders Carlsson2bad1682008-07-08 14:30:00 +0000372 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000373 return APValue();
374 }
375
376 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
377
Anders Carlsson650c92f2008-07-08 15:34:11 +0000378 APValue VisitBinaryOperator(const BinaryOperator *E);
379 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000380 APValue VisitUnaryExtension(const UnaryOperator *E)
381 { return Visit(E->getSubExpr()); }
382 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000383 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
384 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000385 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
386 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000387 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000388 APValue VisitBlockExpr(BlockExpr *E) {
389 if (!E->hasBlockDeclRefExprs())
390 return APValue(E, 0);
391 return APValue();
392 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000393 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
394 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000395 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000396 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000397 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
398 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
399 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000400 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000401};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000402} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000403
Chris Lattner87eae5e2008-07-11 22:52:41 +0000404static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000405 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000406 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000407 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000408 return Result.isLValue();
409}
410
411APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
412 if (E->getOpcode() != BinaryOperator::Add &&
413 E->getOpcode() != BinaryOperator::Sub)
414 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 const Expr *PExp = E->getLHS();
417 const Expr *IExp = E->getRHS();
418 if (IExp->getType()->isPointerType())
419 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000421 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000422 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000423 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000425 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000426 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000427 return APValue();
428
Ted Kremenek6217b802009-07-29 21:53:49 +0000429 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000430 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000432 // Explicitly handle GNU void* and function pointer arithmetic extensions.
433 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
434 SizeOfPointee = 1;
435 else
436 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000437
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000438 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000439
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000440 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000441 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000442 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000443 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
444
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000445 return APValue(ResultLValue.getLValueBase(), Offset);
446}
Eli Friedman4efaa272008-11-12 09:44:48 +0000447
Eli Friedman2217c872009-02-22 11:46:18 +0000448APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
449 APValue result;
450 if (EvaluateLValue(E->getSubExpr(), result, Info))
451 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000452 return APValue();
453}
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000455
Chris Lattnerb542afe2008-07-11 19:10:17 +0000456APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000457 const Expr* SubExpr = E->getSubExpr();
458
459 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000460 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000461 SubExpr->getType()->isObjCObjectPointerType() ||
462 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000463 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000464 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000465 return Result;
466 return APValue();
467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000469 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000470 APValue Result;
471 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
472 return APValue();
473
474 if (Result.isInt()) {
475 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
476 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000479 // Cast is of an lvalue, no need to change value.
480 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000481 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000482
483 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000484 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000485 SubExpr->getType()->isArrayType()) {
486 APValue Result;
487 if (EvaluateLValue(SubExpr, Result, Info))
488 return Result;
489 return APValue();
490 }
491
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000492 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000493}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000494
Eli Friedman3941b182009-01-25 01:54:01 +0000495APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000496 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000497 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000498 return APValue(E, 0);
499 return APValue();
500}
501
Eli Friedman4efaa272008-11-12 09:44:48 +0000502APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
503 bool BoolResult;
504 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
505 return APValue();
506
507 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
508
509 APValue Result;
510 if (EvaluatePointer(EvalExpr, Result, Info))
511 return Result;
512 return APValue();
513}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000514
515//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000516// Vector Evaluation
517//===----------------------------------------------------------------------===//
518
519namespace {
520 class VISIBILITY_HIDDEN VectorExprEvaluator
521 : public StmtVisitor<VectorExprEvaluator, APValue> {
522 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000523 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000524 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Nate Begeman59b5da62009-01-18 03:20:47 +0000526 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Nate Begeman59b5da62009-01-18 03:20:47 +0000528 APValue VisitStmt(Stmt *S) {
529 return APValue();
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Eli Friedman91110ee2009-02-23 04:23:56 +0000532 APValue VisitParenExpr(ParenExpr *E)
533 { return Visit(E->getSubExpr()); }
534 APValue VisitUnaryExtension(const UnaryOperator *E)
535 { return Visit(E->getSubExpr()); }
536 APValue VisitUnaryPlus(const UnaryOperator *E)
537 { return Visit(E->getSubExpr()); }
538 APValue VisitUnaryReal(const UnaryOperator *E)
539 { return Visit(E->getSubExpr()); }
540 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
541 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000542 APValue VisitCastExpr(const CastExpr* E);
543 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
544 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000545 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000546 APValue VisitChooseExpr(const ChooseExpr *E)
547 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000548 APValue VisitUnaryImag(const UnaryOperator *E);
549 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000550 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000551 // shufflevector, ExtVectorElementExpr
552 // (Note that these require implementing conversions
553 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000554 };
555} // end anonymous namespace
556
557static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
558 if (!E->getType()->isVectorType())
559 return false;
560 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
561 return !Result.isUninit();
562}
563
564APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000565 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000566 QualType EltTy = VTy->getElementType();
567 unsigned NElts = VTy->getNumElements();
568 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Nate Begeman59b5da62009-01-18 03:20:47 +0000570 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000571 QualType SETy = SE->getType();
572 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000573
Nate Begemane8c9e922009-06-26 18:22:18 +0000574 // Check for vector->vector bitcast and scalar->vector splat.
575 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000576 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000577 } else if (SETy->isIntegerType()) {
578 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000579 if (!EvaluateInteger(SE, IntResult, Info))
580 return APValue();
581 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000582 } else if (SETy->isRealFloatingType()) {
583 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000584 if (!EvaluateFloat(SE, F, Info))
585 return APValue();
586 Result = APValue(F);
587 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000588 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000589
Nate Begemanc0b8b192009-07-01 07:50:47 +0000590 // For casts of a scalar to ExtVector, convert the scalar to the element type
591 // and splat it to all elements.
592 if (E->getType()->isExtVectorType()) {
593 if (EltTy->isIntegerType() && Result.isInt())
594 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
595 Info.Ctx));
596 else if (EltTy->isIntegerType())
597 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
598 Info.Ctx));
599 else if (EltTy->isRealFloatingType() && Result.isInt())
600 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
601 Info.Ctx));
602 else if (EltTy->isRealFloatingType())
603 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
604 Info.Ctx));
605 else
606 return APValue();
607
608 // Splat and create vector APValue.
609 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
610 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000611 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000612
613 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
614 // to the vector. To construct the APValue vector initializer, bitcast the
615 // initializing value to an APInt, and shift out the bits pertaining to each
616 // element.
617 APSInt Init;
618 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Nate Begemanc0b8b192009-07-01 07:50:47 +0000620 llvm::SmallVector<APValue, 4> Elts;
621 for (unsigned i = 0; i != NElts; ++i) {
622 APSInt Tmp = Init;
623 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Nate Begemanc0b8b192009-07-01 07:50:47 +0000625 if (EltTy->isIntegerType())
626 Elts.push_back(APValue(Tmp));
627 else if (EltTy->isRealFloatingType())
628 Elts.push_back(APValue(APFloat(Tmp)));
629 else
630 return APValue();
631
632 Init >>= EltWidth;
633 }
634 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000635}
636
Mike Stump1eb44332009-09-09 15:08:12 +0000637APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000638VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
639 return this->Visit(const_cast<Expr*>(E->getInitializer()));
640}
641
Mike Stump1eb44332009-09-09 15:08:12 +0000642APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000643VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000644 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000645 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000646 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Nate Begeman59b5da62009-01-18 03:20:47 +0000648 QualType EltTy = VT->getElementType();
649 llvm::SmallVector<APValue, 4> Elements;
650
Eli Friedman91110ee2009-02-23 04:23:56 +0000651 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000652 if (EltTy->isIntegerType()) {
653 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000654 if (i < NumInits) {
655 if (!EvaluateInteger(E->getInit(i), sInt, Info))
656 return APValue();
657 } else {
658 sInt = Info.Ctx.MakeIntValue(0, EltTy);
659 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000660 Elements.push_back(APValue(sInt));
661 } else {
662 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000663 if (i < NumInits) {
664 if (!EvaluateFloat(E->getInit(i), f, Info))
665 return APValue();
666 } else {
667 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
668 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000669 Elements.push_back(APValue(f));
670 }
671 }
672 return APValue(&Elements[0], Elements.size());
673}
674
Mike Stump1eb44332009-09-09 15:08:12 +0000675APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000676VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000677 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000678 QualType EltTy = VT->getElementType();
679 APValue ZeroElement;
680 if (EltTy->isIntegerType())
681 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
682 else
683 ZeroElement =
684 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
685
686 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
687 return APValue(&Elements[0], Elements.size());
688}
689
690APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
691 bool BoolResult;
692 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
693 return APValue();
694
695 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
696
697 APValue Result;
698 if (EvaluateVector(EvalExpr, Result, Info))
699 return Result;
700 return APValue();
701}
702
Eli Friedman91110ee2009-02-23 04:23:56 +0000703APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
704 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
705 Info.EvalResult.HasSideEffects = true;
706 return GetZeroVector(E->getType());
707}
708
Nate Begeman59b5da62009-01-18 03:20:47 +0000709//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000710// Integer Evaluation
711//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000712
713namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000714class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000715 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000716 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000717 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000718public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000719 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000720 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000721
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000722 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000723 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000724 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000725 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000726 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000727 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000728 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000729 return true;
730 }
731
Daniel Dunbar131eb432009-02-19 09:06:44 +0000732 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000733 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000734 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000735 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000736 Result = APValue(APSInt(I));
737 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000738 return true;
739 }
740
741 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000742 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000743 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000744 return true;
745 }
746
Anders Carlsson82206e22008-11-30 18:14:57 +0000747 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000748 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000749 if (Info.EvalResult.Diag == 0) {
750 Info.EvalResult.DiagLoc = L;
751 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000752 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000753 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000754 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Anders Carlssonc754aa62008-07-08 05:13:58 +0000757 //===--------------------------------------------------------------------===//
758 // Visitor Methods
759 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattner32fea9d2008-11-12 07:43:42 +0000761 bool VisitStmt(Stmt *) {
762 assert(0 && "This should be called on integers, stmts are not integers");
763 return false;
764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattner32fea9d2008-11-12 07:43:42 +0000766 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000767 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnerb542afe2008-07-11 19:10:17 +0000770 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000771
Chris Lattner4c4867e2008-07-12 00:38:25 +0000772 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000773 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000774 }
775 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000776 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000777 }
778 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000779 // Per gcc docs "this built-in function ignores top level
780 // qualifiers". We need to use the canonical version to properly
781 // be able to strip CRV qualifiers from the type.
782 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
783 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000784 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000785 T1.getUnqualifiedType()),
786 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000787 }
788 bool VisitDeclRefExpr(const DeclRefExpr *E);
789 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000790 bool VisitBinaryOperator(const BinaryOperator *E);
791 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000792 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000793
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000794 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000795 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
796
Anders Carlsson3068d112008-11-16 19:01:22 +0000797 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000798 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Anders Carlsson3f704562008-12-21 22:39:40 +0000801 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000802 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Anders Carlsson3068d112008-11-16 19:01:22 +0000805 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000806 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000807 }
808
Eli Friedman664a1042009-02-27 04:45:43 +0000809 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
810 return Success(0, E);
811 }
812
Sebastian Redl64b45f72009-01-05 20:52:13 +0000813 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000814 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000815 }
816
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000817 bool VisitChooseExpr(const ChooseExpr *E) {
818 return Visit(E->getChosenSubExpr(Info.Ctx));
819 }
820
Eli Friedman722c7172009-02-28 03:59:05 +0000821 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000822 bool VisitUnaryImag(const UnaryOperator *E);
823
Chris Lattnerfcee0012008-07-11 21:24:13 +0000824private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000825 unsigned GetAlignOfExpr(const Expr *E);
826 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000827 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000828};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000829} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000830
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000831static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000832 if (!E->getType()->isIntegralType())
833 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000835 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
836}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000837
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000838static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
839 APValue Val;
840 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
841 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000842 Result = Val.getInt();
843 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000844}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000845
Chris Lattner4c4867e2008-07-12 00:38:25 +0000846bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
847 // Enums are integer constant exprs.
848 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000849 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000850 // signedness consistently; see PR3173.
851 APSInt SI = D->getInitVal();
852 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
853 // FIXME: This is an ugly hack around the fact that enums don't
854 // set their width (!?!) consistently; see PR3173.
855 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
856 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000857 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000858
859 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000860 // In C, they can also be folded, although they are not ICEs.
John McCall0953e762009-09-24 19:53:00 +0000861 if (E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000862 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +0000863 if (APValue *V = D->getEvaluatedValue())
864 return Success(V->getInt(), E);
865 if (const Expr *Init = D->getInit()) {
866 if (Visit(const_cast<Expr*>(Init))) {
867 // Cache the evaluated value in the variable declaration.
868 D->setEvaluatedValue(Info.Ctx, Result);
869 return true;
870 }
871
872 return false;
873 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000874 }
875 }
876
Chris Lattner4c4867e2008-07-12 00:38:25 +0000877 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000878 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000879}
880
Chris Lattnera4d55d82008-10-06 06:40:35 +0000881/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
882/// as GCC.
883static int EvaluateBuiltinClassifyType(const CallExpr *E) {
884 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000885 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000886 enum gcc_type_class {
887 no_type_class = -1,
888 void_type_class, integer_type_class, char_type_class,
889 enumeral_type_class, boolean_type_class,
890 pointer_type_class, reference_type_class, offset_type_class,
891 real_type_class, complex_type_class,
892 function_type_class, method_type_class,
893 record_type_class, union_type_class,
894 array_type_class, string_type_class,
895 lang_type_class
896 };
Mike Stump1eb44332009-09-09 15:08:12 +0000897
898 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000899 // ideal, however it is what gcc does.
900 if (E->getNumArgs() == 0)
901 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattnera4d55d82008-10-06 06:40:35 +0000903 QualType ArgTy = E->getArg(0)->getType();
904 if (ArgTy->isVoidType())
905 return void_type_class;
906 else if (ArgTy->isEnumeralType())
907 return enumeral_type_class;
908 else if (ArgTy->isBooleanType())
909 return boolean_type_class;
910 else if (ArgTy->isCharType())
911 return string_type_class; // gcc doesn't appear to use char_type_class
912 else if (ArgTy->isIntegerType())
913 return integer_type_class;
914 else if (ArgTy->isPointerType())
915 return pointer_type_class;
916 else if (ArgTy->isReferenceType())
917 return reference_type_class;
918 else if (ArgTy->isRealType())
919 return real_type_class;
920 else if (ArgTy->isComplexType())
921 return complex_type_class;
922 else if (ArgTy->isFunctionType())
923 return function_type_class;
924 else if (ArgTy->isStructureType())
925 return record_type_class;
926 else if (ArgTy->isUnionType())
927 return union_type_class;
928 else if (ArgTy->isArrayType())
929 return array_type_class;
930 else if (ArgTy->isUnionType())
931 return union_type_class;
932 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
933 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
934 return -1;
935}
936
Chris Lattner4c4867e2008-07-12 00:38:25 +0000937bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000938 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000939 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000940 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000941
942 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000943 const Expr *Arg = E->getArg(0)->IgnoreParens();
944 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000945 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000946 && Base.Val.getKind() == APValue::LValue
947 && !Base.HasSideEffects)
948 if (const Expr *LVBase = Base.Val.getLValueBase())
949 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
950 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000951 if (!VD->getType()->isIncompleteType()
952 && VD->getType()->isObjectType()
953 && !VD->getType()->isVariablyModifiedType()
954 && !VD->getType()->isDependentType()) {
955 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
956 uint64_t Offset = Base.Val.getLValueOffset();
957 if (Offset <= Size)
958 Size -= Base.Val.getLValueOffset();
959 else
960 Size = 0;
961 return Success(Size, E);
962 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000963 }
964 }
965
Mike Stumpc4c90452009-10-27 22:09:17 +0000966 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump4fc87582009-10-26 18:57:47 +0000967 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump64eda9e2009-10-26 18:35:08 +0000968 return Success(-1, E);
969 return Success(0, E);
970 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000971
Mike Stump64eda9e2009-10-26 18:35:08 +0000972 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
973 }
974
Chris Lattner019f4e82008-10-06 05:28:25 +0000975 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000976 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000978 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000979 // __builtin_constant_p always has one operand: it returns true if that
980 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000981 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000982
983 case Builtin::BI__builtin_eh_return_data_regno: {
984 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
985 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
986 return Success(Operand, E);
987 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000988 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000989}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000990
Chris Lattnerb542afe2008-07-11 19:10:17 +0000991bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000992 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000993 if (!Visit(E->getRHS()))
994 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000995
Eli Friedman33ef1452009-02-26 10:19:36 +0000996 // If we can't evaluate the LHS, it might have side effects;
997 // conservatively mark it.
998 if (!E->getLHS()->isEvaluatable(Info.Ctx))
999 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001000
Anders Carlsson027f62e2008-12-01 02:07:06 +00001001 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001002 }
1003
1004 if (E->isLogicalOp()) {
1005 // These need to be handled specially because the operands aren't
1006 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001007 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001009 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001010 // We were able to evaluate the LHS, see if we can get away with not
1011 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001012 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001013 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001014
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001015 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001016 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001017 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001018 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001019 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001020 }
1021 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001022 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001023 // We can't evaluate the LHS; however, sometimes the result
1024 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001025 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001026 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001027 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001028 // must have had side effects.
1029 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001030
1031 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001032 }
1033 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001034 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001035
Eli Friedmana6afa762008-11-13 06:09:17 +00001036 return false;
1037 }
1038
Anders Carlsson286f85e2008-11-16 07:17:21 +00001039 QualType LHSTy = E->getLHS()->getType();
1040 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001041
1042 if (LHSTy->isAnyComplexType()) {
1043 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1044 APValue LHS, RHS;
1045
1046 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1047 return false;
1048
1049 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1050 return false;
1051
1052 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001053 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001054 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001055 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001056 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1057
Daniel Dunbar4087e242009-01-29 06:43:41 +00001058 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001059 return Success((CR_r == APFloat::cmpEqual &&
1060 CR_i == APFloat::cmpEqual), E);
1061 else {
1062 assert(E->getOpcode() == BinaryOperator::NE &&
1063 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001064 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001065 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001066 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001067 CR_i == APFloat::cmpLessThan)), E);
1068 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001069 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001070 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001071 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1072 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1073 else {
1074 assert(E->getOpcode() == BinaryOperator::NE &&
1075 "Invalid compex comparison.");
1076 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1077 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1078 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001079 }
1080 }
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Anders Carlsson286f85e2008-11-16 07:17:21 +00001082 if (LHSTy->isRealFloatingType() &&
1083 RHSTy->isRealFloatingType()) {
1084 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Anders Carlsson286f85e2008-11-16 07:17:21 +00001086 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1087 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Anders Carlsson286f85e2008-11-16 07:17:21 +00001089 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1090 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Anders Carlsson286f85e2008-11-16 07:17:21 +00001092 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001093
Anders Carlsson286f85e2008-11-16 07:17:21 +00001094 switch (E->getOpcode()) {
1095 default:
1096 assert(0 && "Invalid binary operator!");
1097 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001098 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001099 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001100 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001101 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001102 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001103 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001104 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001105 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001106 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001107 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001108 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001109 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001110 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001111 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001114 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1115 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001116 APValue LHSValue;
1117 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1118 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001119
Anders Carlsson3068d112008-11-16 19:01:22 +00001120 APValue RHSValue;
1121 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1122 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001123
Eli Friedman5bc86102009-06-14 02:17:33 +00001124 // Reject any bases from the normal codepath; we special-case comparisons
1125 // to null.
1126 if (LHSValue.getLValueBase()) {
1127 if (!E->isEqualityOp())
1128 return false;
1129 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1130 return false;
1131 bool bres;
1132 if (!EvalPointerValueAsBool(LHSValue, bres))
1133 return false;
1134 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1135 } else if (RHSValue.getLValueBase()) {
1136 if (!E->isEqualityOp())
1137 return false;
1138 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1139 return false;
1140 bool bres;
1141 if (!EvalPointerValueAsBool(RHSValue, bres))
1142 return false;
1143 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1144 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001145
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001146 if (E->getOpcode() == BinaryOperator::Sub) {
1147 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001148 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001149
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001150 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001151 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1152 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001153
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001154 return Success(D, E);
1155 }
1156 bool Result;
1157 if (E->getOpcode() == BinaryOperator::EQ) {
1158 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001159 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001160 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1161 }
1162 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001163 }
1164 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001165 if (!LHSTy->isIntegralType() ||
1166 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001167 // We can't continue from here for non-integral types, and they
1168 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001169 return false;
1170 }
1171
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001172 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001173 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001174 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001175
Eli Friedman42edd0d2009-03-24 01:14:50 +00001176 APValue RHSVal;
1177 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001178 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001179
1180 // Handle cases like (unsigned long)&a + 4.
1181 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1182 uint64_t offset = Result.getLValueOffset();
1183 if (E->getOpcode() == BinaryOperator::Add)
1184 offset += RHSVal.getInt().getZExtValue();
1185 else
1186 offset -= RHSVal.getInt().getZExtValue();
1187 Result = APValue(Result.getLValueBase(), offset);
1188 return true;
1189 }
1190
1191 // Handle cases like 4 + (unsigned long)&a
1192 if (E->getOpcode() == BinaryOperator::Add &&
1193 RHSVal.isLValue() && Result.isInt()) {
1194 uint64_t offset = RHSVal.getLValueOffset();
1195 offset += Result.getInt().getZExtValue();
1196 Result = APValue(RHSVal.getLValueBase(), offset);
1197 return true;
1198 }
1199
1200 // All the following cases expect both operands to be an integer
1201 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001202 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001203
Eli Friedman42edd0d2009-03-24 01:14:50 +00001204 APSInt& RHS = RHSVal.getInt();
1205
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001206 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001207 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001208 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001209 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1210 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1211 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1212 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1213 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1214 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001215 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001216 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001217 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001218 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001219 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001220 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001221 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001222 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001223 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001224 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001225 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001226 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1227 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001228 }
1229 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001230 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001231 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1232 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001235 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1236 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1237 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1238 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1239 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1240 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001241 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001242}
1243
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001244bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001245 bool Cond;
1246 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001247 return false;
1248
Nuno Lopesa25bd552008-11-16 22:06:39 +00001249 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001250}
1251
Chris Lattneraf707ab2009-01-24 21:53:27 +00001252unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001253 // Get information about the alignment.
1254 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001255
Eli Friedman2be58612009-05-30 21:09:44 +00001256 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001257 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001258}
1259
Chris Lattneraf707ab2009-01-24 21:53:27 +00001260unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1261 E = E->IgnoreParens();
1262
1263 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001264 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001265 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001266 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001267
Chris Lattneraf707ab2009-01-24 21:53:27 +00001268 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001269 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001270
Chris Lattnere9feb472009-01-24 21:09:06 +00001271 return GetAlignOfType(E->getType());
1272}
1273
1274
Sebastian Redl05189992008-11-11 17:56:53 +00001275/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1276/// expression's type.
1277bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1278 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001279
Chris Lattnere9feb472009-01-24 21:09:06 +00001280 // Handle alignof separately.
1281 if (!E->isSizeOf()) {
1282 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001283 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001284 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001285 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001286 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001287
Sebastian Redl05189992008-11-11 17:56:53 +00001288 QualType SrcTy = E->getTypeOfArgument();
1289
Daniel Dunbar131eb432009-02-19 09:06:44 +00001290 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1291 // extension.
1292 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1293 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001294
Chris Lattnerfcee0012008-07-11 21:24:13 +00001295 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001296 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001297 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001298
Chris Lattnere9feb472009-01-24 21:09:06 +00001299 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001300 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001301 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001302}
1303
Chris Lattnerb542afe2008-07-11 19:10:17 +00001304bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001305 // Special case unary operators that do not need their subexpression
1306 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001307 if (E->isOffsetOfOp()) {
1308 // The AST for offsetof is defined in such a way that we can just
1309 // directly Evaluate it as an l-value.
1310 APValue LV;
1311 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1312 return false;
1313 if (LV.getLValueBase())
1314 return false;
1315 return Success(LV.getLValueOffset(), E);
1316 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001317
1318 if (E->getOpcode() == UnaryOperator::LNot) {
1319 // LNot's operand isn't necessarily an integer, so we handle it specially.
1320 bool bres;
1321 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1322 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001323 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001324 }
1325
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001326 // Only handle integral operations...
1327 if (!E->getSubExpr()->getType()->isIntegralType())
1328 return false;
1329
Chris Lattner87eae5e2008-07-11 22:52:41 +00001330 // Get the operand value into 'Result'.
1331 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001332 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001333
Chris Lattner75a48812008-07-11 22:15:16 +00001334 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001335 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001336 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1337 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001338 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001339 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001340 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1341 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001342 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001343 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001344 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001345 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001346 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001347 if (!Result.isInt()) return false;
1348 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001349 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001350 if (!Result.isInt()) return false;
1351 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001352 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001353}
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Chris Lattner732b2232008-07-12 01:15:53 +00001355/// HandleCast - This is used to evaluate implicit or explicit casts where the
1356/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001357bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001358 Expr *SubExpr = E->getSubExpr();
1359 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001360 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001361
Eli Friedman4efaa272008-11-12 09:44:48 +00001362 if (DestType->isBooleanType()) {
1363 bool BoolResult;
1364 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1365 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001366 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001367 }
1368
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001369 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001370 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001371 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001372 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001373
Eli Friedmanbe265702009-02-20 01:15:07 +00001374 if (!Result.isInt()) {
1375 // Only allow casts of lvalues if they are lossless.
1376 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1377 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001378
Daniel Dunbardd211642009-02-19 22:24:01 +00001379 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001380 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Chris Lattner732b2232008-07-12 01:15:53 +00001383 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001384 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001385 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001386 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001387 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001388
Daniel Dunbardd211642009-02-19 22:24:01 +00001389 if (LV.getLValueBase()) {
1390 // Only allow based lvalue casts if they are lossless.
1391 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1392 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001393
Daniel Dunbardd211642009-02-19 22:24:01 +00001394 Result = LV;
1395 return true;
1396 }
1397
1398 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1399 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001400 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001401
Eli Friedmanbe265702009-02-20 01:15:07 +00001402 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1403 // This handles double-conversion cases, where there's both
1404 // an l-value promotion and an implicit conversion to int.
1405 APValue LV;
1406 if (!EvaluateLValue(SubExpr, LV, Info))
1407 return false;
1408
1409 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1410 return false;
1411
1412 Result = LV;
1413 return true;
1414 }
1415
Eli Friedman1725f682009-04-22 19:23:09 +00001416 if (SrcType->isAnyComplexType()) {
1417 APValue C;
1418 if (!EvaluateComplex(SubExpr, C, Info))
1419 return false;
1420 if (C.isComplexFloat())
1421 return Success(HandleFloatToIntCast(DestType, SrcType,
1422 C.getComplexFloatReal(), Info.Ctx),
1423 E);
1424 else
1425 return Success(HandleIntToIntCast(DestType, SrcType,
1426 C.getComplexIntReal(), Info.Ctx), E);
1427 }
Eli Friedman2217c872009-02-22 11:46:18 +00001428 // FIXME: Handle vectors
1429
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001430 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001431 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001432
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001433 APFloat F(0.0);
1434 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001435 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001437 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001438}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001439
Eli Friedman722c7172009-02-28 03:59:05 +00001440bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1441 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1442 APValue LV;
1443 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1444 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1445 return Success(LV.getComplexIntReal(), E);
1446 }
1447
1448 return Visit(E->getSubExpr());
1449}
1450
Eli Friedman664a1042009-02-27 04:45:43 +00001451bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001452 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1453 APValue LV;
1454 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1455 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1456 return Success(LV.getComplexIntImag(), E);
1457 }
1458
Eli Friedman664a1042009-02-27 04:45:43 +00001459 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1460 Info.EvalResult.HasSideEffects = true;
1461 return Success(0, E);
1462}
1463
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001464//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001465// Float Evaluation
1466//===----------------------------------------------------------------------===//
1467
1468namespace {
1469class VISIBILITY_HIDDEN FloatExprEvaluator
1470 : public StmtVisitor<FloatExprEvaluator, bool> {
1471 EvalInfo &Info;
1472 APFloat &Result;
1473public:
1474 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1475 : Info(info), Result(result) {}
1476
1477 bool VisitStmt(Stmt *S) {
1478 return false;
1479 }
1480
1481 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001482 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001483
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001484 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001485 bool VisitBinaryOperator(const BinaryOperator *E);
1486 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001487 bool VisitCastExpr(CastExpr *E);
1488 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001489
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001490 bool VisitChooseExpr(const ChooseExpr *E)
1491 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1492 bool VisitUnaryExtension(const UnaryOperator *E)
1493 { return Visit(E->getSubExpr()); }
1494
1495 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1496 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001497 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001498};
1499} // end anonymous namespace
1500
1501static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1502 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1503}
1504
Chris Lattner019f4e82008-10-06 05:28:25 +00001505bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001506 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001507 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001508 case Builtin::BI__builtin_huge_val:
1509 case Builtin::BI__builtin_huge_valf:
1510 case Builtin::BI__builtin_huge_vall:
1511 case Builtin::BI__builtin_inf:
1512 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001513 case Builtin::BI__builtin_infl: {
1514 const llvm::fltSemantics &Sem =
1515 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001516 Result = llvm::APFloat::getInf(Sem);
1517 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Chris Lattner9e621712008-10-06 06:31:58 +00001520 case Builtin::BI__builtin_nan:
1521 case Builtin::BI__builtin_nanf:
1522 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001523 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001524 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001525 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001526 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001527 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001528 const llvm::fltSemantics &Sem =
1529 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001530 llvm::SmallString<16> s;
1531 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1532 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001533 long l;
1534 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001535 l = strtol(&s[0], &endp, 0);
1536 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001537 return false;
1538 unsigned type = (unsigned int)l;;
1539 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001540 return true;
1541 }
1542 }
1543 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001544
1545 case Builtin::BI__builtin_fabs:
1546 case Builtin::BI__builtin_fabsf:
1547 case Builtin::BI__builtin_fabsl:
1548 if (!EvaluateFloat(E->getArg(0), Result, Info))
1549 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001551 if (Result.isNegative())
1552 Result.changeSign();
1553 return true;
1554
Mike Stump1eb44332009-09-09 15:08:12 +00001555 case Builtin::BI__builtin_copysign:
1556 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001557 case Builtin::BI__builtin_copysignl: {
1558 APFloat RHS(0.);
1559 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1560 !EvaluateFloat(E->getArg(1), RHS, Info))
1561 return false;
1562 Result.copySign(RHS);
1563 return true;
1564 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001565 }
1566}
1567
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001568bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001569 if (E->getOpcode() == UnaryOperator::Deref)
1570 return false;
1571
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001572 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1573 return false;
1574
1575 switch (E->getOpcode()) {
1576 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001577 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001578 return true;
1579 case UnaryOperator::Minus:
1580 Result.changeSign();
1581 return true;
1582 }
1583}
Chris Lattner019f4e82008-10-06 05:28:25 +00001584
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001585bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1586 // FIXME: Diagnostics? I really don't understand how the warnings
1587 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001588 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001589 if (!EvaluateFloat(E->getLHS(), Result, Info))
1590 return false;
1591 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1592 return false;
1593
1594 switch (E->getOpcode()) {
1595 default: return false;
1596 case BinaryOperator::Mul:
1597 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1598 return true;
1599 case BinaryOperator::Add:
1600 Result.add(RHS, APFloat::rmNearestTiesToEven);
1601 return true;
1602 case BinaryOperator::Sub:
1603 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1604 return true;
1605 case BinaryOperator::Div:
1606 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1607 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001608 }
1609}
1610
1611bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1612 Result = E->getValue();
1613 return true;
1614}
1615
Eli Friedman4efaa272008-11-12 09:44:48 +00001616bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1617 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Eli Friedman4efaa272008-11-12 09:44:48 +00001619 if (SubExpr->getType()->isIntegralType()) {
1620 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001621 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001622 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001623 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001624 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001625 return true;
1626 }
1627 if (SubExpr->getType()->isRealFloatingType()) {
1628 if (!Visit(SubExpr))
1629 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001630 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1631 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001632 return true;
1633 }
Eli Friedman2217c872009-02-22 11:46:18 +00001634 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001635
1636 return false;
1637}
1638
1639bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1640 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1641 return true;
1642}
1643
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001644//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001645// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001646//===----------------------------------------------------------------------===//
1647
1648namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001649class VISIBILITY_HIDDEN ComplexExprEvaluator
1650 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001651 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001653public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001654 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001656 //===--------------------------------------------------------------------===//
1657 // Visitor Methods
1658 //===--------------------------------------------------------------------===//
1659
1660 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001661 return APValue();
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001664 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1665
1666 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001667 Expr* SubExpr = E->getSubExpr();
1668
1669 if (SubExpr->getType()->isRealFloatingType()) {
1670 APFloat Result(0.0);
1671
1672 if (!EvaluateFloat(SubExpr, Result, Info))
1673 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001674
1675 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001676 Result);
1677 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001678 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001679 "Unexpected imaginary literal.");
1680
1681 llvm::APSInt Result;
1682 if (!EvaluateInteger(SubExpr, Result, Info))
1683 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001685 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1686 Zero = 0;
1687 return APValue(Zero, Result);
1688 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001689 }
1690
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001691 APValue VisitCastExpr(CastExpr *E) {
1692 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001693 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001694 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001695
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001696 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001697 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001698
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001699 if (!EvaluateFloat(SubExpr, Result, Info))
1700 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001701
1702 if (EltType->isRealFloatingType()) {
1703 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001704 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001705 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1706 } else {
1707 llvm::APSInt IResult;
1708 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1709 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1710 Zero = 0;
1711 return APValue(IResult, Zero);
1712 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001713 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001714 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001715
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001716 if (!EvaluateInteger(SubExpr, Result, Info))
1717 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001718
Eli Friedman1725f682009-04-22 19:23:09 +00001719 if (EltType->isRealFloatingType()) {
1720 APFloat FResult =
1721 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001722 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001723 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1724 } else {
1725 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1726 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1727 Zero = 0;
1728 return APValue(Result, Zero);
1729 }
John McCall183700f2009-09-21 23:43:11 +00001730 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001731 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001732
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001733 if (!EvaluateComplex(SubExpr, Src, Info))
1734 return APValue();
1735
1736 QualType SrcType = CT->getElementType();
1737
1738 if (Src.isComplexFloat()) {
1739 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001740 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001741 Src.getComplexFloatReal(),
1742 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001743 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001744 Src.getComplexFloatImag(),
1745 Info.Ctx));
1746 } else {
1747 return APValue(HandleFloatToIntCast(EltType, SrcType,
1748 Src.getComplexFloatReal(),
1749 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001750 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001751 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001752 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001753 }
1754 } else {
1755 assert(Src.isComplexInt() && "Invalid evaluate result.");
1756 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001757 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001758 Src.getComplexIntReal(),
1759 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001760 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001761 Src.getComplexIntImag(),
1762 Info.Ctx));
1763 } else {
1764 return APValue(HandleIntToIntCast(EltType, SrcType,
1765 Src.getComplexIntReal(),
1766 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001767 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001768 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001769 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001770 }
1771 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001772 }
1773
1774 // FIXME: Handle more casts.
1775 return APValue();
1776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001778 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001779 APValue VisitChooseExpr(const ChooseExpr *E)
1780 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1781 APValue VisitUnaryExtension(const UnaryOperator *E)
1782 { return Visit(E->getSubExpr()); }
1783 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001784 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001785};
1786} // end anonymous namespace
1787
Mike Stump1eb44332009-09-09 15:08:12 +00001788static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001789 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1790 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001791 (&Result.getComplexFloatReal().getSemantics() ==
1792 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001793 "Invalid complex evaluation.");
1794 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001795}
1796
Mike Stump1eb44332009-09-09 15:08:12 +00001797APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001798 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001800 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001801 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001803 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001804 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001805
Daniel Dunbar3f279872009-01-29 01:32:56 +00001806 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1807 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001808 switch (E->getOpcode()) {
1809 default: return APValue();
1810 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001811 if (Result.isComplexFloat()) {
1812 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1813 APFloat::rmNearestTiesToEven);
1814 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1815 APFloat::rmNearestTiesToEven);
1816 } else {
1817 Result.getComplexIntReal() += RHS.getComplexIntReal();
1818 Result.getComplexIntImag() += RHS.getComplexIntImag();
1819 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001820 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001821 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001822 if (Result.isComplexFloat()) {
1823 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1824 APFloat::rmNearestTiesToEven);
1825 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1826 APFloat::rmNearestTiesToEven);
1827 } else {
1828 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1829 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1830 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001831 break;
1832 case BinaryOperator::Mul:
1833 if (Result.isComplexFloat()) {
1834 APValue LHS = Result;
1835 APFloat &LHS_r = LHS.getComplexFloatReal();
1836 APFloat &LHS_i = LHS.getComplexFloatImag();
1837 APFloat &RHS_r = RHS.getComplexFloatReal();
1838 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Daniel Dunbar3f279872009-01-29 01:32:56 +00001840 APFloat Tmp = LHS_r;
1841 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1842 Result.getComplexFloatReal() = Tmp;
1843 Tmp = LHS_i;
1844 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1845 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1846
1847 Tmp = LHS_r;
1848 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1849 Result.getComplexFloatImag() = Tmp;
1850 Tmp = LHS_i;
1851 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1852 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1853 } else {
1854 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001855 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001856 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1857 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001858 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001859 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1860 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1861 }
1862 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001863 }
1864
1865 return Result;
1866}
1867
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001868//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001869// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001870//===----------------------------------------------------------------------===//
1871
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001872/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001873/// any crazy technique (that has nothing to do with language standards) that
1874/// we want to. If this function returns true, it returns the folded constant
1875/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001876bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1877 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001878
Nate Begeman59b5da62009-01-18 03:20:47 +00001879 if (getType()->isVectorType()) {
1880 if (!EvaluateVector(this, Result.Val, Info))
1881 return false;
1882 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001883 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001884 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001885 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001886 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001887 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001888 } else if (getType()->isRealFloatingType()) {
1889 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001890 if (!EvaluateFloat(this, f, Info))
1891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001893 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001894 } else if (getType()->isAnyComplexType()) {
1895 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001896 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001897 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001898 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001899
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001900 return true;
1901}
1902
Mike Stump660e6f72009-10-26 23:05:19 +00001903bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1904 EvalInfo Info(Ctx, Result, true);
1905
1906 if (getType()->isVectorType()) {
1907 if (!EvaluateVector(this, Result.Val, Info))
1908 return false;
1909 } else if (getType()->isIntegerType()) {
1910 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1911 return false;
1912 } else if (getType()->hasPointerRepresentation()) {
1913 if (!EvaluatePointer(this, Result.Val, Info))
1914 return false;
1915 } else if (getType()->isRealFloatingType()) {
1916 llvm::APFloat f(0.0);
1917 if (!EvaluateFloat(this, f, Info))
1918 return false;
1919
1920 Result.Val = APValue(f);
1921 } else if (getType()->isAnyComplexType()) {
1922 if (!EvaluateComplex(this, Result.Val, Info))
1923 return false;
1924 } else
1925 return false;
1926
1927 return true;
1928}
1929
Anders Carlsson1b782762009-04-10 04:54:13 +00001930bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1931 EvalInfo Info(Ctx, Result);
1932
1933 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1934}
1935
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001936bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1937 EvalInfo Info(Ctx, Result, true);
1938
1939 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1940}
1941
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001942/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001943/// folded, but discard the result.
1944bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001945 EvalResult Result;
1946 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001947}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001948
1949APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001950 EvalResult EvalResult;
1951 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001952 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001953 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001954 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001955
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001956 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001957}