blob: d551eac3006f47476b89dbd1e220d647feb65354 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerda8249e2008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
Chris Lattner2085fd62009-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
Reid Spencer5f016e22007-07-11 17:01:13 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000059
Chris Lattner726e1682009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000063}
64
65
Ted Kremenek6e94ef52009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Reid Spencer5f016e22007-07-11 17:01:13 +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";
Reid Spencer5f016e22007-07-11 17:01:13 +000089 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000091 }
92}
93
94//===----------------------------------------------------------------------===//
95// Postfix Operators.
96//===----------------------------------------------------------------------===//
97
Ted Kremenek668bf912009-02-09 20:51:47 +000098CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +000099 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000100 : Expr(SC, t,
101 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000102 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000103 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000104
105 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000109
Douglas Gregorb4609802008-11-14 16:09:21 +0000110 RParenLoc = rparenloc;
111}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000112
Ted Kremenek668bf912009-02-09 20:51:47 +0000113CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
114 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000115 : Expr(CallExprClass, t,
116 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000117 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000118 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000119
120 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000121 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000123 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 RParenLoc = rparenloc;
126}
127
Ted Kremenek668bf912009-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 Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000138void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000145 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Kremenek55499762008-06-17 02:43:46 +0000151 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000159 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000160 SubExprs = NewSubExprs;
161 this->NumArgs = NumArgs;
162}
163
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000166unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000172 return 0;
173
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000174 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
175 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000176 return 0;
177
Anders Carlssonbcba2012008-01-31 02:13:57 +0000178 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
179 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000180 return 0;
181
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000182 if (!FDecl->getIdentifier())
183 return 0;
184
Douglas Gregor3c385e52009-02-14 18:57:46 +0000185 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000186}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000187
Chris Lattnercb888962008-10-06 05:00:53 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson66b5a8a2007-08-31 04:56:16 +0000227InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000228 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000229 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000230 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000231 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000232 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000233
234 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000235}
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
Douglas Gregor4c678342009-01-28 21:54:33 +0000237void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000238 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000239 Idx < LastIdx; ++Idx)
Douglas Gregor4c678342009-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 Naroffbfdcae62008-09-04 15:31:07 +0000256/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000257///
258const FunctionType *BlockExpr::getFunctionType() const {
259 return getType()->getAsBlockPointerType()->
260 getPointeeType()->getAsFunctionType();
261}
262
Steve Naroff56ee6892008-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000270//===----------------------------------------------------------------------===//
271// Generic Expression Routines
272//===----------------------------------------------------------------------===//
273
Chris Lattner026dc962009-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 {
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 switch (getStmtClass()) {
281 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000282 Loc = getExprLoc();
283 R1 = getSourceRange();
284 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000286 return cast<ParenExpr>(this)->getSubExpr()->
287 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 case UnaryOperatorClass: {
289 const UnaryOperator *UO = cast<UnaryOperator>(this);
290
291 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000292 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 case UnaryOperator::PostInc:
294 case UnaryOperator::PostDec:
295 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000296 case UnaryOperator::PreDec: // ++/--
297 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 case UnaryOperator::Deref:
299 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000300 if (getType().isVolatileQualified())
301 return false;
302 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 case UnaryOperator::Real:
304 case UnaryOperator::Imag:
305 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000306 if (UO->getSubExpr()->getType().isVolatileQualified())
307 return false;
308 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000310 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 }
Chris Lattner026dc962009-02-14 07:37:35 +0000312 Loc = UO->getOperatorLoc();
313 R1 = UO->getSubExpr()->getSourceRange();
314 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000316 case BinaryOperatorClass: {
Chris Lattner026dc962009-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 Lattnere7716e62007-12-01 06:07:34 +0000322
Chris Lattner026dc962009-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 Lattnere7716e62007-12-01 06:07:34 +0000329 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000330 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000331 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000332
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000333 case ConditionalOperatorClass: {
Chris Lattner026dc962009-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 Jahanianab38e4b2007-12-01 19:58:28 +0000336 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner026dc962009-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 Jahanianab38e4b2007-12-01 19:58:28 +0000340 }
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 case MemberExprClass:
Chris Lattner026dc962009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 case ArraySubscriptExprClass:
353 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-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 Friedman211f6ad2008-05-27 15:24:04 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 case CallExprClass:
Chris Lattner026dc962009-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 Lattnera9c01022007-09-26 22:06:30 +0000384 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000385 return false;
Chris Lattner611b2ec2008-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 Lattner026dc962009-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 Lattner611b2ec2008-07-26 19:51:01 +0000400 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000401 case CStyleCastExprClass:
Chris Lattner026dc962009-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;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000410 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner026dc962009-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 Friedman4be1f472008-05-19 21:24:43 +0000420 case ImplicitCastExprClass:
421 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000422 return cast<ImplicitCastExpr>(this)
423 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000424
Chris Lattner04421082008-04-08 04:40:51 +0000425 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000426 return cast<CXXDefaultArgExpr>(this)
427 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-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 Lattner026dc962009-02-14 07:37:35 +0000433 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000434 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000435}
436
Douglas Gregorba7e2102008-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 Gregor72c3f312008-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 Gregor44b43212008-12-11 16:49:14 +0000446 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner7da36f62007-10-30 22:53:42 +0000461/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000462/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000463///
Chris Lattner28be73f2008-07-26 21:30:36 +0000464Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-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())
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 return LV_NotObjectType;
469
Steve Naroffacb818a2008-02-10 01:39:04 +0000470 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000471 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000472 return LV_IncompleteVoidType;
473
Douglas Gregor98cd5992008-10-21 23:43:52 +0000474 /// FIXME: Expressions can't have reference type, so the following
475 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000476 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000477 return LV_Valid;
478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 // the type looks fine, now check the expression
480 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000481 case StringLiteralClass: // C99 6.5.1p4
482 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000483 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
485 // For vectors, make sure base is an lvalue (i.e. not a function call).
486 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000487 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000489 case DeclRefExprClass:
490 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000491 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
492 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 return LV_Valid;
494 break;
Chris Lattner41110242008-06-17 18:05:57 +0000495 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000496 case BlockDeclRefExprClass: {
497 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000498 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000499 return LV_Valid;
500 break;
501 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000502 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000504 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
505 NamedDecl *Member = m->getMemberDecl();
506 // C++ [expr.ref]p4:
507 // If E2 is declared to have type "reference to T", then E1.E2
508 // is an lvalue.
509 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
510 if (Value->getType()->isReferenceType())
511 return LV_Valid;
512
513 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
514 if (isa<CXXClassVarDecl>(Member))
515 return LV_Valid;
516
517 // -- If E2 is a non-static data member [...]. If E1 is an
518 // lvalue, then E1.E2 is an lvalue.
519 if (isa<FieldDecl>(Member))
520 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
521
522 // -- If it refers to a static member function [...], then
523 // E1.E2 is an lvalue.
524 // -- Otherwise, if E1.E2 refers to a non-static member
525 // function [...], then E1.E2 is not an lvalue.
526 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
527 return Method->isStatic()? LV_Valid : LV_MemberFunction;
528
529 // -- If E2 is a member enumerator [...], the expression E1.E2
530 // is not an lvalue.
531 if (isa<EnumConstantDecl>(Member))
532 return LV_InvalidExpression;
533
534 // Not an lvalue.
535 return LV_InvalidExpression;
536 }
537
538 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000539 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000540 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000541 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000543 return LV_Valid; // C99 6.5.3p4
544
545 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000546 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
547 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000548 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000549
550 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
551 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
552 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
553 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000555 case ImplicitCastExprClass:
556 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
557 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000559 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000560 case BinaryOperatorClass:
561 case CompoundAssignOperatorClass: {
562 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000563
564 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
565 BinOp->getOpcode() == BinaryOperator::Comma)
566 return BinOp->getRHS()->isLvalue(Ctx);
567
Sebastian Redl22460502009-02-07 00:15:38 +0000568 // C++ [expr.mptr.oper]p6
569 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
570 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
571 !BinOp->getType()->isFunctionType())
572 return BinOp->getLHS()->isLvalue(Ctx);
573
Douglas Gregorbf3af052008-11-13 20:12:29 +0000574 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000575 return LV_InvalidExpression;
576
Douglas Gregorbf3af052008-11-13 20:12:29 +0000577 if (Ctx.getLangOptions().CPlusPlus)
578 // C++ [expr.ass]p1:
579 // The result of an assignment operation [...] is an lvalue.
580 return LV_Valid;
581
582
583 // C99 6.5.16:
584 // An assignment expression [...] is not an lvalue.
585 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000586 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000587 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000588 case CXXOperatorCallExprClass:
589 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000590 // C++ [expr.call]p10:
591 // A function call is an lvalue if and only if the result type
592 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000593 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000594 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000595 CalleeType = FnTypePtr->getPointeeType();
596 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
597 if (FnType->getResultType()->isReferenceType())
598 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000599
600 break;
601 }
Steve Naroffe6386392007-12-05 04:00:10 +0000602 case CompoundLiteralExprClass: // C99 6.5.2.5p5
603 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000604 case ChooseExprClass:
605 // __builtin_choose_expr is an lvalue if the selected operand is.
606 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
607 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
608 else
609 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
610
Nate Begeman213541a2008-04-18 23:10:10 +0000611 case ExtVectorElementExprClass:
612 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000613 return LV_DuplicateVectorComponents;
614 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000615 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
616 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000617 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
618 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000619 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000620 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000621 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000622 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000623 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000624 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000625 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000626 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000627 case CXXConditionDeclExprClass:
628 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000629 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000630 case CXXFunctionalCastExprClass:
631 case CXXStaticCastExprClass:
632 case CXXDynamicCastExprClass:
633 case CXXReinterpretCastExprClass:
634 case CXXConstCastExprClass:
635 // The result of an explicit cast is an lvalue if the type we are
636 // casting to is a reference type. See C++ [expr.cast]p1,
637 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
638 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
639 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
640 return LV_Valid;
641 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000642 case CXXTypeidExprClass:
643 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
644 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 default:
646 break;
647 }
648 return LV_InvalidExpression;
649}
650
651/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
652/// does not have an incomplete type, does not have a const-qualified type, and
653/// if it is a structure or union, does not have any member (including,
654/// recursively, any member or element of all contained aggregates or unions)
655/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000656Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
657 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658
659 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000660 case LV_Valid:
661 // C++ 3.10p11: Functions cannot be modified, but pointers to
662 // functions can be modifiable.
663 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
664 return MLV_NotObjectType;
665 break;
666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 case LV_NotObjectType: return MLV_NotObjectType;
668 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000669 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000670 case LV_InvalidExpression:
671 // If the top level is a C-style cast, and the subexpression is a valid
672 // lvalue, then this is probably a use of the old-school "cast as lvalue"
673 // GCC extension. We don't support it, but we want to produce good
674 // diagnostics when it happens so that the user knows why.
675 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
676 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
677 return MLV_LValueCast;
678 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000679 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000681
682 QualType CT = Ctx.getCanonicalType(getType());
683
684 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000686 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000688 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 return MLV_IncompleteType;
690
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000691 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 if (r->hasConstFields())
693 return MLV_ConstQualified;
694 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000695 // The following is illegal:
696 // void takeclosure(void (^C)(void));
697 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
698 //
699 if (getStmtClass() == BlockDeclRefExprClass) {
700 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
701 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
702 return MLV_NotBlockQualified;
703 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000704
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000705 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000706 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000707 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
708 if (KVCExpr->getSetterMethod() == 0)
709 return MLV_NoSetterProperty;
710 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 return MLV_Valid;
712}
713
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000714/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000715/// duration. This means that the address of this expression is a link-time
716/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000717bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000718 switch (getStmtClass()) {
719 default:
720 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000721 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000722 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000723 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000724 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000725 case CompoundLiteralExprClass:
726 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000727 case DeclRefExprClass:
728 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000729 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
730 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000731 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000732 if (isa<FunctionDecl>(D))
733 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000734 return false;
735 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000736 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000737 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000738 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000739 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000740 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000741 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000742 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000743 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000744 case CXXDefaultArgExprClass:
745 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000746 }
747}
748
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000749/// isOBJCGCCandidate - Check if an expression is objc gc'able.
750///
751bool Expr::isOBJCGCCandidate() const {
752 switch (getStmtClass()) {
753 default:
754 return false;
755 case ObjCIvarRefExprClass:
756 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000757 case Expr::UnaryOperatorClass:
758 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000759 case ParenExprClass:
760 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
761 case ImplicitCastExprClass:
762 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
763 case DeclRefExprClass:
764 case QualifiedDeclRefExprClass: {
765 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
766 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
767 return VD->hasGlobalStorage();
768 return false;
769 }
770 case MemberExprClass: {
771 const MemberExpr *M = cast<MemberExpr>(this);
772 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
773 }
774 case ArraySubscriptExprClass:
775 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
776 }
777}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000778Expr* Expr::IgnoreParens() {
779 Expr* E = this;
780 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
781 E = P->getSubExpr();
782
783 return E;
784}
785
Chris Lattner56f34942008-02-13 01:02:39 +0000786/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
787/// or CastExprs or ImplicitCastExprs, returning their operand.
788Expr *Expr::IgnoreParenCasts() {
789 Expr *E = this;
790 while (true) {
791 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
792 E = P->getSubExpr();
793 else if (CastExpr *P = dyn_cast<CastExpr>(E))
794 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000795 else
796 return E;
797 }
798}
799
Douglas Gregor898574e2008-12-05 23:32:09 +0000800/// hasAnyTypeDependentArguments - Determines if any of the expressions
801/// in Exprs is type-dependent.
802bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
803 for (unsigned I = 0; I < NumExprs; ++I)
804 if (Exprs[I]->isTypeDependent())
805 return true;
806
807 return false;
808}
809
810/// hasAnyValueDependentArguments - Determines if any of the expressions
811/// in Exprs is value-dependent.
812bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
813 for (unsigned I = 0; I < NumExprs; ++I)
814 if (Exprs[I]->isValueDependent())
815 return true;
816
817 return false;
818}
819
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000820bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000821 // This function is attempting whether an expression is an initializer
822 // which can be evaluated at compile-time. isEvaluatable handles most
823 // of the cases, but it can't deal with some initializer-specific
824 // expressions, and it can't deal with aggregates; we deal with those here,
825 // and fall back to isEvaluatable for the other cases.
826
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000827 // FIXME: This function assumes the variable being assigned to
828 // isn't a reference type!
829
Anders Carlssone8a32b82008-11-24 05:23:59 +0000830 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000831 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000832 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000833 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000834 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000835 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000836 // This handles gcc's extension that allows global initializers like
837 // "struct x {int x;} x = (struct x) {};".
838 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +0000839 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000840 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000841 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000842 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000843 // FIXME: This doesn't deal with fields with reference types correctly.
844 // FIXME: This incorrectly allows pointers cast to integers to be assigned
845 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +0000846 const InitListExpr *Exp = cast<InitListExpr>(this);
847 unsigned numInits = Exp->getNumInits();
848 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000849 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000850 return false;
851 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000852 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000853 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000854 case ImplicitValueInitExprClass:
855 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000856 case ParenExprClass: {
857 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
858 }
859 case UnaryOperatorClass: {
860 const UnaryOperator* Exp = cast<UnaryOperator>(this);
861 if (Exp->getOpcode() == UnaryOperator::Extension)
862 return Exp->getSubExpr()->isConstantInitializer(Ctx);
863 break;
864 }
865 case CStyleCastExprClass:
866 // Handle casts with a destination that's a struct or union; this
867 // deals with both the gcc no-op struct cast extension and the
868 // cast-to-union extension.
869 if (getType()->isRecordType())
870 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
871 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000872 }
873
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000874 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000875}
876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877/// isIntegerConstantExpr - this recursive routine will test if an expression is
878/// an integer constant expression. Note: With the introduction of VLA's in
879/// C99 the result of the sizeof operator is no longer always a constant
880/// expression. The generalization of the wording to include any subexpression
881/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
882/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopes5f6b6322008-07-08 21:13:06 +0000883/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Reid Spencer5f016e22007-07-11 17:01:13 +0000884/// occurring in a context requiring a constant, would have been a constraint
885/// violation. FIXME: This routine currently implements C90 semantics.
886/// To properly implement C99 semantics this routine will need to evaluate
887/// expressions involving operators previously mentioned.
888
889/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
890/// comma, etc
891///
892/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000893/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000894///
895/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
896/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
897/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000898bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
899 SourceLocation *Loc, bool isEvaluated) const {
Daniel Dunbar2d6744f2009-02-18 00:47:45 +0000900 if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
901 return false;
902 assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
903 return true;
904}
905
906bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
907 SourceLocation *Loc, bool isEvaluated) const {
908
Eli Friedmana6afa762008-11-13 06:09:17 +0000909 // Pretest for integral type; some parts of the code crash for types that
910 // can't be sized.
911 if (!getType()->isIntegralType()) {
912 if (Loc) *Loc = getLocStart();
913 return false;
914 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 switch (getStmtClass()) {
916 default:
917 if (Loc) *Loc = getLocStart();
918 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 case ParenExprClass:
920 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000921 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 case IntegerLiteralClass:
Daniel Dunbar2d6744f2009-02-18 00:47:45 +0000923 // NOTE: getValue() returns an APInt, we must set sign.
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 Result = cast<IntegerLiteral>(this)->getValue();
Daniel Dunbara1359752009-02-18 00:32:53 +0000925 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000927 case CharacterLiteralClass: {
928 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Daniel Dunbara1359752009-02-18 00:32:53 +0000929 Result = Ctx.MakeIntValue(CL->getValue(), getType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000931 }
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000932 case CXXBoolLiteralExprClass: {
933 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
Daniel Dunbara1359752009-02-18 00:32:53 +0000934 Result = Ctx.MakeIntValue(BL->getValue(), getType());
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000935 break;
936 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000937 case CXXZeroInitValueExprClass:
Daniel Dunbara1359752009-02-18 00:32:53 +0000938 Result = Ctx.MakeIntValue(0, getType());
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000939 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000940 case TypesCompatibleExprClass: {
941 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Daniel Dunbarac620de2008-10-24 08:07:57 +0000942 // Per gcc docs "this built-in function ignores top level
943 // qualifiers". We need to use the canonical version to properly
944 // be able to strip CRV qualifiers from the type.
945 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
946 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
Daniel Dunbara1359752009-02-18 00:32:53 +0000947 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
948 T1.getUnqualifiedType()),
949 getType());
Steve Naroff389cecc2007-08-02 00:13:27 +0000950 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000951 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000952 case CallExprClass:
953 case CXXOperatorCallExprClass: {
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000954 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnera4d55d82008-10-06 06:40:35 +0000955
956 // If this is a call to a builtin function, constant fold it otherwise
957 // reject it.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000958 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +0000959 EvalResult EvalResult;
960 if (CE->Evaluate(EvalResult, Ctx)) {
961 assert(!EvalResult.HasSideEffects &&
962 "Foldable builtin call should not have side effects!");
963 Result = EvalResult.Val.getInt();
Chris Lattnera4d55d82008-10-06 06:40:35 +0000964 break; // It is a constant, expand it.
965 }
966 }
967
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000968 if (Loc) *Loc = getLocStart();
969 return false;
970 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000972 case QualifiedDeclRefExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 if (const EnumConstantDecl *D =
974 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
975 Result = D->getInitVal();
976 break;
977 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000978 if (Ctx.getLangOptions().CPlusPlus &&
979 getType().getCVRQualifiers() == QualType::Const) {
980 // C++ 7.1.5.1p2
981 // A variable of non-volatile const-qualified integral or enumeration
982 // type initialized by an ICE can be used in ICEs.
983 if (const VarDecl *Dcl =
984 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
985 if (const Expr *Init = Dcl->getInit())
986 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
987 }
988 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 if (Loc) *Loc = getLocStart();
990 return false;
991 case UnaryOperatorClass: {
992 const UnaryOperator *Exp = cast<UnaryOperator>(this);
993
Sebastian Redl05189992008-11-11 17:56:53 +0000994 // Get the operand value. If this is offsetof, do not evalute the
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 // operand. This affects C99 6.6p3.
Sebastian Redl05189992008-11-11 17:56:53 +0000996 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
997 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 return false;
999
1000 switch (Exp->getOpcode()) {
1001 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
1002 // See C99 6.6p3.
1003 default:
1004 if (Loc) *Loc = Exp->getOperatorLoc();
1005 return false;
1006 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +00001007 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 case UnaryOperator::LNot: {
Daniel Dunbara1359752009-02-18 00:32:53 +00001009 Result = Ctx.MakeIntValue(Result == 0, getType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 break;
1011 }
1012 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 break;
1014 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 Result = -Result;
1016 break;
1017 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 Result = ~Result;
1019 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001020 case UnaryOperator::OffsetOf:
Daniel Dunbara1359752009-02-18 00:32:53 +00001021 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
1022 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 }
1024 break;
1025 }
Sebastian Redl05189992008-11-11 17:56:53 +00001026 case SizeOfAlignOfExprClass: {
1027 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Sebastian Redl05189992008-11-11 17:56:53 +00001028 QualType ArgTy = Exp->getTypeOfArgument();
Eli Friedmandcdafb62009-02-22 02:56:25 +00001029
1030 // alignof is always an ICE; sizeof is an ICE if and only if
1031 // the operand isn't a VLA
1032 if (Exp->isSizeOf() && ArgTy->isVariableArrayType()) {
Chris Lattner65383472007-12-18 07:15:40 +00001033 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 return false;
Chris Lattner65383472007-12-18 07:15:40 +00001035 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001036
Eli Friedmandcdafb62009-02-22 02:56:25 +00001037 // Use the Evaluate logic to calculate the value, since the
1038 // calculation is non-trivial.
1039 Result = EvaluateAsInt(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 break;
1041 }
1042 case BinaryOperatorClass: {
1043 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbare1226d22008-09-22 23:53:24 +00001044 llvm::APSInt LHS, RHS;
1045
1046 // Initialize result to have correct signedness and width.
Daniel Dunbara1359752009-02-18 00:32:53 +00001047 Result = Ctx.MakeIntValue(0, getType());
Eli Friedmanb11e7782008-11-13 02:13:11 +00001048
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbare1226d22008-09-22 23:53:24 +00001050 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 return false;
1052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 // The short-circuiting &&/|| operators don't necessarily evaluate their
1054 // RHS. Make sure to pass isEvaluated down correctly.
1055 if (Exp->isLogicalOp()) {
1056 bool RHSEval;
1057 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbare1226d22008-09-22 23:53:24 +00001058 RHSEval = LHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 else {
1060 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbare1226d22008-09-22 23:53:24 +00001061 RHSEval = LHS == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 }
1063
Chris Lattner590b6642007-07-15 23:26:56 +00001064 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 isEvaluated & RHSEval))
1066 return false;
1067 } else {
Chris Lattner590b6642007-07-15 23:26:56 +00001068 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 return false;
1070 }
1071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 switch (Exp->getOpcode()) {
1073 default:
1074 if (Loc) *Loc = getLocStart();
1075 return false;
1076 case BinaryOperator::Mul:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001077 Result = LHS * RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 break;
1079 case BinaryOperator::Div:
1080 if (RHS == 0) {
1081 if (!isEvaluated) break;
1082 if (Loc) *Loc = getLocStart();
1083 return false;
1084 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001085 Result = LHS / RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 break;
1087 case BinaryOperator::Rem:
1088 if (RHS == 0) {
1089 if (!isEvaluated) break;
1090 if (Loc) *Loc = getLocStart();
1091 return false;
1092 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001093 Result = LHS % RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001095 case BinaryOperator::Add: Result = LHS + RHS; break;
1096 case BinaryOperator::Sub: Result = LHS - RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 case BinaryOperator::Shl:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001098 Result = LHS <<
1099 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1100 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 case BinaryOperator::Shr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001102 Result = LHS >>
1103 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001105 case BinaryOperator::LT: Result = LHS < RHS; break;
1106 case BinaryOperator::GT: Result = LHS > RHS; break;
1107 case BinaryOperator::LE: Result = LHS <= RHS; break;
1108 case BinaryOperator::GE: Result = LHS >= RHS; break;
1109 case BinaryOperator::EQ: Result = LHS == RHS; break;
1110 case BinaryOperator::NE: Result = LHS != RHS; break;
1111 case BinaryOperator::And: Result = LHS & RHS; break;
1112 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1113 case BinaryOperator::Or: Result = LHS | RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 case BinaryOperator::LAnd:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001115 Result = LHS != 0 && RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 break;
1117 case BinaryOperator::LOr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001118 Result = LHS != 0 || RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +00001120
1121 case BinaryOperator::Comma:
1122 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1123 // *except* when they are contained within a subexpression that is not
1124 // evaluated". Note that Assignment can never happen due to constraints
1125 // on the LHS subexpr, so we don't need to check it here.
1126 if (isEvaluated) {
1127 if (Loc) *Loc = getLocStart();
1128 return false;
1129 }
1130
1131 // The result of the constant expr is the RHS.
1132 Result = RHS;
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001133 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
Daniel Dunbara1359752009-02-18 00:32:53 +00001135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1137 break;
1138 }
Chris Lattner26dc7b32007-07-15 23:54:50 +00001139 case ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001140 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001141 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001142 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1143 SourceLocation CastLoc = getLocStart();
Chris Lattner26dc7b32007-07-15 23:54:50 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001146 if (!SubExpr->getType()->isArithmeticType() ||
1147 !getType()->isIntegerType()) {
1148 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 return false;
1150 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001151
Chris Lattner98be4942008-03-05 18:54:05 +00001152 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner987b15d2007-09-22 19:04:13 +00001153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001155 if (SubExpr->getType()->isIntegerType()) {
1156 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +00001158
1159 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001160 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001161 if (getType()->isBooleanType()) {
1162 // Conversion to bool compares against zero.
Daniel Dunbara1359752009-02-18 00:32:53 +00001163 Result = Ctx.MakeIntValue(Result != 0, getType());
1164 } else if (SubExpr->getType()->isSignedIntegerType()) {
Chris Lattner26dc7b32007-07-15 23:54:50 +00001165 Result.sextOrTrunc(DestWidth);
Daniel Dunbara1359752009-02-18 00:32:53 +00001166 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1167 } else { // If the input is unsigned, do a zero extend, noop,
1168 // or truncate.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001169 Result.zextOrTrunc(DestWidth);
Daniel Dunbara1359752009-02-18 00:32:53 +00001170 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1171 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 break;
1173 }
1174
1175 // Allow floating constants that are the immediate operands of casts or that
1176 // are parenthesized.
Daniel Dunbara1359752009-02-18 00:32:53 +00001177 const Expr *Operand = SubExpr->IgnoreParens();
Chris Lattner987b15d2007-09-22 19:04:13 +00001178
1179 // If this isn't a floating literal, we can't handle it.
1180 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1181 if (!FL) {
1182 if (Loc) *Loc = Operand->getLocStart();
1183 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001185
1186 // If the destination is boolean, compare against zero.
1187 if (getType()->isBooleanType()) {
Daniel Dunbara1359752009-02-18 00:32:53 +00001188 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001189 break;
1190 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001191
1192 // Determine whether we are converting to unsigned or signed.
1193 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +00001194
1195 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1196 // be called multiple times per AST.
Dale Johannesenee5a7002008-10-09 23:02:32 +00001197 uint64_t Space[4];
1198 bool ignored;
Chris Lattnerccc213f2007-09-26 00:47:26 +00001199 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +00001200 llvm::APFloat::rmTowardZero,
1201 &ignored);
Chris Lattner987b15d2007-09-22 19:04:13 +00001202 Result = llvm::APInt(DestWidth, 4, Space);
Daniel Dunbara1359752009-02-18 00:32:53 +00001203 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner987b15d2007-09-22 19:04:13 +00001204 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 }
1206 case ConditionalOperatorClass: {
1207 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1208
Chris Lattner28daa532008-12-12 06:55:44 +00001209 const Expr *Cond = Exp->getCond();
1210
1211 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 return false;
1213
1214 const Expr *TrueExp = Exp->getLHS();
1215 const Expr *FalseExp = Exp->getRHS();
1216 if (Result == 0) std::swap(TrueExp, FalseExp);
1217
Chris Lattner28daa532008-12-12 06:55:44 +00001218 // If the condition (ignoring parens) is a __builtin_constant_p call,
1219 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001220 // expression, and it is fully evaluated. This is an important GNU
1221 // extension. See GCC PR38377 for discussion.
Chris Lattner28daa532008-12-12 06:55:44 +00001222 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001223 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner42b83dd2008-12-12 18:00:51 +00001224 EvalResult EVResult;
1225 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1226 return false;
1227 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1228 Result = EVResult.Val.getInt();
1229 if (Loc) *Loc = EVResult.DiagLoc;
1230 return true;
1231 }
Chris Lattner28daa532008-12-12 06:55:44 +00001232
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 // Evaluate the false one first, discard the result.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001234 llvm::APSInt Tmp;
1235 if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 return false;
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001237 // Evalute the true one, capture the result. Note that if TrueExp
1238 // is False then this is an instant of the gcc missing LHS
1239 // extension, and we will just reuse Result.
Anders Carlsson39073232007-11-30 19:04:31 +00001240 if (TrueExp &&
1241 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 break;
1244 }
Chris Lattner04421082008-04-08 04:40:51 +00001245 case CXXDefaultArgExprClass:
1246 return cast<CXXDefaultArgExpr>(this)
1247 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001248
1249 case UnaryTypeTraitExprClass:
Daniel Dunbara1359752009-02-18 00:32:53 +00001250 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1251 getType());
1252 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 }
1254
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 return true;
1256}
1257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1259/// integer constant expression with the value zero, or if this is one that is
1260/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001261bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1262{
Sebastian Redl07779722008-10-31 14:43:28 +00001263 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001264 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001265 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001266 // Check that it is a cast to void*.
1267 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1268 QualType Pointee = PT->getPointeeType();
1269 if (Pointee.getCVRQualifiers() == 0 &&
1270 Pointee->isVoidType() && // to void*
1271 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001272 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001273 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001275 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1276 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001277 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001278 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1279 // Accept ((void*)0) as a null pointer constant, as many other
1280 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001281 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001282 } else if (const CXXDefaultArgExpr *DefaultArg
1283 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001284 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001285 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001286 } else if (isa<GNUNullExpr>(this)) {
1287 // The GNU __null extension is always a null pointer constant.
1288 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001289 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001290
Steve Naroffaa58f002008-01-14 16:10:57 +00001291 // This expression must be an integer type.
1292 if (!getType()->isIntegerType())
1293 return false;
1294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 // If we have an integer constant expression, we need to *evaluate* it and
1296 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001297 // FIXME: We should probably return false if we're compiling in strict mode
1298 // and Diag is not null (this indicates that the value was foldable but not
1299 // an ICE.
1300 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001301 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001302 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001303}
Steve Naroff31a45842007-07-28 23:10:27 +00001304
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001305/// isBitField - Return true if this expression is a bit-field.
1306bool Expr::isBitField() {
1307 Expr *E = this->IgnoreParenCasts();
1308 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001309 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1310 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001311 return false;
1312}
1313
Chris Lattner2140e902009-02-16 22:14:05 +00001314/// isArrow - Return true if the base expression is a pointer to vector,
1315/// return false if the base expression is a vector.
1316bool ExtVectorElementExpr::isArrow() const {
1317 return getBase()->getType()->isPointerType();
1318}
1319
Nate Begeman213541a2008-04-18 23:10:10 +00001320unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001321 if (const VectorType *VT = getType()->getAsVectorType())
1322 return VT->getNumElements();
1323 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001324}
1325
Nate Begeman8a997642008-05-09 06:41:27 +00001326/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001327bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001328 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001329 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001330
1331 // Halving swizzles do not contain duplicate elements.
1332 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1333 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1334 return false;
1335
1336 // Advance past s-char prefix on hex swizzles.
1337 if (*compStr == 's') {
1338 compStr++;
1339 length--;
1340 }
Steve Narofffec0b492007-07-30 03:29:09 +00001341
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001342 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001343 const char *s = compStr+i;
1344 for (const char c = *s++; *s; s++)
1345 if (c == *s)
1346 return true;
1347 }
1348 return false;
1349}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001350
Nate Begeman8a997642008-05-09 06:41:27 +00001351/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001352void ExtVectorElementExpr::getEncodedElementAccess(
1353 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001354 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001355 if (*compStr == 's')
1356 compStr++;
1357
1358 bool isHi = !strcmp(compStr, "hi");
1359 bool isLo = !strcmp(compStr, "lo");
1360 bool isEven = !strcmp(compStr, "even");
1361 bool isOdd = !strcmp(compStr, "odd");
1362
Nate Begeman8a997642008-05-09 06:41:27 +00001363 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1364 uint64_t Index;
1365
1366 if (isHi)
1367 Index = e + i;
1368 else if (isLo)
1369 Index = i;
1370 else if (isEven)
1371 Index = 2 * i;
1372 else if (isOdd)
1373 Index = 2 * i + 1;
1374 else
1375 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001376
Nate Begeman3b8d1162008-05-13 21:03:02 +00001377 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001378 }
Nate Begeman8a997642008-05-09 06:41:27 +00001379}
1380
Steve Naroff68d331a2007-09-27 14:38:14 +00001381// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001382ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001383 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001384 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001385 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001386 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001387 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001388 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001389 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001390 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001391 if (NumArgs) {
1392 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001393 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1394 }
Steve Naroff563477d2007-09-18 23:55:05 +00001395 LBracloc = LBrac;
1396 RBracloc = RBrac;
1397}
1398
Steve Naroff68d331a2007-09-27 14:38:14 +00001399// constructor for class messages.
1400// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001401ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001402 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001403 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001404 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001405 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001406 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001407 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001408 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001409 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001410 if (NumArgs) {
1411 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001412 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1413 }
Steve Naroff563477d2007-09-18 23:55:05 +00001414 LBracloc = LBrac;
1415 RBracloc = RBrac;
1416}
1417
Ted Kremenek4df728e2008-06-24 15:50:53 +00001418// constructor for class messages.
1419ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1420 QualType retType, ObjCMethodDecl *mproto,
1421 SourceLocation LBrac, SourceLocation RBrac,
1422 Expr **ArgExprs, unsigned nargs)
1423: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1424MethodProto(mproto) {
1425 NumArgs = nargs;
1426 SubExprs = new Stmt*[NumArgs+1];
1427 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1428 if (NumArgs) {
1429 for (unsigned i = 0; i != NumArgs; ++i)
1430 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1431 }
1432 LBracloc = LBrac;
1433 RBracloc = RBrac;
1434}
1435
1436ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1437 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1438 switch (x & Flags) {
1439 default:
1440 assert(false && "Invalid ObjCMessageExpr.");
1441 case IsInstMeth:
1442 return ClassInfo(0, 0);
1443 case IsClsMethDeclUnknown:
1444 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1445 case IsClsMethDeclKnown: {
1446 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1447 return ClassInfo(D, D->getIdentifier());
1448 }
1449 }
1450}
1451
Chris Lattner27437ca2007-10-25 00:29:32 +00001452bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001453 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001454}
1455
Chris Lattner670a62c2008-12-12 05:35:08 +00001456static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001457 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1458 QualType Ty = ME->getBase()->getType();
1459
1460 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner98be4942008-03-05 18:54:05 +00001461 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +00001462 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1463 // FIXME: This is linear time. And the fact that we're indexing
1464 // into the layout by position in the record means that we're
1465 // either stuck numbering the fields in the AST or we have to keep
1466 // the linear search (yuck and yuck).
1467 unsigned i = 0;
1468 for (RecordDecl::field_iterator Field = RD->field_begin(),
1469 FieldEnd = RD->field_end();
1470 Field != FieldEnd; (void)++Field, ++i) {
1471 if (*Field == FD)
1472 break;
1473 }
1474
1475 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001476 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001477 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1478 const Expr *Base = ASE->getBase();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001479
Chris Lattner98be4942008-03-05 18:54:05 +00001480 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001481 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001482
1483 return size + evaluateOffsetOf(C, Base);
1484 } else if (isa<CompoundLiteralExpr>(E))
1485 return 0;
1486
1487 assert(0 && "Unknown offsetof subexpression!");
1488 return 0;
1489}
1490
1491int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1492{
1493 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1494
Chris Lattner98be4942008-03-05 18:54:05 +00001495 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek55499762008-06-17 02:43:46 +00001496 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001497}
1498
Sebastian Redl05189992008-11-11 17:56:53 +00001499void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1500 // Override default behavior of traversing children. If this has a type
1501 // operand and the type is a variable-length array, the child iteration
1502 // will iterate over the size expression. However, this expression belongs
1503 // to the type, not to this, so we don't want to delete it.
1504 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001505 if (isArgumentType()) {
1506 this->~SizeOfAlignOfExpr();
1507 C.Deallocate(this);
1508 }
Sebastian Redl05189992008-11-11 17:56:53 +00001509 else
1510 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001511}
1512
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001513//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001514// DesignatedInitExpr
1515//===----------------------------------------------------------------------===//
1516
1517IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1518 assert(Kind == FieldDesignator && "Only valid on a field designator");
1519 if (Field.NameOrField & 0x01)
1520 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1521 else
1522 return getField()->getIdentifier();
1523}
1524
1525DesignatedInitExpr *
1526DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1527 unsigned NumDesignators,
1528 Expr **IndexExprs, unsigned NumIndexExprs,
1529 SourceLocation ColonOrEqualLoc,
1530 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001531 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1532 sizeof(Designator) * NumDesignators +
1533 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001534 DesignatedInitExpr *DIE
1535 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1536 ColonOrEqualLoc, UsesColonSyntax,
1537 NumIndexExprs + 1);
1538
1539 // Fill in the designators
1540 unsigned ExpectedNumSubExprs = 0;
1541 designators_iterator Desig = DIE->designators_begin();
1542 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1543 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1544 if (Designators[Idx].isArrayDesignator())
1545 ++ExpectedNumSubExprs;
1546 else if (Designators[Idx].isArrayRangeDesignator())
1547 ExpectedNumSubExprs += 2;
1548 }
1549 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1550
1551 // Fill in the subexpressions, including the initializer expression.
1552 child_iterator Child = DIE->child_begin();
1553 *Child++ = Init;
1554 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1555 *Child = IndexExprs[Idx];
1556
1557 return DIE;
1558}
1559
1560SourceRange DesignatedInitExpr::getSourceRange() const {
1561 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001562 Designator &First =
1563 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001564 if (First.isFieldDesignator()) {
1565 if (UsesColonSyntax)
1566 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1567 else
1568 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1569 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001570 StartLoc =
1571 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001572 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1573}
1574
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001575DesignatedInitExpr::designators_iterator
1576DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001577 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1578 Ptr += sizeof(DesignatedInitExpr);
1579 return static_cast<Designator*>(static_cast<void*>(Ptr));
1580}
1581
1582DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1583 return designators_begin() + NumDesignators;
1584}
1585
1586Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1587 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1588 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1589 Ptr += sizeof(DesignatedInitExpr);
1590 Ptr += sizeof(Designator) * NumDesignators;
1591 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1592 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1593}
1594
1595Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1596 assert(D.Kind == Designator::ArrayRangeDesignator &&
1597 "Requires array range designator");
1598 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1599 Ptr += sizeof(DesignatedInitExpr);
1600 Ptr += sizeof(Designator) * NumDesignators;
1601 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1602 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1603}
1604
1605Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1606 assert(D.Kind == Designator::ArrayRangeDesignator &&
1607 "Requires array range designator");
1608 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1609 Ptr += sizeof(DesignatedInitExpr);
1610 Ptr += sizeof(Designator) * NumDesignators;
1611 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1612 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1613}
1614
1615//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001616// ExprIterator.
1617//===----------------------------------------------------------------------===//
1618
1619Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1620Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1621Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1622const Expr* ConstExprIterator::operator[](size_t idx) const {
1623 return cast<Expr>(I[idx]);
1624}
1625const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1626const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1627
1628//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001629// Child Iterators for iterating over subexpressions/substatements
1630//===----------------------------------------------------------------------===//
1631
1632// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001633Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1634Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001635
Steve Naroff7779db42007-11-12 14:29:37 +00001636// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001637Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1638Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001639
Steve Naroffe3e9add2008-06-02 23:03:37 +00001640// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001641Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1642Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001643
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001644// ObjCKVCRefExpr
1645Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1646Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1647
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001648// ObjCSuperExpr
1649Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1650Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1651
Chris Lattnerd9f69102008-08-10 01:53:14 +00001652// PredefinedExpr
1653Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1654Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001655
1656// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001657Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1658Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001659
1660// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001661Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001662Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001663
1664// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001665Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1666Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001667
Chris Lattner5d661452007-08-26 03:42:43 +00001668// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001669Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1670Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001671
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001672// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001673Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1674Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001675
1676// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001677Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1678Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001679
1680// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001681Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1682Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001683
Sebastian Redl05189992008-11-11 17:56:53 +00001684// SizeOfAlignOfExpr
1685Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1686 // If this is of a type and the type is a VLA type (and not a typedef), the
1687 // size expression of the VLA needs to be treated as an executable expression.
1688 // Why isn't this weirdness documented better in StmtIterator?
1689 if (isArgumentType()) {
1690 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1691 getArgumentType().getTypePtr()))
1692 return child_iterator(T);
1693 return child_iterator();
1694 }
Sebastian Redld4575892008-12-03 23:17:54 +00001695 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001696}
Sebastian Redl05189992008-11-11 17:56:53 +00001697Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1698 if (isArgumentType())
1699 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001700 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001701}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001702
1703// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001704Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001705 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001706}
Ted Kremenek1237c672007-08-24 20:06:47 +00001707Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001708 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001709}
1710
1711// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001712Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001713 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001714}
Ted Kremenek1237c672007-08-24 20:06:47 +00001715Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001716 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001717}
Ted Kremenek1237c672007-08-24 20:06:47 +00001718
1719// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001720Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1721Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001722
Nate Begeman213541a2008-04-18 23:10:10 +00001723// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001724Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1725Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001726
1727// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001728Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1729Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001730
Ted Kremenek1237c672007-08-24 20:06:47 +00001731// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001732Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1733Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001734
1735// BinaryOperator
1736Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001737 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001738}
Ted Kremenek1237c672007-08-24 20:06:47 +00001739Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001740 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001741}
1742
1743// ConditionalOperator
1744Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001745 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001746}
Ted Kremenek1237c672007-08-24 20:06:47 +00001747Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001748 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001749}
1750
1751// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001752Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1753Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001754
Ted Kremenek1237c672007-08-24 20:06:47 +00001755// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001756Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1757Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001758
1759// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001760Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1761 return child_iterator();
1762}
1763
1764Stmt::child_iterator TypesCompatibleExpr::child_end() {
1765 return child_iterator();
1766}
Ted Kremenek1237c672007-08-24 20:06:47 +00001767
1768// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001769Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1770Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001771
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001772// GNUNullExpr
1773Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1774Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1775
Eli Friedmand38617c2008-05-14 19:38:39 +00001776// ShuffleVectorExpr
1777Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001778 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001779}
1780Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001781 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001782}
1783
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001784// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001785Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1786Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001787
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001788// InitListExpr
1789Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001790 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001791}
1792Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001793 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001794}
1795
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001796// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001797Stmt::child_iterator DesignatedInitExpr::child_begin() {
1798 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1799 Ptr += sizeof(DesignatedInitExpr);
1800 Ptr += sizeof(Designator) * NumDesignators;
1801 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1802}
1803Stmt::child_iterator DesignatedInitExpr::child_end() {
1804 return child_iterator(&*child_begin() + NumSubExprs);
1805}
1806
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001807// ImplicitValueInitExpr
1808Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1809 return child_iterator();
1810}
1811
1812Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1813 return child_iterator();
1814}
1815
Ted Kremenek1237c672007-08-24 20:06:47 +00001816// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001817Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001818 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001819}
1820Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001821 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001822}
Ted Kremenek1237c672007-08-24 20:06:47 +00001823
1824// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001825Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1826Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001827
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001828// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001829Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1830 return child_iterator();
1831}
1832Stmt::child_iterator ObjCSelectorExpr::child_end() {
1833 return child_iterator();
1834}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001835
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001836// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001837Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1838 return child_iterator();
1839}
1840Stmt::child_iterator ObjCProtocolExpr::child_end() {
1841 return child_iterator();
1842}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001843
Steve Naroff563477d2007-09-18 23:55:05 +00001844// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001845Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001846 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001847}
1848Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001849 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001850}
1851
Steve Naroff4eb206b2008-09-03 18:15:37 +00001852// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001853Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1854Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001855
Ted Kremenek9da13f92008-09-26 23:24:14 +00001856Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1857Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }