blob: 0cd68ce3b8aa910d0a6f4d840291895a847b3c12 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnere0391b22008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Ted Kremenek4f530a92009-02-06 19:55:15 +000041StringLiteral::StringLiteral(ASTContext& C, const char *strData,
42 unsigned byteLength, bool Wide, QualType t,
43 SourceLocation firstLoc,
Chris Lattner4b009652007-07-25 00:24:17 +000044 SourceLocation lastLoc) :
45 Expr(StringLiteralClass, t) {
46 // OPTIMIZE: could allocate this appended to the StringLiteral.
Ted Kremenek4f530a92009-02-06 19:55:15 +000047 char *AStrData = new (C, 1) char[byteLength];
Chris Lattner4b009652007-07-25 00:24:17 +000048 memcpy(AStrData, strData, byteLength);
49 StrData = AStrData;
50 ByteLength = byteLength;
51 IsWide = Wide;
52 firstTokLoc = firstLoc;
53 lastTokLoc = lastLoc;
54}
55
Ted Kremenek4f530a92009-02-06 19:55:15 +000056void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000057 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000058 this->~StringLiteral();
59 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000060}
61
62bool UnaryOperator::isPostfix(Opcode Op) {
63 switch (Op) {
64 case PostInc:
65 case PostDec:
66 return true;
67 default:
68 return false;
69 }
70}
71
Ted Kremenek97318dd2008-07-23 22:18:43 +000072bool UnaryOperator::isPrefix(Opcode Op) {
73 switch (Op) {
74 case PreInc:
75 case PreDec:
76 return true;
77 default:
78 return false;
79 }
80}
81
Chris Lattner4b009652007-07-25 00:24:17 +000082/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
83/// corresponds to, e.g. "sizeof" or "[pre]++".
84const char *UnaryOperator::getOpcodeStr(Opcode Op) {
85 switch (Op) {
86 default: assert(0 && "Unknown unary operator");
87 case PostInc: return "++";
88 case PostDec: return "--";
89 case PreInc: return "++";
90 case PreDec: return "--";
91 case AddrOf: return "&";
92 case Deref: return "*";
93 case Plus: return "+";
94 case Minus: return "-";
95 case Not: return "~";
96 case LNot: return "!";
97 case Real: return "__real";
98 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000099 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000100 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000101 }
102}
103
104//===----------------------------------------------------------------------===//
105// Postfix Operators.
106//===----------------------------------------------------------------------===//
107
Ted Kremenek362abcd2009-02-09 20:51:47 +0000108CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000109 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000110 : Expr(SC, t,
111 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000112 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000113 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000114
115 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000116 SubExprs[FN] = fn;
117 for (unsigned i = 0; i != numargs; ++i)
118 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000119
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000120 RParenLoc = rparenloc;
121}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000122
Ted Kremenek362abcd2009-02-09 20:51:47 +0000123CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
124 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000125 : Expr(CallExprClass, t,
126 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000127 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000128 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000129
130 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000131 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000132 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000133 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000134
Chris Lattner4b009652007-07-25 00:24:17 +0000135 RParenLoc = rparenloc;
136}
137
Ted Kremenek362abcd2009-02-09 20:51:47 +0000138void CallExpr::Destroy(ASTContext& C) {
139 DestroyChildren(C);
140 if (SubExprs) C.Deallocate(SubExprs);
141 this->~CallExpr();
142 C.Deallocate(this);
143}
144
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000145/// setNumArgs - This changes the number of arguments present in this call.
146/// Any orphaned expressions are deleted by this, and any new operands are set
147/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000148void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000149 // No change, just return.
150 if (NumArgs == getNumArgs()) return;
151
152 // If shrinking # arguments, just delete the extras and forgot them.
153 if (NumArgs < getNumArgs()) {
154 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000155 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000156 this->NumArgs = NumArgs;
157 return;
158 }
159
160 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000161 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000162 // Copy over args.
163 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
164 NewSubExprs[i] = SubExprs[i];
165 // Null out new args.
166 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
167 NewSubExprs[i] = 0;
168
Ted Kremenek0c97e042009-02-07 01:47:29 +0000169 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000170 SubExprs = NewSubExprs;
171 this->NumArgs = NumArgs;
172}
173
Chris Lattnerc24915f2008-10-06 05:00:53 +0000174/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
175/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000176unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000177 // All simple function calls (e.g. func()) are implicitly cast to pointer to
178 // function. As a result, we try and obtain the DeclRefExpr from the
179 // ImplicitCastExpr.
180 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
181 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000182 return 0;
183
Steve Naroff44aec4c2008-01-31 01:07:12 +0000184 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
185 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000186 return 0;
187
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000188 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
189 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000190 return 0;
191
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000192 if (!FDecl->getIdentifier())
193 return 0;
194
Douglas Gregorb5af7382009-02-14 18:57:46 +0000195 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000196}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000197
Chris Lattnerc24915f2008-10-06 05:00:53 +0000198
Chris Lattner4b009652007-07-25 00:24:17 +0000199/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
200/// corresponds to, e.g. "<<=".
201const char *BinaryOperator::getOpcodeStr(Opcode Op) {
202 switch (Op) {
203 default: assert(0 && "Unknown binary operator");
204 case Mul: return "*";
205 case Div: return "/";
206 case Rem: return "%";
207 case Add: return "+";
208 case Sub: return "-";
209 case Shl: return "<<";
210 case Shr: return ">>";
211 case LT: return "<";
212 case GT: return ">";
213 case LE: return "<=";
214 case GE: return ">=";
215 case EQ: return "==";
216 case NE: return "!=";
217 case And: return "&";
218 case Xor: return "^";
219 case Or: return "|";
220 case LAnd: return "&&";
221 case LOr: return "||";
222 case Assign: return "=";
223 case MulAssign: return "*=";
224 case DivAssign: return "/=";
225 case RemAssign: return "%=";
226 case AddAssign: return "+=";
227 case SubAssign: return "-=";
228 case ShlAssign: return "<<=";
229 case ShrAssign: return ">>=";
230 case AndAssign: return "&=";
231 case XorAssign: return "^=";
232 case OrAssign: return "|=";
233 case Comma: return ",";
234 }
235}
236
Anders Carlsson762b7c72007-08-31 04:56:16 +0000237InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000238 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000239 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000240 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000241 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000242 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000243
244 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000245}
Chris Lattner4b009652007-07-25 00:24:17 +0000246
Douglas Gregorf603b472009-01-28 21:54:33 +0000247void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000248 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000249 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-01-28 21:54:33 +0000250 delete InitExprs[Idx];
251 InitExprs.resize(NumInits, 0);
252}
253
254Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
255 if (Init >= InitExprs.size()) {
256 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
257 InitExprs.back() = expr;
258 return 0;
259 }
260
261 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
262 InitExprs[Init] = expr;
263 return Result;
264}
265
Steve Naroff6f373332008-09-04 15:31:07 +0000266/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000267///
268const FunctionType *BlockExpr::getFunctionType() const {
269 return getType()->getAsBlockPointerType()->
270 getPointeeType()->getAsFunctionType();
271}
272
Steve Naroff9ac456d2008-10-08 17:01:13 +0000273SourceLocation BlockExpr::getCaretLocation() const {
274 return TheBlock->getCaretLocation();
275}
276const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
277Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
278
279
Chris Lattner4b009652007-07-25 00:24:17 +0000280//===----------------------------------------------------------------------===//
281// Generic Expression Routines
282//===----------------------------------------------------------------------===//
283
Chris Lattnerd2c66552009-02-14 07:37:35 +0000284/// isUnusedResultAWarning - Return true if this immediate expression should
285/// be warned about if the result is unused. If so, fill in Loc and Ranges
286/// with location to warn on and the source range[s] to report with the
287/// warning.
288bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
289 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000290 switch (getStmtClass()) {
291 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000292 Loc = getExprLoc();
293 R1 = getSourceRange();
294 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000296 return cast<ParenExpr>(this)->getSubExpr()->
297 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000298 case UnaryOperatorClass: {
299 const UnaryOperator *UO = cast<UnaryOperator>(this);
300
301 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000302 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 case UnaryOperator::PostInc:
304 case UnaryOperator::PostDec:
305 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000306 case UnaryOperator::PreDec: // ++/--
307 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000308 case UnaryOperator::Deref:
309 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000310 if (getType().isVolatileQualified())
311 return false;
312 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000313 case UnaryOperator::Real:
314 case UnaryOperator::Imag:
315 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000316 if (UO->getSubExpr()->getType().isVolatileQualified())
317 return false;
318 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000319 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000320 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000321 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000322 Loc = UO->getOperatorLoc();
323 R1 = UO->getSubExpr()->getSourceRange();
324 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000325 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000326 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000327 const BinaryOperator *BO = cast<BinaryOperator>(this);
328 // Consider comma to have side effects if the LHS or RHS does.
329 if (BO->getOpcode() == BinaryOperator::Comma)
330 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
331 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000332
Chris Lattnerd2c66552009-02-14 07:37:35 +0000333 if (BO->isAssignmentOp())
334 return false;
335 Loc = BO->getOperatorLoc();
336 R1 = BO->getLHS()->getSourceRange();
337 R2 = BO->getRHS()->getSourceRange();
338 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000339 }
Chris Lattner06078d22007-08-25 02:00:02 +0000340 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000341 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000342
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000343 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000344 // The condition must be evaluated, but if either the LHS or RHS is a
345 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000346 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000347 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
348 return true;
349 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000350 }
351
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000353 // If the base pointer or element is to a volatile pointer/field, accessing
354 // it is a side effect.
355 if (getType().isVolatileQualified())
356 return false;
357 Loc = cast<MemberExpr>(this)->getMemberLoc();
358 R1 = SourceRange(Loc, Loc);
359 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
360 return true;
361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 case ArraySubscriptExprClass:
363 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000364 // it is a side effect.
365 if (getType().isVolatileQualified())
366 return false;
367 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
368 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
369 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
370 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000371
Chris Lattner4b009652007-07-25 00:24:17 +0000372 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000373 case CXXOperatorCallExprClass: {
374 // If this is a direct call, get the callee.
375 const CallExpr *CE = cast<CallExpr>(this);
376 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
377 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
378 // If the callee has attribute pure, const, or warn_unused_result, warn
379 // about it. void foo() { strlen("bar"); } should warn.
380 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
381 if (FD->getAttr<WarnUnusedResultAttr>() ||
382 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
383 Loc = CE->getCallee()->getLocStart();
384 R1 = CE->getCallee()->getSourceRange();
385
386 if (unsigned NumArgs = CE->getNumArgs())
387 R2 = SourceRange(CE->getArg(0)->getLocStart(),
388 CE->getArg(NumArgs-1)->getLocEnd());
389 return true;
390 }
391 }
392 return false;
393 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000394 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000395 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000396 case StmtExprClass: {
397 // Statement exprs don't logically have side effects themselves, but are
398 // sometimes used in macros in ways that give them a type that is unused.
399 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
400 // however, if the result of the stmt expr is dead, we don't want to emit a
401 // warning.
402 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
403 if (!CS->body_empty())
404 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000405 return E->isUnusedResultAWarning(Loc, R1, R2);
406
407 Loc = cast<StmtExpr>(this)->getLParenLoc();
408 R1 = getSourceRange();
409 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000410 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000411 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000412 // If this is a cast to void, check the operand. Otherwise, the result of
413 // the cast is unused.
414 if (getType()->isVoidType())
415 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
416 R1, R2);
417 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
418 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
419 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000420 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // If this is a cast to void, check the operand. Otherwise, the result of
422 // the cast is unused.
423 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000424 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
425 R1, R2);
426 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
427 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
428 return true;
429
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000430 case ImplicitCastExprClass:
431 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000432 return cast<ImplicitCastExpr>(this)
433 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000434
Chris Lattner3e254fb2008-04-08 04:40:51 +0000435 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000436 return cast<CXXDefaultArgExpr>(this)
437 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000438
439 case CXXNewExprClass:
440 // FIXME: In theory, there might be new expressions that don't have side
441 // effects (e.g. a placement new with an uninitialized POD).
442 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000443 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000444 }
Chris Lattner4b009652007-07-25 00:24:17 +0000445}
446
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000447/// DeclCanBeLvalue - Determine whether the given declaration can be
448/// an lvalue. This is a helper routine for isLvalue.
449static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000450 // C++ [temp.param]p6:
451 // A non-type non-reference template-parameter is not an lvalue.
452 if (const NonTypeTemplateParmDecl *NTTParm
453 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
454 return NTTParm->getType()->isReferenceType();
455
Douglas Gregor8acb7272008-12-11 16:49:14 +0000456 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000457 // C++ 3.10p2: An lvalue refers to an object or function.
458 (Ctx.getLangOptions().CPlusPlus &&
459 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
460}
461
Chris Lattner4b009652007-07-25 00:24:17 +0000462/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
463/// incomplete type other than void. Nonarray expressions that can be lvalues:
464/// - name, where name must be a variable
465/// - e[i]
466/// - (e), where e must be an lvalue
467/// - e.name, where e must be an lvalue
468/// - e->name
469/// - *e, the type of e cannot be a function type
470/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000471/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000472/// - reference type [C++ [expr]]
473///
Chris Lattner25168a52008-07-26 21:30:36 +0000474Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000475 // first, check the type (C99 6.3.2.1). Expressions with function
476 // type in C are not lvalues, but they can be lvalues in C++.
477 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000478 return LV_NotObjectType;
479
Steve Naroffec7736d2008-02-10 01:39:04 +0000480 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000481 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000482 return LV_IncompleteVoidType;
483
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000484 /// FIXME: Expressions can't have reference type, so the following
485 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000486 if (TR->isReferenceType()) // C++ [expr]
487 return LV_Valid;
488
489 // the type looks fine, now check the expression
490 switch (getStmtClass()) {
491 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000492 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000493 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
494 // For vectors, make sure base is an lvalue (i.e. not a function call).
495 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000496 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000497 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000498 case DeclRefExprClass:
499 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000500 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
501 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000502 return LV_Valid;
503 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000504 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000505 case BlockDeclRefExprClass: {
506 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000507 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000508 return LV_Valid;
509 break;
510 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000511 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000512 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000513 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
514 NamedDecl *Member = m->getMemberDecl();
515 // C++ [expr.ref]p4:
516 // If E2 is declared to have type "reference to T", then E1.E2
517 // is an lvalue.
518 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
519 if (Value->getType()->isReferenceType())
520 return LV_Valid;
521
522 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
523 if (isa<CXXClassVarDecl>(Member))
524 return LV_Valid;
525
526 // -- If E2 is a non-static data member [...]. If E1 is an
527 // lvalue, then E1.E2 is an lvalue.
528 if (isa<FieldDecl>(Member))
529 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
530
531 // -- If it refers to a static member function [...], then
532 // E1.E2 is an lvalue.
533 // -- Otherwise, if E1.E2 refers to a non-static member
534 // function [...], then E1.E2 is not an lvalue.
535 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
536 return Method->isStatic()? LV_Valid : LV_MemberFunction;
537
538 // -- If E2 is a member enumerator [...], the expression E1.E2
539 // is not an lvalue.
540 if (isa<EnumConstantDecl>(Member))
541 return LV_InvalidExpression;
542
543 // Not an lvalue.
544 return LV_InvalidExpression;
545 }
546
547 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000548 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000549 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000550 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000551 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000552 return LV_Valid; // C99 6.5.3p4
553
554 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000555 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
556 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000557 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000558
559 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
560 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
561 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
562 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000563 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000564 case ImplicitCastExprClass:
565 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
566 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000567 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000568 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000569 case BinaryOperatorClass:
570 case CompoundAssignOperatorClass: {
571 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000572
573 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
574 BinOp->getOpcode() == BinaryOperator::Comma)
575 return BinOp->getRHS()->isLvalue(Ctx);
576
Sebastian Redl95216a62009-02-07 00:15:38 +0000577 // C++ [expr.mptr.oper]p6
578 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
579 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
580 !BinOp->getType()->isFunctionType())
581 return BinOp->getLHS()->isLvalue(Ctx);
582
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000583 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000584 return LV_InvalidExpression;
585
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000586 if (Ctx.getLangOptions().CPlusPlus)
587 // C++ [expr.ass]p1:
588 // The result of an assignment operation [...] is an lvalue.
589 return LV_Valid;
590
591
592 // C99 6.5.16:
593 // An assignment expression [...] is not an lvalue.
594 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000595 }
Nate Begemand6d2f772009-01-18 03:20:47 +0000596 // FIXME: OverloadExprClass
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000597 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000598 case CXXOperatorCallExprClass:
599 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000600 // C++ [expr.call]p10:
601 // A function call is an lvalue if and only if the result type
602 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000603 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000604 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000605 CalleeType = FnTypePtr->getPointeeType();
606 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
607 if (FnType->getResultType()->isReferenceType())
608 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000609
610 break;
611 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000612 case CompoundLiteralExprClass: // C99 6.5.2.5p5
613 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000614 case ChooseExprClass:
615 // __builtin_choose_expr is an lvalue if the selected operand is.
616 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
617 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
618 else
619 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
620
Nate Begemanaf6ed502008-04-18 23:10:10 +0000621 case ExtVectorElementExprClass:
622 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000623 return LV_DuplicateVectorComponents;
624 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000625 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
626 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000627 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
628 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000629 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000630 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000631 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000632 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000633 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000634 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000635 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000636 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000637 case CXXConditionDeclExprClass:
638 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000639 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000640 case CXXFunctionalCastExprClass:
641 case CXXStaticCastExprClass:
642 case CXXDynamicCastExprClass:
643 case CXXReinterpretCastExprClass:
644 case CXXConstCastExprClass:
645 // The result of an explicit cast is an lvalue if the type we are
646 // casting to is a reference type. See C++ [expr.cast]p1,
647 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
648 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
649 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
650 return LV_Valid;
651 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000652 case CXXTypeidExprClass:
653 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
654 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000655 default:
656 break;
657 }
658 return LV_InvalidExpression;
659}
660
661/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
662/// does not have an incomplete type, does not have a const-qualified type, and
663/// if it is a structure or union, does not have any member (including,
664/// recursively, any member or element of all contained aggregates or unions)
665/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000666Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
667 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000668
669 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000670 case LV_Valid:
671 // C++ 3.10p11: Functions cannot be modified, but pointers to
672 // functions can be modifiable.
673 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
674 return MLV_NotObjectType;
675 break;
676
Chris Lattner4b009652007-07-25 00:24:17 +0000677 case LV_NotObjectType: return MLV_NotObjectType;
678 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000679 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000680 case LV_InvalidExpression:
681 // If the top level is a C-style cast, and the subexpression is a valid
682 // lvalue, then this is probably a use of the old-school "cast as lvalue"
683 // GCC extension. We don't support it, but we want to produce good
684 // diagnostics when it happens so that the user knows why.
685 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
686 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
687 return MLV_LValueCast;
688 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000689 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000690 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000691
692 QualType CT = Ctx.getCanonicalType(getType());
693
694 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000695 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000696 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000697 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000698 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000699 return MLV_IncompleteType;
700
Chris Lattnera1923f62008-08-04 07:31:14 +0000701 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000702 if (r->hasConstFields())
703 return MLV_ConstQualified;
704 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000705 // The following is illegal:
706 // void takeclosure(void (^C)(void));
707 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
708 //
709 if (getStmtClass() == BlockDeclRefExprClass) {
710 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
711 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
712 return MLV_NotBlockQualified;
713 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000714
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000715 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000716 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000717 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
718 if (KVCExpr->getSetterMethod() == 0)
719 return MLV_NoSetterProperty;
720 }
Chris Lattner4b009652007-07-25 00:24:17 +0000721 return MLV_Valid;
722}
723
Ted Kremenek5778d622008-02-27 18:39:48 +0000724/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000725/// duration. This means that the address of this expression is a link-time
726/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000727bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000728 switch (getStmtClass()) {
729 default:
730 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000731 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000732 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000733 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000734 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000735 case CompoundLiteralExprClass:
736 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000737 case DeclRefExprClass:
738 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000739 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
740 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000741 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000742 if (isa<FunctionDecl>(D))
743 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000744 return false;
745 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000746 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000747 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000748 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000749 }
Chris Lattner743ec372007-11-27 21:35:27 +0000750 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000751 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000752 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000753 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000754 case CXXDefaultArgExprClass:
755 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000756 }
757}
758
Ted Kremenek87e30c52008-01-17 16:57:34 +0000759Expr* Expr::IgnoreParens() {
760 Expr* E = this;
761 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
762 E = P->getSubExpr();
763
764 return E;
765}
766
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000767/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
768/// or CastExprs or ImplicitCastExprs, returning their operand.
769Expr *Expr::IgnoreParenCasts() {
770 Expr *E = this;
771 while (true) {
772 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
773 E = P->getSubExpr();
774 else if (CastExpr *P = dyn_cast<CastExpr>(E))
775 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000776 else
777 return E;
778 }
779}
780
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000781/// hasAnyTypeDependentArguments - Determines if any of the expressions
782/// in Exprs is type-dependent.
783bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
784 for (unsigned I = 0; I < NumExprs; ++I)
785 if (Exprs[I]->isTypeDependent())
786 return true;
787
788 return false;
789}
790
791/// hasAnyValueDependentArguments - Determines if any of the expressions
792/// in Exprs is value-dependent.
793bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
794 for (unsigned I = 0; I < NumExprs; ++I)
795 if (Exprs[I]->isValueDependent())
796 return true;
797
798 return false;
799}
800
Eli Friedmandee41122009-01-25 02:32:41 +0000801bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000802 // This function is attempting whether an expression is an initializer
803 // which can be evaluated at compile-time. isEvaluatable handles most
804 // of the cases, but it can't deal with some initializer-specific
805 // expressions, and it can't deal with aggregates; we deal with those here,
806 // and fall back to isEvaluatable for the other cases.
807
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000808 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000809 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000810 case StringLiteralClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000811 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000812 case CompoundLiteralExprClass: {
813 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000814 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000815 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000816 case InitListExprClass: {
817 const InitListExpr *Exp = cast<InitListExpr>(this);
818 unsigned numInits = Exp->getNumInits();
819 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000820 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000821 return false;
822 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000823 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000824 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000825 case ImplicitValueInitExprClass:
826 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000827 case ParenExprClass: {
828 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
829 }
830 case UnaryOperatorClass: {
831 const UnaryOperator* Exp = cast<UnaryOperator>(this);
832 if (Exp->getOpcode() == UnaryOperator::Extension)
833 return Exp->getSubExpr()->isConstantInitializer(Ctx);
834 break;
835 }
836 case CStyleCastExprClass:
837 // Handle casts with a destination that's a struct or union; this
838 // deals with both the gcc no-op struct cast extension and the
839 // cast-to-union extension.
840 if (getType()->isRecordType())
841 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
842 break;
Eli Friedmanc8a00142009-01-25 03:27:40 +0000843 case DesignatedInitExprClass:
Sebastian Redl94889622009-01-25 13:34:47 +0000844 return cast<DesignatedInitExpr>(this)->
845 getInit()->isConstantInitializer(Ctx);
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000846 }
847
Eli Friedman2b0dec52009-01-25 03:12:18 +0000848 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000849}
850
Chris Lattner4b009652007-07-25 00:24:17 +0000851/// isIntegerConstantExpr - this recursive routine will test if an expression is
852/// an integer constant expression. Note: With the introduction of VLA's in
853/// C99 the result of the sizeof operator is no longer always a constant
854/// expression. The generalization of the wording to include any subexpression
855/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
856/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000857/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000858/// occurring in a context requiring a constant, would have been a constraint
859/// violation. FIXME: This routine currently implements C90 semantics.
860/// To properly implement C99 semantics this routine will need to evaluate
861/// expressions involving operators previously mentioned.
862
863/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
864/// comma, etc
865///
866/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000867/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000868///
869/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
870/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
871/// cast+dereference.
872bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
873 SourceLocation *Loc, bool isEvaluated) const {
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000874 if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
875 return false;
876 assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
877 return true;
878}
879
880bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
881 SourceLocation *Loc, bool isEvaluated) const {
882
Eli Friedman14cc7542008-11-13 06:09:17 +0000883 // Pretest for integral type; some parts of the code crash for types that
884 // can't be sized.
885 if (!getType()->isIntegralType()) {
886 if (Loc) *Loc = getLocStart();
887 return false;
888 }
Chris Lattner4b009652007-07-25 00:24:17 +0000889 switch (getStmtClass()) {
890 default:
891 if (Loc) *Loc = getLocStart();
892 return false;
893 case ParenExprClass:
894 return cast<ParenExpr>(this)->getSubExpr()->
895 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
896 case IntegerLiteralClass:
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000897 // NOTE: getValue() returns an APInt, we must set sign.
Chris Lattner4b009652007-07-25 00:24:17 +0000898 Result = cast<IntegerLiteral>(this)->getValue();
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000899 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner4b009652007-07-25 00:24:17 +0000900 break;
901 case CharacterLiteralClass: {
902 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000903 Result = Ctx.MakeIntValue(CL->getValue(), getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000904 break;
905 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000906 case CXXBoolLiteralExprClass: {
907 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000908 Result = Ctx.MakeIntValue(BL->getValue(), getType());
Anders Carlssoned6c2142008-08-23 21:12:35 +0000909 break;
910 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000911 case CXXZeroInitValueExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000912 Result = Ctx.MakeIntValue(0, getType());
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000913 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000914 case TypesCompatibleExprClass: {
915 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000916 // Per gcc docs "this built-in function ignores top level
917 // qualifiers". We need to use the canonical version to properly
918 // be able to strip CRV qualifiers from the type.
919 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
920 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000921 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
922 T1.getUnqualifiedType()),
923 getType());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000924 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000925 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000926 case CallExprClass:
927 case CXXOperatorCallExprClass: {
Steve Naroff8d3b1702007-08-08 22:15:55 +0000928 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1eee9402008-10-06 06:40:35 +0000929
930 // If this is a call to a builtin function, constant fold it otherwise
931 // reject it.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000932 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson8c3de802008-12-19 20:58:05 +0000933 EvalResult EvalResult;
934 if (CE->Evaluate(EvalResult, Ctx)) {
935 assert(!EvalResult.HasSideEffects &&
936 "Foldable builtin call should not have side effects!");
937 Result = EvalResult.Val.getInt();
Chris Lattner1eee9402008-10-06 06:40:35 +0000938 break; // It is a constant, expand it.
939 }
940 }
941
Steve Naroff8d3b1702007-08-08 22:15:55 +0000942 if (Loc) *Loc = getLocStart();
943 return false;
944 }
Chris Lattner4b009652007-07-25 00:24:17 +0000945 case DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000946 case QualifiedDeclRefExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000947 if (const EnumConstantDecl *D =
948 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
949 Result = D->getInitVal();
950 break;
951 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000952 if (Ctx.getLangOptions().CPlusPlus &&
953 getType().getCVRQualifiers() == QualType::Const) {
954 // C++ 7.1.5.1p2
955 // A variable of non-volatile const-qualified integral or enumeration
956 // type initialized by an ICE can be used in ICEs.
957 if (const VarDecl *Dcl =
958 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
959 if (const Expr *Init = Dcl->getInit())
960 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
961 }
962 }
Chris Lattner4b009652007-07-25 00:24:17 +0000963 if (Loc) *Loc = getLocStart();
964 return false;
965 case UnaryOperatorClass: {
966 const UnaryOperator *Exp = cast<UnaryOperator>(this);
967
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000968 // Get the operand value. If this is offsetof, do not evalute the
Chris Lattner4b009652007-07-25 00:24:17 +0000969 // operand. This affects C99 6.6p3.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000970 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
971 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000972 return false;
973
974 switch (Exp->getOpcode()) {
975 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
976 // See C99 6.6p3.
977 default:
978 if (Loc) *Loc = Exp->getOperatorLoc();
979 return false;
980 case UnaryOperator::Extension:
981 return true; // FIXME: this is wrong.
Chris Lattner4b009652007-07-25 00:24:17 +0000982 case UnaryOperator::LNot: {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000983 Result = Ctx.MakeIntValue(Result == 0, getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000984 break;
985 }
986 case UnaryOperator::Plus:
987 break;
988 case UnaryOperator::Minus:
989 Result = -Result;
990 break;
991 case UnaryOperator::Not:
992 Result = ~Result;
993 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000994 case UnaryOperator::OffsetOf:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000995 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
996 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000997 }
998 break;
999 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001000 case SizeOfAlignOfExprClass: {
1001 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +00001002
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001003 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattner20515462008-02-21 05:45:29 +00001004 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001005 if (ArgTy->isVoidType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001006 Result = Ctx.MakeIntValue(1, getType());
Chris Lattner20515462008-02-21 05:45:29 +00001007 break;
1008 }
1009
1010 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001011 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001012 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +00001013 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001014 }
Chris Lattner4b009652007-07-25 00:24:17 +00001015
Chris Lattner4b009652007-07-25 00:24:17 +00001016 // Get information about the size or align.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001017 if (ArgTy->isFunctionType()) {
Chris Lattnerd4dc2672008-01-02 21:54:09 +00001018 // GCC extension: sizeof(function) = 1.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001019 Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001020 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001021 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001022 if (Exp->isSizeOf())
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001023 Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001024 else
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001025 Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +00001026 }
Chris Lattner4b009652007-07-25 00:24:17 +00001027 break;
1028 }
1029 case BinaryOperatorClass: {
1030 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001031 llvm::APSInt LHS, RHS;
1032
1033 // Initialize result to have correct signedness and width.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001034 Result = Ctx.MakeIntValue(0, getType());
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001035
Chris Lattner4b009652007-07-25 00:24:17 +00001036 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001037 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001038 return false;
1039
Chris Lattner4b009652007-07-25 00:24:17 +00001040 // The short-circuiting &&/|| operators don't necessarily evaluate their
1041 // RHS. Make sure to pass isEvaluated down correctly.
1042 if (Exp->isLogicalOp()) {
1043 bool RHSEval;
1044 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001045 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001046 else {
1047 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001048 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001049 }
1050
1051 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
1052 isEvaluated & RHSEval))
1053 return false;
1054 } else {
1055 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
1056 return false;
1057 }
1058
1059 switch (Exp->getOpcode()) {
1060 default:
1061 if (Loc) *Loc = getLocStart();
1062 return false;
1063 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001064 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001065 break;
1066 case BinaryOperator::Div:
1067 if (RHS == 0) {
1068 if (!isEvaluated) break;
1069 if (Loc) *Loc = getLocStart();
1070 return false;
1071 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001072 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001073 break;
1074 case BinaryOperator::Rem:
1075 if (RHS == 0) {
1076 if (!isEvaluated) break;
1077 if (Loc) *Loc = getLocStart();
1078 return false;
1079 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001080 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001081 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001082 case BinaryOperator::Add: Result = LHS + RHS; break;
1083 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001084 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001085 Result = LHS <<
1086 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1087 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001088 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001089 Result = LHS >>
1090 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +00001091 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001092 case BinaryOperator::LT: Result = LHS < RHS; break;
1093 case BinaryOperator::GT: Result = LHS > RHS; break;
1094 case BinaryOperator::LE: Result = LHS <= RHS; break;
1095 case BinaryOperator::GE: Result = LHS >= RHS; break;
1096 case BinaryOperator::EQ: Result = LHS == RHS; break;
1097 case BinaryOperator::NE: Result = LHS != RHS; break;
1098 case BinaryOperator::And: Result = LHS & RHS; break;
1099 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1100 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001101 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001102 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001103 break;
1104 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001105 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001106 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001107
1108 case BinaryOperator::Comma:
1109 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1110 // *except* when they are contained within a subexpression that is not
1111 // evaluated". Note that Assignment can never happen due to constraints
1112 // on the LHS subexpr, so we don't need to check it here.
1113 if (isEvaluated) {
1114 if (Loc) *Loc = getLocStart();
1115 return false;
1116 }
1117
1118 // The result of the constant expr is the RHS.
1119 Result = RHS;
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001120 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001121 }
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001122
Chris Lattner4b009652007-07-25 00:24:17 +00001123 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1124 break;
1125 }
1126 case ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +00001127 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001128 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001129 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1130 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +00001131
1132 // C99 6.6p6: shall only convert arithmetic types to integer types.
1133 if (!SubExpr->getType()->isArithmeticType() ||
1134 !getType()->isIntegerType()) {
1135 if (Loc) *Loc = SubExpr->getLocStart();
1136 return false;
1137 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001138
Chris Lattner8cd0e932008-03-05 18:54:05 +00001139 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001140
Chris Lattner4b009652007-07-25 00:24:17 +00001141 // Handle simple integer->integer casts.
1142 if (SubExpr->getType()->isIntegerType()) {
1143 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1144 return false;
1145
1146 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +00001147 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +00001148 if (getType()->isBooleanType()) {
1149 // Conversion to bool compares against zero.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001150 Result = Ctx.MakeIntValue(Result != 0, getType());
1151 } else if (SubExpr->getType()->isSignedIntegerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001152 Result.sextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001153 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1154 } else { // If the input is unsigned, do a zero extend, noop,
1155 // or truncate.
Chris Lattner4b009652007-07-25 00:24:17 +00001156 Result.zextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001157 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1158 }
Chris Lattner4b009652007-07-25 00:24:17 +00001159 break;
1160 }
1161
1162 // Allow floating constants that are the immediate operands of casts or that
1163 // are parenthesized.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001164 const Expr *Operand = SubExpr->IgnoreParens();
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001165
1166 // If this isn't a floating literal, we can't handle it.
1167 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1168 if (!FL) {
1169 if (Loc) *Loc = Operand->getLocStart();
1170 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001171 }
Chris Lattner000c4102008-01-09 18:59:34 +00001172
1173 // If the destination is boolean, compare against zero.
1174 if (getType()->isBooleanType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001175 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
Chris Lattner000c4102008-01-09 18:59:34 +00001176 break;
1177 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001178
1179 // Determine whether we are converting to unsigned or signed.
1180 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +00001181
1182 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1183 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +00001184 uint64_t Space[4];
1185 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +00001186 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001187 llvm::APFloat::rmTowardZero,
1188 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001189 Result = llvm::APInt(DestWidth, 4, Space);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001190 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001191 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001192 }
1193 case ConditionalOperatorClass: {
1194 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1195
Chris Lattner45e71bf2008-12-12 06:55:44 +00001196 const Expr *Cond = Exp->getCond();
1197
1198 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001199 return false;
1200
1201 const Expr *TrueExp = Exp->getLHS();
1202 const Expr *FalseExp = Exp->getRHS();
1203 if (Result == 0) std::swap(TrueExp, FalseExp);
1204
Chris Lattner45e71bf2008-12-12 06:55:44 +00001205 // If the condition (ignoring parens) is a __builtin_constant_p call,
1206 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001207 // expression, and it is fully evaluated. This is an important GNU
1208 // extension. See GCC PR38377 for discussion.
Chris Lattner45e71bf2008-12-12 06:55:44 +00001209 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001210 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001211 EvalResult EVResult;
1212 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1213 return false;
1214 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1215 Result = EVResult.Val.getInt();
1216 if (Loc) *Loc = EVResult.DiagLoc;
1217 return true;
1218 }
Chris Lattner45e71bf2008-12-12 06:55:44 +00001219
Chris Lattner4b009652007-07-25 00:24:17 +00001220 // Evaluate the false one first, discard the result.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001221 llvm::APSInt Tmp;
1222 if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001223 return false;
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001224 // Evalute the true one, capture the result. Note that if TrueExp
1225 // is False then this is an instant of the gcc missing LHS
1226 // extension, and we will just reuse Result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001227 if (TrueExp &&
1228 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001229 return false;
1230 break;
1231 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001232 case CXXDefaultArgExprClass:
1233 return cast<CXXDefaultArgExpr>(this)
1234 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001235
1236 case UnaryTypeTraitExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001237 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1238 getType());
1239 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001240 }
1241
Chris Lattner4b009652007-07-25 00:24:17 +00001242 return true;
1243}
1244
Chris Lattner4b009652007-07-25 00:24:17 +00001245/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1246/// integer constant expression with the value zero, or if this is one that is
1247/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001248bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1249{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001250 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001251 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001252 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001253 // Check that it is a cast to void*.
1254 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1255 QualType Pointee = PT->getPointeeType();
1256 if (Pointee.getCVRQualifiers() == 0 &&
1257 Pointee->isVoidType() && // to void*
1258 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001259 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001260 }
Chris Lattner4b009652007-07-25 00:24:17 +00001261 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001262 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1263 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001264 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001265 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1266 // Accept ((void*)0) as a null pointer constant, as many other
1267 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001268 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001269 } else if (const CXXDefaultArgExpr *DefaultArg
1270 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001271 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001272 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001273 } else if (isa<GNUNullExpr>(this)) {
1274 // The GNU __null extension is always a null pointer constant.
1275 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001276 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001277
Steve Naroffa2e53222008-01-14 16:10:57 +00001278 // This expression must be an integer type.
1279 if (!getType()->isIntegerType())
1280 return false;
1281
Chris Lattner4b009652007-07-25 00:24:17 +00001282 // If we have an integer constant expression, we need to *evaluate* it and
1283 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001284 // FIXME: We should probably return false if we're compiling in strict mode
1285 // and Diag is not null (this indicates that the value was foldable but not
1286 // an ICE.
1287 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001288 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001289 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001290}
Steve Naroffc11705f2007-07-28 23:10:27 +00001291
Douglas Gregor81c29152008-10-29 00:13:59 +00001292/// isBitField - Return true if this expression is a bit-field.
1293bool Expr::isBitField() {
1294 Expr *E = this->IgnoreParenCasts();
1295 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001296 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1297 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001298 return false;
1299}
1300
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001301/// isArrow - Return true if the base expression is a pointer to vector,
1302/// return false if the base expression is a vector.
1303bool ExtVectorElementExpr::isArrow() const {
1304 return getBase()->getType()->isPointerType();
1305}
1306
Nate Begemanaf6ed502008-04-18 23:10:10 +00001307unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001308 if (const VectorType *VT = getType()->getAsVectorType())
1309 return VT->getNumElements();
1310 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001311}
1312
Nate Begemanc8e51f82008-05-09 06:41:27 +00001313/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001314bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001315 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001316 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001317
1318 // Halving swizzles do not contain duplicate elements.
1319 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1320 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1321 return false;
1322
1323 // Advance past s-char prefix on hex swizzles.
1324 if (*compStr == 's') {
1325 compStr++;
1326 length--;
1327 }
Steve Naroffba67f692007-07-30 03:29:09 +00001328
Chris Lattner58d3fa52008-11-19 07:55:04 +00001329 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001330 const char *s = compStr+i;
1331 for (const char c = *s++; *s; s++)
1332 if (c == *s)
1333 return true;
1334 }
1335 return false;
1336}
Chris Lattner42158e72007-08-02 23:36:59 +00001337
Nate Begemanc8e51f82008-05-09 06:41:27 +00001338/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001339void ExtVectorElementExpr::getEncodedElementAccess(
1340 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001341 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001342 if (*compStr == 's')
1343 compStr++;
1344
1345 bool isHi = !strcmp(compStr, "hi");
1346 bool isLo = !strcmp(compStr, "lo");
1347 bool isEven = !strcmp(compStr, "even");
1348 bool isOdd = !strcmp(compStr, "odd");
1349
Nate Begemanc8e51f82008-05-09 06:41:27 +00001350 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1351 uint64_t Index;
1352
1353 if (isHi)
1354 Index = e + i;
1355 else if (isLo)
1356 Index = i;
1357 else if (isEven)
1358 Index = 2 * i;
1359 else if (isOdd)
1360 Index = 2 * i + 1;
1361 else
1362 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001363
Nate Begemana1ae7442008-05-13 21:03:02 +00001364 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001365 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001366}
1367
Steve Naroff4ed9d662007-09-27 14:38:14 +00001368// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001369ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001370 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001371 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001372 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001373 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001374 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001375 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001376 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001377 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001378 if (NumArgs) {
1379 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001380 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1381 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001382 LBracloc = LBrac;
1383 RBracloc = RBrac;
1384}
1385
Steve Naroff4ed9d662007-09-27 14:38:14 +00001386// constructor for class messages.
1387// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001388ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001390 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001391 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001392 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001393 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001394 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001395 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001396 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001397 if (NumArgs) {
1398 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001399 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1400 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001401 LBracloc = LBrac;
1402 RBracloc = RBrac;
1403}
1404
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001405// constructor for class messages.
1406ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1407 QualType retType, ObjCMethodDecl *mproto,
1408 SourceLocation LBrac, SourceLocation RBrac,
1409 Expr **ArgExprs, unsigned nargs)
1410: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1411MethodProto(mproto) {
1412 NumArgs = nargs;
1413 SubExprs = new Stmt*[NumArgs+1];
1414 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1415 if (NumArgs) {
1416 for (unsigned i = 0; i != NumArgs; ++i)
1417 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1418 }
1419 LBracloc = LBrac;
1420 RBracloc = RBrac;
1421}
1422
1423ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1424 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1425 switch (x & Flags) {
1426 default:
1427 assert(false && "Invalid ObjCMessageExpr.");
1428 case IsInstMeth:
1429 return ClassInfo(0, 0);
1430 case IsClsMethDeclUnknown:
1431 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1432 case IsClsMethDeclKnown: {
1433 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1434 return ClassInfo(D, D->getIdentifier());
1435 }
1436 }
1437}
1438
Chris Lattnerf624cd22007-10-25 00:29:32 +00001439bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001440 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001441}
1442
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001443static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001444 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1445 QualType Ty = ME->getBase()->getType();
1446
1447 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001448 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001449 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1450 // FIXME: This is linear time. And the fact that we're indexing
1451 // into the layout by position in the record means that we're
1452 // either stuck numbering the fields in the AST or we have to keep
1453 // the linear search (yuck and yuck).
1454 unsigned i = 0;
1455 for (RecordDecl::field_iterator Field = RD->field_begin(),
1456 FieldEnd = RD->field_end();
1457 Field != FieldEnd; (void)++Field, ++i) {
1458 if (*Field == FD)
1459 break;
1460 }
1461
1462 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001463 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001464 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1465 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001466
Chris Lattner8cd0e932008-03-05 18:54:05 +00001467 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001468 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001469
1470 return size + evaluateOffsetOf(C, Base);
1471 } else if (isa<CompoundLiteralExpr>(E))
1472 return 0;
1473
1474 assert(0 && "Unknown offsetof subexpression!");
1475 return 0;
1476}
1477
1478int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1479{
1480 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1481
Chris Lattner8cd0e932008-03-05 18:54:05 +00001482 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001483 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001484}
1485
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001486void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1487 // Override default behavior of traversing children. If this has a type
1488 // operand and the type is a variable-length array, the child iteration
1489 // will iterate over the size expression. However, this expression belongs
1490 // to the type, not to this, so we don't want to delete it.
1491 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001492 if (isArgumentType()) {
1493 this->~SizeOfAlignOfExpr();
1494 C.Deallocate(this);
1495 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001496 else
1497 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001498}
1499
Ted Kremenekf1a67122009-02-09 17:08:14 +00001500void OverloadExpr::Destroy(ASTContext& C) {
1501 DestroyChildren(C);
1502 C.Deallocate(SubExprs);
1503 this->~OverloadExpr();
1504 C.Deallocate(this);
1505}
1506
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001507//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001508// DesignatedInitExpr
1509//===----------------------------------------------------------------------===//
1510
1511IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1512 assert(Kind == FieldDesignator && "Only valid on a field designator");
1513 if (Field.NameOrField & 0x01)
1514 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1515 else
1516 return getField()->getIdentifier();
1517}
1518
1519DesignatedInitExpr *
1520DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1521 unsigned NumDesignators,
1522 Expr **IndexExprs, unsigned NumIndexExprs,
1523 SourceLocation ColonOrEqualLoc,
1524 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001525 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1526 sizeof(Designator) * NumDesignators +
1527 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001528 DesignatedInitExpr *DIE
1529 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1530 ColonOrEqualLoc, UsesColonSyntax,
1531 NumIndexExprs + 1);
1532
1533 // Fill in the designators
1534 unsigned ExpectedNumSubExprs = 0;
1535 designators_iterator Desig = DIE->designators_begin();
1536 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1537 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1538 if (Designators[Idx].isArrayDesignator())
1539 ++ExpectedNumSubExprs;
1540 else if (Designators[Idx].isArrayRangeDesignator())
1541 ExpectedNumSubExprs += 2;
1542 }
1543 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1544
1545 // Fill in the subexpressions, including the initializer expression.
1546 child_iterator Child = DIE->child_begin();
1547 *Child++ = Init;
1548 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1549 *Child = IndexExprs[Idx];
1550
1551 return DIE;
1552}
1553
1554SourceRange DesignatedInitExpr::getSourceRange() const {
1555 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001556 Designator &First =
1557 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001558 if (First.isFieldDesignator()) {
1559 if (UsesColonSyntax)
1560 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1561 else
1562 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1563 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001564 StartLoc =
1565 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001566 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1567}
1568
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001569DesignatedInitExpr::designators_iterator
1570DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001571 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1572 Ptr += sizeof(DesignatedInitExpr);
1573 return static_cast<Designator*>(static_cast<void*>(Ptr));
1574}
1575
1576DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1577 return designators_begin() + NumDesignators;
1578}
1579
1580Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1581 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1582 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1583 Ptr += sizeof(DesignatedInitExpr);
1584 Ptr += sizeof(Designator) * NumDesignators;
1585 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1586 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1587}
1588
1589Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1590 assert(D.Kind == Designator::ArrayRangeDesignator &&
1591 "Requires array range designator");
1592 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1593 Ptr += sizeof(DesignatedInitExpr);
1594 Ptr += sizeof(Designator) * NumDesignators;
1595 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1596 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1597}
1598
1599Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1600 assert(D.Kind == Designator::ArrayRangeDesignator &&
1601 "Requires array range designator");
1602 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1603 Ptr += sizeof(DesignatedInitExpr);
1604 Ptr += sizeof(Designator) * NumDesignators;
1605 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1606 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1607}
1608
1609//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001610// ExprIterator.
1611//===----------------------------------------------------------------------===//
1612
1613Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1614Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1615Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1616const Expr* ConstExprIterator::operator[](size_t idx) const {
1617 return cast<Expr>(I[idx]);
1618}
1619const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1620const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1621
1622//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001623// Child Iterators for iterating over subexpressions/substatements
1624//===----------------------------------------------------------------------===//
1625
1626// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001627Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1628Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001629
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001630// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001631Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1632Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001633
Steve Naroff6f786252008-06-02 23:03:37 +00001634// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001635Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1636Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001637
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001638// ObjCKVCRefExpr
1639Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1640Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1641
Douglas Gregord8606632008-11-04 14:56:14 +00001642// ObjCSuperExpr
1643Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1644Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1645
Chris Lattner69909292008-08-10 01:53:14 +00001646// PredefinedExpr
1647Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1648Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001649
1650// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001651Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1652Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001653
1654// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001655Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001656Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001657
1658// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001659Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1660Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001661
Chris Lattner1de66eb2007-08-26 03:42:43 +00001662// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001663Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1664Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001665
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001666// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001667Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1668Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001669
1670// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001671Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1672Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001673
1674// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001675Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1676Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001677
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001678// SizeOfAlignOfExpr
1679Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1680 // If this is of a type and the type is a VLA type (and not a typedef), the
1681 // size expression of the VLA needs to be treated as an executable expression.
1682 // Why isn't this weirdness documented better in StmtIterator?
1683 if (isArgumentType()) {
1684 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1685 getArgumentType().getTypePtr()))
1686 return child_iterator(T);
1687 return child_iterator();
1688 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001689 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001690}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001691Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1692 if (isArgumentType())
1693 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001694 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001695}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001696
1697// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001698Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001699 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001700}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001701Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001702 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001703}
1704
1705// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001706Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001707 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001708}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001709Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001710 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001711}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001712
1713// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001714Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1715Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001716
Nate Begemanaf6ed502008-04-18 23:10:10 +00001717// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001718Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1719Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001720
1721// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001722Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1723Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001724
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001725// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001726Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1727Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001728
1729// BinaryOperator
1730Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001731 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001732}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001733Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001734 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001735}
1736
1737// ConditionalOperator
1738Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001739 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001740}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001741Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001742 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001743}
1744
1745// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001746Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1747Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001748
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001749// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001750Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1751Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001752
1753// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001754Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1755 return child_iterator();
1756}
1757
1758Stmt::child_iterator TypesCompatibleExpr::child_end() {
1759 return child_iterator();
1760}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001761
1762// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001763Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1764Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001765
Douglas Gregorad4b3792008-11-29 04:51:27 +00001766// GNUNullExpr
1767Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1768Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1769
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001770// OverloadExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001771Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1772Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001773
Eli Friedmand0e9d092008-05-14 19:38:39 +00001774// ShuffleVectorExpr
1775Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001776 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001777}
1778Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001779 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001780}
1781
Anders Carlsson36760332007-10-15 20:28:48 +00001782// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001783Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1784Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001785
Anders Carlsson762b7c72007-08-31 04:56:16 +00001786// InitListExpr
1787Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001788 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001789}
1790Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001791 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001792}
1793
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001794// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001795Stmt::child_iterator DesignatedInitExpr::child_begin() {
1796 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1797 Ptr += sizeof(DesignatedInitExpr);
1798 Ptr += sizeof(Designator) * NumDesignators;
1799 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1800}
1801Stmt::child_iterator DesignatedInitExpr::child_end() {
1802 return child_iterator(&*child_begin() + NumSubExprs);
1803}
1804
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001805// ImplicitValueInitExpr
1806Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1807 return child_iterator();
1808}
1809
1810Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1811 return child_iterator();
1812}
1813
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001814// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001815Stmt::child_iterator ObjCStringLiteral::child_begin() {
1816 return child_iterator();
1817}
1818Stmt::child_iterator ObjCStringLiteral::child_end() {
1819 return child_iterator();
1820}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001821
1822// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001823Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1824Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001825
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001826// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001827Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1828 return child_iterator();
1829}
1830Stmt::child_iterator ObjCSelectorExpr::child_end() {
1831 return child_iterator();
1832}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001833
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001834// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001835Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1836 return child_iterator();
1837}
1838Stmt::child_iterator ObjCProtocolExpr::child_end() {
1839 return child_iterator();
1840}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001841
Steve Naroffc39ca262007-09-18 23:55:05 +00001842// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001843Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001844 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001845}
1846Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001847 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001848}
1849
Steve Naroff52a81c02008-09-03 18:15:37 +00001850// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001851Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1852Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001853
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001854Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1855Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }