blob: 804684cc2bf6ce9fb0068d40ccca4b77a41974de [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 +000061static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
62 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000063static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +000064static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +000065
66//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +000067// Misc utilities
68//===----------------------------------------------------------------------===//
69
Eli Friedman5bc86102009-06-14 02:17:33 +000070static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
71 // FIXME: Is this accurate for all kinds of bases? If not, what would
72 // the check look like?
73 Result = Value.getLValueBase() || Value.getLValueOffset();
74 return true;
75}
76
Eli Friedman4efaa272008-11-12 09:44:48 +000077static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
78 if (E->getType()->isIntegralType()) {
79 APSInt IntResult;
80 if (!EvaluateInteger(E, IntResult, Info))
81 return false;
82 Result = IntResult != 0;
83 return true;
84 } else if (E->getType()->isRealFloatingType()) {
85 APFloat FloatResult(0.0);
86 if (!EvaluateFloat(E, FloatResult, Info))
87 return false;
88 Result = !FloatResult.isZero();
89 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +000090 } else if (E->getType()->hasPointerRepresentation()) {
Eli Friedman4efaa272008-11-12 09:44:48 +000091 APValue PointerResult;
92 if (!EvaluatePointer(E, PointerResult, Info))
93 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +000094 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +000095 } else if (E->getType()->isAnyComplexType()) {
96 APValue ComplexResult;
97 if (!EvaluateComplex(E, ComplexResult, Info))
98 return false;
99 if (ComplexResult.isComplexFloat()) {
100 Result = !ComplexResult.getComplexFloatReal().isZero() ||
101 !ComplexResult.getComplexFloatImag().isZero();
102 } else {
103 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
104 ComplexResult.getComplexIntImag().getBoolValue();
105 }
106 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000107 }
108
109 return false;
110}
111
Mike Stump1eb44332009-09-09 15:08:12 +0000112static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000113 APFloat &Value, ASTContext &Ctx) {
114 unsigned DestWidth = Ctx.getIntWidth(DestType);
115 // Determine whether we are converting to unsigned or signed.
116 bool DestSigned = DestType->isSignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000118 // FIXME: Warning for overflow.
119 uint64_t Space[4];
120 bool ignored;
121 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
122 llvm::APFloat::rmTowardZero, &ignored);
123 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
124}
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000127 APFloat &Value, ASTContext &Ctx) {
128 bool ignored;
129 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000130 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000131 APFloat::rmNearestTiesToEven, &ignored);
132 return Result;
133}
134
Mike Stump1eb44332009-09-09 15:08:12 +0000135static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000136 APSInt &Value, ASTContext &Ctx) {
137 unsigned DestWidth = Ctx.getIntWidth(DestType);
138 APSInt Result = Value;
139 // Figure out if this is a truncate, extend or noop cast.
140 // If the input is signed, do a sign extend, noop, or truncate.
141 Result.extOrTrunc(DestWidth);
142 Result.setIsUnsigned(DestType->isUnsignedIntegerType());
143 return Result;
144}
145
Mike Stump1eb44332009-09-09 15:08:12 +0000146static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000147 APSInt &Value, ASTContext &Ctx) {
148
149 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
150 Result.convertFromAPInt(Value, Value.isSigned(),
151 APFloat::rmNearestTiesToEven);
152 return Result;
153}
154
Mike Stumpc4c90452009-10-27 22:09:17 +0000155namespace {
156class VISIBILITY_HIDDEN HasSideEffect
157 : public StmtVisitor<HasSideEffect, bool> {
158 EvalInfo &Info;
159public:
160
161 HasSideEffect(EvalInfo &info) : Info(info) {}
162
163 // Unhandled nodes conservatively default to having side effects.
164 bool VisitStmt(Stmt *S) {
165 return true;
166 }
167
168 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
169 bool VisitDeclRefExpr(DeclRefExpr *E) {
170 if (E->getType().isVolatileQualified())
171 return true;
172 return false;
173 }
174 // We don't want to evaluate BlockExprs multiple times, as they generate
175 // a ton of code.
176 bool VisitBlockExpr(BlockExpr *E) { return true; }
177 bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
178 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
179 { return Visit(E->getInitializer()); }
180 bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
181 bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
182 bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
183 bool VisitStringLiteral(StringLiteral *E) { return false; }
184 bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
185 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
186 bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000187 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000188 bool VisitChooseExpr(ChooseExpr *E)
189 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
190 bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
191 bool VisitBinAssign(BinaryOperator *E) { return true; }
Mike Stump3f0147e2009-10-29 23:34:20 +0000192 bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
Mike Stump980ca222009-10-29 20:48:09 +0000193 bool VisitBinaryOperator(BinaryOperator *E)
194 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Mike Stumpc4c90452009-10-27 22:09:17 +0000195 bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
196 bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
197 bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
198 bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
199 bool VisitUnaryDeref(UnaryOperator *E) {
200 if (E->getType().isVolatileQualified())
201 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000202 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000203 }
204 bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
205};
206
207bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
208 Expr::EvalResult Result;
209 EvalInfo Info(Ctx, Result);
210
211 return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
212}
213
214} // end anonymous namespace
215
Eli Friedman4efaa272008-11-12 09:44:48 +0000216//===----------------------------------------------------------------------===//
217// LValue Evaluation
218//===----------------------------------------------------------------------===//
219namespace {
220class VISIBILITY_HIDDEN LValueExprEvaluator
221 : public StmtVisitor<LValueExprEvaluator, APValue> {
222 EvalInfo &Info;
223public:
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Eli Friedman4efaa272008-11-12 09:44:48 +0000225 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
226
227 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000228 return APValue();
229 }
230
231 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000232 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000233 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000234 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
235 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
236 APValue VisitMemberExpr(MemberExpr *E);
237 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000238 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000239 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000240 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000241 APValue VisitUnaryExtension(const UnaryOperator *E)
242 { return Visit(E->getSubExpr()); }
243 APValue VisitChooseExpr(const ChooseExpr *E)
244 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000245
246 APValue VisitCastExpr(CastExpr *E) {
247 switch (E->getCastKind()) {
248 default:
249 return APValue();
250
251 case CastExpr::CK_NoOp:
252 return Visit(E->getSubExpr());
253 }
254 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000255 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000256};
257} // end anonymous namespace
258
259static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
260 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
261 return Result.isLValue();
262}
263
Mike Stump1eb44332009-09-09 15:08:12 +0000264APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000265 if (isa<FunctionDecl>(E->getDecl())) {
266 return APValue(E, 0);
267 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000268 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000269 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000270 if (!VD->getType()->isReferenceType())
271 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000272 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000273 const VarDecl *Def = 0;
274 if (const Expr *Init = VD->getDefinition(Def))
275 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000276 }
277
278 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000279}
280
Mike Stump1eb44332009-09-09 15:08:12 +0000281APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000282 if (E->hasBlockDeclRefExprs())
283 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff3aaa4822009-04-16 19:02:57 +0000285 return APValue(E, 0);
286}
287
Eli Friedman4efaa272008-11-12 09:44:48 +0000288APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000289 if (!Info.AnyLValue && !E->isFileScope())
290 return APValue();
291 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000292}
293
294APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
295 APValue result;
296 QualType Ty;
297 if (E->isArrow()) {
298 if (!EvaluatePointer(E->getBase(), result, Info))
299 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000300 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000301 } else {
302 result = Visit(E->getBase());
303 if (result.isUninit())
304 return APValue();
305 Ty = E->getBase()->getType();
306 }
307
Ted Kremenek6217b802009-07-29 21:53:49 +0000308 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000309 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000310
311 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
312 if (!FD) // FIXME: deal with other kinds of member expressions
313 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000314
315 if (FD->getType()->isReferenceType())
316 return APValue();
317
Eli Friedman4efaa272008-11-12 09:44:48 +0000318 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000319 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000320 for (RecordDecl::field_iterator Field = RD->field_begin(),
321 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000322 Field != FieldEnd; (void)++Field, ++i) {
323 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000324 break;
325 }
326
327 result.setLValue(result.getLValueBase(),
328 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
329
330 return result;
331}
332
Mike Stump1eb44332009-09-09 15:08:12 +0000333APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000334 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlsson3068d112008-11-16 19:01:22 +0000336 if (!EvaluatePointer(E->getBase(), Result, Info))
337 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlsson3068d112008-11-16 19:01:22 +0000339 APSInt Index;
340 if (!EvaluateInteger(E->getIdx(), Index, Info))
341 return APValue();
342
343 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
344
345 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000346 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000347 Result.getLValueOffset() + Offset);
348 return Result;
349}
Eli Friedman4efaa272008-11-12 09:44:48 +0000350
Mike Stump1eb44332009-09-09 15:08:12 +0000351APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000352 APValue Result;
353 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
354 return APValue();
355 return Result;
356}
357
Eli Friedman4efaa272008-11-12 09:44:48 +0000358//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000359// Pointer Evaluation
360//===----------------------------------------------------------------------===//
361
Anders Carlssonc754aa62008-07-08 05:13:58 +0000362namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000363class VISIBILITY_HIDDEN PointerExprEvaluator
364 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000365 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000366public:
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattner87eae5e2008-07-11 22:52:41 +0000368 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000369
Anders Carlsson2bad1682008-07-08 14:30:00 +0000370 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000371 return APValue();
372 }
373
374 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
375
Anders Carlsson650c92f2008-07-08 15:34:11 +0000376 APValue VisitBinaryOperator(const BinaryOperator *E);
377 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000378 APValue VisitUnaryExtension(const UnaryOperator *E)
379 { return Visit(E->getSubExpr()); }
380 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000381 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
382 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000383 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
384 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000385 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000386 APValue VisitBlockExpr(BlockExpr *E) {
387 if (!E->hasBlockDeclRefExprs())
388 return APValue(E, 0);
389 return APValue();
390 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000391 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
392 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000393 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000394 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000395 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
396 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
397 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000398 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000399};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000400} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000401
Chris Lattner87eae5e2008-07-11 22:52:41 +0000402static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000403 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000404 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000405 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000406 return Result.isLValue();
407}
408
409APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
410 if (E->getOpcode() != BinaryOperator::Add &&
411 E->getOpcode() != BinaryOperator::Sub)
412 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000414 const Expr *PExp = E->getLHS();
415 const Expr *IExp = E->getRHS();
416 if (IExp->getType()->isPointerType())
417 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000419 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000420 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000421 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000423 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000424 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000425 return APValue();
426
Ted Kremenek6217b802009-07-29 21:53:49 +0000427 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000428 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000430 // Explicitly handle GNU void* and function pointer arithmetic extensions.
431 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
432 SizeOfPointee = 1;
433 else
434 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000435
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000436 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000437
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000438 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000439 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000440 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000441 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
442
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000443 return APValue(ResultLValue.getLValueBase(), Offset);
444}
Eli Friedman4efaa272008-11-12 09:44:48 +0000445
Eli Friedman2217c872009-02-22 11:46:18 +0000446APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
447 APValue result;
448 if (EvaluateLValue(E->getSubExpr(), result, Info))
449 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000450 return APValue();
451}
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000453
Chris Lattnerb542afe2008-07-11 19:10:17 +0000454APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000455 const Expr* SubExpr = E->getSubExpr();
456
457 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000458 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000459 SubExpr->getType()->isObjCObjectPointerType() ||
460 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000461 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000462 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000463 return Result;
464 return APValue();
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000467 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000468 APValue Result;
469 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
470 return APValue();
471
472 if (Result.isInt()) {
473 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
474 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000477 // Cast is of an lvalue, no need to change value.
478 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000479 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000480
481 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000482 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000483 SubExpr->getType()->isArrayType()) {
484 APValue Result;
485 if (EvaluateLValue(SubExpr, Result, Info))
486 return Result;
487 return APValue();
488 }
489
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000490 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000491}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000492
Eli Friedman3941b182009-01-25 01:54:01 +0000493APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000494 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000495 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000496 return APValue(E, 0);
497 return APValue();
498}
499
Eli Friedman4efaa272008-11-12 09:44:48 +0000500APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
501 bool BoolResult;
502 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
503 return APValue();
504
505 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
506
507 APValue Result;
508 if (EvaluatePointer(EvalExpr, Result, Info))
509 return Result;
510 return APValue();
511}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000512
513//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000514// Vector Evaluation
515//===----------------------------------------------------------------------===//
516
517namespace {
518 class VISIBILITY_HIDDEN VectorExprEvaluator
519 : public StmtVisitor<VectorExprEvaluator, APValue> {
520 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000521 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000522 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Nate Begeman59b5da62009-01-18 03:20:47 +0000524 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Nate Begeman59b5da62009-01-18 03:20:47 +0000526 APValue VisitStmt(Stmt *S) {
527 return APValue();
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Eli Friedman91110ee2009-02-23 04:23:56 +0000530 APValue VisitParenExpr(ParenExpr *E)
531 { return Visit(E->getSubExpr()); }
532 APValue VisitUnaryExtension(const UnaryOperator *E)
533 { return Visit(E->getSubExpr()); }
534 APValue VisitUnaryPlus(const UnaryOperator *E)
535 { return Visit(E->getSubExpr()); }
536 APValue VisitUnaryReal(const UnaryOperator *E)
537 { return Visit(E->getSubExpr()); }
538 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
539 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000540 APValue VisitCastExpr(const CastExpr* E);
541 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
542 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000543 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000544 APValue VisitChooseExpr(const ChooseExpr *E)
545 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000546 APValue VisitUnaryImag(const UnaryOperator *E);
547 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000548 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000549 // shufflevector, ExtVectorElementExpr
550 // (Note that these require implementing conversions
551 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000552 };
553} // end anonymous namespace
554
555static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
556 if (!E->getType()->isVectorType())
557 return false;
558 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
559 return !Result.isUninit();
560}
561
562APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000563 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000564 QualType EltTy = VTy->getElementType();
565 unsigned NElts = VTy->getNumElements();
566 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Nate Begeman59b5da62009-01-18 03:20:47 +0000568 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000569 QualType SETy = SE->getType();
570 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000571
Nate Begemane8c9e922009-06-26 18:22:18 +0000572 // Check for vector->vector bitcast and scalar->vector splat.
573 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000574 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000575 } else if (SETy->isIntegerType()) {
576 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000577 if (!EvaluateInteger(SE, IntResult, Info))
578 return APValue();
579 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000580 } else if (SETy->isRealFloatingType()) {
581 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000582 if (!EvaluateFloat(SE, F, Info))
583 return APValue();
584 Result = APValue(F);
585 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000586 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000587
Nate Begemanc0b8b192009-07-01 07:50:47 +0000588 // For casts of a scalar to ExtVector, convert the scalar to the element type
589 // and splat it to all elements.
590 if (E->getType()->isExtVectorType()) {
591 if (EltTy->isIntegerType() && Result.isInt())
592 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
593 Info.Ctx));
594 else if (EltTy->isIntegerType())
595 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
596 Info.Ctx));
597 else if (EltTy->isRealFloatingType() && Result.isInt())
598 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
599 Info.Ctx));
600 else if (EltTy->isRealFloatingType())
601 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
602 Info.Ctx));
603 else
604 return APValue();
605
606 // Splat and create vector APValue.
607 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
608 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000609 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000610
611 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
612 // to the vector. To construct the APValue vector initializer, bitcast the
613 // initializing value to an APInt, and shift out the bits pertaining to each
614 // element.
615 APSInt Init;
616 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Nate Begemanc0b8b192009-07-01 07:50:47 +0000618 llvm::SmallVector<APValue, 4> Elts;
619 for (unsigned i = 0; i != NElts; ++i) {
620 APSInt Tmp = Init;
621 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Nate Begemanc0b8b192009-07-01 07:50:47 +0000623 if (EltTy->isIntegerType())
624 Elts.push_back(APValue(Tmp));
625 else if (EltTy->isRealFloatingType())
626 Elts.push_back(APValue(APFloat(Tmp)));
627 else
628 return APValue();
629
630 Init >>= EltWidth;
631 }
632 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000633}
634
Mike Stump1eb44332009-09-09 15:08:12 +0000635APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000636VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
637 return this->Visit(const_cast<Expr*>(E->getInitializer()));
638}
639
Mike Stump1eb44332009-09-09 15:08:12 +0000640APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000641VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000642 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000643 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000644 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Nate Begeman59b5da62009-01-18 03:20:47 +0000646 QualType EltTy = VT->getElementType();
647 llvm::SmallVector<APValue, 4> Elements;
648
Eli Friedman91110ee2009-02-23 04:23:56 +0000649 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000650 if (EltTy->isIntegerType()) {
651 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000652 if (i < NumInits) {
653 if (!EvaluateInteger(E->getInit(i), sInt, Info))
654 return APValue();
655 } else {
656 sInt = Info.Ctx.MakeIntValue(0, EltTy);
657 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000658 Elements.push_back(APValue(sInt));
659 } else {
660 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000661 if (i < NumInits) {
662 if (!EvaluateFloat(E->getInit(i), f, Info))
663 return APValue();
664 } else {
665 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
666 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000667 Elements.push_back(APValue(f));
668 }
669 }
670 return APValue(&Elements[0], Elements.size());
671}
672
Mike Stump1eb44332009-09-09 15:08:12 +0000673APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000674VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000675 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000676 QualType EltTy = VT->getElementType();
677 APValue ZeroElement;
678 if (EltTy->isIntegerType())
679 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
680 else
681 ZeroElement =
682 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
683
684 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
685 return APValue(&Elements[0], Elements.size());
686}
687
688APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
689 bool BoolResult;
690 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
691 return APValue();
692
693 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
694
695 APValue Result;
696 if (EvaluateVector(EvalExpr, Result, Info))
697 return Result;
698 return APValue();
699}
700
Eli Friedman91110ee2009-02-23 04:23:56 +0000701APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
702 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
703 Info.EvalResult.HasSideEffects = true;
704 return GetZeroVector(E->getType());
705}
706
Nate Begeman59b5da62009-01-18 03:20:47 +0000707//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000708// Integer Evaluation
709//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000710
711namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000712class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000713 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000714 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000715 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000716public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000717 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000718 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000719
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000720 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000721 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000722 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000723 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000724 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000725 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000726 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000727 return true;
728 }
729
Daniel Dunbar131eb432009-02-19 09:06:44 +0000730 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000731 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000732 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000733 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000734 Result = APValue(APSInt(I));
735 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000736 return true;
737 }
738
739 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000740 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000741 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000742 return true;
743 }
744
Anders Carlsson82206e22008-11-30 18:14:57 +0000745 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000746 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000747 if (Info.EvalResult.Diag == 0) {
748 Info.EvalResult.DiagLoc = L;
749 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000750 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000751 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000752 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlssonc754aa62008-07-08 05:13:58 +0000755 //===--------------------------------------------------------------------===//
756 // Visitor Methods
757 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner32fea9d2008-11-12 07:43:42 +0000759 bool VisitStmt(Stmt *) {
760 assert(0 && "This should be called on integers, stmts are not integers");
761 return false;
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattner32fea9d2008-11-12 07:43:42 +0000764 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000765 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattnerb542afe2008-07-11 19:10:17 +0000768 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000769
Chris Lattner4c4867e2008-07-12 00:38:25 +0000770 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000771 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000772 }
773 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000774 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000775 }
776 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000777 // Per gcc docs "this built-in function ignores top level
778 // qualifiers". We need to use the canonical version to properly
779 // be able to strip CRV qualifiers from the type.
780 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
781 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000782 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000783 T1.getUnqualifiedType()),
784 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000785 }
786 bool VisitDeclRefExpr(const DeclRefExpr *E);
787 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000788 bool VisitBinaryOperator(const BinaryOperator *E);
789 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000790 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000791
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000792 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000793 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
794
Anders Carlsson3068d112008-11-16 19:01:22 +0000795 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000796 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Anders Carlsson3f704562008-12-21 22:39:40 +0000799 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000800 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Anders Carlsson3068d112008-11-16 19:01:22 +0000803 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000804 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000805 }
806
Eli Friedman664a1042009-02-27 04:45:43 +0000807 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
808 return Success(0, E);
809 }
810
Sebastian Redl64b45f72009-01-05 20:52:13 +0000811 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000812 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000813 }
814
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000815 bool VisitChooseExpr(const ChooseExpr *E) {
816 return Visit(E->getChosenSubExpr(Info.Ctx));
817 }
818
Eli Friedman722c7172009-02-28 03:59:05 +0000819 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000820 bool VisitUnaryImag(const UnaryOperator *E);
821
Chris Lattnerfcee0012008-07-11 21:24:13 +0000822private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000823 unsigned GetAlignOfExpr(const Expr *E);
824 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000825 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000826};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000827} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000828
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000829static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000830 if (!E->getType()->isIntegralType())
831 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000833 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
834}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000835
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000836static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
837 APValue Val;
838 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
839 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000840 Result = Val.getInt();
841 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000842}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000843
Chris Lattner4c4867e2008-07-12 00:38:25 +0000844bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
845 // Enums are integer constant exprs.
846 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000847 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000848 // signedness consistently; see PR3173.
849 APSInt SI = D->getInitVal();
850 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
851 // FIXME: This is an ugly hack around the fact that enums don't
852 // set their width (!?!) consistently; see PR3173.
853 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
854 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000855 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000856
857 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000858 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000859 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
860 == Qualifiers::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000861 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000862 const VarDecl *Def = 0;
863 if (const Expr *Init = D->getDefinition(Def)) {
864 if (APValue *V = D->getEvaluatedValue())
865 return Success(V->getInt(), E);
866
Douglas Gregor78d15832009-05-26 18:54:04 +0000867 if (Visit(const_cast<Expr*>(Init))) {
868 // Cache the evaluated value in the variable declaration.
869 D->setEvaluatedValue(Info.Ctx, Result);
870 return true;
871 }
872
873 return false;
874 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000875 }
876 }
877
Chris Lattner4c4867e2008-07-12 00:38:25 +0000878 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000879 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000880}
881
Chris Lattnera4d55d82008-10-06 06:40:35 +0000882/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
883/// as GCC.
884static int EvaluateBuiltinClassifyType(const CallExpr *E) {
885 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000886 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000887 enum gcc_type_class {
888 no_type_class = -1,
889 void_type_class, integer_type_class, char_type_class,
890 enumeral_type_class, boolean_type_class,
891 pointer_type_class, reference_type_class, offset_type_class,
892 real_type_class, complex_type_class,
893 function_type_class, method_type_class,
894 record_type_class, union_type_class,
895 array_type_class, string_type_class,
896 lang_type_class
897 };
Mike Stump1eb44332009-09-09 15:08:12 +0000898
899 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000900 // ideal, however it is what gcc does.
901 if (E->getNumArgs() == 0)
902 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattnera4d55d82008-10-06 06:40:35 +0000904 QualType ArgTy = E->getArg(0)->getType();
905 if (ArgTy->isVoidType())
906 return void_type_class;
907 else if (ArgTy->isEnumeralType())
908 return enumeral_type_class;
909 else if (ArgTy->isBooleanType())
910 return boolean_type_class;
911 else if (ArgTy->isCharType())
912 return string_type_class; // gcc doesn't appear to use char_type_class
913 else if (ArgTy->isIntegerType())
914 return integer_type_class;
915 else if (ArgTy->isPointerType())
916 return pointer_type_class;
917 else if (ArgTy->isReferenceType())
918 return reference_type_class;
919 else if (ArgTy->isRealType())
920 return real_type_class;
921 else if (ArgTy->isComplexType())
922 return complex_type_class;
923 else if (ArgTy->isFunctionType())
924 return function_type_class;
925 else if (ArgTy->isStructureType())
926 return record_type_class;
927 else if (ArgTy->isUnionType())
928 return union_type_class;
929 else if (ArgTy->isArrayType())
930 return array_type_class;
931 else if (ArgTy->isUnionType())
932 return union_type_class;
933 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
934 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
935 return -1;
936}
937
Chris Lattner4c4867e2008-07-12 00:38:25 +0000938bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000939 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000940 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000941 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000942
943 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000944 const Expr *Arg = E->getArg(0)->IgnoreParens();
945 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000946 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000947 && Base.Val.getKind() == APValue::LValue
948 && !Base.HasSideEffects)
949 if (const Expr *LVBase = Base.Val.getLValueBase())
950 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
951 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000952 if (!VD->getType()->isIncompleteType()
953 && VD->getType()->isObjectType()
954 && !VD->getType()->isVariablyModifiedType()
955 && !VD->getType()->isDependentType()) {
956 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
957 uint64_t Offset = Base.Val.getLValueOffset();
958 if (Offset <= Size)
959 Size -= Base.Val.getLValueOffset();
960 else
961 Size = 0;
962 return Success(Size, E);
963 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000964 }
965 }
966
Mike Stumpc4c90452009-10-27 22:09:17 +0000967 if (HasSideEffects(E->getArg(0), Info.Ctx)) {
Mike Stump4fc87582009-10-26 18:57:47 +0000968 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Mike Stump64eda9e2009-10-26 18:35:08 +0000969 return Success(-1, E);
970 return Success(0, E);
971 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000972
Mike Stump64eda9e2009-10-26 18:35:08 +0000973 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
974 }
975
Chris Lattner019f4e82008-10-06 05:28:25 +0000976 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000977 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000979 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000980 // __builtin_constant_p always has one operand: it returns true if that
981 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000982 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000983
984 case Builtin::BI__builtin_eh_return_data_regno: {
985 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
986 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
987 return Success(Operand, E);
988 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000989 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000990}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000991
Chris Lattnerb542afe2008-07-11 19:10:17 +0000992bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000993 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000994 if (!Visit(E->getRHS()))
995 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000996
Eli Friedman33ef1452009-02-26 10:19:36 +0000997 // If we can't evaluate the LHS, it might have side effects;
998 // conservatively mark it.
999 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1000 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001001
Anders Carlsson027f62e2008-12-01 02:07:06 +00001002 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001003 }
1004
1005 if (E->isLogicalOp()) {
1006 // These need to be handled specially because the operands aren't
1007 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001008 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001010 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001011 // We were able to evaluate the LHS, see if we can get away with not
1012 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001013 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001014 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001015
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001016 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001017 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001018 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001019 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001020 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001021 }
1022 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001023 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001024 // We can't evaluate the LHS; however, sometimes the result
1025 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001026 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001027 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001028 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001029 // must have had side effects.
1030 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001031
1032 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001033 }
1034 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001035 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001036
Eli Friedmana6afa762008-11-13 06:09:17 +00001037 return false;
1038 }
1039
Anders Carlsson286f85e2008-11-16 07:17:21 +00001040 QualType LHSTy = E->getLHS()->getType();
1041 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001042
1043 if (LHSTy->isAnyComplexType()) {
1044 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1045 APValue LHS, RHS;
1046
1047 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1048 return false;
1049
1050 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1051 return false;
1052
1053 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001054 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001055 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001056 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001057 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1058
Daniel Dunbar4087e242009-01-29 06:43:41 +00001059 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001060 return Success((CR_r == APFloat::cmpEqual &&
1061 CR_i == APFloat::cmpEqual), E);
1062 else {
1063 assert(E->getOpcode() == BinaryOperator::NE &&
1064 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001065 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001066 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001067 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001068 CR_i == APFloat::cmpLessThan)), E);
1069 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001070 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001071 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001072 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1073 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1074 else {
1075 assert(E->getOpcode() == BinaryOperator::NE &&
1076 "Invalid compex comparison.");
1077 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1078 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1079 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001080 }
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Anders Carlsson286f85e2008-11-16 07:17:21 +00001083 if (LHSTy->isRealFloatingType() &&
1084 RHSTy->isRealFloatingType()) {
1085 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Anders Carlsson286f85e2008-11-16 07:17:21 +00001087 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1088 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Anders Carlsson286f85e2008-11-16 07:17:21 +00001090 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1091 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Anders Carlsson286f85e2008-11-16 07:17:21 +00001093 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001094
Anders Carlsson286f85e2008-11-16 07:17:21 +00001095 switch (E->getOpcode()) {
1096 default:
1097 assert(0 && "Invalid binary operator!");
1098 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001099 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001100 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001101 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001102 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001103 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001104 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001105 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001106 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001107 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001108 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001109 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001110 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001111 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001112 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001115 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1116 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001117 APValue LHSValue;
1118 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1119 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001120
Anders Carlsson3068d112008-11-16 19:01:22 +00001121 APValue RHSValue;
1122 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1123 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001124
Eli Friedman5bc86102009-06-14 02:17:33 +00001125 // Reject any bases from the normal codepath; we special-case comparisons
1126 // to null.
1127 if (LHSValue.getLValueBase()) {
1128 if (!E->isEqualityOp())
1129 return false;
1130 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1131 return false;
1132 bool bres;
1133 if (!EvalPointerValueAsBool(LHSValue, bres))
1134 return false;
1135 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1136 } else if (RHSValue.getLValueBase()) {
1137 if (!E->isEqualityOp())
1138 return false;
1139 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1140 return false;
1141 bool bres;
1142 if (!EvalPointerValueAsBool(RHSValue, bres))
1143 return false;
1144 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1145 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001146
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001147 if (E->getOpcode() == BinaryOperator::Sub) {
1148 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001149 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001150
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001151 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001152 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1153 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001154
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001155 return Success(D, E);
1156 }
1157 bool Result;
1158 if (E->getOpcode() == BinaryOperator::EQ) {
1159 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001160 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001161 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1162 }
1163 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001164 }
1165 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001166 if (!LHSTy->isIntegralType() ||
1167 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001168 // We can't continue from here for non-integral types, and they
1169 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001170 return false;
1171 }
1172
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001173 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001174 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001175 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001176
Eli Friedman42edd0d2009-03-24 01:14:50 +00001177 APValue RHSVal;
1178 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001179 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001180
1181 // Handle cases like (unsigned long)&a + 4.
1182 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1183 uint64_t offset = Result.getLValueOffset();
1184 if (E->getOpcode() == BinaryOperator::Add)
1185 offset += RHSVal.getInt().getZExtValue();
1186 else
1187 offset -= RHSVal.getInt().getZExtValue();
1188 Result = APValue(Result.getLValueBase(), offset);
1189 return true;
1190 }
1191
1192 // Handle cases like 4 + (unsigned long)&a
1193 if (E->getOpcode() == BinaryOperator::Add &&
1194 RHSVal.isLValue() && Result.isInt()) {
1195 uint64_t offset = RHSVal.getLValueOffset();
1196 offset += Result.getInt().getZExtValue();
1197 Result = APValue(RHSVal.getLValueBase(), offset);
1198 return true;
1199 }
1200
1201 // All the following cases expect both operands to be an integer
1202 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001203 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001204
Eli Friedman42edd0d2009-03-24 01:14:50 +00001205 APSInt& RHS = RHSVal.getInt();
1206
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001207 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001208 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001209 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001210 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1211 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1212 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1213 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1214 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1215 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001216 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001217 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001218 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001219 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001220 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001221 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001222 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001223 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001224 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001225 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001226 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001227 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1228 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001229 }
1230 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001231 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001232 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1233 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001236 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1237 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1238 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1239 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1240 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1241 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001242 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001243}
1244
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001245bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001246 bool Cond;
1247 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001248 return false;
1249
Nuno Lopesa25bd552008-11-16 22:06:39 +00001250 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001251}
1252
Chris Lattneraf707ab2009-01-24 21:53:27 +00001253unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001254 // Get information about the alignment.
1255 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001256
Eli Friedman2be58612009-05-30 21:09:44 +00001257 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001258 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001259}
1260
Chris Lattneraf707ab2009-01-24 21:53:27 +00001261unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1262 E = E->IgnoreParens();
1263
1264 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001265 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001266 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001267 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001268
Chris Lattneraf707ab2009-01-24 21:53:27 +00001269 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001270 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001271
Chris Lattnere9feb472009-01-24 21:09:06 +00001272 return GetAlignOfType(E->getType());
1273}
1274
1275
Sebastian Redl05189992008-11-11 17:56:53 +00001276/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1277/// expression's type.
1278bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1279 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001280
Chris Lattnere9feb472009-01-24 21:09:06 +00001281 // Handle alignof separately.
1282 if (!E->isSizeOf()) {
1283 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001284 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001285 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001286 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001287 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001288
Sebastian Redl05189992008-11-11 17:56:53 +00001289 QualType SrcTy = E->getTypeOfArgument();
1290
Daniel Dunbar131eb432009-02-19 09:06:44 +00001291 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1292 // extension.
1293 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1294 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001295
Chris Lattnerfcee0012008-07-11 21:24:13 +00001296 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001297 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001298 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001299
Chris Lattnere9feb472009-01-24 21:09:06 +00001300 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001301 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001302 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001303}
1304
Chris Lattnerb542afe2008-07-11 19:10:17 +00001305bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001306 // Special case unary operators that do not need their subexpression
1307 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001308 if (E->isOffsetOfOp()) {
1309 // The AST for offsetof is defined in such a way that we can just
1310 // directly Evaluate it as an l-value.
1311 APValue LV;
1312 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1313 return false;
1314 if (LV.getLValueBase())
1315 return false;
1316 return Success(LV.getLValueOffset(), E);
1317 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001318
1319 if (E->getOpcode() == UnaryOperator::LNot) {
1320 // LNot's operand isn't necessarily an integer, so we handle it specially.
1321 bool bres;
1322 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1323 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001324 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001325 }
1326
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001327 // Only handle integral operations...
1328 if (!E->getSubExpr()->getType()->isIntegralType())
1329 return false;
1330
Chris Lattner87eae5e2008-07-11 22:52:41 +00001331 // Get the operand value into 'Result'.
1332 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001333 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001334
Chris Lattner75a48812008-07-11 22:15:16 +00001335 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001336 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001337 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1338 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001339 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001340 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001341 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1342 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001343 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001344 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001345 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001346 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001347 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001348 if (!Result.isInt()) return false;
1349 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001350 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001351 if (!Result.isInt()) return false;
1352 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001353 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001354}
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Chris Lattner732b2232008-07-12 01:15:53 +00001356/// HandleCast - This is used to evaluate implicit or explicit casts where the
1357/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001358bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001359 Expr *SubExpr = E->getSubExpr();
1360 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001361 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001362
Eli Friedman4efaa272008-11-12 09:44:48 +00001363 if (DestType->isBooleanType()) {
1364 bool BoolResult;
1365 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1366 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001367 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001368 }
1369
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001370 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001371 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001372 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001373 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001374
Eli Friedmanbe265702009-02-20 01:15:07 +00001375 if (!Result.isInt()) {
1376 // Only allow casts of lvalues if they are lossless.
1377 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1378 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001379
Daniel Dunbardd211642009-02-19 22:24:01 +00001380 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001381 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001382 }
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattner732b2232008-07-12 01:15:53 +00001384 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001385 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001386 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001387 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001388 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001389
Daniel Dunbardd211642009-02-19 22:24:01 +00001390 if (LV.getLValueBase()) {
1391 // Only allow based lvalue casts if they are lossless.
1392 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1393 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001394
Daniel Dunbardd211642009-02-19 22:24:01 +00001395 Result = LV;
1396 return true;
1397 }
1398
1399 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1400 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001401 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001402
Eli Friedmanbe265702009-02-20 01:15:07 +00001403 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1404 // This handles double-conversion cases, where there's both
1405 // an l-value promotion and an implicit conversion to int.
1406 APValue LV;
1407 if (!EvaluateLValue(SubExpr, LV, Info))
1408 return false;
1409
1410 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1411 return false;
1412
1413 Result = LV;
1414 return true;
1415 }
1416
Eli Friedman1725f682009-04-22 19:23:09 +00001417 if (SrcType->isAnyComplexType()) {
1418 APValue C;
1419 if (!EvaluateComplex(SubExpr, C, Info))
1420 return false;
1421 if (C.isComplexFloat())
1422 return Success(HandleFloatToIntCast(DestType, SrcType,
1423 C.getComplexFloatReal(), Info.Ctx),
1424 E);
1425 else
1426 return Success(HandleIntToIntCast(DestType, SrcType,
1427 C.getComplexIntReal(), Info.Ctx), E);
1428 }
Eli Friedman2217c872009-02-22 11:46:18 +00001429 // FIXME: Handle vectors
1430
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001431 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001432 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001433
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001434 APFloat F(0.0);
1435 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001436 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001438 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001439}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001440
Eli Friedman722c7172009-02-28 03:59:05 +00001441bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1442 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1443 APValue LV;
1444 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1445 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1446 return Success(LV.getComplexIntReal(), E);
1447 }
1448
1449 return Visit(E->getSubExpr());
1450}
1451
Eli Friedman664a1042009-02-27 04:45:43 +00001452bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001453 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1454 APValue LV;
1455 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1456 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1457 return Success(LV.getComplexIntImag(), E);
1458 }
1459
Eli Friedman664a1042009-02-27 04:45:43 +00001460 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1461 Info.EvalResult.HasSideEffects = true;
1462 return Success(0, E);
1463}
1464
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001465//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001466// Float Evaluation
1467//===----------------------------------------------------------------------===//
1468
1469namespace {
1470class VISIBILITY_HIDDEN FloatExprEvaluator
1471 : public StmtVisitor<FloatExprEvaluator, bool> {
1472 EvalInfo &Info;
1473 APFloat &Result;
1474public:
1475 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1476 : Info(info), Result(result) {}
1477
1478 bool VisitStmt(Stmt *S) {
1479 return false;
1480 }
1481
1482 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001483 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001484
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001485 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001486 bool VisitBinaryOperator(const BinaryOperator *E);
1487 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001488 bool VisitCastExpr(CastExpr *E);
1489 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001490
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001491 bool VisitChooseExpr(const ChooseExpr *E)
1492 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1493 bool VisitUnaryExtension(const UnaryOperator *E)
1494 { return Visit(E->getSubExpr()); }
1495
1496 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1497 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001498 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001499};
1500} // end anonymous namespace
1501
1502static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1503 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1504}
1505
Chris Lattner019f4e82008-10-06 05:28:25 +00001506bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001507 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001508 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001509 case Builtin::BI__builtin_huge_val:
1510 case Builtin::BI__builtin_huge_valf:
1511 case Builtin::BI__builtin_huge_vall:
1512 case Builtin::BI__builtin_inf:
1513 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001514 case Builtin::BI__builtin_infl: {
1515 const llvm::fltSemantics &Sem =
1516 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001517 Result = llvm::APFloat::getInf(Sem);
1518 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Chris Lattner9e621712008-10-06 06:31:58 +00001521 case Builtin::BI__builtin_nan:
1522 case Builtin::BI__builtin_nanf:
1523 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001524 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001525 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001526 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001527 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001528 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001529 const llvm::fltSemantics &Sem =
1530 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001531 llvm::SmallString<16> s;
1532 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1533 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001534 long l;
1535 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001536 l = strtol(&s[0], &endp, 0);
1537 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001538 return false;
1539 unsigned type = (unsigned int)l;;
1540 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001541 return true;
1542 }
1543 }
1544 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001545
1546 case Builtin::BI__builtin_fabs:
1547 case Builtin::BI__builtin_fabsf:
1548 case Builtin::BI__builtin_fabsl:
1549 if (!EvaluateFloat(E->getArg(0), Result, Info))
1550 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001552 if (Result.isNegative())
1553 Result.changeSign();
1554 return true;
1555
Mike Stump1eb44332009-09-09 15:08:12 +00001556 case Builtin::BI__builtin_copysign:
1557 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001558 case Builtin::BI__builtin_copysignl: {
1559 APFloat RHS(0.);
1560 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1561 !EvaluateFloat(E->getArg(1), RHS, Info))
1562 return false;
1563 Result.copySign(RHS);
1564 return true;
1565 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001566 }
1567}
1568
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001569bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001570 if (E->getOpcode() == UnaryOperator::Deref)
1571 return false;
1572
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001573 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1574 return false;
1575
1576 switch (E->getOpcode()) {
1577 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001578 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001579 return true;
1580 case UnaryOperator::Minus:
1581 Result.changeSign();
1582 return true;
1583 }
1584}
Chris Lattner019f4e82008-10-06 05:28:25 +00001585
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001586bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1587 // FIXME: Diagnostics? I really don't understand how the warnings
1588 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001589 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001590 if (!EvaluateFloat(E->getLHS(), Result, Info))
1591 return false;
1592 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1593 return false;
1594
1595 switch (E->getOpcode()) {
1596 default: return false;
1597 case BinaryOperator::Mul:
1598 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1599 return true;
1600 case BinaryOperator::Add:
1601 Result.add(RHS, APFloat::rmNearestTiesToEven);
1602 return true;
1603 case BinaryOperator::Sub:
1604 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1605 return true;
1606 case BinaryOperator::Div:
1607 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1608 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001609 }
1610}
1611
1612bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1613 Result = E->getValue();
1614 return true;
1615}
1616
Eli Friedman4efaa272008-11-12 09:44:48 +00001617bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1618 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Eli Friedman4efaa272008-11-12 09:44:48 +00001620 if (SubExpr->getType()->isIntegralType()) {
1621 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001622 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001623 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001624 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001625 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001626 return true;
1627 }
1628 if (SubExpr->getType()->isRealFloatingType()) {
1629 if (!Visit(SubExpr))
1630 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001631 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1632 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001633 return true;
1634 }
Eli Friedman2217c872009-02-22 11:46:18 +00001635 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001636
1637 return false;
1638}
1639
1640bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1641 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1642 return true;
1643}
1644
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001645//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001646// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001647//===----------------------------------------------------------------------===//
1648
1649namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001650class VISIBILITY_HIDDEN ComplexExprEvaluator
1651 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001652 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001654public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001655 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001657 //===--------------------------------------------------------------------===//
1658 // Visitor Methods
1659 //===--------------------------------------------------------------------===//
1660
1661 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001662 return APValue();
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001665 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1666
1667 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001668 Expr* SubExpr = E->getSubExpr();
1669
1670 if (SubExpr->getType()->isRealFloatingType()) {
1671 APFloat Result(0.0);
1672
1673 if (!EvaluateFloat(SubExpr, Result, Info))
1674 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001675
1676 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001677 Result);
1678 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001679 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001680 "Unexpected imaginary literal.");
1681
1682 llvm::APSInt Result;
1683 if (!EvaluateInteger(SubExpr, Result, Info))
1684 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001686 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1687 Zero = 0;
1688 return APValue(Zero, Result);
1689 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001690 }
1691
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001692 APValue VisitCastExpr(CastExpr *E) {
1693 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001694 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001695 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001696
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001697 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001698 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001699
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001700 if (!EvaluateFloat(SubExpr, Result, Info))
1701 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001702
1703 if (EltType->isRealFloatingType()) {
1704 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001705 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001706 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1707 } else {
1708 llvm::APSInt IResult;
1709 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1710 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1711 Zero = 0;
1712 return APValue(IResult, Zero);
1713 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001714 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001715 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001716
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001717 if (!EvaluateInteger(SubExpr, Result, Info))
1718 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001719
Eli Friedman1725f682009-04-22 19:23:09 +00001720 if (EltType->isRealFloatingType()) {
1721 APFloat FResult =
1722 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001723 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001724 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1725 } else {
1726 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1727 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1728 Zero = 0;
1729 return APValue(Result, Zero);
1730 }
John McCall183700f2009-09-21 23:43:11 +00001731 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001732 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001733
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001734 if (!EvaluateComplex(SubExpr, Src, Info))
1735 return APValue();
1736
1737 QualType SrcType = CT->getElementType();
1738
1739 if (Src.isComplexFloat()) {
1740 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001741 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001742 Src.getComplexFloatReal(),
1743 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001744 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001745 Src.getComplexFloatImag(),
1746 Info.Ctx));
1747 } else {
1748 return APValue(HandleFloatToIntCast(EltType, SrcType,
1749 Src.getComplexFloatReal(),
1750 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001751 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001752 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001753 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001754 }
1755 } else {
1756 assert(Src.isComplexInt() && "Invalid evaluate result.");
1757 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001758 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001759 Src.getComplexIntReal(),
1760 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001761 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001762 Src.getComplexIntImag(),
1763 Info.Ctx));
1764 } else {
1765 return APValue(HandleIntToIntCast(EltType, SrcType,
1766 Src.getComplexIntReal(),
1767 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001768 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001769 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001770 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001771 }
1772 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001773 }
1774
1775 // FIXME: Handle more casts.
1776 return APValue();
1777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001779 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001780 APValue VisitChooseExpr(const ChooseExpr *E)
1781 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1782 APValue VisitUnaryExtension(const UnaryOperator *E)
1783 { return Visit(E->getSubExpr()); }
1784 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001785 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001786};
1787} // end anonymous namespace
1788
Mike Stump1eb44332009-09-09 15:08:12 +00001789static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001790 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1791 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001792 (&Result.getComplexFloatReal().getSemantics() ==
1793 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001794 "Invalid complex evaluation.");
1795 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001796}
1797
Mike Stump1eb44332009-09-09 15:08:12 +00001798APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001799 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001801 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001802 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001804 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001805 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001806
Daniel Dunbar3f279872009-01-29 01:32:56 +00001807 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1808 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001809 switch (E->getOpcode()) {
1810 default: return APValue();
1811 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001812 if (Result.isComplexFloat()) {
1813 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1814 APFloat::rmNearestTiesToEven);
1815 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1816 APFloat::rmNearestTiesToEven);
1817 } else {
1818 Result.getComplexIntReal() += RHS.getComplexIntReal();
1819 Result.getComplexIntImag() += RHS.getComplexIntImag();
1820 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001821 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001822 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001823 if (Result.isComplexFloat()) {
1824 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1825 APFloat::rmNearestTiesToEven);
1826 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1827 APFloat::rmNearestTiesToEven);
1828 } else {
1829 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1830 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1831 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001832 break;
1833 case BinaryOperator::Mul:
1834 if (Result.isComplexFloat()) {
1835 APValue LHS = Result;
1836 APFloat &LHS_r = LHS.getComplexFloatReal();
1837 APFloat &LHS_i = LHS.getComplexFloatImag();
1838 APFloat &RHS_r = RHS.getComplexFloatReal();
1839 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Daniel Dunbar3f279872009-01-29 01:32:56 +00001841 APFloat Tmp = LHS_r;
1842 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1843 Result.getComplexFloatReal() = Tmp;
1844 Tmp = LHS_i;
1845 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1846 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1847
1848 Tmp = LHS_r;
1849 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1850 Result.getComplexFloatImag() = Tmp;
1851 Tmp = LHS_i;
1852 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1853 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1854 } else {
1855 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001856 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001857 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1858 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001859 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001860 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1861 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1862 }
1863 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001864 }
1865
1866 return Result;
1867}
1868
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001869//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001870// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001871//===----------------------------------------------------------------------===//
1872
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001873/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001874/// any crazy technique (that has nothing to do with language standards) that
1875/// we want to. If this function returns true, it returns the folded constant
1876/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001877bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1878 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001879
Nate Begeman59b5da62009-01-18 03:20:47 +00001880 if (getType()->isVectorType()) {
1881 if (!EvaluateVector(this, Result.Val, Info))
1882 return false;
1883 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001884 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001885 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001886 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001887 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001888 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001889 } else if (getType()->isRealFloatingType()) {
1890 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001891 if (!EvaluateFloat(this, f, Info))
1892 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001894 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001895 } else if (getType()->isAnyComplexType()) {
1896 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001897 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001898 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001899 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001900
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001901 return true;
1902}
1903
Mike Stump660e6f72009-10-26 23:05:19 +00001904bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1905 EvalInfo Info(Ctx, Result, true);
1906
1907 if (getType()->isVectorType()) {
1908 if (!EvaluateVector(this, Result.Val, Info))
1909 return false;
1910 } else if (getType()->isIntegerType()) {
1911 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1912 return false;
1913 } else if (getType()->hasPointerRepresentation()) {
1914 if (!EvaluatePointer(this, Result.Val, Info))
1915 return false;
1916 } else if (getType()->isRealFloatingType()) {
1917 llvm::APFloat f(0.0);
1918 if (!EvaluateFloat(this, f, Info))
1919 return false;
1920
1921 Result.Val = APValue(f);
1922 } else if (getType()->isAnyComplexType()) {
1923 if (!EvaluateComplex(this, Result.Val, Info))
1924 return false;
1925 } else
1926 return false;
1927
1928 return true;
1929}
1930
Anders Carlsson1b782762009-04-10 04:54:13 +00001931bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1932 EvalInfo Info(Ctx, Result);
1933
1934 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1935}
1936
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001937bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1938 EvalInfo Info(Ctx, Result, true);
1939
1940 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1941}
1942
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001943/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001944/// folded, but discard the result.
1945bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001946 EvalResult Result;
1947 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001948}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001949
1950APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001951 EvalResult EvalResult;
1952 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001953 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001954 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001955 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001956
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001957 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001958}