blob: fbc8889567d35966ff168fc51fc116c923178e58 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000023#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Sebastian Redlf80e2612009-05-16 18:50:46 +000030PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
31 return new (C) PredefinedExpr(Loc, getType(), Type);
32}
33
Anders Carlsson7f2e7442009-03-15 18:34:13 +000034IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
35 return new (C) IntegerLiteral(Value, getType(), Loc);
36}
37
Sebastian Redlf80e2612009-05-16 18:50:46 +000038CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
39 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
40}
41
42FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
43 bool exact = IsExact;
44 return new (C) FloatingLiteral(Value, &exact, getType(), Loc);
45}
46
Douglas Gregor4a951342009-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 Redlf80e2612009-05-16 18:50:46 +000057GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
58 return new (C) GNUNullExpr(getType(), TokenLoc);
59}
60
Chris Lattnere0391b22008-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 Johannesen2461f612008-10-09 23:02:32 +000066 bool ignored;
67 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
68 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000069 return V.convertToDouble();
70}
71
Chris Lattneraa491192009-02-18 06:40:38 +000072StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
73 unsigned ByteLength, bool Wide,
74 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000075 const SourceLocation *Loc,
76 unsigned NumStrs) {
Chris Lattneraa491192009-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
Chris Lattner4b009652007-07-25 00:24:17 +000084 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-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;
Chris Lattner4b009652007-07-25 00:24:17 +000092
Chris Lattnerc3144742009-02-18 05:49:11 +000093 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000094 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
95 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000096}
97
Douglas Gregor596e0932009-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 Carlsson7f2e7442009-03-15 18:34:13 +0000109StringLiteral* StringLiteral::Clone(ASTContext &C) const {
110 return Create(C, StrData, ByteLength, IsWide, getType(),
111 TokLocs, NumConcatenated);
112}
Chris Lattnerc3144742009-02-18 05:49:11 +0000113
Ted Kremenek4f530a92009-02-06 19:55:15 +0000114void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000115 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +0000116 this->~StringLiteral();
117 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000118}
119
Douglas Gregor596e0932009-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
Chris Lattner4b009652007-07-25 00:24:17 +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";
Chris Lattner4b009652007-07-25 00:24:17 +0000147 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000148 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150}
151
Douglas Gregorc78182d2009-03-13 23:49:33 +0000152UnaryOperator::Opcode
153UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
154 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000155 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-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 Gregorc78182d2009-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
Chris Lattner4b009652007-07-25 00:24:17 +0000182//===----------------------------------------------------------------------===//
183// Postfix Operators.
184//===----------------------------------------------------------------------===//
185
Ted Kremenek362abcd2009-02-09 20:51:47 +0000186CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000187 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000188 : Expr(SC, t,
189 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000190 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000191 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000192
193 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-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 Kremenek362abcd2009-02-09 20:51:47 +0000197
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000198 RParenLoc = rparenloc;
199}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000200
Ted Kremenek362abcd2009-02-09 20:51:47 +0000201CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
202 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000203 : Expr(CallExprClass, t,
204 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000205 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000206 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000207
208 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000209 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000210 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000211 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000212
Chris Lattner4b009652007-07-25 00:24:17 +0000213 RParenLoc = rparenloc;
214}
215
Douglas Gregor7e2b1cd2009-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 Kremenek362abcd2009-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 Lattnerc257c0d2007-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 Kremenek0c97e042009-02-07 01:47:29 +0000231void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-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 Kremenek0c97e042009-02-07 01:47:29 +0000238 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-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 Kremenek2719e982008-06-17 02:43:46 +0000244 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-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 Gregorb9f63642009-04-17 21:46:47 +0000252 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000253 SubExprs = NewSubExprs;
254 this->NumArgs = NumArgs;
255}
256
Chris Lattnerc24915f2008-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 Gregorb5af7382009-02-14 18:57:46 +0000259unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-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 Lattnerc24915f2008-10-06 05:00:53 +0000265 return 0;
266
Steve Naroff44aec4c2008-01-31 01:07:12 +0000267 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
268 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000269 return 0;
270
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000271 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
272 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000273 return 0;
274
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000275 if (!FDecl->getIdentifier())
276 return 0;
277
Douglas Gregorb5af7382009-02-14 18:57:46 +0000278 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000279}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000280
Chris Lattnerc24915f2008-10-06 05:00:53 +0000281
Chris Lattner4b009652007-07-25 00:24:17 +0000282/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
283/// corresponds to, e.g. "<<=".
284const char *BinaryOperator::getOpcodeStr(Opcode Op) {
285 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000286 case PtrMemD: return ".*";
287 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000288 case Mul: return "*";
289 case Div: return "/";
290 case Rem: return "%";
291 case Add: return "+";
292 case Sub: return "-";
293 case Shl: return "<<";
294 case Shr: return ">>";
295 case LT: return "<";
296 case GT: return ">";
297 case LE: return "<=";
298 case GE: return ">=";
299 case EQ: return "==";
300 case NE: return "!=";
301 case And: return "&";
302 case Xor: return "^";
303 case Or: return "|";
304 case LAnd: return "&&";
305 case LOr: return "||";
306 case Assign: return "=";
307 case MulAssign: return "*=";
308 case DivAssign: return "/=";
309 case RemAssign: return "%=";
310 case AddAssign: return "+=";
311 case SubAssign: return "-=";
312 case ShlAssign: return "<<=";
313 case ShrAssign: return ">>=";
314 case AndAssign: return "&=";
315 case XorAssign: return "^=";
316 case OrAssign: return "|=";
317 case Comma: return ",";
318 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000319
320 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000321}
322
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000323BinaryOperator::Opcode
324BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
325 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000326 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000327 case OO_Plus: return Add;
328 case OO_Minus: return Sub;
329 case OO_Star: return Mul;
330 case OO_Slash: return Div;
331 case OO_Percent: return Rem;
332 case OO_Caret: return Xor;
333 case OO_Amp: return And;
334 case OO_Pipe: return Or;
335 case OO_Equal: return Assign;
336 case OO_Less: return LT;
337 case OO_Greater: return GT;
338 case OO_PlusEqual: return AddAssign;
339 case OO_MinusEqual: return SubAssign;
340 case OO_StarEqual: return MulAssign;
341 case OO_SlashEqual: return DivAssign;
342 case OO_PercentEqual: return RemAssign;
343 case OO_CaretEqual: return XorAssign;
344 case OO_AmpEqual: return AndAssign;
345 case OO_PipeEqual: return OrAssign;
346 case OO_LessLess: return Shl;
347 case OO_GreaterGreater: return Shr;
348 case OO_LessLessEqual: return ShlAssign;
349 case OO_GreaterGreaterEqual: return ShrAssign;
350 case OO_EqualEqual: return EQ;
351 case OO_ExclaimEqual: return NE;
352 case OO_LessEqual: return LE;
353 case OO_GreaterEqual: return GE;
354 case OO_AmpAmp: return LAnd;
355 case OO_PipePipe: return LOr;
356 case OO_Comma: return Comma;
357 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000358 }
359}
360
361OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
362 static const OverloadedOperatorKind OverOps[] = {
363 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
364 OO_Star, OO_Slash, OO_Percent,
365 OO_Plus, OO_Minus,
366 OO_LessLess, OO_GreaterGreater,
367 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
368 OO_EqualEqual, OO_ExclaimEqual,
369 OO_Amp,
370 OO_Caret,
371 OO_Pipe,
372 OO_AmpAmp,
373 OO_PipePipe,
374 OO_Equal, OO_StarEqual,
375 OO_SlashEqual, OO_PercentEqual,
376 OO_PlusEqual, OO_MinusEqual,
377 OO_LessLessEqual, OO_GreaterGreaterEqual,
378 OO_AmpEqual, OO_CaretEqual,
379 OO_PipeEqual,
380 OO_Comma
381 };
382 return OverOps[Opc];
383}
384
Anders Carlsson762b7c72007-08-31 04:56:16 +0000385InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000386 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000387 SourceLocation rbraceloc)
Douglas Gregor3a7a06e2009-05-21 23:17:49 +0000388 : Expr(InitListExprClass, QualType(),
389 hasAnyTypeDependentArguments(initExprs, numInits),
390 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor82462762009-01-29 16:53:55 +0000391 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000392 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000393
394 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000395}
Chris Lattner4b009652007-07-25 00:24:17 +0000396
Douglas Gregoree0792c2009-03-20 23:58:33 +0000397void InitListExpr::reserveInits(unsigned NumInits) {
398 if (NumInits > InitExprs.size())
399 InitExprs.reserve(NumInits);
400}
401
Douglas Gregorf603b472009-01-28 21:54:33 +0000402void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000403 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000404 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000405 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000406 InitExprs.resize(NumInits, 0);
407}
408
409Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
410 if (Init >= InitExprs.size()) {
411 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
412 InitExprs.back() = expr;
413 return 0;
414 }
415
416 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
417 InitExprs[Init] = expr;
418 return Result;
419}
420
Steve Naroff6f373332008-09-04 15:31:07 +0000421/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000422///
423const FunctionType *BlockExpr::getFunctionType() const {
424 return getType()->getAsBlockPointerType()->
425 getPointeeType()->getAsFunctionType();
426}
427
Steve Naroff9ac456d2008-10-08 17:01:13 +0000428SourceLocation BlockExpr::getCaretLocation() const {
429 return TheBlock->getCaretLocation();
430}
Douglas Gregore3241e92009-04-18 00:02:19 +0000431const Stmt *BlockExpr::getBody() const {
432 return TheBlock->getBody();
433}
434Stmt *BlockExpr::getBody() {
435 return TheBlock->getBody();
436}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000437
438
Chris Lattner4b009652007-07-25 00:24:17 +0000439//===----------------------------------------------------------------------===//
440// Generic Expression Routines
441//===----------------------------------------------------------------------===//
442
Chris Lattnerd2c66552009-02-14 07:37:35 +0000443/// isUnusedResultAWarning - Return true if this immediate expression should
444/// be warned about if the result is unused. If so, fill in Loc and Ranges
445/// with location to warn on and the source range[s] to report with the
446/// warning.
447bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
448 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000449 // Don't warn if the expr is type dependent. The type could end up
450 // instantiating to void.
451 if (isTypeDependent())
452 return false;
453
Chris Lattner4b009652007-07-25 00:24:17 +0000454 switch (getStmtClass()) {
455 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000456 Loc = getExprLoc();
457 R1 = getSourceRange();
458 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000459 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000460 return cast<ParenExpr>(this)->getSubExpr()->
461 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000462 case UnaryOperatorClass: {
463 const UnaryOperator *UO = cast<UnaryOperator>(this);
464
465 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000466 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000467 case UnaryOperator::PostInc:
468 case UnaryOperator::PostDec:
469 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000470 case UnaryOperator::PreDec: // ++/--
471 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000472 case UnaryOperator::Deref:
473 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000474 if (getType().isVolatileQualified())
475 return false;
476 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 case UnaryOperator::Real:
478 case UnaryOperator::Imag:
479 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000480 if (UO->getSubExpr()->getType().isVolatileQualified())
481 return false;
482 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000483 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000484 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000485 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000486 Loc = UO->getOperatorLoc();
487 R1 = UO->getSubExpr()->getSourceRange();
488 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000489 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000490 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000491 const BinaryOperator *BO = cast<BinaryOperator>(this);
492 // Consider comma to have side effects if the LHS or RHS does.
493 if (BO->getOpcode() == BinaryOperator::Comma)
494 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
495 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000496
Chris Lattnerd2c66552009-02-14 07:37:35 +0000497 if (BO->isAssignmentOp())
498 return false;
499 Loc = BO->getOperatorLoc();
500 R1 = BO->getLHS()->getSourceRange();
501 R2 = BO->getRHS()->getSourceRange();
502 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000503 }
Chris Lattner06078d22007-08-25 02:00:02 +0000504 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000505 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000506
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000507 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000508 // The condition must be evaluated, but if either the LHS or RHS is a
509 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000510 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000511 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000512 return true;
513 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000514 }
515
Chris Lattner4b009652007-07-25 00:24:17 +0000516 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000517 // If the base pointer or element is to a volatile pointer/field, accessing
518 // it is a side effect.
519 if (getType().isVolatileQualified())
520 return false;
521 Loc = cast<MemberExpr>(this)->getMemberLoc();
522 R1 = SourceRange(Loc, Loc);
523 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
524 return true;
525
Chris Lattner4b009652007-07-25 00:24:17 +0000526 case ArraySubscriptExprClass:
527 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000528 // it is a side effect.
529 if (getType().isVolatileQualified())
530 return false;
531 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
532 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
533 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
534 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000535
Chris Lattner4b009652007-07-25 00:24:17 +0000536 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000537 case CXXOperatorCallExprClass:
538 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000539 // If this is a direct call, get the callee.
540 const CallExpr *CE = cast<CallExpr>(this);
541 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
542 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
543 // If the callee has attribute pure, const, or warn_unused_result, warn
544 // about it. void foo() { strlen("bar"); } should warn.
545 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
546 if (FD->getAttr<WarnUnusedResultAttr>() ||
547 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
548 Loc = CE->getCallee()->getLocStart();
549 R1 = CE->getCallee()->getSourceRange();
550
551 if (unsigned NumArgs = CE->getNumArgs())
552 R2 = SourceRange(CE->getArg(0)->getLocStart(),
553 CE->getArg(NumArgs-1)->getLocEnd());
554 return true;
555 }
556 }
557 return false;
558 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000559 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000560 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000561 case StmtExprClass: {
562 // Statement exprs don't logically have side effects themselves, but are
563 // sometimes used in macros in ways that give them a type that is unused.
564 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
565 // however, if the result of the stmt expr is dead, we don't want to emit a
566 // warning.
567 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
568 if (!CS->body_empty())
569 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000570 return E->isUnusedResultAWarning(Loc, R1, R2);
571
572 Loc = cast<StmtExpr>(this)->getLParenLoc();
573 R1 = getSourceRange();
574 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000575 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000576 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000577 // If this is a cast to void, check the operand. Otherwise, the result of
578 // the cast is unused.
579 if (getType()->isVoidType())
580 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
581 R1, R2);
582 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
583 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
584 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000585 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000586 // If this is a cast to void, check the operand. Otherwise, the result of
587 // the cast is unused.
588 if (getType()->isVoidType())
Chris Lattnerd2c66552009-02-14 07:37:35 +0000589 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
590 R1, R2);
591 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
592 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
593 return true;
594
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000595 case ImplicitCastExprClass:
596 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000597 return cast<ImplicitCastExpr>(this)
598 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000599
Chris Lattner3e254fb2008-04-08 04:40:51 +0000600 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000601 return cast<CXXDefaultArgExpr>(this)
602 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000603
604 case CXXNewExprClass:
605 // FIXME: In theory, there might be new expressions that don't have side
606 // effects (e.g. a placement new with an uninitialized POD).
607 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000608 return false;
Anders Carlsson18ca4772009-05-17 21:11:30 +0000609 case CXXExprWithTemporariesClass:
610 return cast<CXXExprWithTemporaries>(this)
611 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000612 }
Chris Lattner4b009652007-07-25 00:24:17 +0000613}
614
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000615/// DeclCanBeLvalue - Determine whether the given declaration can be
616/// an lvalue. This is a helper routine for isLvalue.
617static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000618 // C++ [temp.param]p6:
619 // A non-type non-reference template-parameter is not an lvalue.
620 if (const NonTypeTemplateParmDecl *NTTParm
621 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
622 return NTTParm->getType()->isReferenceType();
623
Douglas Gregor8acb7272008-12-11 16:49:14 +0000624 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000625 // C++ 3.10p2: An lvalue refers to an object or function.
626 (Ctx.getLangOptions().CPlusPlus &&
627 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
628}
629
Chris Lattner4b009652007-07-25 00:24:17 +0000630/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
631/// incomplete type other than void. Nonarray expressions that can be lvalues:
632/// - name, where name must be a variable
633/// - e[i]
634/// - (e), where e must be an lvalue
635/// - e.name, where e must be an lvalue
636/// - e->name
637/// - *e, the type of e cannot be a function type
638/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000639/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000640/// - reference type [C++ [expr]]
641///
Chris Lattner25168a52008-07-26 21:30:36 +0000642Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000643 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
644
645 isLvalueResult Res = isLvalueInternal(Ctx);
646 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
647 return Res;
648
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000649 // first, check the type (C99 6.3.2.1). Expressions with function
650 // type in C are not lvalues, but they can be lvalues in C++.
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000651 if (TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000652 return LV_NotObjectType;
653
Steve Naroffec7736d2008-02-10 01:39:04 +0000654 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000655 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000656 return LV_IncompleteVoidType;
657
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000658 return LV_Valid;
659}
Chris Lattner4b009652007-07-25 00:24:17 +0000660
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000661// Check whether the expression can be sanely treated like an l-value
662Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000663 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000664 case StringLiteralClass: // C99 6.5.1p4
665 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000666 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000667 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
668 // For vectors, make sure base is an lvalue (i.e. not a function call).
669 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000670 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000671 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000672 case DeclRefExprClass:
673 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000674 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
675 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000676 return LV_Valid;
677 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000678 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000679 case BlockDeclRefExprClass: {
680 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000681 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000682 return LV_Valid;
683 break;
684 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000685 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000686 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000687 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
688 NamedDecl *Member = m->getMemberDecl();
689 // C++ [expr.ref]p4:
690 // If E2 is declared to have type "reference to T", then E1.E2
691 // is an lvalue.
692 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
693 if (Value->getType()->isReferenceType())
694 return LV_Valid;
695
696 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000697 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000698 return LV_Valid;
699
700 // -- If E2 is a non-static data member [...]. If E1 is an
701 // lvalue, then E1.E2 is an lvalue.
702 if (isa<FieldDecl>(Member))
703 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
704
705 // -- If it refers to a static member function [...], then
706 // E1.E2 is an lvalue.
707 // -- Otherwise, if E1.E2 refers to a non-static member
708 // function [...], then E1.E2 is not an lvalue.
709 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
710 return Method->isStatic()? LV_Valid : LV_MemberFunction;
711
712 // -- If E2 is a member enumerator [...], the expression E1.E2
713 // is not an lvalue.
714 if (isa<EnumConstantDecl>(Member))
715 return LV_InvalidExpression;
716
717 // Not an lvalue.
718 return LV_InvalidExpression;
719 }
720
721 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000722 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000723 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000724 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000725 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000726 return LV_Valid; // C99 6.5.3p4
727
728 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000729 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
730 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000731 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000732
733 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
734 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
735 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
736 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000737 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000738 case ImplicitCastExprClass:
739 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
740 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000741 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000742 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000743 case BinaryOperatorClass:
744 case CompoundAssignOperatorClass: {
745 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000746
747 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
748 BinOp->getOpcode() == BinaryOperator::Comma)
749 return BinOp->getRHS()->isLvalue(Ctx);
750
Sebastian Redl95216a62009-02-07 00:15:38 +0000751 // C++ [expr.mptr.oper]p6
752 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
753 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
754 !BinOp->getType()->isFunctionType())
755 return BinOp->getLHS()->isLvalue(Ctx);
756
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000757 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000758 return LV_InvalidExpression;
759
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000760 if (Ctx.getLangOptions().CPlusPlus)
761 // C++ [expr.ass]p1:
762 // The result of an assignment operation [...] is an lvalue.
763 return LV_Valid;
764
765
766 // C99 6.5.16:
767 // An assignment expression [...] is not an lvalue.
768 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000769 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000770 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000771 case CXXOperatorCallExprClass:
772 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000773 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000774 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000775 // is an lvalue reference.
Douglas Gregor81c29152008-10-29 00:13:59 +0000776 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000777 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000778 CalleeType = FnTypePtr->getPointeeType();
Anders Carlsson5edcc082009-05-26 02:03:20 +0000779 else if (const BlockPointerType *BPT = CalleeType->getAsBlockPointerType())
780 CalleeType = BPT->getPointeeType();
781
Douglas Gregor3257fb52008-12-22 05:46:06 +0000782 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000783 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000784 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000785
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000786 break;
787 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000788 case CompoundLiteralExprClass: // C99 6.5.2.5p5
789 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000790 case ChooseExprClass:
791 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000792 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000793 case ExtVectorElementExprClass:
794 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000795 return LV_DuplicateVectorComponents;
796 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000797 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
798 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000799 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
800 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000801 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000802 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000803 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000804 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000805 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000806 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000807 case CXXConditionDeclExprClass:
808 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000809 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000810 case CXXFunctionalCastExprClass:
811 case CXXStaticCastExprClass:
812 case CXXDynamicCastExprClass:
813 case CXXReinterpretCastExprClass:
814 case CXXConstCastExprClass:
815 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000816 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000817 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
818 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000819 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
820 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000821 return LV_Valid;
822 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000823 case CXXTypeidExprClass:
824 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
825 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000826 case ConditionalOperatorClass: {
827 // Complicated handling is only for C++.
828 if (!Ctx.getLangOptions().CPlusPlus)
829 return LV_InvalidExpression;
830
831 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
832 // everywhere there's an object converted to an rvalue. Also, any other
833 // casts should be wrapped by ImplicitCastExprs. There's just the special
834 // case involving throws to work out.
835 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000836 Expr *True = Cond->getTrueExpr();
837 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000838 // C++0x 5.16p2
839 // If either the second or the third operand has type (cv) void, [...]
840 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000841 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000842 return LV_InvalidExpression;
843
844 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000845 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000846 return LV_InvalidExpression;
847
848 // That's it.
849 return LV_Valid;
850 }
851
Chris Lattner4b009652007-07-25 00:24:17 +0000852 default:
853 break;
854 }
855 return LV_InvalidExpression;
856}
857
858/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
859/// does not have an incomplete type, does not have a const-qualified type, and
860/// if it is a structure or union, does not have any member (including,
861/// recursively, any member or element of all contained aggregates or unions)
862/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000863Expr::isModifiableLvalueResult
864Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000865 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000866
867 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000868 case LV_Valid:
869 // C++ 3.10p11: Functions cannot be modified, but pointers to
870 // functions can be modifiable.
871 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
872 return MLV_NotObjectType;
873 break;
874
Chris Lattner4b009652007-07-25 00:24:17 +0000875 case LV_NotObjectType: return MLV_NotObjectType;
876 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000877 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000878 case LV_InvalidExpression:
879 // If the top level is a C-style cast, and the subexpression is a valid
880 // lvalue, then this is probably a use of the old-school "cast as lvalue"
881 // GCC extension. We don't support it, but we want to produce good
882 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000883 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
884 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
885 if (Loc)
886 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000887 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000888 }
889 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000890 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000891 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000892 }
Eli Friedman91571da2009-03-22 23:26:56 +0000893
894 // The following is illegal:
895 // void takeclosure(void (^C)(void));
896 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
897 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000898 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000899 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
900 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
901 return MLV_NotBlockQualified;
902 }
903
Chris Lattnera1923f62008-08-04 07:31:14 +0000904 QualType CT = Ctx.getCanonicalType(getType());
905
906 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000907 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000908 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000909 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000910 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000911 return MLV_IncompleteType;
912
Chris Lattnera1923f62008-08-04 07:31:14 +0000913 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000914 if (r->hasConstFields())
915 return MLV_ConstQualified;
916 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000917
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000918 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000919 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000920 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
921 if (KVCExpr->getSetterMethod() == 0)
922 return MLV_NoSetterProperty;
923 }
Chris Lattner4b009652007-07-25 00:24:17 +0000924 return MLV_Valid;
925}
926
Ted Kremenek5778d622008-02-27 18:39:48 +0000927/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000928/// duration. This means that the address of this expression is a link-time
929/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000930bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000931 switch (getStmtClass()) {
932 default:
933 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000934 case BlockExprClass:
935 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000936 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000937 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000938 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000939 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000940 case CompoundLiteralExprClass:
941 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000942 case DeclRefExprClass:
943 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000944 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
945 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000946 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000947 if (isa<FunctionDecl>(D))
948 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000949 return false;
950 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000951 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000952 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000953 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000954 }
Chris Lattner743ec372007-11-27 21:35:27 +0000955 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000956 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000957 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000958 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000959 case CXXDefaultArgExprClass:
960 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000961 }
962}
963
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000964/// isOBJCGCCandidate - Check if an expression is objc gc'able.
965///
966bool Expr::isOBJCGCCandidate() const {
967 switch (getStmtClass()) {
968 default:
969 return false;
970 case ObjCIvarRefExprClass:
971 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000972 case Expr::UnaryOperatorClass:
973 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000974 case ParenExprClass:
975 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
976 case ImplicitCastExprClass:
977 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000978 case CStyleCastExprClass:
979 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000980 case DeclRefExprClass:
981 case QualifiedDeclRefExprClass: {
982 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
983 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
984 return VD->hasGlobalStorage();
985 return false;
986 }
987 case MemberExprClass: {
988 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000989 return M->getBase()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000990 }
991 case ArraySubscriptExprClass:
992 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
993 }
994}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000995Expr* Expr::IgnoreParens() {
996 Expr* E = this;
997 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
998 E = P->getSubExpr();
999
1000 return E;
1001}
1002
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001003/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1004/// or CastExprs or ImplicitCastExprs, returning their operand.
1005Expr *Expr::IgnoreParenCasts() {
1006 Expr *E = this;
1007 while (true) {
1008 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1009 E = P->getSubExpr();
1010 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1011 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001012 else
1013 return E;
1014 }
1015}
1016
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001017/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1018/// value (including ptr->int casts of the same size). Strip off any
1019/// ParenExpr or CastExprs, returning their operand.
1020Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1021 Expr *E = this;
1022 while (true) {
1023 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1024 E = P->getSubExpr();
1025 continue;
1026 }
1027
1028 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1029 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1030 // ptr<->int casts of the same width. We also ignore all identify casts.
1031 Expr *SE = P->getSubExpr();
1032
1033 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1034 E = SE;
1035 continue;
1036 }
1037
1038 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1039 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1040 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1041 E = SE;
1042 continue;
1043 }
1044 }
1045
1046 return E;
1047 }
1048}
1049
1050
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001051/// hasAnyTypeDependentArguments - Determines if any of the expressions
1052/// in Exprs is type-dependent.
1053bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1054 for (unsigned I = 0; I < NumExprs; ++I)
1055 if (Exprs[I]->isTypeDependent())
1056 return true;
1057
1058 return false;
1059}
1060
1061/// hasAnyValueDependentArguments - Determines if any of the expressions
1062/// in Exprs is value-dependent.
1063bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1064 for (unsigned I = 0; I < NumExprs; ++I)
1065 if (Exprs[I]->isValueDependent())
1066 return true;
1067
1068 return false;
1069}
1070
Eli Friedmandee41122009-01-25 02:32:41 +00001071bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001072 // This function is attempting whether an expression is an initializer
1073 // which can be evaluated at compile-time. isEvaluatable handles most
1074 // of the cases, but it can't deal with some initializer-specific
1075 // expressions, and it can't deal with aggregates; we deal with those here,
1076 // and fall back to isEvaluatable for the other cases.
1077
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001078 // FIXME: This function assumes the variable being assigned to
1079 // isn't a reference type!
1080
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001081 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001082 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001083 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001084 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001085 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001086 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001087 // This handles gcc's extension that allows global initializers like
1088 // "struct x {int x;} x = (struct x) {};".
1089 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001090 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001091 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001092 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001093 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001094 // FIXME: This doesn't deal with fields with reference types correctly.
1095 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1096 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001097 const InitListExpr *Exp = cast<InitListExpr>(this);
1098 unsigned numInits = Exp->getNumInits();
1099 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001100 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001101 return false;
1102 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001103 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001104 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001105 case ImplicitValueInitExprClass:
1106 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001107 case ParenExprClass: {
1108 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1109 }
1110 case UnaryOperatorClass: {
1111 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1112 if (Exp->getOpcode() == UnaryOperator::Extension)
1113 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1114 break;
1115 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001116 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001117 case CStyleCastExprClass:
1118 // Handle casts with a destination that's a struct or union; this
1119 // deals with both the gcc no-op struct cast extension and the
1120 // cast-to-union extension.
1121 if (getType()->isRecordType())
1122 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1123 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001124 }
1125
Eli Friedman2b0dec52009-01-25 03:12:18 +00001126 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001127}
1128
Chris Lattner4b009652007-07-25 00:24:17 +00001129/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001130/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001131
1132/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1133/// comma, etc
1134///
Chris Lattner4b009652007-07-25 00:24:17 +00001135/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1136/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1137/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001138
Eli Friedman7beeda62009-02-26 09:29:13 +00001139// CheckICE - This function does the fundamental ICE checking: the returned
1140// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1141// Note that to reduce code duplication, this helper does no evaluation
1142// itself; the caller checks whether the expression is evaluatable, and
1143// in the rare cases where CheckICE actually cares about the evaluated
1144// value, it calls into Evalute.
1145//
1146// Meanings of Val:
1147// 0: This expression is an ICE if it can be evaluated by Evaluate.
1148// 1: This expression is not an ICE, but if it isn't evaluated, it's
1149// a legal subexpression for an ICE. This return value is used to handle
1150// the comma operator in C99 mode.
1151// 2: This expression is not an ICE, and is not a legal subexpression for one.
1152
1153struct ICEDiag {
1154 unsigned Val;
1155 SourceLocation Loc;
1156
1157 public:
1158 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1159 ICEDiag() : Val(0) {}
1160};
1161
1162ICEDiag NoDiag() { return ICEDiag(); }
1163
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001164static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1165 Expr::EvalResult EVResult;
1166 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1167 !EVResult.Val.isInt()) {
1168 return ICEDiag(2, E->getLocStart());
1169 }
1170 return NoDiag();
1171}
1172
Eli Friedman7beeda62009-02-26 09:29:13 +00001173static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001174 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001175 if (!E->getType()->isIntegralType()) {
1176 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001177 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001178
1179 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001180 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001181 return ICEDiag(2, E->getLocStart());
1182 case Expr::ParenExprClass:
1183 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1184 case Expr::IntegerLiteralClass:
1185 case Expr::CharacterLiteralClass:
1186 case Expr::CXXBoolLiteralExprClass:
1187 case Expr::CXXZeroInitValueExprClass:
1188 case Expr::TypesCompatibleExprClass:
1189 case Expr::UnaryTypeTraitExprClass:
1190 return NoDiag();
1191 case Expr::CallExprClass:
1192 case Expr::CXXOperatorCallExprClass: {
1193 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001194 if (CE->isBuiltinCall(Ctx))
1195 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001196 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001197 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001198 case Expr::DeclRefExprClass:
1199 case Expr::QualifiedDeclRefExprClass:
1200 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1201 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001202 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001203 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001204 // C++ 7.1.5.1p2
1205 // A variable of non-volatile const-qualified integral or enumeration
1206 // type initialized by an ICE can be used in ICEs.
1207 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001208 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001209 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001210 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001211 }
1212 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001213 return ICEDiag(2, E->getLocStart());
1214 case Expr::UnaryOperatorClass: {
1215 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001216 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001217 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001218 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001219 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001220 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001221 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001222 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001223 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001224 case UnaryOperator::Real:
1225 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001226 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001227 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001228 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1229 // Evaluate matches the proposed gcc behavior for cases like
1230 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1231 // compliance: we should warn earlier for offsetof expressions with
1232 // array subscripts that aren't ICEs, and if the array subscripts
1233 // are ICEs, the value of the offsetof must be an integer constant.
1234 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001235 }
Chris Lattner4b009652007-07-25 00:24:17 +00001236 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001237 case Expr::SizeOfAlignOfExprClass: {
1238 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1239 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1240 return ICEDiag(2, E->getLocStart());
1241 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001242 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001243 case Expr::BinaryOperatorClass: {
1244 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001245 switch (Exp->getOpcode()) {
1246 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001247 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001248 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001249 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001250 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001251 case BinaryOperator::Add:
1252 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001253 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001254 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 case BinaryOperator::LT:
1256 case BinaryOperator::GT:
1257 case BinaryOperator::LE:
1258 case BinaryOperator::GE:
1259 case BinaryOperator::EQ:
1260 case BinaryOperator::NE:
1261 case BinaryOperator::And:
1262 case BinaryOperator::Xor:
1263 case BinaryOperator::Or:
1264 case BinaryOperator::Comma: {
1265 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1266 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001267 if (Exp->getOpcode() == BinaryOperator::Div ||
1268 Exp->getOpcode() == BinaryOperator::Rem) {
1269 // Evaluate gives an error for undefined Div/Rem, so make sure
1270 // we don't evaluate one.
1271 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1272 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1273 if (REval == 0)
1274 return ICEDiag(1, E->getLocStart());
1275 if (REval.isSigned() && REval.isAllOnesValue()) {
1276 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1277 if (LEval.isMinSignedValue())
1278 return ICEDiag(1, E->getLocStart());
1279 }
1280 }
1281 }
1282 if (Exp->getOpcode() == BinaryOperator::Comma) {
1283 if (Ctx.getLangOptions().C99) {
1284 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1285 // if it isn't evaluated.
1286 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1287 return ICEDiag(1, E->getLocStart());
1288 } else {
1289 // In both C89 and C++, commas in ICEs are illegal.
1290 return ICEDiag(2, E->getLocStart());
1291 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001292 }
1293 if (LHSResult.Val >= RHSResult.Val)
1294 return LHSResult;
1295 return RHSResult;
1296 }
Chris Lattner4b009652007-07-25 00:24:17 +00001297 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001298 case BinaryOperator::LOr: {
1299 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1300 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1301 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1302 // Rare case where the RHS has a comma "side-effect"; we need
1303 // to actually check the condition to see whether the side
1304 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001305 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001306 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001307 return RHSResult;
1308 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001309 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001310
Eli Friedman7beeda62009-02-26 09:29:13 +00001311 if (LHSResult.Val >= RHSResult.Val)
1312 return LHSResult;
1313 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001314 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001315 }
Chris Lattner4b009652007-07-25 00:24:17 +00001316 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001317 case Expr::ImplicitCastExprClass:
1318 case Expr::CStyleCastExprClass:
1319 case Expr::CXXFunctionalCastExprClass: {
1320 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1321 if (SubExpr->getType()->isIntegralType())
1322 return CheckICE(SubExpr, Ctx);
1323 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1324 return NoDiag();
1325 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001326 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001327 case Expr::ConditionalOperatorClass: {
1328 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001329 // If the condition (ignoring parens) is a __builtin_constant_p call,
1330 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001331 // expression, and it is fully evaluated. This is an important GNU
1332 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001333 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001334 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001335 Expr::EvalResult EVResult;
1336 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1337 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001338 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001339 }
1340 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001341 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001342 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1343 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1344 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1345 if (CondResult.Val == 2)
1346 return CondResult;
1347 if (TrueResult.Val == 2)
1348 return TrueResult;
1349 if (FalseResult.Val == 2)
1350 return FalseResult;
1351 if (CondResult.Val == 1)
1352 return CondResult;
1353 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1354 return NoDiag();
1355 // Rare case where the diagnostics depend on which side is evaluated
1356 // Note that if we get here, CondResult is 0, and at least one of
1357 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001358 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001359 return FalseResult;
1360 }
1361 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001362 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001363 case Expr::CXXDefaultArgExprClass:
1364 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001365 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001366 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001367 }
Chris Lattner4b009652007-07-25 00:24:17 +00001368 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001369}
Chris Lattner4b009652007-07-25 00:24:17 +00001370
Eli Friedman7beeda62009-02-26 09:29:13 +00001371bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1372 SourceLocation *Loc, bool isEvaluated) const {
1373 ICEDiag d = CheckICE(this, Ctx);
1374 if (d.Val != 0) {
1375 if (Loc) *Loc = d.Loc;
1376 return false;
1377 }
1378 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001379 if (!Evaluate(EvalResult, Ctx))
1380 assert(0 && "ICE cannot be evaluated!");
1381 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1382 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001383 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001384 return true;
1385}
1386
Chris Lattner4b009652007-07-25 00:24:17 +00001387/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1388/// integer constant expression with the value zero, or if this is one that is
1389/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001390bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1391{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001392 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001393 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001394 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001395 // Check that it is a cast to void*.
1396 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1397 QualType Pointee = PT->getPointeeType();
1398 if (Pointee.getCVRQualifiers() == 0 &&
1399 Pointee->isVoidType() && // to void*
1400 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001401 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001402 }
Chris Lattner4b009652007-07-25 00:24:17 +00001403 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001404 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1405 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001406 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001407 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1408 // Accept ((void*)0) as a null pointer constant, as many other
1409 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001410 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001411 } else if (const CXXDefaultArgExpr *DefaultArg
1412 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001413 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001414 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001415 } else if (isa<GNUNullExpr>(this)) {
1416 // The GNU __null extension is always a null pointer constant.
1417 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001418 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001419
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001420 // C++0x nullptr_t is always a null pointer constant.
1421 if (getType()->isNullPtrType())
1422 return true;
1423
Steve Naroffa2e53222008-01-14 16:10:57 +00001424 // This expression must be an integer type.
1425 if (!getType()->isIntegerType())
1426 return false;
1427
Chris Lattner4b009652007-07-25 00:24:17 +00001428 // If we have an integer constant expression, we need to *evaluate* it and
1429 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001430 llvm::APSInt Result;
1431 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001432}
Steve Naroffc11705f2007-07-28 23:10:27 +00001433
Douglas Gregor531434b2009-05-02 02:18:30 +00001434FieldDecl *Expr::getBitField() {
Douglas Gregor81c29152008-10-29 00:13:59 +00001435 Expr *E = this->IgnoreParenCasts();
Douglas Gregor531434b2009-05-02 02:18:30 +00001436
Douglas Gregor81c29152008-10-29 00:13:59 +00001437 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001438 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001439 if (Field->isBitField())
1440 return Field;
1441
1442 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1443 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1444 return BinOp->getLHS()->getBitField();
1445
1446 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001447}
1448
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001449/// isArrow - Return true if the base expression is a pointer to vector,
1450/// return false if the base expression is a vector.
1451bool ExtVectorElementExpr::isArrow() const {
1452 return getBase()->getType()->isPointerType();
1453}
1454
Nate Begemanaf6ed502008-04-18 23:10:10 +00001455unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001456 if (const VectorType *VT = getType()->getAsVectorType())
1457 return VT->getNumElements();
1458 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001459}
1460
Nate Begemanc8e51f82008-05-09 06:41:27 +00001461/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001462bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001463 const char *compStr = Accessor->getName();
1464 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001465
1466 // Halving swizzles do not contain duplicate elements.
1467 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1468 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1469 return false;
1470
1471 // Advance past s-char prefix on hex swizzles.
1472 if (*compStr == 's') {
1473 compStr++;
1474 length--;
1475 }
Steve Naroffba67f692007-07-30 03:29:09 +00001476
Chris Lattner58d3fa52008-11-19 07:55:04 +00001477 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001478 const char *s = compStr+i;
1479 for (const char c = *s++; *s; s++)
1480 if (c == *s)
1481 return true;
1482 }
1483 return false;
1484}
Chris Lattner42158e72007-08-02 23:36:59 +00001485
Nate Begemanc8e51f82008-05-09 06:41:27 +00001486/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001487void ExtVectorElementExpr::getEncodedElementAccess(
1488 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001489 const char *compStr = Accessor->getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001490 if (*compStr == 's')
1491 compStr++;
1492
1493 bool isHi = !strcmp(compStr, "hi");
1494 bool isLo = !strcmp(compStr, "lo");
1495 bool isEven = !strcmp(compStr, "even");
1496 bool isOdd = !strcmp(compStr, "odd");
1497
Nate Begemanc8e51f82008-05-09 06:41:27 +00001498 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1499 uint64_t Index;
1500
1501 if (isHi)
1502 Index = e + i;
1503 else if (isLo)
1504 Index = i;
1505 else if (isEven)
1506 Index = 2 * i;
1507 else if (isOdd)
1508 Index = 2 * i + 1;
1509 else
1510 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001511
Nate Begemana1ae7442008-05-13 21:03:02 +00001512 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001513 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001514}
1515
Steve Naroff4ed9d662007-09-27 14:38:14 +00001516// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001517ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001518 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001519 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001520 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001521 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001522 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001523 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001524 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001525 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001526 if (NumArgs) {
1527 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001528 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1529 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001530 LBracloc = LBrac;
1531 RBracloc = RBrac;
1532}
1533
Steve Naroff4ed9d662007-09-27 14:38:14 +00001534// constructor for class messages.
1535// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001536ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001537 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001538 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001539 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001540 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001541 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001542 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001543 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001544 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001545 if (NumArgs) {
1546 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001547 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1548 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001549 LBracloc = LBrac;
1550 RBracloc = RBrac;
1551}
1552
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001553// constructor for class messages.
1554ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1555 QualType retType, ObjCMethodDecl *mproto,
1556 SourceLocation LBrac, SourceLocation RBrac,
1557 Expr **ArgExprs, unsigned nargs)
1558: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1559MethodProto(mproto) {
1560 NumArgs = nargs;
1561 SubExprs = new Stmt*[NumArgs+1];
1562 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1563 if (NumArgs) {
1564 for (unsigned i = 0; i != NumArgs; ++i)
1565 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1566 }
1567 LBracloc = LBrac;
1568 RBracloc = RBrac;
1569}
1570
1571ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1572 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1573 switch (x & Flags) {
1574 default:
1575 assert(false && "Invalid ObjCMessageExpr.");
1576 case IsInstMeth:
1577 return ClassInfo(0, 0);
1578 case IsClsMethDeclUnknown:
1579 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1580 case IsClsMethDeclKnown: {
1581 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1582 return ClassInfo(D, D->getIdentifier());
1583 }
1584 }
1585}
1586
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001587void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1588 if (CI.first == 0 && CI.second == 0)
1589 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1590 else if (CI.first == 0)
1591 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1592 else
1593 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1594}
1595
1596
Chris Lattnerf624cd22007-10-25 00:29:32 +00001597bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001598 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001599}
1600
Douglas Gregor725e94b2009-04-16 00:01:45 +00001601void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1602 if (NumExprs)
1603 delete [] SubExprs;
1604
1605 SubExprs = new Stmt* [NumExprs];
1606 this->NumExprs = NumExprs;
1607 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1608}
1609
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001610void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1611 // Override default behavior of traversing children. If this has a type
1612 // operand and the type is a variable-length array, the child iteration
1613 // will iterate over the size expression. However, this expression belongs
1614 // to the type, not to this, so we don't want to delete it.
1615 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001616 if (isArgumentType()) {
1617 this->~SizeOfAlignOfExpr();
1618 C.Deallocate(this);
1619 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001620 else
1621 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001622}
1623
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001624//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001625// DesignatedInitExpr
1626//===----------------------------------------------------------------------===//
1627
1628IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1629 assert(Kind == FieldDesignator && "Only valid on a field designator");
1630 if (Field.NameOrField & 0x01)
1631 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1632 else
1633 return getField()->getIdentifier();
1634}
1635
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001636DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1637 const Designator *Designators,
1638 SourceLocation EqualOrColonLoc,
1639 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001640 Expr **IndexExprs,
1641 unsigned NumIndexExprs,
1642 Expr *Init)
1643 : Expr(DesignatedInitExprClass, Ty,
1644 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001645 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001646 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001647 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001648
1649 // Record the initializer itself.
1650 child_iterator Child = child_begin();
1651 *Child++ = Init;
1652
1653 // Copy the designators and their subexpressions, computing
1654 // value-dependence along the way.
1655 unsigned IndexIdx = 0;
1656 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001657 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001658
1659 if (this->Designators[I].isArrayDesignator()) {
1660 // Compute type- and value-dependence.
1661 Expr *Index = IndexExprs[IndexIdx];
1662 ValueDependent = ValueDependent ||
1663 Index->isTypeDependent() || Index->isValueDependent();
1664
1665 // Copy the index expressions into permanent storage.
1666 *Child++ = IndexExprs[IndexIdx++];
1667 } else if (this->Designators[I].isArrayRangeDesignator()) {
1668 // Compute type- and value-dependence.
1669 Expr *Start = IndexExprs[IndexIdx];
1670 Expr *End = IndexExprs[IndexIdx + 1];
1671 ValueDependent = ValueDependent ||
1672 Start->isTypeDependent() || Start->isValueDependent() ||
1673 End->isTypeDependent() || End->isValueDependent();
1674
1675 // Copy the start/end expressions into permanent storage.
1676 *Child++ = IndexExprs[IndexIdx++];
1677 *Child++ = IndexExprs[IndexIdx++];
1678 }
1679 }
1680
1681 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001682}
1683
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001684DesignatedInitExpr *
1685DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1686 unsigned NumDesignators,
1687 Expr **IndexExprs, unsigned NumIndexExprs,
1688 SourceLocation ColonOrEqualLoc,
1689 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001690 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001691 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001692 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1693 ColonOrEqualLoc, UsesColonSyntax,
1694 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001695}
1696
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001697DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1698 unsigned NumIndexExprs) {
1699 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1700 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1701 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1702}
1703
1704void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1705 unsigned NumDesigs) {
1706 if (Designators)
1707 delete [] Designators;
1708
1709 Designators = new Designator[NumDesigs];
1710 NumDesignators = NumDesigs;
1711 for (unsigned I = 0; I != NumDesigs; ++I)
1712 Designators[I] = Desigs[I];
1713}
1714
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001715SourceRange DesignatedInitExpr::getSourceRange() const {
1716 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001717 Designator &First =
1718 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001719 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001720 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001721 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1722 else
1723 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1724 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001725 StartLoc =
1726 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001727 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1728}
1729
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001730Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1731 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1732 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1733 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001734 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1735 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1736}
1737
1738Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1739 assert(D.Kind == Designator::ArrayRangeDesignator &&
1740 "Requires array range designator");
1741 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1742 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001743 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1744 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1745}
1746
1747Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1748 assert(D.Kind == Designator::ArrayRangeDesignator &&
1749 "Requires array range designator");
1750 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1751 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001752 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1753 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1754}
1755
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001756/// \brief Replaces the designator at index @p Idx with the series
1757/// of designators in [First, Last).
1758void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1759 const Designator *First,
1760 const Designator *Last) {
1761 unsigned NumNewDesignators = Last - First;
1762 if (NumNewDesignators == 0) {
1763 std::copy_backward(Designators + Idx + 1,
1764 Designators + NumDesignators,
1765 Designators + Idx);
1766 --NumNewDesignators;
1767 return;
1768 } else if (NumNewDesignators == 1) {
1769 Designators[Idx] = *First;
1770 return;
1771 }
1772
1773 Designator *NewDesignators
1774 = new Designator[NumDesignators - 1 + NumNewDesignators];
1775 std::copy(Designators, Designators + Idx, NewDesignators);
1776 std::copy(First, Last, NewDesignators + Idx);
1777 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1778 NewDesignators + Idx + NumNewDesignators);
1779 delete [] Designators;
1780 Designators = NewDesignators;
1781 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1782}
1783
1784void DesignatedInitExpr::Destroy(ASTContext &C) {
1785 delete [] Designators;
1786 Expr::Destroy(C);
1787}
1788
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001789ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1790 return new (C) ImplicitValueInitExpr(getType());
1791}
1792
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001793//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001794// ExprIterator.
1795//===----------------------------------------------------------------------===//
1796
1797Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1798Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1799Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1800const Expr* ConstExprIterator::operator[](size_t idx) const {
1801 return cast<Expr>(I[idx]);
1802}
1803const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1804const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1805
1806//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001807// Child Iterators for iterating over subexpressions/substatements
1808//===----------------------------------------------------------------------===//
1809
1810// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001811Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1812Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001813
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001814// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001815Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1816Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001817
Steve Naroff6f786252008-06-02 23:03:37 +00001818// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001819Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1820Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001821
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001822// ObjCKVCRefExpr
1823Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1824Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1825
Douglas Gregord8606632008-11-04 14:56:14 +00001826// ObjCSuperExpr
1827Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1828Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1829
Chris Lattner69909292008-08-10 01:53:14 +00001830// PredefinedExpr
1831Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1832Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001833
1834// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001835Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1836Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001837
1838// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001839Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001840Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001841
1842// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001843Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1844Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001845
Chris Lattner1de66eb2007-08-26 03:42:43 +00001846// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001847Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1848Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001849
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001850// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001851Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1852Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001853
1854// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001855Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1856Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001857
1858// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001859Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1860Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001861
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001862// SizeOfAlignOfExpr
1863Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1864 // If this is of a type and the type is a VLA type (and not a typedef), the
1865 // size expression of the VLA needs to be treated as an executable expression.
1866 // Why isn't this weirdness documented better in StmtIterator?
1867 if (isArgumentType()) {
1868 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1869 getArgumentType().getTypePtr()))
1870 return child_iterator(T);
1871 return child_iterator();
1872 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001873 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001874}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001875Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1876 if (isArgumentType())
1877 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001878 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001879}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001880
1881// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001882Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001883 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001884}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001885Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001886 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001887}
1888
1889// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001890Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001891 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001892}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001893Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001894 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001895}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001896
1897// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001898Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1899Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001900
Nate Begemanaf6ed502008-04-18 23:10:10 +00001901// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001902Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1903Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001904
1905// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001906Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1907Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001908
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001909// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001910Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1911Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001912
1913// BinaryOperator
1914Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001915 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001916}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001917Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001918 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001919}
1920
1921// ConditionalOperator
1922Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001923 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001924}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001925Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001926 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001927}
1928
1929// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001930Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1931Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001932
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001933// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001934Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1935Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001936
1937// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001938Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1939 return child_iterator();
1940}
1941
1942Stmt::child_iterator TypesCompatibleExpr::child_end() {
1943 return child_iterator();
1944}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001945
1946// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001947Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1948Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001949
Douglas Gregorad4b3792008-11-29 04:51:27 +00001950// GNUNullExpr
1951Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1952Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1953
Eli Friedmand0e9d092008-05-14 19:38:39 +00001954// ShuffleVectorExpr
1955Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001956 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001957}
1958Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001959 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001960}
1961
Anders Carlsson36760332007-10-15 20:28:48 +00001962// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001963Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1964Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001965
Anders Carlsson762b7c72007-08-31 04:56:16 +00001966// InitListExpr
1967Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001968 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001969}
1970Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001971 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001972}
1973
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001974// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001975Stmt::child_iterator DesignatedInitExpr::child_begin() {
1976 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1977 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001978 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1979}
1980Stmt::child_iterator DesignatedInitExpr::child_end() {
1981 return child_iterator(&*child_begin() + NumSubExprs);
1982}
1983
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001984// ImplicitValueInitExpr
1985Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1986 return child_iterator();
1987}
1988
1989Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1990 return child_iterator();
1991}
1992
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001993// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001994Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001995 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001996}
1997Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001998 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001999}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002000
2001// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002002Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2003Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002004
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002005// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002006Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2007 return child_iterator();
2008}
2009Stmt::child_iterator ObjCSelectorExpr::child_end() {
2010 return child_iterator();
2011}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002012
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002013// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002014Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2015 return child_iterator();
2016}
2017Stmt::child_iterator ObjCProtocolExpr::child_end() {
2018 return child_iterator();
2019}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002020
Steve Naroffc39ca262007-09-18 23:55:05 +00002021// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002022Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002023 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002024}
2025Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002026 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002027}
2028
Steve Naroff52a81c02008-09-03 18:15:37 +00002029// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002030Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2031Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002032
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002033Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2034Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }