blob: 27ae2b22661cc0af30a995ec0057c57a1d19c737 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnere0391b22008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Ted Kremenek4f530a92009-02-06 19:55:15 +000041StringLiteral::StringLiteral(ASTContext& C, const char *strData,
42 unsigned byteLength, bool Wide, QualType t,
43 SourceLocation firstLoc,
Chris Lattner4b009652007-07-25 00:24:17 +000044 SourceLocation lastLoc) :
45 Expr(StringLiteralClass, t) {
46 // OPTIMIZE: could allocate this appended to the StringLiteral.
Ted Kremenek4f530a92009-02-06 19:55:15 +000047 char *AStrData = new (C, 1) char[byteLength];
Chris Lattner4b009652007-07-25 00:24:17 +000048 memcpy(AStrData, strData, byteLength);
49 StrData = AStrData;
50 ByteLength = byteLength;
51 IsWide = Wide;
52 firstTokLoc = firstLoc;
53 lastTokLoc = lastLoc;
54}
55
Ted Kremenek4f530a92009-02-06 19:55:15 +000056void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000057 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000058 this->~StringLiteral();
59 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000060}
61
62bool UnaryOperator::isPostfix(Opcode Op) {
63 switch (Op) {
64 case PostInc:
65 case PostDec:
66 return true;
67 default:
68 return false;
69 }
70}
71
Ted Kremenek97318dd2008-07-23 22:18:43 +000072bool UnaryOperator::isPrefix(Opcode Op) {
73 switch (Op) {
74 case PreInc:
75 case PreDec:
76 return true;
77 default:
78 return false;
79 }
80}
81
Chris Lattner4b009652007-07-25 00:24:17 +000082/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
83/// corresponds to, e.g. "sizeof" or "[pre]++".
84const char *UnaryOperator::getOpcodeStr(Opcode Op) {
85 switch (Op) {
86 default: assert(0 && "Unknown unary operator");
87 case PostInc: return "++";
88 case PostDec: return "--";
89 case PreInc: return "++";
90 case PreDec: return "--";
91 case AddrOf: return "&";
92 case Deref: return "*";
93 case Plus: return "+";
94 case Minus: return "-";
95 case Not: return "~";
96 case LNot: return "!";
97 case Real: return "__real";
98 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000099 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000100 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000101 }
102}
103
104//===----------------------------------------------------------------------===//
105// Postfix Operators.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek0c97e042009-02-07 01:47:29 +0000108CallExpr::CallExpr(StmtClass SC, Expr *fn, Expr **args,
109 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000110 : Expr(SC, t,
111 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
112 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
113 NumArgs(numargs) {
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000114 SubExprs = new Stmt*[numargs+1];
115 SubExprs[FN] = fn;
116 for (unsigned i = 0; i != numargs; ++i)
117 SubExprs[i+ARGS_START] = args[i];
118 RParenLoc = rparenloc;
119}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000120
Chris Lattner4b009652007-07-25 00:24:17 +0000121CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
122 SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000123 : Expr(CallExprClass, t,
124 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
125 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
126 NumArgs(numargs) {
Ted Kremenek2719e982008-06-17 02:43:46 +0000127 SubExprs = new Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000128 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000129 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000130 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000131 RParenLoc = rparenloc;
132}
133
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000134/// setNumArgs - This changes the number of arguments present in this call.
135/// Any orphaned expressions are deleted by this, and any new operands are set
136/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000137void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000138 // No change, just return.
139 if (NumArgs == getNumArgs()) return;
140
141 // If shrinking # arguments, just delete the extras and forgot them.
142 if (NumArgs < getNumArgs()) {
143 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000144 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000145 this->NumArgs = NumArgs;
146 return;
147 }
148
149 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000150 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000151 // Copy over args.
152 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
153 NewSubExprs[i] = SubExprs[i];
154 // Null out new args.
155 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
156 NewSubExprs[i] = 0;
157
Ted Kremenek0c97e042009-02-07 01:47:29 +0000158 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000159 SubExprs = NewSubExprs;
160 this->NumArgs = NumArgs;
161}
162
Chris Lattnerc24915f2008-10-06 05:00:53 +0000163/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
164/// not, return 0.
165unsigned CallExpr::isBuiltinCall() const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000166 // All simple function calls (e.g. func()) are implicitly cast to pointer to
167 // function. As a result, we try and obtain the DeclRefExpr from the
168 // ImplicitCastExpr.
169 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
170 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000171 return 0;
172
Steve Naroff44aec4c2008-01-31 01:07:12 +0000173 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
174 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000175 return 0;
176
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000177 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
178 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000179 return 0;
180
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000181 if (!FDecl->getIdentifier())
182 return 0;
183
Chris Lattnerc24915f2008-10-06 05:00:53 +0000184 return FDecl->getIdentifier()->getBuiltinID();
185}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000186
Chris Lattnerc24915f2008-10-06 05:00:53 +0000187
Chris Lattner4b009652007-07-25 00:24:17 +0000188/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
189/// corresponds to, e.g. "<<=".
190const char *BinaryOperator::getOpcodeStr(Opcode Op) {
191 switch (Op) {
192 default: assert(0 && "Unknown binary operator");
193 case Mul: return "*";
194 case Div: return "/";
195 case Rem: return "%";
196 case Add: return "+";
197 case Sub: return "-";
198 case Shl: return "<<";
199 case Shr: return ">>";
200 case LT: return "<";
201 case GT: return ">";
202 case LE: return "<=";
203 case GE: return ">=";
204 case EQ: return "==";
205 case NE: return "!=";
206 case And: return "&";
207 case Xor: return "^";
208 case Or: return "|";
209 case LAnd: return "&&";
210 case LOr: return "||";
211 case Assign: return "=";
212 case MulAssign: return "*=";
213 case DivAssign: return "/=";
214 case RemAssign: return "%=";
215 case AddAssign: return "+=";
216 case SubAssign: return "-=";
217 case ShlAssign: return "<<=";
218 case ShrAssign: return ">>=";
219 case AndAssign: return "&=";
220 case XorAssign: return "^=";
221 case OrAssign: return "|=";
222 case Comma: return ",";
223 }
224}
225
Anders Carlsson762b7c72007-08-31 04:56:16 +0000226InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000227 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000228 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000229 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000230 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000231 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000232
233 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000234}
Chris Lattner4b009652007-07-25 00:24:17 +0000235
Douglas Gregorf603b472009-01-28 21:54:33 +0000236void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
237 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
238 delete InitExprs[Idx];
239 InitExprs.resize(NumInits, 0);
240}
241
242Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
243 if (Init >= InitExprs.size()) {
244 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
245 InitExprs.back() = expr;
246 return 0;
247 }
248
249 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
250 InitExprs[Init] = expr;
251 return Result;
252}
253
Steve Naroff6f373332008-09-04 15:31:07 +0000254/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000255///
256const FunctionType *BlockExpr::getFunctionType() const {
257 return getType()->getAsBlockPointerType()->
258 getPointeeType()->getAsFunctionType();
259}
260
Steve Naroff9ac456d2008-10-08 17:01:13 +0000261SourceLocation BlockExpr::getCaretLocation() const {
262 return TheBlock->getCaretLocation();
263}
264const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
265Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
266
267
Chris Lattner4b009652007-07-25 00:24:17 +0000268//===----------------------------------------------------------------------===//
269// Generic Expression Routines
270//===----------------------------------------------------------------------===//
271
272/// hasLocalSideEffect - Return true if this immediate expression has side
273/// effects, not counting any sub-expressions.
274bool Expr::hasLocalSideEffect() const {
275 switch (getStmtClass()) {
276 default:
277 return false;
278 case ParenExprClass:
279 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
280 case UnaryOperatorClass: {
281 const UnaryOperator *UO = cast<UnaryOperator>(this);
282
283 switch (UO->getOpcode()) {
284 default: return false;
285 case UnaryOperator::PostInc:
286 case UnaryOperator::PostDec:
287 case UnaryOperator::PreInc:
288 case UnaryOperator::PreDec:
289 return true; // ++/--
290
291 case UnaryOperator::Deref:
292 // Dereferencing a volatile pointer is a side-effect.
293 return getType().isVolatileQualified();
294 case UnaryOperator::Real:
295 case UnaryOperator::Imag:
296 // accessing a piece of a volatile complex is a side-effect.
297 return UO->getSubExpr()->getType().isVolatileQualified();
298
299 case UnaryOperator::Extension:
300 return UO->getSubExpr()->hasLocalSideEffect();
301 }
302 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000303 case BinaryOperatorClass: {
304 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
305 // Consider comma to have side effects if the LHS and RHS both do.
306 if (BinOp->getOpcode() == BinaryOperator::Comma)
307 return BinOp->getLHS()->hasLocalSideEffect() &&
308 BinOp->getRHS()->hasLocalSideEffect();
309
310 return BinOp->isAssignmentOp();
311 }
Chris Lattner06078d22007-08-25 02:00:02 +0000312 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000313 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000314
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000315 case ConditionalOperatorClass: {
316 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
317 return Exp->getCond()->hasLocalSideEffect()
318 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
319 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
320 }
321
Chris Lattner4b009652007-07-25 00:24:17 +0000322 case MemberExprClass:
323 case ArraySubscriptExprClass:
324 // If the base pointer or element is to a volatile pointer/field, accessing
325 // if is a side effect.
326 return getType().isVolatileQualified();
Eli Friedman21fd0292008-05-27 15:24:04 +0000327
Chris Lattner4b009652007-07-25 00:24:17 +0000328 case CallExprClass:
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000329 case CXXOperatorCallExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000330 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
331 // should warn.
332 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000333 case ObjCMessageExprClass:
334 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000335 case StmtExprClass: {
336 // Statement exprs don't logically have side effects themselves, but are
337 // sometimes used in macros in ways that give them a type that is unused.
338 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
339 // however, if the result of the stmt expr is dead, we don't want to emit a
340 // warning.
341 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
342 if (!CS->body_empty())
343 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
344 return E->hasLocalSideEffect();
345 return false;
346 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000347 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000348 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000349 // If this is a cast to void, check the operand. Otherwise, the result of
350 // the cast is unused.
351 if (getType()->isVoidType())
352 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
353 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000354
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000355 case ImplicitCastExprClass:
356 // Check the operand, since implicit casts are inserted by Sema
357 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
358
Chris Lattner3e254fb2008-04-08 04:40:51 +0000359 case CXXDefaultArgExprClass:
360 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000361
362 case CXXNewExprClass:
363 // FIXME: In theory, there might be new expressions that don't have side
364 // effects (e.g. a placement new with an uninitialized POD).
365 case CXXDeleteExprClass:
366 return true;
367 }
Chris Lattner4b009652007-07-25 00:24:17 +0000368}
369
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000370/// DeclCanBeLvalue - Determine whether the given declaration can be
371/// an lvalue. This is a helper routine for isLvalue.
372static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000373 // C++ [temp.param]p6:
374 // A non-type non-reference template-parameter is not an lvalue.
375 if (const NonTypeTemplateParmDecl *NTTParm
376 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
377 return NTTParm->getType()->isReferenceType();
378
Douglas Gregor8acb7272008-12-11 16:49:14 +0000379 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000380 // C++ 3.10p2: An lvalue refers to an object or function.
381 (Ctx.getLangOptions().CPlusPlus &&
382 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
383}
384
Chris Lattner4b009652007-07-25 00:24:17 +0000385/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
386/// incomplete type other than void. Nonarray expressions that can be lvalues:
387/// - name, where name must be a variable
388/// - e[i]
389/// - (e), where e must be an lvalue
390/// - e.name, where e must be an lvalue
391/// - e->name
392/// - *e, the type of e cannot be a function type
393/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000394/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000395/// - reference type [C++ [expr]]
396///
Chris Lattner25168a52008-07-26 21:30:36 +0000397Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000398 // first, check the type (C99 6.3.2.1). Expressions with function
399 // type in C are not lvalues, but they can be lvalues in C++.
400 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000401 return LV_NotObjectType;
402
Steve Naroffec7736d2008-02-10 01:39:04 +0000403 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000404 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000405 return LV_IncompleteVoidType;
406
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000407 /// FIXME: Expressions can't have reference type, so the following
408 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000409 if (TR->isReferenceType()) // C++ [expr]
410 return LV_Valid;
411
412 // the type looks fine, now check the expression
413 switch (getStmtClass()) {
414 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000415 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
417 // For vectors, make sure base is an lvalue (i.e. not a function call).
418 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000419 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000420 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000421 case DeclRefExprClass:
422 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000423 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
424 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000425 return LV_Valid;
426 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000427 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000428 case BlockDeclRefExprClass: {
429 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000430 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000431 return LV_Valid;
432 break;
433 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000434 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000435 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000436 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
437 NamedDecl *Member = m->getMemberDecl();
438 // C++ [expr.ref]p4:
439 // If E2 is declared to have type "reference to T", then E1.E2
440 // is an lvalue.
441 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
442 if (Value->getType()->isReferenceType())
443 return LV_Valid;
444
445 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
446 if (isa<CXXClassVarDecl>(Member))
447 return LV_Valid;
448
449 // -- If E2 is a non-static data member [...]. If E1 is an
450 // lvalue, then E1.E2 is an lvalue.
451 if (isa<FieldDecl>(Member))
452 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
453
454 // -- If it refers to a static member function [...], then
455 // E1.E2 is an lvalue.
456 // -- Otherwise, if E1.E2 refers to a non-static member
457 // function [...], then E1.E2 is not an lvalue.
458 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
459 return Method->isStatic()? LV_Valid : LV_MemberFunction;
460
461 // -- If E2 is a member enumerator [...], the expression E1.E2
462 // is not an lvalue.
463 if (isa<EnumConstantDecl>(Member))
464 return LV_InvalidExpression;
465
466 // Not an lvalue.
467 return LV_InvalidExpression;
468 }
469
470 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000471 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000472 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000473 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000474 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000475 return LV_Valid; // C99 6.5.3p4
476
477 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000478 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
479 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000480 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000481
482 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
483 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
484 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
485 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000486 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000487 case ImplicitCastExprClass:
488 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
489 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000490 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000491 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000492 case BinaryOperatorClass:
493 case CompoundAssignOperatorClass: {
494 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000495
496 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
497 BinOp->getOpcode() == BinaryOperator::Comma)
498 return BinOp->getRHS()->isLvalue(Ctx);
499
Sebastian Redl95216a62009-02-07 00:15:38 +0000500 // C++ [expr.mptr.oper]p6
501 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
502 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
503 !BinOp->getType()->isFunctionType())
504 return BinOp->getLHS()->isLvalue(Ctx);
505
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000506 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000507 return LV_InvalidExpression;
508
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000509 if (Ctx.getLangOptions().CPlusPlus)
510 // C++ [expr.ass]p1:
511 // The result of an assignment operation [...] is an lvalue.
512 return LV_Valid;
513
514
515 // C99 6.5.16:
516 // An assignment expression [...] is not an lvalue.
517 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000518 }
Nate Begemand6d2f772009-01-18 03:20:47 +0000519 // FIXME: OverloadExprClass
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000520 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000521 case CXXOperatorCallExprClass:
522 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000523 // C++ [expr.call]p10:
524 // A function call is an lvalue if and only if the result type
525 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000526 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000527 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000528 CalleeType = FnTypePtr->getPointeeType();
529 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
530 if (FnType->getResultType()->isReferenceType())
531 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000532
533 break;
534 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000535 case CompoundLiteralExprClass: // C99 6.5.2.5p5
536 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000537 case ChooseExprClass:
538 // __builtin_choose_expr is an lvalue if the selected operand is.
539 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
540 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
541 else
542 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
543
Nate Begemanaf6ed502008-04-18 23:10:10 +0000544 case ExtVectorElementExprClass:
545 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000546 return LV_DuplicateVectorComponents;
547 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000548 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
549 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000550 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
551 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000552 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000553 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000554 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000555 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000556 case VAArgExprClass:
557 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000558 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000559 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000560 case CXXConditionDeclExprClass:
561 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000562 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000563 case CXXFunctionalCastExprClass:
564 case CXXStaticCastExprClass:
565 case CXXDynamicCastExprClass:
566 case CXXReinterpretCastExprClass:
567 case CXXConstCastExprClass:
568 // The result of an explicit cast is an lvalue if the type we are
569 // casting to is a reference type. See C++ [expr.cast]p1,
570 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
571 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
572 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
573 return LV_Valid;
574 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000575 case CXXTypeidExprClass:
576 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
577 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000578 default:
579 break;
580 }
581 return LV_InvalidExpression;
582}
583
584/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
585/// does not have an incomplete type, does not have a const-qualified type, and
586/// if it is a structure or union, does not have any member (including,
587/// recursively, any member or element of all contained aggregates or unions)
588/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000589Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
590 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000591
592 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000593 case LV_Valid:
594 // C++ 3.10p11: Functions cannot be modified, but pointers to
595 // functions can be modifiable.
596 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
597 return MLV_NotObjectType;
598 break;
599
Chris Lattner4b009652007-07-25 00:24:17 +0000600 case LV_NotObjectType: return MLV_NotObjectType;
601 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000602 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000603 case LV_InvalidExpression:
604 // If the top level is a C-style cast, and the subexpression is a valid
605 // lvalue, then this is probably a use of the old-school "cast as lvalue"
606 // GCC extension. We don't support it, but we want to produce good
607 // diagnostics when it happens so that the user knows why.
608 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
609 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
610 return MLV_LValueCast;
611 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000612 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000613 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000614
615 QualType CT = Ctx.getCanonicalType(getType());
616
617 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000618 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000619 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000620 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000621 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000622 return MLV_IncompleteType;
623
Chris Lattnera1923f62008-08-04 07:31:14 +0000624 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000625 if (r->hasConstFields())
626 return MLV_ConstQualified;
627 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000628 // The following is illegal:
629 // void takeclosure(void (^C)(void));
630 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
631 //
632 if (getStmtClass() == BlockDeclRefExprClass) {
633 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
634 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
635 return MLV_NotBlockQualified;
636 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000637
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000638 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000639 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000640 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
641 if (KVCExpr->getSetterMethod() == 0)
642 return MLV_NoSetterProperty;
643 }
Chris Lattner4b009652007-07-25 00:24:17 +0000644 return MLV_Valid;
645}
646
Ted Kremenek5778d622008-02-27 18:39:48 +0000647/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000648/// duration. This means that the address of this expression is a link-time
649/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000650bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000651 switch (getStmtClass()) {
652 default:
653 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000654 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000655 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000656 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000657 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000658 case CompoundLiteralExprClass:
659 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000660 case DeclRefExprClass:
661 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000662 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
663 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000664 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000665 if (isa<FunctionDecl>(D))
666 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000667 return false;
668 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000669 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000670 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000671 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000672 }
Chris Lattner743ec372007-11-27 21:35:27 +0000673 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000674 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000675 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000676 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000677 case CXXDefaultArgExprClass:
678 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000679 }
680}
681
Ted Kremenek87e30c52008-01-17 16:57:34 +0000682Expr* Expr::IgnoreParens() {
683 Expr* E = this;
684 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
685 E = P->getSubExpr();
686
687 return E;
688}
689
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000690/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
691/// or CastExprs or ImplicitCastExprs, returning their operand.
692Expr *Expr::IgnoreParenCasts() {
693 Expr *E = this;
694 while (true) {
695 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
696 E = P->getSubExpr();
697 else if (CastExpr *P = dyn_cast<CastExpr>(E))
698 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000699 else
700 return E;
701 }
702}
703
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000704/// hasAnyTypeDependentArguments - Determines if any of the expressions
705/// in Exprs is type-dependent.
706bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
707 for (unsigned I = 0; I < NumExprs; ++I)
708 if (Exprs[I]->isTypeDependent())
709 return true;
710
711 return false;
712}
713
714/// hasAnyValueDependentArguments - Determines if any of the expressions
715/// in Exprs is value-dependent.
716bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
717 for (unsigned I = 0; I < NumExprs; ++I)
718 if (Exprs[I]->isValueDependent())
719 return true;
720
721 return false;
722}
723
Eli Friedmandee41122009-01-25 02:32:41 +0000724bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000725 // This function is attempting whether an expression is an initializer
726 // which can be evaluated at compile-time. isEvaluatable handles most
727 // of the cases, but it can't deal with some initializer-specific
728 // expressions, and it can't deal with aggregates; we deal with those here,
729 // and fall back to isEvaluatable for the other cases.
730
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000731 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000732 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000733 case StringLiteralClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000734 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000735 case CompoundLiteralExprClass: {
736 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000737 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000738 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000739 case InitListExprClass: {
740 const InitListExpr *Exp = cast<InitListExpr>(this);
741 unsigned numInits = Exp->getNumInits();
742 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000743 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000744 return false;
745 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000746 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000747 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000748 case ImplicitValueInitExprClass:
749 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000750 case ParenExprClass: {
751 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
752 }
753 case UnaryOperatorClass: {
754 const UnaryOperator* Exp = cast<UnaryOperator>(this);
755 if (Exp->getOpcode() == UnaryOperator::Extension)
756 return Exp->getSubExpr()->isConstantInitializer(Ctx);
757 break;
758 }
759 case CStyleCastExprClass:
760 // Handle casts with a destination that's a struct or union; this
761 // deals with both the gcc no-op struct cast extension and the
762 // cast-to-union extension.
763 if (getType()->isRecordType())
764 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
765 break;
Eli Friedmanc8a00142009-01-25 03:27:40 +0000766 case DesignatedInitExprClass:
Sebastian Redl94889622009-01-25 13:34:47 +0000767 return cast<DesignatedInitExpr>(this)->
768 getInit()->isConstantInitializer(Ctx);
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000769 }
770
Eli Friedman2b0dec52009-01-25 03:12:18 +0000771 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000772}
773
Chris Lattner4b009652007-07-25 00:24:17 +0000774/// isIntegerConstantExpr - this recursive routine will test if an expression is
775/// an integer constant expression. Note: With the introduction of VLA's in
776/// C99 the result of the sizeof operator is no longer always a constant
777/// expression. The generalization of the wording to include any subexpression
778/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
779/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000780/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000781/// occurring in a context requiring a constant, would have been a constraint
782/// violation. FIXME: This routine currently implements C90 semantics.
783/// To properly implement C99 semantics this routine will need to evaluate
784/// expressions involving operators previously mentioned.
785
786/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
787/// comma, etc
788///
789/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000790/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000791///
792/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
793/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
794/// cast+dereference.
795bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
796 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedman14cc7542008-11-13 06:09:17 +0000797 // Pretest for integral type; some parts of the code crash for types that
798 // can't be sized.
799 if (!getType()->isIntegralType()) {
800 if (Loc) *Loc = getLocStart();
801 return false;
802 }
Chris Lattner4b009652007-07-25 00:24:17 +0000803 switch (getStmtClass()) {
804 default:
805 if (Loc) *Loc = getLocStart();
806 return false;
807 case ParenExprClass:
808 return cast<ParenExpr>(this)->getSubExpr()->
809 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
810 case IntegerLiteralClass:
811 Result = cast<IntegerLiteral>(this)->getValue();
812 break;
813 case CharacterLiteralClass: {
814 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000815 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000816 Result = CL->getValue();
817 Result.setIsUnsigned(!getType()->isSignedIntegerType());
818 break;
819 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000820 case CXXBoolLiteralExprClass: {
821 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
822 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
823 Result = BL->getValue();
824 Result.setIsUnsigned(!getType()->isSignedIntegerType());
825 break;
826 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000827 case CXXZeroInitValueExprClass:
828 Result.clear();
829 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000830 case TypesCompatibleExprClass: {
831 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000832 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000833 // Per gcc docs "this built-in function ignores top level
834 // qualifiers". We need to use the canonical version to properly
835 // be able to strip CRV qualifiers from the type.
836 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
837 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
838 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
839 T1.getUnqualifiedType());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000840 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000841 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000842 case CallExprClass:
843 case CXXOperatorCallExprClass: {
Steve Naroff8d3b1702007-08-08 22:15:55 +0000844 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000845 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner1eee9402008-10-06 06:40:35 +0000846
847 // If this is a call to a builtin function, constant fold it otherwise
848 // reject it.
849 if (CE->isBuiltinCall()) {
Anders Carlsson8c3de802008-12-19 20:58:05 +0000850 EvalResult EvalResult;
851 if (CE->Evaluate(EvalResult, Ctx)) {
852 assert(!EvalResult.HasSideEffects &&
853 "Foldable builtin call should not have side effects!");
854 Result = EvalResult.Val.getInt();
Chris Lattner1eee9402008-10-06 06:40:35 +0000855 break; // It is a constant, expand it.
856 }
857 }
858
Steve Naroff8d3b1702007-08-08 22:15:55 +0000859 if (Loc) *Loc = getLocStart();
860 return false;
861 }
Chris Lattner4b009652007-07-25 00:24:17 +0000862 case DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000863 case QualifiedDeclRefExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000864 if (const EnumConstantDecl *D =
865 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
866 Result = D->getInitVal();
867 break;
868 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000869 if (Ctx.getLangOptions().CPlusPlus &&
870 getType().getCVRQualifiers() == QualType::Const) {
871 // C++ 7.1.5.1p2
872 // A variable of non-volatile const-qualified integral or enumeration
873 // type initialized by an ICE can be used in ICEs.
874 if (const VarDecl *Dcl =
875 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
876 if (const Expr *Init = Dcl->getInit())
877 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
878 }
879 }
Chris Lattner4b009652007-07-25 00:24:17 +0000880 if (Loc) *Loc = getLocStart();
881 return false;
882 case UnaryOperatorClass: {
883 const UnaryOperator *Exp = cast<UnaryOperator>(this);
884
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000885 // Get the operand value. If this is offsetof, do not evalute the
Chris Lattner4b009652007-07-25 00:24:17 +0000886 // operand. This affects C99 6.6p3.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000887 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
888 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000889 return false;
890
891 switch (Exp->getOpcode()) {
892 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
893 // See C99 6.6p3.
894 default:
895 if (Loc) *Loc = Exp->getOperatorLoc();
896 return false;
897 case UnaryOperator::Extension:
898 return true; // FIXME: this is wrong.
Chris Lattner4b009652007-07-25 00:24:17 +0000899 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000900 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000901 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000902 Result = Val;
903 break;
904 }
905 case UnaryOperator::Plus:
906 break;
907 case UnaryOperator::Minus:
908 Result = -Result;
909 break;
910 case UnaryOperator::Not:
911 Result = ~Result;
912 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000913 case UnaryOperator::OffsetOf:
Daniel Dunbar461d08c2008-08-28 18:42:20 +0000914 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000915 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000916 }
917 break;
918 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000919 case SizeOfAlignOfExprClass: {
920 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000921
922 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000923 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000924
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000925 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattner20515462008-02-21 05:45:29 +0000926 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000927 if (ArgTy->isVoidType()) {
Chris Lattner20515462008-02-21 05:45:29 +0000928 Result = 1;
929 break;
930 }
931
932 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000933 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000934 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000935 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000936 }
Chris Lattner4b009652007-07-25 00:24:17 +0000937
Chris Lattner4b009652007-07-25 00:24:17 +0000938 // Get information about the size or align.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000939 if (ArgTy->isFunctionType()) {
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000940 // GCC extension: sizeof(function) = 1.
941 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000942 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000943 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000944 if (Exp->isSizeOf())
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000945 Result = Ctx.getTypeSize(ArgTy) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000946 else
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000947 Result = Ctx.getTypeAlign(ArgTy) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000948 }
Chris Lattner4b009652007-07-25 00:24:17 +0000949 break;
950 }
951 case BinaryOperatorClass: {
952 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000953 llvm::APSInt LHS, RHS;
954
955 // Initialize result to have correct signedness and width.
956 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
Eli Friedmanb2935ab2008-11-13 02:13:11 +0000957 !getType()->isSignedIntegerType());
958
Chris Lattner4b009652007-07-25 00:24:17 +0000959 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000960 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000961 return false;
962
Chris Lattner4b009652007-07-25 00:24:17 +0000963 // The short-circuiting &&/|| operators don't necessarily evaluate their
964 // RHS. Make sure to pass isEvaluated down correctly.
965 if (Exp->isLogicalOp()) {
966 bool RHSEval;
967 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000968 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000969 else {
970 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000971 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000972 }
973
974 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
975 isEvaluated & RHSEval))
976 return false;
977 } else {
978 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
979 return false;
980 }
981
982 switch (Exp->getOpcode()) {
983 default:
984 if (Loc) *Loc = getLocStart();
985 return false;
986 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000987 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000988 break;
989 case BinaryOperator::Div:
990 if (RHS == 0) {
991 if (!isEvaluated) break;
992 if (Loc) *Loc = getLocStart();
993 return false;
994 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000995 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000996 break;
997 case BinaryOperator::Rem:
998 if (RHS == 0) {
999 if (!isEvaluated) break;
1000 if (Loc) *Loc = getLocStart();
1001 return false;
1002 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001003 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001004 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001005 case BinaryOperator::Add: Result = LHS + RHS; break;
1006 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001007 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001008 Result = LHS <<
1009 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1010 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001011 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001012 Result = LHS >>
1013 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +00001014 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001015 case BinaryOperator::LT: Result = LHS < RHS; break;
1016 case BinaryOperator::GT: Result = LHS > RHS; break;
1017 case BinaryOperator::LE: Result = LHS <= RHS; break;
1018 case BinaryOperator::GE: Result = LHS >= RHS; break;
1019 case BinaryOperator::EQ: Result = LHS == RHS; break;
1020 case BinaryOperator::NE: Result = LHS != RHS; break;
1021 case BinaryOperator::And: Result = LHS & RHS; break;
1022 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1023 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001024 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001025 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001026 break;
1027 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001028 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001029 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001030
1031 case BinaryOperator::Comma:
1032 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1033 // *except* when they are contained within a subexpression that is not
1034 // evaluated". Note that Assignment can never happen due to constraints
1035 // on the LHS subexpr, so we don't need to check it here.
1036 if (isEvaluated) {
1037 if (Loc) *Loc = getLocStart();
1038 return false;
1039 }
1040
1041 // The result of the constant expr is the RHS.
1042 Result = RHS;
1043 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001044 }
1045
1046 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1047 break;
1048 }
1049 case ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +00001050 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001051 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001052 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1053 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +00001054
1055 // C99 6.6p6: shall only convert arithmetic types to integer types.
1056 if (!SubExpr->getType()->isArithmeticType() ||
1057 !getType()->isIntegerType()) {
1058 if (Loc) *Loc = SubExpr->getLocStart();
1059 return false;
1060 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001061
Chris Lattner8cd0e932008-03-05 18:54:05 +00001062 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001063
Chris Lattner4b009652007-07-25 00:24:17 +00001064 // Handle simple integer->integer casts.
1065 if (SubExpr->getType()->isIntegerType()) {
1066 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1067 return false;
1068
1069 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +00001070 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +00001071 if (getType()->isBooleanType()) {
1072 // Conversion to bool compares against zero.
1073 Result = Result != 0;
1074 Result.zextOrTrunc(DestWidth);
1075 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +00001076 Result.sextOrTrunc(DestWidth);
1077 else // If the input is unsigned, do a zero extend, noop, or truncate.
1078 Result.zextOrTrunc(DestWidth);
1079 break;
1080 }
1081
1082 // Allow floating constants that are the immediate operands of casts or that
1083 // are parenthesized.
1084 const Expr *Operand = SubExpr;
1085 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1086 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001087
1088 // If this isn't a floating literal, we can't handle it.
1089 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1090 if (!FL) {
1091 if (Loc) *Loc = Operand->getLocStart();
1092 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001093 }
Chris Lattner000c4102008-01-09 18:59:34 +00001094
1095 // If the destination is boolean, compare against zero.
1096 if (getType()->isBooleanType()) {
1097 Result = !FL->getValue().isZero();
1098 Result.zextOrTrunc(DestWidth);
1099 break;
1100 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001101
1102 // Determine whether we are converting to unsigned or signed.
1103 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +00001104
1105 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1106 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +00001107 uint64_t Space[4];
1108 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +00001109 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001110 llvm::APFloat::rmTowardZero,
1111 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001112 Result = llvm::APInt(DestWidth, 4, Space);
1113 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001114 }
1115 case ConditionalOperatorClass: {
1116 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1117
Chris Lattner45e71bf2008-12-12 06:55:44 +00001118 const Expr *Cond = Exp->getCond();
1119
1120 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001121 return false;
1122
1123 const Expr *TrueExp = Exp->getLHS();
1124 const Expr *FalseExp = Exp->getRHS();
1125 if (Result == 0) std::swap(TrueExp, FalseExp);
1126
Chris Lattner45e71bf2008-12-12 06:55:44 +00001127 // If the condition (ignoring parens) is a __builtin_constant_p call,
1128 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001129 // expression, and it is fully evaluated. This is an important GNU
1130 // extension. See GCC PR38377 for discussion.
Chris Lattner45e71bf2008-12-12 06:55:44 +00001131 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001132 if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) {
1133 EvalResult EVResult;
1134 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1135 return false;
1136 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1137 Result = EVResult.Val.getInt();
1138 if (Loc) *Loc = EVResult.DiagLoc;
1139 return true;
1140 }
Chris Lattner45e71bf2008-12-12 06:55:44 +00001141
Chris Lattner4b009652007-07-25 00:24:17 +00001142 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001143 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001144 return false;
1145 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001146 if (TrueExp &&
1147 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001148 return false;
1149 break;
1150 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001151 case CXXDefaultArgExprClass:
1152 return cast<CXXDefaultArgExpr>(this)
1153 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001154
1155 case UnaryTypeTraitExprClass:
1156 Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
1157 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001158 }
1159
1160 // Cases that are valid constant exprs fall through to here.
1161 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1162 return true;
1163}
1164
Chris Lattner4b009652007-07-25 00:24:17 +00001165/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1166/// integer constant expression with the value zero, or if this is one that is
1167/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001168bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1169{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001170 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001171 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001172 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001173 // Check that it is a cast to void*.
1174 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1175 QualType Pointee = PT->getPointeeType();
1176 if (Pointee.getCVRQualifiers() == 0 &&
1177 Pointee->isVoidType() && // to void*
1178 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001179 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001180 }
Chris Lattner4b009652007-07-25 00:24:17 +00001181 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001182 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1183 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001184 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001185 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1186 // Accept ((void*)0) as a null pointer constant, as many other
1187 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001188 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001189 } else if (const CXXDefaultArgExpr *DefaultArg
1190 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001191 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001192 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001193 } else if (isa<GNUNullExpr>(this)) {
1194 // The GNU __null extension is always a null pointer constant.
1195 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001196 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001197
Steve Naroffa2e53222008-01-14 16:10:57 +00001198 // This expression must be an integer type.
1199 if (!getType()->isIntegerType())
1200 return false;
1201
Chris Lattner4b009652007-07-25 00:24:17 +00001202 // If we have an integer constant expression, we need to *evaluate* it and
1203 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001204 // FIXME: We should probably return false if we're compiling in strict mode
1205 // and Diag is not null (this indicates that the value was foldable but not
1206 // an ICE.
1207 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001208 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001209 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001210}
Steve Naroffc11705f2007-07-28 23:10:27 +00001211
Douglas Gregor81c29152008-10-29 00:13:59 +00001212/// isBitField - Return true if this expression is a bit-field.
1213bool Expr::isBitField() {
1214 Expr *E = this->IgnoreParenCasts();
1215 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001216 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1217 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001218 return false;
1219}
1220
Nate Begemanaf6ed502008-04-18 23:10:10 +00001221unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001222 if (const VectorType *VT = getType()->getAsVectorType())
1223 return VT->getNumElements();
1224 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001225}
1226
Nate Begemanc8e51f82008-05-09 06:41:27 +00001227/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001228bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001229 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001230 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001231
1232 // Halving swizzles do not contain duplicate elements.
1233 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1234 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1235 return false;
1236
1237 // Advance past s-char prefix on hex swizzles.
1238 if (*compStr == 's') {
1239 compStr++;
1240 length--;
1241 }
Steve Naroffba67f692007-07-30 03:29:09 +00001242
Chris Lattner58d3fa52008-11-19 07:55:04 +00001243 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001244 const char *s = compStr+i;
1245 for (const char c = *s++; *s; s++)
1246 if (c == *s)
1247 return true;
1248 }
1249 return false;
1250}
Chris Lattner42158e72007-08-02 23:36:59 +00001251
Nate Begemanc8e51f82008-05-09 06:41:27 +00001252/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001253void ExtVectorElementExpr::getEncodedElementAccess(
1254 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001255 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001256 if (*compStr == 's')
1257 compStr++;
1258
1259 bool isHi = !strcmp(compStr, "hi");
1260 bool isLo = !strcmp(compStr, "lo");
1261 bool isEven = !strcmp(compStr, "even");
1262 bool isOdd = !strcmp(compStr, "odd");
1263
Nate Begemanc8e51f82008-05-09 06:41:27 +00001264 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1265 uint64_t Index;
1266
1267 if (isHi)
1268 Index = e + i;
1269 else if (isLo)
1270 Index = i;
1271 else if (isEven)
1272 Index = 2 * i;
1273 else if (isOdd)
1274 Index = 2 * i + 1;
1275 else
1276 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001277
Nate Begemana1ae7442008-05-13 21:03:02 +00001278 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001279 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001280}
1281
Steve Naroff4ed9d662007-09-27 14:38:14 +00001282// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001283ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001284 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001285 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001286 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001287 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001288 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001289 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001290 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001291 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001292 if (NumArgs) {
1293 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001294 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1295 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001296 LBracloc = LBrac;
1297 RBracloc = RBrac;
1298}
1299
Steve Naroff4ed9d662007-09-27 14:38:14 +00001300// constructor for class messages.
1301// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001302ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001303 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001304 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001305 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001306 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001307 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001308 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001309 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001310 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001311 if (NumArgs) {
1312 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001313 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1314 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001315 LBracloc = LBrac;
1316 RBracloc = RBrac;
1317}
1318
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001319// constructor for class messages.
1320ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1321 QualType retType, ObjCMethodDecl *mproto,
1322 SourceLocation LBrac, SourceLocation RBrac,
1323 Expr **ArgExprs, unsigned nargs)
1324: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1325MethodProto(mproto) {
1326 NumArgs = nargs;
1327 SubExprs = new Stmt*[NumArgs+1];
1328 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1329 if (NumArgs) {
1330 for (unsigned i = 0; i != NumArgs; ++i)
1331 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1332 }
1333 LBracloc = LBrac;
1334 RBracloc = RBrac;
1335}
1336
1337ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1338 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1339 switch (x & Flags) {
1340 default:
1341 assert(false && "Invalid ObjCMessageExpr.");
1342 case IsInstMeth:
1343 return ClassInfo(0, 0);
1344 case IsClsMethDeclUnknown:
1345 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1346 case IsClsMethDeclKnown: {
1347 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1348 return ClassInfo(D, D->getIdentifier());
1349 }
1350 }
1351}
1352
Chris Lattnerf624cd22007-10-25 00:29:32 +00001353bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001354 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001355}
1356
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001357static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001358 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1359 QualType Ty = ME->getBase()->getType();
1360
1361 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001362 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001363 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1364 // FIXME: This is linear time. And the fact that we're indexing
1365 // into the layout by position in the record means that we're
1366 // either stuck numbering the fields in the AST or we have to keep
1367 // the linear search (yuck and yuck).
1368 unsigned i = 0;
1369 for (RecordDecl::field_iterator Field = RD->field_begin(),
1370 FieldEnd = RD->field_end();
1371 Field != FieldEnd; (void)++Field, ++i) {
1372 if (*Field == FD)
1373 break;
1374 }
1375
1376 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001377 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001378 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1379 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001380
Chris Lattner8cd0e932008-03-05 18:54:05 +00001381 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001382 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001383
1384 return size + evaluateOffsetOf(C, Base);
1385 } else if (isa<CompoundLiteralExpr>(E))
1386 return 0;
1387
1388 assert(0 && "Unknown offsetof subexpression!");
1389 return 0;
1390}
1391
1392int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1393{
1394 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1395
Chris Lattner8cd0e932008-03-05 18:54:05 +00001396 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001397 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001398}
1399
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001400void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1401 // Override default behavior of traversing children. If this has a type
1402 // operand and the type is a variable-length array, the child iteration
1403 // will iterate over the size expression. However, this expression belongs
1404 // to the type, not to this, so we don't want to delete it.
1405 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001406 if (isArgumentType()) {
1407 this->~SizeOfAlignOfExpr();
1408 C.Deallocate(this);
1409 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001410 else
1411 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001412}
1413
Ted Kremenekf1a67122009-02-09 17:08:14 +00001414void OverloadExpr::Destroy(ASTContext& C) {
1415 DestroyChildren(C);
1416 C.Deallocate(SubExprs);
1417 this->~OverloadExpr();
1418 C.Deallocate(this);
1419}
1420
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001421//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001422// DesignatedInitExpr
1423//===----------------------------------------------------------------------===//
1424
1425IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1426 assert(Kind == FieldDesignator && "Only valid on a field designator");
1427 if (Field.NameOrField & 0x01)
1428 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1429 else
1430 return getField()->getIdentifier();
1431}
1432
1433DesignatedInitExpr *
1434DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1435 unsigned NumDesignators,
1436 Expr **IndexExprs, unsigned NumIndexExprs,
1437 SourceLocation ColonOrEqualLoc,
1438 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001439 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1440 sizeof(Designator) * NumDesignators +
1441 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001442 DesignatedInitExpr *DIE
1443 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1444 ColonOrEqualLoc, UsesColonSyntax,
1445 NumIndexExprs + 1);
1446
1447 // Fill in the designators
1448 unsigned ExpectedNumSubExprs = 0;
1449 designators_iterator Desig = DIE->designators_begin();
1450 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1451 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1452 if (Designators[Idx].isArrayDesignator())
1453 ++ExpectedNumSubExprs;
1454 else if (Designators[Idx].isArrayRangeDesignator())
1455 ExpectedNumSubExprs += 2;
1456 }
1457 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1458
1459 // Fill in the subexpressions, including the initializer expression.
1460 child_iterator Child = DIE->child_begin();
1461 *Child++ = Init;
1462 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1463 *Child = IndexExprs[Idx];
1464
1465 return DIE;
1466}
1467
1468SourceRange DesignatedInitExpr::getSourceRange() const {
1469 SourceLocation StartLoc;
1470 Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
1471 if (First.isFieldDesignator()) {
1472 if (UsesColonSyntax)
1473 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1474 else
1475 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1476 } else
1477 StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
1478 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1479}
1480
1481DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
1482 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1483 Ptr += sizeof(DesignatedInitExpr);
1484 return static_cast<Designator*>(static_cast<void*>(Ptr));
1485}
1486
1487DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1488 return designators_begin() + NumDesignators;
1489}
1490
1491Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1492 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1493 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1494 Ptr += sizeof(DesignatedInitExpr);
1495 Ptr += sizeof(Designator) * NumDesignators;
1496 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1497 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1498}
1499
1500Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1501 assert(D.Kind == Designator::ArrayRangeDesignator &&
1502 "Requires array range designator");
1503 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1504 Ptr += sizeof(DesignatedInitExpr);
1505 Ptr += sizeof(Designator) * NumDesignators;
1506 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1507 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1508}
1509
1510Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1511 assert(D.Kind == Designator::ArrayRangeDesignator &&
1512 "Requires array range designator");
1513 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1514 Ptr += sizeof(DesignatedInitExpr);
1515 Ptr += sizeof(Designator) * NumDesignators;
1516 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1517 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1518}
1519
1520//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001521// ExprIterator.
1522//===----------------------------------------------------------------------===//
1523
1524Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1525Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1526Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1527const Expr* ConstExprIterator::operator[](size_t idx) const {
1528 return cast<Expr>(I[idx]);
1529}
1530const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1531const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1532
1533//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001534// Child Iterators for iterating over subexpressions/substatements
1535//===----------------------------------------------------------------------===//
1536
1537// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001538Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1539Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001540
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001541// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001542Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1543Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001544
Steve Naroff6f786252008-06-02 23:03:37 +00001545// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001546Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1547Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001548
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001549// ObjCKVCRefExpr
1550Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1551Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1552
Douglas Gregord8606632008-11-04 14:56:14 +00001553// ObjCSuperExpr
1554Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1555Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1556
Chris Lattner69909292008-08-10 01:53:14 +00001557// PredefinedExpr
1558Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1559Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001560
1561// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001562Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1563Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001564
1565// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001566Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1567Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001568
1569// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001570Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1571Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001572
Chris Lattner1de66eb2007-08-26 03:42:43 +00001573// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001574Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1575Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001576
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001577// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001578Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1579Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001580
1581// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001582Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1583Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001584
1585// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001586Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1587Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001588
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001589// SizeOfAlignOfExpr
1590Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1591 // If this is of a type and the type is a VLA type (and not a typedef), the
1592 // size expression of the VLA needs to be treated as an executable expression.
1593 // Why isn't this weirdness documented better in StmtIterator?
1594 if (isArgumentType()) {
1595 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1596 getArgumentType().getTypePtr()))
1597 return child_iterator(T);
1598 return child_iterator();
1599 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001600 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001601}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001602Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1603 if (isArgumentType())
1604 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001605 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001606}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001607
1608// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001609Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001610 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001611}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001612Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001613 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001614}
1615
1616// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001617Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001618 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001619}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001620Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001621 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001622}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001623
1624// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001625Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1626Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001627
Nate Begemanaf6ed502008-04-18 23:10:10 +00001628// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001629Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1630Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001631
1632// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001633Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1634Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001635
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001636// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001637Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1638Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001639
1640// BinaryOperator
1641Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001642 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001643}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001644Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001645 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001646}
1647
1648// ConditionalOperator
1649Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001650 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001651}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001652Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001653 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001654}
1655
1656// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001657Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1658Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001659
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001660// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001661Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1662Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001663
1664// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001665Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1666 return child_iterator();
1667}
1668
1669Stmt::child_iterator TypesCompatibleExpr::child_end() {
1670 return child_iterator();
1671}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001672
1673// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001674Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1675Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001676
Douglas Gregorad4b3792008-11-29 04:51:27 +00001677// GNUNullExpr
1678Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1679Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1680
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001681// OverloadExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001682Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1683Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001684
Eli Friedmand0e9d092008-05-14 19:38:39 +00001685// ShuffleVectorExpr
1686Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001687 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001688}
1689Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001690 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001691}
1692
Anders Carlsson36760332007-10-15 20:28:48 +00001693// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001694Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1695Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001696
Anders Carlsson762b7c72007-08-31 04:56:16 +00001697// InitListExpr
1698Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001699 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001700}
1701Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001702 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001703}
1704
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001705// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001706Stmt::child_iterator DesignatedInitExpr::child_begin() {
1707 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1708 Ptr += sizeof(DesignatedInitExpr);
1709 Ptr += sizeof(Designator) * NumDesignators;
1710 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1711}
1712Stmt::child_iterator DesignatedInitExpr::child_end() {
1713 return child_iterator(&*child_begin() + NumSubExprs);
1714}
1715
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001716// ImplicitValueInitExpr
1717Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1718 return child_iterator();
1719}
1720
1721Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1722 return child_iterator();
1723}
1724
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001725// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001726Stmt::child_iterator ObjCStringLiteral::child_begin() {
1727 return child_iterator();
1728}
1729Stmt::child_iterator ObjCStringLiteral::child_end() {
1730 return child_iterator();
1731}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001732
1733// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001734Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1735Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001736
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001737// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001738Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1739 return child_iterator();
1740}
1741Stmt::child_iterator ObjCSelectorExpr::child_end() {
1742 return child_iterator();
1743}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001744
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001745// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001746Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1747 return child_iterator();
1748}
1749Stmt::child_iterator ObjCProtocolExpr::child_end() {
1750 return child_iterator();
1751}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001752
Steve Naroffc39ca262007-09-18 23:55:05 +00001753// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001754Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001755 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001756}
1757Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001758 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001759}
1760
Steve Naroff52a81c02008-09-03 18:15:37 +00001761// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001762Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1763Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001764
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001765Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1766Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }