blob: eb4136c2e2a114213f50c0b6c96596062e0f2fc6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000023#include "clang/Basic/TargetInfo.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000024#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Primary Expressions.
29//===----------------------------------------------------------------------===//
30
Sebastian Redl8b0b4752009-05-16 18:50:46 +000031PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
32 return new (C) PredefinedExpr(Loc, getType(), Type);
33}
34
Anders Carlssona135fb42009-03-15 18:34:13 +000035IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
36 return new (C) IntegerLiteral(Value, getType(), Loc);
37}
38
Sebastian Redl8b0b4752009-05-16 18:50:46 +000039CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
40 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
41}
42
43FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
44 bool exact = IsExact;
45 return new (C) FloatingLiteral(Value, &exact, getType(), Loc);
46}
47
Douglas Gregord8ac4362009-05-18 22:38:38 +000048ImaginaryLiteral* ImaginaryLiteral::Clone(ASTContext &C) const {
49 // FIXME: Use virtual Clone(), once it is available
50 Expr *ClonedVal = 0;
51 if (const IntegerLiteral *IntLit = dyn_cast<IntegerLiteral>(Val))
52 ClonedVal = IntLit->Clone(C);
53 else
54 ClonedVal = cast<FloatingLiteral>(Val)->Clone(C);
55 return new (C) ImaginaryLiteral(ClonedVal, getType());
56}
57
Sebastian Redl8b0b4752009-05-16 18:50:46 +000058GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
59 return new (C) GNUNullExpr(getType(), TokenLoc);
60}
61
Chris Lattnerda8249e2008-06-07 22:13:43 +000062/// getValueAsApproximateDouble - This returns the value as an inaccurate
63/// double. Note that this may cause loss of precision, but is useful for
64/// debugging dumps, etc.
65double FloatingLiteral::getValueAsApproximateDouble() const {
66 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000067 bool ignored;
68 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
69 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000070 return V.convertToDouble();
71}
72
Chris Lattner2085fd62009-02-18 06:40:38 +000073StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
74 unsigned ByteLength, bool Wide,
75 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000076 const SourceLocation *Loc,
77 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000078 // Allocate enough space for the StringLiteral plus an array of locations for
79 // any concatenated string tokens.
80 void *Mem = C.Allocate(sizeof(StringLiteral)+
81 sizeof(SourceLocation)*(NumStrs-1),
82 llvm::alignof<StringLiteral>());
83 StringLiteral *SL = new (Mem) StringLiteral(Ty);
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000086 char *AStrData = new (C, 1) char[ByteLength];
87 memcpy(AStrData, StrData, ByteLength);
88 SL->StrData = AStrData;
89 SL->ByteLength = ByteLength;
90 SL->IsWide = Wide;
91 SL->TokLocs[0] = Loc[0];
92 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000093
Chris Lattner726e1682009-02-18 05:49:11 +000094 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000095 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
96 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000097}
98
Douglas Gregor673ecd62009-04-15 16:35:07 +000099StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
100 void *Mem = C.Allocate(sizeof(StringLiteral)+
101 sizeof(SourceLocation)*(NumStrs-1),
102 llvm::alignof<StringLiteral>());
103 StringLiteral *SL = new (Mem) StringLiteral(QualType());
104 SL->StrData = 0;
105 SL->ByteLength = 0;
106 SL->NumConcatenated = NumStrs;
107 return SL;
108}
109
Anders Carlssona135fb42009-03-15 18:34:13 +0000110StringLiteral* StringLiteral::Clone(ASTContext &C) const {
111 return Create(C, StrData, ByteLength, IsWide, getType(),
112 TokLocs, NumConcatenated);
113}
Chris Lattner726e1682009-02-18 05:49:11 +0000114
Ted Kremenek6e94ef52009-02-06 19:55:15 +0000115void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000116 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +0000117 this->~StringLiteral();
118 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000119}
120
Douglas Gregor673ecd62009-04-15 16:35:07 +0000121void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
122 if (StrData)
123 C.Deallocate(const_cast<char*>(StrData));
124
125 char *AStrData = new (C, 1) char[Len];
126 memcpy(AStrData, Str, Len);
127 StrData = AStrData;
128 ByteLength = Len;
129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
132/// corresponds to, e.g. "sizeof" or "[pre]++".
133const char *UnaryOperator::getOpcodeStr(Opcode Op) {
134 switch (Op) {
135 default: assert(0 && "Unknown unary operator");
136 case PostInc: return "++";
137 case PostDec: return "--";
138 case PreInc: return "++";
139 case PreDec: return "--";
140 case AddrOf: return "&";
141 case Deref: return "*";
142 case Plus: return "+";
143 case Minus: return "-";
144 case Not: return "~";
145 case LNot: return "!";
146 case Real: return "__real";
147 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000149 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 }
151}
152
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000153UnaryOperator::Opcode
154UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
155 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000156 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000157 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
158 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
159 case OO_Amp: return AddrOf;
160 case OO_Star: return Deref;
161 case OO_Plus: return Plus;
162 case OO_Minus: return Minus;
163 case OO_Tilde: return Not;
164 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000165 }
166}
167
168OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
169 switch (Opc) {
170 case PostInc: case PreInc: return OO_PlusPlus;
171 case PostDec: case PreDec: return OO_MinusMinus;
172 case AddrOf: return OO_Amp;
173 case Deref: return OO_Star;
174 case Plus: return OO_Plus;
175 case Minus: return OO_Minus;
176 case Not: return OO_Tilde;
177 case LNot: return OO_Exclaim;
178 default: return OO_None;
179 }
180}
181
182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183//===----------------------------------------------------------------------===//
184// Postfix Operators.
185//===----------------------------------------------------------------------===//
186
Ted Kremenek668bf912009-02-09 20:51:47 +0000187CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000188 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000189 : Expr(SC, t,
190 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000191 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000192 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000193
194 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000195 SubExprs[FN] = fn;
196 for (unsigned i = 0; i != numargs; ++i)
197 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000198
Douglas Gregorb4609802008-11-14 16:09:21 +0000199 RParenLoc = rparenloc;
200}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000201
Ted Kremenek668bf912009-02-09 20:51:47 +0000202CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
203 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000204 : Expr(CallExprClass, t,
205 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000206 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000207 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000208
209 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000210 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000212 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 RParenLoc = rparenloc;
215}
216
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000217CallExpr::CallExpr(ASTContext &C, EmptyShell Empty)
218 : Expr(CallExprClass, Empty), SubExprs(0), NumArgs(0) {
219 SubExprs = new (C) Stmt*[1];
220}
221
Ted Kremenek668bf912009-02-09 20:51:47 +0000222void CallExpr::Destroy(ASTContext& C) {
223 DestroyChildren(C);
224 if (SubExprs) C.Deallocate(SubExprs);
225 this->~CallExpr();
226 C.Deallocate(this);
227}
228
Chris Lattnerd18b3292007-12-28 05:25:02 +0000229/// setNumArgs - This changes the number of arguments present in this call.
230/// Any orphaned expressions are deleted by this, and any new operands are set
231/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000232void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000233 // No change, just return.
234 if (NumArgs == getNumArgs()) return;
235
236 // If shrinking # arguments, just delete the extras and forgot them.
237 if (NumArgs < getNumArgs()) {
238 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000239 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000240 this->NumArgs = NumArgs;
241 return;
242 }
243
244 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000245 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000246 // Copy over args.
247 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
248 NewSubExprs[i] = SubExprs[i];
249 // Null out new args.
250 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
251 NewSubExprs[i] = 0;
252
Douglas Gregor88c9a462009-04-17 21:46:47 +0000253 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000254 SubExprs = NewSubExprs;
255 this->NumArgs = NumArgs;
256}
257
Chris Lattnercb888962008-10-06 05:00:53 +0000258/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
259/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000260unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000261 // All simple function calls (e.g. func()) are implicitly cast to pointer to
262 // function. As a result, we try and obtain the DeclRefExpr from the
263 // ImplicitCastExpr.
264 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
265 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000266 return 0;
267
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000268 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
269 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000270 return 0;
271
Anders Carlssonbcba2012008-01-31 02:13:57 +0000272 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
273 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000274 return 0;
275
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000276 if (!FDecl->getIdentifier())
277 return 0;
278
Douglas Gregor3c385e52009-02-14 18:57:46 +0000279 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000280}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000281
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000282QualType CallExpr::getCallReturnType() const {
283 QualType CalleeType = getCallee()->getType();
284 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
285 CalleeType = FnTypePtr->getPointeeType();
286 else if (const BlockPointerType *BPT = CalleeType->getAsBlockPointerType())
287 CalleeType = BPT->getPointeeType();
288
289 const FunctionType *FnType = CalleeType->getAsFunctionType();
290 return FnType->getResultType();
291}
Chris Lattnercb888962008-10-06 05:00:53 +0000292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
294/// corresponds to, e.g. "<<=".
295const char *BinaryOperator::getOpcodeStr(Opcode Op) {
296 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000297 case PtrMemD: return ".*";
298 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 case Mul: return "*";
300 case Div: return "/";
301 case Rem: return "%";
302 case Add: return "+";
303 case Sub: return "-";
304 case Shl: return "<<";
305 case Shr: return ">>";
306 case LT: return "<";
307 case GT: return ">";
308 case LE: return "<=";
309 case GE: return ">=";
310 case EQ: return "==";
311 case NE: return "!=";
312 case And: return "&";
313 case Xor: return "^";
314 case Or: return "|";
315 case LAnd: return "&&";
316 case LOr: return "||";
317 case Assign: return "=";
318 case MulAssign: return "*=";
319 case DivAssign: return "/=";
320 case RemAssign: return "%=";
321 case AddAssign: return "+=";
322 case SubAssign: return "-=";
323 case ShlAssign: return "<<=";
324 case ShrAssign: return ">>=";
325 case AndAssign: return "&=";
326 case XorAssign: return "^=";
327 case OrAssign: return "|=";
328 case Comma: return ",";
329 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000330
331 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332}
333
Douglas Gregor063daf62009-03-13 18:40:31 +0000334BinaryOperator::Opcode
335BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
336 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000337 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000338 case OO_Plus: return Add;
339 case OO_Minus: return Sub;
340 case OO_Star: return Mul;
341 case OO_Slash: return Div;
342 case OO_Percent: return Rem;
343 case OO_Caret: return Xor;
344 case OO_Amp: return And;
345 case OO_Pipe: return Or;
346 case OO_Equal: return Assign;
347 case OO_Less: return LT;
348 case OO_Greater: return GT;
349 case OO_PlusEqual: return AddAssign;
350 case OO_MinusEqual: return SubAssign;
351 case OO_StarEqual: return MulAssign;
352 case OO_SlashEqual: return DivAssign;
353 case OO_PercentEqual: return RemAssign;
354 case OO_CaretEqual: return XorAssign;
355 case OO_AmpEqual: return AndAssign;
356 case OO_PipeEqual: return OrAssign;
357 case OO_LessLess: return Shl;
358 case OO_GreaterGreater: return Shr;
359 case OO_LessLessEqual: return ShlAssign;
360 case OO_GreaterGreaterEqual: return ShrAssign;
361 case OO_EqualEqual: return EQ;
362 case OO_ExclaimEqual: return NE;
363 case OO_LessEqual: return LE;
364 case OO_GreaterEqual: return GE;
365 case OO_AmpAmp: return LAnd;
366 case OO_PipePipe: return LOr;
367 case OO_Comma: return Comma;
368 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000369 }
370}
371
372OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
373 static const OverloadedOperatorKind OverOps[] = {
374 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
375 OO_Star, OO_Slash, OO_Percent,
376 OO_Plus, OO_Minus,
377 OO_LessLess, OO_GreaterGreater,
378 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
379 OO_EqualEqual, OO_ExclaimEqual,
380 OO_Amp,
381 OO_Caret,
382 OO_Pipe,
383 OO_AmpAmp,
384 OO_PipePipe,
385 OO_Equal, OO_StarEqual,
386 OO_SlashEqual, OO_PercentEqual,
387 OO_PlusEqual, OO_MinusEqual,
388 OO_LessLessEqual, OO_GreaterGreaterEqual,
389 OO_AmpEqual, OO_CaretEqual,
390 OO_PipeEqual,
391 OO_Comma
392 };
393 return OverOps[Opc];
394}
395
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000396InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000397 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000398 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000399 : Expr(InitListExprClass, QualType(),
400 hasAnyTypeDependentArguments(initExprs, numInits),
401 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000402 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000403 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000404
405 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000406}
Reid Spencer5f016e22007-07-11 17:01:13 +0000407
Douglas Gregorfa219202009-03-20 23:58:33 +0000408void InitListExpr::reserveInits(unsigned NumInits) {
409 if (NumInits > InitExprs.size())
410 InitExprs.reserve(NumInits);
411}
412
Douglas Gregor4c678342009-01-28 21:54:33 +0000413void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000414 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000415 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000416 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000417 InitExprs.resize(NumInits, 0);
418}
419
420Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
421 if (Init >= InitExprs.size()) {
422 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
423 InitExprs.back() = expr;
424 return 0;
425 }
426
427 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
428 InitExprs[Init] = expr;
429 return Result;
430}
431
Steve Naroffbfdcae62008-09-04 15:31:07 +0000432/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000433///
434const FunctionType *BlockExpr::getFunctionType() const {
435 return getType()->getAsBlockPointerType()->
436 getPointeeType()->getAsFunctionType();
437}
438
Steve Naroff56ee6892008-10-08 17:01:13 +0000439SourceLocation BlockExpr::getCaretLocation() const {
440 return TheBlock->getCaretLocation();
441}
Douglas Gregor72971342009-04-18 00:02:19 +0000442const Stmt *BlockExpr::getBody() const {
443 return TheBlock->getBody();
444}
445Stmt *BlockExpr::getBody() {
446 return TheBlock->getBody();
447}
Steve Naroff56ee6892008-10-08 17:01:13 +0000448
449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450//===----------------------------------------------------------------------===//
451// Generic Expression Routines
452//===----------------------------------------------------------------------===//
453
Chris Lattner026dc962009-02-14 07:37:35 +0000454/// isUnusedResultAWarning - Return true if this immediate expression should
455/// be warned about if the result is unused. If so, fill in Loc and Ranges
456/// with location to warn on and the source range[s] to report with the
457/// warning.
458bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Douglas Gregor68584ed2009-06-18 16:11:24 +0000459 SourceRange &R2, ASTContext &Context) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000460 // Don't warn if the expr is type dependent. The type could end up
461 // instantiating to void.
462 if (isTypeDependent())
463 return false;
464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 switch (getStmtClass()) {
466 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000467 Loc = getExprLoc();
468 R1 = getSourceRange();
469 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000471 return cast<ParenExpr>(this)->getSubExpr()->
Douglas Gregor68584ed2009-06-18 16:11:24 +0000472 isUnusedResultAWarning(Loc, R1, R2, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 case UnaryOperatorClass: {
474 const UnaryOperator *UO = cast<UnaryOperator>(this);
475
476 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000477 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 case UnaryOperator::PostInc:
479 case UnaryOperator::PostDec:
480 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000481 case UnaryOperator::PreDec: // ++/--
482 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 case UnaryOperator::Deref:
484 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000485 if (getType().isVolatileQualified())
486 return false;
487 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 case UnaryOperator::Real:
489 case UnaryOperator::Imag:
490 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000491 if (UO->getSubExpr()->getType().isVolatileQualified())
492 return false;
493 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 case UnaryOperator::Extension:
Douglas Gregor68584ed2009-06-18 16:11:24 +0000495 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 }
Chris Lattner026dc962009-02-14 07:37:35 +0000497 Loc = UO->getOperatorLoc();
498 R1 = UO->getSubExpr()->getSourceRange();
499 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000501 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000502 const BinaryOperator *BO = cast<BinaryOperator>(this);
503 // Consider comma to have side effects if the LHS or RHS does.
504 if (BO->getOpcode() == BinaryOperator::Comma)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000505 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Context) ||
506 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Context);
Chris Lattnere7716e62007-12-01 06:07:34 +0000507
Chris Lattner026dc962009-02-14 07:37:35 +0000508 if (BO->isAssignmentOp())
509 return false;
510 Loc = BO->getOperatorLoc();
511 R1 = BO->getLHS()->getSourceRange();
512 R2 = BO->getRHS()->getSourceRange();
513 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000514 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000515 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000516 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000517
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000518 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000519 // The condition must be evaluated, but if either the LHS or RHS is a
520 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000521 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor68584ed2009-06-18 16:11:24 +0000522 if (Exp->getLHS() &&
523 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Context))
Chris Lattner026dc962009-02-14 07:37:35 +0000524 return true;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000525 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Context);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000526 }
527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000529 // If the base pointer or element is to a volatile pointer/field, accessing
530 // it is a side effect.
531 if (getType().isVolatileQualified())
532 return false;
533 Loc = cast<MemberExpr>(this)->getMemberLoc();
534 R1 = SourceRange(Loc, Loc);
535 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
536 return true;
537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 case ArraySubscriptExprClass:
539 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000540 // it is a side effect.
541 if (getType().isVolatileQualified())
542 return false;
543 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
544 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
545 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
546 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000547
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000549 case CXXOperatorCallExprClass:
550 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000551 // If this is a direct call, get the callee.
552 const CallExpr *CE = cast<CallExpr>(this);
553 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
554 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
555 // If the callee has attribute pure, const, or warn_unused_result, warn
556 // about it. void foo() { strlen("bar"); } should warn.
557 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000558 if (FD->getAttr<WarnUnusedResultAttr>(Context) ||
559 FD->getAttr<PureAttr>(Context) || FD->getAttr<ConstAttr>(Context)) {
Chris Lattner026dc962009-02-14 07:37:35 +0000560 Loc = CE->getCallee()->getLocStart();
561 R1 = CE->getCallee()->getSourceRange();
562
563 if (unsigned NumArgs = CE->getNumArgs())
564 R2 = SourceRange(CE->getArg(0)->getLocStart(),
565 CE->getArg(NumArgs-1)->getLocEnd());
566 return true;
567 }
568 }
569 return false;
570 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000571 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000572 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000573 case StmtExprClass: {
574 // Statement exprs don't logically have side effects themselves, but are
575 // sometimes used in macros in ways that give them a type that is unused.
576 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
577 // however, if the result of the stmt expr is dead, we don't want to emit a
578 // warning.
579 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
580 if (!CS->body_empty())
581 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Douglas Gregor68584ed2009-06-18 16:11:24 +0000582 return E->isUnusedResultAWarning(Loc, R1, R2, Context);
Chris Lattner026dc962009-02-14 07:37:35 +0000583
584 Loc = cast<StmtExpr>(this)->getLParenLoc();
585 R1 = getSourceRange();
586 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000587 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000588 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000589 // If this is a cast to void, check the operand. Otherwise, the result of
590 // the cast is unused.
591 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000592 return cast<CastExpr>(this)->getSubExpr()
593 ->isUnusedResultAWarning(Loc, R1, R2, Context);
Chris Lattner026dc962009-02-14 07:37:35 +0000594 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
595 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
596 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000597 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // If this is a cast to void, check the operand. Otherwise, the result of
599 // the cast is unused.
600 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000601 return cast<CastExpr>(this)->getSubExpr()
602 ->isUnusedResultAWarning(Loc, R1, R2, Context);
Chris Lattner026dc962009-02-14 07:37:35 +0000603 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
604 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
605 return true;
606
Eli Friedman4be1f472008-05-19 21:24:43 +0000607 case ImplicitCastExprClass:
608 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000609 return cast<ImplicitCastExpr>(this)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000610 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Context);
Eli Friedman4be1f472008-05-19 21:24:43 +0000611
Chris Lattner04421082008-04-08 04:40:51 +0000612 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000613 return cast<CXXDefaultArgExpr>(this)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000614 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Context);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000615
616 case CXXNewExprClass:
617 // FIXME: In theory, there might be new expressions that don't have side
618 // effects (e.g. a placement new with an uninitialized POD).
619 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000620 return false;
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000621 case CXXExprWithTemporariesClass:
622 return cast<CXXExprWithTemporaries>(this)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000623 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Context);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000624 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000625}
626
Douglas Gregorba7e2102008-10-22 15:04:37 +0000627/// DeclCanBeLvalue - Determine whether the given declaration can be
628/// an lvalue. This is a helper routine for isLvalue.
629static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000630 // C++ [temp.param]p6:
631 // A non-type non-reference template-parameter is not an lvalue.
632 if (const NonTypeTemplateParmDecl *NTTParm
633 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
634 return NTTParm->getType()->isReferenceType();
635
Douglas Gregor44b43212008-12-11 16:49:14 +0000636 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000637 // C++ 3.10p2: An lvalue refers to an object or function.
638 (Ctx.getLangOptions().CPlusPlus &&
639 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
640}
641
Reid Spencer5f016e22007-07-11 17:01:13 +0000642/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
643/// incomplete type other than void. Nonarray expressions that can be lvalues:
644/// - name, where name must be a variable
645/// - e[i]
646/// - (e), where e must be an lvalue
647/// - e.name, where e must be an lvalue
648/// - e->name
649/// - *e, the type of e cannot be a function type
650/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000651/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000652/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000653///
Chris Lattner28be73f2008-07-26 21:30:36 +0000654Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000655 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
656
657 isLvalueResult Res = isLvalueInternal(Ctx);
658 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
659 return Res;
660
Douglas Gregor98cd5992008-10-21 23:43:52 +0000661 // first, check the type (C99 6.3.2.1). Expressions with function
662 // type in C are not lvalues, but they can be lvalues in C++.
Eli Friedman53202852009-05-03 22:36:05 +0000663 if (TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 return LV_NotObjectType;
665
Steve Naroffacb818a2008-02-10 01:39:04 +0000666 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000667 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000668 return LV_IncompleteVoidType;
669
Eli Friedman53202852009-05-03 22:36:05 +0000670 return LV_Valid;
671}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000672
Eli Friedman53202852009-05-03 22:36:05 +0000673// Check whether the expression can be sanely treated like an l-value
674Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000676 case StringLiteralClass: // C99 6.5.1p4
677 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000678 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
680 // For vectors, make sure base is an lvalue (i.e. not a function call).
681 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000682 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000684 case DeclRefExprClass:
685 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000686 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
687 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 return LV_Valid;
689 break;
Chris Lattner41110242008-06-17 18:05:57 +0000690 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000691 case BlockDeclRefExprClass: {
692 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000693 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000694 return LV_Valid;
695 break;
696 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000697 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000699 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
700 NamedDecl *Member = m->getMemberDecl();
701 // C++ [expr.ref]p4:
702 // If E2 is declared to have type "reference to T", then E1.E2
703 // is an lvalue.
704 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
705 if (Value->getType()->isReferenceType())
706 return LV_Valid;
707
708 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000709 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000710 return LV_Valid;
711
712 // -- If E2 is a non-static data member [...]. If E1 is an
713 // lvalue, then E1.E2 is an lvalue.
714 if (isa<FieldDecl>(Member))
715 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
716
717 // -- If it refers to a static member function [...], then
718 // E1.E2 is an lvalue.
719 // -- Otherwise, if E1.E2 refers to a non-static member
720 // function [...], then E1.E2 is not an lvalue.
721 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
722 return Method->isStatic()? LV_Valid : LV_MemberFunction;
723
724 // -- If E2 is a member enumerator [...], the expression E1.E2
725 // is not an lvalue.
726 if (isa<EnumConstantDecl>(Member))
727 return LV_InvalidExpression;
728
729 // Not an lvalue.
730 return LV_InvalidExpression;
731 }
732
733 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000734 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000735 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000736 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000738 return LV_Valid; // C99 6.5.3p4
739
740 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000741 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
742 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000743 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000744
745 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
746 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
747 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
748 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000750 case ImplicitCastExprClass:
751 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
752 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000754 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000755 case BinaryOperatorClass:
756 case CompoundAssignOperatorClass: {
757 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000758
759 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
760 BinOp->getOpcode() == BinaryOperator::Comma)
761 return BinOp->getRHS()->isLvalue(Ctx);
762
Sebastian Redl22460502009-02-07 00:15:38 +0000763 // C++ [expr.mptr.oper]p6
764 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
765 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
766 !BinOp->getType()->isFunctionType())
767 return BinOp->getLHS()->isLvalue(Ctx);
768
Douglas Gregorbf3af052008-11-13 20:12:29 +0000769 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000770 return LV_InvalidExpression;
771
Douglas Gregorbf3af052008-11-13 20:12:29 +0000772 if (Ctx.getLangOptions().CPlusPlus)
773 // C++ [expr.ass]p1:
774 // The result of an assignment operation [...] is an lvalue.
775 return LV_Valid;
776
777
778 // C99 6.5.16:
779 // An assignment expression [...] is not an lvalue.
780 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000781 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000782 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000783 case CXXOperatorCallExprClass:
784 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000785 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000786 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000787 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000788 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
789 if (ReturnType->isLValueReferenceType())
790 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000791
Douglas Gregor9d293df2008-10-28 00:22:11 +0000792 break;
793 }
Steve Naroffe6386392007-12-05 04:00:10 +0000794 case CompoundLiteralExprClass: // C99 6.5.2.5p5
795 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000796 case ChooseExprClass:
797 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000798 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000799 case ExtVectorElementExprClass:
800 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000801 return LV_DuplicateVectorComponents;
802 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000803 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
804 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000805 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
806 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000807 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000808 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000809 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000810 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000811 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000812 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000813 case CXXConditionDeclExprClass:
814 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000815 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000816 case CXXFunctionalCastExprClass:
817 case CXXStaticCastExprClass:
818 case CXXDynamicCastExprClass:
819 case CXXReinterpretCastExprClass:
820 case CXXConstCastExprClass:
821 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000822 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000823 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
824 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000825 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
826 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000827 return LV_Valid;
828 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000829 case CXXTypeidExprClass:
830 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
831 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +0000832 case ConditionalOperatorClass: {
833 // Complicated handling is only for C++.
834 if (!Ctx.getLangOptions().CPlusPlus)
835 return LV_InvalidExpression;
836
837 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
838 // everywhere there's an object converted to an rvalue. Also, any other
839 // casts should be wrapped by ImplicitCastExprs. There's just the special
840 // case involving throws to work out.
841 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000842 Expr *True = Cond->getTrueExpr();
843 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +0000844 // C++0x 5.16p2
845 // If either the second or the third operand has type (cv) void, [...]
846 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000847 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +0000848 return LV_InvalidExpression;
849
850 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000851 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +0000852 return LV_InvalidExpression;
853
854 // That's it.
855 return LV_Valid;
856 }
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 default:
859 break;
860 }
861 return LV_InvalidExpression;
862}
863
864/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
865/// does not have an incomplete type, does not have a const-qualified type, and
866/// if it is a structure or union, does not have any member (including,
867/// recursively, any member or element of all contained aggregates or unions)
868/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000869Expr::isModifiableLvalueResult
870Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000871 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000872
873 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000874 case LV_Valid:
875 // C++ 3.10p11: Functions cannot be modified, but pointers to
876 // functions can be modifiable.
877 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
878 return MLV_NotObjectType;
879 break;
880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 case LV_NotObjectType: return MLV_NotObjectType;
882 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000883 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000884 case LV_InvalidExpression:
885 // If the top level is a C-style cast, and the subexpression is a valid
886 // lvalue, then this is probably a use of the old-school "cast as lvalue"
887 // GCC extension. We don't support it, but we want to produce good
888 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000889 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
890 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
891 if (Loc)
892 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000893 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000894 }
895 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000896 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000897 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000899
900 // The following is illegal:
901 // void takeclosure(void (^C)(void));
902 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
903 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000904 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000905 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
906 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
907 return MLV_NotBlockQualified;
908 }
909
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000910 QualType CT = Ctx.getCanonicalType(getType());
911
912 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000914 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000916 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 return MLV_IncompleteType;
918
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000919 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 if (r->hasConstFields())
921 return MLV_ConstQualified;
922 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000923
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000924 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000925 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000926 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
927 if (KVCExpr->getSetterMethod() == 0)
928 return MLV_NoSetterProperty;
929 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 return MLV_Valid;
931}
932
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000933/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000934/// duration. This means that the address of this expression is a link-time
935/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000936bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000937 switch (getStmtClass()) {
938 default:
939 return false;
Steve Naroff3aaa4822009-04-16 19:02:57 +0000940 case BlockExprClass:
941 return true;
Chris Lattner4cc62712007-11-27 21:35:27 +0000942 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000943 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000944 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000945 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000946 case CompoundLiteralExprClass:
947 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000948 case DeclRefExprClass:
949 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000950 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
951 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000952 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000953 if (isa<FunctionDecl>(D))
954 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000955 return false;
956 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000957 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000958 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000959 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000960 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000961 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000962 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000963 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000964 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000965 case CXXDefaultArgExprClass:
966 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000967 }
968}
969
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000970/// isOBJCGCCandidate - Check if an expression is objc gc'able.
971///
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000972bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000973 switch (getStmtClass()) {
974 default:
975 return false;
976 case ObjCIvarRefExprClass:
977 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000978 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000979 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000980 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000981 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000982 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000983 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000984 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000985 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000986 case DeclRefExprClass:
987 case QualifiedDeclRefExprClass: {
988 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000989 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
990 if (VD->hasGlobalStorage())
991 return true;
992 QualType T = VD->getType();
993 // dereferencing to an object pointer is always a gc'able candidate
994 if (T->isPointerType() &&
995 Ctx.isObjCObjectPointerType(T->getAsPointerType()->getPointeeType()))
996 return true;
997
998 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000999 return false;
1000 }
1001 case MemberExprClass: {
1002 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001003 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001004 }
1005 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001006 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001007 }
1008}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001009Expr* Expr::IgnoreParens() {
1010 Expr* E = this;
1011 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1012 E = P->getSubExpr();
1013
1014 return E;
1015}
1016
Chris Lattner56f34942008-02-13 01:02:39 +00001017/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1018/// or CastExprs or ImplicitCastExprs, returning their operand.
1019Expr *Expr::IgnoreParenCasts() {
1020 Expr *E = this;
1021 while (true) {
1022 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1023 E = P->getSubExpr();
1024 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1025 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001026 else
1027 return E;
1028 }
1029}
1030
Chris Lattnerecdd8412009-03-13 17:28:01 +00001031/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1032/// value (including ptr->int casts of the same size). Strip off any
1033/// ParenExpr or CastExprs, returning their operand.
1034Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1035 Expr *E = this;
1036 while (true) {
1037 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1038 E = P->getSubExpr();
1039 continue;
1040 }
1041
1042 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1043 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1044 // ptr<->int casts of the same width. We also ignore all identify casts.
1045 Expr *SE = P->getSubExpr();
1046
1047 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1048 E = SE;
1049 continue;
1050 }
1051
1052 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1053 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1054 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1055 E = SE;
1056 continue;
1057 }
1058 }
1059
1060 return E;
1061 }
1062}
1063
1064
Douglas Gregor898574e2008-12-05 23:32:09 +00001065/// hasAnyTypeDependentArguments - Determines if any of the expressions
1066/// in Exprs is type-dependent.
1067bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1068 for (unsigned I = 0; I < NumExprs; ++I)
1069 if (Exprs[I]->isTypeDependent())
1070 return true;
1071
1072 return false;
1073}
1074
1075/// hasAnyValueDependentArguments - Determines if any of the expressions
1076/// in Exprs is value-dependent.
1077bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1078 for (unsigned I = 0; I < NumExprs; ++I)
1079 if (Exprs[I]->isValueDependent())
1080 return true;
1081
1082 return false;
1083}
1084
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001085bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001086 // This function is attempting whether an expression is an initializer
1087 // which can be evaluated at compile-time. isEvaluatable handles most
1088 // of the cases, but it can't deal with some initializer-specific
1089 // expressions, and it can't deal with aggregates; we deal with those here,
1090 // and fall back to isEvaluatable for the other cases.
1091
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001092 // FIXME: This function assumes the variable being assigned to
1093 // isn't a reference type!
1094
Anders Carlssone8a32b82008-11-24 05:23:59 +00001095 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001096 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001097 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001098 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001099 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001100 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001101 // This handles gcc's extension that allows global initializers like
1102 // "struct x {int x;} x = (struct x) {};".
1103 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001104 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001105 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001106 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001107 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001108 // FIXME: This doesn't deal with fields with reference types correctly.
1109 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1110 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001111 const InitListExpr *Exp = cast<InitListExpr>(this);
1112 unsigned numInits = Exp->getNumInits();
1113 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001114 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001115 return false;
1116 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001117 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001118 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001119 case ImplicitValueInitExprClass:
1120 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001121 case ParenExprClass: {
1122 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1123 }
1124 case UnaryOperatorClass: {
1125 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1126 if (Exp->getOpcode() == UnaryOperator::Extension)
1127 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1128 break;
1129 }
Chris Lattner81045d82009-04-21 05:19:11 +00001130 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001131 case CStyleCastExprClass:
1132 // Handle casts with a destination that's a struct or union; this
1133 // deals with both the gcc no-op struct cast extension and the
1134 // cast-to-union extension.
1135 if (getType()->isRecordType())
1136 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1137 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001138 }
1139
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001140 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001141}
1142
Reid Spencer5f016e22007-07-11 17:01:13 +00001143/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001144/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001145
1146/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1147/// comma, etc
1148///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001149/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1150/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1151/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001152
Eli Friedmane28d7192009-02-26 09:29:13 +00001153// CheckICE - This function does the fundamental ICE checking: the returned
1154// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1155// Note that to reduce code duplication, this helper does no evaluation
1156// itself; the caller checks whether the expression is evaluatable, and
1157// in the rare cases where CheckICE actually cares about the evaluated
1158// value, it calls into Evalute.
1159//
1160// Meanings of Val:
1161// 0: This expression is an ICE if it can be evaluated by Evaluate.
1162// 1: This expression is not an ICE, but if it isn't evaluated, it's
1163// a legal subexpression for an ICE. This return value is used to handle
1164// the comma operator in C99 mode.
1165// 2: This expression is not an ICE, and is not a legal subexpression for one.
1166
1167struct ICEDiag {
1168 unsigned Val;
1169 SourceLocation Loc;
1170
1171 public:
1172 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1173 ICEDiag() : Val(0) {}
1174};
1175
1176ICEDiag NoDiag() { return ICEDiag(); }
1177
Eli Friedman60ce9632009-02-27 04:07:58 +00001178static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1179 Expr::EvalResult EVResult;
1180 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1181 !EVResult.Val.isInt()) {
1182 return ICEDiag(2, E->getLocStart());
1183 }
1184 return NoDiag();
1185}
1186
Eli Friedmane28d7192009-02-26 09:29:13 +00001187static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001188 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001189 if (!E->getType()->isIntegralType()) {
1190 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001191 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001192
1193 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001195 return ICEDiag(2, E->getLocStart());
1196 case Expr::ParenExprClass:
1197 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1198 case Expr::IntegerLiteralClass:
1199 case Expr::CharacterLiteralClass:
1200 case Expr::CXXBoolLiteralExprClass:
1201 case Expr::CXXZeroInitValueExprClass:
1202 case Expr::TypesCompatibleExprClass:
1203 case Expr::UnaryTypeTraitExprClass:
1204 return NoDiag();
1205 case Expr::CallExprClass:
1206 case Expr::CXXOperatorCallExprClass: {
1207 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001208 if (CE->isBuiltinCall(Ctx))
1209 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001210 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001211 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001212 case Expr::DeclRefExprClass:
1213 case Expr::QualifiedDeclRefExprClass:
1214 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1215 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001216 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001217 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001218 // C++ 7.1.5.1p2
1219 // A variable of non-volatile const-qualified integral or enumeration
1220 // type initialized by an ICE can be used in ICEs.
1221 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001222 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001223 if (Dcl->isInitKnownICE()) {
1224 // We have already checked whether this subexpression is an
1225 // integral constant expression.
1226 if (Dcl->isInitICE())
1227 return NoDiag();
1228 else
1229 return ICEDiag(2, E->getLocStart());
1230 }
1231
1232 if (const Expr *Init = Dcl->getInit()) {
1233 ICEDiag Result = CheckICE(Init, Ctx);
1234 // Cache the result of the ICE test.
1235 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1236 return Result;
1237 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001238 }
1239 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001240 return ICEDiag(2, E->getLocStart());
1241 case Expr::UnaryOperatorClass: {
1242 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001245 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001247 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001251 case UnaryOperator::Real:
1252 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001253 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001254 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001255 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1256 // Evaluate matches the proposed gcc behavior for cases like
1257 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1258 // compliance: we should warn earlier for offsetof expressions with
1259 // array subscripts that aren't ICEs, and if the array subscripts
1260 // are ICEs, the value of the offsetof must be an integer constant.
1261 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001264 case Expr::SizeOfAlignOfExprClass: {
1265 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1266 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1267 return ICEDiag(2, E->getLocStart());
1268 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001270 case Expr::BinaryOperatorClass: {
1271 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 switch (Exp->getOpcode()) {
1273 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001274 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001278 case BinaryOperator::Add:
1279 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001282 case BinaryOperator::LT:
1283 case BinaryOperator::GT:
1284 case BinaryOperator::LE:
1285 case BinaryOperator::GE:
1286 case BinaryOperator::EQ:
1287 case BinaryOperator::NE:
1288 case BinaryOperator::And:
1289 case BinaryOperator::Xor:
1290 case BinaryOperator::Or:
1291 case BinaryOperator::Comma: {
1292 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1293 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001294 if (Exp->getOpcode() == BinaryOperator::Div ||
1295 Exp->getOpcode() == BinaryOperator::Rem) {
1296 // Evaluate gives an error for undefined Div/Rem, so make sure
1297 // we don't evaluate one.
1298 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1299 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1300 if (REval == 0)
1301 return ICEDiag(1, E->getLocStart());
1302 if (REval.isSigned() && REval.isAllOnesValue()) {
1303 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1304 if (LEval.isMinSignedValue())
1305 return ICEDiag(1, E->getLocStart());
1306 }
1307 }
1308 }
1309 if (Exp->getOpcode() == BinaryOperator::Comma) {
1310 if (Ctx.getLangOptions().C99) {
1311 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1312 // if it isn't evaluated.
1313 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1314 return ICEDiag(1, E->getLocStart());
1315 } else {
1316 // In both C89 and C++, commas in ICEs are illegal.
1317 return ICEDiag(2, E->getLocStart());
1318 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001319 }
1320 if (LHSResult.Val >= RHSResult.Val)
1321 return LHSResult;
1322 return RHSResult;
1323 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001325 case BinaryOperator::LOr: {
1326 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1327 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1328 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1329 // Rare case where the RHS has a comma "side-effect"; we need
1330 // to actually check the condition to see whether the side
1331 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001332 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001333 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001334 return RHSResult;
1335 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001336 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001337
Eli Friedmane28d7192009-02-26 09:29:13 +00001338 if (LHSResult.Val >= RHSResult.Val)
1339 return LHSResult;
1340 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001342 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001344 case Expr::ImplicitCastExprClass:
1345 case Expr::CStyleCastExprClass:
1346 case Expr::CXXFunctionalCastExprClass: {
1347 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1348 if (SubExpr->getType()->isIntegralType())
1349 return CheckICE(SubExpr, Ctx);
1350 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1351 return NoDiag();
1352 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001354 case Expr::ConditionalOperatorClass: {
1355 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001356 // If the condition (ignoring parens) is a __builtin_constant_p call,
1357 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001358 // expression, and it is fully evaluated. This is an important GNU
1359 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001360 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001361 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001362 Expr::EvalResult EVResult;
1363 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1364 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001365 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001366 }
1367 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001368 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001369 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1370 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1371 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1372 if (CondResult.Val == 2)
1373 return CondResult;
1374 if (TrueResult.Val == 2)
1375 return TrueResult;
1376 if (FalseResult.Val == 2)
1377 return FalseResult;
1378 if (CondResult.Val == 1)
1379 return CondResult;
1380 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1381 return NoDiag();
1382 // Rare case where the diagnostics depend on which side is evaluated
1383 // Note that if we get here, CondResult is 0, and at least one of
1384 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001385 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001386 return FalseResult;
1387 }
1388 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001390 case Expr::CXXDefaultArgExprClass:
1391 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001392 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001393 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001394 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001396}
Reid Spencer5f016e22007-07-11 17:01:13 +00001397
Eli Friedmane28d7192009-02-26 09:29:13 +00001398bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1399 SourceLocation *Loc, bool isEvaluated) const {
1400 ICEDiag d = CheckICE(this, Ctx);
1401 if (d.Val != 0) {
1402 if (Loc) *Loc = d.Loc;
1403 return false;
1404 }
1405 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001406 if (!Evaluate(EvalResult, Ctx))
1407 assert(0 && "ICE cannot be evaluated!");
1408 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1409 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001410 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 return true;
1412}
1413
Reid Spencer5f016e22007-07-11 17:01:13 +00001414/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1415/// integer constant expression with the value zero, or if this is one that is
1416/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001417bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1418{
Sebastian Redl07779722008-10-31 14:43:28 +00001419 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001420 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001421 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001422 // Check that it is a cast to void*.
1423 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1424 QualType Pointee = PT->getPointeeType();
1425 if (Pointee.getCVRQualifiers() == 0 &&
1426 Pointee->isVoidType() && // to void*
1427 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001428 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001429 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001431 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1432 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001433 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001434 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1435 // Accept ((void*)0) as a null pointer constant, as many other
1436 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001437 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001438 } else if (const CXXDefaultArgExpr *DefaultArg
1439 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001440 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001441 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001442 } else if (isa<GNUNullExpr>(this)) {
1443 // The GNU __null extension is always a null pointer constant.
1444 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001445 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001446
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001447 // C++0x nullptr_t is always a null pointer constant.
1448 if (getType()->isNullPtrType())
1449 return true;
1450
Steve Naroffaa58f002008-01-14 16:10:57 +00001451 // This expression must be an integer type.
1452 if (!getType()->isIntegerType())
1453 return false;
1454
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 // If we have an integer constant expression, we need to *evaluate* it and
1456 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001457 llvm::APSInt Result;
1458 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001459}
Steve Naroff31a45842007-07-28 23:10:27 +00001460
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001461FieldDecl *Expr::getBitField() {
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001462 Expr *E = this->IgnoreParenCasts();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001463
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001464 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001465 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001466 if (Field->isBitField())
1467 return Field;
1468
1469 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1470 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1471 return BinOp->getLHS()->getBitField();
1472
1473 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001474}
1475
Chris Lattner2140e902009-02-16 22:14:05 +00001476/// isArrow - Return true if the base expression is a pointer to vector,
1477/// return false if the base expression is a vector.
1478bool ExtVectorElementExpr::isArrow() const {
1479 return getBase()->getType()->isPointerType();
1480}
1481
Nate Begeman213541a2008-04-18 23:10:10 +00001482unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001483 if (const VectorType *VT = getType()->getAsVectorType())
1484 return VT->getNumElements();
1485 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001486}
1487
Nate Begeman8a997642008-05-09 06:41:27 +00001488/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001489bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001490 const char *compStr = Accessor->getName();
1491 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001492
1493 // Halving swizzles do not contain duplicate elements.
1494 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1495 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1496 return false;
1497
1498 // Advance past s-char prefix on hex swizzles.
1499 if (*compStr == 's') {
1500 compStr++;
1501 length--;
1502 }
Steve Narofffec0b492007-07-30 03:29:09 +00001503
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001504 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001505 const char *s = compStr+i;
1506 for (const char c = *s++; *s; s++)
1507 if (c == *s)
1508 return true;
1509 }
1510 return false;
1511}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001512
Nate Begeman8a997642008-05-09 06:41:27 +00001513/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001514void ExtVectorElementExpr::getEncodedElementAccess(
1515 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001516 const char *compStr = Accessor->getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001517 if (*compStr == 's')
1518 compStr++;
1519
1520 bool isHi = !strcmp(compStr, "hi");
1521 bool isLo = !strcmp(compStr, "lo");
1522 bool isEven = !strcmp(compStr, "even");
1523 bool isOdd = !strcmp(compStr, "odd");
1524
Nate Begeman8a997642008-05-09 06:41:27 +00001525 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1526 uint64_t Index;
1527
1528 if (isHi)
1529 Index = e + i;
1530 else if (isLo)
1531 Index = i;
1532 else if (isEven)
1533 Index = 2 * i;
1534 else if (isOdd)
1535 Index = 2 * i + 1;
1536 else
1537 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001538
Nate Begeman3b8d1162008-05-13 21:03:02 +00001539 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001540 }
Nate Begeman8a997642008-05-09 06:41:27 +00001541}
1542
Steve Naroff68d331a2007-09-27 14:38:14 +00001543// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001544ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001546 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001547 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001548 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001549 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001550 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001551 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001552 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001553 if (NumArgs) {
1554 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001555 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1556 }
Steve Naroff563477d2007-09-18 23:55:05 +00001557 LBracloc = LBrac;
1558 RBracloc = RBrac;
1559}
1560
Anders Carlsson02d95ba2009-06-07 19:51:47 +00001561ObjCStringLiteral* ObjCStringLiteral::Clone(ASTContext &C) const {
1562 // Clone the string literal.
1563 StringLiteral *NewString =
1564 String ? cast<StringLiteral>(String)->Clone(C) : 0;
1565
1566 return new (C) ObjCStringLiteral(NewString, getType(), AtLoc);
1567}
1568
1569ObjCSelectorExpr *ObjCSelectorExpr::Clone(ASTContext &C) const {
1570 return new (C) ObjCSelectorExpr(getType(), SelName, AtLoc, RParenLoc);
1571}
1572
1573ObjCProtocolExpr *ObjCProtocolExpr::Clone(ASTContext &C) const {
1574 return new (C) ObjCProtocolExpr(getType(), Protocol, AtLoc, RParenLoc);
1575}
1576
Steve Naroff68d331a2007-09-27 14:38:14 +00001577// constructor for class messages.
1578// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001579ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001580 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001581 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001582 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001583 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001584 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001585 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001586 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001587 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001588 if (NumArgs) {
1589 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001590 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1591 }
Steve Naroff563477d2007-09-18 23:55:05 +00001592 LBracloc = LBrac;
1593 RBracloc = RBrac;
1594}
1595
Ted Kremenek4df728e2008-06-24 15:50:53 +00001596// constructor for class messages.
1597ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1598 QualType retType, ObjCMethodDecl *mproto,
1599 SourceLocation LBrac, SourceLocation RBrac,
1600 Expr **ArgExprs, unsigned nargs)
1601: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1602MethodProto(mproto) {
1603 NumArgs = nargs;
1604 SubExprs = new Stmt*[NumArgs+1];
1605 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1606 if (NumArgs) {
1607 for (unsigned i = 0; i != NumArgs; ++i)
1608 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1609 }
1610 LBracloc = LBrac;
1611 RBracloc = RBrac;
1612}
1613
1614ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1615 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1616 switch (x & Flags) {
1617 default:
1618 assert(false && "Invalid ObjCMessageExpr.");
1619 case IsInstMeth:
1620 return ClassInfo(0, 0);
1621 case IsClsMethDeclUnknown:
1622 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1623 case IsClsMethDeclKnown: {
1624 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1625 return ClassInfo(D, D->getIdentifier());
1626 }
1627 }
1628}
1629
Chris Lattner0389e6b2009-04-26 00:44:05 +00001630void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1631 if (CI.first == 0 && CI.second == 0)
1632 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1633 else if (CI.first == 0)
1634 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1635 else
1636 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1637}
1638
1639
Chris Lattner27437ca2007-10-25 00:29:32 +00001640bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001641 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001642}
1643
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001644void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1645 if (NumExprs)
1646 delete [] SubExprs;
1647
1648 SubExprs = new Stmt* [NumExprs];
1649 this->NumExprs = NumExprs;
1650 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1651}
1652
Sebastian Redl05189992008-11-11 17:56:53 +00001653void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1654 // Override default behavior of traversing children. If this has a type
1655 // operand and the type is a variable-length array, the child iteration
1656 // will iterate over the size expression. However, this expression belongs
1657 // to the type, not to this, so we don't want to delete it.
1658 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001659 if (isArgumentType()) {
1660 this->~SizeOfAlignOfExpr();
1661 C.Deallocate(this);
1662 }
Sebastian Redl05189992008-11-11 17:56:53 +00001663 else
1664 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001665}
1666
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001667//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001668// DesignatedInitExpr
1669//===----------------------------------------------------------------------===//
1670
1671IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1672 assert(Kind == FieldDesignator && "Only valid on a field designator");
1673 if (Field.NameOrField & 0x01)
1674 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1675 else
1676 return getField()->getIdentifier();
1677}
1678
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001679DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1680 const Designator *Designators,
1681 SourceLocation EqualOrColonLoc,
1682 bool GNUSyntax,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001683 Expr **IndexExprs,
1684 unsigned NumIndexExprs,
1685 Expr *Init)
1686 : Expr(DesignatedInitExprClass, Ty,
1687 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001688 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor9ea62762009-05-21 23:17:49 +00001689 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001690 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001691
1692 // Record the initializer itself.
1693 child_iterator Child = child_begin();
1694 *Child++ = Init;
1695
1696 // Copy the designators and their subexpressions, computing
1697 // value-dependence along the way.
1698 unsigned IndexIdx = 0;
1699 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001700 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001701
1702 if (this->Designators[I].isArrayDesignator()) {
1703 // Compute type- and value-dependence.
1704 Expr *Index = IndexExprs[IndexIdx];
1705 ValueDependent = ValueDependent ||
1706 Index->isTypeDependent() || Index->isValueDependent();
1707
1708 // Copy the index expressions into permanent storage.
1709 *Child++ = IndexExprs[IndexIdx++];
1710 } else if (this->Designators[I].isArrayRangeDesignator()) {
1711 // Compute type- and value-dependence.
1712 Expr *Start = IndexExprs[IndexIdx];
1713 Expr *End = IndexExprs[IndexIdx + 1];
1714 ValueDependent = ValueDependent ||
1715 Start->isTypeDependent() || Start->isValueDependent() ||
1716 End->isTypeDependent() || End->isValueDependent();
1717
1718 // Copy the start/end expressions into permanent storage.
1719 *Child++ = IndexExprs[IndexIdx++];
1720 *Child++ = IndexExprs[IndexIdx++];
1721 }
1722 }
1723
1724 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001725}
1726
Douglas Gregor05c13a32009-01-22 00:58:24 +00001727DesignatedInitExpr *
1728DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1729 unsigned NumDesignators,
1730 Expr **IndexExprs, unsigned NumIndexExprs,
1731 SourceLocation ColonOrEqualLoc,
1732 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001733 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001734 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001735 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1736 ColonOrEqualLoc, UsesColonSyntax,
1737 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001738}
1739
Douglas Gregord077d752009-04-16 00:55:48 +00001740DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1741 unsigned NumIndexExprs) {
1742 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1743 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1744 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1745}
1746
1747void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1748 unsigned NumDesigs) {
1749 if (Designators)
1750 delete [] Designators;
1751
1752 Designators = new Designator[NumDesigs];
1753 NumDesignators = NumDesigs;
1754 for (unsigned I = 0; I != NumDesigs; ++I)
1755 Designators[I] = Desigs[I];
1756}
1757
Douglas Gregor05c13a32009-01-22 00:58:24 +00001758SourceRange DesignatedInitExpr::getSourceRange() const {
1759 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001760 Designator &First =
1761 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001762 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001763 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001764 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1765 else
1766 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1767 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001768 StartLoc =
1769 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001770 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1771}
1772
Douglas Gregor05c13a32009-01-22 00:58:24 +00001773Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1774 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1775 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1776 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001777 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1778 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1779}
1780
1781Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1782 assert(D.Kind == Designator::ArrayRangeDesignator &&
1783 "Requires array range designator");
1784 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1785 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001786 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1787 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1788}
1789
1790Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1791 assert(D.Kind == Designator::ArrayRangeDesignator &&
1792 "Requires array range designator");
1793 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1794 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001795 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1796 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1797}
1798
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001799/// \brief Replaces the designator at index @p Idx with the series
1800/// of designators in [First, Last).
1801void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1802 const Designator *First,
1803 const Designator *Last) {
1804 unsigned NumNewDesignators = Last - First;
1805 if (NumNewDesignators == 0) {
1806 std::copy_backward(Designators + Idx + 1,
1807 Designators + NumDesignators,
1808 Designators + Idx);
1809 --NumNewDesignators;
1810 return;
1811 } else if (NumNewDesignators == 1) {
1812 Designators[Idx] = *First;
1813 return;
1814 }
1815
1816 Designator *NewDesignators
1817 = new Designator[NumDesignators - 1 + NumNewDesignators];
1818 std::copy(Designators, Designators + Idx, NewDesignators);
1819 std::copy(First, Last, NewDesignators + Idx);
1820 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1821 NewDesignators + Idx + NumNewDesignators);
1822 delete [] Designators;
1823 Designators = NewDesignators;
1824 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1825}
1826
1827void DesignatedInitExpr::Destroy(ASTContext &C) {
1828 delete [] Designators;
1829 Expr::Destroy(C);
1830}
1831
Douglas Gregor9ea62762009-05-21 23:17:49 +00001832ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1833 return new (C) ImplicitValueInitExpr(getType());
1834}
1835
Douglas Gregor05c13a32009-01-22 00:58:24 +00001836//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001837// ExprIterator.
1838//===----------------------------------------------------------------------===//
1839
1840Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1841Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1842Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1843const Expr* ConstExprIterator::operator[](size_t idx) const {
1844 return cast<Expr>(I[idx]);
1845}
1846const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1847const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1848
1849//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001850// Child Iterators for iterating over subexpressions/substatements
1851//===----------------------------------------------------------------------===//
1852
1853// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001854Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1855Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001856
Steve Naroff7779db42007-11-12 14:29:37 +00001857// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001858Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1859Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001860
Steve Naroffe3e9add2008-06-02 23:03:37 +00001861// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001862Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1863Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001864
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001865// ObjCKVCRefExpr
1866Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1867Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1868
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001869// ObjCSuperExpr
1870Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1871Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1872
Chris Lattnerd9f69102008-08-10 01:53:14 +00001873// PredefinedExpr
1874Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1875Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001876
1877// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001878Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1879Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001880
1881// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001882Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001883Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001884
1885// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001886Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1887Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001888
Chris Lattner5d661452007-08-26 03:42:43 +00001889// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001890Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1891Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001892
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001893// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001894Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1895Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001896
1897// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001898Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1899Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001900
1901// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001902Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1903Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001904
Sebastian Redl05189992008-11-11 17:56:53 +00001905// SizeOfAlignOfExpr
1906Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1907 // If this is of a type and the type is a VLA type (and not a typedef), the
1908 // size expression of the VLA needs to be treated as an executable expression.
1909 // Why isn't this weirdness documented better in StmtIterator?
1910 if (isArgumentType()) {
1911 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1912 getArgumentType().getTypePtr()))
1913 return child_iterator(T);
1914 return child_iterator();
1915 }
Sebastian Redld4575892008-12-03 23:17:54 +00001916 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001917}
Sebastian Redl05189992008-11-11 17:56:53 +00001918Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1919 if (isArgumentType())
1920 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001921 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001922}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001923
1924// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001925Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001926 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001927}
Ted Kremenek1237c672007-08-24 20:06:47 +00001928Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001929 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001930}
1931
1932// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001933Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001934 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001935}
Ted Kremenek1237c672007-08-24 20:06:47 +00001936Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001937 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001938}
Ted Kremenek1237c672007-08-24 20:06:47 +00001939
1940// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001941Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1942Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001943
Nate Begeman213541a2008-04-18 23:10:10 +00001944// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001945Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1946Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001947
1948// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001949Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1950Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001951
Ted Kremenek1237c672007-08-24 20:06:47 +00001952// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001953Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1954Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001955
1956// BinaryOperator
1957Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001958 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001959}
Ted Kremenek1237c672007-08-24 20:06:47 +00001960Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001961 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001962}
1963
1964// ConditionalOperator
1965Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001966 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001967}
Ted Kremenek1237c672007-08-24 20:06:47 +00001968Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001969 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001970}
1971
1972// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001973Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1974Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001975
Ted Kremenek1237c672007-08-24 20:06:47 +00001976// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001977Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1978Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001979
1980// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001981Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1982 return child_iterator();
1983}
1984
1985Stmt::child_iterator TypesCompatibleExpr::child_end() {
1986 return child_iterator();
1987}
Ted Kremenek1237c672007-08-24 20:06:47 +00001988
1989// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001990Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1991Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001992
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001993// GNUNullExpr
1994Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1995Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1996
Eli Friedmand38617c2008-05-14 19:38:39 +00001997// ShuffleVectorExpr
1998Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001999 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002000}
2001Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002002 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002003}
2004
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002005// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002006Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2007Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002008
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002009// InitListExpr
2010Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002011 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002012}
2013Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002014 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002015}
2016
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002017// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002018Stmt::child_iterator DesignatedInitExpr::child_begin() {
2019 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2020 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002021 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2022}
2023Stmt::child_iterator DesignatedInitExpr::child_end() {
2024 return child_iterator(&*child_begin() + NumSubExprs);
2025}
2026
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002027// ImplicitValueInitExpr
2028Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2029 return child_iterator();
2030}
2031
2032Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2033 return child_iterator();
2034}
2035
Ted Kremenek1237c672007-08-24 20:06:47 +00002036// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002037Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002038 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002039}
2040Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002041 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002042}
Ted Kremenek1237c672007-08-24 20:06:47 +00002043
2044// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002045Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2046Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002047
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002048// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002049Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2050 return child_iterator();
2051}
2052Stmt::child_iterator ObjCSelectorExpr::child_end() {
2053 return child_iterator();
2054}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002055
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002056// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002057Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2058 return child_iterator();
2059}
2060Stmt::child_iterator ObjCProtocolExpr::child_end() {
2061 return child_iterator();
2062}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002063
Steve Naroff563477d2007-09-18 23:55:05 +00002064// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00002065Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002066 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002067}
2068Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002069 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002070}
2071
Steve Naroff4eb206b2008-09-03 18:15:37 +00002072// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002073Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2074Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002075
Ted Kremenek9da13f92008-09-26 23:24:14 +00002076Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2077Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }