blob: d738afdd81bcd6380177a76705c4ffaacd3d826c [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) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001247 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1248 // the result is the size of the referenced type."
1249 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1250 // result shall be the alignment of the referenced type."
1251 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1252 T = Ref->getPointeeType();
1253
Chris Lattnere9feb472009-01-24 21:09:06 +00001254 // Get information about the alignment.
1255 unsigned CharSize = Info.Ctx.Target.getCharWidth();
Douglas Gregor18857642009-04-30 17:32:17 +00001256
Eli Friedman2be58612009-05-30 21:09:44 +00001257 // __alignof is defined to return the preferred alignment.
Douglas Gregor18857642009-04-30 17:32:17 +00001258 return Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize;
Chris Lattnere9feb472009-01-24 21:09:06 +00001259}
1260
Chris Lattneraf707ab2009-01-24 21:53:27 +00001261unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
1262 E = E->IgnoreParens();
1263
1264 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001265 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001266 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001267 return Info.Ctx.getDeclAlignInBytes(DRE->getDecl(), /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001268
Chris Lattneraf707ab2009-01-24 21:53:27 +00001269 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Sebastian Redl5d484e82009-11-23 17:18:46 +00001270 return Info.Ctx.getDeclAlignInBytes(ME->getMemberDecl(),
1271 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001272
Chris Lattnere9feb472009-01-24 21:09:06 +00001273 return GetAlignOfType(E->getType());
1274}
1275
1276
Sebastian Redl05189992008-11-11 17:56:53 +00001277/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
1278/// expression's type.
1279bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
1280 QualType DstTy = E->getType();
Chris Lattnerfcee0012008-07-11 21:24:13 +00001281
Chris Lattnere9feb472009-01-24 21:09:06 +00001282 // Handle alignof separately.
1283 if (!E->isSizeOf()) {
1284 if (E->isArgumentType())
Daniel Dunbar131eb432009-02-19 09:06:44 +00001285 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001286 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001287 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001288 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001289
Sebastian Redl05189992008-11-11 17:56:53 +00001290 QualType SrcTy = E->getTypeOfArgument();
Sebastian Redl5d484e82009-11-23 17:18:46 +00001291 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1292 // the result is the size of the referenced type."
1293 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1294 // result shall be the alignment of the referenced type."
1295 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1296 SrcTy = Ref->getPointeeType();
Sebastian Redl05189992008-11-11 17:56:53 +00001297
Daniel Dunbar131eb432009-02-19 09:06:44 +00001298 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1299 // extension.
1300 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1301 return Success(1, E);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001302
Chris Lattnerfcee0012008-07-11 21:24:13 +00001303 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnere9feb472009-01-24 21:09:06 +00001304 if (!SrcTy->isConstantSizeType())
Chris Lattnerfcee0012008-07-11 21:24:13 +00001305 return false;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001306
Chris Lattnere9feb472009-01-24 21:09:06 +00001307 // Get information about the size.
Daniel Dunbar24cbfb92009-05-03 10:35:52 +00001308 unsigned BitWidth = Info.Ctx.getTypeSize(SrcTy);
Daniel Dunbarff896662009-04-21 15:48:54 +00001309 return Success(BitWidth / Info.Ctx.Target.getCharWidth(), E);
Chris Lattnerfcee0012008-07-11 21:24:13 +00001310}
1311
Chris Lattnerb542afe2008-07-11 19:10:17 +00001312bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001313 // Special case unary operators that do not need their subexpression
1314 // evaluated. offsetof/sizeof/alignof are all special.
Eli Friedman35183ac2009-02-27 06:44:11 +00001315 if (E->isOffsetOfOp()) {
1316 // The AST for offsetof is defined in such a way that we can just
1317 // directly Evaluate it as an l-value.
1318 APValue LV;
1319 if (!EvaluateLValue(E->getSubExpr(), LV, Info))
1320 return false;
1321 if (LV.getLValueBase())
1322 return false;
1323 return Success(LV.getLValueOffset(), E);
1324 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001325
1326 if (E->getOpcode() == UnaryOperator::LNot) {
1327 // LNot's operand isn't necessarily an integer, so we handle it specially.
1328 bool bres;
1329 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1330 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001331 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001332 }
1333
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001334 // Only handle integral operations...
1335 if (!E->getSubExpr()->getType()->isIntegralType())
1336 return false;
1337
Chris Lattner87eae5e2008-07-11 22:52:41 +00001338 // Get the operand value into 'Result'.
1339 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001340 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001341
Chris Lattner75a48812008-07-11 22:15:16 +00001342 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001343 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001344 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1345 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001346 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner75a48812008-07-11 22:15:16 +00001347 case UnaryOperator::Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001348 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1349 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001350 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001351 case UnaryOperator::Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001353 return true;
Chris Lattner75a48812008-07-11 22:15:16 +00001354 case UnaryOperator::Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001355 if (!Result.isInt()) return false;
1356 return Success(-Result.getInt(), E);
Chris Lattner75a48812008-07-11 22:15:16 +00001357 case UnaryOperator::Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001358 if (!Result.isInt()) return false;
1359 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001360 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001361}
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattner732b2232008-07-12 01:15:53 +00001363/// HandleCast - This is used to evaluate implicit or explicit casts where the
1364/// result type is integer.
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001365bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
Anders Carlsson82206e22008-11-30 18:14:57 +00001366 Expr *SubExpr = E->getSubExpr();
1367 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001368 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001369
Eli Friedman4efaa272008-11-12 09:44:48 +00001370 if (DestType->isBooleanType()) {
1371 bool BoolResult;
1372 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1373 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001374 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001375 }
1376
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001377 // Handle simple integer->integer casts.
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001378 if (SrcType->isIntegralType()) {
Chris Lattner732b2232008-07-12 01:15:53 +00001379 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001380 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001381
Eli Friedmanbe265702009-02-20 01:15:07 +00001382 if (!Result.isInt()) {
1383 // Only allow casts of lvalues if they are lossless.
1384 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1385 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001386
Daniel Dunbardd211642009-02-19 22:24:01 +00001387 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001388 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Chris Lattner732b2232008-07-12 01:15:53 +00001391 // FIXME: Clean this up!
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001392 if (SrcType->isPointerType()) {
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001393 APValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001394 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001395 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001396
Daniel Dunbardd211642009-02-19 22:24:01 +00001397 if (LV.getLValueBase()) {
1398 // Only allow based lvalue casts if they are lossless.
1399 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1400 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001401
Daniel Dunbardd211642009-02-19 22:24:01 +00001402 Result = LV;
1403 return true;
1404 }
1405
1406 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset(), SrcType);
1407 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001408 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001409
Eli Friedmanbe265702009-02-20 01:15:07 +00001410 if (SrcType->isArrayType() || SrcType->isFunctionType()) {
1411 // This handles double-conversion cases, where there's both
1412 // an l-value promotion and an implicit conversion to int.
1413 APValue LV;
1414 if (!EvaluateLValue(SubExpr, LV, Info))
1415 return false;
1416
1417 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
1418 return false;
1419
1420 Result = LV;
1421 return true;
1422 }
1423
Eli Friedman1725f682009-04-22 19:23:09 +00001424 if (SrcType->isAnyComplexType()) {
1425 APValue C;
1426 if (!EvaluateComplex(SubExpr, C, Info))
1427 return false;
1428 if (C.isComplexFloat())
1429 return Success(HandleFloatToIntCast(DestType, SrcType,
1430 C.getComplexFloatReal(), Info.Ctx),
1431 E);
1432 else
1433 return Success(HandleIntToIntCast(DestType, SrcType,
1434 C.getComplexIntReal(), Info.Ctx), E);
1435 }
Eli Friedman2217c872009-02-22 11:46:18 +00001436 // FIXME: Handle vectors
1437
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001438 if (!SrcType->isRealFloatingType())
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001439 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner732b2232008-07-12 01:15:53 +00001440
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001441 APFloat F(0.0);
1442 if (!EvaluateFloat(SubExpr, F, Info))
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001443 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001445 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001446}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001447
Eli Friedman722c7172009-02-28 03:59:05 +00001448bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1449 if (E->getSubExpr()->getType()->isAnyComplexType()) {
1450 APValue LV;
1451 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1452 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1453 return Success(LV.getComplexIntReal(), E);
1454 }
1455
1456 return Visit(E->getSubExpr());
1457}
1458
Eli Friedman664a1042009-02-27 04:45:43 +00001459bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001460 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
1461 APValue LV;
1462 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1463 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1464 return Success(LV.getComplexIntImag(), E);
1465 }
1466
Eli Friedman664a1042009-02-27 04:45:43 +00001467 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1468 Info.EvalResult.HasSideEffects = true;
1469 return Success(0, E);
1470}
1471
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001472//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001473// Float Evaluation
1474//===----------------------------------------------------------------------===//
1475
1476namespace {
1477class VISIBILITY_HIDDEN FloatExprEvaluator
1478 : public StmtVisitor<FloatExprEvaluator, bool> {
1479 EvalInfo &Info;
1480 APFloat &Result;
1481public:
1482 FloatExprEvaluator(EvalInfo &info, APFloat &result)
1483 : Info(info), Result(result) {}
1484
1485 bool VisitStmt(Stmt *S) {
1486 return false;
1487 }
1488
1489 bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
Chris Lattner019f4e82008-10-06 05:28:25 +00001490 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001491
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001492 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001493 bool VisitBinaryOperator(const BinaryOperator *E);
1494 bool VisitFloatingLiteral(const FloatingLiteral *E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001495 bool VisitCastExpr(CastExpr *E);
1496 bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001497
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001498 bool VisitChooseExpr(const ChooseExpr *E)
1499 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1500 bool VisitUnaryExtension(const UnaryOperator *E)
1501 { return Visit(E->getSubExpr()); }
1502
1503 // FIXME: Missing: __real__/__imag__, array subscript of vector,
1504 // member of vector, ImplicitValueInitExpr,
Eli Friedman7f92f032009-11-16 04:25:37 +00001505 // conditional ?:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001506};
1507} // end anonymous namespace
1508
1509static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
1510 return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
1511}
1512
Chris Lattner019f4e82008-10-06 05:28:25 +00001513bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001514 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner34a74ab2008-10-06 05:53:16 +00001515 default: return false;
Chris Lattner019f4e82008-10-06 05:28:25 +00001516 case Builtin::BI__builtin_huge_val:
1517 case Builtin::BI__builtin_huge_valf:
1518 case Builtin::BI__builtin_huge_vall:
1519 case Builtin::BI__builtin_inf:
1520 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001521 case Builtin::BI__builtin_infl: {
1522 const llvm::fltSemantics &Sem =
1523 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001524 Result = llvm::APFloat::getInf(Sem);
1525 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Chris Lattner9e621712008-10-06 06:31:58 +00001528 case Builtin::BI__builtin_nan:
1529 case Builtin::BI__builtin_nanf:
1530 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001531 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001532 // can't constant fold it.
Mike Stump1eb44332009-09-09 15:08:12 +00001533 if (const StringLiteral *S =
Chris Lattner9e621712008-10-06 06:31:58 +00001534 dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
Mike Stump4572bab2009-05-30 03:56:50 +00001535 if (!S->isWide()) {
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001536 const llvm::fltSemantics &Sem =
1537 Info.Ctx.getFloatTypeSemantics(E->getType());
Mike Stump7462b392009-05-30 14:43:18 +00001538 llvm::SmallString<16> s;
1539 s.append(S->getStrData(), S->getStrData() + S->getByteLength());
1540 s += '\0';
Mike Stump4572bab2009-05-30 03:56:50 +00001541 long l;
1542 char *endp;
Mike Stump7462b392009-05-30 14:43:18 +00001543 l = strtol(&s[0], &endp, 0);
1544 if (endp != s.end()-1)
Mike Stump4572bab2009-05-30 03:56:50 +00001545 return false;
1546 unsigned type = (unsigned int)l;;
1547 Result = llvm::APFloat::getNaN(Sem, false, type);
Chris Lattner9e621712008-10-06 06:31:58 +00001548 return true;
1549 }
1550 }
1551 return false;
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001552
1553 case Builtin::BI__builtin_fabs:
1554 case Builtin::BI__builtin_fabsf:
1555 case Builtin::BI__builtin_fabsl:
1556 if (!EvaluateFloat(E->getArg(0), Result, Info))
1557 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001559 if (Result.isNegative())
1560 Result.changeSign();
1561 return true;
1562
Mike Stump1eb44332009-09-09 15:08:12 +00001563 case Builtin::BI__builtin_copysign:
1564 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001565 case Builtin::BI__builtin_copysignl: {
1566 APFloat RHS(0.);
1567 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
1568 !EvaluateFloat(E->getArg(1), RHS, Info))
1569 return false;
1570 Result.copySign(RHS);
1571 return true;
1572 }
Chris Lattner019f4e82008-10-06 05:28:25 +00001573 }
1574}
1575
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001576bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
Nuno Lopesa468d342008-11-19 17:44:31 +00001577 if (E->getOpcode() == UnaryOperator::Deref)
1578 return false;
1579
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001580 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
1581 return false;
1582
1583 switch (E->getOpcode()) {
1584 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001585 case UnaryOperator::Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001586 return true;
1587 case UnaryOperator::Minus:
1588 Result.changeSign();
1589 return true;
1590 }
1591}
Chris Lattner019f4e82008-10-06 05:28:25 +00001592
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001593bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Eli Friedman7f92f032009-11-16 04:25:37 +00001594 if (E->getOpcode() == BinaryOperator::Comma) {
1595 if (!EvaluateFloat(E->getRHS(), Result, Info))
1596 return false;
1597
1598 // If we can't evaluate the LHS, it might have side effects;
1599 // conservatively mark it.
1600 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1601 Info.EvalResult.HasSideEffects = true;
1602
1603 return true;
1604 }
1605
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001606 // FIXME: Diagnostics? I really don't understand how the warnings
1607 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001608 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001609 if (!EvaluateFloat(E->getLHS(), Result, Info))
1610 return false;
1611 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1612 return false;
1613
1614 switch (E->getOpcode()) {
1615 default: return false;
1616 case BinaryOperator::Mul:
1617 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
1618 return true;
1619 case BinaryOperator::Add:
1620 Result.add(RHS, APFloat::rmNearestTiesToEven);
1621 return true;
1622 case BinaryOperator::Sub:
1623 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
1624 return true;
1625 case BinaryOperator::Div:
1626 Result.divide(RHS, APFloat::rmNearestTiesToEven);
1627 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001628 }
1629}
1630
1631bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
1632 Result = E->getValue();
1633 return true;
1634}
1635
Eli Friedman4efaa272008-11-12 09:44:48 +00001636bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
1637 Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Eli Friedman4efaa272008-11-12 09:44:48 +00001639 if (SubExpr->getType()->isIntegralType()) {
1640 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001641 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00001642 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001643 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001644 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001645 return true;
1646 }
1647 if (SubExpr->getType()->isRealFloatingType()) {
1648 if (!Visit(SubExpr))
1649 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001650 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
1651 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00001652 return true;
1653 }
Eli Friedman2217c872009-02-22 11:46:18 +00001654 // FIXME: Handle complex types
Eli Friedman4efaa272008-11-12 09:44:48 +00001655
1656 return false;
1657}
1658
1659bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
1660 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
1661 return true;
1662}
1663
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001664//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001665// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001666//===----------------------------------------------------------------------===//
1667
1668namespace {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001669class VISIBILITY_HIDDEN ComplexExprEvaluator
1670 : public StmtVisitor<ComplexExprEvaluator, APValue> {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001671 EvalInfo &Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001673public:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001674 ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001676 //===--------------------------------------------------------------------===//
1677 // Visitor Methods
1678 //===--------------------------------------------------------------------===//
1679
1680 APValue VisitStmt(Stmt *S) {
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001681 return APValue();
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001684 APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
1685
1686 APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001687 Expr* SubExpr = E->getSubExpr();
1688
1689 if (SubExpr->getType()->isRealFloatingType()) {
1690 APFloat Result(0.0);
1691
1692 if (!EvaluateFloat(SubExpr, Result, Info))
1693 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001694
1695 return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001696 Result);
1697 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001698 assert(SubExpr->getType()->isIntegerType() &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001699 "Unexpected imaginary literal.");
1700
1701 llvm::APSInt Result;
1702 if (!EvaluateInteger(SubExpr, Result, Info))
1703 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001705 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1706 Zero = 0;
1707 return APValue(Zero, Result);
1708 }
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001709 }
1710
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001711 APValue VisitCastExpr(CastExpr *E) {
1712 Expr* SubExpr = E->getSubExpr();
John McCall183700f2009-09-21 23:43:11 +00001713 QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001714 QualType SubType = SubExpr->getType();
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001715
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001716 if (SubType->isRealFloatingType()) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001717 APFloat Result(0.0);
Eli Friedman1725f682009-04-22 19:23:09 +00001718
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001719 if (!EvaluateFloat(SubExpr, Result, Info))
1720 return APValue();
Eli Friedman1725f682009-04-22 19:23:09 +00001721
1722 if (EltType->isRealFloatingType()) {
1723 Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001724 return APValue(Result,
Eli Friedman1725f682009-04-22 19:23:09 +00001725 APFloat(Result.getSemantics(), APFloat::fcZero, false));
1726 } else {
1727 llvm::APSInt IResult;
1728 IResult = HandleFloatToIntCast(EltType, SubType, Result, Info.Ctx);
1729 llvm::APSInt Zero(IResult.getBitWidth(), !IResult.isSigned());
1730 Zero = 0;
1731 return APValue(IResult, Zero);
1732 }
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001733 } else if (SubType->isIntegerType()) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001734 APSInt Result;
Eli Friedman1725f682009-04-22 19:23:09 +00001735
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001736 if (!EvaluateInteger(SubExpr, Result, Info))
1737 return APValue();
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001738
Eli Friedman1725f682009-04-22 19:23:09 +00001739 if (EltType->isRealFloatingType()) {
1740 APFloat FResult =
1741 HandleIntToFloatCast(EltType, SubType, Result, Info.Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001742 return APValue(FResult,
Eli Friedman1725f682009-04-22 19:23:09 +00001743 APFloat(FResult.getSemantics(), APFloat::fcZero, false));
1744 } else {
1745 Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
1746 llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
1747 Zero = 0;
1748 return APValue(Result, Zero);
1749 }
John McCall183700f2009-09-21 23:43:11 +00001750 } else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001751 APValue Src;
Eli Friedman1725f682009-04-22 19:23:09 +00001752
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001753 if (!EvaluateComplex(SubExpr, Src, Info))
1754 return APValue();
1755
1756 QualType SrcType = CT->getElementType();
1757
1758 if (Src.isComplexFloat()) {
1759 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001760 return APValue(HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001761 Src.getComplexFloatReal(),
1762 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001763 HandleFloatToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001764 Src.getComplexFloatImag(),
1765 Info.Ctx));
1766 } else {
1767 return APValue(HandleFloatToIntCast(EltType, SrcType,
1768 Src.getComplexFloatReal(),
1769 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001770 HandleFloatToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001771 Src.getComplexFloatImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001772 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001773 }
1774 } else {
1775 assert(Src.isComplexInt() && "Invalid evaluate result.");
1776 if (EltType->isRealFloatingType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001777 return APValue(HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001778 Src.getComplexIntReal(),
1779 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001780 HandleIntToFloatCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001781 Src.getComplexIntImag(),
1782 Info.Ctx));
1783 } else {
1784 return APValue(HandleIntToIntCast(EltType, SrcType,
1785 Src.getComplexIntReal(),
1786 Info.Ctx),
Mike Stump1eb44332009-09-09 15:08:12 +00001787 HandleIntToIntCast(EltType, SrcType,
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001788 Src.getComplexIntImag(),
Mike Stump1eb44332009-09-09 15:08:12 +00001789 Info.Ctx));
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001790 }
1791 }
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001792 }
1793
1794 // FIXME: Handle more casts.
1795 return APValue();
1796 }
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001798 APValue VisitBinaryOperator(const BinaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001799 APValue VisitChooseExpr(const ChooseExpr *E)
1800 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
1801 APValue VisitUnaryExtension(const UnaryOperator *E)
1802 { return Visit(E->getSubExpr()); }
1803 // FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
Eli Friedman2217c872009-02-22 11:46:18 +00001804 // conditional ?:, comma
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001805};
1806} // end anonymous namespace
1807
Mike Stump1eb44332009-09-09 15:08:12 +00001808static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info) {
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001809 Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
1810 assert((!Result.isComplexFloat() ||
Mike Stump1eb44332009-09-09 15:08:12 +00001811 (&Result.getComplexFloatReal().getSemantics() ==
1812 &Result.getComplexFloatImag().getSemantics())) &&
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001813 "Invalid complex evaluation.");
1814 return Result.isComplexFloat() || Result.isComplexInt();
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001815}
1816
Mike Stump1eb44332009-09-09 15:08:12 +00001817APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001818 APValue Result, RHS;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001820 if (!EvaluateComplex(E->getLHS(), Result, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001821 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001823 if (!EvaluateComplex(E->getRHS(), RHS, Info))
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001824 return APValue();
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001825
Daniel Dunbar3f279872009-01-29 01:32:56 +00001826 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
1827 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001828 switch (E->getOpcode()) {
1829 default: return APValue();
1830 case BinaryOperator::Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001831 if (Result.isComplexFloat()) {
1832 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
1833 APFloat::rmNearestTiesToEven);
1834 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
1835 APFloat::rmNearestTiesToEven);
1836 } else {
1837 Result.getComplexIntReal() += RHS.getComplexIntReal();
1838 Result.getComplexIntImag() += RHS.getComplexIntImag();
1839 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001840 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001841 case BinaryOperator::Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001842 if (Result.isComplexFloat()) {
1843 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
1844 APFloat::rmNearestTiesToEven);
1845 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
1846 APFloat::rmNearestTiesToEven);
1847 } else {
1848 Result.getComplexIntReal() -= RHS.getComplexIntReal();
1849 Result.getComplexIntImag() -= RHS.getComplexIntImag();
1850 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00001851 break;
1852 case BinaryOperator::Mul:
1853 if (Result.isComplexFloat()) {
1854 APValue LHS = Result;
1855 APFloat &LHS_r = LHS.getComplexFloatReal();
1856 APFloat &LHS_i = LHS.getComplexFloatImag();
1857 APFloat &RHS_r = RHS.getComplexFloatReal();
1858 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Daniel Dunbar3f279872009-01-29 01:32:56 +00001860 APFloat Tmp = LHS_r;
1861 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1862 Result.getComplexFloatReal() = Tmp;
1863 Tmp = LHS_i;
1864 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1865 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
1866
1867 Tmp = LHS_r;
1868 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
1869 Result.getComplexFloatImag() = Tmp;
1870 Tmp = LHS_i;
1871 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
1872 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
1873 } else {
1874 APValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001875 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001876 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
1877 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00001878 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00001879 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
1880 LHS.getComplexIntImag() * RHS.getComplexIntReal());
1881 }
1882 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00001883 }
1884
1885 return Result;
1886}
1887
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00001888//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001889// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001890//===----------------------------------------------------------------------===//
1891
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001892/// Evaluate - Return true if this is a constant which we can fold using
Chris Lattner019f4e82008-10-06 05:28:25 +00001893/// any crazy technique (that has nothing to do with language standards) that
1894/// we want to. If this function returns true, it returns the folded constant
1895/// in Result.
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001896bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
1897 EvalInfo Info(Ctx, Result);
Anders Carlsson54da0492008-11-30 16:38:33 +00001898
Nate Begeman59b5da62009-01-18 03:20:47 +00001899 if (getType()->isVectorType()) {
1900 if (!EvaluateVector(this, Result.Val, Info))
1901 return false;
1902 } else if (getType()->isIntegerType()) {
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001903 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001904 return false;
Daniel Dunbar89588912009-02-26 20:52:22 +00001905 } else if (getType()->hasPointerRepresentation()) {
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001906 if (!EvaluatePointer(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001907 return false;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001908 } else if (getType()->isRealFloatingType()) {
1909 llvm::APFloat f(0.0);
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001910 if (!EvaluateFloat(this, f, Info))
1911 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001913 Result.Val = APValue(f);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001914 } else if (getType()->isAnyComplexType()) {
1915 if (!EvaluateComplex(this, Result.Val, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001916 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00001917 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00001918 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00001919
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00001920 return true;
1921}
1922
Mike Stump660e6f72009-10-26 23:05:19 +00001923bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
1924 EvalInfo Info(Ctx, Result, true);
1925
1926 if (getType()->isVectorType()) {
1927 if (!EvaluateVector(this, Result.Val, Info))
1928 return false;
1929 } else if (getType()->isIntegerType()) {
1930 if (!IntExprEvaluator(Info, Result.Val).Visit(const_cast<Expr*>(this)))
1931 return false;
1932 } else if (getType()->hasPointerRepresentation()) {
1933 if (!EvaluatePointer(this, Result.Val, Info))
1934 return false;
1935 } else if (getType()->isRealFloatingType()) {
1936 llvm::APFloat f(0.0);
1937 if (!EvaluateFloat(this, f, Info))
1938 return false;
1939
1940 Result.Val = APValue(f);
1941 } else if (getType()->isAnyComplexType()) {
1942 if (!EvaluateComplex(this, Result.Val, Info))
1943 return false;
1944 } else
1945 return false;
1946
1947 return true;
1948}
1949
Anders Carlsson1b782762009-04-10 04:54:13 +00001950bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
1951 EvalInfo Info(Ctx, Result);
1952
1953 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1954}
1955
Eli Friedmanb2f295c2009-09-13 10:17:44 +00001956bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
1957 EvalInfo Info(Ctx, Result, true);
1958
1959 return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
1960}
1961
Chris Lattner6ee7aa12008-11-16 21:24:15 +00001962/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001963/// folded, but discard the result.
1964bool Expr::isEvaluatable(ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001965 EvalResult Result;
1966 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00001967}
Anders Carlsson51fe9962008-11-22 21:04:56 +00001968
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001969bool Expr::HasSideEffects(ASTContext &Ctx) const {
1970 Expr::EvalResult Result;
1971 EvalInfo Info(Ctx, Result);
1972 return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
1973}
1974
Anders Carlsson51fe9962008-11-22 21:04:56 +00001975APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001976 EvalResult EvalResult;
1977 bool Result = Evaluate(EvalResult, Ctx);
Daniel Dunbarf1853192009-01-15 18:32:35 +00001978 Result = Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00001979 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001980 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00001981
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00001982 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00001983}