blob: 06c5645afb3f64ba678198470cc25ac2e223bbc6 [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"
Ken Dyck199c3d62010-01-11 17:06:35 +000016#include "clang/AST/CharUnits.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000017#include "clang/AST/RecordLayout.h"
Seo Sanghyeon0fe52e12008-07-08 07:23:12 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000019#include "clang/AST/TypeLoc.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor8ecdb652010-04-28 22:16:22 +000021#include "clang/AST/Expr.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Anders Carlsson06a36752008-07-08 05:49:43 +000023#include "clang/Basic/TargetInfo.h"
Mike Stump7462b392009-05-30 14:43:18 +000024#include "llvm/ADT/SmallString.h"
Mike Stump4572bab2009-05-30 03:56:50 +000025#include <cstring>
26
Anders Carlssonc44eec62008-07-03 04:20:39 +000027using namespace clang;
Chris Lattnerf5eeb052008-07-11 18:11:29 +000028using llvm::APSInt;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +000029using llvm::APFloat;
Anders Carlssonc44eec62008-07-03 04:20:39 +000030
Chris Lattner87eae5e2008-07-11 22:52:41 +000031/// EvalInfo - This is a private struct used by the evaluator to capture
32/// information about a subexpression as it is folded. It retains information
33/// about the AST context, but also maintains information about the folded
34/// expression.
35///
36/// If an expression could be evaluated, it is still possible it is not a C
37/// "integer constant expression" or constant expression. If not, this struct
38/// captures information about how and why not.
39///
40/// One bit of information passed *into* the request for constant folding
41/// indicates whether the subexpression is "evaluated" or not according to C
42/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
43/// evaluate the expression regardless of what the RHS is, but C only allows
44/// certain things in certain situations.
John McCallf4cf1a12010-05-07 17:22:02 +000045namespace {
Benjamin Kramerc54061a2011-03-04 13:12:48 +000046 struct EvalInfo {
47 const ASTContext &Ctx;
48
49 /// EvalResult - Contains information about the evaluation.
50 Expr::EvalResult &EvalResult;
51
52 typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy;
53 MapTy OpaqueValues;
54 const APValue *getOpaqueValue(const OpaqueValueExpr *e) const {
55 MapTy::const_iterator i = OpaqueValues.find(e);
56 if (i == OpaqueValues.end()) return 0;
57 return &i->second;
58 }
59
60 EvalInfo(const ASTContext &ctx, Expr::EvalResult &evalresult)
61 : Ctx(ctx), EvalResult(evalresult) {}
62 };
63
John McCallf4cf1a12010-05-07 17:22:02 +000064 struct ComplexValue {
65 private:
66 bool IsInt;
67
68 public:
69 APSInt IntReal, IntImag;
70 APFloat FloatReal, FloatImag;
71
72 ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
73
74 void makeComplexFloat() { IsInt = false; }
75 bool isComplexFloat() const { return !IsInt; }
76 APFloat &getComplexFloatReal() { return FloatReal; }
77 APFloat &getComplexFloatImag() { return FloatImag; }
78
79 void makeComplexInt() { IsInt = true; }
80 bool isComplexInt() const { return IsInt; }
81 APSInt &getComplexIntReal() { return IntReal; }
82 APSInt &getComplexIntImag() { return IntImag; }
83
John McCall56ca35d2011-02-17 10:25:35 +000084 void moveInto(APValue &v) const {
John McCallf4cf1a12010-05-07 17:22:02 +000085 if (isComplexFloat())
86 v = APValue(FloatReal, FloatImag);
87 else
88 v = APValue(IntReal, IntImag);
89 }
John McCall56ca35d2011-02-17 10:25:35 +000090 void setFrom(const APValue &v) {
91 assert(v.isComplexFloat() || v.isComplexInt());
92 if (v.isComplexFloat()) {
93 makeComplexFloat();
94 FloatReal = v.getComplexFloatReal();
95 FloatImag = v.getComplexFloatImag();
96 } else {
97 makeComplexInt();
98 IntReal = v.getComplexIntReal();
99 IntImag = v.getComplexIntImag();
100 }
101 }
John McCallf4cf1a12010-05-07 17:22:02 +0000102 };
John McCallefdb83e2010-05-07 21:00:08 +0000103
104 struct LValue {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000105 const Expr *Base;
John McCallefdb83e2010-05-07 21:00:08 +0000106 CharUnits Offset;
107
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000108 const Expr *getLValueBase() { return Base; }
John McCallefdb83e2010-05-07 21:00:08 +0000109 CharUnits getLValueOffset() { return Offset; }
110
John McCall56ca35d2011-02-17 10:25:35 +0000111 void moveInto(APValue &v) const {
John McCallefdb83e2010-05-07 21:00:08 +0000112 v = APValue(Base, Offset);
113 }
John McCall56ca35d2011-02-17 10:25:35 +0000114 void setFrom(const APValue &v) {
115 assert(v.isLValue());
116 Base = v.getLValueBase();
117 Offset = v.getLValueOffset();
118 }
John McCallefdb83e2010-05-07 21:00:08 +0000119 };
John McCallf4cf1a12010-05-07 17:22:02 +0000120}
Chris Lattner87eae5e2008-07-11 22:52:41 +0000121
John McCall56ca35d2011-02-17 10:25:35 +0000122static bool Evaluate(EvalInfo &info, const Expr *E);
John McCallefdb83e2010-05-07 21:00:08 +0000123static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
124static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
Chris Lattner87eae5e2008-07-11 22:52:41 +0000125static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
Chris Lattnerd9becd12009-10-28 23:59:40 +0000126static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
127 EvalInfo &Info);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +0000128static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
John McCallf4cf1a12010-05-07 17:22:02 +0000129static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000130
131//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000132// Misc utilities
133//===----------------------------------------------------------------------===//
134
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000135static bool IsGlobalLValue(const Expr* E) {
John McCall42c8f872010-05-10 23:27:23 +0000136 if (!E) return true;
137
138 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
139 if (isa<FunctionDecl>(DRE->getDecl()))
140 return true;
141 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
142 return VD->hasGlobalStorage();
143 return false;
144 }
145
146 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
147 return CLE->isFileScope();
148
149 return true;
150}
151
John McCallefdb83e2010-05-07 21:00:08 +0000152static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
153 const Expr* Base = Value.Base;
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000154
John McCall35542832010-05-07 21:34:32 +0000155 // A null base expression indicates a null pointer. These are always
156 // evaluatable, and they are false unless the offset is zero.
157 if (!Base) {
158 Result = !Value.Offset.isZero();
159 return true;
160 }
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000161
John McCall42c8f872010-05-10 23:27:23 +0000162 // Require the base expression to be a global l-value.
Abramo Bagnarae17a6432010-05-14 17:07:14 +0000163 if (!IsGlobalLValue(Base)) return false;
John McCall42c8f872010-05-10 23:27:23 +0000164
John McCall35542832010-05-07 21:34:32 +0000165 // We have a non-null base expression. These are generally known to
166 // be true, but if it'a decl-ref to a weak symbol it can be null at
167 // runtime.
John McCall35542832010-05-07 21:34:32 +0000168 Result = true;
169
170 const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000171 if (!DeclRef)
172 return true;
173
John McCall35542832010-05-07 21:34:32 +0000174 // If it's a weak symbol, it isn't constant-evaluable.
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000175 const ValueDecl* Decl = DeclRef->getDecl();
176 if (Decl->hasAttr<WeakAttr>() ||
177 Decl->hasAttr<WeakRefAttr>() ||
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000178 Decl->isWeakImported())
Rafael Espindolaa7d3c042010-05-07 15:18:43 +0000179 return false;
180
Eli Friedman5bc86102009-06-14 02:17:33 +0000181 return true;
182}
183
John McCallcd7a4452010-01-05 23:42:56 +0000184static bool HandleConversionToBool(const Expr* E, bool& Result,
185 EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000186 if (E->getType()->isIntegralOrEnumerationType()) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000187 APSInt IntResult;
188 if (!EvaluateInteger(E, IntResult, Info))
189 return false;
190 Result = IntResult != 0;
191 return true;
192 } else if (E->getType()->isRealFloatingType()) {
193 APFloat FloatResult(0.0);
194 if (!EvaluateFloat(E, FloatResult, Info))
195 return false;
196 Result = !FloatResult.isZero();
197 return true;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000198 } else if (E->getType()->hasPointerRepresentation()) {
John McCallefdb83e2010-05-07 21:00:08 +0000199 LValue PointerResult;
Eli Friedman4efaa272008-11-12 09:44:48 +0000200 if (!EvaluatePointer(E, PointerResult, Info))
201 return false;
Eli Friedman5bc86102009-06-14 02:17:33 +0000202 return EvalPointerValueAsBool(PointerResult, Result);
Eli Friedmana1f47c42009-03-23 04:38:34 +0000203 } else if (E->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +0000204 ComplexValue ComplexResult;
Eli Friedmana1f47c42009-03-23 04:38:34 +0000205 if (!EvaluateComplex(E, ComplexResult, Info))
206 return false;
207 if (ComplexResult.isComplexFloat()) {
208 Result = !ComplexResult.getComplexFloatReal().isZero() ||
209 !ComplexResult.getComplexFloatImag().isZero();
210 } else {
211 Result = ComplexResult.getComplexIntReal().getBoolValue() ||
212 ComplexResult.getComplexIntImag().getBoolValue();
213 }
214 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000215 }
216
217 return false;
218}
219
Mike Stump1eb44332009-09-09 15:08:12 +0000220static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000221 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000222 unsigned DestWidth = Ctx.getIntWidth(DestType);
223 // Determine whether we are converting to unsigned or signed.
Douglas Gregor575a1c92011-05-20 16:38:50 +0000224 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000226 // FIXME: Warning for overflow.
227 uint64_t Space[4];
228 bool ignored;
229 (void)Value.convertToInteger(Space, DestWidth, DestSigned,
230 llvm::APFloat::rmTowardZero, &ignored);
231 return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
232}
233
Mike Stump1eb44332009-09-09 15:08:12 +0000234static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000235 APFloat &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000236 bool ignored;
237 APFloat Result = Value;
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Result.convert(Ctx.getFloatTypeSemantics(DestType),
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000239 APFloat::rmNearestTiesToEven, &ignored);
240 return Result;
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000244 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000245 unsigned DestWidth = Ctx.getIntWidth(DestType);
246 APSInt Result = Value;
247 // Figure out if this is a truncate, extend or noop cast.
248 // If the input is signed, do a sign extend, noop, or truncate.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000249 Result = Result.extOrTrunc(DestWidth);
Douglas Gregor575a1c92011-05-20 16:38:50 +0000250 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000251 return Result;
252}
253
Mike Stump1eb44332009-09-09 15:08:12 +0000254static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
Jay Foad4ba2a172011-01-12 09:06:06 +0000255 APSInt &Value, const ASTContext &Ctx) {
Daniel Dunbara2cfd342009-01-29 06:16:07 +0000256
257 APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
258 Result.convertFromAPInt(Value, Value.isSigned(),
259 APFloat::rmNearestTiesToEven);
260 return Result;
261}
262
Mike Stumpc4c90452009-10-27 22:09:17 +0000263namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000264class HasSideEffect
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000265 : public ConstStmtVisitor<HasSideEffect, bool> {
Mike Stumpc4c90452009-10-27 22:09:17 +0000266 EvalInfo &Info;
267public:
268
269 HasSideEffect(EvalInfo &info) : Info(info) {}
270
271 // Unhandled nodes conservatively default to having side effects.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000272 bool VisitStmt(const Stmt *S) {
Mike Stumpc4c90452009-10-27 22:09:17 +0000273 return true;
274 }
275
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000276 bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
277 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
Peter Collingbournef111d932011-04-15 00:35:48 +0000278 return Visit(E->getResultExpr());
279 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000280 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000281 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000282 return true;
283 return false;
284 }
285 // We don't want to evaluate BlockExprs multiple times, as they generate
286 // a ton of code.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000287 bool VisitBlockExpr(const BlockExpr *E) { return true; }
288 bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
289 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
Mike Stumpc4c90452009-10-27 22:09:17 +0000290 { return Visit(E->getInitializer()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000291 bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
292 bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
293 bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
294 bool VisitStringLiteral(const StringLiteral *E) { return false; }
295 bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
296 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000297 { return false; }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000298 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
Mike Stump980ca222009-10-29 20:48:09 +0000299 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000300 bool VisitChooseExpr(const ChooseExpr *E)
Mike Stumpc4c90452009-10-27 22:09:17 +0000301 { return Visit(E->getChosenSubExpr(Info.Ctx)); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000302 bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
303 bool VisitBinAssign(const BinaryOperator *E) { return true; }
304 bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
305 bool VisitBinaryOperator(const BinaryOperator *E)
Mike Stump980ca222009-10-29 20:48:09 +0000306 { return Visit(E->getLHS()) || Visit(E->getRHS()); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000307 bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
308 bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
309 bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
310 bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
311 bool VisitUnaryDeref(const UnaryOperator *E) {
Mike Stumpdf317bf2009-11-03 23:25:48 +0000312 if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
Mike Stumpc4c90452009-10-27 22:09:17 +0000313 return true;
Mike Stump980ca222009-10-29 20:48:09 +0000314 return Visit(E->getSubExpr());
Mike Stumpc4c90452009-10-27 22:09:17 +0000315 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000316 bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
Chris Lattner363ff232010-04-13 17:34:23 +0000317
318 // Has side effects if any element does.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000319 bool VisitInitListExpr(const InitListExpr *E) {
Chris Lattner363ff232010-04-13 17:34:23 +0000320 for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
321 if (Visit(E->getInit(i))) return true;
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000322 if (const Expr *filler = E->getArrayFiller())
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +0000323 return Visit(filler);
Chris Lattner363ff232010-04-13 17:34:23 +0000324 return false;
325 }
Douglas Gregoree8aff02011-01-04 17:33:58 +0000326
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000327 bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
Mike Stumpc4c90452009-10-27 22:09:17 +0000328};
329
John McCall56ca35d2011-02-17 10:25:35 +0000330class OpaqueValueEvaluation {
331 EvalInfo &info;
332 OpaqueValueExpr *opaqueValue;
333
334public:
335 OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
336 Expr *value)
337 : info(info), opaqueValue(opaqueValue) {
338
339 // If evaluation fails, fail immediately.
340 if (!Evaluate(info, value)) {
341 this->opaqueValue = 0;
342 return;
343 }
344 info.OpaqueValues[opaqueValue] = info.EvalResult.Val;
345 }
346
347 bool hasError() const { return opaqueValue == 0; }
348
349 ~OpaqueValueEvaluation() {
350 if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
351 }
352};
353
Mike Stumpc4c90452009-10-27 22:09:17 +0000354} // end anonymous namespace
355
Eli Friedman4efaa272008-11-12 09:44:48 +0000356//===----------------------------------------------------------------------===//
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000357// Generic Evaluation
358//===----------------------------------------------------------------------===//
359namespace {
360
361template <class Derived, typename RetTy=void>
362class ExprEvaluatorBase
363 : public ConstStmtVisitor<Derived, RetTy> {
364private:
365 RetTy DerivedSuccess(const APValue &V, const Expr *E) {
366 return static_cast<Derived*>(this)->Success(V, E);
367 }
368 RetTy DerivedError(const Expr *E) {
369 return static_cast<Derived*>(this)->Error(E);
370 }
371
372protected:
373 EvalInfo &Info;
374 typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
375 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
376
377public:
378 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
379
380 RetTy VisitStmt(const Stmt *) {
381 assert(0 && "Expression evaluator should not be called on stmts");
382 return DerivedError(0);
383 }
384 RetTy VisitExpr(const Expr *E) {
385 return DerivedError(E);
386 }
387
388 RetTy VisitParenExpr(const ParenExpr *E)
389 { return StmtVisitorTy::Visit(E->getSubExpr()); }
390 RetTy VisitUnaryExtension(const UnaryOperator *E)
391 { return StmtVisitorTy::Visit(E->getSubExpr()); }
392 RetTy VisitUnaryPlus(const UnaryOperator *E)
393 { return StmtVisitorTy::Visit(E->getSubExpr()); }
394 RetTy VisitChooseExpr(const ChooseExpr *E)
395 { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
396 RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
397 { return StmtVisitorTy::Visit(E->getResultExpr()); }
398
399 RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
400 OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());
401 if (opaque.hasError())
402 return DerivedError(E);
403
404 bool cond;
405 if (!HandleConversionToBool(E->getCond(), cond, Info))
406 return DerivedError(E);
407
408 return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr());
409 }
410
411 RetTy VisitConditionalOperator(const ConditionalOperator *E) {
412 bool BoolResult;
413 if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
414 return DerivedError(E);
415
416 Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
417 return StmtVisitorTy::Visit(EvalExpr);
418 }
419
420 RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
421 const APValue *value = Info.getOpaqueValue(E);
422 if (!value)
423 return (E->getSourceExpr() ? StmtVisitorTy::Visit(E->getSourceExpr())
424 : DerivedError(E));
425 return DerivedSuccess(*value, E);
426 }
427};
428
429}
430
431//===----------------------------------------------------------------------===//
Eli Friedman4efaa272008-11-12 09:44:48 +0000432// LValue Evaluation
433//===----------------------------------------------------------------------===//
434namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000435class LValueExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000436 : public ExprEvaluatorBase<LValueExprEvaluator, bool> {
John McCallefdb83e2010-05-07 21:00:08 +0000437 LValue &Result;
438
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000439 bool Success(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000440 Result.Base = E;
441 Result.Offset = CharUnits::Zero();
442 return true;
443 }
Eli Friedman4efaa272008-11-12 09:44:48 +0000444public:
Mike Stump1eb44332009-09-09 15:08:12 +0000445
John McCallefdb83e2010-05-07 21:00:08 +0000446 LValueExprEvaluator(EvalInfo &info, LValue &Result) :
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000447 ExprEvaluatorBaseTy(info), Result(Result) {}
Eli Friedman4efaa272008-11-12 09:44:48 +0000448
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000449 bool Success(const APValue &V, const Expr *E) {
450 Result.setFrom(V);
451 return true;
452 }
453 bool Error(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000454 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000455 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000456
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000457 bool VisitDeclRefExpr(const DeclRefExpr *E);
458 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
459 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
460 bool VisitMemberExpr(const MemberExpr *E);
461 bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
462 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
463 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
464 bool VisitUnaryDeref(const UnaryOperator *E);
Anders Carlsson26bc2202009-10-03 16:30:22 +0000465
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000466 bool VisitCastExpr(const CastExpr *E) {
Anders Carlsson26bc2202009-10-03 16:30:22 +0000467 switch (E->getCastKind()) {
468 default:
John McCallefdb83e2010-05-07 21:00:08 +0000469 return false;
Anders Carlsson26bc2202009-10-03 16:30:22 +0000470
John McCall2de56d12010-08-25 11:45:40 +0000471 case CK_NoOp:
Anders Carlsson26bc2202009-10-03 16:30:22 +0000472 return Visit(E->getSubExpr());
473 }
474 }
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000475 // FIXME: Missing: __real__, __imag__
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000476
Eli Friedman4efaa272008-11-12 09:44:48 +0000477};
478} // end anonymous namespace
479
John McCallefdb83e2010-05-07 21:00:08 +0000480static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000481 return LValueExprEvaluator(Info, Result).Visit(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000482}
483
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000484bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000485 if (isa<FunctionDecl>(E->getDecl())) {
John McCallefdb83e2010-05-07 21:00:08 +0000486 return Success(E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000487 } else if (const VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
Eli Friedman50c39ea2009-05-27 06:04:58 +0000488 if (!VD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000489 return Success(E);
Chandler Carruth761c94e2010-05-16 09:32:51 +0000490 // Reference parameters can refer to anything even if they have an
491 // "initializer" in the form of a default argument.
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000492 if (!isa<ParmVarDecl>(VD))
493 // FIXME: Check whether VD might be overridden!
494 if (const Expr *Init = VD->getAnyInitializer())
495 return Visit(Init);
Eli Friedman50c39ea2009-05-27 06:04:58 +0000496 }
497
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000498 return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
Anders Carlsson35873c42008-11-24 04:41:22 +0000499}
500
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000501bool
502LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000503 return Success(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000504}
505
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000506bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +0000507 QualType Ty;
508 if (E->isArrow()) {
John McCallefdb83e2010-05-07 21:00:08 +0000509 if (!EvaluatePointer(E->getBase(), Result, Info))
510 return false;
Ted Kremenek6217b802009-07-29 21:53:49 +0000511 Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
Eli Friedman4efaa272008-11-12 09:44:48 +0000512 } else {
John McCallefdb83e2010-05-07 21:00:08 +0000513 if (!Visit(E->getBase()))
514 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +0000515 Ty = E->getBase()->getType();
516 }
517
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000518 const RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
Eli Friedman4efaa272008-11-12 09:44:48 +0000519 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +0000520
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000521 const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Douglas Gregor86f19402008-12-20 23:49:58 +0000522 if (!FD) // FIXME: deal with other kinds of member expressions
John McCallefdb83e2010-05-07 21:00:08 +0000523 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000524
525 if (FD->getType()->isReferenceType())
John McCallefdb83e2010-05-07 21:00:08 +0000526 return false;
Eli Friedman2be58612009-05-30 21:09:44 +0000527
Eli Friedman4efaa272008-11-12 09:44:48 +0000528 // FIXME: This is linear time.
Douglas Gregor44b43212008-12-11 16:49:14 +0000529 unsigned i = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000530 for (RecordDecl::field_iterator Field = RD->field_begin(),
531 FieldEnd = RD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000532 Field != FieldEnd; (void)++Field, ++i) {
533 if (*Field == FD)
Eli Friedman4efaa272008-11-12 09:44:48 +0000534 break;
535 }
536
Ken Dyckfb1e3bc2011-01-18 01:56:16 +0000537 Result.Offset += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
John McCallefdb83e2010-05-07 21:00:08 +0000538 return true;
Eli Friedman4efaa272008-11-12 09:44:48 +0000539}
540
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000541bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Anders Carlsson3068d112008-11-16 19:01:22 +0000542 if (!EvaluatePointer(E->getBase(), Result, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000543 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Anders Carlsson3068d112008-11-16 19:01:22 +0000545 APSInt Index;
546 if (!EvaluateInteger(E->getIdx(), Index, Info))
John McCallefdb83e2010-05-07 21:00:08 +0000547 return false;
Anders Carlsson3068d112008-11-16 19:01:22 +0000548
Ken Dyck199c3d62010-01-11 17:06:35 +0000549 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
John McCallefdb83e2010-05-07 21:00:08 +0000550 Result.Offset += Index.getSExtValue() * ElementSize;
551 return true;
Anders Carlsson3068d112008-11-16 19:01:22 +0000552}
Eli Friedman4efaa272008-11-12 09:44:48 +0000553
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000554bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000555 return EvaluatePointer(E->getSubExpr(), Result, Info);
Eli Friedmane8761c82009-02-20 01:57:15 +0000556}
557
Eli Friedman4efaa272008-11-12 09:44:48 +0000558//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000559// Pointer Evaluation
560//===----------------------------------------------------------------------===//
561
Anders Carlssonc754aa62008-07-08 05:13:58 +0000562namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000563class PointerExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000564 : public ExprEvaluatorBase<PointerExprEvaluator, bool> {
John McCallefdb83e2010-05-07 21:00:08 +0000565 LValue &Result;
566
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000567 bool Success(const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +0000568 Result.Base = E;
569 Result.Offset = CharUnits::Zero();
570 return true;
571 }
Anders Carlsson2bad1682008-07-08 14:30:00 +0000572public:
Mike Stump1eb44332009-09-09 15:08:12 +0000573
John McCallefdb83e2010-05-07 21:00:08 +0000574 PointerExprEvaluator(EvalInfo &info, LValue &Result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000575 : ExprEvaluatorBaseTy(info), Result(Result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000576
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000577 bool Success(const APValue &V, const Expr *E) {
578 Result.setFrom(V);
579 return true;
580 }
581 bool Error(const Stmt *S) {
John McCallefdb83e2010-05-07 21:00:08 +0000582 return false;
Anders Carlsson2bad1682008-07-08 14:30:00 +0000583 }
584
John McCallefdb83e2010-05-07 21:00:08 +0000585 bool VisitBinaryOperator(const BinaryOperator *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000586 bool VisitCastExpr(const CastExpr* E);
John McCallefdb83e2010-05-07 21:00:08 +0000587 bool VisitUnaryAddrOf(const UnaryOperator *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000588 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
John McCallefdb83e2010-05-07 21:00:08 +0000589 { return Success(E); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000590 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000591 { return Success(E); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000592 bool VisitCallExpr(const CallExpr *E);
593 bool VisitBlockExpr(const BlockExpr *E) {
John McCall469a1eb2011-02-02 13:00:07 +0000594 if (!E->getBlockDecl()->hasCaptures())
John McCallefdb83e2010-05-07 21:00:08 +0000595 return Success(E);
596 return false;
Mike Stumpb83d2872009-02-19 22:01:56 +0000597 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000598 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000599 { return Success((Expr*)0); }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000600 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
John McCallefdb83e2010-05-07 21:00:08 +0000601 { return Success((Expr*)0); }
John McCall56ca35d2011-02-17 10:25:35 +0000602
Eli Friedmanba98d6b2009-03-23 04:56:01 +0000603 // FIXME: Missing: @protocol, @selector
Anders Carlsson650c92f2008-07-08 15:34:11 +0000604};
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000605} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +0000606
John McCallefdb83e2010-05-07 21:00:08 +0000607static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +0000608 assert(E->getType()->hasPointerRepresentation());
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000609 return PointerExprEvaluator(Info, Result).Visit(E);
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000610}
611
John McCallefdb83e2010-05-07 21:00:08 +0000612bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000613 if (E->getOpcode() != BO_Add &&
614 E->getOpcode() != BO_Sub)
John McCallefdb83e2010-05-07 21:00:08 +0000615 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000617 const Expr *PExp = E->getLHS();
618 const Expr *IExp = E->getRHS();
619 if (IExp->getType()->isPointerType())
620 std::swap(PExp, IExp);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
John McCallefdb83e2010-05-07 21:00:08 +0000622 if (!EvaluatePointer(PExp, Result, Info))
623 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
John McCallefdb83e2010-05-07 21:00:08 +0000625 llvm::APSInt Offset;
626 if (!EvaluateInteger(IExp, Offset, Info))
627 return false;
628 int64_t AdditionalOffset
629 = Offset.isSigned() ? Offset.getSExtValue()
630 : static_cast<int64_t>(Offset.getZExtValue());
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000631
Daniel Dunbare0cdb4e2010-03-20 05:53:45 +0000632 // Compute the new offset in the appropriate width.
633
634 QualType PointeeType =
635 PExp->getType()->getAs<PointerType>()->getPointeeType();
John McCallefdb83e2010-05-07 21:00:08 +0000636 CharUnits SizeOfPointee;
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000638 // Explicitly handle GNU void* and function pointer arithmetic extensions.
639 if (PointeeType->isVoidType() || PointeeType->isFunctionType())
John McCallefdb83e2010-05-07 21:00:08 +0000640 SizeOfPointee = CharUnits::One();
Anders Carlsson4d4c50d2009-02-19 04:55:58 +0000641 else
John McCallefdb83e2010-05-07 21:00:08 +0000642 SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
Eli Friedman4efaa272008-11-12 09:44:48 +0000643
John McCall2de56d12010-08-25 11:45:40 +0000644 if (E->getOpcode() == BO_Add)
John McCallefdb83e2010-05-07 21:00:08 +0000645 Result.Offset += AdditionalOffset * SizeOfPointee;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000646 else
John McCallefdb83e2010-05-07 21:00:08 +0000647 Result.Offset -= AdditionalOffset * SizeOfPointee;
Eli Friedman4efaa272008-11-12 09:44:48 +0000648
John McCallefdb83e2010-05-07 21:00:08 +0000649 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000650}
Eli Friedman4efaa272008-11-12 09:44:48 +0000651
John McCallefdb83e2010-05-07 21:00:08 +0000652bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
653 return EvaluateLValue(E->getSubExpr(), Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000654}
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000656
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000657bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
658 const Expr* SubExpr = E->getSubExpr();
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000659
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000660 switch (E->getCastKind()) {
661 default:
662 break;
663
John McCall2de56d12010-08-25 11:45:40 +0000664 case CK_NoOp:
665 case CK_BitCast:
John McCall2de56d12010-08-25 11:45:40 +0000666 case CK_AnyPointerToObjCPointerCast:
667 case CK_AnyPointerToBlockPointerCast:
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000668 return Visit(SubExpr);
669
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000670 case CK_DerivedToBase:
671 case CK_UncheckedDerivedToBase: {
672 LValue BaseLV;
673 if (!EvaluatePointer(E->getSubExpr(), BaseLV, Info))
674 return false;
675
676 // Now figure out the necessary offset to add to the baseLV to get from
677 // the derived class to the base class.
Ken Dyck7c7f8202011-01-26 02:17:08 +0000678 CharUnits Offset = CharUnits::Zero();
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000679
680 QualType Ty = E->getSubExpr()->getType();
681 const CXXRecordDecl *DerivedDecl =
682 Ty->getAs<PointerType>()->getPointeeType()->getAsCXXRecordDecl();
683
684 for (CastExpr::path_const_iterator PathI = E->path_begin(),
685 PathE = E->path_end(); PathI != PathE; ++PathI) {
686 const CXXBaseSpecifier *Base = *PathI;
687
688 // FIXME: If the base is virtual, we'd need to determine the type of the
689 // most derived class and we don't support that right now.
690 if (Base->isVirtual())
691 return false;
692
693 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
694 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
695
Ken Dyck7c7f8202011-01-26 02:17:08 +0000696 Offset += Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000697 DerivedDecl = BaseDecl;
698 }
699
700 Result.Base = BaseLV.getLValueBase();
Ken Dyck7c7f8202011-01-26 02:17:08 +0000701 Result.Offset = BaseLV.getLValueOffset() + Offset;
Anders Carlsson5c5a7642010-10-31 20:41:46 +0000702 return true;
703 }
704
John McCall404cd162010-11-13 01:35:44 +0000705 case CK_NullToPointer: {
706 Result.Base = 0;
707 Result.Offset = CharUnits::Zero();
708 return true;
709 }
710
John McCall2de56d12010-08-25 11:45:40 +0000711 case CK_IntegralToPointer: {
John McCallefdb83e2010-05-07 21:00:08 +0000712 APValue Value;
713 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
Eli Friedman09a8a0e2009-12-27 05:43:15 +0000714 break;
Daniel Dunbar69ab26a2009-02-20 18:22:23 +0000715
John McCallefdb83e2010-05-07 21:00:08 +0000716 if (Value.isInt()) {
Jay Foad9f71a8f2010-12-07 08:25:34 +0000717 Value.getInt() = Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
John McCallefdb83e2010-05-07 21:00:08 +0000718 Result.Base = 0;
719 Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
720 return true;
721 } else {
722 // Cast is of an lvalue, no need to change value.
723 Result.Base = Value.getLValueBase();
724 Result.Offset = Value.getLValueOffset();
725 return true;
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000726 }
727 }
John McCall2de56d12010-08-25 11:45:40 +0000728 case CK_ArrayToPointerDecay:
729 case CK_FunctionToPointerDecay:
John McCallefdb83e2010-05-07 21:00:08 +0000730 return EvaluateLValue(SubExpr, Result, Info);
Eli Friedman4efaa272008-11-12 09:44:48 +0000731 }
732
John McCallefdb83e2010-05-07 21:00:08 +0000733 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000734}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000735
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000736bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000737 if (E->isBuiltinCall(Info.Ctx) ==
David Chisnall0d13f6f2010-01-23 02:40:42 +0000738 Builtin::BI__builtin___CFStringMakeConstantString ||
739 E->isBuiltinCall(Info.Ctx) ==
740 Builtin::BI__builtin___NSStringMakeConstantString)
John McCallefdb83e2010-05-07 21:00:08 +0000741 return Success(E);
Eli Friedman3941b182009-01-25 01:54:01 +0000742
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000743 return ExprEvaluatorBaseTy::VisitCallExpr(E);
Eli Friedman4efaa272008-11-12 09:44:48 +0000744}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000745
746//===----------------------------------------------------------------------===//
Nate Begeman59b5da62009-01-18 03:20:47 +0000747// Vector Evaluation
748//===----------------------------------------------------------------------===//
749
750namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000751 class VectorExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000752 : public ExprEvaluatorBase<VectorExprEvaluator, APValue> {
Eli Friedman91110ee2009-02-23 04:23:56 +0000753 APValue GetZeroVector(QualType VecType);
Nate Begeman59b5da62009-01-18 03:20:47 +0000754 public:
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000756 VectorExprEvaluator(EvalInfo &info) : ExprEvaluatorBaseTy(info) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000758 APValue Success(const APValue &V, const Expr *E) { return V; }
759 APValue Error(const Expr *E) { return APValue(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Eli Friedman91110ee2009-02-23 04:23:56 +0000761 APValue VisitUnaryReal(const UnaryOperator *E)
762 { return Visit(E->getSubExpr()); }
763 APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
764 { return GetZeroVector(E->getType()); }
Nate Begeman59b5da62009-01-18 03:20:47 +0000765 APValue VisitCastExpr(const CastExpr* E);
766 APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
767 APValue VisitInitListExpr(const InitListExpr *E);
Eli Friedman91110ee2009-02-23 04:23:56 +0000768 APValue VisitUnaryImag(const UnaryOperator *E);
769 // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
Eli Friedman2217c872009-02-22 11:46:18 +0000770 // binary comparisons, binary and/or/xor,
Eli Friedman91110ee2009-02-23 04:23:56 +0000771 // shufflevector, ExtVectorElementExpr
772 // (Note that these require implementing conversions
773 // between vector types.)
Nate Begeman59b5da62009-01-18 03:20:47 +0000774 };
775} // end anonymous namespace
776
777static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
778 if (!E->getType()->isVectorType())
779 return false;
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000780 Result = VectorExprEvaluator(Info).Visit(E);
Nate Begeman59b5da62009-01-18 03:20:47 +0000781 return !Result.isUninit();
782}
783
784APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
John McCall183700f2009-09-21 23:43:11 +0000785 const VectorType *VTy = E->getType()->getAs<VectorType>();
Nate Begemanc0b8b192009-07-01 07:50:47 +0000786 QualType EltTy = VTy->getElementType();
787 unsigned NElts = VTy->getNumElements();
788 unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Nate Begeman59b5da62009-01-18 03:20:47 +0000790 const Expr* SE = E->getSubExpr();
Nate Begemane8c9e922009-06-26 18:22:18 +0000791 QualType SETy = SE->getType();
Nate Begeman59b5da62009-01-18 03:20:47 +0000792
Eli Friedman46a52322011-03-25 00:43:55 +0000793 switch (E->getCastKind()) {
794 case CK_VectorSplat: {
795 APValue Result = APValue();
796 if (SETy->isIntegerType()) {
797 APSInt IntResult;
798 if (!EvaluateInteger(SE, IntResult, Info))
799 return APValue();
800 Result = APValue(IntResult);
801 } else if (SETy->isRealFloatingType()) {
802 APFloat F(0.0);
803 if (!EvaluateFloat(SE, F, Info))
804 return APValue();
805 Result = APValue(F);
806 } else {
Anders Carlsson0254e702011-03-25 11:22:47 +0000807 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000808 }
Nate Begemanc0b8b192009-07-01 07:50:47 +0000809
810 // Splat and create vector APValue.
811 llvm::SmallVector<APValue, 4> Elts(NElts, Result);
812 return APValue(&Elts[0], Elts.size());
Nate Begemane8c9e922009-06-26 18:22:18 +0000813 }
Eli Friedman46a52322011-03-25 00:43:55 +0000814 case CK_BitCast: {
815 if (SETy->isVectorType())
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000816 return Visit(SE);
Nate Begemanc0b8b192009-07-01 07:50:47 +0000817
Eli Friedman46a52322011-03-25 00:43:55 +0000818 if (!SETy->isIntegerType())
Anders Carlsson0254e702011-03-25 11:22:47 +0000819 return APValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Eli Friedman46a52322011-03-25 00:43:55 +0000821 APSInt Init;
822 if (!EvaluateInteger(SE, Init, Info))
Nate Begemanc0b8b192009-07-01 07:50:47 +0000823 return APValue();
824
Eli Friedman46a52322011-03-25 00:43:55 +0000825 assert((EltTy->isIntegerType() || EltTy->isRealFloatingType()) &&
826 "Vectors must be composed of ints or floats");
827
828 llvm::SmallVector<APValue, 4> Elts;
829 for (unsigned i = 0; i != NElts; ++i) {
830 APSInt Tmp = Init.extOrTrunc(EltWidth);
831
832 if (EltTy->isIntegerType())
833 Elts.push_back(APValue(Tmp));
834 else
835 Elts.push_back(APValue(APFloat(Tmp)));
836
837 Init >>= EltWidth;
838 }
839 return APValue(&Elts[0], Elts.size());
Nate Begemanc0b8b192009-07-01 07:50:47 +0000840 }
Eli Friedman46a52322011-03-25 00:43:55 +0000841 case CK_LValueToRValue:
842 case CK_NoOp:
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000843 return Visit(SE);
Eli Friedman46a52322011-03-25 00:43:55 +0000844 default:
Anders Carlsson0254e702011-03-25 11:22:47 +0000845 return APValue();
Eli Friedman46a52322011-03-25 00:43:55 +0000846 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000847}
848
Mike Stump1eb44332009-09-09 15:08:12 +0000849APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000850VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000851 return this->Visit(E->getInitializer());
Nate Begeman59b5da62009-01-18 03:20:47 +0000852}
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854APValue
Nate Begeman59b5da62009-01-18 03:20:47 +0000855VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
John McCall183700f2009-09-21 23:43:11 +0000856 const VectorType *VT = E->getType()->getAs<VectorType>();
Nate Begeman59b5da62009-01-18 03:20:47 +0000857 unsigned NumInits = E->getNumInits();
Eli Friedman91110ee2009-02-23 04:23:56 +0000858 unsigned NumElements = VT->getNumElements();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Nate Begeman59b5da62009-01-18 03:20:47 +0000860 QualType EltTy = VT->getElementType();
861 llvm::SmallVector<APValue, 4> Elements;
862
John McCalla7d6c222010-06-11 17:54:15 +0000863 // If a vector is initialized with a single element, that value
864 // becomes every element of the vector, not just the first.
865 // This is the behavior described in the IBM AltiVec documentation.
866 if (NumInits == 1) {
Tanya Lattnerb92ae0e2011-04-15 22:42:59 +0000867
868 // Handle the case where the vector is initialized by a another
869 // vector (OpenCL 6.1.6).
870 if (E->getInit(0)->getType()->isVectorType())
871 return this->Visit(const_cast<Expr*>(E->getInit(0)));
872
John McCalla7d6c222010-06-11 17:54:15 +0000873 APValue InitValue;
Nate Begeman59b5da62009-01-18 03:20:47 +0000874 if (EltTy->isIntegerType()) {
875 llvm::APSInt sInt(32);
John McCalla7d6c222010-06-11 17:54:15 +0000876 if (!EvaluateInteger(E->getInit(0), sInt, Info))
877 return APValue();
878 InitValue = APValue(sInt);
Nate Begeman59b5da62009-01-18 03:20:47 +0000879 } else {
880 llvm::APFloat f(0.0);
John McCalla7d6c222010-06-11 17:54:15 +0000881 if (!EvaluateFloat(E->getInit(0), f, Info))
882 return APValue();
883 InitValue = APValue(f);
884 }
885 for (unsigned i = 0; i < NumElements; i++) {
886 Elements.push_back(InitValue);
887 }
888 } else {
889 for (unsigned i = 0; i < NumElements; i++) {
890 if (EltTy->isIntegerType()) {
891 llvm::APSInt sInt(32);
892 if (i < NumInits) {
893 if (!EvaluateInteger(E->getInit(i), sInt, Info))
894 return APValue();
895 } else {
896 sInt = Info.Ctx.MakeIntValue(0, EltTy);
897 }
898 Elements.push_back(APValue(sInt));
Eli Friedman91110ee2009-02-23 04:23:56 +0000899 } else {
John McCalla7d6c222010-06-11 17:54:15 +0000900 llvm::APFloat f(0.0);
901 if (i < NumInits) {
902 if (!EvaluateFloat(E->getInit(i), f, Info))
903 return APValue();
904 } else {
905 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
906 }
907 Elements.push_back(APValue(f));
Eli Friedman91110ee2009-02-23 04:23:56 +0000908 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000909 }
910 }
911 return APValue(&Elements[0], Elements.size());
912}
913
Mike Stump1eb44332009-09-09 15:08:12 +0000914APValue
Eli Friedman91110ee2009-02-23 04:23:56 +0000915VectorExprEvaluator::GetZeroVector(QualType T) {
John McCall183700f2009-09-21 23:43:11 +0000916 const VectorType *VT = T->getAs<VectorType>();
Eli Friedman91110ee2009-02-23 04:23:56 +0000917 QualType EltTy = VT->getElementType();
918 APValue ZeroElement;
919 if (EltTy->isIntegerType())
920 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
921 else
922 ZeroElement =
923 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
924
925 llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
926 return APValue(&Elements[0], Elements.size());
927}
928
Eli Friedman91110ee2009-02-23 04:23:56 +0000929APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
930 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
931 Info.EvalResult.HasSideEffects = true;
932 return GetZeroVector(E->getType());
933}
934
Nate Begeman59b5da62009-01-18 03:20:47 +0000935//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000936// Integer Evaluation
937//===----------------------------------------------------------------------===//
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000938
939namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +0000940class IntExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000941 : public ExprEvaluatorBase<IntExprEvaluator, bool> {
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000942 APValue &Result;
Anders Carlssonc754aa62008-07-08 05:13:58 +0000943public:
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000944 IntExprEvaluator(EvalInfo &info, APValue &result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000945 : ExprEvaluatorBaseTy(info), Result(result) {}
Chris Lattnerf5eeb052008-07-11 18:11:29 +0000946
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000947 bool Success(const llvm::APSInt &SI, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000948 assert(E->getType()->isIntegralOrEnumerationType() &&
949 "Invalid evaluation result.");
Douglas Gregor575a1c92011-05-20 16:38:50 +0000950 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000951 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000952 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000953 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000954 Result = APValue(SI);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000955 return true;
956 }
957
Daniel Dunbar131eb432009-02-19 09:06:44 +0000958 bool Success(const llvm::APInt &I, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000959 assert(E->getType()->isIntegralOrEnumerationType() &&
960 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000961 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
Daniel Dunbar3f7d9952009-02-19 18:37:50 +0000962 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000963 Result = APValue(APSInt(I));
Douglas Gregor575a1c92011-05-20 16:38:50 +0000964 Result.getInt().setIsUnsigned(
965 E->getType()->isUnsignedIntegerOrEnumerationType());
Daniel Dunbar131eb432009-02-19 09:06:44 +0000966 return true;
967 }
968
969 bool Success(uint64_t Value, const Expr *E) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000970 assert(E->getType()->isIntegralOrEnumerationType() &&
971 "Invalid evaluation result.");
Daniel Dunbar30c37f42009-02-19 20:17:33 +0000972 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
Daniel Dunbar131eb432009-02-19 09:06:44 +0000973 return true;
974 }
975
Ken Dyck4f3bc8f2011-03-11 02:13:43 +0000976 bool Success(CharUnits Size, const Expr *E) {
977 return Success(Size.getQuantity(), E);
978 }
979
980
Anders Carlsson82206e22008-11-30 18:14:57 +0000981 bool Error(SourceLocation L, diag::kind D, const Expr *E) {
Chris Lattner32fea9d2008-11-12 07:43:42 +0000982 // Take the first error.
Anders Carlsson54da0492008-11-30 16:38:33 +0000983 if (Info.EvalResult.Diag == 0) {
984 Info.EvalResult.DiagLoc = L;
985 Info.EvalResult.Diag = D;
Anders Carlsson82206e22008-11-30 18:14:57 +0000986 Info.EvalResult.DiagExpr = E;
Chris Lattner32fea9d2008-11-12 07:43:42 +0000987 }
Chris Lattner54176fd2008-07-12 00:14:42 +0000988 return false;
Chris Lattner7a767782008-07-11 19:24:49 +0000989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000991 bool Success(const APValue &V, const Expr *E) {
992 return Success(V.getInt(), E);
Chris Lattner32fea9d2008-11-12 07:43:42 +0000993 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000994 bool Error(const Expr *E) {
Anders Carlsson0e8acbb2008-11-30 18:37:00 +0000995 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Anders Carlssonc754aa62008-07-08 05:13:58 +0000996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Peter Collingbourne8cad3042011-05-13 03:29:01 +0000998 //===--------------------------------------------------------------------===//
999 // Visitor Methods
1000 //===--------------------------------------------------------------------===//
Anders Carlssonc754aa62008-07-08 05:13:58 +00001001
Chris Lattner4c4867e2008-07-12 00:38:25 +00001002 bool VisitIntegerLiteral(const IntegerLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001003 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001004 }
1005 bool VisitCharacterLiteral(const CharacterLiteral *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001006 return Success(E->getValue(), E);
Chris Lattner4c4867e2008-07-12 00:38:25 +00001007 }
Eli Friedman04309752009-11-24 05:28:59 +00001008
1009 bool CheckReferencedDecl(const Expr *E, const Decl *D);
1010 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001011 if (CheckReferencedDecl(E, E->getDecl()))
1012 return true;
1013
1014 return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
Eli Friedman04309752009-11-24 05:28:59 +00001015 }
1016 bool VisitMemberExpr(const MemberExpr *E) {
1017 if (CheckReferencedDecl(E, E->getMemberDecl())) {
1018 // Conservatively assume a MemberExpr will have side-effects
1019 Info.EvalResult.HasSideEffects = true;
1020 return true;
1021 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001022
1023 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
Eli Friedman04309752009-11-24 05:28:59 +00001024 }
1025
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001026 bool VisitCallExpr(const CallExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001027 bool VisitBinaryOperator(const BinaryOperator *E);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001028 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
Chris Lattnerb542afe2008-07-11 19:10:17 +00001029 bool VisitUnaryOperator(const UnaryOperator *E);
Anders Carlsson06a36752008-07-08 05:49:43 +00001030
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001031 bool VisitCastExpr(const CastExpr* E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001032 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
Sebastian Redl05189992008-11-11 17:56:53 +00001033
Anders Carlsson3068d112008-11-16 19:01:22 +00001034 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001035 return Success(E->getValue(), E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Anders Carlsson3f704562008-12-21 22:39:40 +00001038 bool VisitGNUNullExpr(const GNUNullExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001039 return Success(0, E);
Anders Carlsson3f704562008-12-21 22:39:40 +00001040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregored8abf12010-07-08 06:14:04 +00001042 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001043 return Success(0, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001044 }
1045
Eli Friedman664a1042009-02-27 04:45:43 +00001046 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
1047 return Success(0, E);
1048 }
1049
Sebastian Redl64b45f72009-01-05 20:52:13 +00001050 bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
Sebastian Redl0dfd8482010-09-13 20:56:31 +00001051 return Success(E->getValue(), E);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001052 }
1053
Francois Pichet6ad6f282010-12-07 00:08:36 +00001054 bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
1055 return Success(E->getValue(), E);
1056 }
1057
John Wiegley21ff2e52011-04-28 00:16:57 +00001058 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
1059 return Success(E->getValue(), E);
1060 }
1061
John Wiegley55262202011-04-25 06:54:41 +00001062 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
1063 return Success(E->getValue(), E);
1064 }
1065
Eli Friedman722c7172009-02-28 03:59:05 +00001066 bool VisitUnaryReal(const UnaryOperator *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001067 bool VisitUnaryImag(const UnaryOperator *E);
1068
Sebastian Redl295995c2010-09-10 20:55:47 +00001069 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
Douglas Gregoree8aff02011-01-04 17:33:58 +00001070 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
1071
Chris Lattnerfcee0012008-07-11 21:24:13 +00001072private:
Ken Dyck8b752f12010-01-27 17:10:57 +00001073 CharUnits GetAlignOfExpr(const Expr *E);
1074 CharUnits GetAlignOfType(QualType T);
John McCall42c8f872010-05-10 23:27:23 +00001075 static QualType GetObjectType(const Expr *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001076 bool TryEvaluateBuiltinObjectSize(const CallExpr *E);
Eli Friedman664a1042009-02-27 04:45:43 +00001077 // FIXME: Missing: array subscript of vector, member of vector
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001078};
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001079} // end anonymous namespace
Anders Carlsson650c92f2008-07-08 15:34:11 +00001080
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001081static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001082 assert(E->getType()->isIntegralOrEnumerationType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001083 return IntExprEvaluator(Info, Result).Visit(E);
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001084}
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001085
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001086static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001087 assert(E->getType()->isIntegralOrEnumerationType());
John McCall7db7acb2010-05-07 05:46:35 +00001088
Daniel Dunbar69ab26a2009-02-20 18:22:23 +00001089 APValue Val;
1090 if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
1091 return false;
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001092 Result = Val.getInt();
1093 return true;
Anders Carlsson650c92f2008-07-08 15:34:11 +00001094}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001095
Eli Friedman04309752009-11-24 05:28:59 +00001096bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001097 // Enums are integer constant exprs.
Eli Friedman29a7f332009-12-10 22:29:29 +00001098 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
1099 return Success(ECD->getInitVal(), E);
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001100
1101 // In C++, const, non-volatile integers initialized with ICEs are ICEs.
Eli Friedmane1646da2009-03-30 23:39:01 +00001102 // In C, they can also be folded, although they are not ICEs.
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001103 if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
1104 == Qualifiers::Const) {
Anders Carlssonf6b60252010-02-03 21:58:41 +00001105
1106 if (isa<ParmVarDecl>(D))
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001107 return false;
Anders Carlssonf6b60252010-02-03 21:58:41 +00001108
Eli Friedman04309752009-11-24 05:28:59 +00001109 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001110 if (const Expr *Init = VD->getAnyInitializer()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001111 if (APValue *V = VD->getEvaluatedValue()) {
1112 if (V->isInt())
1113 return Success(V->getInt(), E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001114 return false;
Eli Friedmanc0131182009-12-03 20:31:57 +00001115 }
1116
1117 if (VD->isEvaluatingValue())
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001118 return false;
Eli Friedmanc0131182009-12-03 20:31:57 +00001119
1120 VD->setEvaluatingValue();
1121
Eli Friedmana7dedf72010-09-06 00:10:32 +00001122 Expr::EvalResult EResult;
1123 if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
1124 EResult.Val.isInt()) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001125 // Cache the evaluated value in the variable declaration.
Eli Friedmana7dedf72010-09-06 00:10:32 +00001126 Result = EResult.Val;
Eli Friedmanc0131182009-12-03 20:31:57 +00001127 VD->setEvaluatedValue(Result);
Douglas Gregor78d15832009-05-26 18:54:04 +00001128 return true;
1129 }
1130
Eli Friedmanc0131182009-12-03 20:31:57 +00001131 VD->setEvaluatedValue(APValue());
Douglas Gregor78d15832009-05-26 18:54:04 +00001132 }
Sebastian Redlb2bc62b2009-02-08 15:51:17 +00001133 }
1134 }
1135
Chris Lattner4c4867e2008-07-12 00:38:25 +00001136 // Otherwise, random variable references are not constants.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001137 return false;
Chris Lattner4c4867e2008-07-12 00:38:25 +00001138}
1139
Chris Lattnera4d55d82008-10-06 06:40:35 +00001140/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
1141/// as GCC.
1142static int EvaluateBuiltinClassifyType(const CallExpr *E) {
1143 // The following enum mimics the values returned by GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001144 // FIXME: Does GCC differ between lvalue and rvalue references here?
Chris Lattnera4d55d82008-10-06 06:40:35 +00001145 enum gcc_type_class {
1146 no_type_class = -1,
1147 void_type_class, integer_type_class, char_type_class,
1148 enumeral_type_class, boolean_type_class,
1149 pointer_type_class, reference_type_class, offset_type_class,
1150 real_type_class, complex_type_class,
1151 function_type_class, method_type_class,
1152 record_type_class, union_type_class,
1153 array_type_class, string_type_class,
1154 lang_type_class
1155 };
Mike Stump1eb44332009-09-09 15:08:12 +00001156
1157 // If no argument was supplied, default to "no_type_class". This isn't
Chris Lattnera4d55d82008-10-06 06:40:35 +00001158 // ideal, however it is what gcc does.
1159 if (E->getNumArgs() == 0)
1160 return no_type_class;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattnera4d55d82008-10-06 06:40:35 +00001162 QualType ArgTy = E->getArg(0)->getType();
1163 if (ArgTy->isVoidType())
1164 return void_type_class;
1165 else if (ArgTy->isEnumeralType())
1166 return enumeral_type_class;
1167 else if (ArgTy->isBooleanType())
1168 return boolean_type_class;
1169 else if (ArgTy->isCharType())
1170 return string_type_class; // gcc doesn't appear to use char_type_class
1171 else if (ArgTy->isIntegerType())
1172 return integer_type_class;
1173 else if (ArgTy->isPointerType())
1174 return pointer_type_class;
1175 else if (ArgTy->isReferenceType())
1176 return reference_type_class;
1177 else if (ArgTy->isRealType())
1178 return real_type_class;
1179 else if (ArgTy->isComplexType())
1180 return complex_type_class;
1181 else if (ArgTy->isFunctionType())
1182 return function_type_class;
Douglas Gregorfb87b892010-04-26 21:31:17 +00001183 else if (ArgTy->isStructureOrClassType())
Chris Lattnera4d55d82008-10-06 06:40:35 +00001184 return record_type_class;
1185 else if (ArgTy->isUnionType())
1186 return union_type_class;
1187 else if (ArgTy->isArrayType())
1188 return array_type_class;
1189 else if (ArgTy->isUnionType())
1190 return union_type_class;
1191 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
1192 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
1193 return -1;
1194}
1195
John McCall42c8f872010-05-10 23:27:23 +00001196/// Retrieves the "underlying object type" of the given expression,
1197/// as used by __builtin_object_size.
1198QualType IntExprEvaluator::GetObjectType(const Expr *E) {
1199 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
1200 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
1201 return VD->getType();
1202 } else if (isa<CompoundLiteralExpr>(E)) {
1203 return E->getType();
1204 }
1205
1206 return QualType();
1207}
1208
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001209bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
John McCall42c8f872010-05-10 23:27:23 +00001210 // TODO: Perhaps we should let LLVM lower this?
1211 LValue Base;
1212 if (!EvaluatePointer(E->getArg(0), Base, Info))
1213 return false;
1214
1215 // If we can prove the base is null, lower to zero now.
1216 const Expr *LVBase = Base.getLValueBase();
1217 if (!LVBase) return Success(0, E);
1218
1219 QualType T = GetObjectType(LVBase);
1220 if (T.isNull() ||
1221 T->isIncompleteType() ||
Eli Friedman13578692010-08-05 02:49:48 +00001222 T->isFunctionType() ||
John McCall42c8f872010-05-10 23:27:23 +00001223 T->isVariablyModifiedType() ||
1224 T->isDependentType())
1225 return false;
1226
1227 CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
1228 CharUnits Offset = Base.getLValueOffset();
1229
1230 if (!Offset.isNegative() && Offset <= Size)
1231 Size -= Offset;
1232 else
1233 Size = CharUnits::Zero();
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001234 return Success(Size, E);
John McCall42c8f872010-05-10 23:27:23 +00001235}
1236
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001237bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001238 switch (E->isBuiltinCall(Info.Ctx)) {
Chris Lattner019f4e82008-10-06 05:28:25 +00001239 default:
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001240 return ExprEvaluatorBaseTy::VisitCallExpr(E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001241
1242 case Builtin::BI__builtin_object_size: {
John McCall42c8f872010-05-10 23:27:23 +00001243 if (TryEvaluateBuiltinObjectSize(E))
1244 return true;
Mike Stump64eda9e2009-10-26 18:35:08 +00001245
Eric Christopherb2aaf512010-01-19 22:58:35 +00001246 // If evaluating the argument has side-effects we can't determine
1247 // the size of the object and lower it to unknown now.
Fariborz Jahanian393c2472009-11-05 18:03:03 +00001248 if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
Benjamin Kramer3f27b382010-01-03 18:18:37 +00001249 if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
Chris Lattnercf184652009-11-03 19:48:51 +00001250 return Success(-1ULL, E);
Mike Stump64eda9e2009-10-26 18:35:08 +00001251 return Success(0, E);
1252 }
Mike Stumpc4c90452009-10-27 22:09:17 +00001253
Mike Stump64eda9e2009-10-26 18:35:08 +00001254 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
1255 }
1256
Chris Lattner019f4e82008-10-06 05:28:25 +00001257 case Builtin::BI__builtin_classify_type:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001258 return Success(EvaluateBuiltinClassifyType(E), E);
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001260 case Builtin::BI__builtin_constant_p:
Chris Lattner019f4e82008-10-06 05:28:25 +00001261 // __builtin_constant_p always has one operand: it returns true if that
1262 // operand can be folded, false otherwise.
Daniel Dunbar131eb432009-02-19 09:06:44 +00001263 return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
Chris Lattner21fb98e2009-09-23 06:06:36 +00001264
1265 case Builtin::BI__builtin_eh_return_data_regno: {
1266 int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
1267 Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
1268 return Success(Operand, E);
1269 }
Eli Friedmanc4a26382010-02-13 00:10:10 +00001270
1271 case Builtin::BI__builtin_expect:
1272 return Visit(E->getArg(0));
Douglas Gregor5726d402010-09-10 06:27:15 +00001273
1274 case Builtin::BIstrlen:
1275 case Builtin::BI__builtin_strlen:
1276 // As an extension, we support strlen() and __builtin_strlen() as constant
1277 // expressions when the argument is a string literal.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001278 if (const StringLiteral *S
Douglas Gregor5726d402010-09-10 06:27:15 +00001279 = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
1280 // The string literal may have embedded null characters. Find the first
1281 // one and truncate there.
1282 llvm::StringRef Str = S->getString();
1283 llvm::StringRef::size_type Pos = Str.find(0);
1284 if (Pos != llvm::StringRef::npos)
1285 Str = Str.substr(0, Pos);
1286
1287 return Success(Str.size(), E);
1288 }
1289
1290 return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
Chris Lattner019f4e82008-10-06 05:28:25 +00001291 }
Chris Lattner4c4867e2008-07-12 00:38:25 +00001292}
Anders Carlsson650c92f2008-07-08 15:34:11 +00001293
Chris Lattnerb542afe2008-07-11 19:10:17 +00001294bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001295 if (E->getOpcode() == BO_Comma) {
Anders Carlsson027f62e2008-12-01 02:07:06 +00001296 if (!Visit(E->getRHS()))
1297 return false;
Anders Carlsson4fdfb092008-12-01 06:44:05 +00001298
Eli Friedman33ef1452009-02-26 10:19:36 +00001299 // If we can't evaluate the LHS, it might have side effects;
1300 // conservatively mark it.
1301 if (!E->getLHS()->isEvaluatable(Info.Ctx))
1302 Info.EvalResult.HasSideEffects = true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001303
Anders Carlsson027f62e2008-12-01 02:07:06 +00001304 return true;
Eli Friedmana6afa762008-11-13 06:09:17 +00001305 }
1306
1307 if (E->isLogicalOp()) {
1308 // These need to be handled specially because the operands aren't
1309 // necessarily integral
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001310 bool lhsResult, rhsResult;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001312 if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
Anders Carlsson51fe9962008-11-22 21:04:56 +00001313 // We were able to evaluate the LHS, see if we can get away with not
1314 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
John McCall2de56d12010-08-25 11:45:40 +00001315 if (lhsResult == (E->getOpcode() == BO_LOr))
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001316 return Success(lhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001317
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001318 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
John McCall2de56d12010-08-25 11:45:40 +00001319 if (E->getOpcode() == BO_LOr)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001320 return Success(lhsResult || rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001321 else
Daniel Dunbar131eb432009-02-19 09:06:44 +00001322 return Success(lhsResult && rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001323 }
1324 } else {
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001325 if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001326 // We can't evaluate the LHS; however, sometimes the result
1327 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
John McCall2de56d12010-08-25 11:45:40 +00001328 if (rhsResult == (E->getOpcode() == BO_LOr) ||
1329 !rhsResult == (E->getOpcode() == BO_LAnd)) {
Daniel Dunbar131eb432009-02-19 09:06:44 +00001330 // Since we weren't able to evaluate the left hand side, it
Anders Carlssonfcb4d092008-11-30 16:51:17 +00001331 // must have had side effects.
1332 Info.EvalResult.HasSideEffects = true;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001333
1334 return Success(rhsResult, E);
Anders Carlsson4bbc0e02008-11-24 04:21:33 +00001335 }
1336 }
Anders Carlsson51fe9962008-11-22 21:04:56 +00001337 }
Eli Friedmana6afa762008-11-13 06:09:17 +00001338
Eli Friedmana6afa762008-11-13 06:09:17 +00001339 return false;
1340 }
1341
Anders Carlsson286f85e2008-11-16 07:17:21 +00001342 QualType LHSTy = E->getLHS()->getType();
1343 QualType RHSTy = E->getRHS()->getType();
Daniel Dunbar4087e242009-01-29 06:43:41 +00001344
1345 if (LHSTy->isAnyComplexType()) {
1346 assert(RHSTy->isAnyComplexType() && "Invalid comparison");
John McCallf4cf1a12010-05-07 17:22:02 +00001347 ComplexValue LHS, RHS;
Daniel Dunbar4087e242009-01-29 06:43:41 +00001348
1349 if (!EvaluateComplex(E->getLHS(), LHS, Info))
1350 return false;
1351
1352 if (!EvaluateComplex(E->getRHS(), RHS, Info))
1353 return false;
1354
1355 if (LHS.isComplexFloat()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001356 APFloat::cmpResult CR_r =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001357 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
Mike Stump1eb44332009-09-09 15:08:12 +00001358 APFloat::cmpResult CR_i =
Daniel Dunbar4087e242009-01-29 06:43:41 +00001359 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
1360
John McCall2de56d12010-08-25 11:45:40 +00001361 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001362 return Success((CR_r == APFloat::cmpEqual &&
1363 CR_i == APFloat::cmpEqual), E);
1364 else {
John McCall2de56d12010-08-25 11:45:40 +00001365 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001366 "Invalid complex comparison.");
Mike Stump1eb44332009-09-09 15:08:12 +00001367 return Success(((CR_r == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001368 CR_r == APFloat::cmpLessThan ||
1369 CR_r == APFloat::cmpUnordered) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001370 (CR_i == APFloat::cmpGreaterThan ||
Mon P Wangfc39dc42010-04-29 05:53:29 +00001371 CR_i == APFloat::cmpLessThan ||
1372 CR_i == APFloat::cmpUnordered)), E);
Daniel Dunbar131eb432009-02-19 09:06:44 +00001373 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001374 } else {
John McCall2de56d12010-08-25 11:45:40 +00001375 if (E->getOpcode() == BO_EQ)
Daniel Dunbar131eb432009-02-19 09:06:44 +00001376 return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
1377 LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
1378 else {
John McCall2de56d12010-08-25 11:45:40 +00001379 assert(E->getOpcode() == BO_NE &&
Daniel Dunbar131eb432009-02-19 09:06:44 +00001380 "Invalid compex comparison.");
1381 return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
1382 LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
1383 }
Daniel Dunbar4087e242009-01-29 06:43:41 +00001384 }
1385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Anders Carlsson286f85e2008-11-16 07:17:21 +00001387 if (LHSTy->isRealFloatingType() &&
1388 RHSTy->isRealFloatingType()) {
1389 APFloat RHS(0.0), LHS(0.0);
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Anders Carlsson286f85e2008-11-16 07:17:21 +00001391 if (!EvaluateFloat(E->getRHS(), RHS, Info))
1392 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Anders Carlsson286f85e2008-11-16 07:17:21 +00001394 if (!EvaluateFloat(E->getLHS(), LHS, Info))
1395 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Anders Carlsson286f85e2008-11-16 07:17:21 +00001397 APFloat::cmpResult CR = LHS.compare(RHS);
Anders Carlsson529569e2008-11-16 22:46:56 +00001398
Anders Carlsson286f85e2008-11-16 07:17:21 +00001399 switch (E->getOpcode()) {
1400 default:
1401 assert(0 && "Invalid binary operator!");
John McCall2de56d12010-08-25 11:45:40 +00001402 case BO_LT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001403 return Success(CR == APFloat::cmpLessThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001404 case BO_GT:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001405 return Success(CR == APFloat::cmpGreaterThan, E);
John McCall2de56d12010-08-25 11:45:40 +00001406 case BO_LE:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001407 return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001408 case BO_GE:
Mike Stump1eb44332009-09-09 15:08:12 +00001409 return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
Daniel Dunbar131eb432009-02-19 09:06:44 +00001410 E);
John McCall2de56d12010-08-25 11:45:40 +00001411 case BO_EQ:
Daniel Dunbar131eb432009-02-19 09:06:44 +00001412 return Success(CR == APFloat::cmpEqual, E);
John McCall2de56d12010-08-25 11:45:40 +00001413 case BO_NE:
Mike Stump1eb44332009-09-09 15:08:12 +00001414 return Success(CR == APFloat::cmpGreaterThan
Mon P Wangfc39dc42010-04-29 05:53:29 +00001415 || CR == APFloat::cmpLessThan
1416 || CR == APFloat::cmpUnordered, E);
Anders Carlsson286f85e2008-11-16 07:17:21 +00001417 }
Anders Carlsson286f85e2008-11-16 07:17:21 +00001418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001420 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
John McCall2de56d12010-08-25 11:45:40 +00001421 if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
John McCallefdb83e2010-05-07 21:00:08 +00001422 LValue LHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001423 if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
1424 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001425
John McCallefdb83e2010-05-07 21:00:08 +00001426 LValue RHSValue;
Anders Carlsson3068d112008-11-16 19:01:22 +00001427 if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
1428 return false;
Eli Friedmana1f47c42009-03-23 04:38:34 +00001429
Eli Friedman5bc86102009-06-14 02:17:33 +00001430 // Reject any bases from the normal codepath; we special-case comparisons
1431 // to null.
1432 if (LHSValue.getLValueBase()) {
1433 if (!E->isEqualityOp())
1434 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001435 if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001436 return false;
1437 bool bres;
1438 if (!EvalPointerValueAsBool(LHSValue, bres))
1439 return false;
John McCall2de56d12010-08-25 11:45:40 +00001440 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001441 } else if (RHSValue.getLValueBase()) {
1442 if (!E->isEqualityOp())
1443 return false;
Ken Dycka7305832010-01-15 12:37:54 +00001444 if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
Eli Friedman5bc86102009-06-14 02:17:33 +00001445 return false;
1446 bool bres;
1447 if (!EvalPointerValueAsBool(RHSValue, bres))
1448 return false;
John McCall2de56d12010-08-25 11:45:40 +00001449 return Success(bres ^ (E->getOpcode() == BO_EQ), E);
Eli Friedman5bc86102009-06-14 02:17:33 +00001450 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001451
John McCall2de56d12010-08-25 11:45:40 +00001452 if (E->getOpcode() == BO_Sub) {
Chris Lattner4992bdd2010-04-20 17:13:14 +00001453 QualType Type = E->getLHS()->getType();
1454 QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
Anders Carlsson3068d112008-11-16 19:01:22 +00001455
Ken Dycka7305832010-01-15 12:37:54 +00001456 CharUnits ElementSize = CharUnits::One();
Eli Friedmance1bca72009-06-04 20:23:20 +00001457 if (!ElementType->isVoidType() && !ElementType->isFunctionType())
Ken Dycka7305832010-01-15 12:37:54 +00001458 ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001459
Ken Dycka7305832010-01-15 12:37:54 +00001460 CharUnits Diff = LHSValue.getLValueOffset() -
1461 RHSValue.getLValueOffset();
1462 return Success(Diff / ElementSize, E);
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001463 }
1464 bool Result;
John McCall2de56d12010-08-25 11:45:40 +00001465 if (E->getOpcode() == BO_EQ) {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001466 Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
Eli Friedman267c0ab2009-04-29 20:29:43 +00001467 } else {
Eli Friedmanad02d7d2009-04-28 19:17:36 +00001468 Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
1469 }
1470 return Success(Result, E);
Anders Carlsson3068d112008-11-16 19:01:22 +00001471 }
1472 }
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001473 if (!LHSTy->isIntegralOrEnumerationType() ||
1474 !RHSTy->isIntegralOrEnumerationType()) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001475 // We can't continue from here for non-integral types, and they
1476 // could potentially confuse the following operations.
Eli Friedmana6afa762008-11-13 06:09:17 +00001477 return false;
1478 }
1479
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001480 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001481 if (!Visit(E->getLHS()))
Chris Lattner54176fd2008-07-12 00:14:42 +00001482 return false; // error in subexpression.
Eli Friedmand9f4bcd2008-07-27 05:46:18 +00001483
Eli Friedman42edd0d2009-03-24 01:14:50 +00001484 APValue RHSVal;
1485 if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001486 return false;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001487
1488 // Handle cases like (unsigned long)&a + 4.
1489 if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001490 CharUnits Offset = Result.getLValueOffset();
1491 CharUnits AdditionalOffset = CharUnits::fromQuantity(
1492 RHSVal.getInt().getZExtValue());
John McCall2de56d12010-08-25 11:45:40 +00001493 if (E->getOpcode() == BO_Add)
Ken Dycka7305832010-01-15 12:37:54 +00001494 Offset += AdditionalOffset;
Eli Friedman42edd0d2009-03-24 01:14:50 +00001495 else
Ken Dycka7305832010-01-15 12:37:54 +00001496 Offset -= AdditionalOffset;
1497 Result = APValue(Result.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001498 return true;
1499 }
1500
1501 // Handle cases like 4 + (unsigned long)&a
John McCall2de56d12010-08-25 11:45:40 +00001502 if (E->getOpcode() == BO_Add &&
Eli Friedman42edd0d2009-03-24 01:14:50 +00001503 RHSVal.isLValue() && Result.isInt()) {
Ken Dycka7305832010-01-15 12:37:54 +00001504 CharUnits Offset = RHSVal.getLValueOffset();
1505 Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
1506 Result = APValue(RHSVal.getLValueBase(), Offset);
Eli Friedman42edd0d2009-03-24 01:14:50 +00001507 return true;
1508 }
1509
1510 // All the following cases expect both operands to be an integer
1511 if (!Result.isInt() || !RHSVal.isInt())
Chris Lattnerb542afe2008-07-11 19:10:17 +00001512 return false;
Eli Friedmana6afa762008-11-13 06:09:17 +00001513
Eli Friedman42edd0d2009-03-24 01:14:50 +00001514 APSInt& RHS = RHSVal.getInt();
1515
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001516 switch (E->getOpcode()) {
Chris Lattner32fea9d2008-11-12 07:43:42 +00001517 default:
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001518 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001519 case BO_Mul: return Success(Result.getInt() * RHS, E);
1520 case BO_Add: return Success(Result.getInt() + RHS, E);
1521 case BO_Sub: return Success(Result.getInt() - RHS, E);
1522 case BO_And: return Success(Result.getInt() & RHS, E);
1523 case BO_Xor: return Success(Result.getInt() ^ RHS, E);
1524 case BO_Or: return Success(Result.getInt() | RHS, E);
1525 case BO_Div:
Chris Lattner54176fd2008-07-12 00:14:42 +00001526 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001527 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001528 return Success(Result.getInt() / RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001529 case BO_Rem:
Chris Lattner54176fd2008-07-12 00:14:42 +00001530 if (RHS == 0)
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001531 return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001532 return Success(Result.getInt() % RHS, E);
John McCall2de56d12010-08-25 11:45:40 +00001533 case BO_Shl: {
John McCall091f23f2010-11-09 22:22:12 +00001534 // During constant-folding, a negative shift is an opposite shift.
1535 if (RHS.isSigned() && RHS.isNegative()) {
1536 RHS = -RHS;
1537 goto shift_right;
1538 }
1539
1540 shift_left:
1541 unsigned SA
1542 = (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001543 return Success(Result.getInt() << SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001544 }
John McCall2de56d12010-08-25 11:45:40 +00001545 case BO_Shr: {
John McCall091f23f2010-11-09 22:22:12 +00001546 // During constant-folding, a negative shift is an opposite shift.
1547 if (RHS.isSigned() && RHS.isNegative()) {
1548 RHS = -RHS;
1549 goto shift_left;
1550 }
1551
1552 shift_right:
Mike Stump1eb44332009-09-09 15:08:12 +00001553 unsigned SA =
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001554 (unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
1555 return Success(Result.getInt() >> SA, E);
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
John McCall2de56d12010-08-25 11:45:40 +00001558 case BO_LT: return Success(Result.getInt() < RHS, E);
1559 case BO_GT: return Success(Result.getInt() > RHS, E);
1560 case BO_LE: return Success(Result.getInt() <= RHS, E);
1561 case BO_GE: return Success(Result.getInt() >= RHS, E);
1562 case BO_EQ: return Success(Result.getInt() == RHS, E);
1563 case BO_NE: return Success(Result.getInt() != RHS, E);
Eli Friedmanb11e7782008-11-13 02:13:11 +00001564 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001565}
1566
Ken Dyck8b752f12010-01-27 17:10:57 +00001567CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
Sebastian Redl5d484e82009-11-23 17:18:46 +00001568 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1569 // the result is the size of the referenced type."
1570 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1571 // result shall be the alignment of the referenced type."
1572 if (const ReferenceType *Ref = T->getAs<ReferenceType>())
1573 T = Ref->getPointeeType();
1574
Eli Friedman2be58612009-05-30 21:09:44 +00001575 // __alignof is defined to return the preferred alignment.
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001576 return Info.Ctx.toCharUnitsFromBits(
1577 Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
Chris Lattnere9feb472009-01-24 21:09:06 +00001578}
1579
Ken Dyck8b752f12010-01-27 17:10:57 +00001580CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
Chris Lattneraf707ab2009-01-24 21:53:27 +00001581 E = E->IgnoreParens();
1582
1583 // alignof decl is always accepted, even if it doesn't make sense: we default
Mike Stump1eb44332009-09-09 15:08:12 +00001584 // to 1 in those cases.
Chris Lattneraf707ab2009-01-24 21:53:27 +00001585 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001586 return Info.Ctx.getDeclAlign(DRE->getDecl(),
1587 /*RefAsPointee*/true);
Eli Friedmana1f47c42009-03-23 04:38:34 +00001588
Chris Lattneraf707ab2009-01-24 21:53:27 +00001589 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
Ken Dyck8b752f12010-01-27 17:10:57 +00001590 return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
1591 /*RefAsPointee*/true);
Chris Lattneraf707ab2009-01-24 21:53:27 +00001592
Chris Lattnere9feb472009-01-24 21:09:06 +00001593 return GetAlignOfType(E->getType());
1594}
1595
1596
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001597/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
1598/// a result as the expression's type.
1599bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
1600 const UnaryExprOrTypeTraitExpr *E) {
1601 switch(E->getKind()) {
1602 case UETT_AlignOf: {
Chris Lattnere9feb472009-01-24 21:09:06 +00001603 if (E->isArgumentType())
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001604 return Success(GetAlignOfType(E->getArgumentType()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001605 else
Ken Dyck4f3bc8f2011-03-11 02:13:43 +00001606 return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
Chris Lattnere9feb472009-01-24 21:09:06 +00001607 }
Eli Friedmana1f47c42009-03-23 04:38:34 +00001608
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001609 case UETT_VecStep: {
1610 QualType Ty = E->getTypeOfArgument();
Sebastian Redl05189992008-11-11 17:56:53 +00001611
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001612 if (Ty->isVectorType()) {
1613 unsigned n = Ty->getAs<VectorType>()->getNumElements();
Eli Friedmana1f47c42009-03-23 04:38:34 +00001614
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001615 // The vec_step built-in functions that take a 3-component
1616 // vector return 4. (OpenCL 1.1 spec 6.11.12)
1617 if (n == 3)
1618 n = 4;
Eli Friedmanf2da9df2009-01-24 22:19:05 +00001619
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00001620 return Success(n, E);
1621 } else
1622 return Success(1, E);
1623 }
1624
1625 case UETT_SizeOf: {
1626 QualType SrcTy = E->getTypeOfArgument();
1627 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1628 // the result is the size of the referenced type."
1629 // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1630 // result shall be the alignment of the referenced type."
1631 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
1632 SrcTy = Ref->getPointeeType();
1633
1634 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
1635 // extension.
1636 if (SrcTy->isVoidType() || SrcTy->isFunctionType())
1637 return Success(1, E);
1638
1639 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
1640 if (!SrcTy->isConstantSizeType())
1641 return false;
1642
1643 // Get information about the size.
1644 return Success(Info.Ctx.getTypeSizeInChars(SrcTy), E);
1645 }
1646 }
1647
1648 llvm_unreachable("unknown expr/type trait");
1649 return false;
Chris Lattnerfcee0012008-07-11 21:24:13 +00001650}
1651
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001652bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001653 CharUnits Result;
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001654 unsigned n = OOE->getNumComponents();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001655 if (n == 0)
1656 return false;
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001657 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001658 for (unsigned i = 0; i != n; ++i) {
1659 OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
1660 switch (ON.getKind()) {
1661 case OffsetOfExpr::OffsetOfNode::Array: {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001662 const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001663 APSInt IdxResult;
1664 if (!EvaluateInteger(Idx, IdxResult, Info))
1665 return false;
1666 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
1667 if (!AT)
1668 return false;
1669 CurrentType = AT->getElementType();
1670 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
1671 Result += IdxResult.getSExtValue() * ElementSize;
1672 break;
1673 }
1674
1675 case OffsetOfExpr::OffsetOfNode::Field: {
1676 FieldDecl *MemberDecl = ON.getField();
1677 const RecordType *RT = CurrentType->getAs<RecordType>();
1678 if (!RT)
1679 return false;
1680 RecordDecl *RD = RT->getDecl();
1681 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
John McCallba4f5d52011-01-20 07:57:12 +00001682 unsigned i = MemberDecl->getFieldIndex();
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001683 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001684 Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001685 CurrentType = MemberDecl->getType().getNonReferenceType();
1686 break;
1687 }
1688
1689 case OffsetOfExpr::OffsetOfNode::Identifier:
1690 llvm_unreachable("dependent __builtin_offsetof");
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001691 return false;
1692
1693 case OffsetOfExpr::OffsetOfNode::Base: {
1694 CXXBaseSpecifier *BaseSpec = ON.getBase();
1695 if (BaseSpec->isVirtual())
1696 return false;
1697
1698 // Find the layout of the class whose base we are looking into.
1699 const RecordType *RT = CurrentType->getAs<RecordType>();
1700 if (!RT)
1701 return false;
1702 RecordDecl *RD = RT->getDecl();
1703 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
1704
1705 // Find the base class itself.
1706 CurrentType = BaseSpec->getType();
1707 const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1708 if (!BaseRT)
1709 return false;
1710
1711 // Add the offset to the base.
Ken Dyck7c7f8202011-01-26 02:17:08 +00001712 Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
Douglas Gregorcc8a5d52010-04-29 00:18:15 +00001713 break;
1714 }
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001715 }
1716 }
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001717 return Success(Result, OOE);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001718}
1719
Chris Lattnerb542afe2008-07-11 19:10:17 +00001720bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00001721 if (E->getOpcode() == UO_LNot) {
Eli Friedmana6afa762008-11-13 06:09:17 +00001722 // LNot's operand isn't necessarily an integer, so we handle it specially.
1723 bool bres;
1724 if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
1725 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001726 return Success(!bres, E);
Eli Friedmana6afa762008-11-13 06:09:17 +00001727 }
1728
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001729 // Only handle integral operations...
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001730 if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
Daniel Dunbar4fff4812009-02-21 18:14:20 +00001731 return false;
1732
Chris Lattner87eae5e2008-07-11 22:52:41 +00001733 // Get the operand value into 'Result'.
1734 if (!Visit(E->getSubExpr()))
Chris Lattner75a48812008-07-11 22:15:16 +00001735 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001736
Chris Lattner75a48812008-07-11 22:15:16 +00001737 switch (E->getOpcode()) {
Chris Lattner4c4867e2008-07-12 00:38:25 +00001738 default:
Chris Lattner75a48812008-07-11 22:15:16 +00001739 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1740 // See C99 6.6p3.
Anders Carlsson0e8acbb2008-11-30 18:37:00 +00001741 return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
John McCall2de56d12010-08-25 11:45:40 +00001742 case UO_Extension:
Chris Lattner4c4867e2008-07-12 00:38:25 +00001743 // FIXME: Should extension allow i-c-e extension expressions in its scope?
1744 // If so, we could clear the diagnostic ID.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001745 return true;
John McCall2de56d12010-08-25 11:45:40 +00001746 case UO_Plus:
Mike Stump1eb44332009-09-09 15:08:12 +00001747 // The result is always just the subexpr.
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00001748 return true;
John McCall2de56d12010-08-25 11:45:40 +00001749 case UO_Minus:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001750 if (!Result.isInt()) return false;
1751 return Success(-Result.getInt(), E);
John McCall2de56d12010-08-25 11:45:40 +00001752 case UO_Not:
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001753 if (!Result.isInt()) return false;
1754 return Success(~Result.getInt(), E);
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001755 }
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001756}
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Chris Lattner732b2232008-07-12 01:15:53 +00001758/// HandleCast - This is used to evaluate implicit or explicit casts where the
1759/// result type is integer.
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001760bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1761 const Expr *SubExpr = E->getSubExpr();
Anders Carlsson82206e22008-11-30 18:14:57 +00001762 QualType DestType = E->getType();
Daniel Dunbarb92dac82009-02-19 22:16:29 +00001763 QualType SrcType = SubExpr->getType();
Anders Carlsson82206e22008-11-30 18:14:57 +00001764
Eli Friedman46a52322011-03-25 00:43:55 +00001765 switch (E->getCastKind()) {
Eli Friedman46a52322011-03-25 00:43:55 +00001766 case CK_BaseToDerived:
1767 case CK_DerivedToBase:
1768 case CK_UncheckedDerivedToBase:
1769 case CK_Dynamic:
1770 case CK_ToUnion:
1771 case CK_ArrayToPointerDecay:
1772 case CK_FunctionToPointerDecay:
1773 case CK_NullToPointer:
1774 case CK_NullToMemberPointer:
1775 case CK_BaseToDerivedMemberPointer:
1776 case CK_DerivedToBaseMemberPointer:
1777 case CK_ConstructorConversion:
1778 case CK_IntegralToPointer:
1779 case CK_ToVoid:
1780 case CK_VectorSplat:
1781 case CK_IntegralToFloating:
1782 case CK_FloatingCast:
1783 case CK_AnyPointerToObjCPointerCast:
1784 case CK_AnyPointerToBlockPointerCast:
1785 case CK_ObjCObjectLValueCast:
1786 case CK_FloatingRealToComplex:
1787 case CK_FloatingComplexToReal:
1788 case CK_FloatingComplexCast:
1789 case CK_FloatingComplexToIntegralComplex:
1790 case CK_IntegralRealToComplex:
1791 case CK_IntegralComplexCast:
1792 case CK_IntegralComplexToFloatingComplex:
1793 llvm_unreachable("invalid cast kind for integral value");
1794
Eli Friedmane50c2972011-03-25 19:07:11 +00001795 case CK_BitCast:
Eli Friedman46a52322011-03-25 00:43:55 +00001796 case CK_Dependent:
1797 case CK_GetObjCProperty:
1798 case CK_LValueBitCast:
1799 case CK_UserDefinedConversion:
1800 return false;
1801
1802 case CK_LValueToRValue:
1803 case CK_NoOp:
1804 return Visit(E->getSubExpr());
1805
1806 case CK_MemberPointerToBoolean:
1807 case CK_PointerToBoolean:
1808 case CK_IntegralToBoolean:
1809 case CK_FloatingToBoolean:
1810 case CK_FloatingComplexToBoolean:
1811 case CK_IntegralComplexToBoolean: {
Eli Friedman4efaa272008-11-12 09:44:48 +00001812 bool BoolResult;
1813 if (!HandleConversionToBool(SubExpr, BoolResult, Info))
1814 return false;
Daniel Dunbar131eb432009-02-19 09:06:44 +00001815 return Success(BoolResult, E);
Eli Friedman4efaa272008-11-12 09:44:48 +00001816 }
1817
Eli Friedman46a52322011-03-25 00:43:55 +00001818 case CK_IntegralCast: {
Chris Lattner732b2232008-07-12 01:15:53 +00001819 if (!Visit(SubExpr))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001820 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00001821
Eli Friedmanbe265702009-02-20 01:15:07 +00001822 if (!Result.isInt()) {
1823 // Only allow casts of lvalues if they are lossless.
1824 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
1825 }
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001826
Daniel Dunbardd211642009-02-19 22:24:01 +00001827 return Success(HandleIntToIntCast(DestType, SrcType,
Daniel Dunbar30c37f42009-02-19 20:17:33 +00001828 Result.getInt(), Info.Ctx), E);
Chris Lattner732b2232008-07-12 01:15:53 +00001829 }
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Eli Friedman46a52322011-03-25 00:43:55 +00001831 case CK_PointerToIntegral: {
John McCallefdb83e2010-05-07 21:00:08 +00001832 LValue LV;
Chris Lattner87eae5e2008-07-11 22:52:41 +00001833 if (!EvaluatePointer(SubExpr, LV, Info))
Chris Lattnerb542afe2008-07-11 19:10:17 +00001834 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001835
Daniel Dunbardd211642009-02-19 22:24:01 +00001836 if (LV.getLValueBase()) {
1837 // Only allow based lvalue casts if they are lossless.
1838 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
1839 return false;
Eli Friedman4efaa272008-11-12 09:44:48 +00001840
John McCallefdb83e2010-05-07 21:00:08 +00001841 LV.moveInto(Result);
Daniel Dunbardd211642009-02-19 22:24:01 +00001842 return true;
1843 }
1844
Ken Dycka7305832010-01-15 12:37:54 +00001845 APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
1846 SrcType);
Daniel Dunbardd211642009-02-19 22:24:01 +00001847 return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
Anders Carlsson2bad1682008-07-08 14:30:00 +00001848 }
Eli Friedman4efaa272008-11-12 09:44:48 +00001849
Eli Friedman46a52322011-03-25 00:43:55 +00001850 case CK_IntegralComplexToReal: {
John McCallf4cf1a12010-05-07 17:22:02 +00001851 ComplexValue C;
Eli Friedman1725f682009-04-22 19:23:09 +00001852 if (!EvaluateComplex(SubExpr, C, Info))
1853 return false;
Eli Friedman46a52322011-03-25 00:43:55 +00001854 return Success(C.getComplexIntReal(), E);
Eli Friedman1725f682009-04-22 19:23:09 +00001855 }
Eli Friedman2217c872009-02-22 11:46:18 +00001856
Eli Friedman46a52322011-03-25 00:43:55 +00001857 case CK_FloatingToIntegral: {
1858 APFloat F(0.0);
1859 if (!EvaluateFloat(SubExpr, F, Info))
1860 return false;
Chris Lattner732b2232008-07-12 01:15:53 +00001861
Eli Friedman46a52322011-03-25 00:43:55 +00001862 return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
1863 }
1864 }
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Eli Friedman46a52322011-03-25 00:43:55 +00001866 llvm_unreachable("unknown cast resulting in integral value");
1867 return false;
Anders Carlssona25ae3d2008-07-08 14:35:21 +00001868}
Anders Carlsson2bad1682008-07-08 14:30:00 +00001869
Eli Friedman722c7172009-02-28 03:59:05 +00001870bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
1871 if (E->getSubExpr()->getType()->isAnyComplexType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001872 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001873 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1874 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1875 return Success(LV.getComplexIntReal(), E);
1876 }
1877
1878 return Visit(E->getSubExpr());
1879}
1880
Eli Friedman664a1042009-02-27 04:45:43 +00001881bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman722c7172009-02-28 03:59:05 +00001882 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
John McCallf4cf1a12010-05-07 17:22:02 +00001883 ComplexValue LV;
Eli Friedman722c7172009-02-28 03:59:05 +00001884 if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
1885 return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
1886 return Success(LV.getComplexIntImag(), E);
1887 }
1888
Eli Friedman664a1042009-02-27 04:45:43 +00001889 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
1890 Info.EvalResult.HasSideEffects = true;
1891 return Success(0, E);
1892}
1893
Douglas Gregoree8aff02011-01-04 17:33:58 +00001894bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1895 return Success(E->getPackLength(), E);
1896}
1897
Sebastian Redl295995c2010-09-10 20:55:47 +00001898bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1899 return Success(E->getValue(), E);
1900}
1901
Chris Lattnerf5eeb052008-07-11 18:11:29 +00001902//===----------------------------------------------------------------------===//
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001903// Float Evaluation
1904//===----------------------------------------------------------------------===//
1905
1906namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00001907class FloatExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001908 : public ExprEvaluatorBase<FloatExprEvaluator, bool> {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001909 APFloat &Result;
1910public:
1911 FloatExprEvaluator(EvalInfo &info, APFloat &result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001912 : ExprEvaluatorBaseTy(info), Result(result) {}
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001913
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001914 bool Success(const APValue &V, const Expr *e) {
1915 Result = V.getFloat();
1916 return true;
1917 }
1918 bool Error(const Stmt *S) {
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001919 return false;
1920 }
1921
Chris Lattner019f4e82008-10-06 05:28:25 +00001922 bool VisitCallExpr(const CallExpr *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001923
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00001924 bool VisitUnaryOperator(const UnaryOperator *E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001925 bool VisitBinaryOperator(const BinaryOperator *E);
1926 bool VisitFloatingLiteral(const FloatingLiteral *E);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001927 bool VisitCastExpr(const CastExpr *E);
1928 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
Eli Friedman2217c872009-02-22 11:46:18 +00001929
John McCallabd3a852010-05-07 22:08:54 +00001930 bool VisitUnaryReal(const UnaryOperator *E);
1931 bool VisitUnaryImag(const UnaryOperator *E);
Eli Friedmanba98d6b2009-03-23 04:56:01 +00001932
John McCall189d6ef2010-10-09 01:34:31 +00001933 bool VisitDeclRefExpr(const DeclRefExpr *E);
1934
John McCallabd3a852010-05-07 22:08:54 +00001935 // FIXME: Missing: array subscript of vector, member of vector,
1936 // ImplicitValueInitExpr
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001937};
1938} // end anonymous namespace
1939
1940static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00001941 assert(E->getType()->isRealFloatingType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001942 return FloatExprEvaluator(Info, Result).Visit(E);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00001943}
1944
Jay Foad4ba2a172011-01-12 09:06:06 +00001945static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
John McCalldb7b72a2010-02-28 13:00:19 +00001946 QualType ResultTy,
1947 const Expr *Arg,
1948 bool SNaN,
1949 llvm::APFloat &Result) {
1950 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
1951 if (!S) return false;
1952
1953 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
1954
1955 llvm::APInt fill;
1956
1957 // Treat empty strings as if they were zero.
1958 if (S->getString().empty())
1959 fill = llvm::APInt(32, 0);
1960 else if (S->getString().getAsInteger(0, fill))
1961 return false;
1962
1963 if (SNaN)
1964 Result = llvm::APFloat::getSNaN(Sem, false, &fill);
1965 else
1966 Result = llvm::APFloat::getQNaN(Sem, false, &fill);
1967 return true;
1968}
1969
Chris Lattner019f4e82008-10-06 05:28:25 +00001970bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
Douglas Gregor3c385e52009-02-14 18:57:46 +00001971 switch (E->isBuiltinCall(Info.Ctx)) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00001972 default:
1973 return ExprEvaluatorBaseTy::VisitCallExpr(E);
1974
Chris Lattner019f4e82008-10-06 05:28:25 +00001975 case Builtin::BI__builtin_huge_val:
1976 case Builtin::BI__builtin_huge_valf:
1977 case Builtin::BI__builtin_huge_vall:
1978 case Builtin::BI__builtin_inf:
1979 case Builtin::BI__builtin_inff:
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001980 case Builtin::BI__builtin_infl: {
1981 const llvm::fltSemantics &Sem =
1982 Info.Ctx.getFloatTypeSemantics(E->getType());
Chris Lattner34a74ab2008-10-06 05:53:16 +00001983 Result = llvm::APFloat::getInf(Sem);
1984 return true;
Daniel Dunbar7cbed032008-10-14 05:41:12 +00001985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
John McCalldb7b72a2010-02-28 13:00:19 +00001987 case Builtin::BI__builtin_nans:
1988 case Builtin::BI__builtin_nansf:
1989 case Builtin::BI__builtin_nansl:
1990 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1991 true, Result);
1992
Chris Lattner9e621712008-10-06 06:31:58 +00001993 case Builtin::BI__builtin_nan:
1994 case Builtin::BI__builtin_nanf:
1995 case Builtin::BI__builtin_nanl:
Mike Stump4572bab2009-05-30 03:56:50 +00001996 // If this is __builtin_nan() turn this into a nan, otherwise we
Chris Lattner9e621712008-10-06 06:31:58 +00001997 // can't constant fold it.
John McCalldb7b72a2010-02-28 13:00:19 +00001998 return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
1999 false, Result);
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002000
2001 case Builtin::BI__builtin_fabs:
2002 case Builtin::BI__builtin_fabsf:
2003 case Builtin::BI__builtin_fabsl:
2004 if (!EvaluateFloat(E->getArg(0), Result, Info))
2005 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002007 if (Result.isNegative())
2008 Result.changeSign();
2009 return true;
2010
Mike Stump1eb44332009-09-09 15:08:12 +00002011 case Builtin::BI__builtin_copysign:
2012 case Builtin::BI__builtin_copysignf:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002013 case Builtin::BI__builtin_copysignl: {
2014 APFloat RHS(0.);
2015 if (!EvaluateFloat(E->getArg(0), Result, Info) ||
2016 !EvaluateFloat(E->getArg(1), RHS, Info))
2017 return false;
2018 Result.copySign(RHS);
2019 return true;
2020 }
Chris Lattner019f4e82008-10-06 05:28:25 +00002021 }
2022}
2023
John McCall189d6ef2010-10-09 01:34:31 +00002024bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002025 if (ExprEvaluatorBaseTy::VisitDeclRefExpr(E))
2026 return true;
2027
John McCall189d6ef2010-10-09 01:34:31 +00002028 const Decl *D = E->getDecl();
2029 if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false;
2030 const VarDecl *VD = cast<VarDecl>(D);
2031
2032 // Require the qualifiers to be const and not volatile.
2033 CanQualType T = Info.Ctx.getCanonicalType(E->getType());
2034 if (!T.isConstQualified() || T.isVolatileQualified())
2035 return false;
2036
2037 const Expr *Init = VD->getAnyInitializer();
2038 if (!Init) return false;
2039
2040 if (APValue *V = VD->getEvaluatedValue()) {
2041 if (V->isFloat()) {
2042 Result = V->getFloat();
2043 return true;
2044 }
2045 return false;
2046 }
2047
2048 if (VD->isEvaluatingValue())
2049 return false;
2050
2051 VD->setEvaluatingValue();
2052
2053 Expr::EvalResult InitResult;
2054 if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects &&
2055 InitResult.Val.isFloat()) {
2056 // Cache the evaluated value in the variable declaration.
2057 Result = InitResult.Val.getFloat();
2058 VD->setEvaluatedValue(InitResult.Val);
2059 return true;
2060 }
2061
2062 VD->setEvaluatedValue(APValue());
2063 return false;
2064}
2065
John McCallabd3a852010-05-07 22:08:54 +00002066bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002067 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2068 ComplexValue CV;
2069 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2070 return false;
2071 Result = CV.FloatReal;
2072 return true;
2073 }
2074
2075 return Visit(E->getSubExpr());
John McCallabd3a852010-05-07 22:08:54 +00002076}
2077
2078bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
Eli Friedman43efa312010-08-14 20:52:13 +00002079 if (E->getSubExpr()->getType()->isAnyComplexType()) {
2080 ComplexValue CV;
2081 if (!EvaluateComplex(E->getSubExpr(), CV, Info))
2082 return false;
2083 Result = CV.FloatImag;
2084 return true;
2085 }
2086
2087 if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
2088 Info.EvalResult.HasSideEffects = true;
2089 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
2090 Result = llvm::APFloat::getZero(Sem);
John McCallabd3a852010-05-07 22:08:54 +00002091 return true;
2092}
2093
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002094bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002095 if (E->getOpcode() == UO_Deref)
Nuno Lopesa468d342008-11-19 17:44:31 +00002096 return false;
2097
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002098 if (!EvaluateFloat(E->getSubExpr(), Result, Info))
2099 return false;
2100
2101 switch (E->getOpcode()) {
2102 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002103 case UO_Plus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002104 return true;
John McCall2de56d12010-08-25 11:45:40 +00002105 case UO_Minus:
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002106 Result.changeSign();
2107 return true;
2108 }
2109}
Chris Lattner019f4e82008-10-06 05:28:25 +00002110
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002111bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +00002112 if (E->getOpcode() == BO_Comma) {
Eli Friedman7f92f032009-11-16 04:25:37 +00002113 if (!EvaluateFloat(E->getRHS(), Result, Info))
2114 return false;
2115
2116 // If we can't evaluate the LHS, it might have side effects;
2117 // conservatively mark it.
2118 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2119 Info.EvalResult.HasSideEffects = true;
2120
2121 return true;
2122 }
2123
Anders Carlsson96e93662010-10-31 01:21:47 +00002124 // We can't evaluate pointer-to-member operations.
2125 if (E->isPtrMemOp())
2126 return false;
2127
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002128 // FIXME: Diagnostics? I really don't understand how the warnings
2129 // and errors are supposed to work.
Daniel Dunbar5db4b3f2008-10-16 03:51:50 +00002130 APFloat RHS(0.0);
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002131 if (!EvaluateFloat(E->getLHS(), Result, Info))
2132 return false;
2133 if (!EvaluateFloat(E->getRHS(), RHS, Info))
2134 return false;
2135
2136 switch (E->getOpcode()) {
2137 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002138 case BO_Mul:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002139 Result.multiply(RHS, APFloat::rmNearestTiesToEven);
2140 return true;
John McCall2de56d12010-08-25 11:45:40 +00002141 case BO_Add:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002142 Result.add(RHS, APFloat::rmNearestTiesToEven);
2143 return true;
John McCall2de56d12010-08-25 11:45:40 +00002144 case BO_Sub:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002145 Result.subtract(RHS, APFloat::rmNearestTiesToEven);
2146 return true;
John McCall2de56d12010-08-25 11:45:40 +00002147 case BO_Div:
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002148 Result.divide(RHS, APFloat::rmNearestTiesToEven);
2149 return true;
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002150 }
2151}
2152
2153bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
2154 Result = E->getValue();
2155 return true;
2156}
2157
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002158bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
2159 const Expr* SubExpr = E->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Eli Friedman2a523ee2011-03-25 00:54:52 +00002161 switch (E->getCastKind()) {
2162 default:
2163 return false;
2164
2165 case CK_LValueToRValue:
2166 case CK_NoOp:
2167 return Visit(SubExpr);
2168
2169 case CK_IntegralToFloating: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002170 APSInt IntResult;
Daniel Dunbar3f7d9952009-02-19 18:37:50 +00002171 if (!EvaluateInteger(SubExpr, IntResult, Info))
Eli Friedman4efaa272008-11-12 09:44:48 +00002172 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002173 Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002174 IntResult, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002175 return true;
2176 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002177
2178 case CK_FloatingCast: {
Eli Friedman4efaa272008-11-12 09:44:48 +00002179 if (!Visit(SubExpr))
2180 return false;
Daniel Dunbara2cfd342009-01-29 06:16:07 +00002181 Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
2182 Result, Info.Ctx);
Eli Friedman4efaa272008-11-12 09:44:48 +00002183 return true;
2184 }
John McCallf3ea8cf2010-11-14 08:17:51 +00002185
Eli Friedman2a523ee2011-03-25 00:54:52 +00002186 case CK_FloatingComplexToReal: {
John McCallf3ea8cf2010-11-14 08:17:51 +00002187 ComplexValue V;
2188 if (!EvaluateComplex(SubExpr, V, Info))
2189 return false;
2190 Result = V.getComplexFloatReal();
2191 return true;
2192 }
Eli Friedman2a523ee2011-03-25 00:54:52 +00002193 }
Eli Friedman4efaa272008-11-12 09:44:48 +00002194
2195 return false;
2196}
2197
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002198bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
Eli Friedman4efaa272008-11-12 09:44:48 +00002199 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
2200 return true;
2201}
2202
Eli Friedmand8bfe7f2008-08-22 00:06:13 +00002203//===----------------------------------------------------------------------===//
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002204// Complex Evaluation (for float and integer)
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002205//===----------------------------------------------------------------------===//
2206
2207namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +00002208class ComplexExprEvaluator
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002209 : public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
John McCallf4cf1a12010-05-07 17:22:02 +00002210 ComplexValue &Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002212public:
John McCallf4cf1a12010-05-07 17:22:02 +00002213 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002214 : ExprEvaluatorBaseTy(info), Result(Result) {}
2215
2216 bool Success(const APValue &V, const Expr *e) {
2217 Result.setFrom(V);
2218 return true;
2219 }
2220 bool Error(const Expr *E) {
2221 return false;
2222 }
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002224 //===--------------------------------------------------------------------===//
2225 // Visitor Methods
2226 //===--------------------------------------------------------------------===//
2227
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002228 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002230 bool VisitCastExpr(const CastExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00002231
John McCallf4cf1a12010-05-07 17:22:02 +00002232 bool VisitBinaryOperator(const BinaryOperator *E);
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002233 bool VisitUnaryOperator(const UnaryOperator *E);
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002234 // FIXME Missing: ImplicitValueInitExpr
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002235};
2236} // end anonymous namespace
2237
John McCallf4cf1a12010-05-07 17:22:02 +00002238static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
2239 EvalInfo &Info) {
John McCall7db7acb2010-05-07 05:46:35 +00002240 assert(E->getType()->isAnyComplexType());
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002241 return ComplexExprEvaluator(Info, Result).Visit(E);
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002242}
2243
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002244bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
2245 const Expr* SubExpr = E->getSubExpr();
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002246
2247 if (SubExpr->getType()->isRealFloatingType()) {
2248 Result.makeComplexFloat();
2249 APFloat &Imag = Result.FloatImag;
2250 if (!EvaluateFloat(SubExpr, Imag, Info))
2251 return false;
2252
2253 Result.FloatReal = APFloat(Imag.getSemantics());
2254 return true;
2255 } else {
2256 assert(SubExpr->getType()->isIntegerType() &&
2257 "Unexpected imaginary literal.");
2258
2259 Result.makeComplexInt();
2260 APSInt &Imag = Result.IntImag;
2261 if (!EvaluateInteger(SubExpr, Imag, Info))
2262 return false;
2263
2264 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
2265 return true;
2266 }
2267}
2268
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002269bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002270
John McCall8786da72010-12-14 17:51:41 +00002271 switch (E->getCastKind()) {
2272 case CK_BitCast:
John McCall8786da72010-12-14 17:51:41 +00002273 case CK_BaseToDerived:
2274 case CK_DerivedToBase:
2275 case CK_UncheckedDerivedToBase:
2276 case CK_Dynamic:
2277 case CK_ToUnion:
2278 case CK_ArrayToPointerDecay:
2279 case CK_FunctionToPointerDecay:
2280 case CK_NullToPointer:
2281 case CK_NullToMemberPointer:
2282 case CK_BaseToDerivedMemberPointer:
2283 case CK_DerivedToBaseMemberPointer:
2284 case CK_MemberPointerToBoolean:
2285 case CK_ConstructorConversion:
2286 case CK_IntegralToPointer:
2287 case CK_PointerToIntegral:
2288 case CK_PointerToBoolean:
2289 case CK_ToVoid:
2290 case CK_VectorSplat:
2291 case CK_IntegralCast:
2292 case CK_IntegralToBoolean:
2293 case CK_IntegralToFloating:
2294 case CK_FloatingToIntegral:
2295 case CK_FloatingToBoolean:
2296 case CK_FloatingCast:
2297 case CK_AnyPointerToObjCPointerCast:
2298 case CK_AnyPointerToBlockPointerCast:
2299 case CK_ObjCObjectLValueCast:
2300 case CK_FloatingComplexToReal:
2301 case CK_FloatingComplexToBoolean:
2302 case CK_IntegralComplexToReal:
2303 case CK_IntegralComplexToBoolean:
2304 llvm_unreachable("invalid cast kind for complex value");
John McCall2bb5d002010-11-13 09:02:35 +00002305
John McCall8786da72010-12-14 17:51:41 +00002306 case CK_LValueToRValue:
2307 case CK_NoOp:
2308 return Visit(E->getSubExpr());
2309
2310 case CK_Dependent:
2311 case CK_GetObjCProperty:
Eli Friedman46a52322011-03-25 00:43:55 +00002312 case CK_LValueBitCast:
John McCall8786da72010-12-14 17:51:41 +00002313 case CK_UserDefinedConversion:
2314 return false;
2315
2316 case CK_FloatingRealToComplex: {
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002317 APFloat &Real = Result.FloatReal;
John McCall8786da72010-12-14 17:51:41 +00002318 if (!EvaluateFloat(E->getSubExpr(), Real, Info))
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002319 return false;
2320
John McCall8786da72010-12-14 17:51:41 +00002321 Result.makeComplexFloat();
2322 Result.FloatImag = APFloat(Real.getSemantics());
2323 return true;
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002324 }
2325
John McCall8786da72010-12-14 17:51:41 +00002326 case CK_FloatingComplexCast: {
2327 if (!Visit(E->getSubExpr()))
2328 return false;
2329
2330 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2331 QualType From
2332 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2333
2334 Result.FloatReal
2335 = HandleFloatToFloatCast(To, From, Result.FloatReal, Info.Ctx);
2336 Result.FloatImag
2337 = HandleFloatToFloatCast(To, From, Result.FloatImag, Info.Ctx);
2338 return true;
2339 }
2340
2341 case CK_FloatingComplexToIntegralComplex: {
2342 if (!Visit(E->getSubExpr()))
2343 return false;
2344
2345 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2346 QualType From
2347 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2348 Result.makeComplexInt();
2349 Result.IntReal = HandleFloatToIntCast(To, From, Result.FloatReal, Info.Ctx);
2350 Result.IntImag = HandleFloatToIntCast(To, From, Result.FloatImag, Info.Ctx);
2351 return true;
2352 }
2353
2354 case CK_IntegralRealToComplex: {
2355 APSInt &Real = Result.IntReal;
2356 if (!EvaluateInteger(E->getSubExpr(), Real, Info))
2357 return false;
2358
2359 Result.makeComplexInt();
2360 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
2361 return true;
2362 }
2363
2364 case CK_IntegralComplexCast: {
2365 if (!Visit(E->getSubExpr()))
2366 return false;
2367
2368 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2369 QualType From
2370 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2371
2372 Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx);
2373 Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx);
2374 return true;
2375 }
2376
2377 case CK_IntegralComplexToFloatingComplex: {
2378 if (!Visit(E->getSubExpr()))
2379 return false;
2380
2381 QualType To = E->getType()->getAs<ComplexType>()->getElementType();
2382 QualType From
2383 = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
2384 Result.makeComplexFloat();
2385 Result.FloatReal = HandleIntToFloatCast(To, From, Result.IntReal, Info.Ctx);
2386 Result.FloatImag = HandleIntToFloatCast(To, From, Result.IntImag, Info.Ctx);
2387 return true;
2388 }
2389 }
2390
2391 llvm_unreachable("unknown cast resulting in complex value");
Eli Friedmanb2dc7f52010-08-16 23:27:44 +00002392 return false;
2393}
2394
John McCallf4cf1a12010-05-07 17:22:02 +00002395bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002396 if (E->getOpcode() == BO_Comma) {
2397 if (!Visit(E->getRHS()))
2398 return false;
2399
2400 // If we can't evaluate the LHS, it might have side effects;
2401 // conservatively mark it.
2402 if (!E->getLHS()->isEvaluatable(Info.Ctx))
2403 Info.EvalResult.HasSideEffects = true;
2404
2405 return true;
2406 }
John McCallf4cf1a12010-05-07 17:22:02 +00002407 if (!Visit(E->getLHS()))
2408 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002409
John McCallf4cf1a12010-05-07 17:22:02 +00002410 ComplexValue RHS;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002411 if (!EvaluateComplex(E->getRHS(), RHS, Info))
John McCallf4cf1a12010-05-07 17:22:02 +00002412 return false;
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002413
Daniel Dunbar3f279872009-01-29 01:32:56 +00002414 assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
2415 "Invalid operands to binary operator.");
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002416 switch (E->getOpcode()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002417 default: return false;
John McCall2de56d12010-08-25 11:45:40 +00002418 case BO_Add:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002419 if (Result.isComplexFloat()) {
2420 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
2421 APFloat::rmNearestTiesToEven);
2422 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
2423 APFloat::rmNearestTiesToEven);
2424 } else {
2425 Result.getComplexIntReal() += RHS.getComplexIntReal();
2426 Result.getComplexIntImag() += RHS.getComplexIntImag();
2427 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002428 break;
John McCall2de56d12010-08-25 11:45:40 +00002429 case BO_Sub:
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002430 if (Result.isComplexFloat()) {
2431 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
2432 APFloat::rmNearestTiesToEven);
2433 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
2434 APFloat::rmNearestTiesToEven);
2435 } else {
2436 Result.getComplexIntReal() -= RHS.getComplexIntReal();
2437 Result.getComplexIntImag() -= RHS.getComplexIntImag();
2438 }
Daniel Dunbar3f279872009-01-29 01:32:56 +00002439 break;
John McCall2de56d12010-08-25 11:45:40 +00002440 case BO_Mul:
Daniel Dunbar3f279872009-01-29 01:32:56 +00002441 if (Result.isComplexFloat()) {
John McCallf4cf1a12010-05-07 17:22:02 +00002442 ComplexValue LHS = Result;
Daniel Dunbar3f279872009-01-29 01:32:56 +00002443 APFloat &LHS_r = LHS.getComplexFloatReal();
2444 APFloat &LHS_i = LHS.getComplexFloatImag();
2445 APFloat &RHS_r = RHS.getComplexFloatReal();
2446 APFloat &RHS_i = RHS.getComplexFloatImag();
Mike Stump1eb44332009-09-09 15:08:12 +00002447
Daniel Dunbar3f279872009-01-29 01:32:56 +00002448 APFloat Tmp = LHS_r;
2449 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2450 Result.getComplexFloatReal() = Tmp;
2451 Tmp = LHS_i;
2452 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2453 Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
2454
2455 Tmp = LHS_r;
2456 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2457 Result.getComplexFloatImag() = Tmp;
2458 Tmp = LHS_i;
2459 Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2460 Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
2461 } else {
John McCallf4cf1a12010-05-07 17:22:02 +00002462 ComplexValue LHS = Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002463 Result.getComplexIntReal() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002464 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
2465 LHS.getComplexIntImag() * RHS.getComplexIntImag());
Mike Stump1eb44332009-09-09 15:08:12 +00002466 Result.getComplexIntImag() =
Daniel Dunbar3f279872009-01-29 01:32:56 +00002467 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
2468 LHS.getComplexIntImag() * RHS.getComplexIntReal());
2469 }
2470 break;
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002471 case BO_Div:
2472 if (Result.isComplexFloat()) {
2473 ComplexValue LHS = Result;
2474 APFloat &LHS_r = LHS.getComplexFloatReal();
2475 APFloat &LHS_i = LHS.getComplexFloatImag();
2476 APFloat &RHS_r = RHS.getComplexFloatReal();
2477 APFloat &RHS_i = RHS.getComplexFloatImag();
2478 APFloat &Res_r = Result.getComplexFloatReal();
2479 APFloat &Res_i = Result.getComplexFloatImag();
2480
2481 APFloat Den = RHS_r;
2482 Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2483 APFloat Tmp = RHS_i;
2484 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2485 Den.add(Tmp, APFloat::rmNearestTiesToEven);
2486
2487 Res_r = LHS_r;
2488 Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2489 Tmp = LHS_i;
2490 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2491 Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
2492 Res_r.divide(Den, APFloat::rmNearestTiesToEven);
2493
2494 Res_i = LHS_i;
2495 Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
2496 Tmp = LHS_r;
2497 Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
2498 Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
2499 Res_i.divide(Den, APFloat::rmNearestTiesToEven);
2500 } else {
2501 if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) {
2502 // FIXME: what about diagnostics?
2503 return false;
2504 }
2505 ComplexValue LHS = Result;
2506 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
2507 RHS.getComplexIntImag() * RHS.getComplexIntImag();
2508 Result.getComplexIntReal() =
2509 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
2510 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
2511 Result.getComplexIntImag() =
2512 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
2513 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
2514 }
2515 break;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002516 }
2517
John McCallf4cf1a12010-05-07 17:22:02 +00002518 return true;
Anders Carlssonccc3fce2008-11-16 21:51:21 +00002519}
2520
Abramo Bagnara96fc8e42010-12-11 16:05:48 +00002521bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
2522 // Get the operand value into 'Result'.
2523 if (!Visit(E->getSubExpr()))
2524 return false;
2525
2526 switch (E->getOpcode()) {
2527 default:
2528 // FIXME: what about diagnostics?
2529 return false;
2530 case UO_Extension:
2531 return true;
2532 case UO_Plus:
2533 // The result is always just the subexpr.
2534 return true;
2535 case UO_Minus:
2536 if (Result.isComplexFloat()) {
2537 Result.getComplexFloatReal().changeSign();
2538 Result.getComplexFloatImag().changeSign();
2539 }
2540 else {
2541 Result.getComplexIntReal() = -Result.getComplexIntReal();
2542 Result.getComplexIntImag() = -Result.getComplexIntImag();
2543 }
2544 return true;
2545 case UO_Not:
2546 if (Result.isComplexFloat())
2547 Result.getComplexFloatImag().changeSign();
2548 else
2549 Result.getComplexIntImag() = -Result.getComplexIntImag();
2550 return true;
2551 }
2552}
2553
Anders Carlsson9ad16ae2008-11-16 20:27:53 +00002554//===----------------------------------------------------------------------===//
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002555// Top level Expr::Evaluate method.
Chris Lattnerf5eeb052008-07-11 18:11:29 +00002556//===----------------------------------------------------------------------===//
2557
John McCall56ca35d2011-02-17 10:25:35 +00002558static bool Evaluate(EvalInfo &Info, const Expr *E) {
John McCallefdb83e2010-05-07 21:00:08 +00002559 if (E->getType()->isVectorType()) {
2560 if (!EvaluateVector(E, Info.EvalResult.Val, Info))
Nate Begeman59b5da62009-01-18 03:20:47 +00002561 return false;
Douglas Gregor575a1c92011-05-20 16:38:50 +00002562 } else if (E->getType()->isIntegralOrEnumerationType()) {
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002563 if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(E))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002564 return false;
John McCall56ca35d2011-02-17 10:25:35 +00002565 if (Info.EvalResult.Val.isLValue() &&
2566 !IsGlobalLValue(Info.EvalResult.Val.getLValueBase()))
John McCall0f2b6922010-07-07 05:08:32 +00002567 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002568 } else if (E->getType()->hasPointerRepresentation()) {
2569 LValue LV;
2570 if (!EvaluatePointer(E, LV, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002571 return false;
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002572 if (!IsGlobalLValue(LV.Base))
John McCall42c8f872010-05-10 23:27:23 +00002573 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002574 LV.moveInto(Info.EvalResult.Val);
2575 } else if (E->getType()->isRealFloatingType()) {
2576 llvm::APFloat F(0.0);
2577 if (!EvaluateFloat(E, F, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002578 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002579
John McCallefdb83e2010-05-07 21:00:08 +00002580 Info.EvalResult.Val = APValue(F);
2581 } else if (E->getType()->isAnyComplexType()) {
2582 ComplexValue C;
2583 if (!EvaluateComplex(E, C, Info))
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002584 return false;
John McCallefdb83e2010-05-07 21:00:08 +00002585 C.moveInto(Info.EvalResult.Val);
Daniel Dunbara5fd07b2009-01-28 22:24:07 +00002586 } else
Anders Carlsson9d4c1572008-11-22 22:56:32 +00002587 return false;
Anders Carlsson6dde0d52008-11-22 21:50:49 +00002588
Anders Carlsson5b45d4e2008-11-30 16:58:53 +00002589 return true;
2590}
2591
John McCall56ca35d2011-02-17 10:25:35 +00002592/// Evaluate - Return true if this is a constant which we can fold using
2593/// any crazy technique (that has nothing to do with language standards) that
2594/// we want to. If this function returns true, it returns the folded constant
2595/// in Result.
2596bool Expr::Evaluate(EvalResult &Result, const ASTContext &Ctx) const {
2597 EvalInfo Info(Ctx, Result);
2598 return ::Evaluate(Info, this);
2599}
2600
Jay Foad4ba2a172011-01-12 09:06:06 +00002601bool Expr::EvaluateAsBooleanCondition(bool &Result,
2602 const ASTContext &Ctx) const {
John McCallcd7a4452010-01-05 23:42:56 +00002603 EvalResult Scratch;
2604 EvalInfo Info(Ctx, Scratch);
2605
2606 return HandleConversionToBool(this, Result, Info);
2607}
2608
Jay Foad4ba2a172011-01-12 09:06:06 +00002609bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
Anders Carlsson1b782762009-04-10 04:54:13 +00002610 EvalInfo Info(Ctx, Result);
2611
John McCallefdb83e2010-05-07 21:00:08 +00002612 LValue LV;
John McCall42c8f872010-05-10 23:27:23 +00002613 if (EvaluateLValue(this, LV, Info) &&
2614 !Result.HasSideEffects &&
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002615 IsGlobalLValue(LV.Base)) {
2616 LV.moveInto(Result.Val);
2617 return true;
2618 }
2619 return false;
2620}
2621
Jay Foad4ba2a172011-01-12 09:06:06 +00002622bool Expr::EvaluateAsAnyLValue(EvalResult &Result,
2623 const ASTContext &Ctx) const {
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002624 EvalInfo Info(Ctx, Result);
2625
2626 LValue LV;
2627 if (EvaluateLValue(this, LV, Info)) {
John McCallefdb83e2010-05-07 21:00:08 +00002628 LV.moveInto(Result.Val);
2629 return true;
2630 }
2631 return false;
Eli Friedmanb2f295c2009-09-13 10:17:44 +00002632}
2633
Chris Lattner6ee7aa12008-11-16 21:24:15 +00002634/// isEvaluatable - Call Evaluate to see if this expression can be constant
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002635/// folded, but discard the result.
Jay Foad4ba2a172011-01-12 09:06:06 +00002636bool Expr::isEvaluatable(const ASTContext &Ctx) const {
Anders Carlsson4fdfb092008-12-01 06:44:05 +00002637 EvalResult Result;
2638 return Evaluate(Result, Ctx) && !Result.HasSideEffects;
Chris Lattner45b6b9d2008-10-06 06:49:02 +00002639}
Anders Carlsson51fe9962008-11-22 21:04:56 +00002640
Jay Foad4ba2a172011-01-12 09:06:06 +00002641bool Expr::HasSideEffects(const ASTContext &Ctx) const {
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002642 Expr::EvalResult Result;
2643 EvalInfo Info(Ctx, Result);
Peter Collingbourne8cad3042011-05-13 03:29:01 +00002644 return HasSideEffect(Info).Visit(this);
Fariborz Jahanian393c2472009-11-05 18:03:03 +00002645}
2646
Jay Foad4ba2a172011-01-12 09:06:06 +00002647APSInt Expr::EvaluateAsInt(const ASTContext &Ctx) const {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002648 EvalResult EvalResult;
2649 bool Result = Evaluate(EvalResult, Ctx);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00002650 (void)Result;
Anders Carlsson51fe9962008-11-22 21:04:56 +00002651 assert(Result && "Could not evaluate expression");
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002652 assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
Anders Carlsson51fe9962008-11-22 21:04:56 +00002653
Anders Carlsson1c0cfd42008-12-19 20:58:05 +00002654 return EvalResult.Val.getInt();
Anders Carlsson51fe9962008-11-22 21:04:56 +00002655}
John McCalld905f5a2010-05-07 05:32:02 +00002656
Abramo Bagnarae17a6432010-05-14 17:07:14 +00002657 bool Expr::EvalResult::isGlobalLValue() const {
2658 assert(Val.isLValue());
2659 return IsGlobalLValue(Val.getLValueBase());
2660 }
2661
2662
John McCalld905f5a2010-05-07 05:32:02 +00002663/// isIntegerConstantExpr - this recursive routine will test if an expression is
2664/// an integer constant expression.
2665
2666/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
2667/// comma, etc
2668///
2669/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
2670/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
2671/// cast+dereference.
2672
2673// CheckICE - This function does the fundamental ICE checking: the returned
2674// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
2675// Note that to reduce code duplication, this helper does no evaluation
2676// itself; the caller checks whether the expression is evaluatable, and
2677// in the rare cases where CheckICE actually cares about the evaluated
2678// value, it calls into Evalute.
2679//
2680// Meanings of Val:
2681// 0: This expression is an ICE if it can be evaluated by Evaluate.
2682// 1: This expression is not an ICE, but if it isn't evaluated, it's
2683// a legal subexpression for an ICE. This return value is used to handle
2684// the comma operator in C99 mode.
2685// 2: This expression is not an ICE, and is not a legal subexpression for one.
2686
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002687namespace {
2688
John McCalld905f5a2010-05-07 05:32:02 +00002689struct ICEDiag {
2690 unsigned Val;
2691 SourceLocation Loc;
2692
2693 public:
2694 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
2695 ICEDiag() : Val(0) {}
2696};
2697
Dan Gohman3c46e8d2010-07-26 21:25:24 +00002698}
2699
2700static ICEDiag NoDiag() { return ICEDiag(); }
John McCalld905f5a2010-05-07 05:32:02 +00002701
2702static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
2703 Expr::EvalResult EVResult;
2704 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2705 !EVResult.Val.isInt()) {
2706 return ICEDiag(2, E->getLocStart());
2707 }
2708 return NoDiag();
2709}
2710
2711static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
2712 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002713 if (!E->getType()->isIntegralOrEnumerationType()) {
John McCalld905f5a2010-05-07 05:32:02 +00002714 return ICEDiag(2, E->getLocStart());
2715 }
2716
2717 switch (E->getStmtClass()) {
John McCall63c00d72011-02-09 08:16:59 +00002718#define ABSTRACT_STMT(Node)
John McCalld905f5a2010-05-07 05:32:02 +00002719#define STMT(Node, Base) case Expr::Node##Class:
2720#define EXPR(Node, Base)
2721#include "clang/AST/StmtNodes.inc"
2722 case Expr::PredefinedExprClass:
2723 case Expr::FloatingLiteralClass:
2724 case Expr::ImaginaryLiteralClass:
2725 case Expr::StringLiteralClass:
2726 case Expr::ArraySubscriptExprClass:
2727 case Expr::MemberExprClass:
2728 case Expr::CompoundAssignOperatorClass:
2729 case Expr::CompoundLiteralExprClass:
2730 case Expr::ExtVectorElementExprClass:
2731 case Expr::InitListExprClass:
2732 case Expr::DesignatedInitExprClass:
2733 case Expr::ImplicitValueInitExprClass:
2734 case Expr::ParenListExprClass:
2735 case Expr::VAArgExprClass:
2736 case Expr::AddrLabelExprClass:
2737 case Expr::StmtExprClass:
2738 case Expr::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +00002739 case Expr::CUDAKernelCallExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002740 case Expr::CXXDynamicCastExprClass:
2741 case Expr::CXXTypeidExprClass:
Francois Pichet9be88402010-09-08 23:47:05 +00002742 case Expr::CXXUuidofExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002743 case Expr::CXXNullPtrLiteralExprClass:
2744 case Expr::CXXThisExprClass:
2745 case Expr::CXXThrowExprClass:
2746 case Expr::CXXNewExprClass:
2747 case Expr::CXXDeleteExprClass:
2748 case Expr::CXXPseudoDestructorExprClass:
2749 case Expr::UnresolvedLookupExprClass:
2750 case Expr::DependentScopeDeclRefExprClass:
2751 case Expr::CXXConstructExprClass:
2752 case Expr::CXXBindTemporaryExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002753 case Expr::ExprWithCleanupsClass:
John McCalld905f5a2010-05-07 05:32:02 +00002754 case Expr::CXXTemporaryObjectExprClass:
2755 case Expr::CXXUnresolvedConstructExprClass:
2756 case Expr::CXXDependentScopeMemberExprClass:
2757 case Expr::UnresolvedMemberExprClass:
2758 case Expr::ObjCStringLiteralClass:
2759 case Expr::ObjCEncodeExprClass:
2760 case Expr::ObjCMessageExprClass:
2761 case Expr::ObjCSelectorExprClass:
2762 case Expr::ObjCProtocolExprClass:
2763 case Expr::ObjCIvarRefExprClass:
2764 case Expr::ObjCPropertyRefExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002765 case Expr::ObjCIsaExprClass:
2766 case Expr::ShuffleVectorExprClass:
2767 case Expr::BlockExprClass:
2768 case Expr::BlockDeclRefExprClass:
2769 case Expr::NoStmtClass:
John McCall7cd7d1a2010-11-15 23:31:06 +00002770 case Expr::OpaqueValueExprClass:
Douglas Gregorbe230c32011-01-03 17:17:50 +00002771 case Expr::PackExpansionExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +00002772 case Expr::SubstNonTypeTemplateParmPackExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +00002773 case Expr::AsTypeExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002774 return ICEDiag(2, E->getLocStart());
2775
Douglas Gregoree8aff02011-01-04 17:33:58 +00002776 case Expr::SizeOfPackExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002777 case Expr::GNUNullExprClass:
2778 // GCC considers the GNU __null value to be an integral constant expression.
2779 return NoDiag();
2780
2781 case Expr::ParenExprClass:
2782 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00002783 case Expr::GenericSelectionExprClass:
2784 return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002785 case Expr::IntegerLiteralClass:
2786 case Expr::CharacterLiteralClass:
2787 case Expr::CXXBoolLiteralExprClass:
Douglas Gregored8abf12010-07-08 06:14:04 +00002788 case Expr::CXXScalarValueInitExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002789 case Expr::UnaryTypeTraitExprClass:
Francois Pichet6ad6f282010-12-07 00:08:36 +00002790 case Expr::BinaryTypeTraitExprClass:
John Wiegley21ff2e52011-04-28 00:16:57 +00002791 case Expr::ArrayTypeTraitExprClass:
John Wiegley55262202011-04-25 06:54:41 +00002792 case Expr::ExpressionTraitExprClass:
Sebastian Redl2e156222010-09-10 20:55:43 +00002793 case Expr::CXXNoexceptExprClass:
John McCalld905f5a2010-05-07 05:32:02 +00002794 return NoDiag();
2795 case Expr::CallExprClass:
Sean Hunt6cf75022010-08-30 17:47:05 +00002796 case Expr::CXXOperatorCallExprClass: {
John McCalld905f5a2010-05-07 05:32:02 +00002797 const CallExpr *CE = cast<CallExpr>(E);
2798 if (CE->isBuiltinCall(Ctx))
2799 return CheckEvalInICE(E, Ctx);
2800 return ICEDiag(2, E->getLocStart());
2801 }
2802 case Expr::DeclRefExprClass:
2803 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
2804 return NoDiag();
2805 if (Ctx.getLangOptions().CPlusPlus &&
2806 E->getType().getCVRQualifiers() == Qualifiers::Const) {
2807 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
2808
2809 // Parameter variables are never constants. Without this check,
2810 // getAnyInitializer() can find a default argument, which leads
2811 // to chaos.
2812 if (isa<ParmVarDecl>(D))
2813 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2814
2815 // C++ 7.1.5.1p2
2816 // A variable of non-volatile const-qualified integral or enumeration
2817 // type initialized by an ICE can be used in ICEs.
2818 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
2819 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
2820 if (Quals.hasVolatile() || !Quals.hasConst())
2821 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2822
2823 // Look for a declaration of this variable that has an initializer.
2824 const VarDecl *ID = 0;
2825 const Expr *Init = Dcl->getAnyInitializer(ID);
2826 if (Init) {
2827 if (ID->isInitKnownICE()) {
2828 // We have already checked whether this subexpression is an
2829 // integral constant expression.
2830 if (ID->isInitICE())
2831 return NoDiag();
2832 else
2833 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2834 }
2835
2836 // It's an ICE whether or not the definition we found is
2837 // out-of-line. See DR 721 and the discussion in Clang PR
2838 // 6206 for details.
2839
2840 if (Dcl->isCheckingICE()) {
2841 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
2842 }
2843
2844 Dcl->setCheckingICE();
2845 ICEDiag Result = CheckICE(Init, Ctx);
2846 // Cache the result of the ICE test.
2847 Dcl->setInitKnownICE(Result.Val == 0);
2848 return Result;
2849 }
2850 }
2851 }
2852 return ICEDiag(2, E->getLocStart());
2853 case Expr::UnaryOperatorClass: {
2854 const UnaryOperator *Exp = cast<UnaryOperator>(E);
2855 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002856 case UO_PostInc:
2857 case UO_PostDec:
2858 case UO_PreInc:
2859 case UO_PreDec:
2860 case UO_AddrOf:
2861 case UO_Deref:
John McCalld905f5a2010-05-07 05:32:02 +00002862 return ICEDiag(2, E->getLocStart());
John McCall2de56d12010-08-25 11:45:40 +00002863 case UO_Extension:
2864 case UO_LNot:
2865 case UO_Plus:
2866 case UO_Minus:
2867 case UO_Not:
2868 case UO_Real:
2869 case UO_Imag:
John McCalld905f5a2010-05-07 05:32:02 +00002870 return CheckICE(Exp->getSubExpr(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00002871 }
2872
2873 // OffsetOf falls through here.
2874 }
2875 case Expr::OffsetOfExprClass: {
2876 // Note that per C99, offsetof must be an ICE. And AFAIK, using
2877 // Evaluate matches the proposed gcc behavior for cases like
2878 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
2879 // compliance: we should warn earlier for offsetof expressions with
2880 // array subscripts that aren't ICEs, and if the array subscripts
2881 // are ICEs, the value of the offsetof must be an integer constant.
2882 return CheckEvalInICE(E, Ctx);
2883 }
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002884 case Expr::UnaryExprOrTypeTraitExprClass: {
2885 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
2886 if ((Exp->getKind() == UETT_SizeOf) &&
2887 Exp->getTypeOfArgument()->isVariableArrayType())
John McCalld905f5a2010-05-07 05:32:02 +00002888 return ICEDiag(2, E->getLocStart());
2889 return NoDiag();
2890 }
2891 case Expr::BinaryOperatorClass: {
2892 const BinaryOperator *Exp = cast<BinaryOperator>(E);
2893 switch (Exp->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002894 case BO_PtrMemD:
2895 case BO_PtrMemI:
2896 case BO_Assign:
2897 case BO_MulAssign:
2898 case BO_DivAssign:
2899 case BO_RemAssign:
2900 case BO_AddAssign:
2901 case BO_SubAssign:
2902 case BO_ShlAssign:
2903 case BO_ShrAssign:
2904 case BO_AndAssign:
2905 case BO_XorAssign:
2906 case BO_OrAssign:
John McCalld905f5a2010-05-07 05:32:02 +00002907 return ICEDiag(2, E->getLocStart());
2908
John McCall2de56d12010-08-25 11:45:40 +00002909 case BO_Mul:
2910 case BO_Div:
2911 case BO_Rem:
2912 case BO_Add:
2913 case BO_Sub:
2914 case BO_Shl:
2915 case BO_Shr:
2916 case BO_LT:
2917 case BO_GT:
2918 case BO_LE:
2919 case BO_GE:
2920 case BO_EQ:
2921 case BO_NE:
2922 case BO_And:
2923 case BO_Xor:
2924 case BO_Or:
2925 case BO_Comma: {
John McCalld905f5a2010-05-07 05:32:02 +00002926 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2927 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
John McCall2de56d12010-08-25 11:45:40 +00002928 if (Exp->getOpcode() == BO_Div ||
2929 Exp->getOpcode() == BO_Rem) {
John McCalld905f5a2010-05-07 05:32:02 +00002930 // Evaluate gives an error for undefined Div/Rem, so make sure
2931 // we don't evaluate one.
John McCall3b332ab2011-02-26 08:27:17 +00002932 if (LHSResult.Val == 0 && RHSResult.Val == 0) {
John McCalld905f5a2010-05-07 05:32:02 +00002933 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2934 if (REval == 0)
2935 return ICEDiag(1, E->getLocStart());
2936 if (REval.isSigned() && REval.isAllOnesValue()) {
2937 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2938 if (LEval.isMinSignedValue())
2939 return ICEDiag(1, E->getLocStart());
2940 }
2941 }
2942 }
John McCall2de56d12010-08-25 11:45:40 +00002943 if (Exp->getOpcode() == BO_Comma) {
John McCalld905f5a2010-05-07 05:32:02 +00002944 if (Ctx.getLangOptions().C99) {
2945 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2946 // if it isn't evaluated.
2947 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2948 return ICEDiag(1, E->getLocStart());
2949 } else {
2950 // In both C89 and C++, commas in ICEs are illegal.
2951 return ICEDiag(2, E->getLocStart());
2952 }
2953 }
2954 if (LHSResult.Val >= RHSResult.Val)
2955 return LHSResult;
2956 return RHSResult;
2957 }
John McCall2de56d12010-08-25 11:45:40 +00002958 case BO_LAnd:
2959 case BO_LOr: {
John McCalld905f5a2010-05-07 05:32:02 +00002960 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
Douglas Gregor63fe6812011-05-24 16:02:01 +00002961
2962 // C++0x [expr.const]p2:
2963 // [...] subexpressions of logical AND (5.14), logical OR
2964 // (5.15), and condi- tional (5.16) operations that are not
2965 // evaluated are not considered.
2966 if (Ctx.getLangOptions().CPlusPlus0x && LHSResult.Val == 0) {
2967 if (Exp->getOpcode() == BO_LAnd &&
2968 Exp->getLHS()->EvaluateAsInt(Ctx) == 0)
2969 return LHSResult;
2970
2971 if (Exp->getOpcode() == BO_LOr &&
2972 Exp->getLHS()->EvaluateAsInt(Ctx) != 0)
2973 return LHSResult;
2974 }
2975
John McCalld905f5a2010-05-07 05:32:02 +00002976 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2977 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2978 // Rare case where the RHS has a comma "side-effect"; we need
2979 // to actually check the condition to see whether the side
2980 // with the comma is evaluated.
John McCall2de56d12010-08-25 11:45:40 +00002981 if ((Exp->getOpcode() == BO_LAnd) !=
John McCalld905f5a2010-05-07 05:32:02 +00002982 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
2983 return RHSResult;
2984 return NoDiag();
2985 }
2986
2987 if (LHSResult.Val >= RHSResult.Val)
2988 return LHSResult;
2989 return RHSResult;
2990 }
2991 }
2992 }
2993 case Expr::ImplicitCastExprClass:
2994 case Expr::CStyleCastExprClass:
2995 case Expr::CXXFunctionalCastExprClass:
2996 case Expr::CXXStaticCastExprClass:
2997 case Expr::CXXReinterpretCastExprClass:
2998 case Expr::CXXConstCastExprClass: {
2999 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003000 if (SubExpr->getType()->isIntegralOrEnumerationType())
John McCalld905f5a2010-05-07 05:32:02 +00003001 return CheckICE(SubExpr, Ctx);
3002 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
3003 return NoDiag();
3004 return ICEDiag(2, E->getLocStart());
3005 }
John McCall56ca35d2011-02-17 10:25:35 +00003006 case Expr::BinaryConditionalOperatorClass: {
3007 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
3008 ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
3009 if (CommonResult.Val == 2) return CommonResult;
3010 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3011 if (FalseResult.Val == 2) return FalseResult;
3012 if (CommonResult.Val == 1) return CommonResult;
3013 if (FalseResult.Val == 1 &&
3014 Exp->getCommon()->EvaluateAsInt(Ctx) == 0) return NoDiag();
3015 return FalseResult;
3016 }
John McCalld905f5a2010-05-07 05:32:02 +00003017 case Expr::ConditionalOperatorClass: {
3018 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
3019 // If the condition (ignoring parens) is a __builtin_constant_p call,
3020 // then only the true side is actually considered in an integer constant
3021 // expression, and it is fully evaluated. This is an important GNU
3022 // extension. See GCC PR38377 for discussion.
3023 if (const CallExpr *CallCE
3024 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
3025 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
3026 Expr::EvalResult EVResult;
3027 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
3028 !EVResult.Val.isInt()) {
3029 return ICEDiag(2, E->getLocStart());
3030 }
3031 return NoDiag();
3032 }
3033 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
John McCalld905f5a2010-05-07 05:32:02 +00003034 if (CondResult.Val == 2)
3035 return CondResult;
Douglas Gregor63fe6812011-05-24 16:02:01 +00003036
3037 // C++0x [expr.const]p2:
3038 // subexpressions of [...] conditional (5.16) operations that
3039 // are not evaluated are not considered
3040 bool TrueBranch = Ctx.getLangOptions().CPlusPlus0x
3041 ? Exp->getCond()->EvaluateAsInt(Ctx) != 0
3042 : false;
3043 ICEDiag TrueResult = NoDiag();
3044 if (!Ctx.getLangOptions().CPlusPlus0x || TrueBranch)
3045 TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
3046 ICEDiag FalseResult = NoDiag();
3047 if (!Ctx.getLangOptions().CPlusPlus0x || !TrueBranch)
3048 FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
3049
John McCalld905f5a2010-05-07 05:32:02 +00003050 if (TrueResult.Val == 2)
3051 return TrueResult;
3052 if (FalseResult.Val == 2)
3053 return FalseResult;
3054 if (CondResult.Val == 1)
3055 return CondResult;
3056 if (TrueResult.Val == 0 && FalseResult.Val == 0)
3057 return NoDiag();
3058 // Rare case where the diagnostics depend on which side is evaluated
3059 // Note that if we get here, CondResult is 0, and at least one of
3060 // TrueResult and FalseResult is non-zero.
3061 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
3062 return FalseResult;
3063 }
3064 return TrueResult;
3065 }
3066 case Expr::CXXDefaultArgExprClass:
3067 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
3068 case Expr::ChooseExprClass: {
3069 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
3070 }
3071 }
3072
3073 // Silence a GCC warning
3074 return ICEDiag(2, E->getLocStart());
3075}
3076
3077bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
3078 SourceLocation *Loc, bool isEvaluated) const {
3079 ICEDiag d = CheckICE(this, Ctx);
3080 if (d.Val != 0) {
3081 if (Loc) *Loc = d.Loc;
3082 return false;
3083 }
3084 EvalResult EvalResult;
3085 if (!Evaluate(EvalResult, Ctx))
3086 llvm_unreachable("ICE cannot be evaluated!");
3087 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
3088 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
3089 Result = EvalResult.Val.getInt();
3090 return true;
3091}