blob: 81936efb08612774154523d7561cd9a6f606015d [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();
779 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redlce6fff02009-03-16 23:22:08 +0000780 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor3257fb52008-12-22 05:46:06 +0000781 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000782
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000783 break;
784 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000785 case CompoundLiteralExprClass: // C99 6.5.2.5p5
786 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000787 case ChooseExprClass:
788 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000789 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000790 case ExtVectorElementExprClass:
791 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000792 return LV_DuplicateVectorComponents;
793 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000794 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
795 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000796 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
797 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000798 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000799 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000800 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000801 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000802 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000803 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000804 case CXXConditionDeclExprClass:
805 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000806 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000807 case CXXFunctionalCastExprClass:
808 case CXXStaticCastExprClass:
809 case CXXDynamicCastExprClass:
810 case CXXReinterpretCastExprClass:
811 case CXXConstCastExprClass:
812 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000813 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000814 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
815 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000816 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
817 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000818 return LV_Valid;
819 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000820 case CXXTypeidExprClass:
821 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
822 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000823 case ConditionalOperatorClass: {
824 // Complicated handling is only for C++.
825 if (!Ctx.getLangOptions().CPlusPlus)
826 return LV_InvalidExpression;
827
828 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
829 // everywhere there's an object converted to an rvalue. Also, any other
830 // casts should be wrapped by ImplicitCastExprs. There's just the special
831 // case involving throws to work out.
832 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000833 Expr *True = Cond->getTrueExpr();
834 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000835 // C++0x 5.16p2
836 // If either the second or the third operand has type (cv) void, [...]
837 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000838 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000839 return LV_InvalidExpression;
840
841 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000842 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000843 return LV_InvalidExpression;
844
845 // That's it.
846 return LV_Valid;
847 }
848
Chris Lattner4b009652007-07-25 00:24:17 +0000849 default:
850 break;
851 }
852 return LV_InvalidExpression;
853}
854
855/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
856/// does not have an incomplete type, does not have a const-qualified type, and
857/// if it is a structure or union, does not have any member (including,
858/// recursively, any member or element of all contained aggregates or unions)
859/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000860Expr::isModifiableLvalueResult
861Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000862 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000863
864 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000865 case LV_Valid:
866 // C++ 3.10p11: Functions cannot be modified, but pointers to
867 // functions can be modifiable.
868 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
869 return MLV_NotObjectType;
870 break;
871
Chris Lattner4b009652007-07-25 00:24:17 +0000872 case LV_NotObjectType: return MLV_NotObjectType;
873 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000874 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000875 case LV_InvalidExpression:
876 // If the top level is a C-style cast, and the subexpression is a valid
877 // lvalue, then this is probably a use of the old-school "cast as lvalue"
878 // GCC extension. We don't support it, but we want to produce good
879 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000880 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
881 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
882 if (Loc)
883 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000884 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000885 }
886 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000887 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000888 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000889 }
Eli Friedman91571da2009-03-22 23:26:56 +0000890
891 // The following is illegal:
892 // void takeclosure(void (^C)(void));
893 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
894 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000895 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000896 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
897 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
898 return MLV_NotBlockQualified;
899 }
900
Chris Lattnera1923f62008-08-04 07:31:14 +0000901 QualType CT = Ctx.getCanonicalType(getType());
902
903 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000904 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000905 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000906 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000907 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000908 return MLV_IncompleteType;
909
Chris Lattnera1923f62008-08-04 07:31:14 +0000910 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000911 if (r->hasConstFields())
912 return MLV_ConstQualified;
913 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000914
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000915 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000916 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000917 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
918 if (KVCExpr->getSetterMethod() == 0)
919 return MLV_NoSetterProperty;
920 }
Chris Lattner4b009652007-07-25 00:24:17 +0000921 return MLV_Valid;
922}
923
Ted Kremenek5778d622008-02-27 18:39:48 +0000924/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000925/// duration. This means that the address of this expression is a link-time
926/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000927bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000928 switch (getStmtClass()) {
929 default:
930 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000931 case BlockExprClass:
932 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000933 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000934 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000935 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000936 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000937 case CompoundLiteralExprClass:
938 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000939 case DeclRefExprClass:
940 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000941 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
942 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000943 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000944 if (isa<FunctionDecl>(D))
945 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000946 return false;
947 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000948 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000949 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000950 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000951 }
Chris Lattner743ec372007-11-27 21:35:27 +0000952 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000953 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000954 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000955 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000956 case CXXDefaultArgExprClass:
957 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000958 }
959}
960
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000961/// isOBJCGCCandidate - Check if an expression is objc gc'able.
962///
963bool Expr::isOBJCGCCandidate() const {
964 switch (getStmtClass()) {
965 default:
966 return false;
967 case ObjCIvarRefExprClass:
968 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000969 case Expr::UnaryOperatorClass:
970 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000971 case ParenExprClass:
972 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
973 case ImplicitCastExprClass:
974 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000975 case CStyleCastExprClass:
976 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000977 case DeclRefExprClass:
978 case QualifiedDeclRefExprClass: {
979 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
980 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
981 return VD->hasGlobalStorage();
982 return false;
983 }
984 case MemberExprClass: {
985 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000986 return M->getBase()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000987 }
988 case ArraySubscriptExprClass:
989 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
990 }
991}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000992Expr* Expr::IgnoreParens() {
993 Expr* E = this;
994 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
995 E = P->getSubExpr();
996
997 return E;
998}
999
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001000/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1001/// or CastExprs or ImplicitCastExprs, returning their operand.
1002Expr *Expr::IgnoreParenCasts() {
1003 Expr *E = this;
1004 while (true) {
1005 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1006 E = P->getSubExpr();
1007 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1008 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001009 else
1010 return E;
1011 }
1012}
1013
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001014/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1015/// value (including ptr->int casts of the same size). Strip off any
1016/// ParenExpr or CastExprs, returning their operand.
1017Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1018 Expr *E = this;
1019 while (true) {
1020 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1021 E = P->getSubExpr();
1022 continue;
1023 }
1024
1025 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1026 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1027 // ptr<->int casts of the same width. We also ignore all identify casts.
1028 Expr *SE = P->getSubExpr();
1029
1030 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1031 E = SE;
1032 continue;
1033 }
1034
1035 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1036 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1037 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1038 E = SE;
1039 continue;
1040 }
1041 }
1042
1043 return E;
1044 }
1045}
1046
1047
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001048/// hasAnyTypeDependentArguments - Determines if any of the expressions
1049/// in Exprs is type-dependent.
1050bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1051 for (unsigned I = 0; I < NumExprs; ++I)
1052 if (Exprs[I]->isTypeDependent())
1053 return true;
1054
1055 return false;
1056}
1057
1058/// hasAnyValueDependentArguments - Determines if any of the expressions
1059/// in Exprs is value-dependent.
1060bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1061 for (unsigned I = 0; I < NumExprs; ++I)
1062 if (Exprs[I]->isValueDependent())
1063 return true;
1064
1065 return false;
1066}
1067
Eli Friedmandee41122009-01-25 02:32:41 +00001068bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001069 // This function is attempting whether an expression is an initializer
1070 // which can be evaluated at compile-time. isEvaluatable handles most
1071 // of the cases, but it can't deal with some initializer-specific
1072 // expressions, and it can't deal with aggregates; we deal with those here,
1073 // and fall back to isEvaluatable for the other cases.
1074
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001075 // FIXME: This function assumes the variable being assigned to
1076 // isn't a reference type!
1077
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001078 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001079 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001080 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001081 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001082 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001083 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001084 // This handles gcc's extension that allows global initializers like
1085 // "struct x {int x;} x = (struct x) {};".
1086 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001087 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001088 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001089 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001090 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001091 // FIXME: This doesn't deal with fields with reference types correctly.
1092 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1093 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001094 const InitListExpr *Exp = cast<InitListExpr>(this);
1095 unsigned numInits = Exp->getNumInits();
1096 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001097 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001098 return false;
1099 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001100 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001101 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001102 case ImplicitValueInitExprClass:
1103 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001104 case ParenExprClass: {
1105 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1106 }
1107 case UnaryOperatorClass: {
1108 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1109 if (Exp->getOpcode() == UnaryOperator::Extension)
1110 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1111 break;
1112 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001113 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001114 case CStyleCastExprClass:
1115 // Handle casts with a destination that's a struct or union; this
1116 // deals with both the gcc no-op struct cast extension and the
1117 // cast-to-union extension.
1118 if (getType()->isRecordType())
1119 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1120 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001121 }
1122
Eli Friedman2b0dec52009-01-25 03:12:18 +00001123 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001124}
1125
Chris Lattner4b009652007-07-25 00:24:17 +00001126/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001127/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001128
1129/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1130/// comma, etc
1131///
Chris Lattner4b009652007-07-25 00:24:17 +00001132/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1133/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1134/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001135
Eli Friedman7beeda62009-02-26 09:29:13 +00001136// CheckICE - This function does the fundamental ICE checking: the returned
1137// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1138// Note that to reduce code duplication, this helper does no evaluation
1139// itself; the caller checks whether the expression is evaluatable, and
1140// in the rare cases where CheckICE actually cares about the evaluated
1141// value, it calls into Evalute.
1142//
1143// Meanings of Val:
1144// 0: This expression is an ICE if it can be evaluated by Evaluate.
1145// 1: This expression is not an ICE, but if it isn't evaluated, it's
1146// a legal subexpression for an ICE. This return value is used to handle
1147// the comma operator in C99 mode.
1148// 2: This expression is not an ICE, and is not a legal subexpression for one.
1149
1150struct ICEDiag {
1151 unsigned Val;
1152 SourceLocation Loc;
1153
1154 public:
1155 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1156 ICEDiag() : Val(0) {}
1157};
1158
1159ICEDiag NoDiag() { return ICEDiag(); }
1160
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001161static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1162 Expr::EvalResult EVResult;
1163 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1164 !EVResult.Val.isInt()) {
1165 return ICEDiag(2, E->getLocStart());
1166 }
1167 return NoDiag();
1168}
1169
Eli Friedman7beeda62009-02-26 09:29:13 +00001170static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001171 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001172 if (!E->getType()->isIntegralType()) {
1173 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001174 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001175
1176 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001177 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001178 return ICEDiag(2, E->getLocStart());
1179 case Expr::ParenExprClass:
1180 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1181 case Expr::IntegerLiteralClass:
1182 case Expr::CharacterLiteralClass:
1183 case Expr::CXXBoolLiteralExprClass:
1184 case Expr::CXXZeroInitValueExprClass:
1185 case Expr::TypesCompatibleExprClass:
1186 case Expr::UnaryTypeTraitExprClass:
1187 return NoDiag();
1188 case Expr::CallExprClass:
1189 case Expr::CXXOperatorCallExprClass: {
1190 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001191 if (CE->isBuiltinCall(Ctx))
1192 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001193 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001194 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001195 case Expr::DeclRefExprClass:
1196 case Expr::QualifiedDeclRefExprClass:
1197 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1198 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001199 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001200 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001201 // C++ 7.1.5.1p2
1202 // A variable of non-volatile const-qualified integral or enumeration
1203 // type initialized by an ICE can be used in ICEs.
1204 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001205 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001206 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001207 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001208 }
1209 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001210 return ICEDiag(2, E->getLocStart());
1211 case Expr::UnaryOperatorClass: {
1212 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001213 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001214 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001215 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001216 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001217 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001218 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001219 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001220 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001221 case UnaryOperator::Real:
1222 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001223 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001224 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001225 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1226 // Evaluate matches the proposed gcc behavior for cases like
1227 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1228 // compliance: we should warn earlier for offsetof expressions with
1229 // array subscripts that aren't ICEs, and if the array subscripts
1230 // are ICEs, the value of the offsetof must be an integer constant.
1231 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001232 }
Chris Lattner4b009652007-07-25 00:24:17 +00001233 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001234 case Expr::SizeOfAlignOfExprClass: {
1235 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1236 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1237 return ICEDiag(2, E->getLocStart());
1238 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001239 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001240 case Expr::BinaryOperatorClass: {
1241 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001242 switch (Exp->getOpcode()) {
1243 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001244 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001245 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001246 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001247 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001248 case BinaryOperator::Add:
1249 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001250 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001251 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001252 case BinaryOperator::LT:
1253 case BinaryOperator::GT:
1254 case BinaryOperator::LE:
1255 case BinaryOperator::GE:
1256 case BinaryOperator::EQ:
1257 case BinaryOperator::NE:
1258 case BinaryOperator::And:
1259 case BinaryOperator::Xor:
1260 case BinaryOperator::Or:
1261 case BinaryOperator::Comma: {
1262 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1263 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001264 if (Exp->getOpcode() == BinaryOperator::Div ||
1265 Exp->getOpcode() == BinaryOperator::Rem) {
1266 // Evaluate gives an error for undefined Div/Rem, so make sure
1267 // we don't evaluate one.
1268 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1269 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1270 if (REval == 0)
1271 return ICEDiag(1, E->getLocStart());
1272 if (REval.isSigned() && REval.isAllOnesValue()) {
1273 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1274 if (LEval.isMinSignedValue())
1275 return ICEDiag(1, E->getLocStart());
1276 }
1277 }
1278 }
1279 if (Exp->getOpcode() == BinaryOperator::Comma) {
1280 if (Ctx.getLangOptions().C99) {
1281 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1282 // if it isn't evaluated.
1283 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1284 return ICEDiag(1, E->getLocStart());
1285 } else {
1286 // In both C89 and C++, commas in ICEs are illegal.
1287 return ICEDiag(2, E->getLocStart());
1288 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001289 }
1290 if (LHSResult.Val >= RHSResult.Val)
1291 return LHSResult;
1292 return RHSResult;
1293 }
Chris Lattner4b009652007-07-25 00:24:17 +00001294 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001295 case BinaryOperator::LOr: {
1296 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1297 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1298 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1299 // Rare case where the RHS has a comma "side-effect"; we need
1300 // to actually check the condition to see whether the side
1301 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001302 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001303 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001304 return RHSResult;
1305 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001306 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001307
Eli Friedman7beeda62009-02-26 09:29:13 +00001308 if (LHSResult.Val >= RHSResult.Val)
1309 return LHSResult;
1310 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001311 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001312 }
Chris Lattner4b009652007-07-25 00:24:17 +00001313 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001314 case Expr::ImplicitCastExprClass:
1315 case Expr::CStyleCastExprClass:
1316 case Expr::CXXFunctionalCastExprClass: {
1317 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1318 if (SubExpr->getType()->isIntegralType())
1319 return CheckICE(SubExpr, Ctx);
1320 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1321 return NoDiag();
1322 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001323 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001324 case Expr::ConditionalOperatorClass: {
1325 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001326 // If the condition (ignoring parens) is a __builtin_constant_p call,
1327 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001328 // expression, and it is fully evaluated. This is an important GNU
1329 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001330 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001331 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001332 Expr::EvalResult EVResult;
1333 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1334 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001335 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001336 }
1337 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001338 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001339 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1340 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1341 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1342 if (CondResult.Val == 2)
1343 return CondResult;
1344 if (TrueResult.Val == 2)
1345 return TrueResult;
1346 if (FalseResult.Val == 2)
1347 return FalseResult;
1348 if (CondResult.Val == 1)
1349 return CondResult;
1350 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1351 return NoDiag();
1352 // Rare case where the diagnostics depend on which side is evaluated
1353 // Note that if we get here, CondResult is 0, and at least one of
1354 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001355 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001356 return FalseResult;
1357 }
1358 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001359 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001360 case Expr::CXXDefaultArgExprClass:
1361 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001362 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001363 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001364 }
Chris Lattner4b009652007-07-25 00:24:17 +00001365 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001366}
Chris Lattner4b009652007-07-25 00:24:17 +00001367
Eli Friedman7beeda62009-02-26 09:29:13 +00001368bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1369 SourceLocation *Loc, bool isEvaluated) const {
1370 ICEDiag d = CheckICE(this, Ctx);
1371 if (d.Val != 0) {
1372 if (Loc) *Loc = d.Loc;
1373 return false;
1374 }
1375 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001376 if (!Evaluate(EvalResult, Ctx))
1377 assert(0 && "ICE cannot be evaluated!");
1378 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1379 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001380 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001381 return true;
1382}
1383
Chris Lattner4b009652007-07-25 00:24:17 +00001384/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1385/// integer constant expression with the value zero, or if this is one that is
1386/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001387bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1388{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001389 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001390 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001391 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001392 // Check that it is a cast to void*.
1393 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1394 QualType Pointee = PT->getPointeeType();
1395 if (Pointee.getCVRQualifiers() == 0 &&
1396 Pointee->isVoidType() && // to void*
1397 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001398 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001399 }
Chris Lattner4b009652007-07-25 00:24:17 +00001400 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001401 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1402 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001403 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001404 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1405 // Accept ((void*)0) as a null pointer constant, as many other
1406 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001407 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001408 } else if (const CXXDefaultArgExpr *DefaultArg
1409 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001410 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001411 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001412 } else if (isa<GNUNullExpr>(this)) {
1413 // The GNU __null extension is always a null pointer constant.
1414 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001415 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001416
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001417 // C++0x nullptr_t is always a null pointer constant.
1418 if (getType()->isNullPtrType())
1419 return true;
1420
Steve Naroffa2e53222008-01-14 16:10:57 +00001421 // This expression must be an integer type.
1422 if (!getType()->isIntegerType())
1423 return false;
1424
Chris Lattner4b009652007-07-25 00:24:17 +00001425 // If we have an integer constant expression, we need to *evaluate* it and
1426 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001427 llvm::APSInt Result;
1428 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001429}
Steve Naroffc11705f2007-07-28 23:10:27 +00001430
Douglas Gregor531434b2009-05-02 02:18:30 +00001431FieldDecl *Expr::getBitField() {
Douglas Gregor81c29152008-10-29 00:13:59 +00001432 Expr *E = this->IgnoreParenCasts();
Douglas Gregor531434b2009-05-02 02:18:30 +00001433
Douglas Gregor81c29152008-10-29 00:13:59 +00001434 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001435 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001436 if (Field->isBitField())
1437 return Field;
1438
1439 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1440 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1441 return BinOp->getLHS()->getBitField();
1442
1443 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001444}
1445
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001446/// isArrow - Return true if the base expression is a pointer to vector,
1447/// return false if the base expression is a vector.
1448bool ExtVectorElementExpr::isArrow() const {
1449 return getBase()->getType()->isPointerType();
1450}
1451
Nate Begemanaf6ed502008-04-18 23:10:10 +00001452unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001453 if (const VectorType *VT = getType()->getAsVectorType())
1454 return VT->getNumElements();
1455 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001456}
1457
Nate Begemanc8e51f82008-05-09 06:41:27 +00001458/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001459bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001460 const char *compStr = Accessor->getName();
1461 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001462
1463 // Halving swizzles do not contain duplicate elements.
1464 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1465 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1466 return false;
1467
1468 // Advance past s-char prefix on hex swizzles.
1469 if (*compStr == 's') {
1470 compStr++;
1471 length--;
1472 }
Steve Naroffba67f692007-07-30 03:29:09 +00001473
Chris Lattner58d3fa52008-11-19 07:55:04 +00001474 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001475 const char *s = compStr+i;
1476 for (const char c = *s++; *s; s++)
1477 if (c == *s)
1478 return true;
1479 }
1480 return false;
1481}
Chris Lattner42158e72007-08-02 23:36:59 +00001482
Nate Begemanc8e51f82008-05-09 06:41:27 +00001483/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001484void ExtVectorElementExpr::getEncodedElementAccess(
1485 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001486 const char *compStr = Accessor->getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001487 if (*compStr == 's')
1488 compStr++;
1489
1490 bool isHi = !strcmp(compStr, "hi");
1491 bool isLo = !strcmp(compStr, "lo");
1492 bool isEven = !strcmp(compStr, "even");
1493 bool isOdd = !strcmp(compStr, "odd");
1494
Nate Begemanc8e51f82008-05-09 06:41:27 +00001495 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1496 uint64_t Index;
1497
1498 if (isHi)
1499 Index = e + i;
1500 else if (isLo)
1501 Index = i;
1502 else if (isEven)
1503 Index = 2 * i;
1504 else if (isOdd)
1505 Index = 2 * i + 1;
1506 else
1507 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001508
Nate Begemana1ae7442008-05-13 21:03:02 +00001509 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001510 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001511}
1512
Steve Naroff4ed9d662007-09-27 14:38:14 +00001513// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001514ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001515 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001516 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001517 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001518 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001519 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001520 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001521 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001522 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001523 if (NumArgs) {
1524 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001525 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1526 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001527 LBracloc = LBrac;
1528 RBracloc = RBrac;
1529}
1530
Steve Naroff4ed9d662007-09-27 14:38:14 +00001531// constructor for class messages.
1532// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001533ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001535 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001536 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001537 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001538 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001539 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001540 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001541 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001542 if (NumArgs) {
1543 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001544 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1545 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001546 LBracloc = LBrac;
1547 RBracloc = RBrac;
1548}
1549
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001550// constructor for class messages.
1551ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1552 QualType retType, ObjCMethodDecl *mproto,
1553 SourceLocation LBrac, SourceLocation RBrac,
1554 Expr **ArgExprs, unsigned nargs)
1555: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1556MethodProto(mproto) {
1557 NumArgs = nargs;
1558 SubExprs = new Stmt*[NumArgs+1];
1559 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1560 if (NumArgs) {
1561 for (unsigned i = 0; i != NumArgs; ++i)
1562 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1563 }
1564 LBracloc = LBrac;
1565 RBracloc = RBrac;
1566}
1567
1568ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1569 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1570 switch (x & Flags) {
1571 default:
1572 assert(false && "Invalid ObjCMessageExpr.");
1573 case IsInstMeth:
1574 return ClassInfo(0, 0);
1575 case IsClsMethDeclUnknown:
1576 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1577 case IsClsMethDeclKnown: {
1578 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1579 return ClassInfo(D, D->getIdentifier());
1580 }
1581 }
1582}
1583
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001584void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1585 if (CI.first == 0 && CI.second == 0)
1586 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1587 else if (CI.first == 0)
1588 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1589 else
1590 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1591}
1592
1593
Chris Lattnerf624cd22007-10-25 00:29:32 +00001594bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001595 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001596}
1597
Douglas Gregor725e94b2009-04-16 00:01:45 +00001598void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1599 if (NumExprs)
1600 delete [] SubExprs;
1601
1602 SubExprs = new Stmt* [NumExprs];
1603 this->NumExprs = NumExprs;
1604 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1605}
1606
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001607void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1608 // Override default behavior of traversing children. If this has a type
1609 // operand and the type is a variable-length array, the child iteration
1610 // will iterate over the size expression. However, this expression belongs
1611 // to the type, not to this, so we don't want to delete it.
1612 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001613 if (isArgumentType()) {
1614 this->~SizeOfAlignOfExpr();
1615 C.Deallocate(this);
1616 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001617 else
1618 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001619}
1620
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001621//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001622// DesignatedInitExpr
1623//===----------------------------------------------------------------------===//
1624
1625IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1626 assert(Kind == FieldDesignator && "Only valid on a field designator");
1627 if (Field.NameOrField & 0x01)
1628 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1629 else
1630 return getField()->getIdentifier();
1631}
1632
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001633DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1634 const Designator *Designators,
1635 SourceLocation EqualOrColonLoc,
1636 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001637 Expr **IndexExprs,
1638 unsigned NumIndexExprs,
1639 Expr *Init)
1640 : Expr(DesignatedInitExprClass, Ty,
1641 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001642 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001643 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001644 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001645
1646 // Record the initializer itself.
1647 child_iterator Child = child_begin();
1648 *Child++ = Init;
1649
1650 // Copy the designators and their subexpressions, computing
1651 // value-dependence along the way.
1652 unsigned IndexIdx = 0;
1653 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001654 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001655
1656 if (this->Designators[I].isArrayDesignator()) {
1657 // Compute type- and value-dependence.
1658 Expr *Index = IndexExprs[IndexIdx];
1659 ValueDependent = ValueDependent ||
1660 Index->isTypeDependent() || Index->isValueDependent();
1661
1662 // Copy the index expressions into permanent storage.
1663 *Child++ = IndexExprs[IndexIdx++];
1664 } else if (this->Designators[I].isArrayRangeDesignator()) {
1665 // Compute type- and value-dependence.
1666 Expr *Start = IndexExprs[IndexIdx];
1667 Expr *End = IndexExprs[IndexIdx + 1];
1668 ValueDependent = ValueDependent ||
1669 Start->isTypeDependent() || Start->isValueDependent() ||
1670 End->isTypeDependent() || End->isValueDependent();
1671
1672 // Copy the start/end expressions into permanent storage.
1673 *Child++ = IndexExprs[IndexIdx++];
1674 *Child++ = IndexExprs[IndexIdx++];
1675 }
1676 }
1677
1678 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001679}
1680
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001681DesignatedInitExpr *
1682DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1683 unsigned NumDesignators,
1684 Expr **IndexExprs, unsigned NumIndexExprs,
1685 SourceLocation ColonOrEqualLoc,
1686 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001687 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001688 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001689 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1690 ColonOrEqualLoc, UsesColonSyntax,
1691 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001692}
1693
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001694DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1695 unsigned NumIndexExprs) {
1696 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1697 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1698 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1699}
1700
1701void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1702 unsigned NumDesigs) {
1703 if (Designators)
1704 delete [] Designators;
1705
1706 Designators = new Designator[NumDesigs];
1707 NumDesignators = NumDesigs;
1708 for (unsigned I = 0; I != NumDesigs; ++I)
1709 Designators[I] = Desigs[I];
1710}
1711
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001712SourceRange DesignatedInitExpr::getSourceRange() const {
1713 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001714 Designator &First =
1715 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001716 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001717 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001718 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1719 else
1720 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1721 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001722 StartLoc =
1723 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001724 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1725}
1726
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001727Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1728 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1729 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1730 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001731 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1732 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1733}
1734
1735Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1736 assert(D.Kind == Designator::ArrayRangeDesignator &&
1737 "Requires array range designator");
1738 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1739 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001740 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1741 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1742}
1743
1744Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1745 assert(D.Kind == Designator::ArrayRangeDesignator &&
1746 "Requires array range designator");
1747 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1748 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001749 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1750 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1751}
1752
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001753/// \brief Replaces the designator at index @p Idx with the series
1754/// of designators in [First, Last).
1755void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1756 const Designator *First,
1757 const Designator *Last) {
1758 unsigned NumNewDesignators = Last - First;
1759 if (NumNewDesignators == 0) {
1760 std::copy_backward(Designators + Idx + 1,
1761 Designators + NumDesignators,
1762 Designators + Idx);
1763 --NumNewDesignators;
1764 return;
1765 } else if (NumNewDesignators == 1) {
1766 Designators[Idx] = *First;
1767 return;
1768 }
1769
1770 Designator *NewDesignators
1771 = new Designator[NumDesignators - 1 + NumNewDesignators];
1772 std::copy(Designators, Designators + Idx, NewDesignators);
1773 std::copy(First, Last, NewDesignators + Idx);
1774 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1775 NewDesignators + Idx + NumNewDesignators);
1776 delete [] Designators;
1777 Designators = NewDesignators;
1778 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1779}
1780
1781void DesignatedInitExpr::Destroy(ASTContext &C) {
1782 delete [] Designators;
1783 Expr::Destroy(C);
1784}
1785
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001786ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1787 return new (C) ImplicitValueInitExpr(getType());
1788}
1789
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001790//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001791// ExprIterator.
1792//===----------------------------------------------------------------------===//
1793
1794Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1795Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1796Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1797const Expr* ConstExprIterator::operator[](size_t idx) const {
1798 return cast<Expr>(I[idx]);
1799}
1800const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1801const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1802
1803//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001804// Child Iterators for iterating over subexpressions/substatements
1805//===----------------------------------------------------------------------===//
1806
1807// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001808Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1809Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001810
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001811// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001812Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1813Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001814
Steve Naroff6f786252008-06-02 23:03:37 +00001815// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001816Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1817Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001818
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001819// ObjCKVCRefExpr
1820Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1821Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1822
Douglas Gregord8606632008-11-04 14:56:14 +00001823// ObjCSuperExpr
1824Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1825Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1826
Chris Lattner69909292008-08-10 01:53:14 +00001827// PredefinedExpr
1828Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1829Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001830
1831// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001832Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1833Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001834
1835// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001836Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001837Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001838
1839// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001840Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1841Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001842
Chris Lattner1de66eb2007-08-26 03:42:43 +00001843// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001844Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1845Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001846
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001847// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001848Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1849Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001850
1851// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001852Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1853Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001854
1855// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001856Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1857Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001858
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001859// SizeOfAlignOfExpr
1860Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1861 // If this is of a type and the type is a VLA type (and not a typedef), the
1862 // size expression of the VLA needs to be treated as an executable expression.
1863 // Why isn't this weirdness documented better in StmtIterator?
1864 if (isArgumentType()) {
1865 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1866 getArgumentType().getTypePtr()))
1867 return child_iterator(T);
1868 return child_iterator();
1869 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001870 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001871}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001872Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1873 if (isArgumentType())
1874 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001875 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001876}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001877
1878// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001879Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001880 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001881}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001882Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001883 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001884}
1885
1886// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001887Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001888 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001889}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001890Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001891 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001892}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001893
1894// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001895Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1896Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001897
Nate Begemanaf6ed502008-04-18 23:10:10 +00001898// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001899Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1900Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001901
1902// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001903Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1904Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001905
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001906// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001907Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1908Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001909
1910// BinaryOperator
1911Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001912 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001913}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001914Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001915 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001916}
1917
1918// ConditionalOperator
1919Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001920 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001921}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001922Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001923 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001924}
1925
1926// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001927Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1928Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001929
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001930// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001931Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1932Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001933
1934// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001935Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1936 return child_iterator();
1937}
1938
1939Stmt::child_iterator TypesCompatibleExpr::child_end() {
1940 return child_iterator();
1941}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001942
1943// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001944Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1945Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001946
Douglas Gregorad4b3792008-11-29 04:51:27 +00001947// GNUNullExpr
1948Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1949Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1950
Eli Friedmand0e9d092008-05-14 19:38:39 +00001951// ShuffleVectorExpr
1952Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001953 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001954}
1955Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001956 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001957}
1958
Anders Carlsson36760332007-10-15 20:28:48 +00001959// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001960Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1961Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001962
Anders Carlsson762b7c72007-08-31 04:56:16 +00001963// InitListExpr
1964Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001965 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001966}
1967Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001968 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001969}
1970
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001971// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001972Stmt::child_iterator DesignatedInitExpr::child_begin() {
1973 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1974 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001975 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1976}
1977Stmt::child_iterator DesignatedInitExpr::child_end() {
1978 return child_iterator(&*child_begin() + NumSubExprs);
1979}
1980
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001981// ImplicitValueInitExpr
1982Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1983 return child_iterator();
1984}
1985
1986Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1987 return child_iterator();
1988}
1989
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001990// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001991Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001992 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001993}
1994Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001995 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001996}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001997
1998// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001999Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2000Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002001
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002002// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002003Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2004 return child_iterator();
2005}
2006Stmt::child_iterator ObjCSelectorExpr::child_end() {
2007 return child_iterator();
2008}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002009
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002010// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002011Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2012 return child_iterator();
2013}
2014Stmt::child_iterator ObjCProtocolExpr::child_end() {
2015 return child_iterator();
2016}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002017
Steve Naroffc39ca262007-09-18 23:55:05 +00002018// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002019Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002020 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002021}
2022Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002023 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002024}
2025
Steve Naroff52a81c02008-09-03 18:15:37 +00002026// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002027Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2028Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002029
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002030Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2031Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }