blob: c036640b71121a2852beb03275ebdfbcd7243a3b [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) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000170 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000171 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) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000200 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000201 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
Mike Stumpc4c90452009-10-27 22:09:17 +0000207} // end anonymous namespace
208
Eli Friedman4efaa272008-11-12 09:44:48 +0000209//===----------------------------------------------------------------------===//
210// LValue Evaluation
211//===----------------------------------------------------------------------===//
212namespace {
213class VISIBILITY_HIDDEN LValueExprEvaluator
214 : public StmtVisitor<LValueExprEvaluator, APValue> {
215 EvalInfo &Info;
216public:
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Eli Friedman4efaa272008-11-12 09:44:48 +0000218 LValueExprEvaluator(EvalInfo &info) : Info(info) {}
219
220 APValue VisitStmt(Stmt *S) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000221 return APValue();
222 }
223
224 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlsson35873c42008-11-24 04:41:22 +0000225 APValue VisitDeclRefExpr(DeclRefExpr *E);
Steve Naroff3aaa4822009-04-16 19:02:57 +0000226 APValue VisitBlockExpr(BlockExpr *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000227 APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
228 APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
229 APValue VisitMemberExpr(MemberExpr *E);
230 APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000231 APValue VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return APValue(E, 0); }
Anders Carlsson3068d112008-11-16 19:01:22 +0000232 APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Eli Friedmane8761c82009-02-20 01:57:15 +0000233 APValue VisitUnaryDeref(UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000234 APValue VisitUnaryExtension(const UnaryOperator *E)
235 { return Visit(E->getSubExpr()); }
236 APValue VisitChooseExpr(const ChooseExpr *E)
237 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Anders Carlsson26bc2202009-10-03 16:30:22 +0000238
239 APValue VisitCastExpr(CastExpr *E) {
240 switch (E->getCastKind()) {
241 default:
242 return APValue();
243
244 case CastExpr::CK_NoOp:
245 return Visit(E->getSubExpr());
246 }
247 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000248 // FIXME: Missing: __real__, __imag__
Eli Friedman4efaa272008-11-12 09:44:48 +0000249};
250} // end anonymous namespace
251
252static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
253 Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
254 return Result.isLValue();
255}
256
Mike Stump1eb44332009-09-09 15:08:12 +0000257APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000258 if (isa<FunctionDecl>(E->getDecl())) {
259 return APValue(E, 0);
260 } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000261 if (!Info.AnyLValue && !VD->hasGlobalStorage())
Eli Friedmand933a012009-08-29 19:09:59 +0000262 return APValue();
Eli Friedman50c39ea2009-05-27 06:04:58 +0000263 if (!VD->getType()->isReferenceType())
264 return APValue(E, 0);
Eli Friedmand933a012009-08-29 19:09:59 +0000265 // FIXME: Check whether VD might be overridden!
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000266 const VarDecl *Def = 0;
267 if (const Expr *Init = VD->getDefinition(Def))
268 return Visit(const_cast<Expr *>(Init));
Eli Friedman50c39ea2009-05-27 06:04:58 +0000269 }
270
271 return APValue();
Anders Carlsson35873c42008-11-24 04:41:22 +0000272}
273
Mike Stump1eb44332009-09-09 15:08:12 +0000274APValue LValueExprEvaluator::VisitBlockExpr(BlockExpr *E) {
Steve Naroff3aaa4822009-04-16 19:02:57 +0000275 if (E->hasBlockDeclRefExprs())
276 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Steve Naroff3aaa4822009-04-16 19:02:57 +0000278 return APValue(E, 0);
279}
280
Eli Friedman4efaa272008-11-12 09:44:48 +0000281APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Eli Friedmanb2f295c2009-09-13 10:17:44 +0000282 if (!Info.AnyLValue && !E->isFileScope())
283 return APValue();
284 return APValue(E, 0);
Eli Friedman4efaa272008-11-12 09:44:48 +0000285}
286
287APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
288 APValue result;
289 QualType Ty;
290 if (E->isArrow()) {
291 if (!EvaluatePointer(E->getBase(), result, Info))
292 return APValue();
Ted Kremenek6217b802009-07-29 21:53:49 +0000293 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000294 } else {
295 result = Visit(E->getBase());
296 if (result.isUninit())
297 return APValue();
298 Ty = E->getBase()->getType();
299 }
300
Ted Kremenek6217b802009-07-29 21:53:49 +0000301 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000302 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000303
304 FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
305 if (!FD) // FIXME: deal with other kinds of member expressions
306 return APValue();
Eli Friedman2be58612009-05-30 21:09:44 +0000307
308 if (FD->getType()->isReferenceType())
309 return APValue();
310
Eli Friedman4efaa272008-11-12 09:44:48 +0000311 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000312 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000313 for (RecordDecl::field_iterator Field = RD->field_begin(),
314 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 Field != FieldEnd; (void)++Field, ++i) {
316 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000317 break;
318 }
319
320 result.setLValue(result.getLValueBase(),
321 result.getLValueOffset() + RL.getFieldOffset(i) / 8);
322
323 return result;
324}
325
Mike Stump1eb44332009-09-09 15:08:12 +0000326APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000327 APValue Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlsson3068d112008-11-16 19:01:22 +0000329 if (!EvaluatePointer(E->getBase(), Result, Info))
330 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlsson3068d112008-11-16 19:01:22 +0000332 APSInt Index;
333 if (!EvaluateInteger(E->getIdx(), Index, Info))
334 return APValue();
335
336 uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
337
338 uint64_t Offset = Index.getSExtValue() * ElementSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000339 Result.setLValue(Result.getLValueBase(),
Anders Carlsson3068d112008-11-16 19:01:22 +0000340 Result.getLValueOffset() + Offset);
341 return Result;
342}
Eli Friedman4efaa272008-11-12 09:44:48 +0000343
Mike Stump1eb44332009-09-09 15:08:12 +0000344APValue LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
Eli Friedmane8761c82009-02-20 01:57:15 +0000345 APValue Result;
346 if (!EvaluatePointer(E->getSubExpr(), Result, Info))
347 return APValue();
348 return Result;
349}
350
Eli Friedman4efaa272008-11-12 09:44:48 +0000351//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000352// Pointer Evaluation
353//===----------------------------------------------------------------------===//
354
Anders Carlssonc754aa62008-07-08 05:13:58 +0000355namespace {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000356class VISIBILITY_HIDDEN PointerExprEvaluator
357 : public StmtVisitor<PointerExprEvaluator, APValue> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000358 EvalInfo &Info;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000359public:
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner87eae5e2008-07-11 22:52:41 +0000361 PointerExprEvaluator(EvalInfo &info) : Info(info) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000362
Anders Carlsson2bad1682008-07-08 14:30:00 +0000363 APValue VisitStmt(Stmt *S) {
Anders Carlsson2bad1682008-07-08 14:30:00 +0000364 return APValue();
365 }
366
367 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
368
Anders Carlsson650c92f2008-07-08 15:34:11 +0000369 APValue VisitBinaryOperator(const BinaryOperator *E);
370 APValue VisitCastExpr(const CastExpr* E);
Eli Friedman2217c872009-02-22 11:46:18 +0000371 APValue VisitUnaryExtension(const UnaryOperator *E)
372 { return Visit(E->getSubExpr()); }
373 APValue VisitUnaryAddrOf(const UnaryOperator *E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000374 APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
375 { return APValue(E, 0); }
Eli Friedmanf0115892009-01-25 01:21:06 +0000376 APValue VisitAddrLabelExpr(AddrLabelExpr *E)
377 { return APValue(E, 0); }
Eli Friedman3941b182009-01-25 01:54:01 +0000378 APValue VisitCallExpr(CallExpr *E);
Mike Stumpb83d2872009-02-19 22:01:56 +0000379 APValue VisitBlockExpr(BlockExpr *E) {
380 if (!E->hasBlockDeclRefExprs())
381 return APValue(E, 0);
382 return APValue();
383 }
Eli Friedman91110ee2009-02-23 04:23:56 +0000384 APValue VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
385 { return APValue((Expr*)0, 0); }
Eli Friedman4efaa272008-11-12 09:44:48 +0000386 APValue VisitConditionalOperator(ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000387 APValue VisitChooseExpr(ChooseExpr *E)
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000388 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
389 APValue VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
390 { return APValue((Expr*)0, 0); }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000391 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000392};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000393} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000394
Chris Lattner87eae5e2008-07-11 22:52:41 +0000395static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
Daniel Dunbar89588912009-02-26 20:52:22 +0000396 if (!E->getType()->hasPointerRepresentation())
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000397 return false;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000398 Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000399 return Result.isLValue();
400}
401
402APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
403 if (E->getOpcode() != BinaryOperator::Add &&
404 E->getOpcode() != BinaryOperator::Sub)
405 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000407 const Expr *PExp = E->getLHS();
408 const Expr *IExp = E->getRHS();
409 if (IExp->getType()->isPointerType())
410 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000412 APValue ResultLValue;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000413 if (!EvaluatePointer(PExp, ResultLValue, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000414 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000416 llvm::APSInt AdditionalOffset(32);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000417 if (!EvaluateInteger(IExp, AdditionalOffset, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000418 return APValue();
419
Ted Kremenek6217b802009-07-29 21:53:49 +0000420 QualType PointeeType = PExp->getType()->getAs<PointerType>()->getPointeeType();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000421 uint64_t SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000423 // Explicitly handle GNU void* and function pointer arithmetic extensions.
424 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
425 SizeOfPointee = 1;
426 else
427 SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
Eli Friedman4efaa272008-11-12 09:44:48 +0000428
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000429 uint64_t Offset = ResultLValue.getLValueOffset();
Eli Friedman4efaa272008-11-12 09:44:48 +0000430
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000431 if (E->getOpcode() == BinaryOperator::Add)
Eli Friedman4efaa272008-11-12 09:44:48 +0000432 Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000433 else
Eli Friedman4efaa272008-11-12 09:44:48 +0000434 Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
435
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000436 return APValue(ResultLValue.getLValueBase(), Offset);
437}
Eli Friedman4efaa272008-11-12 09:44:48 +0000438
Eli Friedman2217c872009-02-22 11:46:18 +0000439APValue PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
440 APValue result;
441 if (EvaluateLValue(E->getSubExpr(), result, Info))
442 return result;
Eli Friedman4efaa272008-11-12 09:44:48 +0000443 return APValue();
444}
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000446
Chris Lattnerb542afe2008-07-11 19:10:17 +0000447APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000448 const Expr* SubExpr = E->getSubExpr();
449
450 // Check for pointer->pointer cast
Steve Naroff14108da2009-07-10 23:34:53 +0000451 if (SubExpr->getType()->isPointerType() ||
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000452 SubExpr->getType()->isObjCObjectPointerType() ||
453 SubExpr->getType()->isNullPtrType()) {
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000454 APValue Result;
Chris Lattner87eae5e2008-07-11 22:52:41 +0000455 if (EvaluatePointer(SubExpr, Result, Info))
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000456 return Result;
457 return APValue();
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Eli Friedmand9f4bcd2008-07-27 05:46:18 +0000460 if (SubExpr->getType()->isIntegralType()) {
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000461 APValue Result;
462 if (!EvaluateIntegerOrLValue(SubExpr, Result, Info))
463 return APValue();
464
465 if (Result.isInt()) {
466 Result.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
467 return APValue(0, Result.getInt().getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000470 // Cast is of an lvalue, no need to change value.
471 return Result;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000472 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000473
474 if (SubExpr->getType()->isFunctionType() ||
Steve Naroff3aaa4822009-04-16 19:02:57 +0000475 SubExpr->getType()->isBlockPointerType() ||
Eli Friedman4efaa272008-11-12 09:44:48 +0000476 SubExpr->getType()->isArrayType()) {
477 APValue Result;
478 if (EvaluateLValue(SubExpr, Result, Info))
479 return Result;
480 return APValue();
481 }
482
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000483 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000484}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000485
Eli Friedman3941b182009-01-25 01:54:01 +0000486APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000487 if (E->isBuiltinCall(Info.Ctx) ==
Douglas Gregor3c385e52009-02-14 18:57:46 +0000488 Builtin::BI__builtin___CFStringMakeConstantString)
Eli Friedman3941b182009-01-25 01:54:01 +0000489 return APValue(E, 0);
490 return APValue();
491}
492
Eli Friedman4efaa272008-11-12 09:44:48 +0000493APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
494 bool BoolResult;
495 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
496 return APValue();
497
498 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
499
500 APValue Result;
501 if (EvaluatePointer(EvalExpr, Result, Info))
502 return Result;
503 return APValue();
504}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000505
506//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000507// Vector Evaluation
508//===----------------------------------------------------------------------===//
509
510namespace {
511 class VISIBILITY_HIDDEN VectorExprEvaluator
512 : public StmtVisitor<VectorExprEvaluator, APValue> {
513 EvalInfo &Info;
Eli Friedman91110ee2009-02-23 04:23:56 +0000514 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000515 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Nate Begeman59b5da62009-01-18 03:20:47 +0000517 VectorExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Nate Begeman59b5da62009-01-18 03:20:47 +0000519 APValue VisitStmt(Stmt *S) {
520 return APValue();
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Eli Friedman91110ee2009-02-23 04:23:56 +0000523 APValue VisitParenExpr(ParenExpr *E)
524 { return Visit(E->getSubExpr()); }
525 APValue VisitUnaryExtension(const UnaryOperator *E)
526 { return Visit(E->getSubExpr()); }
527 APValue VisitUnaryPlus(const UnaryOperator *E)
528 { return Visit(E->getSubExpr()); }
529 APValue VisitUnaryReal(const UnaryOperator *E)
530 { return Visit(E->getSubExpr()); }
531 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
532 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000533 APValue VisitCastExpr(const CastExpr* E);
534 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
535 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000536 APValue VisitConditionalOperator(const ConditionalOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000537 APValue VisitChooseExpr(const ChooseExpr *E)
538 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Eli Friedman91110ee2009-02-23 04:23:56 +0000539 APValue VisitUnaryImag(const UnaryOperator *E);
540 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000541 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000542 // shufflevector, ExtVectorElementExpr
543 // (Note that these require implementing conversions
544 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000545 };
546} // end anonymous namespace
547
548static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
549 if (!E->getType()->isVectorType())
550 return false;
551 Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
552 return !Result.isUninit();
553}
554
555APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000556 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000557 QualType EltTy = VTy->getElementType();
558 unsigned NElts = VTy->getNumElements();
559 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Nate Begeman59b5da62009-01-18 03:20:47 +0000561 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000562 QualType SETy = SE->getType();
563 APValue Result = APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000564
Nate Begemane8c9e922009-06-26 18:22:18 +0000565 // Check for vector->vector bitcast and scalar->vector splat.
566 if (SETy->isVectorType()) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000567 return this->Visit(const_cast<Expr*>(SE));
Nate Begemane8c9e922009-06-26 18:22:18 +0000568 } else if (SETy->isIntegerType()) {
569 APSInt IntResult;
Daniel Dunbard906dc72009-07-01 20:37:45 +0000570 if (!EvaluateInteger(SE, IntResult, Info))
571 return APValue();
572 Result = APValue(IntResult);
Nate Begemane8c9e922009-06-26 18:22:18 +0000573 } else if (SETy->isRealFloatingType()) {
574 APFloat F(0.0);
Daniel Dunbard906dc72009-07-01 20:37:45 +0000575 if (!EvaluateFloat(SE, F, Info))
576 return APValue();
577 Result = APValue(F);
578 } else
Nate Begemanc0b8b192009-07-01 07:50:47 +0000579 return APValue();
Nate Begeman59b5da62009-01-18 03:20:47 +0000580
Nate Begemanc0b8b192009-07-01 07:50:47 +0000581 // For casts of a scalar to ExtVector, convert the scalar to the element type
582 // and splat it to all elements.
583 if (E->getType()->isExtVectorType()) {
584 if (EltTy->isIntegerType() && Result.isInt())
585 Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
586 Info.Ctx));
587 else if (EltTy->isIntegerType())
588 Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
589 Info.Ctx));
590 else if (EltTy->isRealFloatingType() && Result.isInt())
591 Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
592 Info.Ctx));
593 else if (EltTy->isRealFloatingType())
594 Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
595 Info.Ctx));
596 else
597 return APValue();
598
599 // Splat and create vector APValue.
600 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
601 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000602 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000603
604 // For casts of a scalar to regular gcc-style vector type, bitcast the scalar
605 // to the vector. To construct the APValue vector initializer, bitcast the
606 // initializing value to an APInt, and shift out the bits pertaining to each
607 // element.
608 APSInt Init;
609 Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Nate Begemanc0b8b192009-07-01 07:50:47 +0000611 llvm::SmallVector<APValue, 4> Elts;
612 for (unsigned i = 0; i != NElts; ++i) {
613 APSInt Tmp = Init;
614 Tmp.extOrTrunc(EltWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Nate Begemanc0b8b192009-07-01 07:50:47 +0000616 if (EltTy->isIntegerType())
617 Elts.push_back(APValue(Tmp));
618 else if (EltTy->isRealFloatingType())
619 Elts.push_back(APValue(APFloat(Tmp)));
620 else
621 return APValue();
622
623 Init >>= EltWidth;
624 }
625 return APValue(&Elts[0], Elts.size());
Nate Begeman59b5da62009-01-18 03:20:47 +0000626}
627
Mike Stump1eb44332009-09-09 15:08:12 +0000628APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000629VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
630 return this->Visit(const_cast<Expr*>(E->getInitializer()));
631}
632
Mike Stump1eb44332009-09-09 15:08:12 +0000633APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000634VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000635 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000636 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000637 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Nate Begeman59b5da62009-01-18 03:20:47 +0000639 QualType EltTy = VT->getElementType();
640 llvm::SmallVector<APValue, 4> Elements;
641
Eli Friedman91110ee2009-02-23 04:23:56 +0000642 for (unsigned i = 0; i < NumElements; i++) {
Nate Begeman59b5da62009-01-18 03:20:47 +0000643 if (EltTy->isIntegerType()) {
644 llvm::APSInt sInt(32);
Eli Friedman91110ee2009-02-23 04:23:56 +0000645 if (i < NumInits) {
646 if (!EvaluateInteger(E->getInit(i), sInt, Info))
647 return APValue();
648 } else {
649 sInt = Info.Ctx.MakeIntValue(0, EltTy);
650 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000651 Elements.push_back(APValue(sInt));
652 } else {
653 llvm::APFloat f(0.0);
Eli Friedman91110ee2009-02-23 04:23:56 +0000654 if (i < NumInits) {
655 if (!EvaluateFloat(E->getInit(i), f, Info))
656 return APValue();
657 } else {
658 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
659 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000660 Elements.push_back(APValue(f));
661 }
662 }
663 return APValue(&Elements[0], Elements.size());
664}
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000667VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000668 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000669 QualType EltTy = VT->getElementType();
670 APValue ZeroElement;
671 if (EltTy->isIntegerType())
672 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
673 else
674 ZeroElement =
675 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
676
677 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
678 return APValue(&Elements[0], Elements.size());
679}
680
681APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
682 bool BoolResult;
683 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
684 return APValue();
685
686 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
687
688 APValue Result;
689 if (EvaluateVector(EvalExpr, Result, Info))
690 return Result;
691 return APValue();
692}
693
Eli Friedman91110ee2009-02-23 04:23:56 +0000694APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
695 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
696 Info.EvalResult.HasSideEffects = true;
697 return GetZeroVector(E->getType());
698}
699
Nate Begeman59b5da62009-01-18 03:20:47 +0000700//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000701// Integer Evaluation
702//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000703
704namespace {
Anders Carlssonc754aa62008-07-08 05:13:58 +0000705class VISIBILITY_HIDDEN IntExprEvaluator
Chris Lattnerb542afe2008-07-11 19:10:17 +0000706 : public StmtVisitor<IntExprEvaluator, bool> {
Chris Lattner87eae5e2008-07-11 22:52:41 +0000707 EvalInfo &Info;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000708 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000709public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000710 IntExprEvaluator(EvalInfo &info, APValue &result)
Chris Lattner87eae5e2008-07-11 22:52:41 +0000711 : Info(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000712
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000713 bool Success(const llvm::APSInt &SI, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000714 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000715 assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000716 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000717 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000718 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000719 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000720 return true;
721 }
722
Daniel Dunbar131eb432009-02-19 09:06:44 +0000723 bool Success(const llvm::APInt &I, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000724 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000725 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000726 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000727 Result = APValue(APSInt(I));
728 Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000729 return true;
730 }
731
732 bool Success(uint64_t Value, const Expr *E) {
Daniel Dunbar4fff4812009-02-21 18:14:20 +0000733 assert(E->getType()->isIntegralType() && "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000734 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000735 return true;
736 }
737
Anders Carlsson82206e22008-11-30 18:14:57 +0000738 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000739 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000740 if (Info.EvalResult.Diag == 0) {
741 Info.EvalResult.DiagLoc = L;
742 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000743 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000744 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000745 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Anders Carlssonc754aa62008-07-08 05:13:58 +0000748 //===--------------------------------------------------------------------===//
749 // Visitor Methods
750 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattner32fea9d2008-11-12 07:43:42 +0000752 bool VisitStmt(Stmt *) {
753 assert(0 && "This should be called on integers, stmts are not integers");
754 return false;
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattner32fea9d2008-11-12 07:43:42 +0000757 bool VisitExpr(Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000758 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattnerb542afe2008-07-11 19:10:17 +0000761 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Anders Carlssonc754aa62008-07-08 05:13:58 +0000762
Chris Lattner4c4867e2008-07-12 00:38:25 +0000763 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000764 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000765 }
766 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000767 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000768 }
769 bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
Daniel Dunbarac620de2008-10-24 08:07:57 +0000770 // Per gcc docs "this built-in function ignores top level
771 // qualifiers". We need to use the canonical version to properly
772 // be able to strip CRV qualifiers from the type.
773 QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
774 QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
Mike Stump1eb44332009-09-09 15:08:12 +0000775 return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
Daniel Dunbar131eb432009-02-19 09:06:44 +0000776 T1.getUnqualifiedType()),
777 E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000778 }
779 bool VisitDeclRefExpr(const DeclRefExpr *E);
780 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +0000781 bool VisitBinaryOperator(const BinaryOperator *E);
782 bool VisitUnaryOperator(const UnaryOperator *E);
Nuno Lopesca7c2ea2008-11-16 19:28:31 +0000783 bool VisitConditionalOperator(const ConditionalOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +0000784
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000785 bool VisitCastExpr(CastExpr* E);
Sebastian Redl05189992008-11-11 17:56:53 +0000786 bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
787
Anders Carlsson3068d112008-11-16 19:01:22 +0000788 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000789 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Anders Carlsson3f704562008-12-21 22:39:40 +0000792 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000793 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +0000794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Anders Carlsson3068d112008-11-16 19:01:22 +0000796 bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +0000797 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +0000798 }
799
Eli Friedman664a1042009-02-27 04:45:43 +0000800 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
801 return Success(0, E);
802 }
803
Sebastian Redl64b45f72009-01-05 20:52:13 +0000804 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000805 return Success(E->EvaluateTrait(Info.Ctx), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +0000806 }
807
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000808 bool VisitChooseExpr(const ChooseExpr *E) {
809 return Visit(E->getChosenSubExpr(Info.Ctx));
810 }
811
Eli Friedman722c7172009-02-28 03:59:05 +0000812 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +0000813 bool VisitUnaryImag(const UnaryOperator *E);
814
Chris Lattnerfcee0012008-07-11 21:24:13 +0000815private:
Chris Lattneraf707ab2009-01-24 21:53:27 +0000816 unsigned GetAlignOfExpr(const Expr *E);
817 unsigned GetAlignOfType(QualType T);
Eli Friedman664a1042009-02-27 04:45:43 +0000818 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +0000819};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000820} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000821
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000822static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000823 if (!E->getType()->isIntegralType())
824 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000826 return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
827}
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000828
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000829static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
830 APValue Val;
831 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
832 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000833 Result = Val.getInt();
834 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +0000835}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000836
Chris Lattner4c4867e2008-07-12 00:38:25 +0000837bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
838 // Enums are integer constant exprs.
839 if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
Eli Friedmane9a0f432008-12-08 02:21:03 +0000840 // FIXME: This is an ugly hack around the fact that enums don't set their
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000841 // signedness consistently; see PR3173.
842 APSInt SI = D->getInitVal();
843 SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
844 // FIXME: This is an ugly hack around the fact that enums don't
845 // set their width (!?!) consistently; see PR3173.
846 SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
847 return Success(SI, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000848 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000849
850 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +0000851 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000852 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
853 == Qualifiers::Const) {
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000854 if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000855 const VarDecl *Def = 0;
856 if (const Expr *Init = D->getDefinition(Def)) {
857 if (APValue *V = D->getEvaluatedValue())
858 return Success(V->getInt(), E);
859
Douglas Gregor78d15832009-05-26 18:54:04 +0000860 if (Visit(const_cast<Expr*>(Init))) {
861 // Cache the evaluated value in the variable declaration.
862 D->setEvaluatedValue(Info.Ctx, Result);
863 return true;
864 }
865
866 return false;
867 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +0000868 }
869 }
870
Chris Lattner4c4867e2008-07-12 00:38:25 +0000871 // Otherwise, random variable references are not constants.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000872 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner4c4867e2008-07-12 00:38:25 +0000873}
874
Chris Lattnera4d55d82008-10-06 06:40:35 +0000875/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
876/// as GCC.
877static int EvaluateBuiltinClassifyType(const CallExpr *E) {
878 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000879 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +0000880 enum gcc_type_class {
881 no_type_class = -1,
882 void_type_class, integer_type_class, char_type_class,
883 enumeral_type_class, boolean_type_class,
884 pointer_type_class, reference_type_class, offset_type_class,
885 real_type_class, complex_type_class,
886 function_type_class, method_type_class,
887 record_type_class, union_type_class,
888 array_type_class, string_type_class,
889 lang_type_class
890 };
Mike Stump1eb44332009-09-09 15:08:12 +0000891
892 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +0000893 // ideal, however it is what gcc does.
894 if (E->getNumArgs() == 0)
895 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattnera4d55d82008-10-06 06:40:35 +0000897 QualType ArgTy = E->getArg(0)->getType();
898 if (ArgTy->isVoidType())
899 return void_type_class;
900 else if (ArgTy->isEnumeralType())
901 return enumeral_type_class;
902 else if (ArgTy->isBooleanType())
903 return boolean_type_class;
904 else if (ArgTy->isCharType())
905 return string_type_class; // gcc doesn't appear to use char_type_class
906 else if (ArgTy->isIntegerType())
907 return integer_type_class;
908 else if (ArgTy->isPointerType())
909 return pointer_type_class;
910 else if (ArgTy->isReferenceType())
911 return reference_type_class;
912 else if (ArgTy->isRealType())
913 return real_type_class;
914 else if (ArgTy->isComplexType())
915 return complex_type_class;
916 else if (ArgTy->isFunctionType())
917 return function_type_class;
918 else if (ArgTy->isStructureType())
919 return record_type_class;
920 else if (ArgTy->isUnionType())
921 return union_type_class;
922 else if (ArgTy->isArrayType())
923 return array_type_class;
924 else if (ArgTy->isUnionType())
925 return union_type_class;
926 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
927 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
928 return -1;
929}
930
Chris Lattner4c4867e2008-07-12 00:38:25 +0000931bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +0000932 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +0000933 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000934 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000935
936 case Builtin::BI__builtin_object_size: {
Mike Stump64eda9e2009-10-26 18:35:08 +0000937 const Expr *Arg = E->getArg(0)->IgnoreParens();
938 Expr::EvalResult Base;
Mike Stump660e6f72009-10-26 23:05:19 +0000939 if (Arg->EvaluateAsAny(Base, Info.Ctx)
Mike Stump64eda9e2009-10-26 18:35:08 +0000940 && Base.Val.getKind() == APValue::LValue
941 && !Base.HasSideEffects)
942 if (const Expr *LVBase = Base.Val.getLValueBase())
943 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
944 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
Mike Stump460d1382009-10-28 21:22:24 +0000945 if (!VD->getType()->isIncompleteType()
946 && VD->getType()->isObjectType()
947 && !VD->getType()->isVariablyModifiedType()
948 && !VD->getType()->isDependentType()) {
949 uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
950 uint64_t Offset = Base.Val.getLValueOffset();
951 if (Offset <= Size)
952 Size -= Base.Val.getLValueOffset();
953 else
954 Size = 0;
955 return Success(Size, E);
956 }
Mike Stump64eda9e2009-10-26 18:35:08 +0000957 }
958 }
959
Fariborz Jahanian393c2472009-11-05 18:03:03 +0000960 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Mike Stump4fc87582009-10-26 18:57:47 +0000961 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
Chris Lattnercf184652009-11-03 19:48:51 +0000962 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +0000963 return Success(0, E);
964 }
Mike Stumpc4c90452009-10-27 22:09:17 +0000965
Mike Stump64eda9e2009-10-26 18:35:08 +0000966 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
967 }
968
Chris Lattner019f4e82008-10-06 05:28:25 +0000969 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +0000970 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Anders Carlsson4bbc0e02008-11-24 04:21:33 +0000972 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +0000973 // __builtin_constant_p always has one operand: it returns true if that
974 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +0000975 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +0000976
977 case Builtin::BI__builtin_eh_return_data_regno: {
978 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
979 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
980 return Success(Operand, E);
981 }
Chris Lattner019f4e82008-10-06 05:28:25 +0000982 }
Chris Lattner4c4867e2008-07-12 00:38:25 +0000983}
Anders Carlsson650c92f2008-07-08 15:34:11 +0000984
Chris Lattnerb542afe2008-07-11 19:10:17 +0000985bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedmana6afa762008-11-13 06:09:17 +0000986 if (E->getOpcode() == BinaryOperator::Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +0000987 if (!Visit(E->getRHS()))
988 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +0000989
Eli Friedman33ef1452009-02-26 10:19:36 +0000990 // If we can't evaluate the LHS, it might have side effects;
991 // conservatively mark it.
992 if (!E->getLHS()->isEvaluatable(Info.Ctx))
993 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000994
Anders Carlsson027f62e2008-12-01 02:07:06 +0000995 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +0000996 }
997
998 if (E->isLogicalOp()) {
999 // These need to be handled specially because the operands aren't
1000 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001001 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001003 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001004 // We were able to evaluate the LHS, see if we can get away with not
1005 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
Eli Friedman33ef1452009-02-26 10:19:36 +00001006 if (lhsResult == (E->getOpcode() == BinaryOperator::LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001007 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001008
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001009 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001010 if (E->getOpcode() == BinaryOperator::LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001011 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001012 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001013 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001014 }
1015 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001016 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001017 // We can't evaluate the LHS; however, sometimes the result
1018 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
Mike Stump1eb44332009-09-09 15:08:12 +00001019 if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001020 !rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001021 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001022 // must have had side effects.
1023 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001024
1025 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001026 }
1027 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001028 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001029
Eli Friedmana6afa762008-11-13 06:09:17 +00001030 return false;
1031 }
1032
Anders Carlsson286f85e2008-11-16 07:17:21 +00001033 QualType LHSTy = E->getLHS()->getType();
1034 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001035
1036 if (LHSTy->isAnyComplexType()) {
1037 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
1038 APValue LHS, RHS;
1039
1040 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1041 return false;
1042
1043 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1044 return false;
1045
1046 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001047 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001048 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001049 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001050 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1051
Daniel Dunbar4087e242009-01-29 06:43:41 +00001052 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001053 return Success((CR_r == APFloat::cmpEqual &&
1054 CR_i == APFloat::cmpEqual), E);
1055 else {
1056 assert(E->getOpcode() == BinaryOperator::NE &&
1057 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001058 return Success(((CR_r == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001059 CR_r == APFloat::cmpLessThan) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001060 (CR_i == APFloat::cmpGreaterThan ||
Daniel Dunbar131eb432009-02-19 09:06:44 +00001061 CR_i == APFloat::cmpLessThan)), E);
1062 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001063 } else {
Daniel Dunbar4087e242009-01-29 06:43:41 +00001064 if (E->getOpcode() == BinaryOperator::EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001065 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1066 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1067 else {
1068 assert(E->getOpcode() == BinaryOperator::NE &&
1069 "Invalid compex comparison.");
1070 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1071 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1072 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001073 }
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Anders Carlsson286f85e2008-11-16 07:17:21 +00001076 if (LHSTy->isRealFloatingType() &&
1077 RHSTy->isRealFloatingType()) {
1078 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Anders Carlsson286f85e2008-11-16 07:17:21 +00001080 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1081 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Anders Carlsson286f85e2008-11-16 07:17:21 +00001083 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1084 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Anders Carlsson286f85e2008-11-16 07:17:21 +00001086 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001087
Anders Carlsson286f85e2008-11-16 07:17:21 +00001088 switch (E->getOpcode()) {
1089 default:
1090 assert(0 && "Invalid binary operator!");
1091 case BinaryOperator::LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001092 return Success(CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001093 case BinaryOperator::GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001094 return Success(CR == APFloat::cmpGreaterThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001095 case BinaryOperator::LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001096 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001097 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001098 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001099 E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001100 case BinaryOperator::EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001101 return Success(CR == APFloat::cmpEqual, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001102 case BinaryOperator::NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001103 return Success(CR == APFloat::cmpGreaterThan
Daniel Dunbar131eb432009-02-19 09:06:44 +00001104 || CR == APFloat::cmpLessThan, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001105 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001108 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
1109 if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
Anders Carlsson3068d112008-11-16 19:01:22 +00001110 APValue LHSValue;
1111 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1112 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001113
Anders Carlsson3068d112008-11-16 19:01:22 +00001114 APValue RHSValue;
1115 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1116 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001117
Eli Friedman5bc86102009-06-14 02:17:33 +00001118 // Reject any bases from the normal codepath; we special-case comparisons
1119 // to null.
1120 if (LHSValue.getLValueBase()) {
1121 if (!E->isEqualityOp())
1122 return false;
1123 if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
1124 return false;
1125 bool bres;
1126 if (!EvalPointerValueAsBool(LHSValue, bres))
1127 return false;
1128 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1129 } else if (RHSValue.getLValueBase()) {
1130 if (!E->isEqualityOp())
1131 return false;
1132 if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
1133 return false;
1134 bool bres;
1135 if (!EvalPointerValueAsBool(RHSValue, bres))
1136 return false;
1137 return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
1138 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001139
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001140 if (E->getOpcode() == BinaryOperator::Sub) {
1141 const QualType Type = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001142 const QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001143
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001144 uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
Eli Friedmance1bca72009-06-04 20:23:20 +00001145 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
1146 D /= Info.Ctx.getTypeSize(ElementType) / 8;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001147
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001148 return Success(D, E);
1149 }
1150 bool Result;
1151 if (E->getOpcode() == BinaryOperator::EQ) {
1152 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001153 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001154 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1155 }
1156 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001157 }
1158 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001159 if (!LHSTy->isIntegralType() ||
1160 !RHSTy->isIntegralType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001161 // We can't continue from here for non-integral types, and they
1162 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001163 return false;
1164 }
1165
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001166 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001167 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001168 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001169
Eli Friedman42edd0d2009-03-24 01:14:50 +00001170 APValue RHSVal;
1171 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001172 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001173
1174 // Handle cases like (unsigned long)&a + 4.
1175 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
1176 uint64_t offset = Result.getLValueOffset();
1177 if (E->getOpcode() == BinaryOperator::Add)
1178 offset += RHSVal.getInt().getZExtValue();
1179 else
1180 offset -= RHSVal.getInt().getZExtValue();
1181 Result = APValue(Result.getLValueBase(), offset);
1182 return true;
1183 }
1184
1185 // Handle cases like 4 + (unsigned long)&a
1186 if (E->getOpcode() == BinaryOperator::Add &&
1187 RHSVal.isLValue() && Result.isInt()) {
1188 uint64_t offset = RHSVal.getLValueOffset();
1189 offset += Result.getInt().getZExtValue();
1190 Result = APValue(RHSVal.getLValueBase(), offset);
1191 return true;
1192 }
1193
1194 // All the following cases expect both operands to be an integer
1195 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001196 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001197
Eli Friedman42edd0d2009-03-24 01:14:50 +00001198 APSInt& RHS = RHSVal.getInt();
1199
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001200 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001201 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001202 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001203 case BinaryOperator::Mul: return Success(Result.getInt() * RHS, E);
1204 case BinaryOperator::Add: return Success(Result.getInt() + RHS, E);
1205 case BinaryOperator::Sub: return Success(Result.getInt() - RHS, E);
1206 case BinaryOperator::And: return Success(Result.getInt() & RHS, E);
1207 case BinaryOperator::Xor: return Success(Result.getInt() ^ RHS, E);
1208 case BinaryOperator::Or: return Success(Result.getInt() | RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001209 case BinaryOperator::Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001210 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001211 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001212 return Success(Result.getInt() / RHS, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001213 case BinaryOperator::Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001214 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001215 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001216 return Success(Result.getInt() % RHS, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001217 case BinaryOperator::Shl: {
Chris Lattner54176fd2008-07-12 00:14:42 +00001218 // FIXME: Warn about out of range shift amounts!
Mike Stump1eb44332009-09-09 15:08:12 +00001219 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001220 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1221 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001222 }
1223 case BinaryOperator::Shr: {
Mike Stump1eb44332009-09-09 15:08:12 +00001224 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001225 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1226 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001229 case BinaryOperator::LT: return Success(Result.getInt() < RHS, E);
1230 case BinaryOperator::GT: return Success(Result.getInt() > RHS, E);
1231 case BinaryOperator::LE: return Success(Result.getInt() <= RHS, E);
1232 case BinaryOperator::GE: return Success(Result.getInt() >= RHS, E);
1233 case BinaryOperator::EQ: return Success(Result.getInt() == RHS, E);
1234 case BinaryOperator::NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001235 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001236}
1237
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001238bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
Nuno Lopesa25bd552008-11-16 22:06:39 +00001239 bool Cond;
1240 if (!HandleConversionToBool(E->getCond(), Cond, Info))
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001241 return false;
1242
Nuno Lopesa25bd552008-11-16 22:06:39 +00001243 return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
Nuno Lopesca7c2ea2008-11-16 19:28:31 +00001244}
1245
Chris Lattneraf707ab2009-01-24 21:53:27 +00001246unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
Chris Lattnere9feb472009-01-24 21:09:06 +00001247 // Get information about the alignment.
1248 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001249
Eli Friedman2be58612009-05-30 21:09:44 +00001250 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001251 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001252}
1253
Chris Lattneraf707ab2009-01-24 21:53:27 +00001254unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1255 E = E->IgnoreParens();
1256
1257 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001258 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001259 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001260 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl());
Eli Friedmana1f47c42009-03-23 04:38:34 +00001261
Chris Lattneraf707ab2009-01-24 21:53:27 +00001262 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Daniel Dunbarb7d08442009-02-17 22:16:19 +00001263 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl());
Chris Lattneraf707ab2009-01-24 21:53:27 +00001264
Chris Lattnere9feb472009-01-24 21:09:06 +00001265 return GetAlignOfType(E->getType());
1266}
1267
1268
Sebastian Redl05189992008-11-11 17:56:53 +00001269/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1270/// expression's type.
1271bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1272 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001273
Chris Lattnere9feb472009-01-24 21:09:06 +00001274 // Handle alignof separately.
1275 if (!E->isSizeOf()) {
1276 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001277 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001278 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001279 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001280 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001281
Sebastian Redl05189992008-11-11 17:56:53 +00001282 QualType SrcTy = E->getTypeOfArgument();
1283
Daniel Dunbar131eb432009-02-19 09:06:44 +00001284 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1285 // extension.
1286 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1287 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001288
Chris Lattnerfcee0012008-07-11 21:24:13 +00001289 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001290 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001291 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001292
Chris Lattnere9feb472009-01-24 21:09:06 +00001293 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001294 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001295 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001296}
1297
Chris Lattnerb542afe2008-07-11 19:10:17 +00001298bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001299 // Special case unary operators that do not need their subexpression
1300 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001301 if (E->isOffsetOfOp()) {
1302 // The AST for offsetof is defined in such a way that we can just
1303 // directly Evaluate it as an l-value.
1304 APValue LV;
1305 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1306 return false;
1307 if (LV.getLValueBase())
1308 return false;
1309 return Success(LV.getLValueOffset(), E);
1310 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001311
1312 if (E->getOpcode() == UnaryOperator::LNot) {
1313 // LNot's operand isn't necessarily an integer, so we handle it specially.
1314 bool bres;
1315 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1316 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001317 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001318 }
1319
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001320 // Only handle integral operations...
1321 if (!E->getSubExpr()->getType()->isIntegralType())
1322 return false;
1323
Chris Lattner87eae5e2008-07-11 22:52:41 +00001324 // Get the operand value into 'Result'.
1325 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001326 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001327
Chris Lattner75a48812008-07-11 22:15:16 +00001328 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001329 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001330 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1331 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001332 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001333 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001334 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1335 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001336 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001337 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001338 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001339 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001340 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001341 if (!Result.isInt()) return false;
1342 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001343 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001344 if (!Result.isInt()) return false;
1345 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001346 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001347}
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattner732b2232008-07-12 01:15:53 +00001349/// HandleCast - This is used to evaluate implicit or explicit casts where the
1350/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001351bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001352 Expr *SubExpr = E->getSubExpr();
1353 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001354 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001355
Eli Friedman4efaa272008-11-12 09:44:48 +00001356 if (DestType->isBooleanType()) {
1357 bool BoolResult;
1358 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1359 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001360 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001361 }
1362
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001363 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001364 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001365 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001366 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001367
Eli Friedmanbe265702009-02-20 01:15:07 +00001368 if (!Result.isInt()) {
1369 // Only allow casts of lvalues if they are lossless.
1370 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1371 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001372
Daniel Dunbardd211642009-02-19 22:24:01 +00001373 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001374 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Chris Lattner732b2232008-07-12 01:15:53 +00001377 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001378 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001379 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001380 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001381 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001382
Daniel Dunbardd211642009-02-19 22:24:01 +00001383 if (LV.getLValueBase()) {
1384 // Only allow based lvalue casts if they are lossless.
1385 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1386 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001387
Daniel Dunbardd211642009-02-19 22:24:01 +00001388 Result = LV;
1389 return true;
1390 }
1391
1392 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1393 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001394 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001395
Eli Friedmanbe265702009-02-20 01:15:07 +00001396 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1397 // This handles double-conversion cases, where there's both
1398 // an l-value promotion and an implicit conversion to int.
1399 APValue LV;
1400 if (!EvaluateLValue(SubExpr, LV, Info))
1401 return false;
1402
1403 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1404 return false;
1405
1406 Result = LV;
1407 return true;
1408 }
1409
Eli Friedman1725f682009-04-22 19:23:09 +00001410 if (SrcType->isAnyComplexType()) {
1411 APValue C;
1412 if (!EvaluateComplex(SubExpr, C, Info))
1413 return false;
1414 if (C.isComplexFloat())
1415 return Success(HandleFloatToIntCast(DestType, SrcType,
1416 C.getComplexFloatReal(), Info.Ctx),
1417 E);
1418 else
1419 return Success(HandleIntToIntCast(DestType, SrcType,
1420 C.getComplexIntReal(), Info.Ctx), E);
1421 }
Eli Friedman2217c872009-02-22 11:46:18 +00001422 // FIXME: Handle vectors
1423
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001424 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001425 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001426
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001427 APFloat F(0.0);
1428 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001429 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001431 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001432}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001433
Eli Friedman722c7172009-02-28 03:59:05 +00001434bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1435 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1436 APValue LV;
1437 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1438 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1439 return Success(LV.getComplexIntReal(), E);
1440 }
1441
1442 return Visit(E->getSubExpr());
1443}
1444
Eli Friedman664a1042009-02-27 04:45:43 +00001445bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001446 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1447 APValue LV;
1448 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1449 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1450 return Success(LV.getComplexIntImag(), E);
1451 }
1452
Eli Friedman664a1042009-02-27 04:45:43 +00001453 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1454 Info.EvalResult.HasSideEffects = true;
1455 return Success(0, E);
1456}
1457
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001458//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001459// Float Evaluation
1460//===----------------------------------------------------------------------===//
1461
1462namespace {
1463class VISIBILITY_HIDDEN FloatExprEvaluator
1464 : public StmtVisitor<FloatExprEvaluator, bool> {
1465 EvalInfo &Info;
1466 APFloat &Result;
1467public:
1468 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1469 : Info(info), Result(result) {}
1470
1471 bool VisitStmt(Stmt *S) {
1472 return false;
1473 }
1474
1475 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001476 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001477
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001478 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001479 bool VisitBinaryOperator(const BinaryOperator *E);
1480 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001481 bool VisitCastExpr(CastExpr *E);
1482 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001483
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001484 bool VisitChooseExpr(const ChooseExpr *E)
1485 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1486 bool VisitUnaryExtension(const UnaryOperator *E)
1487 { return Visit(E->getSubExpr()); }
1488
1489 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1490 // member of vector, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001491 // conditional ?:, comma
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001492};
1493} // end anonymous namespace
1494
1495static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1496 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1497}
1498
Chris Lattner019f4e82008-10-06 05:28:25 +00001499bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001500 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001501 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001502 case Builtin::BI__builtin_huge_val:
1503 case Builtin::BI__builtin_huge_valf:
1504 case Builtin::BI__builtin_huge_vall:
1505 case Builtin::BI__builtin_inf:
1506 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001507 case Builtin::BI__builtin_infl: {
1508 const llvm::fltSemantics &Sem =
1509 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001510 Result = llvm::APFloat::getInf(Sem);
1511 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Chris Lattner9e621712008-10-06 06:31:58 +00001514 case Builtin::BI__builtin_nan:
1515 case Builtin::BI__builtin_nanf:
1516 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001517 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001518 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001519 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001520 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001521 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001522 const llvm::fltSemantics &Sem =
1523 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001524 llvm::SmallString<16> s;
1525 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1526 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001527 long l;
1528 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001529 l = strtol(&s[0], &endp, 0);
1530 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001531 return false;
1532 unsigned type = (unsigned int)l;;
1533 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001534 return true;
1535 }
1536 }
1537 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001538
1539 case Builtin::BI__builtin_fabs:
1540 case Builtin::BI__builtin_fabsf:
1541 case Builtin::BI__builtin_fabsl:
1542 if (!EvaluateFloat(E->getArg(0), Result, Info))
1543 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001545 if (Result.isNegative())
1546 Result.changeSign();
1547 return true;
1548
Mike Stump1eb44332009-09-09 15:08:12 +00001549 case Builtin::BI__builtin_copysign:
1550 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001551 case Builtin::BI__builtin_copysignl: {
1552 APFloat RHS(0.);
1553 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1554 !EvaluateFloat(E->getArg(1), RHS, Info))
1555 return false;
1556 Result.copySign(RHS);
1557 return true;
1558 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001559 }
1560}
1561
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001562bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001563 if (E->getOpcode() == UnaryOperator::Deref)
1564 return false;
1565
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001566 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1567 return false;
1568
1569 switch (E->getOpcode()) {
1570 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001571 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001572 return true;
1573 case UnaryOperator::Minus:
1574 Result.changeSign();
1575 return true;
1576 }
1577}
Chris Lattner019f4e82008-10-06 05:28:25 +00001578
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001579bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
1580 // FIXME: Diagnostics? I really don't understand how the warnings
1581 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001582 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001583 if (!EvaluateFloat(E->getLHS(), Result, Info))
1584 return false;
1585 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1586 return false;
1587
1588 switch (E->getOpcode()) {
1589 default: return false;
1590 case BinaryOperator::Mul:
1591 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1592 return true;
1593 case BinaryOperator::Add:
1594 Result.add(RHS, APFloat::rmNearestTiesToEven);
1595 return true;
1596 case BinaryOperator::Sub:
1597 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1598 return true;
1599 case BinaryOperator::Div:
1600 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1601 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001602 }
1603}
1604
1605bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1606 Result = E->getValue();
1607 return true;
1608}
1609
Eli Friedman4efaa272008-11-12 09:44:48 +00001610bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1611 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Eli Friedman4efaa272008-11-12 09:44:48 +00001613 if (SubExpr->getType()->isIntegralType()) {
1614 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001615 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001616 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001617 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001618 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001619 return true;
1620 }
1621 if (SubExpr->getType()->isRealFloatingType()) {
1622 if (!Visit(SubExpr))
1623 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001624 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1625 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001626 return true;
1627 }
Eli Friedman2217c872009-02-22 11:46:18 +00001628 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001629
1630 return false;
1631}
1632
1633bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1634 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1635 return true;
1636}
1637
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001638//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001639// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001640//===----------------------------------------------------------------------===//
1641
1642namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001643class VISIBILITY_HIDDEN ComplexExprEvaluator
1644 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001645 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001647public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001648 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001650 //===--------------------------------------------------------------------===//
1651 // Visitor Methods
1652 //===--------------------------------------------------------------------===//
1653
1654 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001655 return APValue();
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001658 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1659
1660 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001661 Expr* SubExpr = E->getSubExpr();
1662
1663 if (SubExpr->getType()->isRealFloatingType()) {
1664 APFloat Result(0.0);
1665
1666 if (!EvaluateFloat(SubExpr, Result, Info))
1667 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001668
1669 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001670 Result);
1671 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001672 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001673 "Unexpected imaginary literal.");
1674
1675 llvm::APSInt Result;
1676 if (!EvaluateInteger(SubExpr, Result, Info))
1677 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001679 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1680 Zero = 0;
1681 return APValue(Zero, Result);
1682 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001683 }
1684
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001685 APValue VisitCastExpr(CastExpr *E) {
1686 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001687 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001688 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001689
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001690 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001691 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001692
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001693 if (!EvaluateFloat(SubExpr, Result, Info))
1694 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001695
1696 if (EltType->isRealFloatingType()) {
1697 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001698 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001699 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1700 } else {
1701 llvm::APSInt IResult;
1702 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1703 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1704 Zero = 0;
1705 return APValue(IResult, Zero);
1706 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001707 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001708 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001709
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001710 if (!EvaluateInteger(SubExpr, Result, Info))
1711 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001712
Eli Friedman1725f682009-04-22 19:23:09 +00001713 if (EltType->isRealFloatingType()) {
1714 APFloat FResult =
1715 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001716 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001717 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1718 } else {
1719 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1720 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1721 Zero = 0;
1722 return APValue(Result, Zero);
1723 }
John McCall183700f2009-09-21 23:43:11 +00001724 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001725 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001726
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001727 if (!EvaluateComplex(SubExpr, Src, Info))
1728 return APValue();
1729
1730 QualType SrcType = CT->getElementType();
1731
1732 if (Src.isComplexFloat()) {
1733 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001734 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001735 Src.getComplexFloatReal(),
1736 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001737 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001738 Src.getComplexFloatImag(),
1739 Info.Ctx));
1740 } else {
1741 return APValue(HandleFloatToIntCast(EltType, SrcType,
1742 Src.getComplexFloatReal(),
1743 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001744 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001745 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001746 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001747 }
1748 } else {
1749 assert(Src.isComplexInt() && "Invalid evaluate result.");
1750 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001751 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001752 Src.getComplexIntReal(),
1753 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001754 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001755 Src.getComplexIntImag(),
1756 Info.Ctx));
1757 } else {
1758 return APValue(HandleIntToIntCast(EltType, SrcType,
1759 Src.getComplexIntReal(),
1760 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001761 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001762 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001763 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001764 }
1765 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001766 }
1767
1768 // FIXME: Handle more casts.
1769 return APValue();
1770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001772 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001773 APValue VisitChooseExpr(const ChooseExpr *E)
1774 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1775 APValue VisitUnaryExtension(const UnaryOperator *E)
1776 { return Visit(E->getSubExpr()); }
1777 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001778 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001779};
1780} // end anonymous namespace
1781
Mike Stump1eb44332009-09-09 15:08:12 +00001782static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001783 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1784 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001785 (&Result.getComplexFloatReal().getSemantics() ==
1786 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001787 "Invalid complex evaluation.");
1788 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001789}
1790
Mike Stump1eb44332009-09-09 15:08:12 +00001791APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001792 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001794 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001795 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001797 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001798 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001799
Daniel Dunbar3f279872009-01-29 01:32:56 +00001800 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1801 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001802 switch (E->getOpcode()) {
1803 default: return APValue();
1804 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001805 if (Result.isComplexFloat()) {
1806 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1807 APFloat::rmNearestTiesToEven);
1808 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1809 APFloat::rmNearestTiesToEven);
1810 } else {
1811 Result.getComplexIntReal() += RHS.getComplexIntReal();
1812 Result.getComplexIntImag() += RHS.getComplexIntImag();
1813 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001814 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001815 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001816 if (Result.isComplexFloat()) {
1817 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1818 APFloat::rmNearestTiesToEven);
1819 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1820 APFloat::rmNearestTiesToEven);
1821 } else {
1822 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1823 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1824 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001825 break;
1826 case BinaryOperator::Mul:
1827 if (Result.isComplexFloat()) {
1828 APValue LHS = Result;
1829 APFloat &LHS_r = LHS.getComplexFloatReal();
1830 APFloat &LHS_i = LHS.getComplexFloatImag();
1831 APFloat &RHS_r = RHS.getComplexFloatReal();
1832 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Daniel Dunbar3f279872009-01-29 01:32:56 +00001834 APFloat Tmp = LHS_r;
1835 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1836 Result.getComplexFloatReal() = Tmp;
1837 Tmp = LHS_i;
1838 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1839 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1840
1841 Tmp = LHS_r;
1842 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1843 Result.getComplexFloatImag() = Tmp;
1844 Tmp = LHS_i;
1845 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1846 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1847 } else {
1848 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001849 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001850 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1851 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001852 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001853 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1854 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1855 }
1856 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001857 }
1858
1859 return Result;
1860}
1861
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001862//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001863// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001864//===----------------------------------------------------------------------===//
1865
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001866/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001867/// any crazy technique (that has nothing to do with language standards) that
1868/// we want to. If this function returns true, it returns the folded constant
1869/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001870bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1871 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001872
Nate Begeman59b5da62009-01-18 03:20:47 +00001873 if (getType()->isVectorType()) {
1874 if (!EvaluateVector(this, Result.Val, Info))
1875 return false;
1876 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001877 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001878 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001879 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001880 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001881 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001882 } else if (getType()->isRealFloatingType()) {
1883 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001884 if (!EvaluateFloat(this, f, Info))
1885 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001887 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001888 } else if (getType()->isAnyComplexType()) {
1889 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001890 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001891 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001892 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001893
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001894 return true;
1895}
1896
Mike Stump660e6f72009-10-26 23:05:19 +00001897bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1898 EvalInfo Info(Ctx, Result, true);
1899
1900 if (getType()->isVectorType()) {
1901 if (!EvaluateVector(this, Result.Val, Info))
1902 return false;
1903 } else if (getType()->isIntegerType()) {
1904 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1905 return false;
1906 } else if (getType()->hasPointerRepresentation()) {
1907 if (!EvaluatePointer(this, Result.Val, Info))
1908 return false;
1909 } else if (getType()->isRealFloatingType()) {
1910 llvm::APFloat f(0.0);
1911 if (!EvaluateFloat(this, f, Info))
1912 return false;
1913
1914 Result.Val = APValue(f);
1915 } else if (getType()->isAnyComplexType()) {
1916 if (!EvaluateComplex(this, Result.Val, Info))
1917 return false;
1918 } else
1919 return false;
1920
1921 return true;
1922}
1923
Anders Carlsson1b782762009-04-10 04:54:13 +00001924bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1925 EvalInfo Info(Ctx, Result);
1926
1927 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1928}
1929
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001930bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1931 EvalInfo Info(Ctx, Result, true);
1932
1933 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1934}
1935
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001936/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001937/// folded, but discard the result.
1938bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001939 EvalResult Result;
1940 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001941}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001942
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001943bool Expr::HasSideEffects(ASTContext &Ctx) const {
1944 Expr::EvalResult Result;
1945 EvalInfo Info(Ctx, Result);
1946 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1947}
1948
Anders Carlsson51fe9962008-11-22 21:04:56 +00001949APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001950 EvalResult EvalResult;
1951 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001952 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001953 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001954 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001955
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001956 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001957}