blob: 3133a8f42c25c3870a2269575408a5e5be4cf981 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000023#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Sebastian Redlf80e2612009-05-16 18:50:46 +000030PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
31 return new (C) PredefinedExpr(Loc, getType(), Type);
32}
33
Anders Carlsson7f2e7442009-03-15 18:34:13 +000034IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
35 return new (C) IntegerLiteral(Value, getType(), Loc);
36}
37
Sebastian Redlf80e2612009-05-16 18:50:46 +000038CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
39 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
40}
41
42FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
43 bool exact = IsExact;
44 return new (C) FloatingLiteral(Value, &exact, getType(), Loc);
45}
46
47GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
48 return new (C) GNUNullExpr(getType(), TokenLoc);
49}
50
Chris Lattnere0391b22008-06-07 22:13:43 +000051/// getValueAsApproximateDouble - This returns the value as an inaccurate
52/// double. Note that this may cause loss of precision, but is useful for
53/// debugging dumps, etc.
54double FloatingLiteral::getValueAsApproximateDouble() const {
55 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000056 bool ignored;
57 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
58 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000059 return V.convertToDouble();
60}
61
Chris Lattneraa491192009-02-18 06:40:38 +000062StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
63 unsigned ByteLength, bool Wide,
64 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000065 const SourceLocation *Loc,
66 unsigned NumStrs) {
Chris Lattneraa491192009-02-18 06:40:38 +000067 // Allocate enough space for the StringLiteral plus an array of locations for
68 // any concatenated string tokens.
69 void *Mem = C.Allocate(sizeof(StringLiteral)+
70 sizeof(SourceLocation)*(NumStrs-1),
71 llvm::alignof<StringLiteral>());
72 StringLiteral *SL = new (Mem) StringLiteral(Ty);
73
Chris Lattner4b009652007-07-25 00:24:17 +000074 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000075 char *AStrData = new (C, 1) char[ByteLength];
76 memcpy(AStrData, StrData, ByteLength);
77 SL->StrData = AStrData;
78 SL->ByteLength = ByteLength;
79 SL->IsWide = Wide;
80 SL->TokLocs[0] = Loc[0];
81 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Chris Lattnerc3144742009-02-18 05:49:11 +000083 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000084 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
85 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000086}
87
Douglas Gregor596e0932009-04-15 16:35:07 +000088StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
89 void *Mem = C.Allocate(sizeof(StringLiteral)+
90 sizeof(SourceLocation)*(NumStrs-1),
91 llvm::alignof<StringLiteral>());
92 StringLiteral *SL = new (Mem) StringLiteral(QualType());
93 SL->StrData = 0;
94 SL->ByteLength = 0;
95 SL->NumConcatenated = NumStrs;
96 return SL;
97}
98
Anders Carlsson7f2e7442009-03-15 18:34:13 +000099StringLiteral* StringLiteral::Clone(ASTContext &C) const {
100 return Create(C, StrData, ByteLength, IsWide, getType(),
101 TokLocs, NumConcatenated);
102}
Chris Lattnerc3144742009-02-18 05:49:11 +0000103
Ted Kremenek4f530a92009-02-06 19:55:15 +0000104void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000105 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +0000106 this->~StringLiteral();
107 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000108}
109
Douglas Gregor596e0932009-04-15 16:35:07 +0000110void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
111 if (StrData)
112 C.Deallocate(const_cast<char*>(StrData));
113
114 char *AStrData = new (C, 1) char[Len];
115 memcpy(AStrData, Str, Len);
116 StrData = AStrData;
117 ByteLength = Len;
118}
119
Chris Lattner4b009652007-07-25 00:24:17 +0000120/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
121/// corresponds to, e.g. "sizeof" or "[pre]++".
122const char *UnaryOperator::getOpcodeStr(Opcode Op) {
123 switch (Op) {
124 default: assert(0 && "Unknown unary operator");
125 case PostInc: return "++";
126 case PostDec: return "--";
127 case PreInc: return "++";
128 case PreDec: return "--";
129 case AddrOf: return "&";
130 case Deref: return "*";
131 case Plus: return "+";
132 case Minus: return "-";
133 case Not: return "~";
134 case LNot: return "!";
135 case Real: return "__real";
136 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000137 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000138 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
140}
141
Douglas Gregorc78182d2009-03-13 23:49:33 +0000142UnaryOperator::Opcode
143UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
144 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000145 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000146 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
147 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
148 case OO_Amp: return AddrOf;
149 case OO_Star: return Deref;
150 case OO_Plus: return Plus;
151 case OO_Minus: return Minus;
152 case OO_Tilde: return Not;
153 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000154 }
155}
156
157OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
158 switch (Opc) {
159 case PostInc: case PreInc: return OO_PlusPlus;
160 case PostDec: case PreDec: return OO_MinusMinus;
161 case AddrOf: return OO_Amp;
162 case Deref: return OO_Star;
163 case Plus: return OO_Plus;
164 case Minus: return OO_Minus;
165 case Not: return OO_Tilde;
166 case LNot: return OO_Exclaim;
167 default: return OO_None;
168 }
169}
170
171
Chris Lattner4b009652007-07-25 00:24:17 +0000172//===----------------------------------------------------------------------===//
173// Postfix Operators.
174//===----------------------------------------------------------------------===//
175
Ted Kremenek362abcd2009-02-09 20:51:47 +0000176CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000177 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000178 : Expr(SC, t,
179 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000180 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000181 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000182
183 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000184 SubExprs[FN] = fn;
185 for (unsigned i = 0; i != numargs; ++i)
186 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000187
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000188 RParenLoc = rparenloc;
189}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000190
Ted Kremenek362abcd2009-02-09 20:51:47 +0000191CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
192 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000193 : Expr(CallExprClass, t,
194 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000195 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000196 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000197
198 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000199 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000200 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000201 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000202
Chris Lattner4b009652007-07-25 00:24:17 +0000203 RParenLoc = rparenloc;
204}
205
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000206CallExpr::CallExpr(ASTContext &C, EmptyShell Empty)
207 : Expr(CallExprClass, Empty), SubExprs(0), NumArgs(0) {
208 SubExprs = new (C) Stmt*[1];
209}
210
Ted Kremenek362abcd2009-02-09 20:51:47 +0000211void CallExpr::Destroy(ASTContext& C) {
212 DestroyChildren(C);
213 if (SubExprs) C.Deallocate(SubExprs);
214 this->~CallExpr();
215 C.Deallocate(this);
216}
217
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000218/// setNumArgs - This changes the number of arguments present in this call.
219/// Any orphaned expressions are deleted by this, and any new operands are set
220/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000221void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000222 // No change, just return.
223 if (NumArgs == getNumArgs()) return;
224
225 // If shrinking # arguments, just delete the extras and forgot them.
226 if (NumArgs < getNumArgs()) {
227 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000228 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000229 this->NumArgs = NumArgs;
230 return;
231 }
232
233 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000234 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000235 // Copy over args.
236 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
237 NewSubExprs[i] = SubExprs[i];
238 // Null out new args.
239 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
240 NewSubExprs[i] = 0;
241
Douglas Gregorb9f63642009-04-17 21:46:47 +0000242 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000243 SubExprs = NewSubExprs;
244 this->NumArgs = NumArgs;
245}
246
Chris Lattnerc24915f2008-10-06 05:00:53 +0000247/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
248/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000249unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000250 // All simple function calls (e.g. func()) are implicitly cast to pointer to
251 // function. As a result, we try and obtain the DeclRefExpr from the
252 // ImplicitCastExpr.
253 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
254 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000255 return 0;
256
Steve Naroff44aec4c2008-01-31 01:07:12 +0000257 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
258 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000259 return 0;
260
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000261 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
262 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000263 return 0;
264
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000265 if (!FDecl->getIdentifier())
266 return 0;
267
Douglas Gregorb5af7382009-02-14 18:57:46 +0000268 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000269}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000270
Chris Lattnerc24915f2008-10-06 05:00:53 +0000271
Chris Lattner4b009652007-07-25 00:24:17 +0000272/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
273/// corresponds to, e.g. "<<=".
274const char *BinaryOperator::getOpcodeStr(Opcode Op) {
275 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000276 case PtrMemD: return ".*";
277 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000278 case Mul: return "*";
279 case Div: return "/";
280 case Rem: return "%";
281 case Add: return "+";
282 case Sub: return "-";
283 case Shl: return "<<";
284 case Shr: return ">>";
285 case LT: return "<";
286 case GT: return ">";
287 case LE: return "<=";
288 case GE: return ">=";
289 case EQ: return "==";
290 case NE: return "!=";
291 case And: return "&";
292 case Xor: return "^";
293 case Or: return "|";
294 case LAnd: return "&&";
295 case LOr: return "||";
296 case Assign: return "=";
297 case MulAssign: return "*=";
298 case DivAssign: return "/=";
299 case RemAssign: return "%=";
300 case AddAssign: return "+=";
301 case SubAssign: return "-=";
302 case ShlAssign: return "<<=";
303 case ShrAssign: return ">>=";
304 case AndAssign: return "&=";
305 case XorAssign: return "^=";
306 case OrAssign: return "|=";
307 case Comma: return ",";
308 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000309
310 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000311}
312
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000313BinaryOperator::Opcode
314BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
315 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000316 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000317 case OO_Plus: return Add;
318 case OO_Minus: return Sub;
319 case OO_Star: return Mul;
320 case OO_Slash: return Div;
321 case OO_Percent: return Rem;
322 case OO_Caret: return Xor;
323 case OO_Amp: return And;
324 case OO_Pipe: return Or;
325 case OO_Equal: return Assign;
326 case OO_Less: return LT;
327 case OO_Greater: return GT;
328 case OO_PlusEqual: return AddAssign;
329 case OO_MinusEqual: return SubAssign;
330 case OO_StarEqual: return MulAssign;
331 case OO_SlashEqual: return DivAssign;
332 case OO_PercentEqual: return RemAssign;
333 case OO_CaretEqual: return XorAssign;
334 case OO_AmpEqual: return AndAssign;
335 case OO_PipeEqual: return OrAssign;
336 case OO_LessLess: return Shl;
337 case OO_GreaterGreater: return Shr;
338 case OO_LessLessEqual: return ShlAssign;
339 case OO_GreaterGreaterEqual: return ShrAssign;
340 case OO_EqualEqual: return EQ;
341 case OO_ExclaimEqual: return NE;
342 case OO_LessEqual: return LE;
343 case OO_GreaterEqual: return GE;
344 case OO_AmpAmp: return LAnd;
345 case OO_PipePipe: return LOr;
346 case OO_Comma: return Comma;
347 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000348 }
349}
350
351OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
352 static const OverloadedOperatorKind OverOps[] = {
353 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
354 OO_Star, OO_Slash, OO_Percent,
355 OO_Plus, OO_Minus,
356 OO_LessLess, OO_GreaterGreater,
357 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
358 OO_EqualEqual, OO_ExclaimEqual,
359 OO_Amp,
360 OO_Caret,
361 OO_Pipe,
362 OO_AmpAmp,
363 OO_PipePipe,
364 OO_Equal, OO_StarEqual,
365 OO_SlashEqual, OO_PercentEqual,
366 OO_PlusEqual, OO_MinusEqual,
367 OO_LessLessEqual, OO_GreaterGreaterEqual,
368 OO_AmpEqual, OO_CaretEqual,
369 OO_PipeEqual,
370 OO_Comma
371 };
372 return OverOps[Opc];
373}
374
Anders Carlsson762b7c72007-08-31 04:56:16 +0000375InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000376 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000377 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000378 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000379 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000380 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000381
382 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000383}
Chris Lattner4b009652007-07-25 00:24:17 +0000384
Douglas Gregoree0792c2009-03-20 23:58:33 +0000385void InitListExpr::reserveInits(unsigned NumInits) {
386 if (NumInits > InitExprs.size())
387 InitExprs.reserve(NumInits);
388}
389
Douglas Gregorf603b472009-01-28 21:54:33 +0000390void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000391 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000392 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000393 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000394 InitExprs.resize(NumInits, 0);
395}
396
397Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
398 if (Init >= InitExprs.size()) {
399 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
400 InitExprs.back() = expr;
401 return 0;
402 }
403
404 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
405 InitExprs[Init] = expr;
406 return Result;
407}
408
Steve Naroff6f373332008-09-04 15:31:07 +0000409/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000410///
411const FunctionType *BlockExpr::getFunctionType() const {
412 return getType()->getAsBlockPointerType()->
413 getPointeeType()->getAsFunctionType();
414}
415
Steve Naroff9ac456d2008-10-08 17:01:13 +0000416SourceLocation BlockExpr::getCaretLocation() const {
417 return TheBlock->getCaretLocation();
418}
Douglas Gregore3241e92009-04-18 00:02:19 +0000419const Stmt *BlockExpr::getBody() const {
420 return TheBlock->getBody();
421}
422Stmt *BlockExpr::getBody() {
423 return TheBlock->getBody();
424}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000425
426
Chris Lattner4b009652007-07-25 00:24:17 +0000427//===----------------------------------------------------------------------===//
428// Generic Expression Routines
429//===----------------------------------------------------------------------===//
430
Chris Lattnerd2c66552009-02-14 07:37:35 +0000431/// isUnusedResultAWarning - Return true if this immediate expression should
432/// be warned about if the result is unused. If so, fill in Loc and Ranges
433/// with location to warn on and the source range[s] to report with the
434/// warning.
435bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
436 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000437 // Don't warn if the expr is type dependent. The type could end up
438 // instantiating to void.
439 if (isTypeDependent())
440 return false;
441
Chris Lattner4b009652007-07-25 00:24:17 +0000442 switch (getStmtClass()) {
443 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000444 Loc = getExprLoc();
445 R1 = getSourceRange();
446 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000447 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000448 return cast<ParenExpr>(this)->getSubExpr()->
449 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000450 case UnaryOperatorClass: {
451 const UnaryOperator *UO = cast<UnaryOperator>(this);
452
453 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000454 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000455 case UnaryOperator::PostInc:
456 case UnaryOperator::PostDec:
457 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000458 case UnaryOperator::PreDec: // ++/--
459 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000460 case UnaryOperator::Deref:
461 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000462 if (getType().isVolatileQualified())
463 return false;
464 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000465 case UnaryOperator::Real:
466 case UnaryOperator::Imag:
467 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000468 if (UO->getSubExpr()->getType().isVolatileQualified())
469 return false;
470 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000471 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000472 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000473 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000474 Loc = UO->getOperatorLoc();
475 R1 = UO->getSubExpr()->getSourceRange();
476 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000478 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000479 const BinaryOperator *BO = cast<BinaryOperator>(this);
480 // Consider comma to have side effects if the LHS or RHS does.
481 if (BO->getOpcode() == BinaryOperator::Comma)
482 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
483 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000484
Chris Lattnerd2c66552009-02-14 07:37:35 +0000485 if (BO->isAssignmentOp())
486 return false;
487 Loc = BO->getOperatorLoc();
488 R1 = BO->getLHS()->getSourceRange();
489 R2 = BO->getRHS()->getSourceRange();
490 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000491 }
Chris Lattner06078d22007-08-25 02:00:02 +0000492 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000493 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000494
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000495 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000496 // The condition must be evaluated, but if either the LHS or RHS is a
497 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000498 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000499 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000500 return true;
501 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000502 }
503
Chris Lattner4b009652007-07-25 00:24:17 +0000504 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000505 // If the base pointer or element is to a volatile pointer/field, accessing
506 // it is a side effect.
507 if (getType().isVolatileQualified())
508 return false;
509 Loc = cast<MemberExpr>(this)->getMemberLoc();
510 R1 = SourceRange(Loc, Loc);
511 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
512 return true;
513
Chris Lattner4b009652007-07-25 00:24:17 +0000514 case ArraySubscriptExprClass:
515 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000516 // it is a side effect.
517 if (getType().isVolatileQualified())
518 return false;
519 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
520 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
521 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
522 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000523
Chris Lattner4b009652007-07-25 00:24:17 +0000524 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000525 case CXXOperatorCallExprClass:
526 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000527 // If this is a direct call, get the callee.
528 const CallExpr *CE = cast<CallExpr>(this);
529 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
530 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
531 // If the callee has attribute pure, const, or warn_unused_result, warn
532 // about it. void foo() { strlen("bar"); } should warn.
533 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
534 if (FD->getAttr<WarnUnusedResultAttr>() ||
535 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
536 Loc = CE->getCallee()->getLocStart();
537 R1 = CE->getCallee()->getSourceRange();
538
539 if (unsigned NumArgs = CE->getNumArgs())
540 R2 = SourceRange(CE->getArg(0)->getLocStart(),
541 CE->getArg(NumArgs-1)->getLocEnd());
542 return true;
543 }
544 }
545 return false;
546 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000547 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000548 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000549 case StmtExprClass: {
550 // Statement exprs don't logically have side effects themselves, but are
551 // sometimes used in macros in ways that give them a type that is unused.
552 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
553 // however, if the result of the stmt expr is dead, we don't want to emit a
554 // warning.
555 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
556 if (!CS->body_empty())
557 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000558 return E->isUnusedResultAWarning(Loc, R1, R2);
559
560 Loc = cast<StmtExpr>(this)->getLParenLoc();
561 R1 = getSourceRange();
562 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000563 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000564 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000565 // If this is a cast to void, check the operand. Otherwise, the result of
566 // the cast is unused.
567 if (getType()->isVoidType())
568 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
569 R1, R2);
570 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
571 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
572 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000573 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000574 // If this is a cast to void, check the operand. Otherwise, the result of
575 // the cast is unused.
576 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000577 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
578 R1, R2);
579 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
580 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
581 return true;
582
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000583 case ImplicitCastExprClass:
584 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000585 return cast<ImplicitCastExpr>(this)
586 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000587
Chris Lattner3e254fb2008-04-08 04:40:51 +0000588 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000589 return cast<CXXDefaultArgExpr>(this)
590 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000591
592 case CXXNewExprClass:
593 // FIXME: In theory, there might be new expressions that don't have side
594 // effects (e.g. a placement new with an uninitialized POD).
595 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000596 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000597 }
Chris Lattner4b009652007-07-25 00:24:17 +0000598}
599
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000600/// DeclCanBeLvalue - Determine whether the given declaration can be
601/// an lvalue. This is a helper routine for isLvalue.
602static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000603 // C++ [temp.param]p6:
604 // A non-type non-reference template-parameter is not an lvalue.
605 if (const NonTypeTemplateParmDecl *NTTParm
606 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
607 return NTTParm->getType()->isReferenceType();
608
Douglas Gregor8acb7272008-12-11 16:49:14 +0000609 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000610 // C++ 3.10p2: An lvalue refers to an object or function.
611 (Ctx.getLangOptions().CPlusPlus &&
612 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
613}
614
Chris Lattner4b009652007-07-25 00:24:17 +0000615/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
616/// incomplete type other than void. Nonarray expressions that can be lvalues:
617/// - name, where name must be a variable
618/// - e[i]
619/// - (e), where e must be an lvalue
620/// - e.name, where e must be an lvalue
621/// - e->name
622/// - *e, the type of e cannot be a function type
623/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000624/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000625/// - reference type [C++ [expr]]
626///
Chris Lattner25168a52008-07-26 21:30:36 +0000627Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000628 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
629
630 isLvalueResult Res = isLvalueInternal(Ctx);
631 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
632 return Res;
633
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000634 // first, check the type (C99 6.3.2.1). Expressions with function
635 // type in C are not lvalues, but they can be lvalues in C++.
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000636 if (TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000637 return LV_NotObjectType;
638
Steve Naroffec7736d2008-02-10 01:39:04 +0000639 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000640 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000641 return LV_IncompleteVoidType;
642
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000643 return LV_Valid;
644}
Chris Lattner4b009652007-07-25 00:24:17 +0000645
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000646// Check whether the expression can be sanely treated like an l-value
647Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000648 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000649 case StringLiteralClass: // C99 6.5.1p4
650 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000651 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000652 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
653 // For vectors, make sure base is an lvalue (i.e. not a function call).
654 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000655 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000656 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000657 case DeclRefExprClass:
658 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000659 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
660 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000661 return LV_Valid;
662 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000663 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000664 case BlockDeclRefExprClass: {
665 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000666 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000667 return LV_Valid;
668 break;
669 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000670 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000671 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000672 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
673 NamedDecl *Member = m->getMemberDecl();
674 // C++ [expr.ref]p4:
675 // If E2 is declared to have type "reference to T", then E1.E2
676 // is an lvalue.
677 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
678 if (Value->getType()->isReferenceType())
679 return LV_Valid;
680
681 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000682 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000683 return LV_Valid;
684
685 // -- If E2 is a non-static data member [...]. If E1 is an
686 // lvalue, then E1.E2 is an lvalue.
687 if (isa<FieldDecl>(Member))
688 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
689
690 // -- If it refers to a static member function [...], then
691 // E1.E2 is an lvalue.
692 // -- Otherwise, if E1.E2 refers to a non-static member
693 // function [...], then E1.E2 is not an lvalue.
694 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
695 return Method->isStatic()? LV_Valid : LV_MemberFunction;
696
697 // -- If E2 is a member enumerator [...], the expression E1.E2
698 // is not an lvalue.
699 if (isa<EnumConstantDecl>(Member))
700 return LV_InvalidExpression;
701
702 // Not an lvalue.
703 return LV_InvalidExpression;
704 }
705
706 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000707 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000708 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000709 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000710 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000711 return LV_Valid; // C99 6.5.3p4
712
713 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000714 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
715 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000716 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000717
718 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
719 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
720 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
721 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000722 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000723 case ImplicitCastExprClass:
724 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
725 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000726 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000727 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000728 case BinaryOperatorClass:
729 case CompoundAssignOperatorClass: {
730 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000731
732 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
733 BinOp->getOpcode() == BinaryOperator::Comma)
734 return BinOp->getRHS()->isLvalue(Ctx);
735
Sebastian Redl95216a62009-02-07 00:15:38 +0000736 // C++ [expr.mptr.oper]p6
737 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
738 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
739 !BinOp->getType()->isFunctionType())
740 return BinOp->getLHS()->isLvalue(Ctx);
741
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000742 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000743 return LV_InvalidExpression;
744
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000745 if (Ctx.getLangOptions().CPlusPlus)
746 // C++ [expr.ass]p1:
747 // The result of an assignment operation [...] is an lvalue.
748 return LV_Valid;
749
750
751 // C99 6.5.16:
752 // An assignment expression [...] is not an lvalue.
753 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000754 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000755 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000756 case CXXOperatorCallExprClass:
757 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000758 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000759 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000760 // is an lvalue reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000761 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000762 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000763 CalleeType = FnTypePtr->getPointeeType();
764 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000765 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000766 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000767
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000768 break;
769 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000770 case CompoundLiteralExprClass: // C99 6.5.2.5p5
771 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000772 case ChooseExprClass:
773 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000774 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000775 case ExtVectorElementExprClass:
776 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000777 return LV_DuplicateVectorComponents;
778 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000779 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
780 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000781 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
782 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000783 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000784 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000785 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000786 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000787 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000788 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000789 case CXXConditionDeclExprClass:
790 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000791 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000792 case CXXFunctionalCastExprClass:
793 case CXXStaticCastExprClass:
794 case CXXDynamicCastExprClass:
795 case CXXReinterpretCastExprClass:
796 case CXXConstCastExprClass:
797 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000798 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000799 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
800 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000801 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
802 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000803 return LV_Valid;
804 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000805 case CXXTypeidExprClass:
806 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
807 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000808 case ConditionalOperatorClass: {
809 // Complicated handling is only for C++.
810 if (!Ctx.getLangOptions().CPlusPlus)
811 return LV_InvalidExpression;
812
813 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
814 // everywhere there's an object converted to an rvalue. Also, any other
815 // casts should be wrapped by ImplicitCastExprs. There's just the special
816 // case involving throws to work out.
817 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
818 Expr *LHS = Cond->getLHS();
819 Expr *RHS = Cond->getRHS();
820 // C++0x 5.16p2
821 // If either the second or the third operand has type (cv) void, [...]
822 // the result [...] is an rvalue.
823 if (LHS->getType()->isVoidType() || RHS->getType()->isVoidType())
824 return LV_InvalidExpression;
825
826 // Both sides must be lvalues for the result to be an lvalue.
827 if (LHS->isLvalue(Ctx) != LV_Valid || RHS->isLvalue(Ctx) != LV_Valid)
828 return LV_InvalidExpression;
829
830 // That's it.
831 return LV_Valid;
832 }
833
Chris Lattner4b009652007-07-25 00:24:17 +0000834 default:
835 break;
836 }
837 return LV_InvalidExpression;
838}
839
840/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
841/// does not have an incomplete type, does not have a const-qualified type, and
842/// if it is a structure or union, does not have any member (including,
843/// recursively, any member or element of all contained aggregates or unions)
844/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000845Expr::isModifiableLvalueResult
846Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000847 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000848
849 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000850 case LV_Valid:
851 // C++ 3.10p11: Functions cannot be modified, but pointers to
852 // functions can be modifiable.
853 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
854 return MLV_NotObjectType;
855 break;
856
Chris Lattner4b009652007-07-25 00:24:17 +0000857 case LV_NotObjectType: return MLV_NotObjectType;
858 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000859 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000860 case LV_InvalidExpression:
861 // If the top level is a C-style cast, and the subexpression is a valid
862 // lvalue, then this is probably a use of the old-school "cast as lvalue"
863 // GCC extension. We don't support it, but we want to produce good
864 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000865 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
866 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
867 if (Loc)
868 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000869 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000870 }
871 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000872 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000873 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000874 }
Eli Friedman91571da2009-03-22 23:26:56 +0000875
876 // The following is illegal:
877 // void takeclosure(void (^C)(void));
878 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
879 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000880 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000881 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
882 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
883 return MLV_NotBlockQualified;
884 }
885
Chris Lattnera1923f62008-08-04 07:31:14 +0000886 QualType CT = Ctx.getCanonicalType(getType());
887
888 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000889 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000890 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000891 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000892 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000893 return MLV_IncompleteType;
894
Chris Lattnera1923f62008-08-04 07:31:14 +0000895 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000896 if (r->hasConstFields())
897 return MLV_ConstQualified;
898 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000899
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000900 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000901 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000902 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
903 if (KVCExpr->getSetterMethod() == 0)
904 return MLV_NoSetterProperty;
905 }
Chris Lattner4b009652007-07-25 00:24:17 +0000906 return MLV_Valid;
907}
908
Ted Kremenek5778d622008-02-27 18:39:48 +0000909/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000910/// duration. This means that the address of this expression is a link-time
911/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000912bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000913 switch (getStmtClass()) {
914 default:
915 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000916 case BlockExprClass:
917 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000918 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000919 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000920 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000921 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000922 case CompoundLiteralExprClass:
923 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000924 case DeclRefExprClass:
925 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000926 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
927 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000928 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000929 if (isa<FunctionDecl>(D))
930 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000931 return false;
932 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000933 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000934 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000935 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000936 }
Chris Lattner743ec372007-11-27 21:35:27 +0000937 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000938 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000939 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000940 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000941 case CXXDefaultArgExprClass:
942 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000943 }
944}
945
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000946/// isOBJCGCCandidate - Check if an expression is objc gc'able.
947///
948bool Expr::isOBJCGCCandidate() const {
949 switch (getStmtClass()) {
950 default:
951 return false;
952 case ObjCIvarRefExprClass:
953 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000954 case Expr::UnaryOperatorClass:
955 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000956 case ParenExprClass:
957 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
958 case ImplicitCastExprClass:
959 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000960 case CStyleCastExprClass:
961 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000962 case DeclRefExprClass:
963 case QualifiedDeclRefExprClass: {
964 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
965 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
966 return VD->hasGlobalStorage();
967 return false;
968 }
969 case MemberExprClass: {
970 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000971 return M->getBase()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000972 }
973 case ArraySubscriptExprClass:
974 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
975 }
976}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000977Expr* Expr::IgnoreParens() {
978 Expr* E = this;
979 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
980 E = P->getSubExpr();
981
982 return E;
983}
984
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000985/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
986/// or CastExprs or ImplicitCastExprs, returning their operand.
987Expr *Expr::IgnoreParenCasts() {
988 Expr *E = this;
989 while (true) {
990 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
991 E = P->getSubExpr();
992 else if (CastExpr *P = dyn_cast<CastExpr>(E))
993 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000994 else
995 return E;
996 }
997}
998
Chris Lattnerab0b8b12009-03-13 17:28:01 +0000999/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1000/// value (including ptr->int casts of the same size). Strip off any
1001/// ParenExpr or CastExprs, returning their operand.
1002Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1003 Expr *E = this;
1004 while (true) {
1005 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1006 E = P->getSubExpr();
1007 continue;
1008 }
1009
1010 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1011 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1012 // ptr<->int casts of the same width. We also ignore all identify casts.
1013 Expr *SE = P->getSubExpr();
1014
1015 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1016 E = SE;
1017 continue;
1018 }
1019
1020 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1021 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1022 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1023 E = SE;
1024 continue;
1025 }
1026 }
1027
1028 return E;
1029 }
1030}
1031
1032
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001033/// hasAnyTypeDependentArguments - Determines if any of the expressions
1034/// in Exprs is type-dependent.
1035bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1036 for (unsigned I = 0; I < NumExprs; ++I)
1037 if (Exprs[I]->isTypeDependent())
1038 return true;
1039
1040 return false;
1041}
1042
1043/// hasAnyValueDependentArguments - Determines if any of the expressions
1044/// in Exprs is value-dependent.
1045bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1046 for (unsigned I = 0; I < NumExprs; ++I)
1047 if (Exprs[I]->isValueDependent())
1048 return true;
1049
1050 return false;
1051}
1052
Eli Friedmandee41122009-01-25 02:32:41 +00001053bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001054 // This function is attempting whether an expression is an initializer
1055 // which can be evaluated at compile-time. isEvaluatable handles most
1056 // of the cases, but it can't deal with some initializer-specific
1057 // expressions, and it can't deal with aggregates; we deal with those here,
1058 // and fall back to isEvaluatable for the other cases.
1059
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001060 // FIXME: This function assumes the variable being assigned to
1061 // isn't a reference type!
1062
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001063 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001064 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001065 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001066 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001067 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001068 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001069 // This handles gcc's extension that allows global initializers like
1070 // "struct x {int x;} x = (struct x) {};".
1071 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001072 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001073 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001074 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001075 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001076 // FIXME: This doesn't deal with fields with reference types correctly.
1077 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1078 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001079 const InitListExpr *Exp = cast<InitListExpr>(this);
1080 unsigned numInits = Exp->getNumInits();
1081 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001082 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001083 return false;
1084 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001085 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001086 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001087 case ImplicitValueInitExprClass:
1088 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001089 case ParenExprClass: {
1090 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1091 }
1092 case UnaryOperatorClass: {
1093 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1094 if (Exp->getOpcode() == UnaryOperator::Extension)
1095 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1096 break;
1097 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001098 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001099 case CStyleCastExprClass:
1100 // Handle casts with a destination that's a struct or union; this
1101 // deals with both the gcc no-op struct cast extension and the
1102 // cast-to-union extension.
1103 if (getType()->isRecordType())
1104 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1105 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001106 }
1107
Eli Friedman2b0dec52009-01-25 03:12:18 +00001108 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001109}
1110
Chris Lattner4b009652007-07-25 00:24:17 +00001111/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001112/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1115/// comma, etc
1116///
Chris Lattner4b009652007-07-25 00:24:17 +00001117/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1118/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1119/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001120
Eli Friedman7beeda62009-02-26 09:29:13 +00001121// CheckICE - This function does the fundamental ICE checking: the returned
1122// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1123// Note that to reduce code duplication, this helper does no evaluation
1124// itself; the caller checks whether the expression is evaluatable, and
1125// in the rare cases where CheckICE actually cares about the evaluated
1126// value, it calls into Evalute.
1127//
1128// Meanings of Val:
1129// 0: This expression is an ICE if it can be evaluated by Evaluate.
1130// 1: This expression is not an ICE, but if it isn't evaluated, it's
1131// a legal subexpression for an ICE. This return value is used to handle
1132// the comma operator in C99 mode.
1133// 2: This expression is not an ICE, and is not a legal subexpression for one.
1134
1135struct ICEDiag {
1136 unsigned Val;
1137 SourceLocation Loc;
1138
1139 public:
1140 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1141 ICEDiag() : Val(0) {}
1142};
1143
1144ICEDiag NoDiag() { return ICEDiag(); }
1145
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001146static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1147 Expr::EvalResult EVResult;
1148 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1149 !EVResult.Val.isInt()) {
1150 return ICEDiag(2, E->getLocStart());
1151 }
1152 return NoDiag();
1153}
1154
Eli Friedman7beeda62009-02-26 09:29:13 +00001155static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001156 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001157 if (!E->getType()->isIntegralType()) {
1158 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001159 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001160
1161 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001162 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001163 return ICEDiag(2, E->getLocStart());
1164 case Expr::ParenExprClass:
1165 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1166 case Expr::IntegerLiteralClass:
1167 case Expr::CharacterLiteralClass:
1168 case Expr::CXXBoolLiteralExprClass:
1169 case Expr::CXXZeroInitValueExprClass:
1170 case Expr::TypesCompatibleExprClass:
1171 case Expr::UnaryTypeTraitExprClass:
1172 return NoDiag();
1173 case Expr::CallExprClass:
1174 case Expr::CXXOperatorCallExprClass: {
1175 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001176 if (CE->isBuiltinCall(Ctx))
1177 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001178 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001179 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001180 case Expr::DeclRefExprClass:
1181 case Expr::QualifiedDeclRefExprClass:
1182 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1183 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001184 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001185 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001186 // C++ 7.1.5.1p2
1187 // A variable of non-volatile const-qualified integral or enumeration
1188 // type initialized by an ICE can be used in ICEs.
1189 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001190 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001191 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001192 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001193 }
1194 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001195 return ICEDiag(2, E->getLocStart());
1196 case Expr::UnaryOperatorClass: {
1197 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001198 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001199 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001200 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001201 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001202 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001203 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001204 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001205 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001206 case UnaryOperator::Real:
1207 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001208 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001209 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001210 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1211 // Evaluate matches the proposed gcc behavior for cases like
1212 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1213 // compliance: we should warn earlier for offsetof expressions with
1214 // array subscripts that aren't ICEs, and if the array subscripts
1215 // are ICEs, the value of the offsetof must be an integer constant.
1216 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001217 }
Chris Lattner4b009652007-07-25 00:24:17 +00001218 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001219 case Expr::SizeOfAlignOfExprClass: {
1220 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1221 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1222 return ICEDiag(2, E->getLocStart());
1223 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001224 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001225 case Expr::BinaryOperatorClass: {
1226 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001227 switch (Exp->getOpcode()) {
1228 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001229 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001230 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001231 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001232 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001233 case BinaryOperator::Add:
1234 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001235 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001236 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001237 case BinaryOperator::LT:
1238 case BinaryOperator::GT:
1239 case BinaryOperator::LE:
1240 case BinaryOperator::GE:
1241 case BinaryOperator::EQ:
1242 case BinaryOperator::NE:
1243 case BinaryOperator::And:
1244 case BinaryOperator::Xor:
1245 case BinaryOperator::Or:
1246 case BinaryOperator::Comma: {
1247 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1248 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001249 if (Exp->getOpcode() == BinaryOperator::Div ||
1250 Exp->getOpcode() == BinaryOperator::Rem) {
1251 // Evaluate gives an error for undefined Div/Rem, so make sure
1252 // we don't evaluate one.
1253 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1254 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1255 if (REval == 0)
1256 return ICEDiag(1, E->getLocStart());
1257 if (REval.isSigned() && REval.isAllOnesValue()) {
1258 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1259 if (LEval.isMinSignedValue())
1260 return ICEDiag(1, E->getLocStart());
1261 }
1262 }
1263 }
1264 if (Exp->getOpcode() == BinaryOperator::Comma) {
1265 if (Ctx.getLangOptions().C99) {
1266 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1267 // if it isn't evaluated.
1268 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1269 return ICEDiag(1, E->getLocStart());
1270 } else {
1271 // In both C89 and C++, commas in ICEs are illegal.
1272 return ICEDiag(2, E->getLocStart());
1273 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001274 }
1275 if (LHSResult.Val >= RHSResult.Val)
1276 return LHSResult;
1277 return RHSResult;
1278 }
Chris Lattner4b009652007-07-25 00:24:17 +00001279 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001280 case BinaryOperator::LOr: {
1281 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1282 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1283 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1284 // Rare case where the RHS has a comma "side-effect"; we need
1285 // to actually check the condition to see whether the side
1286 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001287 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001288 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001289 return RHSResult;
1290 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001291 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001292
Eli Friedman7beeda62009-02-26 09:29:13 +00001293 if (LHSResult.Val >= RHSResult.Val)
1294 return LHSResult;
1295 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001296 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001297 }
Chris Lattner4b009652007-07-25 00:24:17 +00001298 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001299 case Expr::ImplicitCastExprClass:
1300 case Expr::CStyleCastExprClass:
1301 case Expr::CXXFunctionalCastExprClass: {
1302 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1303 if (SubExpr->getType()->isIntegralType())
1304 return CheckICE(SubExpr, Ctx);
1305 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1306 return NoDiag();
1307 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001308 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001309 case Expr::ConditionalOperatorClass: {
1310 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001311 // If the condition (ignoring parens) is a __builtin_constant_p call,
1312 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001313 // expression, and it is fully evaluated. This is an important GNU
1314 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001315 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001316 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001317 Expr::EvalResult EVResult;
1318 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1319 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001320 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001321 }
1322 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001323 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001324 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1325 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1326 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1327 if (CondResult.Val == 2)
1328 return CondResult;
1329 if (TrueResult.Val == 2)
1330 return TrueResult;
1331 if (FalseResult.Val == 2)
1332 return FalseResult;
1333 if (CondResult.Val == 1)
1334 return CondResult;
1335 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1336 return NoDiag();
1337 // Rare case where the diagnostics depend on which side is evaluated
1338 // Note that if we get here, CondResult is 0, and at least one of
1339 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001340 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001341 return FalseResult;
1342 }
1343 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001344 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001345 case Expr::CXXDefaultArgExprClass:
1346 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001347 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001348 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001349 }
Chris Lattner4b009652007-07-25 00:24:17 +00001350 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001351}
Chris Lattner4b009652007-07-25 00:24:17 +00001352
Eli Friedman7beeda62009-02-26 09:29:13 +00001353bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1354 SourceLocation *Loc, bool isEvaluated) const {
1355 ICEDiag d = CheckICE(this, Ctx);
1356 if (d.Val != 0) {
1357 if (Loc) *Loc = d.Loc;
1358 return false;
1359 }
1360 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001361 if (!Evaluate(EvalResult, Ctx))
1362 assert(0 && "ICE cannot be evaluated!");
1363 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1364 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001365 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001366 return true;
1367}
1368
Chris Lattner4b009652007-07-25 00:24:17 +00001369/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1370/// integer constant expression with the value zero, or if this is one that is
1371/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001372bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1373{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001374 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001375 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001376 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001377 // Check that it is a cast to void*.
1378 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1379 QualType Pointee = PT->getPointeeType();
1380 if (Pointee.getCVRQualifiers() == 0 &&
1381 Pointee->isVoidType() && // to void*
1382 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001383 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001384 }
Chris Lattner4b009652007-07-25 00:24:17 +00001385 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001386 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1387 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001388 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001389 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1390 // Accept ((void*)0) as a null pointer constant, as many other
1391 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001392 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001393 } else if (const CXXDefaultArgExpr *DefaultArg
1394 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001395 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001396 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001397 } else if (isa<GNUNullExpr>(this)) {
1398 // The GNU __null extension is always a null pointer constant.
1399 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001400 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001401
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001402 // C++0x nullptr_t is always a null pointer constant.
1403 if (getType()->isNullPtrType())
1404 return true;
1405
Steve Naroffa2e53222008-01-14 16:10:57 +00001406 // This expression must be an integer type.
1407 if (!getType()->isIntegerType())
1408 return false;
1409
Chris Lattner4b009652007-07-25 00:24:17 +00001410 // If we have an integer constant expression, we need to *evaluate* it and
1411 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001412 llvm::APSInt Result;
1413 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001414}
Steve Naroffc11705f2007-07-28 23:10:27 +00001415
Douglas Gregor531434b2009-05-02 02:18:30 +00001416FieldDecl *Expr::getBitField() {
Douglas Gregor81c29152008-10-29 00:13:59 +00001417 Expr *E = this->IgnoreParenCasts();
Douglas Gregor531434b2009-05-02 02:18:30 +00001418
Douglas Gregor81c29152008-10-29 00:13:59 +00001419 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001420 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001421 if (Field->isBitField())
1422 return Field;
1423
1424 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1425 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1426 return BinOp->getLHS()->getBitField();
1427
1428 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001429}
1430
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001431/// isArrow - Return true if the base expression is a pointer to vector,
1432/// return false if the base expression is a vector.
1433bool ExtVectorElementExpr::isArrow() const {
1434 return getBase()->getType()->isPointerType();
1435}
1436
Nate Begemanaf6ed502008-04-18 23:10:10 +00001437unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001438 if (const VectorType *VT = getType()->getAsVectorType())
1439 return VT->getNumElements();
1440 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001441}
1442
Nate Begemanc8e51f82008-05-09 06:41:27 +00001443/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001444bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001445 const char *compStr = Accessor->getName();
1446 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001447
1448 // Halving swizzles do not contain duplicate elements.
1449 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1450 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1451 return false;
1452
1453 // Advance past s-char prefix on hex swizzles.
1454 if (*compStr == 's') {
1455 compStr++;
1456 length--;
1457 }
Steve Naroffba67f692007-07-30 03:29:09 +00001458
Chris Lattner58d3fa52008-11-19 07:55:04 +00001459 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001460 const char *s = compStr+i;
1461 for (const char c = *s++; *s; s++)
1462 if (c == *s)
1463 return true;
1464 }
1465 return false;
1466}
Chris Lattner42158e72007-08-02 23:36:59 +00001467
Nate Begemanc8e51f82008-05-09 06:41:27 +00001468/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001469void ExtVectorElementExpr::getEncodedElementAccess(
1470 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001471 const char *compStr = Accessor->getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001472 if (*compStr == 's')
1473 compStr++;
1474
1475 bool isHi = !strcmp(compStr, "hi");
1476 bool isLo = !strcmp(compStr, "lo");
1477 bool isEven = !strcmp(compStr, "even");
1478 bool isOdd = !strcmp(compStr, "odd");
1479
Nate Begemanc8e51f82008-05-09 06:41:27 +00001480 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1481 uint64_t Index;
1482
1483 if (isHi)
1484 Index = e + i;
1485 else if (isLo)
1486 Index = i;
1487 else if (isEven)
1488 Index = 2 * i;
1489 else if (isOdd)
1490 Index = 2 * i + 1;
1491 else
1492 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001493
Nate Begemana1ae7442008-05-13 21:03:02 +00001494 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001495 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001496}
1497
Steve Naroff4ed9d662007-09-27 14:38:14 +00001498// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001499ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001500 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001501 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001502 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001503 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001504 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001505 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001506 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001507 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001508 if (NumArgs) {
1509 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001510 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1511 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001512 LBracloc = LBrac;
1513 RBracloc = RBrac;
1514}
1515
Steve Naroff4ed9d662007-09-27 14:38:14 +00001516// constructor for class messages.
1517// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001518ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001519 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001520 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001521 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001522 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001523 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001524 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001525 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001526 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001527 if (NumArgs) {
1528 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001529 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1530 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001531 LBracloc = LBrac;
1532 RBracloc = RBrac;
1533}
1534
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001535// constructor for class messages.
1536ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1537 QualType retType, ObjCMethodDecl *mproto,
1538 SourceLocation LBrac, SourceLocation RBrac,
1539 Expr **ArgExprs, unsigned nargs)
1540: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1541MethodProto(mproto) {
1542 NumArgs = nargs;
1543 SubExprs = new Stmt*[NumArgs+1];
1544 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1545 if (NumArgs) {
1546 for (unsigned i = 0; i != NumArgs; ++i)
1547 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1548 }
1549 LBracloc = LBrac;
1550 RBracloc = RBrac;
1551}
1552
1553ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1554 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1555 switch (x & Flags) {
1556 default:
1557 assert(false && "Invalid ObjCMessageExpr.");
1558 case IsInstMeth:
1559 return ClassInfo(0, 0);
1560 case IsClsMethDeclUnknown:
1561 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1562 case IsClsMethDeclKnown: {
1563 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1564 return ClassInfo(D, D->getIdentifier());
1565 }
1566 }
1567}
1568
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001569void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1570 if (CI.first == 0 && CI.second == 0)
1571 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1572 else if (CI.first == 0)
1573 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1574 else
1575 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1576}
1577
1578
Chris Lattnerf624cd22007-10-25 00:29:32 +00001579bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001580 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001581}
1582
Douglas Gregor725e94b2009-04-16 00:01:45 +00001583void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1584 if (NumExprs)
1585 delete [] SubExprs;
1586
1587 SubExprs = new Stmt* [NumExprs];
1588 this->NumExprs = NumExprs;
1589 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1590}
1591
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001592void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1593 // Override default behavior of traversing children. If this has a type
1594 // operand and the type is a variable-length array, the child iteration
1595 // will iterate over the size expression. However, this expression belongs
1596 // to the type, not to this, so we don't want to delete it.
1597 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001598 if (isArgumentType()) {
1599 this->~SizeOfAlignOfExpr();
1600 C.Deallocate(this);
1601 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001602 else
1603 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001604}
1605
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001606//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001607// DesignatedInitExpr
1608//===----------------------------------------------------------------------===//
1609
1610IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1611 assert(Kind == FieldDesignator && "Only valid on a field designator");
1612 if (Field.NameOrField & 0x01)
1613 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1614 else
1615 return getField()->getIdentifier();
1616}
1617
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001618DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1619 const Designator *Designators,
1620 SourceLocation EqualOrColonLoc,
1621 bool GNUSyntax,
1622 unsigned NumSubExprs)
1623 : Expr(DesignatedInitExprClass, Ty),
1624 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1625 NumDesignators(NumDesignators), NumSubExprs(NumSubExprs) {
1626 this->Designators = new Designator[NumDesignators];
1627 for (unsigned I = 0; I != NumDesignators; ++I)
1628 this->Designators[I] = Designators[I];
1629}
1630
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001631DesignatedInitExpr *
1632DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1633 unsigned NumDesignators,
1634 Expr **IndexExprs, unsigned NumIndexExprs,
1635 SourceLocation ColonOrEqualLoc,
1636 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001637 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001638 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001639 DesignatedInitExpr *DIE
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001640 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001641 ColonOrEqualLoc, UsesColonSyntax,
1642 NumIndexExprs + 1);
1643
1644 // Fill in the designators
1645 unsigned ExpectedNumSubExprs = 0;
1646 designators_iterator Desig = DIE->designators_begin();
1647 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001648 if (Designators[Idx].isArrayDesignator())
1649 ++ExpectedNumSubExprs;
1650 else if (Designators[Idx].isArrayRangeDesignator())
1651 ExpectedNumSubExprs += 2;
1652 }
1653 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1654
1655 // Fill in the subexpressions, including the initializer expression.
1656 child_iterator Child = DIE->child_begin();
1657 *Child++ = Init;
1658 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1659 *Child = IndexExprs[Idx];
1660
1661 return DIE;
1662}
1663
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001664DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1665 unsigned NumIndexExprs) {
1666 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1667 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1668 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1669}
1670
1671void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1672 unsigned NumDesigs) {
1673 if (Designators)
1674 delete [] Designators;
1675
1676 Designators = new Designator[NumDesigs];
1677 NumDesignators = NumDesigs;
1678 for (unsigned I = 0; I != NumDesigs; ++I)
1679 Designators[I] = Desigs[I];
1680}
1681
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001682SourceRange DesignatedInitExpr::getSourceRange() const {
1683 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001684 Designator &First =
1685 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001686 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001687 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001688 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1689 else
1690 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1691 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001692 StartLoc =
1693 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001694 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1695}
1696
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001697Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1698 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1699 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1700 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001701 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1702 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1703}
1704
1705Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1706 assert(D.Kind == Designator::ArrayRangeDesignator &&
1707 "Requires array range designator");
1708 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1709 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001710 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1711 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1712}
1713
1714Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1715 assert(D.Kind == Designator::ArrayRangeDesignator &&
1716 "Requires array range designator");
1717 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1718 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001719 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1720 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1721}
1722
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001723/// \brief Replaces the designator at index @p Idx with the series
1724/// of designators in [First, Last).
1725void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1726 const Designator *First,
1727 const Designator *Last) {
1728 unsigned NumNewDesignators = Last - First;
1729 if (NumNewDesignators == 0) {
1730 std::copy_backward(Designators + Idx + 1,
1731 Designators + NumDesignators,
1732 Designators + Idx);
1733 --NumNewDesignators;
1734 return;
1735 } else if (NumNewDesignators == 1) {
1736 Designators[Idx] = *First;
1737 return;
1738 }
1739
1740 Designator *NewDesignators
1741 = new Designator[NumDesignators - 1 + NumNewDesignators];
1742 std::copy(Designators, Designators + Idx, NewDesignators);
1743 std::copy(First, Last, NewDesignators + Idx);
1744 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1745 NewDesignators + Idx + NumNewDesignators);
1746 delete [] Designators;
1747 Designators = NewDesignators;
1748 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1749}
1750
1751void DesignatedInitExpr::Destroy(ASTContext &C) {
1752 delete [] Designators;
1753 Expr::Destroy(C);
1754}
1755
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001756//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001757// ExprIterator.
1758//===----------------------------------------------------------------------===//
1759
1760Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1761Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1762Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1763const Expr* ConstExprIterator::operator[](size_t idx) const {
1764 return cast<Expr>(I[idx]);
1765}
1766const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1767const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1768
1769//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001770// Child Iterators for iterating over subexpressions/substatements
1771//===----------------------------------------------------------------------===//
1772
1773// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001774Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1775Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001776
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001777// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001778Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1779Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001780
Steve Naroff6f786252008-06-02 23:03:37 +00001781// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001782Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1783Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001784
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001785// ObjCKVCRefExpr
1786Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1787Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1788
Douglas Gregord8606632008-11-04 14:56:14 +00001789// ObjCSuperExpr
1790Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1791Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1792
Chris Lattner69909292008-08-10 01:53:14 +00001793// PredefinedExpr
1794Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1795Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001796
1797// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001798Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1799Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001800
1801// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001802Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001803Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001804
1805// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001806Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1807Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001808
Chris Lattner1de66eb2007-08-26 03:42:43 +00001809// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001810Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1811Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001812
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001813// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001814Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1815Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001816
1817// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001818Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1819Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001820
1821// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001822Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1823Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001824
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001825// SizeOfAlignOfExpr
1826Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1827 // If this is of a type and the type is a VLA type (and not a typedef), the
1828 // size expression of the VLA needs to be treated as an executable expression.
1829 // Why isn't this weirdness documented better in StmtIterator?
1830 if (isArgumentType()) {
1831 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1832 getArgumentType().getTypePtr()))
1833 return child_iterator(T);
1834 return child_iterator();
1835 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001836 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001837}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001838Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1839 if (isArgumentType())
1840 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001841 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001842}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001843
1844// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001845Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001846 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001847}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001848Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001849 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001850}
1851
1852// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001853Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001854 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001855}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001856Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001857 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001858}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001859
1860// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001861Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1862Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001863
Nate Begemanaf6ed502008-04-18 23:10:10 +00001864// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001865Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1866Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001867
1868// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001869Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1870Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001871
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001872// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001873Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1874Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001875
1876// BinaryOperator
1877Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001878 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001879}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001880Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001881 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001882}
1883
1884// ConditionalOperator
1885Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001886 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001887}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001888Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001889 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001890}
1891
1892// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001893Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1894Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001895
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001896// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001897Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1898Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001899
1900// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001901Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1902 return child_iterator();
1903}
1904
1905Stmt::child_iterator TypesCompatibleExpr::child_end() {
1906 return child_iterator();
1907}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001908
1909// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001910Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1911Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001912
Douglas Gregorad4b3792008-11-29 04:51:27 +00001913// GNUNullExpr
1914Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1915Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1916
Eli Friedmand0e9d092008-05-14 19:38:39 +00001917// ShuffleVectorExpr
1918Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001919 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001920}
1921Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001922 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001923}
1924
Anders Carlsson36760332007-10-15 20:28:48 +00001925// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001926Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1927Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001928
Anders Carlsson762b7c72007-08-31 04:56:16 +00001929// InitListExpr
1930Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001931 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001932}
1933Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001934 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001935}
1936
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001937// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001938Stmt::child_iterator DesignatedInitExpr::child_begin() {
1939 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1940 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001941 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1942}
1943Stmt::child_iterator DesignatedInitExpr::child_end() {
1944 return child_iterator(&*child_begin() + NumSubExprs);
1945}
1946
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001947// ImplicitValueInitExpr
1948Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1949 return child_iterator();
1950}
1951
1952Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1953 return child_iterator();
1954}
1955
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001956// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001957Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001958 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001959}
1960Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001961 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001962}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001963
1964// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001965Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1966Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001967
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001968// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001969Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1970 return child_iterator();
1971}
1972Stmt::child_iterator ObjCSelectorExpr::child_end() {
1973 return child_iterator();
1974}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001975
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001976// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001977Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1978 return child_iterator();
1979}
1980Stmt::child_iterator ObjCProtocolExpr::child_end() {
1981 return child_iterator();
1982}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001983
Steve Naroffc39ca262007-09-18 23:55:05 +00001984// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001985Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001986 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001987}
1988Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001989 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001990}
1991
Steve Naroff52a81c02008-09-03 18:15:37 +00001992// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001993Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1994Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001995
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001996Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1997Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }