blob: 81da44407b1f9789992d5dd677097cffac23142e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerda8249e2008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
40
Ted Kremenek6e94ef52009-02-06 19:55:15 +000041StringLiteral::StringLiteral(ASTContext& C, const char *strData,
Chris Lattner726e1682009-02-18 05:49:11 +000042 unsigned byteLength, bool Wide, QualType Ty,
43 SourceLocation Loc) :
44 Expr(StringLiteralClass, Ty) {
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // OPTIMIZE: could allocate this appended to the StringLiteral.
Ted Kremenek6e94ef52009-02-06 19:55:15 +000046 char *AStrData = new (C, 1) char[byteLength];
Reid Spencer5f016e22007-07-11 17:01:13 +000047 memcpy(AStrData, strData, byteLength);
48 StrData = AStrData;
49 ByteLength = byteLength;
50 IsWide = Wide;
Chris Lattner726e1682009-02-18 05:49:11 +000051 TokLocs[0] = Loc;
52 NumConcatenated = 1;
Reid Spencer5f016e22007-07-11 17:01:13 +000053}
54
Chris Lattner726e1682009-02-18 05:49:11 +000055StringLiteral::StringLiteral(ASTContext &C, const char *strData,
56 unsigned byteLength, bool Wide, QualType Ty,
57 SourceLocation *Loc, unsigned NumStrs) :
58 Expr(StringLiteralClass, Ty) {
59 // OPTIMIZE: could allocate this appended to the StringLiteral.
60 char *AStrData = new (C, 1) char[byteLength];
61 memcpy(AStrData, strData, byteLength);
62 StrData = AStrData;
63 ByteLength = byteLength;
64 IsWide = Wide;
65 TokLocs[0] = Loc[0];
66 NumConcatenated = NumStrs;
67 if (NumStrs != 1)
68 memcpy(&TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
69}
70
71
Ted Kremenek6e94ef52009-02-06 19:55:15 +000072void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000073 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000074 this->~StringLiteral();
75 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000076}
77
78bool UnaryOperator::isPostfix(Opcode Op) {
79 switch (Op) {
80 case PostInc:
81 case PostDec:
82 return true;
83 default:
84 return false;
85 }
86}
87
Ted Kremenek5a56ac32008-07-23 22:18:43 +000088bool UnaryOperator::isPrefix(Opcode Op) {
89 switch (Op) {
90 case PreInc:
91 case PreDec:
92 return true;
93 default:
94 return false;
95 }
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
99/// corresponds to, e.g. "sizeof" or "[pre]++".
100const char *UnaryOperator::getOpcodeStr(Opcode Op) {
101 switch (Op) {
102 default: assert(0 && "Unknown unary operator");
103 case PostInc: return "++";
104 case PostDec: return "--";
105 case PreInc: return "++";
106 case PreDec: return "--";
107 case AddrOf: return "&";
108 case Deref: return "*";
109 case Plus: return "+";
110 case Minus: return "-";
111 case Not: return "~";
112 case LNot: return "!";
113 case Real: return "__real";
114 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000116 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 }
118}
119
120//===----------------------------------------------------------------------===//
121// Postfix Operators.
122//===----------------------------------------------------------------------===//
123
Ted Kremenek668bf912009-02-09 20:51:47 +0000124CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000125 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000126 : Expr(SC, t,
127 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000128 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000129 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000130
131 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000132 SubExprs[FN] = fn;
133 for (unsigned i = 0; i != numargs; ++i)
134 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000135
Douglas Gregorb4609802008-11-14 16:09:21 +0000136 RParenLoc = rparenloc;
137}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000138
Ted Kremenek668bf912009-02-09 20:51:47 +0000139CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
140 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000141 : Expr(CallExprClass, t,
142 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000143 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000144 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000145
146 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000147 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000149 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 RParenLoc = rparenloc;
152}
153
Ted Kremenek668bf912009-02-09 20:51:47 +0000154void CallExpr::Destroy(ASTContext& C) {
155 DestroyChildren(C);
156 if (SubExprs) C.Deallocate(SubExprs);
157 this->~CallExpr();
158 C.Deallocate(this);
159}
160
Chris Lattnerd18b3292007-12-28 05:25:02 +0000161/// setNumArgs - This changes the number of arguments present in this call.
162/// Any orphaned expressions are deleted by this, and any new operands are set
163/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000164void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000165 // No change, just return.
166 if (NumArgs == getNumArgs()) return;
167
168 // If shrinking # arguments, just delete the extras and forgot them.
169 if (NumArgs < getNumArgs()) {
170 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000171 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000172 this->NumArgs = NumArgs;
173 return;
174 }
175
176 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000177 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000178 // Copy over args.
179 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
180 NewSubExprs[i] = SubExprs[i];
181 // Null out new args.
182 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
183 NewSubExprs[i] = 0;
184
Ted Kremenek8189cde2009-02-07 01:47:29 +0000185 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000186 SubExprs = NewSubExprs;
187 this->NumArgs = NumArgs;
188}
189
Chris Lattnercb888962008-10-06 05:00:53 +0000190/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
191/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000192unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000193 // All simple function calls (e.g. func()) are implicitly cast to pointer to
194 // function. As a result, we try and obtain the DeclRefExpr from the
195 // ImplicitCastExpr.
196 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
197 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000198 return 0;
199
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000200 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
201 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000202 return 0;
203
Anders Carlssonbcba2012008-01-31 02:13:57 +0000204 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
205 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000206 return 0;
207
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000208 if (!FDecl->getIdentifier())
209 return 0;
210
Douglas Gregor3c385e52009-02-14 18:57:46 +0000211 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000212}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000213
Chris Lattnercb888962008-10-06 05:00:53 +0000214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
216/// corresponds to, e.g. "<<=".
217const char *BinaryOperator::getOpcodeStr(Opcode Op) {
218 switch (Op) {
219 default: assert(0 && "Unknown binary operator");
220 case Mul: return "*";
221 case Div: return "/";
222 case Rem: return "%";
223 case Add: return "+";
224 case Sub: return "-";
225 case Shl: return "<<";
226 case Shr: return ">>";
227 case LT: return "<";
228 case GT: return ">";
229 case LE: return "<=";
230 case GE: return ">=";
231 case EQ: return "==";
232 case NE: return "!=";
233 case And: return "&";
234 case Xor: return "^";
235 case Or: return "|";
236 case LAnd: return "&&";
237 case LOr: return "||";
238 case Assign: return "=";
239 case MulAssign: return "*=";
240 case DivAssign: return "/=";
241 case RemAssign: return "%=";
242 case AddAssign: return "+=";
243 case SubAssign: return "-=";
244 case ShlAssign: return "<<=";
245 case ShrAssign: return ">>=";
246 case AndAssign: return "&=";
247 case XorAssign: return "^=";
248 case OrAssign: return "|=";
249 case Comma: return ",";
250 }
251}
252
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000253InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000254 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000255 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000256 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000257 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000258 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000259
260 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000261}
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
Douglas Gregor4c678342009-01-28 21:54:33 +0000263void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000264 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000265 Idx < LastIdx; ++Idx)
Douglas Gregor4c678342009-01-28 21:54:33 +0000266 delete InitExprs[Idx];
267 InitExprs.resize(NumInits, 0);
268}
269
270Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
271 if (Init >= InitExprs.size()) {
272 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
273 InitExprs.back() = expr;
274 return 0;
275 }
276
277 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
278 InitExprs[Init] = expr;
279 return Result;
280}
281
Steve Naroffbfdcae62008-09-04 15:31:07 +0000282/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000283///
284const FunctionType *BlockExpr::getFunctionType() const {
285 return getType()->getAsBlockPointerType()->
286 getPointeeType()->getAsFunctionType();
287}
288
Steve Naroff56ee6892008-10-08 17:01:13 +0000289SourceLocation BlockExpr::getCaretLocation() const {
290 return TheBlock->getCaretLocation();
291}
292const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
293Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
294
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296//===----------------------------------------------------------------------===//
297// Generic Expression Routines
298//===----------------------------------------------------------------------===//
299
Chris Lattner026dc962009-02-14 07:37:35 +0000300/// isUnusedResultAWarning - Return true if this immediate expression should
301/// be warned about if the result is unused. If so, fill in Loc and Ranges
302/// with location to warn on and the source range[s] to report with the
303/// warning.
304bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
305 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 switch (getStmtClass()) {
307 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000308 Loc = getExprLoc();
309 R1 = getSourceRange();
310 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000312 return cast<ParenExpr>(this)->getSubExpr()->
313 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 case UnaryOperatorClass: {
315 const UnaryOperator *UO = cast<UnaryOperator>(this);
316
317 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000318 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 case UnaryOperator::PostInc:
320 case UnaryOperator::PostDec:
321 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000322 case UnaryOperator::PreDec: // ++/--
323 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 case UnaryOperator::Deref:
325 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000326 if (getType().isVolatileQualified())
327 return false;
328 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 case UnaryOperator::Real:
330 case UnaryOperator::Imag:
331 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000332 if (UO->getSubExpr()->getType().isVolatileQualified())
333 return false;
334 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000336 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Chris Lattner026dc962009-02-14 07:37:35 +0000338 Loc = UO->getOperatorLoc();
339 R1 = UO->getSubExpr()->getSourceRange();
340 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000342 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000343 const BinaryOperator *BO = cast<BinaryOperator>(this);
344 // Consider comma to have side effects if the LHS or RHS does.
345 if (BO->getOpcode() == BinaryOperator::Comma)
346 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
347 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000348
Chris Lattner026dc962009-02-14 07:37:35 +0000349 if (BO->isAssignmentOp())
350 return false;
351 Loc = BO->getOperatorLoc();
352 R1 = BO->getLHS()->getSourceRange();
353 R2 = BO->getRHS()->getSourceRange();
354 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000355 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000356 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000357 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000358
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000359 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000360 // The condition must be evaluated, but if either the LHS or RHS is a
361 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000362 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner026dc962009-02-14 07:37:35 +0000363 if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
364 return true;
365 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000366 }
367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000369 // If the base pointer or element is to a volatile pointer/field, accessing
370 // it is a side effect.
371 if (getType().isVolatileQualified())
372 return false;
373 Loc = cast<MemberExpr>(this)->getMemberLoc();
374 R1 = SourceRange(Loc, Loc);
375 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
376 return true;
377
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 case ArraySubscriptExprClass:
379 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000380 // it is a side effect.
381 if (getType().isVolatileQualified())
382 return false;
383 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
384 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
385 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
386 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000387
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000389 case CXXOperatorCallExprClass: {
390 // If this is a direct call, get the callee.
391 const CallExpr *CE = cast<CallExpr>(this);
392 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
393 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
394 // If the callee has attribute pure, const, or warn_unused_result, warn
395 // about it. void foo() { strlen("bar"); } should warn.
396 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
397 if (FD->getAttr<WarnUnusedResultAttr>() ||
398 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
399 Loc = CE->getCallee()->getLocStart();
400 R1 = CE->getCallee()->getSourceRange();
401
402 if (unsigned NumArgs = CE->getNumArgs())
403 R2 = SourceRange(CE->getArg(0)->getLocStart(),
404 CE->getArg(NumArgs-1)->getLocEnd());
405 return true;
406 }
407 }
408 return false;
409 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000410 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000411 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000412 case StmtExprClass: {
413 // Statement exprs don't logically have side effects themselves, but are
414 // sometimes used in macros in ways that give them a type that is unused.
415 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
416 // however, if the result of the stmt expr is dead, we don't want to emit a
417 // warning.
418 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
419 if (!CS->body_empty())
420 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000421 return E->isUnusedResultAWarning(Loc, R1, R2);
422
423 Loc = cast<StmtExpr>(this)->getLParenLoc();
424 R1 = getSourceRange();
425 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000426 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000427 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000428 // If this is a cast to void, check the operand. Otherwise, the result of
429 // the cast is unused.
430 if (getType()->isVoidType())
431 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
432 R1, R2);
433 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
434 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
435 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000436 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 // If this is a cast to void, check the operand. Otherwise, the result of
438 // the cast is unused.
439 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000440 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
441 R1, R2);
442 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
443 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
444 return true;
445
Eli Friedman4be1f472008-05-19 21:24:43 +0000446 case ImplicitCastExprClass:
447 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000448 return cast<ImplicitCastExpr>(this)
449 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000450
Chris Lattner04421082008-04-08 04:40:51 +0000451 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000452 return cast<CXXDefaultArgExpr>(this)
453 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000454
455 case CXXNewExprClass:
456 // FIXME: In theory, there might be new expressions that don't have side
457 // effects (e.g. a placement new with an uninitialized POD).
458 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000459 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000460 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000461}
462
Douglas Gregorba7e2102008-10-22 15:04:37 +0000463/// DeclCanBeLvalue - Determine whether the given declaration can be
464/// an lvalue. This is a helper routine for isLvalue.
465static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000466 // C++ [temp.param]p6:
467 // A non-type non-reference template-parameter is not an lvalue.
468 if (const NonTypeTemplateParmDecl *NTTParm
469 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
470 return NTTParm->getType()->isReferenceType();
471
Douglas Gregor44b43212008-12-11 16:49:14 +0000472 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000473 // C++ 3.10p2: An lvalue refers to an object or function.
474 (Ctx.getLangOptions().CPlusPlus &&
475 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
476}
477
Reid Spencer5f016e22007-07-11 17:01:13 +0000478/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
479/// incomplete type other than void. Nonarray expressions that can be lvalues:
480/// - name, where name must be a variable
481/// - e[i]
482/// - (e), where e must be an lvalue
483/// - e.name, where e must be an lvalue
484/// - e->name
485/// - *e, the type of e cannot be a function type
486/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000487/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000488/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000489///
Chris Lattner28be73f2008-07-26 21:30:36 +0000490Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000491 // first, check the type (C99 6.3.2.1). Expressions with function
492 // type in C are not lvalues, but they can be lvalues in C++.
493 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return LV_NotObjectType;
495
Steve Naroffacb818a2008-02-10 01:39:04 +0000496 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000497 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000498 return LV_IncompleteVoidType;
499
Douglas Gregor98cd5992008-10-21 23:43:52 +0000500 /// FIXME: Expressions can't have reference type, so the following
501 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000502 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000503 return LV_Valid;
504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // the type looks fine, now check the expression
506 switch (getStmtClass()) {
507 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000508 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
510 // For vectors, make sure base is an lvalue (i.e. not a function call).
511 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000512 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000514 case DeclRefExprClass:
515 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000516 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
517 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 return LV_Valid;
519 break;
Chris Lattner41110242008-06-17 18:05:57 +0000520 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000521 case BlockDeclRefExprClass: {
522 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000523 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000524 return LV_Valid;
525 break;
526 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000527 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000529 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
530 NamedDecl *Member = m->getMemberDecl();
531 // C++ [expr.ref]p4:
532 // If E2 is declared to have type "reference to T", then E1.E2
533 // is an lvalue.
534 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
535 if (Value->getType()->isReferenceType())
536 return LV_Valid;
537
538 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
539 if (isa<CXXClassVarDecl>(Member))
540 return LV_Valid;
541
542 // -- If E2 is a non-static data member [...]. If E1 is an
543 // lvalue, then E1.E2 is an lvalue.
544 if (isa<FieldDecl>(Member))
545 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
546
547 // -- If it refers to a static member function [...], then
548 // E1.E2 is an lvalue.
549 // -- Otherwise, if E1.E2 refers to a non-static member
550 // function [...], then E1.E2 is not an lvalue.
551 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
552 return Method->isStatic()? LV_Valid : LV_MemberFunction;
553
554 // -- If E2 is a member enumerator [...], the expression E1.E2
555 // is not an lvalue.
556 if (isa<EnumConstantDecl>(Member))
557 return LV_InvalidExpression;
558
559 // Not an lvalue.
560 return LV_InvalidExpression;
561 }
562
563 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000564 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000565 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000566 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000568 return LV_Valid; // C99 6.5.3p4
569
570 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000571 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
572 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000573 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000574
575 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
576 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
577 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
578 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000580 case ImplicitCastExprClass:
581 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
582 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000584 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000585 case BinaryOperatorClass:
586 case CompoundAssignOperatorClass: {
587 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000588
589 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
590 BinOp->getOpcode() == BinaryOperator::Comma)
591 return BinOp->getRHS()->isLvalue(Ctx);
592
Sebastian Redl22460502009-02-07 00:15:38 +0000593 // C++ [expr.mptr.oper]p6
594 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
595 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
596 !BinOp->getType()->isFunctionType())
597 return BinOp->getLHS()->isLvalue(Ctx);
598
Douglas Gregorbf3af052008-11-13 20:12:29 +0000599 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000600 return LV_InvalidExpression;
601
Douglas Gregorbf3af052008-11-13 20:12:29 +0000602 if (Ctx.getLangOptions().CPlusPlus)
603 // C++ [expr.ass]p1:
604 // The result of an assignment operation [...] is an lvalue.
605 return LV_Valid;
606
607
608 // C99 6.5.16:
609 // An assignment expression [...] is not an lvalue.
610 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000611 }
Nate Begeman59b5da62009-01-18 03:20:47 +0000612 // FIXME: OverloadExprClass
Douglas Gregorb4609802008-11-14 16:09:21 +0000613 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000614 case CXXOperatorCallExprClass:
615 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000616 // C++ [expr.call]p10:
617 // A function call is an lvalue if and only if the result type
618 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000619 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000620 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000621 CalleeType = FnTypePtr->getPointeeType();
622 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
623 if (FnType->getResultType()->isReferenceType())
624 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000625
626 break;
627 }
Steve Naroffe6386392007-12-05 04:00:10 +0000628 case CompoundLiteralExprClass: // C99 6.5.2.5p5
629 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000630 case ChooseExprClass:
631 // __builtin_choose_expr is an lvalue if the selected operand is.
632 if (cast<ChooseExpr>(this)->isConditionTrue(Ctx))
633 return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx);
634 else
635 return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx);
636
Nate Begeman213541a2008-04-18 23:10:10 +0000637 case ExtVectorElementExprClass:
638 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000639 return LV_DuplicateVectorComponents;
640 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000641 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
642 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000643 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
644 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000645 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000646 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000647 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000648 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000649 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000650 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000651 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000652 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000653 case CXXConditionDeclExprClass:
654 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000655 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000656 case CXXFunctionalCastExprClass:
657 case CXXStaticCastExprClass:
658 case CXXDynamicCastExprClass:
659 case CXXReinterpretCastExprClass:
660 case CXXConstCastExprClass:
661 // The result of an explicit cast is an lvalue if the type we are
662 // casting to is a reference type. See C++ [expr.cast]p1,
663 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
664 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
665 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
666 return LV_Valid;
667 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000668 case CXXTypeidExprClass:
669 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
670 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 default:
672 break;
673 }
674 return LV_InvalidExpression;
675}
676
677/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
678/// does not have an incomplete type, does not have a const-qualified type, and
679/// if it is a structure or union, does not have any member (including,
680/// recursively, any member or element of all contained aggregates or unions)
681/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000682Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
683 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000684
685 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000686 case LV_Valid:
687 // C++ 3.10p11: Functions cannot be modified, but pointers to
688 // functions can be modifiable.
689 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
690 return MLV_NotObjectType;
691 break;
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 case LV_NotObjectType: return MLV_NotObjectType;
694 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000695 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000696 case LV_InvalidExpression:
697 // If the top level is a C-style cast, and the subexpression is a valid
698 // lvalue, then this is probably a use of the old-school "cast as lvalue"
699 // GCC extension. We don't support it, but we want to produce good
700 // diagnostics when it happens so that the user knows why.
701 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
702 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
703 return MLV_LValueCast;
704 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000705 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000707
708 QualType CT = Ctx.getCanonicalType(getType());
709
710 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000712 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000714 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 return MLV_IncompleteType;
716
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000717 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 if (r->hasConstFields())
719 return MLV_ConstQualified;
720 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000721 // The following is illegal:
722 // void takeclosure(void (^C)(void));
723 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
724 //
725 if (getStmtClass() == BlockDeclRefExprClass) {
726 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
727 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
728 return MLV_NotBlockQualified;
729 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000730
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000731 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000732 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000733 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
734 if (KVCExpr->getSetterMethod() == 0)
735 return MLV_NoSetterProperty;
736 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 return MLV_Valid;
738}
739
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000740/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000741/// duration. This means that the address of this expression is a link-time
742/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000743bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000744 switch (getStmtClass()) {
745 default:
746 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000747 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000748 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000749 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000750 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000751 case CompoundLiteralExprClass:
752 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000753 case DeclRefExprClass:
754 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000755 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
756 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000757 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000758 if (isa<FunctionDecl>(D))
759 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000760 return false;
761 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000762 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000763 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000764 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000765 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000766 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000767 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000768 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000769 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000770 case CXXDefaultArgExprClass:
771 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000772 }
773}
774
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000775Expr* Expr::IgnoreParens() {
776 Expr* E = this;
777 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
778 E = P->getSubExpr();
779
780 return E;
781}
782
Chris Lattner56f34942008-02-13 01:02:39 +0000783/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
784/// or CastExprs or ImplicitCastExprs, returning their operand.
785Expr *Expr::IgnoreParenCasts() {
786 Expr *E = this;
787 while (true) {
788 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
789 E = P->getSubExpr();
790 else if (CastExpr *P = dyn_cast<CastExpr>(E))
791 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000792 else
793 return E;
794 }
795}
796
Douglas Gregor898574e2008-12-05 23:32:09 +0000797/// hasAnyTypeDependentArguments - Determines if any of the expressions
798/// in Exprs is type-dependent.
799bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
800 for (unsigned I = 0; I < NumExprs; ++I)
801 if (Exprs[I]->isTypeDependent())
802 return true;
803
804 return false;
805}
806
807/// hasAnyValueDependentArguments - Determines if any of the expressions
808/// in Exprs is value-dependent.
809bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
810 for (unsigned I = 0; I < NumExprs; ++I)
811 if (Exprs[I]->isValueDependent())
812 return true;
813
814 return false;
815}
816
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000817bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000818 // This function is attempting whether an expression is an initializer
819 // which can be evaluated at compile-time. isEvaluatable handles most
820 // of the cases, but it can't deal with some initializer-specific
821 // expressions, and it can't deal with aggregates; we deal with those here,
822 // and fall back to isEvaluatable for the other cases.
823
Anders Carlssone8a32b82008-11-24 05:23:59 +0000824 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000825 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000826 case StringLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000827 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000828 case CompoundLiteralExprClass: {
829 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000830 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000831 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000832 case InitListExprClass: {
833 const InitListExpr *Exp = cast<InitListExpr>(this);
834 unsigned numInits = Exp->getNumInits();
835 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000836 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000837 return false;
838 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000839 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000840 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000841 case ImplicitValueInitExprClass:
842 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000843 case ParenExprClass: {
844 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
845 }
846 case UnaryOperatorClass: {
847 const UnaryOperator* Exp = cast<UnaryOperator>(this);
848 if (Exp->getOpcode() == UnaryOperator::Extension)
849 return Exp->getSubExpr()->isConstantInitializer(Ctx);
850 break;
851 }
852 case CStyleCastExprClass:
853 // Handle casts with a destination that's a struct or union; this
854 // deals with both the gcc no-op struct cast extension and the
855 // cast-to-union extension.
856 if (getType()->isRecordType())
857 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
858 break;
Eli Friedman32a311e2009-01-25 03:27:40 +0000859 case DesignatedInitExprClass:
Sebastian Redl4e716e02009-01-25 13:34:47 +0000860 return cast<DesignatedInitExpr>(this)->
861 getInit()->isConstantInitializer(Ctx);
Anders Carlssone8a32b82008-11-24 05:23:59 +0000862 }
863
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000864 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +0000865}
866
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// isIntegerConstantExpr - this recursive routine will test if an expression is
868/// an integer constant expression. Note: With the introduction of VLA's in
869/// C99 the result of the sizeof operator is no longer always a constant
870/// expression. The generalization of the wording to include any subexpression
871/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
872/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopes5f6b6322008-07-08 21:13:06 +0000873/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Reid Spencer5f016e22007-07-11 17:01:13 +0000874/// occurring in a context requiring a constant, would have been a constraint
875/// violation. FIXME: This routine currently implements C90 semantics.
876/// To properly implement C99 semantics this routine will need to evaluate
877/// expressions involving operators previously mentioned.
878
879/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
880/// comma, etc
881///
882/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000883/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000884///
885/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
886/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
887/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000888bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
889 SourceLocation *Loc, bool isEvaluated) const {
Daniel Dunbar2d6744f2009-02-18 00:47:45 +0000890 if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated))
891 return false;
892 assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!");
893 return true;
894}
895
896bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx,
897 SourceLocation *Loc, bool isEvaluated) const {
898
Eli Friedmana6afa762008-11-13 06:09:17 +0000899 // Pretest for integral type; some parts of the code crash for types that
900 // can't be sized.
901 if (!getType()->isIntegralType()) {
902 if (Loc) *Loc = getLocStart();
903 return false;
904 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 switch (getStmtClass()) {
906 default:
907 if (Loc) *Loc = getLocStart();
908 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 case ParenExprClass:
910 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000911 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 case IntegerLiteralClass:
Daniel Dunbar2d6744f2009-02-18 00:47:45 +0000913 // NOTE: getValue() returns an APInt, we must set sign.
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 Result = cast<IntegerLiteral>(this)->getValue();
Daniel Dunbara1359752009-02-18 00:32:53 +0000915 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000917 case CharacterLiteralClass: {
918 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Daniel Dunbara1359752009-02-18 00:32:53 +0000919 Result = Ctx.MakeIntValue(CL->getValue(), getType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000921 }
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000922 case CXXBoolLiteralExprClass: {
923 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
Daniel Dunbara1359752009-02-18 00:32:53 +0000924 Result = Ctx.MakeIntValue(BL->getValue(), getType());
Anders Carlssonb88d45e2008-08-23 21:12:35 +0000925 break;
926 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000927 case CXXZeroInitValueExprClass:
Daniel Dunbara1359752009-02-18 00:32:53 +0000928 Result = Ctx.MakeIntValue(0, getType());
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000929 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000930 case TypesCompatibleExprClass: {
931 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Daniel Dunbarac620de2008-10-24 08:07:57 +0000932 // Per gcc docs "this built-in function ignores top level
933 // qualifiers". We need to use the canonical version to properly
934 // be able to strip CRV qualifiers from the type.
935 QualType T0 = Ctx.getCanonicalType(TCE->getArgType1());
936 QualType T1 = Ctx.getCanonicalType(TCE->getArgType2());
Daniel Dunbara1359752009-02-18 00:32:53 +0000937 Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(),
938 T1.getUnqualifiedType()),
939 getType());
Steve Naroff389cecc2007-08-02 00:13:27 +0000940 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000941 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000942 case CallExprClass:
943 case CXXOperatorCallExprClass: {
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000944 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnera4d55d82008-10-06 06:40:35 +0000945
946 // If this is a call to a builtin function, constant fold it otherwise
947 // reject it.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000948 if (CE->isBuiltinCall(Ctx)) {
Anders Carlsson1c0cfd42008-12-19 20:58:05 +0000949 EvalResult EvalResult;
950 if (CE->Evaluate(EvalResult, Ctx)) {
951 assert(!EvalResult.HasSideEffects &&
952 "Foldable builtin call should not have side effects!");
953 Result = EvalResult.Val.getInt();
Chris Lattnera4d55d82008-10-06 06:40:35 +0000954 break; // It is a constant, expand it.
955 }
956 }
957
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000958 if (Loc) *Loc = getLocStart();
959 return false;
960 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 case DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000962 case QualifiedDeclRefExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 if (const EnumConstantDecl *D =
964 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
965 Result = D->getInitVal();
966 break;
967 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +0000968 if (Ctx.getLangOptions().CPlusPlus &&
969 getType().getCVRQualifiers() == QualType::Const) {
970 // C++ 7.1.5.1p2
971 // A variable of non-volatile const-qualified integral or enumeration
972 // type initialized by an ICE can be used in ICEs.
973 if (const VarDecl *Dcl =
974 dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) {
975 if (const Expr *Init = Dcl->getInit())
976 return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
977 }
978 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 if (Loc) *Loc = getLocStart();
980 return false;
981 case UnaryOperatorClass: {
982 const UnaryOperator *Exp = cast<UnaryOperator>(this);
983
Sebastian Redl05189992008-11-11 17:56:53 +0000984 // Get the operand value. If this is offsetof, do not evalute the
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 // operand. This affects C99 6.6p3.
Sebastian Redl05189992008-11-11 17:56:53 +0000986 if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()->
987 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 return false;
989
990 switch (Exp->getOpcode()) {
991 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
992 // See C99 6.6p3.
993 default:
994 if (Loc) *Loc = Exp->getOperatorLoc();
995 return false;
996 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000997 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 case UnaryOperator::LNot: {
Daniel Dunbara1359752009-02-18 00:32:53 +0000999 Result = Ctx.MakeIntValue(Result == 0, getType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 break;
1001 }
1002 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 break;
1004 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 Result = -Result;
1006 break;
1007 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 Result = ~Result;
1009 break;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001010 case UnaryOperator::OffsetOf:
Daniel Dunbara1359752009-02-18 00:32:53 +00001011 Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType());
1012 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 }
1014 break;
1015 }
Sebastian Redl05189992008-11-11 17:56:53 +00001016 case SizeOfAlignOfExprClass: {
1017 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
Chris Lattnera269ebf2008-02-21 05:45:29 +00001018
Sebastian Redl05189992008-11-11 17:56:53 +00001019 QualType ArgTy = Exp->getTypeOfArgument();
Chris Lattnera269ebf2008-02-21 05:45:29 +00001020 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
Sebastian Redl05189992008-11-11 17:56:53 +00001021 if (ArgTy->isVoidType()) {
Daniel Dunbara1359752009-02-18 00:32:53 +00001022 Result = Ctx.MakeIntValue(1, getType());
Chris Lattnera269ebf2008-02-21 05:45:29 +00001023 break;
1024 }
1025
1026 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Sebastian Redl05189992008-11-11 17:56:53 +00001027 if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
Chris Lattner65383472007-12-18 07:15:40 +00001028 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 return false;
Chris Lattner65383472007-12-18 07:15:40 +00001030 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001031
Chris Lattner76e773a2007-07-18 18:38:36 +00001032 // Get information about the size or align.
Sebastian Redl05189992008-11-11 17:56:53 +00001033 if (ArgTy->isFunctionType()) {
Chris Lattnerefdd1572008-01-02 21:54:09 +00001034 // GCC extension: sizeof(function) = 1.
Daniel Dunbara1359752009-02-18 00:32:53 +00001035 Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType());
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001036 } else {
Chris Lattner98be4942008-03-05 18:54:05 +00001037 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001038 if (Exp->isSizeOf())
Daniel Dunbara1359752009-02-18 00:32:53 +00001039 Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType());
Anders Carlsson6a24acb2008-02-16 01:20:23 +00001040 else
Daniel Dunbara1359752009-02-18 00:32:53 +00001041 Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType());
Ted Kremenek060e4702007-12-17 17:38:43 +00001042 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 break;
1044 }
1045 case BinaryOperatorClass: {
1046 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbare1226d22008-09-22 23:53:24 +00001047 llvm::APSInt LHS, RHS;
1048
1049 // Initialize result to have correct signedness and width.
Daniel Dunbara1359752009-02-18 00:32:53 +00001050 Result = Ctx.MakeIntValue(0, getType());
Eli Friedmanb11e7782008-11-13 02:13:11 +00001051
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbare1226d22008-09-22 23:53:24 +00001053 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 return false;
1055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // The short-circuiting &&/|| operators don't necessarily evaluate their
1057 // RHS. Make sure to pass isEvaluated down correctly.
1058 if (Exp->isLogicalOp()) {
1059 bool RHSEval;
1060 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbare1226d22008-09-22 23:53:24 +00001061 RHSEval = LHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 else {
1063 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbare1226d22008-09-22 23:53:24 +00001064 RHSEval = LHS == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 }
1066
Chris Lattner590b6642007-07-15 23:26:56 +00001067 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 isEvaluated & RHSEval))
1069 return false;
1070 } else {
Chris Lattner590b6642007-07-15 23:26:56 +00001071 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 return false;
1073 }
1074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 switch (Exp->getOpcode()) {
1076 default:
1077 if (Loc) *Loc = getLocStart();
1078 return false;
1079 case BinaryOperator::Mul:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001080 Result = LHS * RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 break;
1082 case BinaryOperator::Div:
1083 if (RHS == 0) {
1084 if (!isEvaluated) break;
1085 if (Loc) *Loc = getLocStart();
1086 return false;
1087 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001088 Result = LHS / RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 break;
1090 case BinaryOperator::Rem:
1091 if (RHS == 0) {
1092 if (!isEvaluated) break;
1093 if (Loc) *Loc = getLocStart();
1094 return false;
1095 }
Daniel Dunbare1226d22008-09-22 23:53:24 +00001096 Result = LHS % RHS;
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001098 case BinaryOperator::Add: Result = LHS + RHS; break;
1099 case BinaryOperator::Sub: Result = LHS - RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 case BinaryOperator::Shl:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001101 Result = LHS <<
1102 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
1103 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 case BinaryOperator::Shr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001105 Result = LHS >>
1106 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 break;
Daniel Dunbare1226d22008-09-22 23:53:24 +00001108 case BinaryOperator::LT: Result = LHS < RHS; break;
1109 case BinaryOperator::GT: Result = LHS > RHS; break;
1110 case BinaryOperator::LE: Result = LHS <= RHS; break;
1111 case BinaryOperator::GE: Result = LHS >= RHS; break;
1112 case BinaryOperator::EQ: Result = LHS == RHS; break;
1113 case BinaryOperator::NE: Result = LHS != RHS; break;
1114 case BinaryOperator::And: Result = LHS & RHS; break;
1115 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
1116 case BinaryOperator::Or: Result = LHS | RHS; break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 case BinaryOperator::LAnd:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001118 Result = LHS != 0 && RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 break;
1120 case BinaryOperator::LOr:
Daniel Dunbare1226d22008-09-22 23:53:24 +00001121 Result = LHS != 0 || RHS != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 break;
Eli Friedmanb11e7782008-11-13 02:13:11 +00001123
1124 case BinaryOperator::Comma:
1125 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
1126 // *except* when they are contained within a subexpression that is not
1127 // evaluated". Note that Assignment can never happen due to constraints
1128 // on the LHS subexpr, so we don't need to check it here.
1129 if (isEvaluated) {
1130 if (Loc) *Loc = getLocStart();
1131 return false;
1132 }
1133
1134 // The result of the constant expr is the RHS.
1135 Result = RHS;
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001136 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 }
Daniel Dunbara1359752009-02-18 00:32:53 +00001138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
1140 break;
1141 }
Chris Lattner26dc7b32007-07-15 23:54:50 +00001142 case ImplicitCastExprClass:
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001143 case CStyleCastExprClass:
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +00001144 case CXXFunctionalCastExprClass: {
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001145 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
1146 SourceLocation CastLoc = getLocStart();
Chris Lattner26dc7b32007-07-15 23:54:50 +00001147
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001149 if (!SubExpr->getType()->isArithmeticType() ||
1150 !getType()->isIntegerType()) {
1151 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 return false;
1153 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001154
Chris Lattner98be4942008-03-05 18:54:05 +00001155 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner987b15d2007-09-22 19:04:13 +00001156
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001158 if (SubExpr->getType()->isIntegerType()) {
1159 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +00001161
1162 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001163 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001164 if (getType()->isBooleanType()) {
1165 // Conversion to bool compares against zero.
Daniel Dunbara1359752009-02-18 00:32:53 +00001166 Result = Ctx.MakeIntValue(Result != 0, getType());
1167 } else if (SubExpr->getType()->isSignedIntegerType()) {
Chris Lattner26dc7b32007-07-15 23:54:50 +00001168 Result.sextOrTrunc(DestWidth);
Daniel Dunbara1359752009-02-18 00:32:53 +00001169 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1170 } else { // If the input is unsigned, do a zero extend, noop,
1171 // or truncate.
Chris Lattner26dc7b32007-07-15 23:54:50 +00001172 Result.zextOrTrunc(DestWidth);
Daniel Dunbara1359752009-02-18 00:32:53 +00001173 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1174 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 break;
1176 }
1177
1178 // Allow floating constants that are the immediate operands of casts or that
1179 // are parenthesized.
Daniel Dunbara1359752009-02-18 00:32:53 +00001180 const Expr *Operand = SubExpr->IgnoreParens();
Chris Lattner987b15d2007-09-22 19:04:13 +00001181
1182 // If this isn't a floating literal, we can't handle it.
1183 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
1184 if (!FL) {
1185 if (Loc) *Loc = Operand->getLocStart();
1186 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 }
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001188
1189 // If the destination is boolean, compare against zero.
1190 if (getType()->isBooleanType()) {
Daniel Dunbara1359752009-02-18 00:32:53 +00001191 Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType());
Chris Lattnerc0a356b2008-01-09 18:59:34 +00001192 break;
1193 }
Chris Lattner987b15d2007-09-22 19:04:13 +00001194
1195 // Determine whether we are converting to unsigned or signed.
1196 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +00001197
1198 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
1199 // be called multiple times per AST.
Dale Johannesenee5a7002008-10-09 23:02:32 +00001200 uint64_t Space[4];
1201 bool ignored;
Chris Lattnerccc213f2007-09-26 00:47:26 +00001202 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesenee5a7002008-10-09 23:02:32 +00001203 llvm::APFloat::rmTowardZero,
1204 &ignored);
Chris Lattner987b15d2007-09-22 19:04:13 +00001205 Result = llvm::APInt(DestWidth, 4, Space);
Daniel Dunbara1359752009-02-18 00:32:53 +00001206 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
Chris Lattner987b15d2007-09-22 19:04:13 +00001207 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
1209 case ConditionalOperatorClass: {
1210 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1211
Chris Lattner28daa532008-12-12 06:55:44 +00001212 const Expr *Cond = Exp->getCond();
1213
1214 if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 return false;
1216
1217 const Expr *TrueExp = Exp->getLHS();
1218 const Expr *FalseExp = Exp->getRHS();
1219 if (Result == 0) std::swap(TrueExp, FalseExp);
1220
Chris Lattner28daa532008-12-12 06:55:44 +00001221 // If the condition (ignoring parens) is a __builtin_constant_p call,
1222 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001223 // expression, and it is fully evaluated. This is an important GNU
1224 // extension. See GCC PR38377 for discussion.
Chris Lattner28daa532008-12-12 06:55:44 +00001225 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001226 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Chris Lattner42b83dd2008-12-12 18:00:51 +00001227 EvalResult EVResult;
1228 if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects)
1229 return false;
1230 assert(EVResult.Val.isInt() && "FP conditional expr not expected");
1231 Result = EVResult.Val.getInt();
1232 if (Loc) *Loc = EVResult.DiagLoc;
1233 return true;
1234 }
Chris Lattner28daa532008-12-12 06:55:44 +00001235
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 // Evaluate the false one first, discard the result.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001237 llvm::APSInt Tmp;
1238 if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 return false;
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001240 // Evalute the true one, capture the result. Note that if TrueExp
1241 // is False then this is an instant of the gcc missing LHS
1242 // extension, and we will just reuse Result.
Anders Carlsson39073232007-11-30 19:04:31 +00001243 if (TrueExp &&
1244 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 break;
1247 }
Chris Lattner04421082008-04-08 04:40:51 +00001248 case CXXDefaultArgExprClass:
1249 return cast<CXXDefaultArgExpr>(this)
1250 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Sebastian Redl64b45f72009-01-05 20:52:13 +00001251
1252 case UnaryTypeTraitExprClass:
Daniel Dunbara1359752009-02-18 00:32:53 +00001253 Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(),
1254 getType());
1255 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 }
1257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 return true;
1259}
1260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1262/// integer constant expression with the value zero, or if this is one that is
1263/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001264bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1265{
Sebastian Redl07779722008-10-31 14:43:28 +00001266 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001267 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001268 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001269 // Check that it is a cast to void*.
1270 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1271 QualType Pointee = PT->getPointeeType();
1272 if (Pointee.getCVRQualifiers() == 0 &&
1273 Pointee->isVoidType() && // to void*
1274 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001275 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001276 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001278 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1279 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001280 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001281 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1282 // Accept ((void*)0) as a null pointer constant, as many other
1283 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001284 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001285 } else if (const CXXDefaultArgExpr *DefaultArg
1286 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001287 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001288 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001289 } else if (isa<GNUNullExpr>(this)) {
1290 // The GNU __null extension is always a null pointer constant.
1291 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001292 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001293
Steve Naroffaa58f002008-01-14 16:10:57 +00001294 // This expression must be an integer type.
1295 if (!getType()->isIntegerType())
1296 return false;
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 // If we have an integer constant expression, we need to *evaluate* it and
1299 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001300 // FIXME: We should probably return false if we're compiling in strict mode
1301 // and Diag is not null (this indicates that the value was foldable but not
1302 // an ICE.
1303 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001304 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001305 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001306}
Steve Naroff31a45842007-07-28 23:10:27 +00001307
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001308/// isBitField - Return true if this expression is a bit-field.
1309bool Expr::isBitField() {
1310 Expr *E = this->IgnoreParenCasts();
1311 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001312 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1313 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001314 return false;
1315}
1316
Chris Lattner2140e902009-02-16 22:14:05 +00001317/// isArrow - Return true if the base expression is a pointer to vector,
1318/// return false if the base expression is a vector.
1319bool ExtVectorElementExpr::isArrow() const {
1320 return getBase()->getType()->isPointerType();
1321}
1322
Nate Begeman213541a2008-04-18 23:10:10 +00001323unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001324 if (const VectorType *VT = getType()->getAsVectorType())
1325 return VT->getNumElements();
1326 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001327}
1328
Nate Begeman8a997642008-05-09 06:41:27 +00001329/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001330bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001331 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001332 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001333
1334 // Halving swizzles do not contain duplicate elements.
1335 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1336 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1337 return false;
1338
1339 // Advance past s-char prefix on hex swizzles.
1340 if (*compStr == 's') {
1341 compStr++;
1342 length--;
1343 }
Steve Narofffec0b492007-07-30 03:29:09 +00001344
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001345 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001346 const char *s = compStr+i;
1347 for (const char c = *s++; *s; s++)
1348 if (c == *s)
1349 return true;
1350 }
1351 return false;
1352}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001353
Nate Begeman8a997642008-05-09 06:41:27 +00001354/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001355void ExtVectorElementExpr::getEncodedElementAccess(
1356 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001357 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001358 if (*compStr == 's')
1359 compStr++;
1360
1361 bool isHi = !strcmp(compStr, "hi");
1362 bool isLo = !strcmp(compStr, "lo");
1363 bool isEven = !strcmp(compStr, "even");
1364 bool isOdd = !strcmp(compStr, "odd");
1365
Nate Begeman8a997642008-05-09 06:41:27 +00001366 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1367 uint64_t Index;
1368
1369 if (isHi)
1370 Index = e + i;
1371 else if (isLo)
1372 Index = i;
1373 else if (isEven)
1374 Index = 2 * i;
1375 else if (isOdd)
1376 Index = 2 * i + 1;
1377 else
1378 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001379
Nate Begeman3b8d1162008-05-13 21:03:02 +00001380 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001381 }
Nate Begeman8a997642008-05-09 06:41:27 +00001382}
1383
Steve Naroff68d331a2007-09-27 14:38:14 +00001384// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001385ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001386 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001387 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001388 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001389 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001390 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001391 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001392 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001393 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001394 if (NumArgs) {
1395 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001396 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1397 }
Steve Naroff563477d2007-09-18 23:55:05 +00001398 LBracloc = LBrac;
1399 RBracloc = RBrac;
1400}
1401
Steve Naroff68d331a2007-09-27 14:38:14 +00001402// constructor for class messages.
1403// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001404ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001405 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001406 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001407 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001408 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001409 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001410 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001411 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001412 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001413 if (NumArgs) {
1414 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001415 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1416 }
Steve Naroff563477d2007-09-18 23:55:05 +00001417 LBracloc = LBrac;
1418 RBracloc = RBrac;
1419}
1420
Ted Kremenek4df728e2008-06-24 15:50:53 +00001421// constructor for class messages.
1422ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1423 QualType retType, ObjCMethodDecl *mproto,
1424 SourceLocation LBrac, SourceLocation RBrac,
1425 Expr **ArgExprs, unsigned nargs)
1426: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1427MethodProto(mproto) {
1428 NumArgs = nargs;
1429 SubExprs = new Stmt*[NumArgs+1];
1430 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1431 if (NumArgs) {
1432 for (unsigned i = 0; i != NumArgs; ++i)
1433 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1434 }
1435 LBracloc = LBrac;
1436 RBracloc = RBrac;
1437}
1438
1439ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1440 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1441 switch (x & Flags) {
1442 default:
1443 assert(false && "Invalid ObjCMessageExpr.");
1444 case IsInstMeth:
1445 return ClassInfo(0, 0);
1446 case IsClsMethDeclUnknown:
1447 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1448 case IsClsMethDeclKnown: {
1449 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1450 return ClassInfo(D, D->getIdentifier());
1451 }
1452 }
1453}
1454
Chris Lattner27437ca2007-10-25 00:29:32 +00001455bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001456 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001457}
1458
Chris Lattner670a62c2008-12-12 05:35:08 +00001459static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) {
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001460 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1461 QualType Ty = ME->getBase()->getType();
1462
1463 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner98be4942008-03-05 18:54:05 +00001464 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Douglas Gregor86f19402008-12-20 23:49:58 +00001465 if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
1466 // FIXME: This is linear time. And the fact that we're indexing
1467 // into the layout by position in the record means that we're
1468 // either stuck numbering the fields in the AST or we have to keep
1469 // the linear search (yuck and yuck).
1470 unsigned i = 0;
1471 for (RecordDecl::field_iterator Field = RD->field_begin(),
1472 FieldEnd = RD->field_end();
1473 Field != FieldEnd; (void)++Field, ++i) {
1474 if (*Field == FD)
1475 break;
1476 }
1477
1478 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001479 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001480 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1481 const Expr *Base = ASE->getBase();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001482
Chris Lattner98be4942008-03-05 18:54:05 +00001483 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001484 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001485
1486 return size + evaluateOffsetOf(C, Base);
1487 } else if (isa<CompoundLiteralExpr>(E))
1488 return 0;
1489
1490 assert(0 && "Unknown offsetof subexpression!");
1491 return 0;
1492}
1493
1494int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1495{
1496 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1497
Chris Lattner98be4942008-03-05 18:54:05 +00001498 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek55499762008-06-17 02:43:46 +00001499 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001500}
1501
Sebastian Redl05189992008-11-11 17:56:53 +00001502void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1503 // Override default behavior of traversing children. If this has a type
1504 // operand and the type is a variable-length array, the child iteration
1505 // will iterate over the size expression. However, this expression belongs
1506 // to the type, not to this, so we don't want to delete it.
1507 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001508 if (isArgumentType()) {
1509 this->~SizeOfAlignOfExpr();
1510 C.Deallocate(this);
1511 }
Sebastian Redl05189992008-11-11 17:56:53 +00001512 else
1513 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001514}
1515
Ted Kremenekfb7413f2009-02-09 17:08:14 +00001516void OverloadExpr::Destroy(ASTContext& C) {
1517 DestroyChildren(C);
1518 C.Deallocate(SubExprs);
1519 this->~OverloadExpr();
1520 C.Deallocate(this);
1521}
1522
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001523//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001524// DesignatedInitExpr
1525//===----------------------------------------------------------------------===//
1526
1527IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1528 assert(Kind == FieldDesignator && "Only valid on a field designator");
1529 if (Field.NameOrField & 0x01)
1530 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1531 else
1532 return getField()->getIdentifier();
1533}
1534
1535DesignatedInitExpr *
1536DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1537 unsigned NumDesignators,
1538 Expr **IndexExprs, unsigned NumIndexExprs,
1539 SourceLocation ColonOrEqualLoc,
1540 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001541 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1542 sizeof(Designator) * NumDesignators +
1543 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001544 DesignatedInitExpr *DIE
1545 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1546 ColonOrEqualLoc, UsesColonSyntax,
1547 NumIndexExprs + 1);
1548
1549 // Fill in the designators
1550 unsigned ExpectedNumSubExprs = 0;
1551 designators_iterator Desig = DIE->designators_begin();
1552 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1553 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1554 if (Designators[Idx].isArrayDesignator())
1555 ++ExpectedNumSubExprs;
1556 else if (Designators[Idx].isArrayRangeDesignator())
1557 ExpectedNumSubExprs += 2;
1558 }
1559 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1560
1561 // Fill in the subexpressions, including the initializer expression.
1562 child_iterator Child = DIE->child_begin();
1563 *Child++ = Init;
1564 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1565 *Child = IndexExprs[Idx];
1566
1567 return DIE;
1568}
1569
1570SourceRange DesignatedInitExpr::getSourceRange() const {
1571 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001572 Designator &First =
1573 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001574 if (First.isFieldDesignator()) {
1575 if (UsesColonSyntax)
1576 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1577 else
1578 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1579 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001580 StartLoc =
1581 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001582 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1583}
1584
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001585DesignatedInitExpr::designators_iterator
1586DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001587 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1588 Ptr += sizeof(DesignatedInitExpr);
1589 return static_cast<Designator*>(static_cast<void*>(Ptr));
1590}
1591
1592DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1593 return designators_begin() + NumDesignators;
1594}
1595
1596Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1597 assert(D.Kind == Designator::ArrayDesignator && "Requires array 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::getArrayRangeStart(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 + 1));
1613}
1614
1615Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1616 assert(D.Kind == Designator::ArrayRangeDesignator &&
1617 "Requires array range designator");
1618 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1619 Ptr += sizeof(DesignatedInitExpr);
1620 Ptr += sizeof(Designator) * NumDesignators;
1621 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1622 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1623}
1624
1625//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001626// ExprIterator.
1627//===----------------------------------------------------------------------===//
1628
1629Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1630Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1631Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1632const Expr* ConstExprIterator::operator[](size_t idx) const {
1633 return cast<Expr>(I[idx]);
1634}
1635const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1636const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1637
1638//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001639// Child Iterators for iterating over subexpressions/substatements
1640//===----------------------------------------------------------------------===//
1641
1642// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001643Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1644Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001645
Steve Naroff7779db42007-11-12 14:29:37 +00001646// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001647Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1648Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001649
Steve Naroffe3e9add2008-06-02 23:03:37 +00001650// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001651Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1652Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001653
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001654// ObjCKVCRefExpr
1655Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1656Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1657
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001658// ObjCSuperExpr
1659Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1660Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1661
Chris Lattnerd9f69102008-08-10 01:53:14 +00001662// PredefinedExpr
1663Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1664Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001665
1666// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001667Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1668Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001669
1670// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001671Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001672Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001673
1674// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001675Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1676Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001677
Chris Lattner5d661452007-08-26 03:42:43 +00001678// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001679Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1680Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001681
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001682// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001683Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1684Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001685
1686// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001687Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1688Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001689
1690// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001691Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1692Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001693
Sebastian Redl05189992008-11-11 17:56:53 +00001694// SizeOfAlignOfExpr
1695Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1696 // If this is of a type and the type is a VLA type (and not a typedef), the
1697 // size expression of the VLA needs to be treated as an executable expression.
1698 // Why isn't this weirdness documented better in StmtIterator?
1699 if (isArgumentType()) {
1700 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1701 getArgumentType().getTypePtr()))
1702 return child_iterator(T);
1703 return child_iterator();
1704 }
Sebastian Redld4575892008-12-03 23:17:54 +00001705 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001706}
Sebastian Redl05189992008-11-11 17:56:53 +00001707Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1708 if (isArgumentType())
1709 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001710 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001711}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001712
1713// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001714Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001715 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001716}
Ted Kremenek1237c672007-08-24 20:06:47 +00001717Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001718 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001719}
1720
1721// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001722Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001723 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001724}
Ted Kremenek1237c672007-08-24 20:06:47 +00001725Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001726 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001727}
Ted Kremenek1237c672007-08-24 20:06:47 +00001728
1729// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001730Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1731Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001732
Nate Begeman213541a2008-04-18 23:10:10 +00001733// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001734Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1735Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001736
1737// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001738Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1739Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001740
Ted Kremenek1237c672007-08-24 20:06:47 +00001741// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001742Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1743Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001744
1745// BinaryOperator
1746Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001747 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001748}
Ted Kremenek1237c672007-08-24 20:06:47 +00001749Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001750 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001751}
1752
1753// ConditionalOperator
1754Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001755 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001756}
Ted Kremenek1237c672007-08-24 20:06:47 +00001757Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001758 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001759}
1760
1761// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001762Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1763Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001764
Ted Kremenek1237c672007-08-24 20:06:47 +00001765// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001766Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1767Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001768
1769// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001770Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1771 return child_iterator();
1772}
1773
1774Stmt::child_iterator TypesCompatibleExpr::child_end() {
1775 return child_iterator();
1776}
Ted Kremenek1237c672007-08-24 20:06:47 +00001777
1778// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001779Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1780Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001781
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001782// GNUNullExpr
1783Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1784Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1785
Nate Begemane2ce1d92008-01-17 17:46:27 +00001786// OverloadExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001787Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1788Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begemane2ce1d92008-01-17 17:46:27 +00001789
Eli Friedmand38617c2008-05-14 19:38:39 +00001790// ShuffleVectorExpr
1791Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001792 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001793}
1794Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001795 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001796}
1797
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001798// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001799Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1800Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001801
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001802// InitListExpr
1803Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001804 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001805}
1806Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001807 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001808}
1809
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001810// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001811Stmt::child_iterator DesignatedInitExpr::child_begin() {
1812 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1813 Ptr += sizeof(DesignatedInitExpr);
1814 Ptr += sizeof(Designator) * NumDesignators;
1815 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1816}
1817Stmt::child_iterator DesignatedInitExpr::child_end() {
1818 return child_iterator(&*child_begin() + NumSubExprs);
1819}
1820
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001821// ImplicitValueInitExpr
1822Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1823 return child_iterator();
1824}
1825
1826Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1827 return child_iterator();
1828}
1829
Ted Kremenek1237c672007-08-24 20:06:47 +00001830// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001831Stmt::child_iterator ObjCStringLiteral::child_begin() {
1832 return child_iterator();
1833}
1834Stmt::child_iterator ObjCStringLiteral::child_end() {
1835 return child_iterator();
1836}
Ted Kremenek1237c672007-08-24 20:06:47 +00001837
1838// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001839Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1840Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001841
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001842// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001843Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1844 return child_iterator();
1845}
1846Stmt::child_iterator ObjCSelectorExpr::child_end() {
1847 return child_iterator();
1848}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001849
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001850// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001851Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1852 return child_iterator();
1853}
1854Stmt::child_iterator ObjCProtocolExpr::child_end() {
1855 return child_iterator();
1856}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001857
Steve Naroff563477d2007-09-18 23:55:05 +00001858// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001859Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001860 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001861}
1862Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001863 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001864}
1865
Steve Naroff4eb206b2008-09-03 18:15:37 +00001866// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001867Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1868Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001869
Ted Kremenek9da13f92008-09-26 23:24:14 +00001870Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1871Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }