blob: 5b583a520053445d4d7b2e82fe44e4e87e87eb2d [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"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Primary Expressions.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerda8249e2008-06-07 22:13:43 +000032/// getValueAsApproximateDouble - This returns the value as an inaccurate
33/// double. Note that this may cause loss of precision, but is useful for
34/// debugging dumps, etc.
35double FloatingLiteral::getValueAsApproximateDouble() const {
36 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000037 bool ignored;
38 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
39 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000040 return V.convertToDouble();
41}
42
Chris Lattner2085fd62009-02-18 06:40:38 +000043StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
44 unsigned ByteLength, bool Wide,
45 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000046 const SourceLocation *Loc,
47 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000048 // Allocate enough space for the StringLiteral plus an array of locations for
49 // any concatenated string tokens.
50 void *Mem = C.Allocate(sizeof(StringLiteral)+
51 sizeof(SourceLocation)*(NumStrs-1),
52 llvm::alignof<StringLiteral>());
53 StringLiteral *SL = new (Mem) StringLiteral(Ty);
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000056 char *AStrData = new (C, 1) char[ByteLength];
57 memcpy(AStrData, StrData, ByteLength);
58 SL->StrData = AStrData;
59 SL->ByteLength = ByteLength;
60 SL->IsWide = Wide;
61 SL->TokLocs[0] = Loc[0];
62 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000063
Chris Lattner726e1682009-02-18 05:49:11 +000064 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000065 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
66 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000067}
68
Douglas Gregor673ecd62009-04-15 16:35:07 +000069StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
70 void *Mem = C.Allocate(sizeof(StringLiteral)+
71 sizeof(SourceLocation)*(NumStrs-1),
72 llvm::alignof<StringLiteral>());
73 StringLiteral *SL = new (Mem) StringLiteral(QualType());
74 SL->StrData = 0;
75 SL->ByteLength = 0;
76 SL->NumConcatenated = NumStrs;
77 return SL;
78}
79
Douglas Gregor42602bb2009-08-07 06:08:38 +000080void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000081 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +000082 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
Douglas Gregor673ecd62009-04-15 16:35:07 +000085void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
86 if (StrData)
87 C.Deallocate(const_cast<char*>(StrData));
88
89 char *AStrData = new (C, 1) char[Len];
90 memcpy(AStrData, Str, Len);
91 StrData = AStrData;
92 ByteLength = Len;
93}
94
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
96/// corresponds to, e.g. "sizeof" or "[pre]++".
97const char *UnaryOperator::getOpcodeStr(Opcode Op) {
98 switch (Op) {
99 default: assert(0 && "Unknown unary operator");
100 case PostInc: return "++";
101 case PostDec: return "--";
102 case PreInc: return "++";
103 case PreDec: return "--";
104 case AddrOf: return "&";
105 case Deref: return "*";
106 case Plus: return "+";
107 case Minus: return "-";
108 case Not: return "~";
109 case LNot: return "!";
110 case Real: return "__real";
111 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000113 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000117UnaryOperator::Opcode
118UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
119 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000120 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000121 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
122 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
123 case OO_Amp: return AddrOf;
124 case OO_Star: return Deref;
125 case OO_Plus: return Plus;
126 case OO_Minus: return Minus;
127 case OO_Tilde: return Not;
128 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000129 }
130}
131
132OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
133 switch (Opc) {
134 case PostInc: case PreInc: return OO_PlusPlus;
135 case PostDec: case PreDec: return OO_MinusMinus;
136 case AddrOf: return OO_Amp;
137 case Deref: return OO_Star;
138 case Plus: return OO_Plus;
139 case Minus: return OO_Minus;
140 case Not: return OO_Tilde;
141 case LNot: return OO_Exclaim;
142 default: return OO_None;
143 }
144}
145
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147//===----------------------------------------------------------------------===//
148// Postfix Operators.
149//===----------------------------------------------------------------------===//
150
Ted Kremenek668bf912009-02-09 20:51:47 +0000151CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000152 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000153 : Expr(SC, t,
154 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000155 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000156 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000157
158 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000159 SubExprs[FN] = fn;
160 for (unsigned i = 0; i != numargs; ++i)
161 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000162
Douglas Gregorb4609802008-11-14 16:09:21 +0000163 RParenLoc = rparenloc;
164}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000165
Ted Kremenek668bf912009-02-09 20:51:47 +0000166CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
167 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000168 : Expr(CallExprClass, t,
169 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000170 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000171 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000172
173 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000174 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000176 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 RParenLoc = rparenloc;
179}
180
Argyrios Kyrtzidisba0a9002009-07-14 03:19:21 +0000181CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
182 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000183 SubExprs = new (C) Stmt*[1];
184}
185
Douglas Gregor42602bb2009-08-07 06:08:38 +0000186void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000187 DestroyChildren(C);
188 if (SubExprs) C.Deallocate(SubExprs);
189 this->~CallExpr();
190 C.Deallocate(this);
191}
192
Zhongxing Xua0042542009-07-17 07:29:51 +0000193FunctionDecl *CallExpr::getDirectCallee() {
194 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000195 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000196 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000197
198 return 0;
199}
200
Chris Lattnerd18b3292007-12-28 05:25:02 +0000201/// setNumArgs - This changes the number of arguments present in this call.
202/// Any orphaned expressions are deleted by this, and any new operands are set
203/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000204void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000205 // No change, just return.
206 if (NumArgs == getNumArgs()) return;
207
208 // If shrinking # arguments, just delete the extras and forgot them.
209 if (NumArgs < getNumArgs()) {
210 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000211 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000212 this->NumArgs = NumArgs;
213 return;
214 }
215
216 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000217 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000218 // Copy over args.
219 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
220 NewSubExprs[i] = SubExprs[i];
221 // Null out new args.
222 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
223 NewSubExprs[i] = 0;
224
Douglas Gregor88c9a462009-04-17 21:46:47 +0000225 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000226 SubExprs = NewSubExprs;
227 this->NumArgs = NumArgs;
228}
229
Chris Lattnercb888962008-10-06 05:00:53 +0000230/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
231/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000232unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000233 // All simple function calls (e.g. func()) are implicitly cast to pointer to
234 // function. As a result, we try and obtain the DeclRefExpr from the
235 // ImplicitCastExpr.
236 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
237 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000238 return 0;
239
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000240 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
241 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000242 return 0;
243
Anders Carlssonbcba2012008-01-31 02:13:57 +0000244 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
245 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000246 return 0;
247
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000248 if (!FDecl->getIdentifier())
249 return 0;
250
Douglas Gregor3c385e52009-02-14 18:57:46 +0000251 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000252}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000253
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000254QualType CallExpr::getCallReturnType() const {
255 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000256 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000257 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000258 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000259 CalleeType = BPT->getPointeeType();
260
261 const FunctionType *FnType = CalleeType->getAsFunctionType();
262 return FnType->getResultType();
263}
Chris Lattnercb888962008-10-06 05:00:53 +0000264
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000265MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
266 SourceRange qualrange, NamedDecl *memberdecl,
267 SourceLocation l, QualType ty)
268 : Expr(MemberExprClass, ty,
269 base->isTypeDependent() || (qual && qual->isDependent()),
270 base->isValueDependent() || (qual && qual->isDependent())),
271 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
272 HasQualifier(qual != 0) {
273 // Initialize the qualifier, if any.
274 if (HasQualifier) {
275 NameQualifier *NQ = getMemberQualifier();
276 NQ->NNS = qual;
277 NQ->Range = qualrange;
278 }
279}
280
281MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
282 NestedNameSpecifier *qual,
283 SourceRange qualrange,
284 NamedDecl *memberdecl,
285 SourceLocation l, QualType ty) {
286 std::size_t Size = sizeof(MemberExpr);
287 if (qual != 0)
288 Size += sizeof(NameQualifier);
289
290 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
291 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
292 ty);
293}
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
296/// corresponds to, e.g. "<<=".
297const char *BinaryOperator::getOpcodeStr(Opcode Op) {
298 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000299 case PtrMemD: return ".*";
300 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 case Mul: return "*";
302 case Div: return "/";
303 case Rem: return "%";
304 case Add: return "+";
305 case Sub: return "-";
306 case Shl: return "<<";
307 case Shr: return ">>";
308 case LT: return "<";
309 case GT: return ">";
310 case LE: return "<=";
311 case GE: return ">=";
312 case EQ: return "==";
313 case NE: return "!=";
314 case And: return "&";
315 case Xor: return "^";
316 case Or: return "|";
317 case LAnd: return "&&";
318 case LOr: return "||";
319 case Assign: return "=";
320 case MulAssign: return "*=";
321 case DivAssign: return "/=";
322 case RemAssign: return "%=";
323 case AddAssign: return "+=";
324 case SubAssign: return "-=";
325 case ShlAssign: return "<<=";
326 case ShrAssign: return ">>=";
327 case AndAssign: return "&=";
328 case XorAssign: return "^=";
329 case OrAssign: return "|=";
330 case Comma: return ",";
331 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000332
333 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334}
335
Douglas Gregor063daf62009-03-13 18:40:31 +0000336BinaryOperator::Opcode
337BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
338 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000339 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000340 case OO_Plus: return Add;
341 case OO_Minus: return Sub;
342 case OO_Star: return Mul;
343 case OO_Slash: return Div;
344 case OO_Percent: return Rem;
345 case OO_Caret: return Xor;
346 case OO_Amp: return And;
347 case OO_Pipe: return Or;
348 case OO_Equal: return Assign;
349 case OO_Less: return LT;
350 case OO_Greater: return GT;
351 case OO_PlusEqual: return AddAssign;
352 case OO_MinusEqual: return SubAssign;
353 case OO_StarEqual: return MulAssign;
354 case OO_SlashEqual: return DivAssign;
355 case OO_PercentEqual: return RemAssign;
356 case OO_CaretEqual: return XorAssign;
357 case OO_AmpEqual: return AndAssign;
358 case OO_PipeEqual: return OrAssign;
359 case OO_LessLess: return Shl;
360 case OO_GreaterGreater: return Shr;
361 case OO_LessLessEqual: return ShlAssign;
362 case OO_GreaterGreaterEqual: return ShrAssign;
363 case OO_EqualEqual: return EQ;
364 case OO_ExclaimEqual: return NE;
365 case OO_LessEqual: return LE;
366 case OO_GreaterEqual: return GE;
367 case OO_AmpAmp: return LAnd;
368 case OO_PipePipe: return LOr;
369 case OO_Comma: return Comma;
370 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000371 }
372}
373
374OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
375 static const OverloadedOperatorKind OverOps[] = {
376 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
377 OO_Star, OO_Slash, OO_Percent,
378 OO_Plus, OO_Minus,
379 OO_LessLess, OO_GreaterGreater,
380 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
381 OO_EqualEqual, OO_ExclaimEqual,
382 OO_Amp,
383 OO_Caret,
384 OO_Pipe,
385 OO_AmpAmp,
386 OO_PipePipe,
387 OO_Equal, OO_StarEqual,
388 OO_SlashEqual, OO_PercentEqual,
389 OO_PlusEqual, OO_MinusEqual,
390 OO_LessLessEqual, OO_GreaterGreaterEqual,
391 OO_AmpEqual, OO_CaretEqual,
392 OO_PipeEqual,
393 OO_Comma
394 };
395 return OverOps[Opc];
396}
397
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000398InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000399 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000400 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000401 : Expr(InitListExprClass, QualType(),
402 hasAnyTypeDependentArguments(initExprs, numInits),
403 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000404 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000405 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000406
407 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000408}
Reid Spencer5f016e22007-07-11 17:01:13 +0000409
Douglas Gregorfa219202009-03-20 23:58:33 +0000410void InitListExpr::reserveInits(unsigned NumInits) {
411 if (NumInits > InitExprs.size())
412 InitExprs.reserve(NumInits);
413}
414
Douglas Gregor4c678342009-01-28 21:54:33 +0000415void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000416 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000417 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000418 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000419 InitExprs.resize(NumInits, 0);
420}
421
422Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
423 if (Init >= InitExprs.size()) {
424 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
425 InitExprs.back() = expr;
426 return 0;
427 }
428
429 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
430 InitExprs[Init] = expr;
431 return Result;
432}
433
Steve Naroffbfdcae62008-09-04 15:31:07 +0000434/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000435///
436const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000437 return getType()->getAs<BlockPointerType>()->
Steve Naroff4eb206b2008-09-03 18:15:37 +0000438 getPointeeType()->getAsFunctionType();
439}
440
Steve Naroff56ee6892008-10-08 17:01:13 +0000441SourceLocation BlockExpr::getCaretLocation() const {
442 return TheBlock->getCaretLocation();
443}
Douglas Gregor72971342009-04-18 00:02:19 +0000444const Stmt *BlockExpr::getBody() const {
445 return TheBlock->getBody();
446}
447Stmt *BlockExpr::getBody() {
448 return TheBlock->getBody();
449}
Steve Naroff56ee6892008-10-08 17:01:13 +0000450
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452//===----------------------------------------------------------------------===//
453// Generic Expression Routines
454//===----------------------------------------------------------------------===//
455
Chris Lattner026dc962009-02-14 07:37:35 +0000456/// isUnusedResultAWarning - Return true if this immediate expression should
457/// be warned about if the result is unused. If so, fill in Loc and Ranges
458/// with location to warn on and the source range[s] to report with the
459/// warning.
460bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000461 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000462 // Don't warn if the expr is type dependent. The type could end up
463 // instantiating to void.
464 if (isTypeDependent())
465 return false;
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 switch (getStmtClass()) {
468 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000469 Loc = getExprLoc();
470 R1 = getSourceRange();
471 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000473 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000474 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 case UnaryOperatorClass: {
476 const UnaryOperator *UO = cast<UnaryOperator>(this);
477
478 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000479 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 case UnaryOperator::PostInc:
481 case UnaryOperator::PostDec:
482 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000483 case UnaryOperator::PreDec: // ++/--
484 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 case UnaryOperator::Deref:
486 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000487 if (getType().isVolatileQualified())
488 return false;
489 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 case UnaryOperator::Real:
491 case UnaryOperator::Imag:
492 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000493 if (UO->getSubExpr()->getType().isVolatileQualified())
494 return false;
495 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 case UnaryOperator::Extension:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000497 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 }
Chris Lattner026dc962009-02-14 07:37:35 +0000499 Loc = UO->getOperatorLoc();
500 R1 = UO->getSubExpr()->getSourceRange();
501 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000503 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000504 const BinaryOperator *BO = cast<BinaryOperator>(this);
505 // Consider comma to have side effects if the LHS or RHS does.
506 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000507 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
508 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000509
Chris Lattner026dc962009-02-14 07:37:35 +0000510 if (BO->isAssignmentOp())
511 return false;
512 Loc = BO->getOperatorLoc();
513 R1 = BO->getLHS()->getSourceRange();
514 R2 = BO->getRHS()->getSourceRange();
515 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000516 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000517 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000518 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000520 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000521 // The condition must be evaluated, but if either the LHS or RHS is a
522 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000523 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor68584ed2009-06-18 16:11:24 +0000524 if (Exp->getLHS() &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000526 return true;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000527 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000528 }
529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000531 // If the base pointer or element is to a volatile pointer/field, accessing
532 // it is a side effect.
533 if (getType().isVolatileQualified())
534 return false;
535 Loc = cast<MemberExpr>(this)->getMemberLoc();
536 R1 = SourceRange(Loc, Loc);
537 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
538 return true;
539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 case ArraySubscriptExprClass:
541 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000542 // it is a side effect.
543 if (getType().isVolatileQualified())
544 return false;
545 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
546 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
547 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
548 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000551 case CXXOperatorCallExprClass:
552 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000553 // If this is a direct call, get the callee.
554 const CallExpr *CE = cast<CallExpr>(this);
555 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
556 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
557 // If the callee has attribute pure, const, or warn_unused_result, warn
558 // about it. void foo() { strlen("bar"); } should warn.
559 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000560 if (FD->getAttr<WarnUnusedResultAttr>() ||
561 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000562 Loc = CE->getCallee()->getLocStart();
563 R1 = CE->getCallee()->getSourceRange();
564
565 if (unsigned NumArgs = CE->getNumArgs())
566 R2 = SourceRange(CE->getArg(0)->getLocStart(),
567 CE->getArg(NumArgs-1)->getLocEnd());
568 return true;
569 }
570 }
571 return false;
572 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000573 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000574 return false;
Chris Lattnera50089e2009-08-16 16:45:18 +0000575
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000576 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000577#if 0
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000578 const ObjCImplicitSetterGetterRefExpr *Ref =
579 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000580 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000581 Loc = Ref->getLocation();
582 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
583 if (Ref->getBase())
584 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000585#else
586 Loc = getExprLoc();
587 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000588#endif
589 return true;
590 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000591 case StmtExprClass: {
592 // Statement exprs don't logically have side effects themselves, but are
593 // sometimes used in macros in ways that give them a type that is unused.
594 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
595 // however, if the result of the stmt expr is dead, we don't want to emit a
596 // warning.
597 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
598 if (!CS->body_empty())
599 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000600 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000601
602 Loc = cast<StmtExpr>(this)->getLParenLoc();
603 R1 = getSourceRange();
604 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000605 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000606 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000607 // If this is an explicit cast to void, allow it. People do this when they
608 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000609 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000610 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000611 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
612 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
613 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000614 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 // If this is a cast to void, check the operand. Otherwise, the result of
616 // the cast is unused.
617 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000618 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000619 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000620 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
621 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
622 return true;
623
Eli Friedman4be1f472008-05-19 21:24:43 +0000624 case ImplicitCastExprClass:
625 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000626 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000627 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000628
Chris Lattner04421082008-04-08 04:40:51 +0000629 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000630 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000631 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000632
633 case CXXNewExprClass:
634 // FIXME: In theory, there might be new expressions that don't have side
635 // effects (e.g. a placement new with an uninitialized POD).
636 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000637 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000638 case CXXBindTemporaryExprClass:
639 return cast<CXXBindTemporaryExpr>(this)
640 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000641 case CXXExprWithTemporariesClass:
642 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000643 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000644 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000645}
646
Douglas Gregorba7e2102008-10-22 15:04:37 +0000647/// DeclCanBeLvalue - Determine whether the given declaration can be
648/// an lvalue. This is a helper routine for isLvalue.
649static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000650 // C++ [temp.param]p6:
651 // A non-type non-reference template-parameter is not an lvalue.
652 if (const NonTypeTemplateParmDecl *NTTParm
653 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
654 return NTTParm->getType()->isReferenceType();
655
Douglas Gregor44b43212008-12-11 16:49:14 +0000656 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000657 // C++ 3.10p2: An lvalue refers to an object or function.
658 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000659 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
660 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000661}
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
664/// incomplete type other than void. Nonarray expressions that can be lvalues:
665/// - name, where name must be a variable
666/// - e[i]
667/// - (e), where e must be an lvalue
668/// - e.name, where e must be an lvalue
669/// - e->name
670/// - *e, the type of e cannot be a function type
671/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000672/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000673/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000674///
Chris Lattner28be73f2008-07-26 21:30:36 +0000675Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000676 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
677
678 isLvalueResult Res = isLvalueInternal(Ctx);
679 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
680 return Res;
681
Douglas Gregor98cd5992008-10-21 23:43:52 +0000682 // first, check the type (C99 6.3.2.1). Expressions with function
683 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000684 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 return LV_NotObjectType;
686
Steve Naroffacb818a2008-02-10 01:39:04 +0000687 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000688 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000689 return LV_IncompleteVoidType;
690
Eli Friedman53202852009-05-03 22:36:05 +0000691 return LV_Valid;
692}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000693
Eli Friedman53202852009-05-03 22:36:05 +0000694// Check whether the expression can be sanely treated like an l-value
695Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000697 case StringLiteralClass: // C99 6.5.1p4
698 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000699 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
701 // For vectors, make sure base is an lvalue (i.e. not a function call).
702 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000703 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000705 case DeclRefExprClass:
706 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000707 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
708 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 return LV_Valid;
710 break;
Chris Lattner41110242008-06-17 18:05:57 +0000711 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000712 case BlockDeclRefExprClass: {
713 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000714 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000715 return LV_Valid;
716 break;
717 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000718 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000720 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
721 NamedDecl *Member = m->getMemberDecl();
722 // C++ [expr.ref]p4:
723 // If E2 is declared to have type "reference to T", then E1.E2
724 // is an lvalue.
725 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
726 if (Value->getType()->isReferenceType())
727 return LV_Valid;
728
729 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000730 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000731 return LV_Valid;
732
733 // -- If E2 is a non-static data member [...]. If E1 is an
734 // lvalue, then E1.E2 is an lvalue.
735 if (isa<FieldDecl>(Member))
736 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
737
738 // -- If it refers to a static member function [...], then
739 // E1.E2 is an lvalue.
740 // -- Otherwise, if E1.E2 refers to a non-static member
741 // function [...], then E1.E2 is not an lvalue.
742 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
743 return Method->isStatic()? LV_Valid : LV_MemberFunction;
744
745 // -- If E2 is a member enumerator [...], the expression E1.E2
746 // is not an lvalue.
747 if (isa<EnumConstantDecl>(Member))
748 return LV_InvalidExpression;
749
750 // Not an lvalue.
751 return LV_InvalidExpression;
752 }
753
754 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000755 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000756 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000757 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000759 return LV_Valid; // C99 6.5.3p4
760
761 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000762 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
763 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000764 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000765
766 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
767 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
768 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
769 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000771 case ImplicitCastExprClass:
772 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
773 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000775 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000776 case BinaryOperatorClass:
777 case CompoundAssignOperatorClass: {
778 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000779
780 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
781 BinOp->getOpcode() == BinaryOperator::Comma)
782 return BinOp->getRHS()->isLvalue(Ctx);
783
Sebastian Redl22460502009-02-07 00:15:38 +0000784 // C++ [expr.mptr.oper]p6
785 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
786 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
787 !BinOp->getType()->isFunctionType())
788 return BinOp->getLHS()->isLvalue(Ctx);
789
Douglas Gregorbf3af052008-11-13 20:12:29 +0000790 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000791 return LV_InvalidExpression;
792
Douglas Gregorbf3af052008-11-13 20:12:29 +0000793 if (Ctx.getLangOptions().CPlusPlus)
794 // C++ [expr.ass]p1:
795 // The result of an assignment operation [...] is an lvalue.
796 return LV_Valid;
797
798
799 // C99 6.5.16:
800 // An assignment expression [...] is not an lvalue.
801 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000802 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000803 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000804 case CXXOperatorCallExprClass:
805 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000806 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000807 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000808 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000809 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
810 if (ReturnType->isLValueReferenceType())
811 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000812
Douglas Gregor9d293df2008-10-28 00:22:11 +0000813 break;
814 }
Steve Naroffe6386392007-12-05 04:00:10 +0000815 case CompoundLiteralExprClass: // C99 6.5.2.5p5
816 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000817 case ChooseExprClass:
818 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000819 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000820 case ExtVectorElementExprClass:
821 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000822 return LV_DuplicateVectorComponents;
823 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000824 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
825 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000826 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
827 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000828 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000829 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000830 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000831 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000832 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000833 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000834 case CXXConditionDeclExprClass:
835 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000836 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000837 case CXXFunctionalCastExprClass:
838 case CXXStaticCastExprClass:
839 case CXXDynamicCastExprClass:
840 case CXXReinterpretCastExprClass:
841 case CXXConstCastExprClass:
842 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000843 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000844 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
845 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000846 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
847 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000848 return LV_Valid;
849 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000850 case CXXTypeidExprClass:
851 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
852 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +0000853 case CXXBindTemporaryExprClass:
854 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
855 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +0000856 case ConditionalOperatorClass: {
857 // Complicated handling is only for C++.
858 if (!Ctx.getLangOptions().CPlusPlus)
859 return LV_InvalidExpression;
860
861 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
862 // everywhere there's an object converted to an rvalue. Also, any other
863 // casts should be wrapped by ImplicitCastExprs. There's just the special
864 // case involving throws to work out.
865 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000866 Expr *True = Cond->getTrueExpr();
867 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +0000868 // C++0x 5.16p2
869 // If either the second or the third operand has type (cv) void, [...]
870 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000871 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +0000872 return LV_InvalidExpression;
873
874 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000875 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +0000876 return LV_InvalidExpression;
877
878 // That's it.
879 return LV_Valid;
880 }
881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 default:
883 break;
884 }
885 return LV_InvalidExpression;
886}
887
888/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
889/// does not have an incomplete type, does not have a const-qualified type, and
890/// if it is a structure or union, does not have any member (including,
891/// recursively, any member or element of all contained aggregates or unions)
892/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000893Expr::isModifiableLvalueResult
894Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000895 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000896
897 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000898 case LV_Valid:
899 // C++ 3.10p11: Functions cannot be modified, but pointers to
900 // functions can be modifiable.
901 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
902 return MLV_NotObjectType;
903 break;
904
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 case LV_NotObjectType: return MLV_NotObjectType;
906 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000907 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000908 case LV_InvalidExpression:
909 // If the top level is a C-style cast, and the subexpression is a valid
910 // lvalue, then this is probably a use of the old-school "cast as lvalue"
911 // GCC extension. We don't support it, but we want to produce good
912 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000913 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
914 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
915 if (Loc)
916 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000917 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000918 }
919 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000920 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000921 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000923
924 // The following is illegal:
925 // void takeclosure(void (^C)(void));
926 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
927 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000928 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000929 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
930 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
931 return MLV_NotBlockQualified;
932 }
933
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000934 QualType CT = Ctx.getCanonicalType(getType());
935
936 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000938 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000940 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 return MLV_IncompleteType;
942
Ted Kremenek6217b802009-07-29 21:53:49 +0000943 if (const RecordType *r = CT->getAs<RecordType>()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 if (r->hasConstFields())
945 return MLV_ConstQualified;
946 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000947
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000948 // Assigning to an 'implicit' property?
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000949 else if (isa<ObjCImplicitSetterGetterRefExpr>(this)) {
950 const ObjCImplicitSetterGetterRefExpr* Expr =
951 cast<ObjCImplicitSetterGetterRefExpr>(this);
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000952 if (Expr->getSetterMethod() == 0)
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000953 return MLV_NoSetterProperty;
954 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 return MLV_Valid;
956}
957
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000958/// isOBJCGCCandidate - Check if an expression is objc gc'able.
959///
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000960bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000961 switch (getStmtClass()) {
962 default:
963 return false;
964 case ObjCIvarRefExprClass:
965 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000966 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000967 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000968 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000969 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000970 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000971 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000972 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000973 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000974 case DeclRefExprClass:
975 case QualifiedDeclRefExprClass: {
976 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000977 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
978 if (VD->hasGlobalStorage())
979 return true;
980 QualType T = VD->getType();
981 // dereferencing to an object pointer is always a gc'able candidate
982 if (T->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +0000983 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000984 return true;
985
986 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000987 return false;
988 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000989 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000990 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000991 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000992 }
993 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000994 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000995 }
996}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000997Expr* Expr::IgnoreParens() {
998 Expr* E = this;
999 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1000 E = P->getSubExpr();
1001
1002 return E;
1003}
1004
Chris Lattner56f34942008-02-13 01:02:39 +00001005/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1006/// or CastExprs or ImplicitCastExprs, returning their operand.
1007Expr *Expr::IgnoreParenCasts() {
1008 Expr *E = this;
1009 while (true) {
1010 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1011 E = P->getSubExpr();
1012 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1013 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001014 else
1015 return E;
1016 }
1017}
1018
Chris Lattnerecdd8412009-03-13 17:28:01 +00001019/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1020/// value (including ptr->int casts of the same size). Strip off any
1021/// ParenExpr or CastExprs, returning their operand.
1022Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1023 Expr *E = this;
1024 while (true) {
1025 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1026 E = P->getSubExpr();
1027 continue;
1028 }
1029
1030 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1031 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1032 // ptr<->int casts of the same width. We also ignore all identify casts.
1033 Expr *SE = P->getSubExpr();
1034
1035 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1036 E = SE;
1037 continue;
1038 }
1039
1040 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1041 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1042 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1043 E = SE;
1044 continue;
1045 }
1046 }
1047
1048 return E;
1049 }
1050}
1051
1052
Douglas Gregor898574e2008-12-05 23:32:09 +00001053/// hasAnyTypeDependentArguments - Determines if any of the expressions
1054/// in Exprs is type-dependent.
1055bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1056 for (unsigned I = 0; I < NumExprs; ++I)
1057 if (Exprs[I]->isTypeDependent())
1058 return true;
1059
1060 return false;
1061}
1062
1063/// hasAnyValueDependentArguments - Determines if any of the expressions
1064/// in Exprs is value-dependent.
1065bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1066 for (unsigned I = 0; I < NumExprs; ++I)
1067 if (Exprs[I]->isValueDependent())
1068 return true;
1069
1070 return false;
1071}
1072
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001073bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001074 // This function is attempting whether an expression is an initializer
1075 // which can be evaluated at compile-time. isEvaluatable handles most
1076 // of the cases, but it can't deal with some initializer-specific
1077 // expressions, and it can't deal with aggregates; we deal with those here,
1078 // and fall back to isEvaluatable for the other cases.
1079
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001080 // FIXME: This function assumes the variable being assigned to
1081 // isn't a reference type!
1082
Anders Carlssone8a32b82008-11-24 05:23:59 +00001083 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001084 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001085 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001086 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001087 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001088 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001089 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001090 // This handles gcc's extension that allows global initializers like
1091 // "struct x {int x;} x = (struct x) {};".
1092 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001093 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001094 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001095 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001096 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001097 // FIXME: This doesn't deal with fields with reference types correctly.
1098 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1099 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001100 const InitListExpr *Exp = cast<InitListExpr>(this);
1101 unsigned numInits = Exp->getNumInits();
1102 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001103 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001104 return false;
1105 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001106 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001107 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001108 case ImplicitValueInitExprClass:
1109 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001110 case ParenExprClass: {
1111 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1112 }
1113 case UnaryOperatorClass: {
1114 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1115 if (Exp->getOpcode() == UnaryOperator::Extension)
1116 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1117 break;
1118 }
Chris Lattner81045d82009-04-21 05:19:11 +00001119 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001120 case CStyleCastExprClass:
1121 // Handle casts with a destination that's a struct or union; this
1122 // deals with both the gcc no-op struct cast extension and the
1123 // cast-to-union extension.
1124 if (getType()->isRecordType())
1125 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1126 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001127 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001128 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001129}
1130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001132/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001133
1134/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1135/// comma, etc
1136///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001137/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1138/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1139/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001140
Eli Friedmane28d7192009-02-26 09:29:13 +00001141// CheckICE - This function does the fundamental ICE checking: the returned
1142// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1143// Note that to reduce code duplication, this helper does no evaluation
1144// itself; the caller checks whether the expression is evaluatable, and
1145// in the rare cases where CheckICE actually cares about the evaluated
1146// value, it calls into Evalute.
1147//
1148// Meanings of Val:
1149// 0: This expression is an ICE if it can be evaluated by Evaluate.
1150// 1: This expression is not an ICE, but if it isn't evaluated, it's
1151// a legal subexpression for an ICE. This return value is used to handle
1152// the comma operator in C99 mode.
1153// 2: This expression is not an ICE, and is not a legal subexpression for one.
1154
1155struct ICEDiag {
1156 unsigned Val;
1157 SourceLocation Loc;
1158
1159 public:
1160 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1161 ICEDiag() : Val(0) {}
1162};
1163
1164ICEDiag NoDiag() { return ICEDiag(); }
1165
Eli Friedman60ce9632009-02-27 04:07:58 +00001166static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1167 Expr::EvalResult EVResult;
1168 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1169 !EVResult.Val.isInt()) {
1170 return ICEDiag(2, E->getLocStart());
1171 }
1172 return NoDiag();
1173}
1174
Eli Friedmane28d7192009-02-26 09:29:13 +00001175static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001176 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001177 if (!E->getType()->isIntegralType()) {
1178 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001179 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001180
1181 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001183 return ICEDiag(2, E->getLocStart());
1184 case Expr::ParenExprClass:
1185 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1186 case Expr::IntegerLiteralClass:
1187 case Expr::CharacterLiteralClass:
1188 case Expr::CXXBoolLiteralExprClass:
1189 case Expr::CXXZeroInitValueExprClass:
1190 case Expr::TypesCompatibleExprClass:
1191 case Expr::UnaryTypeTraitExprClass:
1192 return NoDiag();
1193 case Expr::CallExprClass:
1194 case Expr::CXXOperatorCallExprClass: {
1195 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001196 if (CE->isBuiltinCall(Ctx))
1197 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001198 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001199 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001200 case Expr::DeclRefExprClass:
1201 case Expr::QualifiedDeclRefExprClass:
1202 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1203 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001204 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001205 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001206 // C++ 7.1.5.1p2
1207 // A variable of non-volatile const-qualified integral or enumeration
1208 // type initialized by an ICE can be used in ICEs.
1209 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001210 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001211 if (Dcl->isInitKnownICE()) {
1212 // We have already checked whether this subexpression is an
1213 // integral constant expression.
1214 if (Dcl->isInitICE())
1215 return NoDiag();
1216 else
1217 return ICEDiag(2, E->getLocStart());
1218 }
1219
1220 if (const Expr *Init = Dcl->getInit()) {
1221 ICEDiag Result = CheckICE(Init, Ctx);
1222 // Cache the result of the ICE test.
1223 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1224 return Result;
1225 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001226 }
1227 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001228 return ICEDiag(2, E->getLocStart());
1229 case Expr::UnaryOperatorClass: {
1230 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001233 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001235 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001239 case UnaryOperator::Real:
1240 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001241 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001242 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001243 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1244 // Evaluate matches the proposed gcc behavior for cases like
1245 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1246 // compliance: we should warn earlier for offsetof expressions with
1247 // array subscripts that aren't ICEs, and if the array subscripts
1248 // are ICEs, the value of the offsetof must be an integer constant.
1249 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001252 case Expr::SizeOfAlignOfExprClass: {
1253 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1254 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1255 return ICEDiag(2, E->getLocStart());
1256 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001258 case Expr::BinaryOperatorClass: {
1259 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 switch (Exp->getOpcode()) {
1261 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001262 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001266 case BinaryOperator::Add:
1267 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001270 case BinaryOperator::LT:
1271 case BinaryOperator::GT:
1272 case BinaryOperator::LE:
1273 case BinaryOperator::GE:
1274 case BinaryOperator::EQ:
1275 case BinaryOperator::NE:
1276 case BinaryOperator::And:
1277 case BinaryOperator::Xor:
1278 case BinaryOperator::Or:
1279 case BinaryOperator::Comma: {
1280 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1281 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001282 if (Exp->getOpcode() == BinaryOperator::Div ||
1283 Exp->getOpcode() == BinaryOperator::Rem) {
1284 // Evaluate gives an error for undefined Div/Rem, so make sure
1285 // we don't evaluate one.
1286 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1287 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1288 if (REval == 0)
1289 return ICEDiag(1, E->getLocStart());
1290 if (REval.isSigned() && REval.isAllOnesValue()) {
1291 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1292 if (LEval.isMinSignedValue())
1293 return ICEDiag(1, E->getLocStart());
1294 }
1295 }
1296 }
1297 if (Exp->getOpcode() == BinaryOperator::Comma) {
1298 if (Ctx.getLangOptions().C99) {
1299 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1300 // if it isn't evaluated.
1301 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1302 return ICEDiag(1, E->getLocStart());
1303 } else {
1304 // In both C89 and C++, commas in ICEs are illegal.
1305 return ICEDiag(2, E->getLocStart());
1306 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001307 }
1308 if (LHSResult.Val >= RHSResult.Val)
1309 return LHSResult;
1310 return RHSResult;
1311 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001313 case BinaryOperator::LOr: {
1314 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1315 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1316 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1317 // Rare case where the RHS has a comma "side-effect"; we need
1318 // to actually check the condition to see whether the side
1319 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001320 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001321 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001322 return RHSResult;
1323 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001324 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001325
Eli Friedmane28d7192009-02-26 09:29:13 +00001326 if (LHSResult.Val >= RHSResult.Val)
1327 return LHSResult;
1328 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001330 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001332 case Expr::ImplicitCastExprClass:
1333 case Expr::CStyleCastExprClass:
1334 case Expr::CXXFunctionalCastExprClass: {
1335 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1336 if (SubExpr->getType()->isIntegralType())
1337 return CheckICE(SubExpr, Ctx);
1338 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1339 return NoDiag();
1340 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001342 case Expr::ConditionalOperatorClass: {
1343 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001344 // If the condition (ignoring parens) is a __builtin_constant_p call,
1345 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001346 // expression, and it is fully evaluated. This is an important GNU
1347 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001348 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001349 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001350 Expr::EvalResult EVResult;
1351 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1352 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001353 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001354 }
1355 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001356 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001357 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1358 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1359 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1360 if (CondResult.Val == 2)
1361 return CondResult;
1362 if (TrueResult.Val == 2)
1363 return TrueResult;
1364 if (FalseResult.Val == 2)
1365 return FalseResult;
1366 if (CondResult.Val == 1)
1367 return CondResult;
1368 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1369 return NoDiag();
1370 // Rare case where the diagnostics depend on which side is evaluated
1371 // Note that if we get here, CondResult is 0, and at least one of
1372 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001373 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001374 return FalseResult;
1375 }
1376 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001378 case Expr::CXXDefaultArgExprClass:
1379 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001380 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001381 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001382 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001384}
Reid Spencer5f016e22007-07-11 17:01:13 +00001385
Eli Friedmane28d7192009-02-26 09:29:13 +00001386bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1387 SourceLocation *Loc, bool isEvaluated) const {
1388 ICEDiag d = CheckICE(this, Ctx);
1389 if (d.Val != 0) {
1390 if (Loc) *Loc = d.Loc;
1391 return false;
1392 }
1393 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001394 if (!Evaluate(EvalResult, Ctx))
1395 assert(0 && "ICE cannot be evaluated!");
1396 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1397 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001398 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 return true;
1400}
1401
Reid Spencer5f016e22007-07-11 17:01:13 +00001402/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1403/// integer constant expression with the value zero, or if this is one that is
1404/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001405bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1406{
Sebastian Redl07779722008-10-31 14:43:28 +00001407 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001408 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001409 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001410 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001411 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001412 QualType Pointee = PT->getPointeeType();
1413 if (Pointee.getCVRQualifiers() == 0 &&
1414 Pointee->isVoidType() && // to void*
1415 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001416 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001417 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001419 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1420 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001421 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001422 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1423 // Accept ((void*)0) as a null pointer constant, as many other
1424 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001425 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001426 } else if (const CXXDefaultArgExpr *DefaultArg
1427 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001428 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001429 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001430 } else if (isa<GNUNullExpr>(this)) {
1431 // The GNU __null extension is always a null pointer constant.
1432 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001433 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001434
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001435 // C++0x nullptr_t is always a null pointer constant.
1436 if (getType()->isNullPtrType())
1437 return true;
1438
Steve Naroffaa58f002008-01-14 16:10:57 +00001439 // This expression must be an integer type.
1440 if (!getType()->isIntegerType())
1441 return false;
1442
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 // If we have an integer constant expression, we need to *evaluate* it and
1444 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001445 llvm::APSInt Result;
1446 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001447}
Steve Naroff31a45842007-07-28 23:10:27 +00001448
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001449FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001450 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001451
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001452 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001453 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001454 if (Field->isBitField())
1455 return Field;
1456
1457 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1458 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1459 return BinOp->getLHS()->getBitField();
1460
1461 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001462}
1463
Chris Lattner2140e902009-02-16 22:14:05 +00001464/// isArrow - Return true if the base expression is a pointer to vector,
1465/// return false if the base expression is a vector.
1466bool ExtVectorElementExpr::isArrow() const {
1467 return getBase()->getType()->isPointerType();
1468}
1469
Nate Begeman213541a2008-04-18 23:10:10 +00001470unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001471 if (const VectorType *VT = getType()->getAsVectorType())
1472 return VT->getNumElements();
1473 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001474}
1475
Nate Begeman8a997642008-05-09 06:41:27 +00001476/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001477bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001478 const char *compStr = Accessor->getName();
1479 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001480
1481 // Halving swizzles do not contain duplicate elements.
1482 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1483 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1484 return false;
1485
1486 // Advance past s-char prefix on hex swizzles.
Nate Begeman131f4652009-06-25 21:06:09 +00001487 if (*compStr == 's' || *compStr == 'S') {
Nate Begeman190d6a22009-01-18 02:01:21 +00001488 compStr++;
1489 length--;
1490 }
Steve Narofffec0b492007-07-30 03:29:09 +00001491
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001492 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001493 const char *s = compStr+i;
1494 for (const char c = *s++; *s; s++)
1495 if (c == *s)
1496 return true;
1497 }
1498 return false;
1499}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001500
Nate Begeman8a997642008-05-09 06:41:27 +00001501/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001502void ExtVectorElementExpr::getEncodedElementAccess(
1503 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001504 const char *compStr = Accessor->getName();
Nate Begeman131f4652009-06-25 21:06:09 +00001505 if (*compStr == 's' || *compStr == 'S')
Nate Begeman353417a2009-01-18 01:47:54 +00001506 compStr++;
1507
1508 bool isHi = !strcmp(compStr, "hi");
1509 bool isLo = !strcmp(compStr, "lo");
1510 bool isEven = !strcmp(compStr, "even");
1511 bool isOdd = !strcmp(compStr, "odd");
1512
Nate Begeman8a997642008-05-09 06:41:27 +00001513 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1514 uint64_t Index;
1515
1516 if (isHi)
1517 Index = e + i;
1518 else if (isLo)
1519 Index = i;
1520 else if (isEven)
1521 Index = 2 * i;
1522 else if (isOdd)
1523 Index = 2 * i + 1;
1524 else
1525 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001526
Nate Begeman3b8d1162008-05-13 21:03:02 +00001527 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001528 }
Nate Begeman8a997642008-05-09 06:41:27 +00001529}
1530
Steve Naroff68d331a2007-09-27 14:38:14 +00001531// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001532ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001533 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001534 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001535 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001536 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001537 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001538 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001539 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001540 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001541 if (NumArgs) {
1542 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001543 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1544 }
Steve Naroff563477d2007-09-18 23:55:05 +00001545 LBracloc = LBrac;
1546 RBracloc = RBrac;
1547}
1548
Steve Naroff68d331a2007-09-27 14:38:14 +00001549// constructor for class messages.
1550// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001551ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001552 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001553 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001554 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001555 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001556 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001557 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001558 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001559 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001560 if (NumArgs) {
1561 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001562 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1563 }
Steve Naroff563477d2007-09-18 23:55:05 +00001564 LBracloc = LBrac;
1565 RBracloc = RBrac;
1566}
1567
Ted Kremenek4df728e2008-06-24 15:50:53 +00001568// constructor for class messages.
1569ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1570 QualType retType, ObjCMethodDecl *mproto,
1571 SourceLocation LBrac, SourceLocation RBrac,
1572 Expr **ArgExprs, unsigned nargs)
1573: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1574MethodProto(mproto) {
1575 NumArgs = nargs;
1576 SubExprs = new Stmt*[NumArgs+1];
1577 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1578 if (NumArgs) {
1579 for (unsigned i = 0; i != NumArgs; ++i)
1580 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1581 }
1582 LBracloc = LBrac;
1583 RBracloc = RBrac;
1584}
1585
1586ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1587 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1588 switch (x & Flags) {
1589 default:
1590 assert(false && "Invalid ObjCMessageExpr.");
1591 case IsInstMeth:
1592 return ClassInfo(0, 0);
1593 case IsClsMethDeclUnknown:
1594 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1595 case IsClsMethDeclKnown: {
1596 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1597 return ClassInfo(D, D->getIdentifier());
1598 }
1599 }
1600}
1601
Chris Lattner0389e6b2009-04-26 00:44:05 +00001602void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1603 if (CI.first == 0 && CI.second == 0)
1604 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1605 else if (CI.first == 0)
1606 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1607 else
1608 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1609}
1610
1611
Chris Lattner27437ca2007-10-25 00:29:32 +00001612bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001613 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001614}
1615
Nate Begeman888376a2009-08-12 02:28:50 +00001616void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1617 unsigned NumExprs) {
1618 if (SubExprs) C.Deallocate(SubExprs);
1619
1620 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001621 this->NumExprs = NumExprs;
1622 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Nate Begeman888376a2009-08-12 02:28:50 +00001623}
1624
1625void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1626 DestroyChildren(C);
1627 if (SubExprs) C.Deallocate(SubExprs);
1628 this->~ShuffleVectorExpr();
1629 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001630}
1631
Douglas Gregor42602bb2009-08-07 06:08:38 +00001632void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00001633 // Override default behavior of traversing children. If this has a type
1634 // operand and the type is a variable-length array, the child iteration
1635 // will iterate over the size expression. However, this expression belongs
1636 // to the type, not to this, so we don't want to delete it.
1637 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001638 if (isArgumentType()) {
1639 this->~SizeOfAlignOfExpr();
1640 C.Deallocate(this);
1641 }
Sebastian Redl05189992008-11-11 17:56:53 +00001642 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00001643 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001644}
1645
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001646//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001647// DesignatedInitExpr
1648//===----------------------------------------------------------------------===//
1649
1650IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1651 assert(Kind == FieldDesignator && "Only valid on a field designator");
1652 if (Field.NameOrField & 0x01)
1653 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1654 else
1655 return getField()->getIdentifier();
1656}
1657
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001658DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1659 const Designator *Designators,
1660 SourceLocation EqualOrColonLoc,
1661 bool GNUSyntax,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001662 Expr **IndexExprs,
1663 unsigned NumIndexExprs,
1664 Expr *Init)
1665 : Expr(DesignatedInitExprClass, Ty,
1666 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001667 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor9ea62762009-05-21 23:17:49 +00001668 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001669 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001670
1671 // Record the initializer itself.
1672 child_iterator Child = child_begin();
1673 *Child++ = Init;
1674
1675 // Copy the designators and their subexpressions, computing
1676 // value-dependence along the way.
1677 unsigned IndexIdx = 0;
1678 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001679 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001680
1681 if (this->Designators[I].isArrayDesignator()) {
1682 // Compute type- and value-dependence.
1683 Expr *Index = IndexExprs[IndexIdx];
1684 ValueDependent = ValueDependent ||
1685 Index->isTypeDependent() || Index->isValueDependent();
1686
1687 // Copy the index expressions into permanent storage.
1688 *Child++ = IndexExprs[IndexIdx++];
1689 } else if (this->Designators[I].isArrayRangeDesignator()) {
1690 // Compute type- and value-dependence.
1691 Expr *Start = IndexExprs[IndexIdx];
1692 Expr *End = IndexExprs[IndexIdx + 1];
1693 ValueDependent = ValueDependent ||
1694 Start->isTypeDependent() || Start->isValueDependent() ||
1695 End->isTypeDependent() || End->isValueDependent();
1696
1697 // Copy the start/end expressions into permanent storage.
1698 *Child++ = IndexExprs[IndexIdx++];
1699 *Child++ = IndexExprs[IndexIdx++];
1700 }
1701 }
1702
1703 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001704}
1705
Douglas Gregor05c13a32009-01-22 00:58:24 +00001706DesignatedInitExpr *
1707DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1708 unsigned NumDesignators,
1709 Expr **IndexExprs, unsigned NumIndexExprs,
1710 SourceLocation ColonOrEqualLoc,
1711 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001712 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001713 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001714 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1715 ColonOrEqualLoc, UsesColonSyntax,
1716 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001717}
1718
Douglas Gregord077d752009-04-16 00:55:48 +00001719DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1720 unsigned NumIndexExprs) {
1721 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1722 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1723 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1724}
1725
1726void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1727 unsigned NumDesigs) {
1728 if (Designators)
1729 delete [] Designators;
1730
1731 Designators = new Designator[NumDesigs];
1732 NumDesignators = NumDesigs;
1733 for (unsigned I = 0; I != NumDesigs; ++I)
1734 Designators[I] = Desigs[I];
1735}
1736
Douglas Gregor05c13a32009-01-22 00:58:24 +00001737SourceRange DesignatedInitExpr::getSourceRange() const {
1738 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001739 Designator &First =
1740 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001741 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001742 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001743 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1744 else
1745 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1746 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001747 StartLoc =
1748 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001749 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1750}
1751
Douglas Gregor05c13a32009-01-22 00:58:24 +00001752Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1753 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1754 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1755 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001756 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1757 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1758}
1759
1760Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1761 assert(D.Kind == Designator::ArrayRangeDesignator &&
1762 "Requires array range designator");
1763 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1764 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001765 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1766 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1767}
1768
1769Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1770 assert(D.Kind == Designator::ArrayRangeDesignator &&
1771 "Requires array range designator");
1772 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1773 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001774 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1775 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1776}
1777
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001778/// \brief Replaces the designator at index @p Idx with the series
1779/// of designators in [First, Last).
1780void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1781 const Designator *First,
1782 const Designator *Last) {
1783 unsigned NumNewDesignators = Last - First;
1784 if (NumNewDesignators == 0) {
1785 std::copy_backward(Designators + Idx + 1,
1786 Designators + NumDesignators,
1787 Designators + Idx);
1788 --NumNewDesignators;
1789 return;
1790 } else if (NumNewDesignators == 1) {
1791 Designators[Idx] = *First;
1792 return;
1793 }
1794
1795 Designator *NewDesignators
1796 = new Designator[NumDesignators - 1 + NumNewDesignators];
1797 std::copy(Designators, Designators + Idx, NewDesignators);
1798 std::copy(First, Last, NewDesignators + Idx);
1799 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1800 NewDesignators + Idx + NumNewDesignators);
1801 delete [] Designators;
1802 Designators = NewDesignators;
1803 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1804}
1805
Douglas Gregor42602bb2009-08-07 06:08:38 +00001806void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001807 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00001808 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001809}
1810
Nate Begeman2ef13e52009-08-10 23:49:36 +00001811ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
1812 Expr **exprs, unsigned nexprs,
1813 SourceLocation rparenloc)
1814: Expr(ParenListExprClass, QualType(),
1815 hasAnyTypeDependentArguments(exprs, nexprs),
1816 hasAnyValueDependentArguments(exprs, nexprs)),
1817 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
1818
1819 Exprs = new (C) Stmt*[nexprs];
1820 for (unsigned i = 0; i != nexprs; ++i)
1821 Exprs[i] = exprs[i];
1822}
1823
1824void ParenListExpr::DoDestroy(ASTContext& C) {
1825 DestroyChildren(C);
1826 if (Exprs) C.Deallocate(Exprs);
1827 this->~ParenListExpr();
1828 C.Deallocate(this);
1829}
1830
Douglas Gregor05c13a32009-01-22 00:58:24 +00001831//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001832// ExprIterator.
1833//===----------------------------------------------------------------------===//
1834
1835Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1836Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1837Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1838const Expr* ConstExprIterator::operator[](size_t idx) const {
1839 return cast<Expr>(I[idx]);
1840}
1841const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1842const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1843
1844//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001845// Child Iterators for iterating over subexpressions/substatements
1846//===----------------------------------------------------------------------===//
1847
1848// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001849Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1850Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001851
Steve Naroff7779db42007-11-12 14:29:37 +00001852// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001853Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1854Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001855
Steve Naroffe3e9add2008-06-02 23:03:37 +00001856// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001857Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1858Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001859
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001860// ObjCImplicitSetterGetterRefExpr
1861Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001862 return &Base;
1863}
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001864Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001865 return &Base+1;
1866}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001867
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001868// ObjCSuperExpr
1869Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1870Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1871
Steve Narofff242b1b2009-07-24 17:54:45 +00001872// ObjCIsaExpr
1873Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1874Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1875
Chris Lattnerd9f69102008-08-10 01:53:14 +00001876// PredefinedExpr
1877Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1878Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001879
1880// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001881Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1882Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001883
1884// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001885Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001886Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001887
1888// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001889Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1890Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001891
Chris Lattner5d661452007-08-26 03:42:43 +00001892// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001893Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1894Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001895
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001896// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001897Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1898Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001899
1900// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001901Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1902Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001903
1904// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001905Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1906Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001907
Sebastian Redl05189992008-11-11 17:56:53 +00001908// SizeOfAlignOfExpr
1909Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1910 // If this is of a type and the type is a VLA type (and not a typedef), the
1911 // size expression of the VLA needs to be treated as an executable expression.
1912 // Why isn't this weirdness documented better in StmtIterator?
1913 if (isArgumentType()) {
1914 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1915 getArgumentType().getTypePtr()))
1916 return child_iterator(T);
1917 return child_iterator();
1918 }
Sebastian Redld4575892008-12-03 23:17:54 +00001919 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001920}
Sebastian Redl05189992008-11-11 17:56:53 +00001921Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1922 if (isArgumentType())
1923 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001924 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001925}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001926
1927// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001928Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001929 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001930}
Ted Kremenek1237c672007-08-24 20:06:47 +00001931Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001932 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001933}
1934
1935// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001936Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001937 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001938}
Ted Kremenek1237c672007-08-24 20:06:47 +00001939Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001940 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001941}
Ted Kremenek1237c672007-08-24 20:06:47 +00001942
1943// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001944Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1945Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001946
Nate Begeman213541a2008-04-18 23:10:10 +00001947// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001948Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1949Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001950
1951// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001952Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1953Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001954
Ted Kremenek1237c672007-08-24 20:06:47 +00001955// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001956Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1957Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001958
1959// BinaryOperator
1960Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001961 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001962}
Ted Kremenek1237c672007-08-24 20:06:47 +00001963Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001964 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001965}
1966
1967// ConditionalOperator
1968Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001969 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001970}
Ted Kremenek1237c672007-08-24 20:06:47 +00001971Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001972 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001973}
1974
1975// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001976Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1977Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001978
Ted Kremenek1237c672007-08-24 20:06:47 +00001979// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001980Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1981Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001982
1983// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001984Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1985 return child_iterator();
1986}
1987
1988Stmt::child_iterator TypesCompatibleExpr::child_end() {
1989 return child_iterator();
1990}
Ted Kremenek1237c672007-08-24 20:06:47 +00001991
1992// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001993Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1994Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001995
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001996// GNUNullExpr
1997Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1998Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1999
Eli Friedmand38617c2008-05-14 19:38:39 +00002000// ShuffleVectorExpr
2001Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002002 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002003}
2004Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002005 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002006}
2007
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002008// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002009Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2010Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002011
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002012// InitListExpr
2013Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002014 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002015}
2016Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002017 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002018}
2019
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002020// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002021Stmt::child_iterator DesignatedInitExpr::child_begin() {
2022 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2023 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002024 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2025}
2026Stmt::child_iterator DesignatedInitExpr::child_end() {
2027 return child_iterator(&*child_begin() + NumSubExprs);
2028}
2029
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002030// ImplicitValueInitExpr
2031Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2032 return child_iterator();
2033}
2034
2035Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2036 return child_iterator();
2037}
2038
Nate Begeman2ef13e52009-08-10 23:49:36 +00002039// ParenListExpr
2040Stmt::child_iterator ParenListExpr::child_begin() {
2041 return &Exprs[0];
2042}
2043Stmt::child_iterator ParenListExpr::child_end() {
2044 return &Exprs[0]+NumExprs;
2045}
2046
Ted Kremenek1237c672007-08-24 20:06:47 +00002047// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002048Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002049 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002050}
2051Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002052 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002053}
Ted Kremenek1237c672007-08-24 20:06:47 +00002054
2055// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002056Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2057Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002058
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002059// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002060Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2061 return child_iterator();
2062}
2063Stmt::child_iterator ObjCSelectorExpr::child_end() {
2064 return child_iterator();
2065}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002066
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002067// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002068Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2069 return child_iterator();
2070}
2071Stmt::child_iterator ObjCProtocolExpr::child_end() {
2072 return child_iterator();
2073}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002074
Steve Naroff563477d2007-09-18 23:55:05 +00002075// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00002076Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002077 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002078}
2079Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002080 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002081}
2082
Steve Naroff4eb206b2008-09-03 18:15:37 +00002083// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002084Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2085Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002086
Ted Kremenek9da13f92008-09-26 23:24:14 +00002087Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2088Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }