blob: 9e0d0cd954deca20c5876c5f2ba98f9c569a3a18 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerda8249e2008-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 Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Ted Kremenek6e94ef52009-02-06 19:55:15 +000041StringLiteral::StringLiteral(ASTContext& C, const char *strData,
42 unsigned byteLength, bool Wide, QualType t,
43 SourceLocation firstLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +000044 SourceLocation lastLoc) :
45 Expr(StringLiteralClass, t) {
46 // OPTIMIZE: could allocate this appended to the StringLiteral.
Ted Kremenek6e94ef52009-02-06 19:55:15 +000047 char *AStrData = new (C, 1) char[byteLength];
Reid Spencer5f016e22007-07-11 17:01:13 +000048 memcpy(AStrData, strData, byteLength);
49 StrData = AStrData;
50 ByteLength = byteLength;
51 IsWide = Wide;
52 firstTokLoc = firstLoc;
53 lastTokLoc = lastLoc;
54}
55
Ted Kremenek6e94ef52009-02-06 19:55:15 +000056void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000057 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000058 this->~StringLiteral();
59 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek5a56ac32008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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";
Reid Spencer5f016e22007-07-11 17:01:13 +000099 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000100 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102}
103
104//===----------------------------------------------------------------------===//
105// Postfix Operators.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek668bf912009-02-09 20:51:47 +0000108CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000109 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000110 : Expr(SC, t,
111 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
112 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
113 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000114
115 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000119
Douglas Gregorb4609802008-11-14 16:09:21 +0000120 RParenLoc = rparenloc;
121}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000122
Ted Kremenek668bf912009-02-09 20:51:47 +0000123CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
124 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000125 : Expr(CallExprClass, t,
126 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
127 fn->isValueDependent() || hasAnyValueDependentArguments(args, numargs)),
128 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000129
130 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000131 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000133 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 RParenLoc = rparenloc;
136}
137
Ted Kremenek668bf912009-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 Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000148void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000155 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Kremenek55499762008-06-17 02:43:46 +0000161 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000169 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000170 SubExprs = NewSubExprs;
171 this->NumArgs = NumArgs;
172}
173
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000176unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000182 return 0;
183
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000184 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
185 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000186 return 0;
187
Anders Carlssonbcba2012008-01-31 02:13:57 +0000188 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
189 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000190 return 0;
191
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000192 if (!FDecl->getIdentifier())
193 return 0;
194
Douglas Gregor3c385e52009-02-14 18:57:46 +0000195 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000196}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000197
Chris Lattnercb888962008-10-06 05:00:53 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson66b5a8a2007-08-31 04:56:16 +0000237InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000238 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000239 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000240 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000241 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000242 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000243
244 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000245}
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
Douglas Gregor4c678342009-01-28 21:54:33 +0000247void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
248 for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); Idx < LastIdx; ++Idx)
249 delete InitExprs[Idx];
250 InitExprs.resize(NumInits, 0);
251}
252
253Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
254 if (Init >= InitExprs.size()) {
255 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
256 InitExprs.back() = expr;
257 return 0;
258 }
259
260 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
261 InitExprs[Init] = expr;
262 return Result;
263}
264
Steve Naroffbfdcae62008-09-04 15:31:07 +0000265/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000266///
267const FunctionType *BlockExpr::getFunctionType() const {
268 return getType()->getAsBlockPointerType()->
269 getPointeeType()->getAsFunctionType();
270}
271
Steve Naroff56ee6892008-10-08 17:01:13 +0000272SourceLocation BlockExpr::getCaretLocation() const {
273 return TheBlock->getCaretLocation();
274}
275const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
276Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
277
278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279//===----------------------------------------------------------------------===//
280// Generic Expression Routines
281//===----------------------------------------------------------------------===//
282
Chris Lattner026dc962009-02-14 07:37:35 +0000283/// isUnusedResultAWarning - Return true if this immediate expression should
284/// be warned about if the result is unused. If so, fill in Loc and Ranges
285/// with location to warn on and the source range[s] to report with the
286/// warning.
287bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
288 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 switch (getStmtClass()) {
290 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000291 Loc = getExprLoc();
292 R1 = getSourceRange();
293 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000295 return cast<ParenExpr>(this)->getSubExpr()->
296 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 case UnaryOperatorClass: {
298 const UnaryOperator *UO = cast<UnaryOperator>(this);
299
300 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000301 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 case UnaryOperator::PostInc:
303 case UnaryOperator::PostDec:
304 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000305 case UnaryOperator::PreDec: // ++/--
306 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 case UnaryOperator::Deref:
308 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000309 if (getType().isVolatileQualified())
310 return false;
311 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 case UnaryOperator::Real:
313 case UnaryOperator::Imag:
314 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000315 if (UO->getSubExpr()->getType().isVolatileQualified())
316 return false;
317 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000319 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 }
Chris Lattner026dc962009-02-14 07:37:35 +0000321 Loc = UO->getOperatorLoc();
322 R1 = UO->getSubExpr()->getSourceRange();
323 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000325 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000326 const BinaryOperator *BO = cast<BinaryOperator>(this);
327 // Consider comma to have side effects if the LHS or RHS does.
328 if (BO->getOpcode() == BinaryOperator::Comma)
329 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
330 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000331
Chris Lattner026dc962009-02-14 07:37:35 +0000332 if (BO->isAssignmentOp())
333 return false;
334 Loc = BO->getOperatorLoc();
335 R1 = BO->getLHS()->getSourceRange();
336 R2 = BO->getRHS()->getSourceRange();
337 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000338 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000339 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000340 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000341
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000342 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000343 // The condition must be evaluated, but if either the LHS or RHS is a
344 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000345 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner026dc962009-02-14 07:37:35 +0000346 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
347 return true;
348 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000349 }
350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000352 // If the base pointer or element is to a volatile pointer/field, accessing
353 // it is a side effect.
354 if (getType().isVolatileQualified())
355 return false;
356 Loc = cast<MemberExpr>(this)->getMemberLoc();
357 R1 = SourceRange(Loc, Loc);
358 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
359 return true;
360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 case ArraySubscriptExprClass:
362 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000363 // it is a side effect.
364 if (getType().isVolatileQualified())
365 return false;
366 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
367 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
368 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
369 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000370
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000372 case CXXOperatorCallExprClass: {
373 // If this is a direct call, get the callee.
374 const CallExpr *CE = cast<CallExpr>(this);
375 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
376 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
377 // If the callee has attribute pure, const, or warn_unused_result, warn
378 // about it. void foo() { strlen("bar"); } should warn.
379 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
380 if (FD->getAttr<WarnUnusedResultAttr>() ||
381 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
382 Loc = CE->getCallee()->getLocStart();
383 R1 = CE->getCallee()->getSourceRange();
384
385 if (unsigned NumArgs = CE->getNumArgs())
386 R2 = SourceRange(CE->getArg(0)->getLocStart(),
387 CE->getArg(NumArgs-1)->getLocEnd());
388 return true;
389 }
390 }
391 return false;
392 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000393 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000394 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000395 case StmtExprClass: {
396 // Statement exprs don't logically have side effects themselves, but are
397 // sometimes used in macros in ways that give them a type that is unused.
398 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
399 // however, if the result of the stmt expr is dead, we don't want to emit a
400 // warning.
401 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
402 if (!CS->body_empty())
403 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000404 return E->isUnusedResultAWarning(Loc, R1, R2);
405
406 Loc = cast<StmtExpr>(this)->getLParenLoc();
407 R1 = getSourceRange();
408 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000409 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000410 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000411 // If this is a cast to void, check the operand. Otherwise, the result of
412 // the cast is unused.
413 if (getType()->isVoidType())
414 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
415 R1, R2);
416 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
417 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
418 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000419 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // If this is a cast to void, check the operand. Otherwise, the result of
421 // the cast is unused.
422 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000423 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
424 R1, R2);
425 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
426 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
427 return true;
428
Eli Friedman4be1f472008-05-19 21:24:43 +0000429 case ImplicitCastExprClass:
430 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000431 return cast<ImplicitCastExpr>(this)
432 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000433
Chris Lattner04421082008-04-08 04:40:51 +0000434 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000435 return cast<CXXDefaultArgExpr>(this)
436 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000437
438 case CXXNewExprClass:
439 // FIXME: In theory, there might be new expressions that don't have side
440 // effects (e.g. a placement new with an uninitialized POD).
441 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000442 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000443 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000444}
445
Douglas Gregorba7e2102008-10-22 15:04:37 +0000446/// DeclCanBeLvalue - Determine whether the given declaration can be
447/// an lvalue. This is a helper routine for isLvalue.
448static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000449 // C++ [temp.param]p6:
450 // A non-type non-reference template-parameter is not an lvalue.
451 if (const NonTypeTemplateParmDecl *NTTParm
452 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
453 return NTTParm->getType()->isReferenceType();
454
Douglas Gregor44b43212008-12-11 16:49:14 +0000455 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000456 // C++ 3.10p2: An lvalue refers to an object or function.
457 (Ctx.getLangOptions().CPlusPlus &&
458 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
459}
460
Reid Spencer5f016e22007-07-11 17:01:13 +0000461/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
462/// incomplete type other than void. Nonarray expressions that can be lvalues:
463/// - name, where name must be a variable
464/// - e[i]
465/// - (e), where e must be an lvalue
466/// - e.name, where e must be an lvalue
467/// - e->name
468/// - *e, the type of e cannot be a function type
469/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000470/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000471/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000472///
Chris Lattner28be73f2008-07-26 21:30:36 +0000473Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000474 // first, check the type (C99 6.3.2.1). Expressions with function
475 // type in C are not lvalues, but they can be lvalues in C++.
476 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 return LV_NotObjectType;
478
Steve Naroffacb818a2008-02-10 01:39:04 +0000479 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000480 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000481 return LV_IncompleteVoidType;
482
Douglas Gregor98cd5992008-10-21 23:43:52 +0000483 /// FIXME: Expressions can't have reference type, so the following
484 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000485 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000486 return LV_Valid;
487
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 // the type looks fine, now check the expression
489 switch (getStmtClass()) {
490 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000491 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
493 // For vectors, make sure base is an lvalue (i.e. not a function call).
494 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000495 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000497 case DeclRefExprClass:
498 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000499 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
500 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 return LV_Valid;
502 break;
Chris Lattner41110242008-06-17 18:05:57 +0000503 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000504 case BlockDeclRefExprClass: {
505 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000506 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000507 return LV_Valid;
508 break;
509 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000510 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000512 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
513 NamedDecl *Member = m->getMemberDecl();
514 // C++ [expr.ref]p4:
515 // If E2 is declared to have type "reference to T", then E1.E2
516 // is an lvalue.
517 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
518 if (Value->getType()->isReferenceType())
519 return LV_Valid;
520
521 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
522 if (isa<CXXClassVarDecl>(Member))
523 return LV_Valid;
524
525 // -- If E2 is a non-static data member [...]. If E1 is an
526 // lvalue, then E1.E2 is an lvalue.
527 if (isa<FieldDecl>(Member))
528 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
529
530 // -- If it refers to a static member function [...], then
531 // E1.E2 is an lvalue.
532 // -- Otherwise, if E1.E2 refers to a non-static member
533 // function [...], then E1.E2 is not an lvalue.
534 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
535 return Method->isStatic()? LV_Valid : LV_MemberFunction;
536
537 // -- If E2 is a member enumerator [...], the expression E1.E2
538 // is not an lvalue.
539 if (isa<EnumConstantDecl>(Member))
540 return LV_InvalidExpression;
541
542 // Not an lvalue.
543 return LV_InvalidExpression;
544 }
545
546 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000547 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000548 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000549 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000551 return LV_Valid; // C99 6.5.3p4
552
553 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000554 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
555 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000556 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000557
558 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
559 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
560 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
561 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000563 case ImplicitCastExprClass:
564 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
565 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000567 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000568 case BinaryOperatorClass:
569 case CompoundAssignOperatorClass: {
570 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000571
572 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
573 BinOp->getOpcode() == BinaryOperator::Comma)
574 return BinOp->getRHS()->isLvalue(Ctx);
575
Sebastian Redl22460502009-02-07 00:15:38 +0000576 // C++ [expr.mptr.oper]p6
577 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
578 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
579 !BinOp->getType()->isFunctionType())
580 return BinOp->getLHS()->isLvalue(Ctx);
581
Douglas Gregorbf3af052008-11-13 20:12:29 +0000582 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000583 return LV_InvalidExpression;
584
Douglas Gregorbf3af052008-11-13 20:12:29 +0000585 if (Ctx.getLangOptions().CPlusPlus)
586 // C++ [expr.ass]p1:
587 // The result of an assignment operation [...] is an lvalue.
588 return LV_Valid;
589
590
591 // C99 6.5.16:
592 // An assignment expression [...] is not an lvalue.
593 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000594 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000595 // FIXME: OverloadExprClass
Douglas Gregorb4609802008-11-14 16:09:21 +0000596 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000597 case CXXOperatorCallExprClass:
598 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000599 // C++ [expr.call]p10:
600 // A function call is an lvalue if and only if the result type
601 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000602 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000603 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000604 CalleeType = FnTypePtr->getPointeeType();
605 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
606 if (FnType->getResultType()->isReferenceType())
607 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000608
609 break;
610 }
Steve Naroffe6386392007-12-05 04:00:10 +0000611 case CompoundLiteralExprClass: // C99 6.5.2.5p5
612 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000613 case ChooseExprClass:
614 // __builtin_choose_expr is an lvalue if the selected operand is.
615 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
616 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
617 else
618 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
619
Nate Begeman213541a2008-04-18 23:10:10 +0000620 case ExtVectorElementExprClass:
621 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000622 return LV_DuplicateVectorComponents;
623 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000624 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
625 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000626 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
627 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000628 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000629 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000630 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000631 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000632 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000633 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000634 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000635 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000636 case CXXConditionDeclExprClass:
637 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000638 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000639 case CXXFunctionalCastExprClass:
640 case CXXStaticCastExprClass:
641 case CXXDynamicCastExprClass:
642 case CXXReinterpretCastExprClass:
643 case CXXConstCastExprClass:
644 // The result of an explicit cast is an lvalue if the type we are
645 // casting to is a reference type. See C++ [expr.cast]p1,
646 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
647 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
648 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
649 return LV_Valid;
650 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000651 case CXXTypeidExprClass:
652 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
653 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 default:
655 break;
656 }
657 return LV_InvalidExpression;
658}
659
660/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
661/// does not have an incomplete type, does not have a const-qualified type, and
662/// if it is a structure or union, does not have any member (including,
663/// recursively, any member or element of all contained aggregates or unions)
664/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000665Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
666 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000667
668 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000669 case LV_Valid:
670 // C++ 3.10p11: Functions cannot be modified, but pointers to
671 // functions can be modifiable.
672 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
673 return MLV_NotObjectType;
674 break;
675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 case LV_NotObjectType: return MLV_NotObjectType;
677 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000678 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000679 case LV_InvalidExpression:
680 // If the top level is a C-style cast, and the subexpression is a valid
681 // lvalue, then this is probably a use of the old-school "cast as lvalue"
682 // GCC extension. We don't support it, but we want to produce good
683 // diagnostics when it happens so that the user knows why.
684 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
685 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
686 return MLV_LValueCast;
687 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000688 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000690
691 QualType CT = Ctx.getCanonicalType(getType());
692
693 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000695 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000697 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 return MLV_IncompleteType;
699
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000700 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 if (r->hasConstFields())
702 return MLV_ConstQualified;
703 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000704 // The following is illegal:
705 // void takeclosure(void (^C)(void));
706 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
707 //
708 if (getStmtClass() == BlockDeclRefExprClass) {
709 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
710 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
711 return MLV_NotBlockQualified;
712 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000713
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000714 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000715 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000716 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
717 if (KVCExpr->getSetterMethod() == 0)
718 return MLV_NoSetterProperty;
719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 return MLV_Valid;
721}
722
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000723/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000724/// duration. This means that the address of this expression is a link-time
725/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000726bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000727 switch (getStmtClass()) {
728 default:
729 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000730 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000731 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000732 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000733 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000734 case CompoundLiteralExprClass:
735 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000736 case DeclRefExprClass:
737 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000738 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
739 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000740 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000741 if (isa<FunctionDecl>(D))
742 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000743 return false;
744 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000745 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000746 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000747 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000748 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000749 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000750 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000751 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000752 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000753 case CXXDefaultArgExprClass:
754 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000755 }
756}
757
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000758Expr* Expr::IgnoreParens() {
759 Expr* E = this;
760 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
761 E = P->getSubExpr();
762
763 return E;
764}
765
Chris Lattner56f34942008-02-13 01:02:39 +0000766/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
767/// or CastExprs or ImplicitCastExprs, returning their operand.
768Expr *Expr::IgnoreParenCasts() {
769 Expr *E = this;
770 while (true) {
771 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
772 E = P->getSubExpr();
773 else if (CastExpr *P = dyn_cast<CastExpr>(E))
774 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000775 else
776 return E;
777 }
778}
779
Douglas Gregor898574e2008-12-05 23:32:09 +0000780/// hasAnyTypeDependentArguments - Determines if any of the expressions
781/// in Exprs is type-dependent.
782bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
783 for (unsigned I = 0; I < NumExprs; ++I)
784 if (Exprs[I]->isTypeDependent())
785 return true;
786
787 return false;
788}
789
790/// hasAnyValueDependentArguments - Determines if any of the expressions
791/// in Exprs is value-dependent.
792bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
793 for (unsigned I = 0; I < NumExprs; ++I)
794 if (Exprs[I]->isValueDependent())
795 return true;
796
797 return false;
798}
799
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000800bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000801 // This function is attempting whether an expression is an initializer
802 // which can be evaluated at compile-time. isEvaluatable handles most
803 // of the cases, but it can't deal with some initializer-specific
804 // expressions, and it can't deal with aggregates; we deal with those here,
805 // and fall back to isEvaluatable for the other cases.
806
Anders Carlssone8a32b82008-11-24 05:23:59 +0000807 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000808 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000809 case StringLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000810 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000811 case CompoundLiteralExprClass: {
812 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000813 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000814 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000815 case InitListExprClass: {
816 const InitListExpr *Exp = cast<InitListExpr>(this);
817 unsigned numInits = Exp->getNumInits();
818 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000819 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000820 return false;
821 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000822 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000823 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000824 case ImplicitValueInitExprClass:
825 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000826 case ParenExprClass: {
827 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
828 }
829 case UnaryOperatorClass: {
830 const UnaryOperator* Exp = cast<UnaryOperator>(this);
831 if (Exp->getOpcode() == UnaryOperator::Extension)
832 return Exp->getSubExpr()->isConstantInitializer(Ctx);
833 break;
834 }
835 case CStyleCastExprClass:
836 // Handle casts with a destination that's a struct or union; this
837 // deals with both the gcc no-op struct cast extension and the
838 // cast-to-union extension.
839 if (getType()->isRecordType())
840 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
841 break;
Eli Friedman32a311e2009-01-25 03:27:40 +0000842 case DesignatedInitExprClass:
Sebastian Redl4e716e02009-01-25 13:34:47 +0000843 return cast<DesignatedInitExpr>(this)->
844 getInit()->isConstantInitializer(Ctx);
Anders Carlssone8a32b82008-11-24 05:23:59 +0000845 }
846
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000847 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000848}
849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850/// isIntegerConstantExpr - this recursive routine will test if an expression is
851/// an integer constant expression. Note: With the introduction of VLA's in
852/// C99 the result of the sizeof operator is no longer always a constant
853/// expression. The generalization of the wording to include any subexpression
854/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
855/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopes5f6b6322008-07-08 21:13:06 +0000856/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Reid Spencer5f016e22007-07-11 17:01:13 +0000857/// occurring in a context requiring a constant, would have been a constraint
858/// violation. FIXME: This routine currently implements C90 semantics.
859/// To properly implement C99 semantics this routine will need to evaluate
860/// expressions involving operators previously mentioned.
861
862/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
863/// comma, etc
864///
865/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000866/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000867///
868/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
869/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
870/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000871bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
872 SourceLocation *Loc, bool isEvaluated) const {
Eli Friedmana6afa762008-11-13 06:09:17 +0000873 // Pretest for integral type; some parts of the code crash for types that
874 // can't be sized.
875 if (!getType()->isIntegralType()) {
876 if (Loc) *Loc = getLocStart();
877 return false;
878 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 switch (getStmtClass()) {
880 default:
881 if (Loc) *Loc = getLocStart();
882 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 case ParenExprClass:
884 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000885 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 case IntegerLiteralClass:
887 Result = cast<IntegerLiteral>(this)->getValue();
888 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000889 case CharacterLiteralClass: {
890 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000891 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000892 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000893 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000895 }
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000896 case CXXBoolLiteralExprClass: {
897 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
898 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
899 Result = BL->getValue();
900 Result.setIsUnsigned(!getType()->isSignedIntegerType());
901 break;
902 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000903 case CXXZeroInitValueExprClass:
904 Result.clear();
905 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000906 case TypesCompatibleExprClass: {
907 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000908 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Daniel Dunbarac620de2008-10-24 08:07:57 +0000909 // Per gcc docs "this built-in function ignores top level
910 // qualifiers". We need to use the canonical version to properly
911 // be able to strip CRV qualifiers from the type.
912 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
913 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
914 Result = Ctx.typesAreCompatible(T0.getUnqualifiedType(),
915 T1.getUnqualifiedType());
Steve Naroff389cecc2007-08-02 00:13:27 +0000916 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000917 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000918 case CallExprClass:
919 case CXXOperatorCallExprClass: {
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000920 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner98be4942008-03-05 18:54:05 +0000921 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera4d55d82008-10-06 06:40:35 +0000922
923 // If this is a call to a builtin function, constant fold it otherwise
924 // reject it.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000925 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +0000926 EvalResult EvalResult;
927 if (CE->Evaluate(EvalResult, Ctx)) {
928 assert(!EvalResult.HasSideEffects &&
929 "Foldable builtin call should not have side effects!");
930 Result = EvalResult.Val.getInt();
Chris Lattnera4d55d82008-10-06 06:40:35 +0000931 break; // It is a constant, expand it.
932 }
933 }
934
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000935 if (Loc) *Loc = getLocStart();
936 return false;
937 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000939 case QualifiedDeclRefExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 if (const EnumConstantDecl *D =
941 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
942 Result = D->getInitVal();
943 break;
944 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000945 if (Ctx.getLangOptions().CPlusPlus &&
946 getType().getCVRQualifiers() == QualType::Const) {
947 // C++ 7.1.5.1p2
948 // A variable of non-volatile const-qualified integral or enumeration
949 // type initialized by an ICE can be used in ICEs.
950 if (const VarDecl *Dcl =
951 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
952 if (const Expr *Init = Dcl->getInit())
953 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
954 }
955 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 if (Loc) *Loc = getLocStart();
957 return false;
958 case UnaryOperatorClass: {
959 const UnaryOperator *Exp = cast<UnaryOperator>(this);
960
Sebastian Redl05189992008-11-11 17:56:53 +0000961 // Get the operand value. If this is offsetof, do not evalute the
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // operand. This affects C99 6.6p3.
Sebastian Redl05189992008-11-11 17:56:53 +0000963 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
964 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 return false;
966
967 switch (Exp->getOpcode()) {
968 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
969 // See C99 6.6p3.
970 default:
971 if (Loc) *Loc = Exp->getOperatorLoc();
972 return false;
973 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000974 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 case UnaryOperator::LNot: {
Chris Lattnerbf755382008-01-25 19:16:19 +0000976 bool Val = Result == 0;
Chris Lattner98be4942008-03-05 18:54:05 +0000977 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 Result = Val;
979 break;
980 }
981 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 break;
983 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 Result = -Result;
985 break;
986 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 Result = ~Result;
988 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000989 case UnaryOperator::OffsetOf:
Daniel Dunbaraa1f9f12008-08-28 18:42:20 +0000990 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000991 Result = Exp->evaluateOffsetOf(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 }
993 break;
994 }
Sebastian Redl05189992008-11-11 17:56:53 +0000995 case SizeOfAlignOfExprClass: {
996 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattnera269ebf2008-02-21 05:45:29 +0000997
998 // Return the result in the right width.
Chris Lattner98be4942008-03-05 18:54:05 +0000999 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattnera269ebf2008-02-21 05:45:29 +00001000
Sebastian Redl05189992008-11-11 17:56:53 +00001001 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattnera269ebf2008-02-21 05:45:29 +00001002 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl05189992008-11-11 17:56:53 +00001003 if (ArgTy->isVoidType()) {
Chris Lattnera269ebf2008-02-21 05:45:29 +00001004 Result = 1;
1005 break;
1006 }
1007
1008 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl05189992008-11-11 17:56:53 +00001009 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattner65383472007-12-18 07:15:40 +00001010 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 return false;
Chris Lattner65383472007-12-18 07:15:40 +00001012 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001013
Chris Lattner76e773a2007-07-18 18:38:36 +00001014 // Get information about the size or align.
Sebastian Redl05189992008-11-11 17:56:53 +00001015 if (ArgTy->isFunctionType()) {
Chris Lattnerefdd1572008-01-02 21:54:09 +00001016 // GCC extension: sizeof(function) = 1.
1017 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001018 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00001019 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001020 if (Exp->isSizeOf())
Sebastian Redl05189992008-11-11 17:56:53 +00001021 Result = Ctx.getTypeSize(ArgTy) / CharSize;
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001022 else
Sebastian Redl05189992008-11-11 17:56:53 +00001023 Result = Ctx.getTypeAlign(ArgTy) / CharSize;
Ted Kremenek060e4702007-12-17 17:38:43 +00001024 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 break;
1026 }
1027 case BinaryOperatorClass: {
1028 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbare1226d22008-09-22 23:53:24 +00001029 llvm::APSInt LHS, RHS;
1030
1031 // Initialize result to have correct signedness and width.
1032 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
Eli Friedmanb11e7782008-11-13 02:13:11 +00001033 !getType()->isSignedIntegerType());
1034
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbare1226d22008-09-22 23:53:24 +00001036 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 return false;
1038
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 // The short-circuiting &&/|| operators don't necessarily evaluate their
1040 // RHS. Make sure to pass isEvaluated down correctly.
1041 if (Exp->isLogicalOp()) {
1042 bool RHSEval;
1043 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbare1226d22008-09-22 23:53:24 +00001044 RHSEval = LHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 else {
1046 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbare1226d22008-09-22 23:53:24 +00001047 RHSEval = LHS == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 }
1049
Chris Lattner590b6642007-07-15 23:26:56 +00001050 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 isEvaluated & RHSEval))
1052 return false;
1053 } else {
Chris Lattner590b6642007-07-15 23:26:56 +00001054 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 return false;
1056 }
1057
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 switch (Exp->getOpcode()) {
1059 default:
1060 if (Loc) *Loc = getLocStart();
1061 return false;
1062 case BinaryOperator::Mul:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001063 Result = LHS * RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 break;
1065 case BinaryOperator::Div:
1066 if (RHS == 0) {
1067 if (!isEvaluated) break;
1068 if (Loc) *Loc = getLocStart();
1069 return false;
1070 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001071 Result = LHS / RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 break;
1073 case BinaryOperator::Rem:
1074 if (RHS == 0) {
1075 if (!isEvaluated) break;
1076 if (Loc) *Loc = getLocStart();
1077 return false;
1078 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001079 Result = LHS % RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001081 case BinaryOperator::Add: Result = LHS + RHS; break;
1082 case BinaryOperator::Sub: Result = LHS - RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 case BinaryOperator::Shl:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001084 Result = LHS <<
1085 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1086 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 case BinaryOperator::Shr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001088 Result = LHS >>
1089 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001091 case BinaryOperator::LT: Result = LHS < RHS; break;
1092 case BinaryOperator::GT: Result = LHS > RHS; break;
1093 case BinaryOperator::LE: Result = LHS <= RHS; break;
1094 case BinaryOperator::GE: Result = LHS >= RHS; break;
1095 case BinaryOperator::EQ: Result = LHS == RHS; break;
1096 case BinaryOperator::NE: Result = LHS != RHS; break;
1097 case BinaryOperator::And: Result = LHS & RHS; break;
1098 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1099 case BinaryOperator::Or: Result = LHS | RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 case BinaryOperator::LAnd:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001101 Result = LHS != 0 && RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 break;
1103 case BinaryOperator::LOr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001104 Result = LHS != 0 || RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +00001106
1107 case BinaryOperator::Comma:
1108 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1109 // *except* when they are contained within a subexpression that is not
1110 // evaluated". Note that Assignment can never happen due to constraints
1111 // on the LHS subexpr, so we don't need to check it here.
1112 if (isEvaluated) {
1113 if (Loc) *Loc = getLocStart();
1114 return false;
1115 }
1116
1117 // The result of the constant expr is the RHS.
1118 Result = RHS;
1119 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 }
1121
1122 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1123 break;
1124 }
Chris Lattner26dc7b32007-07-15 23:54:50 +00001125 case ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001126 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001127 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001128 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1129 SourceLocation CastLoc = getLocStart();
Chris Lattner26dc7b32007-07-15 23:54:50 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001132 if (!SubExpr->getType()->isArithmeticType() ||
1133 !getType()->isIntegerType()) {
1134 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 return false;
1136 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001137
Chris Lattner98be4942008-03-05 18:54:05 +00001138 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner987b15d2007-09-22 19:04:13 +00001139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001141 if (SubExpr->getType()->isIntegerType()) {
1142 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +00001144
1145 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001146 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001147 if (getType()->isBooleanType()) {
1148 // Conversion to bool compares against zero.
1149 Result = Result != 0;
1150 Result.zextOrTrunc(DestWidth);
1151 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner26dc7b32007-07-15 23:54:50 +00001152 Result.sextOrTrunc(DestWidth);
1153 else // If the input is unsigned, do a zero extend, noop, or truncate.
1154 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 break;
1156 }
1157
1158 // Allow floating constants that are the immediate operands of casts or that
1159 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001160 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
1162 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +00001163
1164 // If this isn't a floating literal, we can't handle it.
1165 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1166 if (!FL) {
1167 if (Loc) *Loc = Operand->getLocStart();
1168 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001170
1171 // If the destination is boolean, compare against zero.
1172 if (getType()->isBooleanType()) {
1173 Result = !FL->getValue().isZero();
1174 Result.zextOrTrunc(DestWidth);
1175 break;
1176 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001177
1178 // Determine whether we are converting to unsigned or signed.
1179 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +00001180
1181 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1182 // be called multiple times per AST.
Dale Johannesenee5a7002008-10-09 23:02:32 +00001183 uint64_t Space[4];
1184 bool ignored;
Chris Lattnerccc213f2007-09-26 00:47:26 +00001185 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +00001186 llvm::APFloat::rmTowardZero,
1187 &ignored);
Chris Lattner987b15d2007-09-22 19:04:13 +00001188 Result = llvm::APInt(DestWidth, 4, Space);
1189 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 }
1191 case ConditionalOperatorClass: {
1192 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1193
Chris Lattner28daa532008-12-12 06:55:44 +00001194 const Expr *Cond = Exp->getCond();
1195
1196 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 return false;
1198
1199 const Expr *TrueExp = Exp->getLHS();
1200 const Expr *FalseExp = Exp->getRHS();
1201 if (Result == 0) std::swap(TrueExp, FalseExp);
1202
Chris Lattner28daa532008-12-12 06:55:44 +00001203 // If the condition (ignoring parens) is a __builtin_constant_p call,
1204 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001205 // expression, and it is fully evaluated. This is an important GNU
1206 // extension. See GCC PR38377 for discussion.
Chris Lattner28daa532008-12-12 06:55:44 +00001207 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001208 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner42b83dd2008-12-12 18:00:51 +00001209 EvalResult EVResult;
1210 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1211 return false;
1212 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1213 Result = EVResult.Val.getInt();
1214 if (Loc) *Loc = EVResult.DiagLoc;
1215 return true;
1216 }
Chris Lattner28daa532008-12-12 06:55:44 +00001217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001219 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 return false;
1221 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +00001222 if (TrueExp &&
1223 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 break;
1226 }
Chris Lattner04421082008-04-08 04:40:51 +00001227 case CXXDefaultArgExprClass:
1228 return cast<CXXDefaultArgExpr>(this)
1229 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001230
1231 case UnaryTypeTraitExprClass:
1232 Result = cast<UnaryTypeTraitExpr>(this)->Evaluate();
1233 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
1235
1236 // Cases that are valid constant exprs fall through to here.
1237 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1238 return true;
1239}
1240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1242/// integer constant expression with the value zero, or if this is one that is
1243/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001244bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1245{
Sebastian Redl07779722008-10-31 14:43:28 +00001246 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001247 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001248 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001249 // Check that it is a cast to void*.
1250 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1251 QualType Pointee = PT->getPointeeType();
1252 if (Pointee.getCVRQualifiers() == 0 &&
1253 Pointee->isVoidType() && // to void*
1254 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001255 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001256 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001258 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1259 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001260 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001261 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1262 // Accept ((void*)0) as a null pointer constant, as many other
1263 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001264 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001265 } else if (const CXXDefaultArgExpr *DefaultArg
1266 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001267 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001268 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001269 } else if (isa<GNUNullExpr>(this)) {
1270 // The GNU __null extension is always a null pointer constant.
1271 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001272 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001273
Steve Naroffaa58f002008-01-14 16:10:57 +00001274 // This expression must be an integer type.
1275 if (!getType()->isIntegerType())
1276 return false;
1277
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 // If we have an integer constant expression, we need to *evaluate* it and
1279 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001280 // FIXME: We should probably return false if we're compiling in strict mode
1281 // and Diag is not null (this indicates that the value was foldable but not
1282 // an ICE.
1283 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001284 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001285 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001286}
Steve Naroff31a45842007-07-28 23:10:27 +00001287
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001288/// isBitField - Return true if this expression is a bit-field.
1289bool Expr::isBitField() {
1290 Expr *E = this->IgnoreParenCasts();
1291 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001292 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1293 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001294 return false;
1295}
1296
Chris Lattner2140e902009-02-16 22:14:05 +00001297/// isArrow - Return true if the base expression is a pointer to vector,
1298/// return false if the base expression is a vector.
1299bool ExtVectorElementExpr::isArrow() const {
1300 return getBase()->getType()->isPointerType();
1301}
1302
Nate Begeman213541a2008-04-18 23:10:10 +00001303unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001304 if (const VectorType *VT = getType()->getAsVectorType())
1305 return VT->getNumElements();
1306 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001307}
1308
Nate Begeman8a997642008-05-09 06:41:27 +00001309/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001310bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001311 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001312 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001313
1314 // Halving swizzles do not contain duplicate elements.
1315 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1316 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1317 return false;
1318
1319 // Advance past s-char prefix on hex swizzles.
1320 if (*compStr == 's') {
1321 compStr++;
1322 length--;
1323 }
Steve Narofffec0b492007-07-30 03:29:09 +00001324
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001325 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001326 const char *s = compStr+i;
1327 for (const char c = *s++; *s; s++)
1328 if (c == *s)
1329 return true;
1330 }
1331 return false;
1332}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001333
Nate Begeman8a997642008-05-09 06:41:27 +00001334/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001335void ExtVectorElementExpr::getEncodedElementAccess(
1336 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001337 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001338 if (*compStr == 's')
1339 compStr++;
1340
1341 bool isHi = !strcmp(compStr, "hi");
1342 bool isLo = !strcmp(compStr, "lo");
1343 bool isEven = !strcmp(compStr, "even");
1344 bool isOdd = !strcmp(compStr, "odd");
1345
Nate Begeman8a997642008-05-09 06:41:27 +00001346 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1347 uint64_t Index;
1348
1349 if (isHi)
1350 Index = e + i;
1351 else if (isLo)
1352 Index = i;
1353 else if (isEven)
1354 Index = 2 * i;
1355 else if (isOdd)
1356 Index = 2 * i + 1;
1357 else
1358 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001359
Nate Begeman3b8d1162008-05-13 21:03:02 +00001360 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001361 }
Nate Begeman8a997642008-05-09 06:41:27 +00001362}
1363
Steve Naroff68d331a2007-09-27 14:38:14 +00001364// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001365ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001367 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001368 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001369 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001370 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001371 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001372 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001373 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001374 if (NumArgs) {
1375 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001376 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1377 }
Steve Naroff563477d2007-09-18 23:55:05 +00001378 LBracloc = LBrac;
1379 RBracloc = RBrac;
1380}
1381
Steve Naroff68d331a2007-09-27 14:38:14 +00001382// constructor for class messages.
1383// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001384ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001385 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001386 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001387 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001388 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001389 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001390 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001391 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001392 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001393 if (NumArgs) {
1394 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001395 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1396 }
Steve Naroff563477d2007-09-18 23:55:05 +00001397 LBracloc = LBrac;
1398 RBracloc = RBrac;
1399}
1400
Ted Kremenek4df728e2008-06-24 15:50:53 +00001401// constructor for class messages.
1402ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1403 QualType retType, ObjCMethodDecl *mproto,
1404 SourceLocation LBrac, SourceLocation RBrac,
1405 Expr **ArgExprs, unsigned nargs)
1406: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1407MethodProto(mproto) {
1408 NumArgs = nargs;
1409 SubExprs = new Stmt*[NumArgs+1];
1410 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1411 if (NumArgs) {
1412 for (unsigned i = 0; i != NumArgs; ++i)
1413 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1414 }
1415 LBracloc = LBrac;
1416 RBracloc = RBrac;
1417}
1418
1419ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1420 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1421 switch (x & Flags) {
1422 default:
1423 assert(false && "Invalid ObjCMessageExpr.");
1424 case IsInstMeth:
1425 return ClassInfo(0, 0);
1426 case IsClsMethDeclUnknown:
1427 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1428 case IsClsMethDeclKnown: {
1429 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1430 return ClassInfo(D, D->getIdentifier());
1431 }
1432 }
1433}
1434
Chris Lattner27437ca2007-10-25 00:29:32 +00001435bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001436 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001437}
1438
Chris Lattner670a62c2008-12-12 05:35:08 +00001439static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001440 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1441 QualType Ty = ME->getBase()->getType();
1442
1443 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner98be4942008-03-05 18:54:05 +00001444 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +00001445 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1446 // FIXME: This is linear time. And the fact that we're indexing
1447 // into the layout by position in the record means that we're
1448 // either stuck numbering the fields in the AST or we have to keep
1449 // the linear search (yuck and yuck).
1450 unsigned i = 0;
1451 for (RecordDecl::field_iterator Field = RD->field_begin(),
1452 FieldEnd = RD->field_end();
1453 Field != FieldEnd; (void)++Field, ++i) {
1454 if (*Field == FD)
1455 break;
1456 }
1457
1458 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001459 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001460 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1461 const Expr *Base = ASE->getBase();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001462
Chris Lattner98be4942008-03-05 18:54:05 +00001463 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001464 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001465
1466 return size + evaluateOffsetOf(C, Base);
1467 } else if (isa<CompoundLiteralExpr>(E))
1468 return 0;
1469
1470 assert(0 && "Unknown offsetof subexpression!");
1471 return 0;
1472}
1473
1474int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1475{
1476 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1477
Chris Lattner98be4942008-03-05 18:54:05 +00001478 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek55499762008-06-17 02:43:46 +00001479 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001480}
1481
Sebastian Redl05189992008-11-11 17:56:53 +00001482void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1483 // Override default behavior of traversing children. If this has a type
1484 // operand and the type is a variable-length array, the child iteration
1485 // will iterate over the size expression. However, this expression belongs
1486 // to the type, not to this, so we don't want to delete it.
1487 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001488 if (isArgumentType()) {
1489 this->~SizeOfAlignOfExpr();
1490 C.Deallocate(this);
1491 }
Sebastian Redl05189992008-11-11 17:56:53 +00001492 else
1493 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001494}
1495
Ted Kremenekfb7413f2009-02-09 17:08:14 +00001496void OverloadExpr::Destroy(ASTContext& C) {
1497 DestroyChildren(C);
1498 C.Deallocate(SubExprs);
1499 this->~OverloadExpr();
1500 C.Deallocate(this);
1501}
1502
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001503//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001504// DesignatedInitExpr
1505//===----------------------------------------------------------------------===//
1506
1507IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1508 assert(Kind == FieldDesignator && "Only valid on a field designator");
1509 if (Field.NameOrField & 0x01)
1510 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1511 else
1512 return getField()->getIdentifier();
1513}
1514
1515DesignatedInitExpr *
1516DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1517 unsigned NumDesignators,
1518 Expr **IndexExprs, unsigned NumIndexExprs,
1519 SourceLocation ColonOrEqualLoc,
1520 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001521 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1522 sizeof(Designator) * NumDesignators +
1523 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001524 DesignatedInitExpr *DIE
1525 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1526 ColonOrEqualLoc, UsesColonSyntax,
1527 NumIndexExprs + 1);
1528
1529 // Fill in the designators
1530 unsigned ExpectedNumSubExprs = 0;
1531 designators_iterator Desig = DIE->designators_begin();
1532 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1533 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1534 if (Designators[Idx].isArrayDesignator())
1535 ++ExpectedNumSubExprs;
1536 else if (Designators[Idx].isArrayRangeDesignator())
1537 ExpectedNumSubExprs += 2;
1538 }
1539 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1540
1541 // Fill in the subexpressions, including the initializer expression.
1542 child_iterator Child = DIE->child_begin();
1543 *Child++ = Init;
1544 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1545 *Child = IndexExprs[Idx];
1546
1547 return DIE;
1548}
1549
1550SourceRange DesignatedInitExpr::getSourceRange() const {
1551 SourceLocation StartLoc;
1552 Designator &First = *const_cast<DesignatedInitExpr*>(this)->designators_begin();
1553 if (First.isFieldDesignator()) {
1554 if (UsesColonSyntax)
1555 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1556 else
1557 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1558 } else
1559 StartLoc = SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
1560 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1561}
1562
1563DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_begin() {
1564 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1565 Ptr += sizeof(DesignatedInitExpr);
1566 return static_cast<Designator*>(static_cast<void*>(Ptr));
1567}
1568
1569DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1570 return designators_begin() + NumDesignators;
1571}
1572
1573Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1574 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1575 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1576 Ptr += sizeof(DesignatedInitExpr);
1577 Ptr += sizeof(Designator) * NumDesignators;
1578 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1579 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1580}
1581
1582Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1583 assert(D.Kind == Designator::ArrayRangeDesignator &&
1584 "Requires array range designator");
1585 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1586 Ptr += sizeof(DesignatedInitExpr);
1587 Ptr += sizeof(Designator) * NumDesignators;
1588 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1589 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1590}
1591
1592Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1593 assert(D.Kind == Designator::ArrayRangeDesignator &&
1594 "Requires array range designator");
1595 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1596 Ptr += sizeof(DesignatedInitExpr);
1597 Ptr += sizeof(Designator) * NumDesignators;
1598 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1599 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1600}
1601
1602//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001603// ExprIterator.
1604//===----------------------------------------------------------------------===//
1605
1606Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1607Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1608Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1609const Expr* ConstExprIterator::operator[](size_t idx) const {
1610 return cast<Expr>(I[idx]);
1611}
1612const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1613const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1614
1615//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001616// Child Iterators for iterating over subexpressions/substatements
1617//===----------------------------------------------------------------------===//
1618
1619// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001620Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1621Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001622
Steve Naroff7779db42007-11-12 14:29:37 +00001623// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001624Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1625Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001626
Steve Naroffe3e9add2008-06-02 23:03:37 +00001627// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001628Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1629Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001630
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001631// ObjCKVCRefExpr
1632Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1633Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1634
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001635// ObjCSuperExpr
1636Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1637Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1638
Chris Lattnerd9f69102008-08-10 01:53:14 +00001639// PredefinedExpr
1640Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1641Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001642
1643// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001644Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1645Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001646
1647// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001648Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1649Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001650
1651// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001652Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1653Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001654
Chris Lattner5d661452007-08-26 03:42:43 +00001655// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001656Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1657Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001658
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001659// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001660Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1661Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001662
1663// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001664Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1665Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001666
1667// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001668Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1669Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001670
Sebastian Redl05189992008-11-11 17:56:53 +00001671// SizeOfAlignOfExpr
1672Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1673 // If this is of a type and the type is a VLA type (and not a typedef), the
1674 // size expression of the VLA needs to be treated as an executable expression.
1675 // Why isn't this weirdness documented better in StmtIterator?
1676 if (isArgumentType()) {
1677 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1678 getArgumentType().getTypePtr()))
1679 return child_iterator(T);
1680 return child_iterator();
1681 }
Sebastian Redld4575892008-12-03 23:17:54 +00001682 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001683}
Sebastian Redl05189992008-11-11 17:56:53 +00001684Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1685 if (isArgumentType())
1686 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001687 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001688}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001689
1690// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001691Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001692 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001693}
Ted Kremenek1237c672007-08-24 20:06:47 +00001694Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001695 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001696}
1697
1698// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001699Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001700 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001701}
Ted Kremenek1237c672007-08-24 20:06:47 +00001702Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001703 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001704}
Ted Kremenek1237c672007-08-24 20:06:47 +00001705
1706// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001707Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1708Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001709
Nate Begeman213541a2008-04-18 23:10:10 +00001710// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001711Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1712Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001713
1714// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001715Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1716Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001717
Ted Kremenek1237c672007-08-24 20:06:47 +00001718// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001719Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1720Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001721
1722// BinaryOperator
1723Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001724 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001725}
Ted Kremenek1237c672007-08-24 20:06:47 +00001726Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001727 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001728}
1729
1730// ConditionalOperator
1731Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001732 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001733}
Ted Kremenek1237c672007-08-24 20:06:47 +00001734Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001735 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001736}
1737
1738// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001739Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1740Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001741
Ted Kremenek1237c672007-08-24 20:06:47 +00001742// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001743Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1744Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001745
1746// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001747Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1748 return child_iterator();
1749}
1750
1751Stmt::child_iterator TypesCompatibleExpr::child_end() {
1752 return child_iterator();
1753}
Ted Kremenek1237c672007-08-24 20:06:47 +00001754
1755// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001756Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1757Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001758
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001759// GNUNullExpr
1760Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1761Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1762
Nate Begemane2ce1d92008-01-17 17:46:27 +00001763// OverloadExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001764Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1765Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001766
Eli Friedmand38617c2008-05-14 19:38:39 +00001767// ShuffleVectorExpr
1768Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001769 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001770}
1771Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001772 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001773}
1774
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001775// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001776Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1777Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001778
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001779// InitListExpr
1780Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001781 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001782}
1783Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001784 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001785}
1786
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001787// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001788Stmt::child_iterator DesignatedInitExpr::child_begin() {
1789 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1790 Ptr += sizeof(DesignatedInitExpr);
1791 Ptr += sizeof(Designator) * NumDesignators;
1792 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1793}
1794Stmt::child_iterator DesignatedInitExpr::child_end() {
1795 return child_iterator(&*child_begin() + NumSubExprs);
1796}
1797
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001798// ImplicitValueInitExpr
1799Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1800 return child_iterator();
1801}
1802
1803Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1804 return child_iterator();
1805}
1806
Ted Kremenek1237c672007-08-24 20:06:47 +00001807// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001808Stmt::child_iterator ObjCStringLiteral::child_begin() {
1809 return child_iterator();
1810}
1811Stmt::child_iterator ObjCStringLiteral::child_end() {
1812 return child_iterator();
1813}
Ted Kremenek1237c672007-08-24 20:06:47 +00001814
1815// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001816Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1817Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001818
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001819// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001820Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1821 return child_iterator();
1822}
1823Stmt::child_iterator ObjCSelectorExpr::child_end() {
1824 return child_iterator();
1825}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001826
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001827// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001828Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1829 return child_iterator();
1830}
1831Stmt::child_iterator ObjCProtocolExpr::child_end() {
1832 return child_iterator();
1833}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001834
Steve Naroff563477d2007-09-18 23:55:05 +00001835// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001836Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001837 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001838}
1839Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001840 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001841}
1842
Steve Naroff4eb206b2008-09-03 18:15:37 +00001843// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001844Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1845Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001846
Ted Kremenek9da13f92008-09-26 23:24:14 +00001847Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1848Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }