blob: aca5efeb16370077ecb44ae327310d07c8d0d92f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000023#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Sebastian Redl8b0b4752009-05-16 18:50:46 +000030PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
31 return new (C) PredefinedExpr(Loc, getType(), Type);
32}
33
Anders Carlssona135fb42009-03-15 18:34:13 +000034IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
35 return new (C) IntegerLiteral(Value, getType(), Loc);
36}
37
Sebastian Redl8b0b4752009-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
Douglas Gregord8ac4362009-05-18 22:38:38 +000047ImaginaryLiteral* ImaginaryLiteral::Clone(ASTContext &C) const {
48 // FIXME: Use virtual Clone(), once it is available
49 Expr *ClonedVal = 0;
50 if (const IntegerLiteral *IntLit = dyn_cast<IntegerLiteral>(Val))
51 ClonedVal = IntLit->Clone(C);
52 else
53 ClonedVal = cast<FloatingLiteral>(Val)->Clone(C);
54 return new (C) ImaginaryLiteral(ClonedVal, getType());
55}
56
Sebastian Redl8b0b4752009-05-16 18:50:46 +000057GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
58 return new (C) GNUNullExpr(getType(), TokenLoc);
59}
60
Chris Lattnerda8249e2008-06-07 22:13:43 +000061/// getValueAsApproximateDouble - This returns the value as an inaccurate
62/// double. Note that this may cause loss of precision, but is useful for
63/// debugging dumps, etc.
64double FloatingLiteral::getValueAsApproximateDouble() const {
65 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000066 bool ignored;
67 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
68 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000069 return V.convertToDouble();
70}
71
Chris Lattner2085fd62009-02-18 06:40:38 +000072StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
73 unsigned ByteLength, bool Wide,
74 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000075 const SourceLocation *Loc,
76 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000077 // Allocate enough space for the StringLiteral plus an array of locations for
78 // any concatenated string tokens.
79 void *Mem = C.Allocate(sizeof(StringLiteral)+
80 sizeof(SourceLocation)*(NumStrs-1),
81 llvm::alignof<StringLiteral>());
82 StringLiteral *SL = new (Mem) StringLiteral(Ty);
83
Reid Spencer5f016e22007-07-11 17:01:13 +000084 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000085 char *AStrData = new (C, 1) char[ByteLength];
86 memcpy(AStrData, StrData, ByteLength);
87 SL->StrData = AStrData;
88 SL->ByteLength = ByteLength;
89 SL->IsWide = Wide;
90 SL->TokLocs[0] = Loc[0];
91 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000092
Chris Lattner726e1682009-02-18 05:49:11 +000093 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000094 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
95 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000096}
97
Douglas Gregor673ecd62009-04-15 16:35:07 +000098StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
99 void *Mem = C.Allocate(sizeof(StringLiteral)+
100 sizeof(SourceLocation)*(NumStrs-1),
101 llvm::alignof<StringLiteral>());
102 StringLiteral *SL = new (Mem) StringLiteral(QualType());
103 SL->StrData = 0;
104 SL->ByteLength = 0;
105 SL->NumConcatenated = NumStrs;
106 return SL;
107}
108
Anders Carlssona135fb42009-03-15 18:34:13 +0000109StringLiteral* StringLiteral::Clone(ASTContext &C) const {
110 return Create(C, StrData, ByteLength, IsWide, getType(),
111 TokLocs, NumConcatenated);
112}
Chris Lattner726e1682009-02-18 05:49:11 +0000113
Ted Kremenek6e94ef52009-02-06 19:55:15 +0000114void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000115 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +0000116 this->~StringLiteral();
117 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
119
Douglas Gregor673ecd62009-04-15 16:35:07 +0000120void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
121 if (StrData)
122 C.Deallocate(const_cast<char*>(StrData));
123
124 char *AStrData = new (C, 1) char[Len];
125 memcpy(AStrData, Str, Len);
126 StrData = AStrData;
127 ByteLength = Len;
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
131/// corresponds to, e.g. "sizeof" or "[pre]++".
132const char *UnaryOperator::getOpcodeStr(Opcode Op) {
133 switch (Op) {
134 default: assert(0 && "Unknown unary operator");
135 case PostInc: return "++";
136 case PostDec: return "--";
137 case PreInc: return "++";
138 case PreDec: return "--";
139 case AddrOf: return "&";
140 case Deref: return "*";
141 case Plus: return "+";
142 case Minus: return "-";
143 case Not: return "~";
144 case LNot: return "!";
145 case Real: return "__real";
146 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000148 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150}
151
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000152UnaryOperator::Opcode
153UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
154 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000155 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000156 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
157 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
158 case OO_Amp: return AddrOf;
159 case OO_Star: return Deref;
160 case OO_Plus: return Plus;
161 case OO_Minus: return Minus;
162 case OO_Tilde: return Not;
163 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000164 }
165}
166
167OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
168 switch (Opc) {
169 case PostInc: case PreInc: return OO_PlusPlus;
170 case PostDec: case PreDec: return OO_MinusMinus;
171 case AddrOf: return OO_Amp;
172 case Deref: return OO_Star;
173 case Plus: return OO_Plus;
174 case Minus: return OO_Minus;
175 case Not: return OO_Tilde;
176 case LNot: return OO_Exclaim;
177 default: return OO_None;
178 }
179}
180
181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182//===----------------------------------------------------------------------===//
183// Postfix Operators.
184//===----------------------------------------------------------------------===//
185
Ted Kremenek668bf912009-02-09 20:51:47 +0000186CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000187 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000188 : Expr(SC, t,
189 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000190 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000191 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000192
193 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000194 SubExprs[FN] = fn;
195 for (unsigned i = 0; i != numargs; ++i)
196 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000197
Douglas Gregorb4609802008-11-14 16:09:21 +0000198 RParenLoc = rparenloc;
199}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000200
Ted Kremenek668bf912009-02-09 20:51:47 +0000201CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
202 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000203 : Expr(CallExprClass, t,
204 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000205 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000206 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000207
208 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000209 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000211 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 RParenLoc = rparenloc;
214}
215
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000216CallExpr::CallExpr(ASTContext &C, EmptyShell Empty)
217 : Expr(CallExprClass, Empty), SubExprs(0), NumArgs(0) {
218 SubExprs = new (C) Stmt*[1];
219}
220
Ted Kremenek668bf912009-02-09 20:51:47 +0000221void CallExpr::Destroy(ASTContext& C) {
222 DestroyChildren(C);
223 if (SubExprs) C.Deallocate(SubExprs);
224 this->~CallExpr();
225 C.Deallocate(this);
226}
227
Chris Lattnerd18b3292007-12-28 05:25:02 +0000228/// setNumArgs - This changes the number of arguments present in this call.
229/// Any orphaned expressions are deleted by this, and any new operands are set
230/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000231void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000232 // No change, just return.
233 if (NumArgs == getNumArgs()) return;
234
235 // If shrinking # arguments, just delete the extras and forgot them.
236 if (NumArgs < getNumArgs()) {
237 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000238 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000239 this->NumArgs = NumArgs;
240 return;
241 }
242
243 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000244 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000245 // Copy over args.
246 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
247 NewSubExprs[i] = SubExprs[i];
248 // Null out new args.
249 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
250 NewSubExprs[i] = 0;
251
Douglas Gregor88c9a462009-04-17 21:46:47 +0000252 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000253 SubExprs = NewSubExprs;
254 this->NumArgs = NumArgs;
255}
256
Chris Lattnercb888962008-10-06 05:00:53 +0000257/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
258/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000259unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000260 // All simple function calls (e.g. func()) are implicitly cast to pointer to
261 // function. As a result, we try and obtain the DeclRefExpr from the
262 // ImplicitCastExpr.
263 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
264 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000265 return 0;
266
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000267 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
268 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000269 return 0;
270
Anders Carlssonbcba2012008-01-31 02:13:57 +0000271 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
272 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000273 return 0;
274
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000275 if (!FDecl->getIdentifier())
276 return 0;
277
Douglas Gregor3c385e52009-02-14 18:57:46 +0000278 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000279}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000280
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000281QualType CallExpr::getCallReturnType() const {
282 QualType CalleeType = getCallee()->getType();
283 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
284 CalleeType = FnTypePtr->getPointeeType();
285 else if (const BlockPointerType *BPT = CalleeType->getAsBlockPointerType())
286 CalleeType = BPT->getPointeeType();
287
288 const FunctionType *FnType = CalleeType->getAsFunctionType();
289 return FnType->getResultType();
290}
Chris Lattnercb888962008-10-06 05:00:53 +0000291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
293/// corresponds to, e.g. "<<=".
294const char *BinaryOperator::getOpcodeStr(Opcode Op) {
295 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000296 case PtrMemD: return ".*";
297 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 case Mul: return "*";
299 case Div: return "/";
300 case Rem: return "%";
301 case Add: return "+";
302 case Sub: return "-";
303 case Shl: return "<<";
304 case Shr: return ">>";
305 case LT: return "<";
306 case GT: return ">";
307 case LE: return "<=";
308 case GE: return ">=";
309 case EQ: return "==";
310 case NE: return "!=";
311 case And: return "&";
312 case Xor: return "^";
313 case Or: return "|";
314 case LAnd: return "&&";
315 case LOr: return "||";
316 case Assign: return "=";
317 case MulAssign: return "*=";
318 case DivAssign: return "/=";
319 case RemAssign: return "%=";
320 case AddAssign: return "+=";
321 case SubAssign: return "-=";
322 case ShlAssign: return "<<=";
323 case ShrAssign: return ">>=";
324 case AndAssign: return "&=";
325 case XorAssign: return "^=";
326 case OrAssign: return "|=";
327 case Comma: return ",";
328 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000329
330 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
Douglas Gregor063daf62009-03-13 18:40:31 +0000333BinaryOperator::Opcode
334BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
335 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000336 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000337 case OO_Plus: return Add;
338 case OO_Minus: return Sub;
339 case OO_Star: return Mul;
340 case OO_Slash: return Div;
341 case OO_Percent: return Rem;
342 case OO_Caret: return Xor;
343 case OO_Amp: return And;
344 case OO_Pipe: return Or;
345 case OO_Equal: return Assign;
346 case OO_Less: return LT;
347 case OO_Greater: return GT;
348 case OO_PlusEqual: return AddAssign;
349 case OO_MinusEqual: return SubAssign;
350 case OO_StarEqual: return MulAssign;
351 case OO_SlashEqual: return DivAssign;
352 case OO_PercentEqual: return RemAssign;
353 case OO_CaretEqual: return XorAssign;
354 case OO_AmpEqual: return AndAssign;
355 case OO_PipeEqual: return OrAssign;
356 case OO_LessLess: return Shl;
357 case OO_GreaterGreater: return Shr;
358 case OO_LessLessEqual: return ShlAssign;
359 case OO_GreaterGreaterEqual: return ShrAssign;
360 case OO_EqualEqual: return EQ;
361 case OO_ExclaimEqual: return NE;
362 case OO_LessEqual: return LE;
363 case OO_GreaterEqual: return GE;
364 case OO_AmpAmp: return LAnd;
365 case OO_PipePipe: return LOr;
366 case OO_Comma: return Comma;
367 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000368 }
369}
370
371OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
372 static const OverloadedOperatorKind OverOps[] = {
373 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
374 OO_Star, OO_Slash, OO_Percent,
375 OO_Plus, OO_Minus,
376 OO_LessLess, OO_GreaterGreater,
377 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
378 OO_EqualEqual, OO_ExclaimEqual,
379 OO_Amp,
380 OO_Caret,
381 OO_Pipe,
382 OO_AmpAmp,
383 OO_PipePipe,
384 OO_Equal, OO_StarEqual,
385 OO_SlashEqual, OO_PercentEqual,
386 OO_PlusEqual, OO_MinusEqual,
387 OO_LessLessEqual, OO_GreaterGreaterEqual,
388 OO_AmpEqual, OO_CaretEqual,
389 OO_PipeEqual,
390 OO_Comma
391 };
392 return OverOps[Opc];
393}
394
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000395InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000396 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000397 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000398 : Expr(InitListExprClass, QualType(),
399 hasAnyTypeDependentArguments(initExprs, numInits),
400 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000401 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000402 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000403
404 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000405}
Reid Spencer5f016e22007-07-11 17:01:13 +0000406
Douglas Gregorfa219202009-03-20 23:58:33 +0000407void InitListExpr::reserveInits(unsigned NumInits) {
408 if (NumInits > InitExprs.size())
409 InitExprs.reserve(NumInits);
410}
411
Douglas Gregor4c678342009-01-28 21:54:33 +0000412void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000413 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000414 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000415 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000416 InitExprs.resize(NumInits, 0);
417}
418
419Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
420 if (Init >= InitExprs.size()) {
421 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
422 InitExprs.back() = expr;
423 return 0;
424 }
425
426 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
427 InitExprs[Init] = expr;
428 return Result;
429}
430
Steve Naroffbfdcae62008-09-04 15:31:07 +0000431/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000432///
433const FunctionType *BlockExpr::getFunctionType() const {
434 return getType()->getAsBlockPointerType()->
435 getPointeeType()->getAsFunctionType();
436}
437
Steve Naroff56ee6892008-10-08 17:01:13 +0000438SourceLocation BlockExpr::getCaretLocation() const {
439 return TheBlock->getCaretLocation();
440}
Douglas Gregor72971342009-04-18 00:02:19 +0000441const Stmt *BlockExpr::getBody() const {
442 return TheBlock->getBody();
443}
444Stmt *BlockExpr::getBody() {
445 return TheBlock->getBody();
446}
Steve Naroff56ee6892008-10-08 17:01:13 +0000447
448
Reid Spencer5f016e22007-07-11 17:01:13 +0000449//===----------------------------------------------------------------------===//
450// Generic Expression Routines
451//===----------------------------------------------------------------------===//
452
Chris Lattner026dc962009-02-14 07:37:35 +0000453/// isUnusedResultAWarning - Return true if this immediate expression should
454/// be warned about if the result is unused. If so, fill in Loc and Ranges
455/// with location to warn on and the source range[s] to report with the
456/// warning.
457bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
458 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000459 // Don't warn if the expr is type dependent. The type could end up
460 // instantiating to void.
461 if (isTypeDependent())
462 return false;
463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 switch (getStmtClass()) {
465 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000466 Loc = getExprLoc();
467 R1 = getSourceRange();
468 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000470 return cast<ParenExpr>(this)->getSubExpr()->
471 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 case UnaryOperatorClass: {
473 const UnaryOperator *UO = cast<UnaryOperator>(this);
474
475 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000476 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 case UnaryOperator::PostInc:
478 case UnaryOperator::PostDec:
479 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000480 case UnaryOperator::PreDec: // ++/--
481 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 case UnaryOperator::Deref:
483 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000484 if (getType().isVolatileQualified())
485 return false;
486 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 case UnaryOperator::Real:
488 case UnaryOperator::Imag:
489 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000490 if (UO->getSubExpr()->getType().isVolatileQualified())
491 return false;
492 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000494 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Chris Lattner026dc962009-02-14 07:37:35 +0000496 Loc = UO->getOperatorLoc();
497 R1 = UO->getSubExpr()->getSourceRange();
498 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000500 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000501 const BinaryOperator *BO = cast<BinaryOperator>(this);
502 // Consider comma to have side effects if the LHS or RHS does.
503 if (BO->getOpcode() == BinaryOperator::Comma)
504 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
505 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000506
Chris Lattner026dc962009-02-14 07:37:35 +0000507 if (BO->isAssignmentOp())
508 return false;
509 Loc = BO->getOperatorLoc();
510 R1 = BO->getLHS()->getSourceRange();
511 R2 = BO->getRHS()->getSourceRange();
512 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000513 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000514 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000515 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000516
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000517 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000518 // The condition must be evaluated, but if either the LHS or RHS is a
519 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000520 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000521 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000522 return true;
523 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000524 }
525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000527 // If the base pointer or element is to a volatile pointer/field, accessing
528 // it is a side effect.
529 if (getType().isVolatileQualified())
530 return false;
531 Loc = cast<MemberExpr>(this)->getMemberLoc();
532 R1 = SourceRange(Loc, Loc);
533 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
534 return true;
535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 case ArraySubscriptExprClass:
537 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000538 // it is a side effect.
539 if (getType().isVolatileQualified())
540 return false;
541 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
542 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
543 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
544 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000547 case CXXOperatorCallExprClass:
548 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000549 // If this is a direct call, get the callee.
550 const CallExpr *CE = cast<CallExpr>(this);
551 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
552 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
553 // If the callee has attribute pure, const, or warn_unused_result, warn
554 // about it. void foo() { strlen("bar"); } should warn.
555 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
556 if (FD->getAttr<WarnUnusedResultAttr>() ||
557 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
558 Loc = CE->getCallee()->getLocStart();
559 R1 = CE->getCallee()->getSourceRange();
560
561 if (unsigned NumArgs = CE->getNumArgs())
562 R2 = SourceRange(CE->getArg(0)->getLocStart(),
563 CE->getArg(NumArgs-1)->getLocEnd());
564 return true;
565 }
566 }
567 return false;
568 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000569 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000570 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000571 case StmtExprClass: {
572 // Statement exprs don't logically have side effects themselves, but are
573 // sometimes used in macros in ways that give them a type that is unused.
574 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
575 // however, if the result of the stmt expr is dead, we don't want to emit a
576 // warning.
577 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
578 if (!CS->body_empty())
579 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000580 return E->isUnusedResultAWarning(Loc, R1, R2);
581
582 Loc = cast<StmtExpr>(this)->getLParenLoc();
583 R1 = getSourceRange();
584 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000585 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000586 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000587 // If this is a cast to void, check the operand. Otherwise, the result of
588 // the cast is unused.
589 if (getType()->isVoidType())
590 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
591 R1, R2);
592 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
593 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
594 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000595 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 // If this is a cast to void, check the operand. Otherwise, the result of
597 // the cast is unused.
598 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000599 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
600 R1, R2);
601 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
602 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
603 return true;
604
Eli Friedman4be1f472008-05-19 21:24:43 +0000605 case ImplicitCastExprClass:
606 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000607 return cast<ImplicitCastExpr>(this)
608 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000609
Chris Lattner04421082008-04-08 04:40:51 +0000610 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000611 return cast<CXXDefaultArgExpr>(this)
612 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000613
614 case CXXNewExprClass:
615 // FIXME: In theory, there might be new expressions that don't have side
616 // effects (e.g. a placement new with an uninitialized POD).
617 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000618 return false;
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000619 case CXXExprWithTemporariesClass:
620 return cast<CXXExprWithTemporaries>(this)
621 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000622 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000623}
624
Douglas Gregorba7e2102008-10-22 15:04:37 +0000625/// DeclCanBeLvalue - Determine whether the given declaration can be
626/// an lvalue. This is a helper routine for isLvalue.
627static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000628 // C++ [temp.param]p6:
629 // A non-type non-reference template-parameter is not an lvalue.
630 if (const NonTypeTemplateParmDecl *NTTParm
631 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
632 return NTTParm->getType()->isReferenceType();
633
Douglas Gregor44b43212008-12-11 16:49:14 +0000634 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000635 // C++ 3.10p2: An lvalue refers to an object or function.
636 (Ctx.getLangOptions().CPlusPlus &&
637 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
638}
639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
641/// incomplete type other than void. Nonarray expressions that can be lvalues:
642/// - name, where name must be a variable
643/// - e[i]
644/// - (e), where e must be an lvalue
645/// - e.name, where e must be an lvalue
646/// - e->name
647/// - *e, the type of e cannot be a function type
648/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000649/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000650/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000651///
Chris Lattner28be73f2008-07-26 21:30:36 +0000652Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000653 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
654
655 isLvalueResult Res = isLvalueInternal(Ctx);
656 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
657 return Res;
658
Douglas Gregor98cd5992008-10-21 23:43:52 +0000659 // first, check the type (C99 6.3.2.1). Expressions with function
660 // type in C are not lvalues, but they can be lvalues in C++.
Eli Friedman53202852009-05-03 22:36:05 +0000661 if (TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 return LV_NotObjectType;
663
Steve Naroffacb818a2008-02-10 01:39:04 +0000664 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000665 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000666 return LV_IncompleteVoidType;
667
Eli Friedman53202852009-05-03 22:36:05 +0000668 return LV_Valid;
669}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000670
Eli Friedman53202852009-05-03 22:36:05 +0000671// Check whether the expression can be sanely treated like an l-value
672Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000674 case StringLiteralClass: // C99 6.5.1p4
675 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000676 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
678 // For vectors, make sure base is an lvalue (i.e. not a function call).
679 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000680 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000682 case DeclRefExprClass:
683 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000684 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
685 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 return LV_Valid;
687 break;
Chris Lattner41110242008-06-17 18:05:57 +0000688 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000689 case BlockDeclRefExprClass: {
690 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000691 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000692 return LV_Valid;
693 break;
694 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000695 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000697 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
698 NamedDecl *Member = m->getMemberDecl();
699 // C++ [expr.ref]p4:
700 // If E2 is declared to have type "reference to T", then E1.E2
701 // is an lvalue.
702 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
703 if (Value->getType()->isReferenceType())
704 return LV_Valid;
705
706 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000707 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000708 return LV_Valid;
709
710 // -- If E2 is a non-static data member [...]. If E1 is an
711 // lvalue, then E1.E2 is an lvalue.
712 if (isa<FieldDecl>(Member))
713 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
714
715 // -- If it refers to a static member function [...], then
716 // E1.E2 is an lvalue.
717 // -- Otherwise, if E1.E2 refers to a non-static member
718 // function [...], then E1.E2 is not an lvalue.
719 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
720 return Method->isStatic()? LV_Valid : LV_MemberFunction;
721
722 // -- If E2 is a member enumerator [...], the expression E1.E2
723 // is not an lvalue.
724 if (isa<EnumConstantDecl>(Member))
725 return LV_InvalidExpression;
726
727 // Not an lvalue.
728 return LV_InvalidExpression;
729 }
730
731 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000732 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000733 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000734 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000736 return LV_Valid; // C99 6.5.3p4
737
738 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000739 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
740 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000741 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000742
743 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
744 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
745 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
746 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000748 case ImplicitCastExprClass:
749 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
750 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000752 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000753 case BinaryOperatorClass:
754 case CompoundAssignOperatorClass: {
755 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000756
757 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
758 BinOp->getOpcode() == BinaryOperator::Comma)
759 return BinOp->getRHS()->isLvalue(Ctx);
760
Sebastian Redl22460502009-02-07 00:15:38 +0000761 // C++ [expr.mptr.oper]p6
762 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
763 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
764 !BinOp->getType()->isFunctionType())
765 return BinOp->getLHS()->isLvalue(Ctx);
766
Douglas Gregorbf3af052008-11-13 20:12:29 +0000767 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000768 return LV_InvalidExpression;
769
Douglas Gregorbf3af052008-11-13 20:12:29 +0000770 if (Ctx.getLangOptions().CPlusPlus)
771 // C++ [expr.ass]p1:
772 // The result of an assignment operation [...] is an lvalue.
773 return LV_Valid;
774
775
776 // C99 6.5.16:
777 // An assignment expression [...] is not an lvalue.
778 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000779 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000780 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000781 case CXXOperatorCallExprClass:
782 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000783 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000784 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000785 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000786 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
787 if (ReturnType->isLValueReferenceType())
788 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000789
Douglas Gregor9d293df2008-10-28 00:22:11 +0000790 break;
791 }
Steve Naroffe6386392007-12-05 04:00:10 +0000792 case CompoundLiteralExprClass: // C99 6.5.2.5p5
793 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000794 case ChooseExprClass:
795 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000796 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000797 case ExtVectorElementExprClass:
798 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000799 return LV_DuplicateVectorComponents;
800 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000801 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
802 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000803 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
804 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000805 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000806 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000807 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000808 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000809 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000810 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000811 case CXXConditionDeclExprClass:
812 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000813 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000814 case CXXFunctionalCastExprClass:
815 case CXXStaticCastExprClass:
816 case CXXDynamicCastExprClass:
817 case CXXReinterpretCastExprClass:
818 case CXXConstCastExprClass:
819 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000820 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000821 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
822 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000823 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
824 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000825 return LV_Valid;
826 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000827 case CXXTypeidExprClass:
828 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
829 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +0000830 case ConditionalOperatorClass: {
831 // Complicated handling is only for C++.
832 if (!Ctx.getLangOptions().CPlusPlus)
833 return LV_InvalidExpression;
834
835 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
836 // everywhere there's an object converted to an rvalue. Also, any other
837 // casts should be wrapped by ImplicitCastExprs. There's just the special
838 // case involving throws to work out.
839 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000840 Expr *True = Cond->getTrueExpr();
841 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +0000842 // C++0x 5.16p2
843 // If either the second or the third operand has type (cv) void, [...]
844 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000845 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +0000846 return LV_InvalidExpression;
847
848 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000849 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +0000850 return LV_InvalidExpression;
851
852 // That's it.
853 return LV_Valid;
854 }
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 default:
857 break;
858 }
859 return LV_InvalidExpression;
860}
861
862/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
863/// does not have an incomplete type, does not have a const-qualified type, and
864/// if it is a structure or union, does not have any member (including,
865/// recursively, any member or element of all contained aggregates or unions)
866/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000867Expr::isModifiableLvalueResult
868Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000869 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000870
871 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000872 case LV_Valid:
873 // C++ 3.10p11: Functions cannot be modified, but pointers to
874 // functions can be modifiable.
875 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
876 return MLV_NotObjectType;
877 break;
878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 case LV_NotObjectType: return MLV_NotObjectType;
880 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000881 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000882 case LV_InvalidExpression:
883 // If the top level is a C-style cast, and the subexpression is a valid
884 // lvalue, then this is probably a use of the old-school "cast as lvalue"
885 // GCC extension. We don't support it, but we want to produce good
886 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000887 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
888 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
889 if (Loc)
890 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000891 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000892 }
893 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000894 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000895 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000897
898 // The following is illegal:
899 // void takeclosure(void (^C)(void));
900 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
901 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000902 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000903 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
904 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
905 return MLV_NotBlockQualified;
906 }
907
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000908 QualType CT = Ctx.getCanonicalType(getType());
909
910 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000912 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000914 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 return MLV_IncompleteType;
916
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000917 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 if (r->hasConstFields())
919 return MLV_ConstQualified;
920 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000921
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000922 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000923 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000924 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
925 if (KVCExpr->getSetterMethod() == 0)
926 return MLV_NoSetterProperty;
927 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 return MLV_Valid;
929}
930
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000931/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000932/// duration. This means that the address of this expression is a link-time
933/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000934bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000935 switch (getStmtClass()) {
936 default:
937 return false;
Steve Naroff3aaa4822009-04-16 19:02:57 +0000938 case BlockExprClass:
939 return true;
Chris Lattner4cc62712007-11-27 21:35:27 +0000940 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000941 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000942 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000943 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000944 case CompoundLiteralExprClass:
945 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000946 case DeclRefExprClass:
947 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000948 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
949 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000950 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000951 if (isa<FunctionDecl>(D))
952 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000953 return false;
954 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000955 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000956 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000957 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000958 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000959 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000960 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000961 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000962 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000963 case CXXDefaultArgExprClass:
964 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000965 }
966}
967
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000968/// isOBJCGCCandidate - Check if an expression is objc gc'able.
969///
970bool Expr::isOBJCGCCandidate() const {
971 switch (getStmtClass()) {
972 default:
973 return false;
974 case ObjCIvarRefExprClass:
975 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000976 case Expr::UnaryOperatorClass:
977 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000978 case ParenExprClass:
979 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
980 case ImplicitCastExprClass:
981 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000982 case CStyleCastExprClass:
983 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000984 case DeclRefExprClass:
985 case QualifiedDeclRefExprClass: {
986 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
987 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
988 return VD->hasGlobalStorage();
989 return false;
990 }
991 case MemberExprClass: {
992 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000993 return M->getBase()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000994 }
995 case ArraySubscriptExprClass:
996 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
997 }
998}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000999Expr* Expr::IgnoreParens() {
1000 Expr* E = this;
1001 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1002 E = P->getSubExpr();
1003
1004 return E;
1005}
1006
Chris Lattner56f34942008-02-13 01:02:39 +00001007/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1008/// or CastExprs or ImplicitCastExprs, returning their operand.
1009Expr *Expr::IgnoreParenCasts() {
1010 Expr *E = this;
1011 while (true) {
1012 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1013 E = P->getSubExpr();
1014 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1015 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001016 else
1017 return E;
1018 }
1019}
1020
Chris Lattnerecdd8412009-03-13 17:28:01 +00001021/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1022/// value (including ptr->int casts of the same size). Strip off any
1023/// ParenExpr or CastExprs, returning their operand.
1024Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1025 Expr *E = this;
1026 while (true) {
1027 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1028 E = P->getSubExpr();
1029 continue;
1030 }
1031
1032 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1033 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1034 // ptr<->int casts of the same width. We also ignore all identify casts.
1035 Expr *SE = P->getSubExpr();
1036
1037 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1038 E = SE;
1039 continue;
1040 }
1041
1042 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1043 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1044 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1045 E = SE;
1046 continue;
1047 }
1048 }
1049
1050 return E;
1051 }
1052}
1053
1054
Douglas Gregor898574e2008-12-05 23:32:09 +00001055/// hasAnyTypeDependentArguments - Determines if any of the expressions
1056/// in Exprs is type-dependent.
1057bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1058 for (unsigned I = 0; I < NumExprs; ++I)
1059 if (Exprs[I]->isTypeDependent())
1060 return true;
1061
1062 return false;
1063}
1064
1065/// hasAnyValueDependentArguments - Determines if any of the expressions
1066/// in Exprs is value-dependent.
1067bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1068 for (unsigned I = 0; I < NumExprs; ++I)
1069 if (Exprs[I]->isValueDependent())
1070 return true;
1071
1072 return false;
1073}
1074
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001075bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001076 // This function is attempting whether an expression is an initializer
1077 // which can be evaluated at compile-time. isEvaluatable handles most
1078 // of the cases, but it can't deal with some initializer-specific
1079 // expressions, and it can't deal with aggregates; we deal with those here,
1080 // and fall back to isEvaluatable for the other cases.
1081
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001082 // FIXME: This function assumes the variable being assigned to
1083 // isn't a reference type!
1084
Anders Carlssone8a32b82008-11-24 05:23:59 +00001085 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001086 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001087 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001088 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001089 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001090 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001091 // This handles gcc's extension that allows global initializers like
1092 // "struct x {int x;} x = (struct x) {};".
1093 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001094 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001095 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001096 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001097 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001098 // FIXME: This doesn't deal with fields with reference types correctly.
1099 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1100 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001101 const InitListExpr *Exp = cast<InitListExpr>(this);
1102 unsigned numInits = Exp->getNumInits();
1103 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001104 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001105 return false;
1106 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001107 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001108 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001109 case ImplicitValueInitExprClass:
1110 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001111 case ParenExprClass: {
1112 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1113 }
1114 case UnaryOperatorClass: {
1115 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1116 if (Exp->getOpcode() == UnaryOperator::Extension)
1117 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1118 break;
1119 }
Chris Lattner81045d82009-04-21 05:19:11 +00001120 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001121 case CStyleCastExprClass:
1122 // Handle casts with a destination that's a struct or union; this
1123 // deals with both the gcc no-op struct cast extension and the
1124 // cast-to-union extension.
1125 if (getType()->isRecordType())
1126 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1127 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001128 }
1129
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001130 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001131}
1132
Reid Spencer5f016e22007-07-11 17:01:13 +00001133/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001134/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001135
1136/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1137/// comma, etc
1138///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001139/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1140/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1141/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001142
Eli Friedmane28d7192009-02-26 09:29:13 +00001143// CheckICE - This function does the fundamental ICE checking: the returned
1144// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1145// Note that to reduce code duplication, this helper does no evaluation
1146// itself; the caller checks whether the expression is evaluatable, and
1147// in the rare cases where CheckICE actually cares about the evaluated
1148// value, it calls into Evalute.
1149//
1150// Meanings of Val:
1151// 0: This expression is an ICE if it can be evaluated by Evaluate.
1152// 1: This expression is not an ICE, but if it isn't evaluated, it's
1153// a legal subexpression for an ICE. This return value is used to handle
1154// the comma operator in C99 mode.
1155// 2: This expression is not an ICE, and is not a legal subexpression for one.
1156
1157struct ICEDiag {
1158 unsigned Val;
1159 SourceLocation Loc;
1160
1161 public:
1162 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1163 ICEDiag() : Val(0) {}
1164};
1165
1166ICEDiag NoDiag() { return ICEDiag(); }
1167
Eli Friedman60ce9632009-02-27 04:07:58 +00001168static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1169 Expr::EvalResult EVResult;
1170 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1171 !EVResult.Val.isInt()) {
1172 return ICEDiag(2, E->getLocStart());
1173 }
1174 return NoDiag();
1175}
1176
Eli Friedmane28d7192009-02-26 09:29:13 +00001177static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001178 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001179 if (!E->getType()->isIntegralType()) {
1180 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001181 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001182
1183 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001185 return ICEDiag(2, E->getLocStart());
1186 case Expr::ParenExprClass:
1187 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1188 case Expr::IntegerLiteralClass:
1189 case Expr::CharacterLiteralClass:
1190 case Expr::CXXBoolLiteralExprClass:
1191 case Expr::CXXZeroInitValueExprClass:
1192 case Expr::TypesCompatibleExprClass:
1193 case Expr::UnaryTypeTraitExprClass:
1194 return NoDiag();
1195 case Expr::CallExprClass:
1196 case Expr::CXXOperatorCallExprClass: {
1197 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001198 if (CE->isBuiltinCall(Ctx))
1199 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001200 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001201 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001202 case Expr::DeclRefExprClass:
1203 case Expr::QualifiedDeclRefExprClass:
1204 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1205 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001206 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001207 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001208 // C++ 7.1.5.1p2
1209 // A variable of non-volatile const-qualified integral or enumeration
1210 // type initialized by an ICE can be used in ICEs.
1211 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001212 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001213 if (Dcl->isInitKnownICE()) {
1214 // We have already checked whether this subexpression is an
1215 // integral constant expression.
1216 if (Dcl->isInitICE())
1217 return NoDiag();
1218 else
1219 return ICEDiag(2, E->getLocStart());
1220 }
1221
1222 if (const Expr *Init = Dcl->getInit()) {
1223 ICEDiag Result = CheckICE(Init, Ctx);
1224 // Cache the result of the ICE test.
1225 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1226 return Result;
1227 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001228 }
1229 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001230 return ICEDiag(2, E->getLocStart());
1231 case Expr::UnaryOperatorClass: {
1232 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001235 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001237 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001241 case UnaryOperator::Real:
1242 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001243 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001244 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001245 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1246 // Evaluate matches the proposed gcc behavior for cases like
1247 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1248 // compliance: we should warn earlier for offsetof expressions with
1249 // array subscripts that aren't ICEs, and if the array subscripts
1250 // are ICEs, the value of the offsetof must be an integer constant.
1251 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001254 case Expr::SizeOfAlignOfExprClass: {
1255 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1256 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1257 return ICEDiag(2, E->getLocStart());
1258 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001260 case Expr::BinaryOperatorClass: {
1261 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 switch (Exp->getOpcode()) {
1263 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001264 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001268 case BinaryOperator::Add:
1269 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001272 case BinaryOperator::LT:
1273 case BinaryOperator::GT:
1274 case BinaryOperator::LE:
1275 case BinaryOperator::GE:
1276 case BinaryOperator::EQ:
1277 case BinaryOperator::NE:
1278 case BinaryOperator::And:
1279 case BinaryOperator::Xor:
1280 case BinaryOperator::Or:
1281 case BinaryOperator::Comma: {
1282 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1283 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001284 if (Exp->getOpcode() == BinaryOperator::Div ||
1285 Exp->getOpcode() == BinaryOperator::Rem) {
1286 // Evaluate gives an error for undefined Div/Rem, so make sure
1287 // we don't evaluate one.
1288 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1289 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1290 if (REval == 0)
1291 return ICEDiag(1, E->getLocStart());
1292 if (REval.isSigned() && REval.isAllOnesValue()) {
1293 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1294 if (LEval.isMinSignedValue())
1295 return ICEDiag(1, E->getLocStart());
1296 }
1297 }
1298 }
1299 if (Exp->getOpcode() == BinaryOperator::Comma) {
1300 if (Ctx.getLangOptions().C99) {
1301 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1302 // if it isn't evaluated.
1303 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1304 return ICEDiag(1, E->getLocStart());
1305 } else {
1306 // In both C89 and C++, commas in ICEs are illegal.
1307 return ICEDiag(2, E->getLocStart());
1308 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001309 }
1310 if (LHSResult.Val >= RHSResult.Val)
1311 return LHSResult;
1312 return RHSResult;
1313 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001315 case BinaryOperator::LOr: {
1316 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1317 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1318 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1319 // Rare case where the RHS has a comma "side-effect"; we need
1320 // to actually check the condition to see whether the side
1321 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001322 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001323 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001324 return RHSResult;
1325 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001326 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001327
Eli Friedmane28d7192009-02-26 09:29:13 +00001328 if (LHSResult.Val >= RHSResult.Val)
1329 return LHSResult;
1330 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001332 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001334 case Expr::ImplicitCastExprClass:
1335 case Expr::CStyleCastExprClass:
1336 case Expr::CXXFunctionalCastExprClass: {
1337 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1338 if (SubExpr->getType()->isIntegralType())
1339 return CheckICE(SubExpr, Ctx);
1340 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1341 return NoDiag();
1342 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001344 case Expr::ConditionalOperatorClass: {
1345 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001346 // If the condition (ignoring parens) is a __builtin_constant_p call,
1347 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001348 // expression, and it is fully evaluated. This is an important GNU
1349 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001350 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001351 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001352 Expr::EvalResult EVResult;
1353 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1354 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001355 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001356 }
1357 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001358 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001359 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1360 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1361 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1362 if (CondResult.Val == 2)
1363 return CondResult;
1364 if (TrueResult.Val == 2)
1365 return TrueResult;
1366 if (FalseResult.Val == 2)
1367 return FalseResult;
1368 if (CondResult.Val == 1)
1369 return CondResult;
1370 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1371 return NoDiag();
1372 // Rare case where the diagnostics depend on which side is evaluated
1373 // Note that if we get here, CondResult is 0, and at least one of
1374 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001375 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001376 return FalseResult;
1377 }
1378 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001380 case Expr::CXXDefaultArgExprClass:
1381 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001382 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001383 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001384 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001386}
Reid Spencer5f016e22007-07-11 17:01:13 +00001387
Eli Friedmane28d7192009-02-26 09:29:13 +00001388bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1389 SourceLocation *Loc, bool isEvaluated) const {
1390 ICEDiag d = CheckICE(this, Ctx);
1391 if (d.Val != 0) {
1392 if (Loc) *Loc = d.Loc;
1393 return false;
1394 }
1395 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001396 if (!Evaluate(EvalResult, Ctx))
1397 assert(0 && "ICE cannot be evaluated!");
1398 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1399 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001400 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 return true;
1402}
1403
Reid Spencer5f016e22007-07-11 17:01:13 +00001404/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1405/// integer constant expression with the value zero, or if this is one that is
1406/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001407bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1408{
Sebastian Redl07779722008-10-31 14:43:28 +00001409 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001410 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001411 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001412 // Check that it is a cast to void*.
1413 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1414 QualType Pointee = PT->getPointeeType();
1415 if (Pointee.getCVRQualifiers() == 0 &&
1416 Pointee->isVoidType() && // to void*
1417 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001418 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001419 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001421 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1422 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001423 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001424 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1425 // Accept ((void*)0) as a null pointer constant, as many other
1426 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001427 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001428 } else if (const CXXDefaultArgExpr *DefaultArg
1429 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001430 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001431 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001432 } else if (isa<GNUNullExpr>(this)) {
1433 // The GNU __null extension is always a null pointer constant.
1434 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001435 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001436
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001437 // C++0x nullptr_t is always a null pointer constant.
1438 if (getType()->isNullPtrType())
1439 return true;
1440
Steve Naroffaa58f002008-01-14 16:10:57 +00001441 // This expression must be an integer type.
1442 if (!getType()->isIntegerType())
1443 return false;
1444
Reid Spencer5f016e22007-07-11 17:01:13 +00001445 // If we have an integer constant expression, we need to *evaluate* it and
1446 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001447 llvm::APSInt Result;
1448 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001449}
Steve Naroff31a45842007-07-28 23:10:27 +00001450
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001451FieldDecl *Expr::getBitField() {
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001452 Expr *E = this->IgnoreParenCasts();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001453
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001454 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001455 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001456 if (Field->isBitField())
1457 return Field;
1458
1459 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1460 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1461 return BinOp->getLHS()->getBitField();
1462
1463 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001464}
1465
Chris Lattner2140e902009-02-16 22:14:05 +00001466/// isArrow - Return true if the base expression is a pointer to vector,
1467/// return false if the base expression is a vector.
1468bool ExtVectorElementExpr::isArrow() const {
1469 return getBase()->getType()->isPointerType();
1470}
1471
Nate Begeman213541a2008-04-18 23:10:10 +00001472unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001473 if (const VectorType *VT = getType()->getAsVectorType())
1474 return VT->getNumElements();
1475 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001476}
1477
Nate Begeman8a997642008-05-09 06:41:27 +00001478/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001479bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001480 const char *compStr = Accessor->getName();
1481 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001482
1483 // Halving swizzles do not contain duplicate elements.
1484 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1485 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1486 return false;
1487
1488 // Advance past s-char prefix on hex swizzles.
1489 if (*compStr == 's') {
1490 compStr++;
1491 length--;
1492 }
Steve Narofffec0b492007-07-30 03:29:09 +00001493
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001494 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001495 const char *s = compStr+i;
1496 for (const char c = *s++; *s; s++)
1497 if (c == *s)
1498 return true;
1499 }
1500 return false;
1501}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001502
Nate Begeman8a997642008-05-09 06:41:27 +00001503/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001504void ExtVectorElementExpr::getEncodedElementAccess(
1505 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001506 const char *compStr = Accessor->getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001507 if (*compStr == 's')
1508 compStr++;
1509
1510 bool isHi = !strcmp(compStr, "hi");
1511 bool isLo = !strcmp(compStr, "lo");
1512 bool isEven = !strcmp(compStr, "even");
1513 bool isOdd = !strcmp(compStr, "odd");
1514
Nate Begeman8a997642008-05-09 06:41:27 +00001515 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1516 uint64_t Index;
1517
1518 if (isHi)
1519 Index = e + i;
1520 else if (isLo)
1521 Index = i;
1522 else if (isEven)
1523 Index = 2 * i;
1524 else if (isOdd)
1525 Index = 2 * i + 1;
1526 else
1527 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001528
Nate Begeman3b8d1162008-05-13 21:03:02 +00001529 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001530 }
Nate Begeman8a997642008-05-09 06:41:27 +00001531}
1532
Steve Naroff68d331a2007-09-27 14:38:14 +00001533// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001534ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001535 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001536 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001537 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001538 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001539 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001540 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001541 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001542 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001543 if (NumArgs) {
1544 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001545 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1546 }
Steve Naroff563477d2007-09-18 23:55:05 +00001547 LBracloc = LBrac;
1548 RBracloc = RBrac;
1549}
1550
Steve Naroff68d331a2007-09-27 14:38:14 +00001551// constructor for class messages.
1552// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001553ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001554 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001555 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001556 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001557 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001558 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001559 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001560 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001561 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001562 if (NumArgs) {
1563 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001564 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1565 }
Steve Naroff563477d2007-09-18 23:55:05 +00001566 LBracloc = LBrac;
1567 RBracloc = RBrac;
1568}
1569
Ted Kremenek4df728e2008-06-24 15:50:53 +00001570// constructor for class messages.
1571ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1572 QualType retType, ObjCMethodDecl *mproto,
1573 SourceLocation LBrac, SourceLocation RBrac,
1574 Expr **ArgExprs, unsigned nargs)
1575: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1576MethodProto(mproto) {
1577 NumArgs = nargs;
1578 SubExprs = new Stmt*[NumArgs+1];
1579 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1580 if (NumArgs) {
1581 for (unsigned i = 0; i != NumArgs; ++i)
1582 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1583 }
1584 LBracloc = LBrac;
1585 RBracloc = RBrac;
1586}
1587
1588ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1589 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1590 switch (x & Flags) {
1591 default:
1592 assert(false && "Invalid ObjCMessageExpr.");
1593 case IsInstMeth:
1594 return ClassInfo(0, 0);
1595 case IsClsMethDeclUnknown:
1596 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1597 case IsClsMethDeclKnown: {
1598 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1599 return ClassInfo(D, D->getIdentifier());
1600 }
1601 }
1602}
1603
Chris Lattner0389e6b2009-04-26 00:44:05 +00001604void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1605 if (CI.first == 0 && CI.second == 0)
1606 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1607 else if (CI.first == 0)
1608 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1609 else
1610 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1611}
1612
1613
Chris Lattner27437ca2007-10-25 00:29:32 +00001614bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001615 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001616}
1617
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001618void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1619 if (NumExprs)
1620 delete [] SubExprs;
1621
1622 SubExprs = new Stmt* [NumExprs];
1623 this->NumExprs = NumExprs;
1624 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1625}
1626
Sebastian Redl05189992008-11-11 17:56:53 +00001627void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1628 // Override default behavior of traversing children. If this has a type
1629 // operand and the type is a variable-length array, the child iteration
1630 // will iterate over the size expression. However, this expression belongs
1631 // to the type, not to this, so we don't want to delete it.
1632 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001633 if (isArgumentType()) {
1634 this->~SizeOfAlignOfExpr();
1635 C.Deallocate(this);
1636 }
Sebastian Redl05189992008-11-11 17:56:53 +00001637 else
1638 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001639}
1640
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001641//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001642// DesignatedInitExpr
1643//===----------------------------------------------------------------------===//
1644
1645IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1646 assert(Kind == FieldDesignator && "Only valid on a field designator");
1647 if (Field.NameOrField & 0x01)
1648 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1649 else
1650 return getField()->getIdentifier();
1651}
1652
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001653DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1654 const Designator *Designators,
1655 SourceLocation EqualOrColonLoc,
1656 bool GNUSyntax,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001657 Expr **IndexExprs,
1658 unsigned NumIndexExprs,
1659 Expr *Init)
1660 : Expr(DesignatedInitExprClass, Ty,
1661 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001662 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor9ea62762009-05-21 23:17:49 +00001663 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001664 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001665
1666 // Record the initializer itself.
1667 child_iterator Child = child_begin();
1668 *Child++ = Init;
1669
1670 // Copy the designators and their subexpressions, computing
1671 // value-dependence along the way.
1672 unsigned IndexIdx = 0;
1673 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001674 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001675
1676 if (this->Designators[I].isArrayDesignator()) {
1677 // Compute type- and value-dependence.
1678 Expr *Index = IndexExprs[IndexIdx];
1679 ValueDependent = ValueDependent ||
1680 Index->isTypeDependent() || Index->isValueDependent();
1681
1682 // Copy the index expressions into permanent storage.
1683 *Child++ = IndexExprs[IndexIdx++];
1684 } else if (this->Designators[I].isArrayRangeDesignator()) {
1685 // Compute type- and value-dependence.
1686 Expr *Start = IndexExprs[IndexIdx];
1687 Expr *End = IndexExprs[IndexIdx + 1];
1688 ValueDependent = ValueDependent ||
1689 Start->isTypeDependent() || Start->isValueDependent() ||
1690 End->isTypeDependent() || End->isValueDependent();
1691
1692 // Copy the start/end expressions into permanent storage.
1693 *Child++ = IndexExprs[IndexIdx++];
1694 *Child++ = IndexExprs[IndexIdx++];
1695 }
1696 }
1697
1698 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001699}
1700
Douglas Gregor05c13a32009-01-22 00:58:24 +00001701DesignatedInitExpr *
1702DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1703 unsigned NumDesignators,
1704 Expr **IndexExprs, unsigned NumIndexExprs,
1705 SourceLocation ColonOrEqualLoc,
1706 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001707 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001708 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001709 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1710 ColonOrEqualLoc, UsesColonSyntax,
1711 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001712}
1713
Douglas Gregord077d752009-04-16 00:55:48 +00001714DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1715 unsigned NumIndexExprs) {
1716 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1717 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1718 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1719}
1720
1721void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1722 unsigned NumDesigs) {
1723 if (Designators)
1724 delete [] Designators;
1725
1726 Designators = new Designator[NumDesigs];
1727 NumDesignators = NumDesigs;
1728 for (unsigned I = 0; I != NumDesigs; ++I)
1729 Designators[I] = Desigs[I];
1730}
1731
Douglas Gregor05c13a32009-01-22 00:58:24 +00001732SourceRange DesignatedInitExpr::getSourceRange() const {
1733 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001734 Designator &First =
1735 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001736 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001737 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001738 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1739 else
1740 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1741 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001742 StartLoc =
1743 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001744 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1745}
1746
Douglas Gregor05c13a32009-01-22 00:58:24 +00001747Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1748 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1749 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1750 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001751 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1752 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1753}
1754
1755Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1756 assert(D.Kind == Designator::ArrayRangeDesignator &&
1757 "Requires array range designator");
1758 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1759 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001760 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1761 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1762}
1763
1764Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1765 assert(D.Kind == Designator::ArrayRangeDesignator &&
1766 "Requires array range designator");
1767 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1768 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001769 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1770 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1771}
1772
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001773/// \brief Replaces the designator at index @p Idx with the series
1774/// of designators in [First, Last).
1775void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1776 const Designator *First,
1777 const Designator *Last) {
1778 unsigned NumNewDesignators = Last - First;
1779 if (NumNewDesignators == 0) {
1780 std::copy_backward(Designators + Idx + 1,
1781 Designators + NumDesignators,
1782 Designators + Idx);
1783 --NumNewDesignators;
1784 return;
1785 } else if (NumNewDesignators == 1) {
1786 Designators[Idx] = *First;
1787 return;
1788 }
1789
1790 Designator *NewDesignators
1791 = new Designator[NumDesignators - 1 + NumNewDesignators];
1792 std::copy(Designators, Designators + Idx, NewDesignators);
1793 std::copy(First, Last, NewDesignators + Idx);
1794 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1795 NewDesignators + Idx + NumNewDesignators);
1796 delete [] Designators;
1797 Designators = NewDesignators;
1798 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1799}
1800
1801void DesignatedInitExpr::Destroy(ASTContext &C) {
1802 delete [] Designators;
1803 Expr::Destroy(C);
1804}
1805
Douglas Gregor9ea62762009-05-21 23:17:49 +00001806ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1807 return new (C) ImplicitValueInitExpr(getType());
1808}
1809
Douglas Gregor05c13a32009-01-22 00:58:24 +00001810//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001811// ExprIterator.
1812//===----------------------------------------------------------------------===//
1813
1814Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1815Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1816Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1817const Expr* ConstExprIterator::operator[](size_t idx) const {
1818 return cast<Expr>(I[idx]);
1819}
1820const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1821const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1822
1823//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001824// Child Iterators for iterating over subexpressions/substatements
1825//===----------------------------------------------------------------------===//
1826
1827// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001828Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1829Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001830
Steve Naroff7779db42007-11-12 14:29:37 +00001831// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001832Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1833Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001834
Steve Naroffe3e9add2008-06-02 23:03:37 +00001835// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001836Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1837Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001838
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001839// ObjCKVCRefExpr
1840Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1841Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1842
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001843// ObjCSuperExpr
1844Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1845Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1846
Chris Lattnerd9f69102008-08-10 01:53:14 +00001847// PredefinedExpr
1848Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1849Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001850
1851// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001852Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1853Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001854
1855// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001856Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001857Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001858
1859// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001860Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1861Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001862
Chris Lattner5d661452007-08-26 03:42:43 +00001863// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001864Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1865Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001866
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001867// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001868Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1869Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001870
1871// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001872Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1873Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001874
1875// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001876Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1877Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001878
Sebastian Redl05189992008-11-11 17:56:53 +00001879// SizeOfAlignOfExpr
1880Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1881 // If this is of a type and the type is a VLA type (and not a typedef), the
1882 // size expression of the VLA needs to be treated as an executable expression.
1883 // Why isn't this weirdness documented better in StmtIterator?
1884 if (isArgumentType()) {
1885 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1886 getArgumentType().getTypePtr()))
1887 return child_iterator(T);
1888 return child_iterator();
1889 }
Sebastian Redld4575892008-12-03 23:17:54 +00001890 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001891}
Sebastian Redl05189992008-11-11 17:56:53 +00001892Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1893 if (isArgumentType())
1894 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001895 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001896}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001897
1898// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001899Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001900 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001901}
Ted Kremenek1237c672007-08-24 20:06:47 +00001902Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001903 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001904}
1905
1906// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001907Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001908 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001909}
Ted Kremenek1237c672007-08-24 20:06:47 +00001910Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001911 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001912}
Ted Kremenek1237c672007-08-24 20:06:47 +00001913
1914// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001915Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1916Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001917
Nate Begeman213541a2008-04-18 23:10:10 +00001918// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001919Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1920Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001921
1922// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001923Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1924Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001925
Ted Kremenek1237c672007-08-24 20:06:47 +00001926// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001927Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1928Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001929
1930// BinaryOperator
1931Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001932 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001933}
Ted Kremenek1237c672007-08-24 20:06:47 +00001934Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001935 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001936}
1937
1938// ConditionalOperator
1939Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001940 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001941}
Ted Kremenek1237c672007-08-24 20:06:47 +00001942Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001943 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001944}
1945
1946// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001947Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1948Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001949
Ted Kremenek1237c672007-08-24 20:06:47 +00001950// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001951Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1952Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001953
1954// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001955Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1956 return child_iterator();
1957}
1958
1959Stmt::child_iterator TypesCompatibleExpr::child_end() {
1960 return child_iterator();
1961}
Ted Kremenek1237c672007-08-24 20:06:47 +00001962
1963// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001964Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1965Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001966
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001967// GNUNullExpr
1968Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1969Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1970
Eli Friedmand38617c2008-05-14 19:38:39 +00001971// ShuffleVectorExpr
1972Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001973 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001974}
1975Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001976 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001977}
1978
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001979// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001980Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1981Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001982
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001983// InitListExpr
1984Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001985 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001986}
1987Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001988 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001989}
1990
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001991// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001992Stmt::child_iterator DesignatedInitExpr::child_begin() {
1993 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1994 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001995 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1996}
1997Stmt::child_iterator DesignatedInitExpr::child_end() {
1998 return child_iterator(&*child_begin() + NumSubExprs);
1999}
2000
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002001// ImplicitValueInitExpr
2002Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2003 return child_iterator();
2004}
2005
2006Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2007 return child_iterator();
2008}
2009
Ted Kremenek1237c672007-08-24 20:06:47 +00002010// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002011Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002012 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002013}
2014Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002015 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002016}
Ted Kremenek1237c672007-08-24 20:06:47 +00002017
2018// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002019Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2020Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002021
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002022// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002023Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2024 return child_iterator();
2025}
2026Stmt::child_iterator ObjCSelectorExpr::child_end() {
2027 return child_iterator();
2028}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002029
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002030// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002031Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2032 return child_iterator();
2033}
2034Stmt::child_iterator ObjCProtocolExpr::child_end() {
2035 return child_iterator();
2036}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002037
Steve Naroff563477d2007-09-18 23:55:05 +00002038// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00002039Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002040 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002041}
2042Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002043 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002044}
2045
Steve Naroff4eb206b2008-09-03 18:15:37 +00002046// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002047Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2048Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002049
Ted Kremenek9da13f92008-09-26 23:24:14 +00002050Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2051Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }