blob: d9db13338530e5cab4259ef3872cee3def9ae7af [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
Chris Lattneraa491192009-02-18 06:40:38 +000040StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
41 unsigned ByteLength, bool Wide,
42 QualType Ty,
43 SourceLocation *Loc, unsigned NumStrs) {
44 // Allocate enough space for the StringLiteral plus an array of locations for
45 // any concatenated string tokens.
46 void *Mem = C.Allocate(sizeof(StringLiteral)+
47 sizeof(SourceLocation)*(NumStrs-1),
48 llvm::alignof<StringLiteral>());
49 StringLiteral *SL = new (Mem) StringLiteral(Ty);
50
Chris Lattner4b009652007-07-25 00:24:17 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000052 char *AStrData = new (C, 1) char[ByteLength];
53 memcpy(AStrData, StrData, ByteLength);
54 SL->StrData = AStrData;
55 SL->ByteLength = ByteLength;
56 SL->IsWide = Wide;
57 SL->TokLocs[0] = Loc[0];
58 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000059
Chris Lattnerc3144742009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000063}
64
65
Ted Kremenek4f530a92009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000070}
71
Chris Lattner4b009652007-07-25 00:24:17 +000072/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
73/// corresponds to, e.g. "sizeof" or "[pre]++".
74const char *UnaryOperator::getOpcodeStr(Opcode Op) {
75 switch (Op) {
76 default: assert(0 && "Unknown unary operator");
77 case PostInc: return "++";
78 case PostDec: return "--";
79 case PreInc: return "++";
80 case PreDec: return "--";
81 case AddrOf: return "&";
82 case Deref: return "*";
83 case Plus: return "+";
84 case Minus: return "-";
85 case Not: return "~";
86 case LNot: return "!";
87 case Real: return "__real";
88 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +000089 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000091 }
92}
93
94//===----------------------------------------------------------------------===//
95// Postfix Operators.
96//===----------------------------------------------------------------------===//
97
Ted Kremenek362abcd2009-02-09 20:51:47 +000098CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +000099 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000100 : Expr(SC, t,
101 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000102 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000103 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000104
105 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000106 SubExprs[FN] = fn;
107 for (unsigned i = 0; i != numargs; ++i)
108 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000109
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000110 RParenLoc = rparenloc;
111}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000112
Ted Kremenek362abcd2009-02-09 20:51:47 +0000113CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
114 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000115 : Expr(CallExprClass, t,
116 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000117 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000118 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000119
120 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000121 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000123 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000124
Chris Lattner4b009652007-07-25 00:24:17 +0000125 RParenLoc = rparenloc;
126}
127
Ted Kremenek362abcd2009-02-09 20:51:47 +0000128void CallExpr::Destroy(ASTContext& C) {
129 DestroyChildren(C);
130 if (SubExprs) C.Deallocate(SubExprs);
131 this->~CallExpr();
132 C.Deallocate(this);
133}
134
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000135/// setNumArgs - This changes the number of arguments present in this call.
136/// Any orphaned expressions are deleted by this, and any new operands are set
137/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000138void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000139 // No change, just return.
140 if (NumArgs == getNumArgs()) return;
141
142 // If shrinking # arguments, just delete the extras and forgot them.
143 if (NumArgs < getNumArgs()) {
144 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000145 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000146 this->NumArgs = NumArgs;
147 return;
148 }
149
150 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000151 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000152 // Copy over args.
153 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
154 NewSubExprs[i] = SubExprs[i];
155 // Null out new args.
156 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
157 NewSubExprs[i] = 0;
158
Ted Kremenek0c97e042009-02-07 01:47:29 +0000159 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000160 SubExprs = NewSubExprs;
161 this->NumArgs = NumArgs;
162}
163
Chris Lattnerc24915f2008-10-06 05:00:53 +0000164/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
165/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000166unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000167 // All simple function calls (e.g. func()) are implicitly cast to pointer to
168 // function. As a result, we try and obtain the DeclRefExpr from the
169 // ImplicitCastExpr.
170 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
171 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000172 return 0;
173
Steve Naroff44aec4c2008-01-31 01:07:12 +0000174 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
175 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000176 return 0;
177
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000178 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
179 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000180 return 0;
181
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000182 if (!FDecl->getIdentifier())
183 return 0;
184
Douglas Gregorb5af7382009-02-14 18:57:46 +0000185 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000186}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000187
Chris Lattnerc24915f2008-10-06 05:00:53 +0000188
Chris Lattner4b009652007-07-25 00:24:17 +0000189/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
190/// corresponds to, e.g. "<<=".
191const char *BinaryOperator::getOpcodeStr(Opcode Op) {
192 switch (Op) {
193 default: assert(0 && "Unknown binary operator");
194 case Mul: return "*";
195 case Div: return "/";
196 case Rem: return "%";
197 case Add: return "+";
198 case Sub: return "-";
199 case Shl: return "<<";
200 case Shr: return ">>";
201 case LT: return "<";
202 case GT: return ">";
203 case LE: return "<=";
204 case GE: return ">=";
205 case EQ: return "==";
206 case NE: return "!=";
207 case And: return "&";
208 case Xor: return "^";
209 case Or: return "|";
210 case LAnd: return "&&";
211 case LOr: return "||";
212 case Assign: return "=";
213 case MulAssign: return "*=";
214 case DivAssign: return "/=";
215 case RemAssign: return "%=";
216 case AddAssign: return "+=";
217 case SubAssign: return "-=";
218 case ShlAssign: return "<<=";
219 case ShrAssign: return ">>=";
220 case AndAssign: return "&=";
221 case XorAssign: return "^=";
222 case OrAssign: return "|=";
223 case Comma: return ",";
224 }
225}
226
Anders Carlsson762b7c72007-08-31 04:56:16 +0000227InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000228 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000229 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000230 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000231 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000232 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000233
234 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000235}
Chris Lattner4b009652007-07-25 00:24:17 +0000236
Douglas Gregorf603b472009-01-28 21:54:33 +0000237void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000238 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000239 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-01-28 21:54:33 +0000240 delete InitExprs[Idx];
241 InitExprs.resize(NumInits, 0);
242}
243
244Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
245 if (Init >= InitExprs.size()) {
246 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
247 InitExprs.back() = expr;
248 return 0;
249 }
250
251 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
252 InitExprs[Init] = expr;
253 return Result;
254}
255
Steve Naroff6f373332008-09-04 15:31:07 +0000256/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000257///
258const FunctionType *BlockExpr::getFunctionType() const {
259 return getType()->getAsBlockPointerType()->
260 getPointeeType()->getAsFunctionType();
261}
262
Steve Naroff9ac456d2008-10-08 17:01:13 +0000263SourceLocation BlockExpr::getCaretLocation() const {
264 return TheBlock->getCaretLocation();
265}
266const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
267Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
268
269
Chris Lattner4b009652007-07-25 00:24:17 +0000270//===----------------------------------------------------------------------===//
271// Generic Expression Routines
272//===----------------------------------------------------------------------===//
273
Chris Lattnerd2c66552009-02-14 07:37:35 +0000274/// isUnusedResultAWarning - Return true if this immediate expression should
275/// be warned about if the result is unused. If so, fill in Loc and Ranges
276/// with location to warn on and the source range[s] to report with the
277/// warning.
278bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
279 SourceRange &R2) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000280 switch (getStmtClass()) {
281 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000282 Loc = getExprLoc();
283 R1 = getSourceRange();
284 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000285 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000286 return cast<ParenExpr>(this)->getSubExpr()->
287 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000288 case UnaryOperatorClass: {
289 const UnaryOperator *UO = cast<UnaryOperator>(this);
290
291 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000292 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000293 case UnaryOperator::PostInc:
294 case UnaryOperator::PostDec:
295 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000296 case UnaryOperator::PreDec: // ++/--
297 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000298 case UnaryOperator::Deref:
299 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000300 if (getType().isVolatileQualified())
301 return false;
302 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 case UnaryOperator::Real:
304 case UnaryOperator::Imag:
305 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000306 if (UO->getSubExpr()->getType().isVolatileQualified())
307 return false;
308 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000309 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000310 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000311 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000312 Loc = UO->getOperatorLoc();
313 R1 = UO->getSubExpr()->getSourceRange();
314 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000316 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000317 const BinaryOperator *BO = cast<BinaryOperator>(this);
318 // Consider comma to have side effects if the LHS or RHS does.
319 if (BO->getOpcode() == BinaryOperator::Comma)
320 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
321 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000322
Chris Lattnerd2c66552009-02-14 07:37:35 +0000323 if (BO->isAssignmentOp())
324 return false;
325 Loc = BO->getOperatorLoc();
326 R1 = BO->getLHS()->getSourceRange();
327 R2 = BO->getRHS()->getSourceRange();
328 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000329 }
Chris Lattner06078d22007-08-25 02:00:02 +0000330 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000331 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000332
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000333 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000334 // The condition must be evaluated, but if either the LHS or RHS is a
335 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000336 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000337 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
338 return true;
339 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000340 }
341
Chris Lattner4b009652007-07-25 00:24:17 +0000342 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000343 // If the base pointer or element is to a volatile pointer/field, accessing
344 // it is a side effect.
345 if (getType().isVolatileQualified())
346 return false;
347 Loc = cast<MemberExpr>(this)->getMemberLoc();
348 R1 = SourceRange(Loc, Loc);
349 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
350 return true;
351
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case ArraySubscriptExprClass:
353 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000354 // it is a side effect.
355 if (getType().isVolatileQualified())
356 return false;
357 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
358 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
359 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
360 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 case CallExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000363 case CXXOperatorCallExprClass: {
364 // If this is a direct call, get the callee.
365 const CallExpr *CE = cast<CallExpr>(this);
366 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
367 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
368 // If the callee has attribute pure, const, or warn_unused_result, warn
369 // about it. void foo() { strlen("bar"); } should warn.
370 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
371 if (FD->getAttr<WarnUnusedResultAttr>() ||
372 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
373 Loc = CE->getCallee()->getLocStart();
374 R1 = CE->getCallee()->getSourceRange();
375
376 if (unsigned NumArgs = CE->getNumArgs())
377 R2 = SourceRange(CE->getArg(0)->getLocStart(),
378 CE->getArg(NumArgs-1)->getLocEnd());
379 return true;
380 }
381 }
382 return false;
383 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000384 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000385 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000386 case StmtExprClass: {
387 // Statement exprs don't logically have side effects themselves, but are
388 // sometimes used in macros in ways that give them a type that is unused.
389 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
390 // however, if the result of the stmt expr is dead, we don't want to emit a
391 // warning.
392 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
393 if (!CS->body_empty())
394 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000395 return E->isUnusedResultAWarning(Loc, R1, R2);
396
397 Loc = cast<StmtExpr>(this)->getLParenLoc();
398 R1 = getSourceRange();
399 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000400 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000401 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000402 // If this is a cast to void, check the operand. Otherwise, the result of
403 // the cast is unused.
404 if (getType()->isVoidType())
405 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
406 R1, R2);
407 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
408 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
409 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000410 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000411 // If this is a cast to void, check the operand. Otherwise, the result of
412 // the cast is unused.
413 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000414 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
415 R1, R2);
416 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
417 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
418 return true;
419
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000420 case ImplicitCastExprClass:
421 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000422 return cast<ImplicitCastExpr>(this)
423 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000424
Chris Lattner3e254fb2008-04-08 04:40:51 +0000425 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000426 return cast<CXXDefaultArgExpr>(this)
427 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000428
429 case CXXNewExprClass:
430 // FIXME: In theory, there might be new expressions that don't have side
431 // effects (e.g. a placement new with an uninitialized POD).
432 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000433 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000434 }
Chris Lattner4b009652007-07-25 00:24:17 +0000435}
436
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000437/// DeclCanBeLvalue - Determine whether the given declaration can be
438/// an lvalue. This is a helper routine for isLvalue.
439static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000440 // C++ [temp.param]p6:
441 // A non-type non-reference template-parameter is not an lvalue.
442 if (const NonTypeTemplateParmDecl *NTTParm
443 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
444 return NTTParm->getType()->isReferenceType();
445
Douglas Gregor8acb7272008-12-11 16:49:14 +0000446 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000447 // C++ 3.10p2: An lvalue refers to an object or function.
448 (Ctx.getLangOptions().CPlusPlus &&
449 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
450}
451
Chris Lattner4b009652007-07-25 00:24:17 +0000452/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
453/// incomplete type other than void. Nonarray expressions that can be lvalues:
454/// - name, where name must be a variable
455/// - e[i]
456/// - (e), where e must be an lvalue
457/// - e.name, where e must be an lvalue
458/// - e->name
459/// - *e, the type of e cannot be a function type
460/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000461/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000462/// - reference type [C++ [expr]]
463///
Chris Lattner25168a52008-07-26 21:30:36 +0000464Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000465 // first, check the type (C99 6.3.2.1). Expressions with function
466 // type in C are not lvalues, but they can be lvalues in C++.
467 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000468 return LV_NotObjectType;
469
Steve Naroffec7736d2008-02-10 01:39:04 +0000470 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000471 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000472 return LV_IncompleteVoidType;
473
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000474 /// FIXME: Expressions can't have reference type, so the following
475 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000476 if (TR->isReferenceType()) // C++ [expr]
477 return LV_Valid;
478
479 // the type looks fine, now check the expression
480 switch (getStmtClass()) {
481 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000482 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000483 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
484 // For vectors, make sure base is an lvalue (i.e. not a function call).
485 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000486 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000487 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000488 case DeclRefExprClass:
489 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000490 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
491 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000492 return LV_Valid;
493 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000494 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000495 case BlockDeclRefExprClass: {
496 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000497 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000498 return LV_Valid;
499 break;
500 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000501 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000502 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000503 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
504 NamedDecl *Member = m->getMemberDecl();
505 // C++ [expr.ref]p4:
506 // If E2 is declared to have type "reference to T", then E1.E2
507 // is an lvalue.
508 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
509 if (Value->getType()->isReferenceType())
510 return LV_Valid;
511
512 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
513 if (isa<CXXClassVarDecl>(Member))
514 return LV_Valid;
515
516 // -- If E2 is a non-static data member [...]. If E1 is an
517 // lvalue, then E1.E2 is an lvalue.
518 if (isa<FieldDecl>(Member))
519 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
520
521 // -- If it refers to a static member function [...], then
522 // E1.E2 is an lvalue.
523 // -- Otherwise, if E1.E2 refers to a non-static member
524 // function [...], then E1.E2 is not an lvalue.
525 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
526 return Method->isStatic()? LV_Valid : LV_MemberFunction;
527
528 // -- If E2 is a member enumerator [...], the expression E1.E2
529 // is not an lvalue.
530 if (isa<EnumConstantDecl>(Member))
531 return LV_InvalidExpression;
532
533 // Not an lvalue.
534 return LV_InvalidExpression;
535 }
536
537 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000538 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000539 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000540 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000541 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000542 return LV_Valid; // C99 6.5.3p4
543
544 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000545 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
546 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000547 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000548
549 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
550 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
551 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
552 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000553 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000554 case ImplicitCastExprClass:
555 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
556 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000557 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000558 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000559 case BinaryOperatorClass:
560 case CompoundAssignOperatorClass: {
561 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000562
563 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
564 BinOp->getOpcode() == BinaryOperator::Comma)
565 return BinOp->getRHS()->isLvalue(Ctx);
566
Sebastian Redl95216a62009-02-07 00:15:38 +0000567 // C++ [expr.mptr.oper]p6
568 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
569 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
570 !BinOp->getType()->isFunctionType())
571 return BinOp->getLHS()->isLvalue(Ctx);
572
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000573 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000574 return LV_InvalidExpression;
575
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000576 if (Ctx.getLangOptions().CPlusPlus)
577 // C++ [expr.ass]p1:
578 // The result of an assignment operation [...] is an lvalue.
579 return LV_Valid;
580
581
582 // C99 6.5.16:
583 // An assignment expression [...] is not an lvalue.
584 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000585 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000586 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000587 case CXXOperatorCallExprClass:
588 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000589 // C++ [expr.call]p10:
590 // A function call is an lvalue if and only if the result type
591 // is a reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000592 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000593 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000594 CalleeType = FnTypePtr->getPointeeType();
595 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
596 if (FnType->getResultType()->isReferenceType())
597 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000598
599 break;
600 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000601 case CompoundLiteralExprClass: // C99 6.5.2.5p5
602 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000603 case ChooseExprClass:
604 // __builtin_choose_expr is an lvalue if the selected operand is.
605 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
606 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
607 else
608 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
609
Nate Begemanaf6ed502008-04-18 23:10:10 +0000610 case ExtVectorElementExprClass:
611 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000612 return LV_DuplicateVectorComponents;
613 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000614 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
615 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000616 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
617 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000618 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000619 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000620 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000621 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000622 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000623 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000624 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000625 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000626 case CXXConditionDeclExprClass:
627 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000628 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000629 case CXXFunctionalCastExprClass:
630 case CXXStaticCastExprClass:
631 case CXXDynamicCastExprClass:
632 case CXXReinterpretCastExprClass:
633 case CXXConstCastExprClass:
634 // The result of an explicit cast is an lvalue if the type we are
635 // casting to is a reference type. See C++ [expr.cast]p1,
636 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
637 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
638 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
639 return LV_Valid;
640 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000641 case CXXTypeidExprClass:
642 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
643 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000644 default:
645 break;
646 }
647 return LV_InvalidExpression;
648}
649
650/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
651/// does not have an incomplete type, does not have a const-qualified type, and
652/// if it is a structure or union, does not have any member (including,
653/// recursively, any member or element of all contained aggregates or unions)
654/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000655Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
656 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000657
658 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000659 case LV_Valid:
660 // C++ 3.10p11: Functions cannot be modified, but pointers to
661 // functions can be modifiable.
662 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
663 return MLV_NotObjectType;
664 break;
665
Chris Lattner4b009652007-07-25 00:24:17 +0000666 case LV_NotObjectType: return MLV_NotObjectType;
667 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000668 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000669 case LV_InvalidExpression:
670 // If the top level is a C-style cast, and the subexpression is a valid
671 // lvalue, then this is probably a use of the old-school "cast as lvalue"
672 // GCC extension. We don't support it, but we want to produce good
673 // diagnostics when it happens so that the user knows why.
674 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
675 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
676 return MLV_LValueCast;
677 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000678 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000679 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000680
681 QualType CT = Ctx.getCanonicalType(getType());
682
683 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000684 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000685 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000686 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000687 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000688 return MLV_IncompleteType;
689
Chris Lattnera1923f62008-08-04 07:31:14 +0000690 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000691 if (r->hasConstFields())
692 return MLV_ConstQualified;
693 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000694 // The following is illegal:
695 // void takeclosure(void (^C)(void));
696 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
697 //
698 if (getStmtClass() == BlockDeclRefExprClass) {
699 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
700 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
701 return MLV_NotBlockQualified;
702 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000703
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000704 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000705 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000706 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
707 if (KVCExpr->getSetterMethod() == 0)
708 return MLV_NoSetterProperty;
709 }
Chris Lattner4b009652007-07-25 00:24:17 +0000710 return MLV_Valid;
711}
712
Ted Kremenek5778d622008-02-27 18:39:48 +0000713/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000714/// duration. This means that the address of this expression is a link-time
715/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000716bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000717 switch (getStmtClass()) {
718 default:
719 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000720 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000721 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000722 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000723 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000724 case CompoundLiteralExprClass:
725 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000726 case DeclRefExprClass:
727 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000728 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
729 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000730 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000731 if (isa<FunctionDecl>(D))
732 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000733 return false;
734 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000735 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000736 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000737 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000738 }
Chris Lattner743ec372007-11-27 21:35:27 +0000739 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000740 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000741 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000742 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000743 case CXXDefaultArgExprClass:
744 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000745 }
746}
747
Ted Kremenek87e30c52008-01-17 16:57:34 +0000748Expr* Expr::IgnoreParens() {
749 Expr* E = this;
750 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
751 E = P->getSubExpr();
752
753 return E;
754}
755
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000756/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
757/// or CastExprs or ImplicitCastExprs, returning their operand.
758Expr *Expr::IgnoreParenCasts() {
759 Expr *E = this;
760 while (true) {
761 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
762 E = P->getSubExpr();
763 else if (CastExpr *P = dyn_cast<CastExpr>(E))
764 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000765 else
766 return E;
767 }
768}
769
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000770/// hasAnyTypeDependentArguments - Determines if any of the expressions
771/// in Exprs is type-dependent.
772bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
773 for (unsigned I = 0; I < NumExprs; ++I)
774 if (Exprs[I]->isTypeDependent())
775 return true;
776
777 return false;
778}
779
780/// hasAnyValueDependentArguments - Determines if any of the expressions
781/// in Exprs is value-dependent.
782bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
783 for (unsigned I = 0; I < NumExprs; ++I)
784 if (Exprs[I]->isValueDependent())
785 return true;
786
787 return false;
788}
789
Eli Friedmandee41122009-01-25 02:32:41 +0000790bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000791 // This function is attempting whether an expression is an initializer
792 // which can be evaluated at compile-time. isEvaluatable handles most
793 // of the cases, but it can't deal with some initializer-specific
794 // expressions, and it can't deal with aggregates; we deal with those here,
795 // and fall back to isEvaluatable for the other cases.
796
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000797 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000798 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000799 case StringLiteralClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000800 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000801 case CompoundLiteralExprClass: {
802 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000803 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000804 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000805 case InitListExprClass: {
806 const InitListExpr *Exp = cast<InitListExpr>(this);
807 unsigned numInits = Exp->getNumInits();
808 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +0000809 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000810 return false;
811 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000812 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000813 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000814 case ImplicitValueInitExprClass:
815 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +0000816 case ParenExprClass: {
817 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
818 }
819 case UnaryOperatorClass: {
820 const UnaryOperator* Exp = cast<UnaryOperator>(this);
821 if (Exp->getOpcode() == UnaryOperator::Extension)
822 return Exp->getSubExpr()->isConstantInitializer(Ctx);
823 break;
824 }
825 case CStyleCastExprClass:
826 // Handle casts with a destination that's a struct or union; this
827 // deals with both the gcc no-op struct cast extension and the
828 // cast-to-union extension.
829 if (getType()->isRecordType())
830 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
831 break;
Eli Friedmanc8a00142009-01-25 03:27:40 +0000832 case DesignatedInitExprClass:
Sebastian Redl94889622009-01-25 13:34:47 +0000833 return cast<DesignatedInitExpr>(this)->
834 getInit()->isConstantInitializer(Ctx);
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000835 }
836
Eli Friedman2b0dec52009-01-25 03:12:18 +0000837 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000838}
839
Chris Lattner4b009652007-07-25 00:24:17 +0000840/// isIntegerConstantExpr - this recursive routine will test if an expression is
841/// an integer constant expression. Note: With the introduction of VLA's in
842/// C99 the result of the sizeof operator is no longer always a constant
843/// expression. The generalization of the wording to include any subexpression
844/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
845/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000846/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000847/// occurring in a context requiring a constant, would have been a constraint
848/// violation. FIXME: This routine currently implements C90 semantics.
849/// To properly implement C99 semantics this routine will need to evaluate
850/// expressions involving operators previously mentioned.
851
852/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
853/// comma, etc
854///
855/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000856/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000857///
858/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
859/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
860/// cast+dereference.
861bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
862 SourceLocation *Loc, bool isEvaluated) const {
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000863 if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
864 return false;
865 assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
866 return true;
867}
868
869bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
870 SourceLocation *Loc, bool isEvaluated) const {
871
Eli Friedman14cc7542008-11-13 06:09:17 +0000872 // Pretest for integral type; some parts of the code crash for types that
873 // can't be sized.
874 if (!getType()->isIntegralType()) {
875 if (Loc) *Loc = getLocStart();
876 return false;
877 }
Chris Lattner4b009652007-07-25 00:24:17 +0000878 switch (getStmtClass()) {
879 default:
880 if (Loc) *Loc = getLocStart();
881 return false;
882 case ParenExprClass:
883 return cast<ParenExpr>(this)->getSubExpr()->
884 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
885 case IntegerLiteralClass:
Daniel Dunbar168b20c2009-02-18 00:47:45 +0000886 // NOTE: getValue() returns an APInt, we must set sign.
Chris Lattner4b009652007-07-25 00:24:17 +0000887 Result = cast<IntegerLiteral>(this)->getValue();
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000888 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner4b009652007-07-25 00:24:17 +0000889 break;
890 case CharacterLiteralClass: {
891 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000892 Result = Ctx.MakeIntValue(CL->getValue(), getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000893 break;
894 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000895 case CXXBoolLiteralExprClass: {
896 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000897 Result = Ctx.MakeIntValue(BL->getValue(), getType());
Anders Carlssoned6c2142008-08-23 21:12:35 +0000898 break;
899 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000900 case CXXZeroInitValueExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000901 Result = Ctx.MakeIntValue(0, getType());
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000902 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000903 case TypesCompatibleExprClass: {
904 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Daniel Dunbarda8ebd22008-10-24 08:07:57 +0000905 // Per gcc docs "this built-in function ignores top level
906 // qualifiers". We need to use the canonical version to properly
907 // be able to strip CRV qualifiers from the type.
908 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
909 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000910 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
911 T1.getUnqualifiedType()),
912 getType());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000913 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000914 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000915 case CallExprClass:
916 case CXXOperatorCallExprClass: {
Steve Naroff8d3b1702007-08-08 22:15:55 +0000917 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1eee9402008-10-06 06:40:35 +0000918
919 // If this is a call to a builtin function, constant fold it otherwise
920 // reject it.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000921 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson8c3de802008-12-19 20:58:05 +0000922 EvalResult EvalResult;
923 if (CE->Evaluate(EvalResult, Ctx)) {
924 assert(!EvalResult.HasSideEffects &&
925 "Foldable builtin call should not have side effects!");
926 Result = EvalResult.Val.getInt();
Chris Lattner1eee9402008-10-06 06:40:35 +0000927 break; // It is a constant, expand it.
928 }
929 }
930
Steve Naroff8d3b1702007-08-08 22:15:55 +0000931 if (Loc) *Loc = getLocStart();
932 return false;
933 }
Chris Lattner4b009652007-07-25 00:24:17 +0000934 case DeclRefExprClass:
Douglas Gregor566782a2009-01-06 05:10:23 +0000935 case QualifiedDeclRefExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000936 if (const EnumConstantDecl *D =
937 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
938 Result = D->getInitVal();
939 break;
940 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +0000941 if (Ctx.getLangOptions().CPlusPlus &&
942 getType().getCVRQualifiers() == QualType::Const) {
943 // C++ 7.1.5.1p2
944 // A variable of non-volatile const-qualified integral or enumeration
945 // type initialized by an ICE can be used in ICEs.
946 if (const VarDecl *Dcl =
947 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
948 if (const Expr *Init = Dcl->getInit())
949 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
950 }
951 }
Chris Lattner4b009652007-07-25 00:24:17 +0000952 if (Loc) *Loc = getLocStart();
953 return false;
954 case UnaryOperatorClass: {
955 const UnaryOperator *Exp = cast<UnaryOperator>(this);
956
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000957 // Get the operand value. If this is offsetof, do not evalute the
Chris Lattner4b009652007-07-25 00:24:17 +0000958 // operand. This affects C99 6.6p3.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000959 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
960 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000961 return false;
962
963 switch (Exp->getOpcode()) {
964 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
965 // See C99 6.6p3.
966 default:
967 if (Loc) *Loc = Exp->getOperatorLoc();
968 return false;
969 case UnaryOperator::Extension:
970 return true; // FIXME: this is wrong.
Chris Lattner4b009652007-07-25 00:24:17 +0000971 case UnaryOperator::LNot: {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000972 Result = Ctx.MakeIntValue(Result == 0, getType());
Chris Lattner4b009652007-07-25 00:24:17 +0000973 break;
974 }
975 case UnaryOperator::Plus:
976 break;
977 case UnaryOperator::Minus:
978 Result = -Result;
979 break;
980 case UnaryOperator::Not:
981 Result = ~Result;
982 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000983 case UnaryOperator::OffsetOf:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000984 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
985 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000986 }
987 break;
988 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000989 case SizeOfAlignOfExprClass: {
990 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000991
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000992 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattner20515462008-02-21 05:45:29 +0000993 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000994 if (ArgTy->isVoidType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +0000995 Result = Ctx.MakeIntValue(1, getType());
Chris Lattner20515462008-02-21 05:45:29 +0000996 break;
997 }
998
999 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001000 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001001 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +00001002 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +00001003 }
Chris Lattner4b009652007-07-25 00:24:17 +00001004
Chris Lattner4b009652007-07-25 00:24:17 +00001005 // Get information about the size or align.
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001006 if (ArgTy->isFunctionType()) {
Chris Lattnerd4dc2672008-01-02 21:54:09 +00001007 // GCC extension: sizeof(function) = 1.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001008 Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001009 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001010 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001011 if (Exp->isSizeOf())
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001012 Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType());
Anders Carlsson8d2b2b72008-02-16 01:20:23 +00001013 else
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001014 Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +00001015 }
Chris Lattner4b009652007-07-25 00:24:17 +00001016 break;
1017 }
1018 case BinaryOperatorClass: {
1019 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001020 llvm::APSInt LHS, RHS;
1021
1022 // Initialize result to have correct signedness and width.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001023 Result = Ctx.MakeIntValue(0, getType());
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001024
Chris Lattner4b009652007-07-25 00:24:17 +00001025 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001026 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001027 return false;
1028
Chris Lattner4b009652007-07-25 00:24:17 +00001029 // The short-circuiting &&/|| operators don't necessarily evaluate their
1030 // RHS. Make sure to pass isEvaluated down correctly.
1031 if (Exp->isLogicalOp()) {
1032 bool RHSEval;
1033 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001034 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001035 else {
1036 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001037 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001038 }
1039
1040 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
1041 isEvaluated & RHSEval))
1042 return false;
1043 } else {
1044 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
1045 return false;
1046 }
1047
1048 switch (Exp->getOpcode()) {
1049 default:
1050 if (Loc) *Loc = getLocStart();
1051 return false;
1052 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001053 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001054 break;
1055 case BinaryOperator::Div:
1056 if (RHS == 0) {
1057 if (!isEvaluated) break;
1058 if (Loc) *Loc = getLocStart();
1059 return false;
1060 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001061 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001062 break;
1063 case BinaryOperator::Rem:
1064 if (RHS == 0) {
1065 if (!isEvaluated) break;
1066 if (Loc) *Loc = getLocStart();
1067 return false;
1068 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001069 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +00001070 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001071 case BinaryOperator::Add: Result = LHS + RHS; break;
1072 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001073 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001074 Result = LHS <<
1075 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1076 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001077 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001078 Result = LHS >>
1079 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +00001080 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001081 case BinaryOperator::LT: Result = LHS < RHS; break;
1082 case BinaryOperator::GT: Result = LHS > RHS; break;
1083 case BinaryOperator::LE: Result = LHS <= RHS; break;
1084 case BinaryOperator::GE: Result = LHS >= RHS; break;
1085 case BinaryOperator::EQ: Result = LHS == RHS; break;
1086 case BinaryOperator::NE: Result = LHS != RHS; break;
1087 case BinaryOperator::And: Result = LHS & RHS; break;
1088 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1089 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +00001090 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001091 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001092 break;
1093 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +00001094 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001095 break;
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001096
1097 case BinaryOperator::Comma:
1098 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1099 // *except* when they are contained within a subexpression that is not
1100 // evaluated". Note that Assignment can never happen due to constraints
1101 // on the LHS subexpr, so we don't need to check it here.
1102 if (isEvaluated) {
1103 if (Loc) *Loc = getLocStart();
1104 return false;
1105 }
1106
1107 // The result of the constant expr is the RHS.
1108 Result = RHS;
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001109 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001110 }
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001111
Chris Lattner4b009652007-07-25 00:24:17 +00001112 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1113 break;
1114 }
1115 case ImplicitCastExprClass:
Douglas Gregor035d0882008-10-28 15:36:24 +00001116 case CStyleCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +00001117 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001118 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1119 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +00001120
1121 // C99 6.6p6: shall only convert arithmetic types to integer types.
1122 if (!SubExpr->getType()->isArithmeticType() ||
1123 !getType()->isIntegerType()) {
1124 if (Loc) *Loc = SubExpr->getLocStart();
1125 return false;
1126 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001127
Chris Lattner8cd0e932008-03-05 18:54:05 +00001128 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001129
Chris Lattner4b009652007-07-25 00:24:17 +00001130 // Handle simple integer->integer casts.
1131 if (SubExpr->getType()->isIntegerType()) {
1132 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1133 return false;
1134
1135 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +00001136 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +00001137 if (getType()->isBooleanType()) {
1138 // Conversion to bool compares against zero.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001139 Result = Ctx.MakeIntValue(Result != 0, getType());
1140 } else if (SubExpr->getType()->isSignedIntegerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001141 Result.sextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001142 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1143 } else { // If the input is unsigned, do a zero extend, noop,
1144 // or truncate.
Chris Lattner4b009652007-07-25 00:24:17 +00001145 Result.zextOrTrunc(DestWidth);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001146 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1147 }
Chris Lattner4b009652007-07-25 00:24:17 +00001148 break;
1149 }
1150
1151 // Allow floating constants that are the immediate operands of casts or that
1152 // are parenthesized.
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001153 const Expr *Operand = SubExpr->IgnoreParens();
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001154
1155 // If this isn't a floating literal, we can't handle it.
1156 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1157 if (!FL) {
1158 if (Loc) *Loc = Operand->getLocStart();
1159 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001160 }
Chris Lattner000c4102008-01-09 18:59:34 +00001161
1162 // If the destination is boolean, compare against zero.
1163 if (getType()->isBooleanType()) {
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001164 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
Chris Lattner000c4102008-01-09 18:59:34 +00001165 break;
1166 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001167
1168 // Determine whether we are converting to unsigned or signed.
1169 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +00001170
1171 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1172 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +00001173 uint64_t Space[4];
1174 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +00001175 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +00001176 llvm::APFloat::rmTowardZero,
1177 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001178 Result = llvm::APInt(DestWidth, 4, Space);
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001179 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner76a0c1b2007-09-22 19:04:13 +00001180 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001181 }
1182 case ConditionalOperatorClass: {
1183 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1184
Chris Lattner45e71bf2008-12-12 06:55:44 +00001185 const Expr *Cond = Exp->getCond();
1186
1187 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001188 return false;
1189
1190 const Expr *TrueExp = Exp->getLHS();
1191 const Expr *FalseExp = Exp->getRHS();
1192 if (Result == 0) std::swap(TrueExp, FalseExp);
1193
Chris Lattner45e71bf2008-12-12 06:55:44 +00001194 // If the condition (ignoring parens) is a __builtin_constant_p call,
1195 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001196 // expression, and it is fully evaluated. This is an important GNU
1197 // extension. See GCC PR38377 for discussion.
Chris Lattner45e71bf2008-12-12 06:55:44 +00001198 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001199 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001200 EvalResult EVResult;
1201 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1202 return false;
1203 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1204 Result = EVResult.Val.getInt();
1205 if (Loc) *Loc = EVResult.DiagLoc;
1206 return true;
1207 }
Chris Lattner45e71bf2008-12-12 06:55:44 +00001208
Chris Lattner4b009652007-07-25 00:24:17 +00001209 // Evaluate the false one first, discard the result.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001210 llvm::APSInt Tmp;
1211 if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001212 return false;
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001213 // Evalute the true one, capture the result. Note that if TrueExp
1214 // is False then this is an instant of the gcc missing LHS
1215 // extension, and we will just reuse Result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001216 if (TrueExp &&
1217 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001218 return false;
1219 break;
1220 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001221 case CXXDefaultArgExprClass:
1222 return cast<CXXDefaultArgExpr>(this)
1223 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl39c0f6f2009-01-05 20:52:13 +00001224
1225 case UnaryTypeTraitExprClass:
Daniel Dunbar7458f4a2009-02-18 00:32:53 +00001226 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1227 getType());
1228 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001229 }
1230
Chris Lattner4b009652007-07-25 00:24:17 +00001231 return true;
1232}
1233
Chris Lattner4b009652007-07-25 00:24:17 +00001234/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1235/// integer constant expression with the value zero, or if this is one that is
1236/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001237bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1238{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001239 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001240 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001241 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001242 // Check that it is a cast to void*.
1243 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1244 QualType Pointee = PT->getPointeeType();
1245 if (Pointee.getCVRQualifiers() == 0 &&
1246 Pointee->isVoidType() && // to void*
1247 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001248 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001249 }
Chris Lattner4b009652007-07-25 00:24:17 +00001250 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001251 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1252 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001253 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001254 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1255 // Accept ((void*)0) as a null pointer constant, as many other
1256 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001257 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001258 } else if (const CXXDefaultArgExpr *DefaultArg
1259 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001260 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001261 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001262 } else if (isa<GNUNullExpr>(this)) {
1263 // The GNU __null extension is always a null pointer constant.
1264 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001265 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001266
Steve Naroffa2e53222008-01-14 16:10:57 +00001267 // This expression must be an integer type.
1268 if (!getType()->isIntegerType())
1269 return false;
1270
Chris Lattner4b009652007-07-25 00:24:17 +00001271 // If we have an integer constant expression, we need to *evaluate* it and
1272 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001273 // FIXME: We should probably return false if we're compiling in strict mode
1274 // and Diag is not null (this indicates that the value was foldable but not
1275 // an ICE.
1276 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001277 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001278 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001279}
Steve Naroffc11705f2007-07-28 23:10:27 +00001280
Douglas Gregor81c29152008-10-29 00:13:59 +00001281/// isBitField - Return true if this expression is a bit-field.
1282bool Expr::isBitField() {
1283 Expr *E = this->IgnoreParenCasts();
1284 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001285 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1286 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001287 return false;
1288}
1289
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001290/// isArrow - Return true if the base expression is a pointer to vector,
1291/// return false if the base expression is a vector.
1292bool ExtVectorElementExpr::isArrow() const {
1293 return getBase()->getType()->isPointerType();
1294}
1295
Nate Begemanaf6ed502008-04-18 23:10:10 +00001296unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001297 if (const VectorType *VT = getType()->getAsVectorType())
1298 return VT->getNumElements();
1299 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001300}
1301
Nate Begemanc8e51f82008-05-09 06:41:27 +00001302/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001303bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001304 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001305 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001306
1307 // Halving swizzles do not contain duplicate elements.
1308 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1309 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1310 return false;
1311
1312 // Advance past s-char prefix on hex swizzles.
1313 if (*compStr == 's') {
1314 compStr++;
1315 length--;
1316 }
Steve Naroffba67f692007-07-30 03:29:09 +00001317
Chris Lattner58d3fa52008-11-19 07:55:04 +00001318 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001319 const char *s = compStr+i;
1320 for (const char c = *s++; *s; s++)
1321 if (c == *s)
1322 return true;
1323 }
1324 return false;
1325}
Chris Lattner42158e72007-08-02 23:36:59 +00001326
Nate Begemanc8e51f82008-05-09 06:41:27 +00001327/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001328void ExtVectorElementExpr::getEncodedElementAccess(
1329 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001330 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001331 if (*compStr == 's')
1332 compStr++;
1333
1334 bool isHi = !strcmp(compStr, "hi");
1335 bool isLo = !strcmp(compStr, "lo");
1336 bool isEven = !strcmp(compStr, "even");
1337 bool isOdd = !strcmp(compStr, "odd");
1338
Nate Begemanc8e51f82008-05-09 06:41:27 +00001339 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1340 uint64_t Index;
1341
1342 if (isHi)
1343 Index = e + i;
1344 else if (isLo)
1345 Index = i;
1346 else if (isEven)
1347 Index = 2 * i;
1348 else if (isOdd)
1349 Index = 2 * i + 1;
1350 else
1351 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001352
Nate Begemana1ae7442008-05-13 21:03:02 +00001353 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001354 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001355}
1356
Steve Naroff4ed9d662007-09-27 14:38:14 +00001357// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001358ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001359 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001360 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001361 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001362 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001363 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001364 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001365 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001366 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001367 if (NumArgs) {
1368 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001369 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1370 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001371 LBracloc = LBrac;
1372 RBracloc = RBrac;
1373}
1374
Steve Naroff4ed9d662007-09-27 14:38:14 +00001375// constructor for class messages.
1376// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001377ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001378 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001379 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001380 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001381 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001382 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001383 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001384 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001385 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001386 if (NumArgs) {
1387 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001388 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1389 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001390 LBracloc = LBrac;
1391 RBracloc = RBrac;
1392}
1393
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001394// constructor for class messages.
1395ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1396 QualType retType, ObjCMethodDecl *mproto,
1397 SourceLocation LBrac, SourceLocation RBrac,
1398 Expr **ArgExprs, unsigned nargs)
1399: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1400MethodProto(mproto) {
1401 NumArgs = nargs;
1402 SubExprs = new Stmt*[NumArgs+1];
1403 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1404 if (NumArgs) {
1405 for (unsigned i = 0; i != NumArgs; ++i)
1406 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1407 }
1408 LBracloc = LBrac;
1409 RBracloc = RBrac;
1410}
1411
1412ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1413 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1414 switch (x & Flags) {
1415 default:
1416 assert(false && "Invalid ObjCMessageExpr.");
1417 case IsInstMeth:
1418 return ClassInfo(0, 0);
1419 case IsClsMethDeclUnknown:
1420 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1421 case IsClsMethDeclKnown: {
1422 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1423 return ClassInfo(D, D->getIdentifier());
1424 }
1425 }
1426}
1427
Chris Lattnerf624cd22007-10-25 00:29:32 +00001428bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001429 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001430}
1431
Chris Lattnera5f779dc2008-12-12 05:35:08 +00001432static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson52774ad2008-01-29 15:56:48 +00001433 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1434 QualType Ty = ME->getBase()->getType();
1435
1436 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001437 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor82d44772008-12-20 23:49:58 +00001438 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1439 // FIXME: This is linear time. And the fact that we're indexing
1440 // into the layout by position in the record means that we're
1441 // either stuck numbering the fields in the AST or we have to keep
1442 // the linear search (yuck and yuck).
1443 unsigned i = 0;
1444 for (RecordDecl::field_iterator Field = RD->field_begin(),
1445 FieldEnd = RD->field_end();
1446 Field != FieldEnd; (void)++Field, ++i) {
1447 if (*Field == FD)
1448 break;
1449 }
1450
1451 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson52774ad2008-01-29 15:56:48 +00001452 }
Anders Carlsson52774ad2008-01-29 15:56:48 +00001453 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1454 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001455
Chris Lattner8cd0e932008-03-05 18:54:05 +00001456 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001457 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001458
1459 return size + evaluateOffsetOf(C, Base);
1460 } else if (isa<CompoundLiteralExpr>(E))
1461 return 0;
1462
1463 assert(0 && "Unknown offsetof subexpression!");
1464 return 0;
1465}
1466
1467int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1468{
1469 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1470
Chris Lattner8cd0e932008-03-05 18:54:05 +00001471 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001472 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001473}
1474
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001475void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1476 // Override default behavior of traversing children. If this has a type
1477 // operand and the type is a variable-length array, the child iteration
1478 // will iterate over the size expression. However, this expression belongs
1479 // to the type, not to this, so we don't want to delete it.
1480 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001481 if (isArgumentType()) {
1482 this->~SizeOfAlignOfExpr();
1483 C.Deallocate(this);
1484 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001485 else
1486 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001487}
1488
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001489//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001490// DesignatedInitExpr
1491//===----------------------------------------------------------------------===//
1492
1493IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1494 assert(Kind == FieldDesignator && "Only valid on a field designator");
1495 if (Field.NameOrField & 0x01)
1496 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1497 else
1498 return getField()->getIdentifier();
1499}
1500
1501DesignatedInitExpr *
1502DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1503 unsigned NumDesignators,
1504 Expr **IndexExprs, unsigned NumIndexExprs,
1505 SourceLocation ColonOrEqualLoc,
1506 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001507 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1508 sizeof(Designator) * NumDesignators +
1509 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001510 DesignatedInitExpr *DIE
1511 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1512 ColonOrEqualLoc, UsesColonSyntax,
1513 NumIndexExprs + 1);
1514
1515 // Fill in the designators
1516 unsigned ExpectedNumSubExprs = 0;
1517 designators_iterator Desig = DIE->designators_begin();
1518 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1519 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1520 if (Designators[Idx].isArrayDesignator())
1521 ++ExpectedNumSubExprs;
1522 else if (Designators[Idx].isArrayRangeDesignator())
1523 ExpectedNumSubExprs += 2;
1524 }
1525 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1526
1527 // Fill in the subexpressions, including the initializer expression.
1528 child_iterator Child = DIE->child_begin();
1529 *Child++ = Init;
1530 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1531 *Child = IndexExprs[Idx];
1532
1533 return DIE;
1534}
1535
1536SourceRange DesignatedInitExpr::getSourceRange() const {
1537 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001538 Designator &First =
1539 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001540 if (First.isFieldDesignator()) {
1541 if (UsesColonSyntax)
1542 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1543 else
1544 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1545 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001546 StartLoc =
1547 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001548 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1549}
1550
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001551DesignatedInitExpr::designators_iterator
1552DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001553 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1554 Ptr += sizeof(DesignatedInitExpr);
1555 return static_cast<Designator*>(static_cast<void*>(Ptr));
1556}
1557
1558DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1559 return designators_begin() + NumDesignators;
1560}
1561
1562Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1563 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1564 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1565 Ptr += sizeof(DesignatedInitExpr);
1566 Ptr += sizeof(Designator) * NumDesignators;
1567 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1568 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1569}
1570
1571Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1572 assert(D.Kind == Designator::ArrayRangeDesignator &&
1573 "Requires array range designator");
1574 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1575 Ptr += sizeof(DesignatedInitExpr);
1576 Ptr += sizeof(Designator) * NumDesignators;
1577 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1578 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1579}
1580
1581Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1582 assert(D.Kind == Designator::ArrayRangeDesignator &&
1583 "Requires array range designator");
1584 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1585 Ptr += sizeof(DesignatedInitExpr);
1586 Ptr += sizeof(Designator) * NumDesignators;
1587 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1588 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1589}
1590
1591//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001592// ExprIterator.
1593//===----------------------------------------------------------------------===//
1594
1595Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1596Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1597Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1598const Expr* ConstExprIterator::operator[](size_t idx) const {
1599 return cast<Expr>(I[idx]);
1600}
1601const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1602const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1603
1604//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001605// Child Iterators for iterating over subexpressions/substatements
1606//===----------------------------------------------------------------------===//
1607
1608// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001609Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1610Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001611
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001612// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001613Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1614Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001615
Steve Naroff6f786252008-06-02 23:03:37 +00001616// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001617Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1618Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001619
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001620// ObjCKVCRefExpr
1621Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1622Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1623
Douglas Gregord8606632008-11-04 14:56:14 +00001624// ObjCSuperExpr
1625Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1626Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1627
Chris Lattner69909292008-08-10 01:53:14 +00001628// PredefinedExpr
1629Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1630Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001631
1632// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001633Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1634Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001635
1636// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001637Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001638Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001639
1640// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001641Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1642Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001643
Chris Lattner1de66eb2007-08-26 03:42:43 +00001644// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001645Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1646Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001647
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001648// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001649Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1650Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001651
1652// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001653Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1654Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001655
1656// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001657Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1658Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001659
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001660// SizeOfAlignOfExpr
1661Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1662 // If this is of a type and the type is a VLA type (and not a typedef), the
1663 // size expression of the VLA needs to be treated as an executable expression.
1664 // Why isn't this weirdness documented better in StmtIterator?
1665 if (isArgumentType()) {
1666 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1667 getArgumentType().getTypePtr()))
1668 return child_iterator(T);
1669 return child_iterator();
1670 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001671 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001672}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001673Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1674 if (isArgumentType())
1675 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001676 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001677}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001678
1679// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001680Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001681 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001682}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001683Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001684 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001685}
1686
1687// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001688Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001689 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001690}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001691Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001692 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001693}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001694
1695// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001696Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1697Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001698
Nate Begemanaf6ed502008-04-18 23:10:10 +00001699// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001700Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1701Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001702
1703// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001704Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1705Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001706
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001707// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001708Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1709Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001710
1711// BinaryOperator
1712Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001713 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001714}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001715Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001716 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001717}
1718
1719// ConditionalOperator
1720Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001721 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001722}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001723Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001724 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001725}
1726
1727// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001728Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1729Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001730
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001731// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001732Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1733Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001734
1735// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001736Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1737 return child_iterator();
1738}
1739
1740Stmt::child_iterator TypesCompatibleExpr::child_end() {
1741 return child_iterator();
1742}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001743
1744// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001745Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1746Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001747
Douglas Gregorad4b3792008-11-29 04:51:27 +00001748// GNUNullExpr
1749Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1750Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1751
Eli Friedmand0e9d092008-05-14 19:38:39 +00001752// ShuffleVectorExpr
1753Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001754 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001755}
1756Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001757 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001758}
1759
Anders Carlsson36760332007-10-15 20:28:48 +00001760// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001761Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1762Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001763
Anders Carlsson762b7c72007-08-31 04:56:16 +00001764// InitListExpr
1765Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001766 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001767}
1768Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001769 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001770}
1771
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001772// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001773Stmt::child_iterator DesignatedInitExpr::child_begin() {
1774 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1775 Ptr += sizeof(DesignatedInitExpr);
1776 Ptr += sizeof(Designator) * NumDesignators;
1777 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1778}
1779Stmt::child_iterator DesignatedInitExpr::child_end() {
1780 return child_iterator(&*child_begin() + NumSubExprs);
1781}
1782
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001783// ImplicitValueInitExpr
1784Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1785 return child_iterator();
1786}
1787
1788Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1789 return child_iterator();
1790}
1791
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001792// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001793Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001794 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001795}
1796Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001797 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001798}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001799
1800// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001801Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1802Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001803
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001804// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001805Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1806 return child_iterator();
1807}
1808Stmt::child_iterator ObjCSelectorExpr::child_end() {
1809 return child_iterator();
1810}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001811
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001812// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001813Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1814 return child_iterator();
1815}
1816Stmt::child_iterator ObjCProtocolExpr::child_end() {
1817 return child_iterator();
1818}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001819
Steve Naroffc39ca262007-09-18 23:55:05 +00001820// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001821Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001822 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001823}
1824Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001825 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001826}
1827
Steve Naroff52a81c02008-09-03 18:15:37 +00001828// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001829Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1830Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001831
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001832Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1833Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }