blob: c1117e8c8841309c573e7ee05c79ff31bb8eff41 [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 Kremenek362abcd2009-02-09 20:51:47 +0000108CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000109 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000110 : Expr(SC, t,
111 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000112 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000113 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000114
115 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000116 SubExprs[FN] = fn;
117 for (unsigned i = 0; i != numargs; ++i)
118 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000119
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000120 RParenLoc = rparenloc;
121}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000122
Ted Kremenek362abcd2009-02-09 20:51:47 +0000123CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
124 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000125 : Expr(CallExprClass, t,
126 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000127 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000128 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000129
130 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000131 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000132 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000133 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000134
Chris Lattner4b009652007-07-25 00:24:17 +0000135 RParenLoc = rparenloc;
136}
137
Ted Kremenek362abcd2009-02-09 20:51:47 +0000138void CallExpr::Destroy(ASTContext& C) {
139 DestroyChildren(C);
140 if (SubExprs) C.Deallocate(SubExprs);
141 this->~CallExpr();
142 C.Deallocate(this);
143}
144
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000145/// setNumArgs - This changes the number of arguments present in this call.
146/// Any orphaned expressions are deleted by this, and any new operands are set
147/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000148void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000149 // No change, just return.
150 if (NumArgs == getNumArgs()) return;
151
152 // If shrinking # arguments, just delete the extras and forgot them.
153 if (NumArgs < getNumArgs()) {
154 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000155 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000156 this->NumArgs = NumArgs;
157 return;
158 }
159
160 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000161 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000162 // Copy over args.
163 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
164 NewSubExprs[i] = SubExprs[i];
165 // Null out new args.
166 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
167 NewSubExprs[i] = 0;
168
Ted Kremenek0c97e042009-02-07 01:47:29 +0000169 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000170 SubExprs = NewSubExprs;
171 this->NumArgs = NumArgs;
172}
173
Chris Lattnerc24915f2008-10-06 05:00:53 +0000174/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
175/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000176unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000177 // All simple function calls (e.g. func()) are implicitly cast to pointer to
178 // function. As a result, we try and obtain the DeclRefExpr from the
179 // ImplicitCastExpr.
180 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
181 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000182 return 0;
183
Steve Naroff44aec4c2008-01-31 01:07:12 +0000184 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
185 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000186 return 0;
187
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000188 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
189 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000190 return 0;
191
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000192 if (!FDecl->getIdentifier())
193 return 0;
194
Douglas Gregorb5af7382009-02-14 18:57:46 +0000195 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000196}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000197
Chris Lattnerc24915f2008-10-06 05:00:53 +0000198
Chris Lattner4b009652007-07-25 00:24:17 +0000199/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
200/// corresponds to, e.g. "<<=".
201const char *BinaryOperator::getOpcodeStr(Opcode Op) {
202 switch (Op) {
203 default: assert(0 && "Unknown binary operator");
204 case Mul: return "*";
205 case Div: return "/";
206 case Rem: return "%";
207 case Add: return "+";
208 case Sub: return "-";
209 case Shl: return "<<";
210 case Shr: return ">>";
211 case LT: return "<";
212 case GT: return ">";
213 case LE: return "<=";
214 case GE: return ">=";
215 case EQ: return "==";
216 case NE: return "!=";
217 case And: return "&";
218 case Xor: return "^";
219 case Or: return "|";
220 case LAnd: return "&&";
221 case LOr: return "||";
222 case Assign: return "=";
223 case MulAssign: return "*=";
224 case DivAssign: return "/=";
225 case RemAssign: return "%=";
226 case AddAssign: return "+=";
227 case SubAssign: return "-=";
228 case ShlAssign: return "<<=";
229 case ShrAssign: return ">>=";
230 case AndAssign: return "&=";
231 case XorAssign: return "^=";
232 case OrAssign: return "|=";
233 case Comma: return ",";
234 }
235}
236
Anders Carlsson762b7c72007-08-31 04:56:16 +0000237InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000238 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000239 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000240 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000241 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000242 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000243
244 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000245}
Chris Lattner4b009652007-07-25 00:24:17 +0000246
Douglas Gregorf603b472009-01-28 21:54:33 +0000247void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000248 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000249 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-01-28 21:54:33 +0000250 delete InitExprs[Idx];
251 InitExprs.resize(NumInits, 0);
252}
253
254Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
255 if (Init >= InitExprs.size()) {
256 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
257 InitExprs.back() = expr;
258 return 0;
259 }
260
261 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
262 InitExprs[Init] = expr;
263 return Result;
264}
265
Steve Naroff6f373332008-09-04 15:31:07 +0000266/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000267///
268const FunctionType *BlockExpr::getFunctionType() const {
269 return getType()->getAsBlockPointerType()->
270 getPointeeType()->getAsFunctionType();
271}
272
Steve Naroff9ac456d2008-10-08 17:01:13 +0000273SourceLocation BlockExpr::getCaretLocation() const {
274 return TheBlock->getCaretLocation();
275}
276const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
277Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
278
279
Chris Lattner4b009652007-07-25 00:24:17 +0000280//===----------------------------------------------------------------------===//
281// Generic Expression Routines
282//===----------------------------------------------------------------------===//
283
Chris Lattnerd2c66552009-02-14 07:37:35 +0000284/// isUnusedResultAWarning - Return true if this immediate expression should
285/// be warned about if the result is unused. If so, fill in Loc and Ranges
286/// with location to warn on and the source range[s] to report with the
287/// warning.
288bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
289 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000290 switch (getStmtClass()) {
291 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000292 Loc = getExprLoc();
293 R1 = getSourceRange();
294 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000296 return cast<ParenExpr>(this)->getSubExpr()->
297 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000298 case UnaryOperatorClass: {
299 const UnaryOperator *UO = cast<UnaryOperator>(this);
300
301 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000302 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 case UnaryOperator::PostInc:
304 case UnaryOperator::PostDec:
305 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000306 case UnaryOperator::PreDec: // ++/--
307 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000308 case UnaryOperator::Deref:
309 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000310 if (getType().isVolatileQualified())
311 return false;
312 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000313 case UnaryOperator::Real:
314 case UnaryOperator::Imag:
315 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000316 if (UO->getSubExpr()->getType().isVolatileQualified())
317 return false;
318 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000319 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000320 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000321 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000322 Loc = UO->getOperatorLoc();
323 R1 = UO->getSubExpr()->getSourceRange();
324 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000325 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000326 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000327 const BinaryOperator *BO = cast<BinaryOperator>(this);
328 // Consider comma to have side effects if the LHS or RHS does.
329 if (BO->getOpcode() == BinaryOperator::Comma)
330 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
331 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000332
Chris Lattnerd2c66552009-02-14 07:37:35 +0000333 if (BO->isAssignmentOp())
334 return false;
335 Loc = BO->getOperatorLoc();
336 R1 = BO->getLHS()->getSourceRange();
337 R2 = BO->getRHS()->getSourceRange();
338 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000339 }
Chris Lattner06078d22007-08-25 02:00:02 +0000340 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000341 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000342
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000343 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000344 // The condition must be evaluated, but if either the LHS or RHS is a
345 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000346 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000347 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
348 return true;
349 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000350 }
351
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000353 // If the base pointer or element is to a volatile pointer/field, accessing
354 // it is a side effect.
355 if (getType().isVolatileQualified())
356 return false;
357 Loc = cast<MemberExpr>(this)->getMemberLoc();
358 R1 = SourceRange(Loc, Loc);
359 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
360 return true;
361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 case ArraySubscriptExprClass:
363 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000364 // it is a side effect.
365 if (getType().isVolatileQualified())
366 return false;
367 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
368 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
369 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
370 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000371
Chris Lattner4b009652007-07-25 00:24:17 +0000372 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000373 case CXXOperatorCallExprClass: {
374 // If this is a direct call, get the callee.
375 const CallExpr *CE = cast<CallExpr>(this);
376 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
377 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
378 // If the callee has attribute pure, const, or warn_unused_result, warn
379 // about it. void foo() { strlen("bar"); } should warn.
380 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
381 if (FD->getAttr<WarnUnusedResultAttr>() ||
382 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
383 Loc = CE->getCallee()->getLocStart();
384 R1 = CE->getCallee()->getSourceRange();
385
386 if (unsigned NumArgs = CE->getNumArgs())
387 R2 = SourceRange(CE->getArg(0)->getLocStart(),
388 CE->getArg(NumArgs-1)->getLocEnd());
389 return true;
390 }
391 }
392 return false;
393 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000394 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000395 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000396 case StmtExprClass: {
397 // Statement exprs don't logically have side effects themselves, but are
398 // sometimes used in macros in ways that give them a type that is unused.
399 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
400 // however, if the result of the stmt expr is dead, we don't want to emit a
401 // warning.
402 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
403 if (!CS->body_empty())
404 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000405 return E->isUnusedResultAWarning(Loc, R1, R2);
406
407 Loc = cast<StmtExpr>(this)->getLParenLoc();
408 R1 = getSourceRange();
409 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000410 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000411 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000412 // If this is a cast to void, check the operand. Otherwise, the result of
413 // the cast is unused.
414 if (getType()->isVoidType())
415 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
416 R1, R2);
417 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
418 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
419 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000420 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // If this is a cast to void, check the operand. Otherwise, the result of
422 // the cast is unused.
423 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000424 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
425 R1, R2);
426 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
427 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
428 return true;
429
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000430 case ImplicitCastExprClass:
431 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000432 return cast<ImplicitCastExpr>(this)
433 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000434
Chris Lattner3e254fb2008-04-08 04:40:51 +0000435 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000436 return cast<CXXDefaultArgExpr>(this)
437 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000438
439 case CXXNewExprClass:
440 // FIXME: In theory, there might be new expressions that don't have side
441 // effects (e.g. a placement new with an uninitialized POD).
442 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000443 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000444 }
Chris Lattner4b009652007-07-25 00:24:17 +0000445}
446
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000447/// DeclCanBeLvalue - Determine whether the given declaration can be
448/// an lvalue. This is a helper routine for isLvalue.
449static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000450 // C++ [temp.param]p6:
451 // A non-type non-reference template-parameter is not an lvalue.
452 if (const NonTypeTemplateParmDecl *NTTParm
453 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
454 return NTTParm->getType()->isReferenceType();
455
Douglas Gregor8acb7272008-12-11 16:49:14 +0000456 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000457 // C++ 3.10p2: An lvalue refers to an object or function.
458 (Ctx.getLangOptions().CPlusPlus &&
459 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
460}
461
Chris Lattner4b009652007-07-25 00:24:17 +0000462/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
463/// incomplete type other than void. Nonarray expressions that can be lvalues:
464/// - name, where name must be a variable
465/// - e[i]
466/// - (e), where e must be an lvalue
467/// - e.name, where e must be an lvalue
468/// - e->name
469/// - *e, the type of e cannot be a function type
470/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000471/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000472/// - reference type [C++ [expr]]
473///
Chris Lattner25168a52008-07-26 21:30:36 +0000474Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000475 // first, check the type (C99 6.3.2.1). Expressions with function
476 // type in C are not lvalues, but they can be lvalues in C++.
477 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000478 return LV_NotObjectType;
479
Steve Naroffec7736d2008-02-10 01:39:04 +0000480 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000481 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000482 return LV_IncompleteVoidType;
483
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000484 /// FIXME: Expressions can't have reference type, so the following
485 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000486 if (TR->isReferenceType()) // C++ [expr]
487 return LV_Valid;
488
489 // the type looks fine, now check the expression
490 switch (getStmtClass()) {
491 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000492 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000493 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
494 // For vectors, make sure base is an lvalue (i.e. not a function call).
495 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000496 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000498 case DeclRefExprClass:
499 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000500 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
501 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000502 return LV_Valid;
503 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000504 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000505 case BlockDeclRefExprClass: {
506 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000507 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000508 return LV_Valid;
509 break;
510 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000511 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000512 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000513 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
514 NamedDecl *Member = m->getMemberDecl();
515 // C++ [expr.ref]p4:
516 // If E2 is declared to have type "reference to T", then E1.E2
517 // is an lvalue.
518 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
519 if (Value->getType()->isReferenceType())
520 return LV_Valid;
521
522 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
523 if (isa<CXXClassVarDecl>(Member))
524 return LV_Valid;
525
526 // -- If E2 is a non-static data member [...]. If E1 is an
527 // lvalue, then E1.E2 is an lvalue.
528 if (isa<FieldDecl>(Member))
529 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
530
531 // -- If it refers to a static member function [...], then
532 // E1.E2 is an lvalue.
533 // -- Otherwise, if E1.E2 refers to a non-static member
534 // function [...], then E1.E2 is not an lvalue.
535 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
536 return Method->isStatic()? LV_Valid : LV_MemberFunction;
537
538 // -- If E2 is a member enumerator [...], the expression E1.E2
539 // is not an lvalue.
540 if (isa<EnumConstantDecl>(Member))
541 return LV_InvalidExpression;
542
543 // Not an lvalue.
544 return LV_InvalidExpression;
545 }
546
547 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000548 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000549 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000550 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000551 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000552 return LV_Valid; // C99 6.5.3p4
553
554 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000555 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
556 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000557 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000558
559 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
560 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
561 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
562 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000563 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000564 case ImplicitCastExprClass:
565 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
566 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000567 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000568 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000569 case BinaryOperatorClass:
570 case CompoundAssignOperatorClass: {
571 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000572
573 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
574 BinOp->getOpcode() == BinaryOperator::Comma)
575 return BinOp->getRHS()->isLvalue(Ctx);
576
Sebastian Redl95216a62009-02-07 00:15:38 +0000577 // C++ [expr.mptr.oper]p6
578 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
579 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
580 !BinOp->getType()->isFunctionType())
581 return BinOp->getLHS()->isLvalue(Ctx);
582
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000583 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000584 return LV_InvalidExpression;
585
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000586 if (Ctx.getLangOptions().CPlusPlus)
587 // C++ [expr.ass]p1:
588 // The result of an assignment operation [...] is an lvalue.
589 return LV_Valid;
590
591
592 // C99 6.5.16:
593 // An assignment expression [...] is not an lvalue.
594 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000595 }
Nate Begemand6d2f772009-01-18 03:20:47 +0000596 // FIXME: OverloadExprClass
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000597 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000598 case CXXOperatorCallExprClass:
599 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000600 // C++ [expr.call]p10:
601 // A function call is an lvalue if and only if the result type
602 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000603 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000604 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000605 CalleeType = FnTypePtr->getPointeeType();
606 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
607 if (FnType->getResultType()->isReferenceType())
608 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000609
610 break;
611 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000612 case CompoundLiteralExprClass: // C99 6.5.2.5p5
613 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000614 case ChooseExprClass:
615 // __builtin_choose_expr is an lvalue if the selected operand is.
616 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
617 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
618 else
619 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
620
Nate Begemanaf6ed502008-04-18 23:10:10 +0000621 case ExtVectorElementExprClass:
622 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000623 return LV_DuplicateVectorComponents;
624 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000625 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
626 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000627 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
628 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000629 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000630 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000631 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000632 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000633 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000634 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000635 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000636 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000637 case CXXConditionDeclExprClass:
638 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000639 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000640 case CXXFunctionalCastExprClass:
641 case CXXStaticCastExprClass:
642 case CXXDynamicCastExprClass:
643 case CXXReinterpretCastExprClass:
644 case CXXConstCastExprClass:
645 // The result of an explicit cast is an lvalue if the type we are
646 // casting to is a reference type. See C++ [expr.cast]p1,
647 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
648 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
649 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
650 return LV_Valid;
651 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000652 case CXXTypeidExprClass:
653 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
654 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000655 default:
656 break;
657 }
658 return LV_InvalidExpression;
659}
660
661/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
662/// does not have an incomplete type, does not have a const-qualified type, and
663/// if it is a structure or union, does not have any member (including,
664/// recursively, any member or element of all contained aggregates or unions)
665/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000666Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
667 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000668
669 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000670 case LV_Valid:
671 // C++ 3.10p11: Functions cannot be modified, but pointers to
672 // functions can be modifiable.
673 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
674 return MLV_NotObjectType;
675 break;
676
Chris Lattner4b009652007-07-25 00:24:17 +0000677 case LV_NotObjectType: return MLV_NotObjectType;
678 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000679 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000680 case LV_InvalidExpression:
681 // If the top level is a C-style cast, and the subexpression is a valid
682 // lvalue, then this is probably a use of the old-school "cast as lvalue"
683 // GCC extension. We don't support it, but we want to produce good
684 // diagnostics when it happens so that the user knows why.
685 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
686 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
687 return MLV_LValueCast;
688 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000689 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000690 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000691
692 QualType CT = Ctx.getCanonicalType(getType());
693
694 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000695 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000696 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000697 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000698 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000699 return MLV_IncompleteType;
700
Chris Lattnera1923f62008-08-04 07:31:14 +0000701 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000702 if (r->hasConstFields())
703 return MLV_ConstQualified;
704 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000705 // The following is illegal:
706 // void takeclosure(void (^C)(void));
707 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
708 //
709 if (getStmtClass() == BlockDeclRefExprClass) {
710 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
711 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
712 return MLV_NotBlockQualified;
713 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000714
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000715 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000716 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000717 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
718 if (KVCExpr->getSetterMethod() == 0)
719 return MLV_NoSetterProperty;
720 }
Chris Lattner4b009652007-07-25 00:24:17 +0000721 return MLV_Valid;
722}
723
Ted Kremenek5778d622008-02-27 18:39:48 +0000724/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000725/// duration. This means that the address of this expression is a link-time
726/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000727bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000728 switch (getStmtClass()) {
729 default:
730 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000731 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000732 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000733 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000734 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000735 case CompoundLiteralExprClass:
736 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000737 case DeclRefExprClass:
738 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000739 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
740 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000741 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000742 if (isa<FunctionDecl>(D))
743 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000744 return false;
745 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000746 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000747 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000748 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000749 }
Chris Lattner743ec372007-11-27 21:35:27 +0000750 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000751 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000752 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000753 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000754 case CXXDefaultArgExprClass:
755 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000756 }
757}
758
Ted Kremenek87e30c52008-01-17 16:57:34 +0000759Expr* Expr::IgnoreParens() {
760 Expr* E = this;
761 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
762 E = P->getSubExpr();
763
764 return E;
765}
766
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000767/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
768/// or CastExprs or ImplicitCastExprs, returning their operand.
769Expr *Expr::IgnoreParenCasts() {
770 Expr *E = this;
771 while (true) {
772 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
773 E = P->getSubExpr();
774 else if (CastExpr *P = dyn_cast<CastExpr>(E))
775 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000776 else
777 return E;
778 }
779}
780
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000781/// hasAnyTypeDependentArguments - Determines if any of the expressions
782/// in Exprs is type-dependent.
783bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
784 for (unsigned I = 0; I < NumExprs; ++I)
785 if (Exprs[I]->isTypeDependent())
786 return true;
787
788 return false;
789}
790
791/// hasAnyValueDependentArguments - Determines if any of the expressions
792/// in Exprs is value-dependent.
793bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
794 for (unsigned I = 0; I < NumExprs; ++I)
795 if (Exprs[I]->isValueDependent())
796 return true;
797
798 return false;
799}
800
Eli Friedmandee41122009-01-25 02:32:41 +0000801bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000802 // This function is attempting whether an expression is an initializer
803 // which can be evaluated at compile-time. isEvaluatable handles most
804 // of the cases, but it can't deal with some initializer-specific
805 // expressions, and it can't deal with aggregates; we deal with those here,
806 // and fall back to isEvaluatable for the other cases.
807
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000808 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000809 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000810 case StringLiteralClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000811 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000812 case CompoundLiteralExprClass: {
813 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000814 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000815 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000816 case InitListExprClass: {
817 const InitListExpr *Exp = cast<InitListExpr>(this);
818 unsigned numInits = Exp->getNumInits();
819 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000820 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000821 return false;
822 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000823 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000824 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000825 case ImplicitValueInitExprClass:
826 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000827 case ParenExprClass: {
828 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
829 }
830 case UnaryOperatorClass: {
831 const UnaryOperator* Exp = cast<UnaryOperator>(this);
832 if (Exp->getOpcode() == UnaryOperator::Extension)
833 return Exp->getSubExpr()->isConstantInitializer(Ctx);
834 break;
835 }
836 case CStyleCastExprClass:
837 // Handle casts with a destination that's a struct or union; this
838 // deals with both the gcc no-op struct cast extension and the
839 // cast-to-union extension.
840 if (getType()->isRecordType())
841 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
842 break;
Eli Friedmanc8a00142009-01-25 03:27:40 +0000843 case DesignatedInitExprClass:
Sebastian Redl94889622009-01-25 13:34:47 +0000844 return cast<DesignatedInitExpr>(this)->
845 getInit()->isConstantInitializer(Ctx);
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000846 }
847
Eli Friedman2b0dec52009-01-25 03:12:18 +0000848 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000849}
850
Chris Lattner4b009652007-07-25 00:24:17 +0000851/// isIntegerConstantExpr - this recursive routine will test if an expression is
852/// an integer constant expression. Note: With the introduction of VLA's in
853/// C99 the result of the sizeof operator is no longer always a constant
854/// expression. The generalization of the wording to include any subexpression
855/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
856/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000857/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000858/// occurring in a context requiring a constant, would have been a constraint
859/// violation. FIXME: This routine currently implements C90 semantics.
860/// To properly implement C99 semantics this routine will need to evaluate
861/// expressions involving operators previously mentioned.
862
863/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
864/// comma, etc
865///
866/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000867/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000868///
869/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
870/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
871/// cast+dereference.
872bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
873 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedman14cc7542008-11-13 06:09:17 +0000874 // Pretest for integral type; some parts of the code crash for types that
875 // can't be sized.
876 if (!getType()->isIntegralType()) {
877 if (Loc) *Loc = getLocStart();
878 return false;
879 }
Chris Lattner4b009652007-07-25 00:24:17 +0000880 switch (getStmtClass()) {
881 default:
882 if (Loc) *Loc = getLocStart();
883 return false;
884 case ParenExprClass:
885 return cast<ParenExpr>(this)->getSubExpr()->
886 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
887 case IntegerLiteralClass:
888 Result = cast<IntegerLiteral>(this)->getValue();
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000889 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner4b009652007-07-25 00:24:17 +0000890 break;
891 case CharacterLiteralClass: {
892 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000893 Result = Ctx.MakeIntValue(CL->getValue(), getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000894 break;
895 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000896 case CXXBoolLiteralExprClass: {
897 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000898 Result = Ctx.MakeIntValue(BL->getValue(), getType());
Anders Carlssoned6c2142008-08-23 21:12:35 +0000899 break;
900 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000901 case CXXZeroInitValueExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000902 Result = Ctx.MakeIntValue(0, getType());
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000903 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000904 case TypesCompatibleExprClass: {
905 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000906 // Per gcc docs "this built-in function ignores top level
907 // qualifiers". We need to use the canonical version to properly
908 // be able to strip CRV qualifiers from the type.
909 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
910 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000911 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
912 T1.getUnqualifiedType()),
913 getType());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000914 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000915 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000916 case CallExprClass:
917 case CXXOperatorCallExprClass: {
Steve Naroff8d3b1702007-08-08 22:15:55 +0000918 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1eee9402008-10-06 06:40:35 +0000919
920 // If this is a call to a builtin function, constant fold it otherwise
921 // reject it.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000922 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson8c3de802008-12-19 20:58:05 +0000923 EvalResult EvalResult;
924 if (CE->Evaluate(EvalResult, Ctx)) {
925 assert(!EvalResult.HasSideEffects &&
926 "Foldable builtin call should not have side effects!");
927 Result = EvalResult.Val.getInt();
Chris Lattner1eee9402008-10-06 06:40:35 +0000928 break; // It is a constant, expand it.
929 }
930 }
931
Steve Naroff8d3b1702007-08-08 22:15:55 +0000932 if (Loc) *Loc = getLocStart();
933 return false;
934 }
Chris Lattner4b009652007-07-25 00:24:17 +0000935 case DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000936 case QualifiedDeclRefExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000937 if (const EnumConstantDecl *D =
938 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
939 Result = D->getInitVal();
940 break;
941 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000942 if (Ctx.getLangOptions().CPlusPlus &&
943 getType().getCVRQualifiers() == QualType::Const) {
944 // C++ 7.1.5.1p2
945 // A variable of non-volatile const-qualified integral or enumeration
946 // type initialized by an ICE can be used in ICEs.
947 if (const VarDecl *Dcl =
948 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
949 if (const Expr *Init = Dcl->getInit())
950 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
951 }
952 }
Chris Lattner4b009652007-07-25 00:24:17 +0000953 if (Loc) *Loc = getLocStart();
954 return false;
955 case UnaryOperatorClass: {
956 const UnaryOperator *Exp = cast<UnaryOperator>(this);
957
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000958 // Get the operand value. If this is offsetof, do not evalute the
Chris Lattner4b009652007-07-25 00:24:17 +0000959 // operand. This affects C99 6.6p3.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000960 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
961 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000962 return false;
963
964 switch (Exp->getOpcode()) {
965 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
966 // See C99 6.6p3.
967 default:
968 if (Loc) *Loc = Exp->getOperatorLoc();
969 return false;
970 case UnaryOperator::Extension:
971 return true; // FIXME: this is wrong.
Chris Lattner4b009652007-07-25 00:24:17 +0000972 case UnaryOperator::LNot: {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000973 Result = Ctx.MakeIntValue(Result == 0, getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000974 break;
975 }
976 case UnaryOperator::Plus:
977 break;
978 case UnaryOperator::Minus:
979 Result = -Result;
980 break;
981 case UnaryOperator::Not:
982 Result = ~Result;
983 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000984 case UnaryOperator::OffsetOf:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000985 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
986 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000987 }
988 break;
989 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000990 case SizeOfAlignOfExprClass: {
991 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000992
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000993 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattner20515462008-02-21 05:45:29 +0000994 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000995 if (ArgTy->isVoidType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000996 Result = Ctx.MakeIntValue(1, getType());
Chris Lattner20515462008-02-21 05:45:29 +0000997 break;
998 }
999
1000 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001001 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001002 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +00001003 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001004 }
Chris Lattner4b009652007-07-25 00:24:17 +00001005
Chris Lattner4b009652007-07-25 00:24:17 +00001006 // Get information about the size or align.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001007 if (ArgTy->isFunctionType()) {
Chris Lattnerd4dc2672008-01-02 21:54:09 +00001008 // GCC extension: sizeof(function) = 1.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001009 Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001010 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001011 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001012 if (Exp->isSizeOf())
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001013 Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001014 else
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001015 Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +00001016 }
Chris Lattner4b009652007-07-25 00:24:17 +00001017 break;
1018 }
1019 case BinaryOperatorClass: {
1020 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001021 llvm::APSInt LHS, RHS;
1022
1023 // Initialize result to have correct signedness and width.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001024 Result = Ctx.MakeIntValue(0, getType());
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001025
Chris Lattner4b009652007-07-25 00:24:17 +00001026 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001027 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001028 return false;
1029
Chris Lattner4b009652007-07-25 00:24:17 +00001030 // The short-circuiting &&/|| operators don't necessarily evaluate their
1031 // RHS. Make sure to pass isEvaluated down correctly.
1032 if (Exp->isLogicalOp()) {
1033 bool RHSEval;
1034 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001035 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001036 else {
1037 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001038 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001039 }
1040
1041 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
1042 isEvaluated & RHSEval))
1043 return false;
1044 } else {
1045 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
1046 return false;
1047 }
1048
1049 switch (Exp->getOpcode()) {
1050 default:
1051 if (Loc) *Loc = getLocStart();
1052 return false;
1053 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001054 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001055 break;
1056 case BinaryOperator::Div:
1057 if (RHS == 0) {
1058 if (!isEvaluated) break;
1059 if (Loc) *Loc = getLocStart();
1060 return false;
1061 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001062 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001063 break;
1064 case BinaryOperator::Rem:
1065 if (RHS == 0) {
1066 if (!isEvaluated) break;
1067 if (Loc) *Loc = getLocStart();
1068 return false;
1069 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001070 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001071 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001072 case BinaryOperator::Add: Result = LHS + RHS; break;
1073 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001074 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001075 Result = LHS <<
1076 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1077 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001078 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001079 Result = LHS >>
1080 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +00001081 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001082 case BinaryOperator::LT: Result = LHS < RHS; break;
1083 case BinaryOperator::GT: Result = LHS > RHS; break;
1084 case BinaryOperator::LE: Result = LHS <= RHS; break;
1085 case BinaryOperator::GE: Result = LHS >= RHS; break;
1086 case BinaryOperator::EQ: Result = LHS == RHS; break;
1087 case BinaryOperator::NE: Result = LHS != RHS; break;
1088 case BinaryOperator::And: Result = LHS & RHS; break;
1089 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1090 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001091 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001092 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001093 break;
1094 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001095 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001096 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001097
1098 case BinaryOperator::Comma:
1099 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1100 // *except* when they are contained within a subexpression that is not
1101 // evaluated". Note that Assignment can never happen due to constraints
1102 // on the LHS subexpr, so we don't need to check it here.
1103 if (isEvaluated) {
1104 if (Loc) *Loc = getLocStart();
1105 return false;
1106 }
1107
1108 // The result of the constant expr is the RHS.
1109 Result = RHS;
1110 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001111 }
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001112
Chris Lattner4b009652007-07-25 00:24:17 +00001113 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1114 break;
1115 }
1116 case ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +00001117 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001118 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001119 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1120 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +00001121
1122 // C99 6.6p6: shall only convert arithmetic types to integer types.
1123 if (!SubExpr->getType()->isArithmeticType() ||
1124 !getType()->isIntegerType()) {
1125 if (Loc) *Loc = SubExpr->getLocStart();
1126 return false;
1127 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001128
Chris Lattner8cd0e932008-03-05 18:54:05 +00001129 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001130
Chris Lattner4b009652007-07-25 00:24:17 +00001131 // Handle simple integer->integer casts.
1132 if (SubExpr->getType()->isIntegerType()) {
1133 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1134 return false;
1135
1136 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +00001137 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +00001138 if (getType()->isBooleanType()) {
1139 // Conversion to bool compares against zero.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001140 Result = Ctx.MakeIntValue(Result != 0, getType());
1141 } else if (SubExpr->getType()->isSignedIntegerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001142 Result.sextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001143 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1144 } else { // If the input is unsigned, do a zero extend, noop,
1145 // or truncate.
Chris Lattner4b009652007-07-25 00:24:17 +00001146 Result.zextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001147 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1148 }
Chris Lattner4b009652007-07-25 00:24:17 +00001149 break;
1150 }
1151
1152 // Allow floating constants that are the immediate operands of casts or that
1153 // are parenthesized.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001154 const Expr *Operand = SubExpr->IgnoreParens();
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001155
1156 // If this isn't a floating literal, we can't handle it.
1157 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1158 if (!FL) {
1159 if (Loc) *Loc = Operand->getLocStart();
1160 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001161 }
Chris Lattner000c4102008-01-09 18:59:34 +00001162
1163 // If the destination is boolean, compare against zero.
1164 if (getType()->isBooleanType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001165 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
Chris Lattner000c4102008-01-09 18:59:34 +00001166 break;
1167 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001168
1169 // Determine whether we are converting to unsigned or signed.
1170 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +00001171
1172 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1173 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +00001174 uint64_t Space[4];
1175 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +00001176 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001177 llvm::APFloat::rmTowardZero,
1178 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001179 Result = llvm::APInt(DestWidth, 4, Space);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001180 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001181 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001182 }
1183 case ConditionalOperatorClass: {
1184 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1185
Chris Lattner45e71bf2008-12-12 06:55:44 +00001186 const Expr *Cond = Exp->getCond();
1187
1188 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001189 return false;
1190
1191 const Expr *TrueExp = Exp->getLHS();
1192 const Expr *FalseExp = Exp->getRHS();
1193 if (Result == 0) std::swap(TrueExp, FalseExp);
1194
Chris Lattner45e71bf2008-12-12 06:55:44 +00001195 // If the condition (ignoring parens) is a __builtin_constant_p call,
1196 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001197 // expression, and it is fully evaluated. This is an important GNU
1198 // extension. See GCC PR38377 for discussion.
Chris Lattner45e71bf2008-12-12 06:55:44 +00001199 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001200 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001201 EvalResult EVResult;
1202 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1203 return false;
1204 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1205 Result = EVResult.Val.getInt();
1206 if (Loc) *Loc = EVResult.DiagLoc;
1207 return true;
1208 }
Chris Lattner45e71bf2008-12-12 06:55:44 +00001209
Chris Lattner4b009652007-07-25 00:24:17 +00001210 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001211 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001212 return false;
1213 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001214 if (TrueExp &&
1215 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001216 return false;
1217 break;
1218 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001219 case CXXDefaultArgExprClass:
1220 return cast<CXXDefaultArgExpr>(this)
1221 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001222
1223 case UnaryTypeTraitExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001224 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1225 getType());
1226 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001227 }
1228
Chris Lattner4b009652007-07-25 00:24:17 +00001229 return true;
1230}
1231
Chris Lattner4b009652007-07-25 00:24:17 +00001232/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1233/// integer constant expression with the value zero, or if this is one that is
1234/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001235bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1236{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001237 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001238 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001239 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001240 // Check that it is a cast to void*.
1241 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1242 QualType Pointee = PT->getPointeeType();
1243 if (Pointee.getCVRQualifiers() == 0 &&
1244 Pointee->isVoidType() && // to void*
1245 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001246 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001247 }
Chris Lattner4b009652007-07-25 00:24:17 +00001248 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001249 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1250 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001251 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001252 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1253 // Accept ((void*)0) as a null pointer constant, as many other
1254 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001255 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001256 } else if (const CXXDefaultArgExpr *DefaultArg
1257 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001258 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001259 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001260 } else if (isa<GNUNullExpr>(this)) {
1261 // The GNU __null extension is always a null pointer constant.
1262 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001263 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001264
Steve Naroffa2e53222008-01-14 16:10:57 +00001265 // This expression must be an integer type.
1266 if (!getType()->isIntegerType())
1267 return false;
1268
Chris Lattner4b009652007-07-25 00:24:17 +00001269 // If we have an integer constant expression, we need to *evaluate* it and
1270 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001271 // FIXME: We should probably return false if we're compiling in strict mode
1272 // and Diag is not null (this indicates that the value was foldable but not
1273 // an ICE.
1274 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001275 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001276 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001277}
Steve Naroffc11705f2007-07-28 23:10:27 +00001278
Douglas Gregor81c29152008-10-29 00:13:59 +00001279/// isBitField - Return true if this expression is a bit-field.
1280bool Expr::isBitField() {
1281 Expr *E = this->IgnoreParenCasts();
1282 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001283 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1284 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001285 return false;
1286}
1287
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001288/// isArrow - Return true if the base expression is a pointer to vector,
1289/// return false if the base expression is a vector.
1290bool ExtVectorElementExpr::isArrow() const {
1291 return getBase()->getType()->isPointerType();
1292}
1293
Nate Begemanaf6ed502008-04-18 23:10:10 +00001294unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001295 if (const VectorType *VT = getType()->getAsVectorType())
1296 return VT->getNumElements();
1297 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001298}
1299
Nate Begemanc8e51f82008-05-09 06:41:27 +00001300/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001301bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001302 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001303 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001304
1305 // Halving swizzles do not contain duplicate elements.
1306 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1307 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1308 return false;
1309
1310 // Advance past s-char prefix on hex swizzles.
1311 if (*compStr == 's') {
1312 compStr++;
1313 length--;
1314 }
Steve Naroffba67f692007-07-30 03:29:09 +00001315
Chris Lattner58d3fa52008-11-19 07:55:04 +00001316 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001317 const char *s = compStr+i;
1318 for (const char c = *s++; *s; s++)
1319 if (c == *s)
1320 return true;
1321 }
1322 return false;
1323}
Chris Lattner42158e72007-08-02 23:36:59 +00001324
Nate Begemanc8e51f82008-05-09 06:41:27 +00001325/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001326void ExtVectorElementExpr::getEncodedElementAccess(
1327 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001328 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001329 if (*compStr == 's')
1330 compStr++;
1331
1332 bool isHi = !strcmp(compStr, "hi");
1333 bool isLo = !strcmp(compStr, "lo");
1334 bool isEven = !strcmp(compStr, "even");
1335 bool isOdd = !strcmp(compStr, "odd");
1336
Nate Begemanc8e51f82008-05-09 06:41:27 +00001337 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1338 uint64_t Index;
1339
1340 if (isHi)
1341 Index = e + i;
1342 else if (isLo)
1343 Index = i;
1344 else if (isEven)
1345 Index = 2 * i;
1346 else if (isOdd)
1347 Index = 2 * i + 1;
1348 else
1349 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001350
Nate Begemana1ae7442008-05-13 21:03:02 +00001351 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001352 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001353}
1354
Steve Naroff4ed9d662007-09-27 14:38:14 +00001355// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001356ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001357 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001358 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001359 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001360 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001361 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001362 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001363 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001364 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001365 if (NumArgs) {
1366 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001367 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1368 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001369 LBracloc = LBrac;
1370 RBracloc = RBrac;
1371}
1372
Steve Naroff4ed9d662007-09-27 14:38:14 +00001373// constructor for class messages.
1374// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001375ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001376 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001377 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001378 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001379 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001380 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001381 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001382 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001383 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001384 if (NumArgs) {
1385 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001386 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1387 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001388 LBracloc = LBrac;
1389 RBracloc = RBrac;
1390}
1391
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001392// constructor for class messages.
1393ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1394 QualType retType, ObjCMethodDecl *mproto,
1395 SourceLocation LBrac, SourceLocation RBrac,
1396 Expr **ArgExprs, unsigned nargs)
1397: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1398MethodProto(mproto) {
1399 NumArgs = nargs;
1400 SubExprs = new Stmt*[NumArgs+1];
1401 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1402 if (NumArgs) {
1403 for (unsigned i = 0; i != NumArgs; ++i)
1404 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1405 }
1406 LBracloc = LBrac;
1407 RBracloc = RBrac;
1408}
1409
1410ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1411 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1412 switch (x & Flags) {
1413 default:
1414 assert(false && "Invalid ObjCMessageExpr.");
1415 case IsInstMeth:
1416 return ClassInfo(0, 0);
1417 case IsClsMethDeclUnknown:
1418 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1419 case IsClsMethDeclKnown: {
1420 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1421 return ClassInfo(D, D->getIdentifier());
1422 }
1423 }
1424}
1425
Chris Lattnerf624cd22007-10-25 00:29:32 +00001426bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001427 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001428}
1429
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001430static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001431 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1432 QualType Ty = ME->getBase()->getType();
1433
1434 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001435 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001436 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1437 // FIXME: This is linear time. And the fact that we're indexing
1438 // into the layout by position in the record means that we're
1439 // either stuck numbering the fields in the AST or we have to keep
1440 // the linear search (yuck and yuck).
1441 unsigned i = 0;
1442 for (RecordDecl::field_iterator Field = RD->field_begin(),
1443 FieldEnd = RD->field_end();
1444 Field != FieldEnd; (void)++Field, ++i) {
1445 if (*Field == FD)
1446 break;
1447 }
1448
1449 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001450 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001451 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1452 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001453
Chris Lattner8cd0e932008-03-05 18:54:05 +00001454 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001455 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001456
1457 return size + evaluateOffsetOf(C, Base);
1458 } else if (isa<CompoundLiteralExpr>(E))
1459 return 0;
1460
1461 assert(0 && "Unknown offsetof subexpression!");
1462 return 0;
1463}
1464
1465int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1466{
1467 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1468
Chris Lattner8cd0e932008-03-05 18:54:05 +00001469 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001470 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001471}
1472
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001473void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1474 // Override default behavior of traversing children. If this has a type
1475 // operand and the type is a variable-length array, the child iteration
1476 // will iterate over the size expression. However, this expression belongs
1477 // to the type, not to this, so we don't want to delete it.
1478 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001479 if (isArgumentType()) {
1480 this->~SizeOfAlignOfExpr();
1481 C.Deallocate(this);
1482 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001483 else
1484 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001485}
1486
Ted Kremenekf1a67122009-02-09 17:08:14 +00001487void OverloadExpr::Destroy(ASTContext& C) {
1488 DestroyChildren(C);
1489 C.Deallocate(SubExprs);
1490 this->~OverloadExpr();
1491 C.Deallocate(this);
1492}
1493
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001494//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001495// DesignatedInitExpr
1496//===----------------------------------------------------------------------===//
1497
1498IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1499 assert(Kind == FieldDesignator && "Only valid on a field designator");
1500 if (Field.NameOrField & 0x01)
1501 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1502 else
1503 return getField()->getIdentifier();
1504}
1505
1506DesignatedInitExpr *
1507DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1508 unsigned NumDesignators,
1509 Expr **IndexExprs, unsigned NumIndexExprs,
1510 SourceLocation ColonOrEqualLoc,
1511 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001512 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1513 sizeof(Designator) * NumDesignators +
1514 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001515 DesignatedInitExpr *DIE
1516 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1517 ColonOrEqualLoc, UsesColonSyntax,
1518 NumIndexExprs + 1);
1519
1520 // Fill in the designators
1521 unsigned ExpectedNumSubExprs = 0;
1522 designators_iterator Desig = DIE->designators_begin();
1523 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1524 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1525 if (Designators[Idx].isArrayDesignator())
1526 ++ExpectedNumSubExprs;
1527 else if (Designators[Idx].isArrayRangeDesignator())
1528 ExpectedNumSubExprs += 2;
1529 }
1530 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1531
1532 // Fill in the subexpressions, including the initializer expression.
1533 child_iterator Child = DIE->child_begin();
1534 *Child++ = Init;
1535 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1536 *Child = IndexExprs[Idx];
1537
1538 return DIE;
1539}
1540
1541SourceRange DesignatedInitExpr::getSourceRange() const {
1542 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001543 Designator &First =
1544 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001545 if (First.isFieldDesignator()) {
1546 if (UsesColonSyntax)
1547 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1548 else
1549 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1550 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001551 StartLoc =
1552 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001553 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1554}
1555
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001556DesignatedInitExpr::designators_iterator
1557DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001558 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1559 Ptr += sizeof(DesignatedInitExpr);
1560 return static_cast<Designator*>(static_cast<void*>(Ptr));
1561}
1562
1563DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1564 return designators_begin() + NumDesignators;
1565}
1566
1567Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1568 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1569 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1570 Ptr += sizeof(DesignatedInitExpr);
1571 Ptr += sizeof(Designator) * NumDesignators;
1572 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1573 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1574}
1575
1576Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1577 assert(D.Kind == Designator::ArrayRangeDesignator &&
1578 "Requires array range designator");
1579 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1580 Ptr += sizeof(DesignatedInitExpr);
1581 Ptr += sizeof(Designator) * NumDesignators;
1582 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1583 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1584}
1585
1586Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1587 assert(D.Kind == Designator::ArrayRangeDesignator &&
1588 "Requires array range designator");
1589 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1590 Ptr += sizeof(DesignatedInitExpr);
1591 Ptr += sizeof(Designator) * NumDesignators;
1592 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1593 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1594}
1595
1596//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001597// ExprIterator.
1598//===----------------------------------------------------------------------===//
1599
1600Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1601Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1602Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1603const Expr* ConstExprIterator::operator[](size_t idx) const {
1604 return cast<Expr>(I[idx]);
1605}
1606const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1607const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1608
1609//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001610// Child Iterators for iterating over subexpressions/substatements
1611//===----------------------------------------------------------------------===//
1612
1613// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001614Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1615Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001616
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001617// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001618Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1619Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001620
Steve Naroff6f786252008-06-02 23:03:37 +00001621// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001622Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1623Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001624
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001625// ObjCKVCRefExpr
1626Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1627Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1628
Douglas Gregord8606632008-11-04 14:56:14 +00001629// ObjCSuperExpr
1630Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1631Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1632
Chris Lattner69909292008-08-10 01:53:14 +00001633// PredefinedExpr
1634Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1635Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001636
1637// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001638Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1639Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001640
1641// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001642Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001643Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001644
1645// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001646Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1647Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001648
Chris Lattner1de66eb2007-08-26 03:42:43 +00001649// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001650Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1651Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001652
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001653// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001654Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1655Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001656
1657// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001658Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1659Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001660
1661// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001662Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1663Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001664
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001665// SizeOfAlignOfExpr
1666Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1667 // If this is of a type and the type is a VLA type (and not a typedef), the
1668 // size expression of the VLA needs to be treated as an executable expression.
1669 // Why isn't this weirdness documented better in StmtIterator?
1670 if (isArgumentType()) {
1671 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1672 getArgumentType().getTypePtr()))
1673 return child_iterator(T);
1674 return child_iterator();
1675 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001676 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001677}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001678Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1679 if (isArgumentType())
1680 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001681 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001682}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001683
1684// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001685Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001686 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001687}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001688Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001689 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001690}
1691
1692// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001693Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001694 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001695}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001696Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001697 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001698}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001699
1700// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001701Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1702Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001703
Nate Begemanaf6ed502008-04-18 23:10:10 +00001704// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001705Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1706Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001707
1708// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001709Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1710Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001711
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001712// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001713Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1714Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001715
1716// BinaryOperator
1717Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001718 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001719}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001720Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001721 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001722}
1723
1724// ConditionalOperator
1725Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001726 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001727}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001728Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001729 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001730}
1731
1732// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001733Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1734Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001735
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001736// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001737Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1738Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001739
1740// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001741Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1742 return child_iterator();
1743}
1744
1745Stmt::child_iterator TypesCompatibleExpr::child_end() {
1746 return child_iterator();
1747}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001748
1749// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001750Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1751Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001752
Douglas Gregorad4b3792008-11-29 04:51:27 +00001753// GNUNullExpr
1754Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1755Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1756
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001757// OverloadExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001758Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1759Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001760
Eli Friedmand0e9d092008-05-14 19:38:39 +00001761// ShuffleVectorExpr
1762Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001763 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001764}
1765Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001766 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001767}
1768
Anders Carlsson36760332007-10-15 20:28:48 +00001769// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001770Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1771Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001772
Anders Carlsson762b7c72007-08-31 04:56:16 +00001773// InitListExpr
1774Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001775 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001776}
1777Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001778 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001779}
1780
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001781// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001782Stmt::child_iterator DesignatedInitExpr::child_begin() {
1783 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1784 Ptr += sizeof(DesignatedInitExpr);
1785 Ptr += sizeof(Designator) * NumDesignators;
1786 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1787}
1788Stmt::child_iterator DesignatedInitExpr::child_end() {
1789 return child_iterator(&*child_begin() + NumSubExprs);
1790}
1791
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001792// ImplicitValueInitExpr
1793Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1794 return child_iterator();
1795}
1796
1797Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1798 return child_iterator();
1799}
1800
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001801// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001802Stmt::child_iterator ObjCStringLiteral::child_begin() {
1803 return child_iterator();
1804}
1805Stmt::child_iterator ObjCStringLiteral::child_end() {
1806 return child_iterator();
1807}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001808
1809// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001810Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1811Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001812
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001813// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001814Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1815 return child_iterator();
1816}
1817Stmt::child_iterator ObjCSelectorExpr::child_end() {
1818 return child_iterator();
1819}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001820
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001821// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001822Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1823 return child_iterator();
1824}
1825Stmt::child_iterator ObjCProtocolExpr::child_end() {
1826 return child_iterator();
1827}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001828
Steve Naroffc39ca262007-09-18 23:55:05 +00001829// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001830Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001831 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001832}
1833Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001834 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001835}
1836
Steve Naroff52a81c02008-09-03 18:15:37 +00001837// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001838Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1839Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001840
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001841Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1842Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }