blob: 54739780bec08a3eac243d27e7a097077e998fd8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000023#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// Primary Expressions.
28//===----------------------------------------------------------------------===//
29
Sebastian Redl8b0b4752009-05-16 18:50:46 +000030PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
31 return new (C) PredefinedExpr(Loc, getType(), Type);
32}
33
Anders Carlssona135fb42009-03-15 18:34:13 +000034IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
35 return new (C) IntegerLiteral(Value, getType(), Loc);
36}
37
Sebastian Redl8b0b4752009-05-16 18:50:46 +000038CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
39 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
40}
41
42FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
Chris Lattner001d64d2009-06-29 17:34:55 +000043 return new (C) FloatingLiteral(Value, IsExact, getType(), Loc);
Sebastian Redl8b0b4752009-05-16 18:50:46 +000044}
45
Douglas Gregord8ac4362009-05-18 22:38:38 +000046ImaginaryLiteral* ImaginaryLiteral::Clone(ASTContext &C) const {
47 // FIXME: Use virtual Clone(), once it is available
48 Expr *ClonedVal = 0;
49 if (const IntegerLiteral *IntLit = dyn_cast<IntegerLiteral>(Val))
50 ClonedVal = IntLit->Clone(C);
51 else
52 ClonedVal = cast<FloatingLiteral>(Val)->Clone(C);
53 return new (C) ImaginaryLiteral(ClonedVal, getType());
54}
55
Sebastian Redl8b0b4752009-05-16 18:50:46 +000056GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
57 return new (C) GNUNullExpr(getType(), TokenLoc);
58}
59
Chris Lattnerda8249e2008-06-07 22:13:43 +000060/// getValueAsApproximateDouble - This returns the value as an inaccurate
61/// double. Note that this may cause loss of precision, but is useful for
62/// debugging dumps, etc.
63double FloatingLiteral::getValueAsApproximateDouble() const {
64 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000065 bool ignored;
66 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
67 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000068 return V.convertToDouble();
69}
70
Chris Lattner2085fd62009-02-18 06:40:38 +000071StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
72 unsigned ByteLength, bool Wide,
73 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000074 const SourceLocation *Loc,
75 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000076 // Allocate enough space for the StringLiteral plus an array of locations for
77 // any concatenated string tokens.
78 void *Mem = C.Allocate(sizeof(StringLiteral)+
79 sizeof(SourceLocation)*(NumStrs-1),
80 llvm::alignof<StringLiteral>());
81 StringLiteral *SL = new (Mem) StringLiteral(Ty);
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000084 char *AStrData = new (C, 1) char[ByteLength];
85 memcpy(AStrData, StrData, ByteLength);
86 SL->StrData = AStrData;
87 SL->ByteLength = ByteLength;
88 SL->IsWide = Wide;
89 SL->TokLocs[0] = Loc[0];
90 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000091
Chris Lattner726e1682009-02-18 05:49:11 +000092 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000093 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
94 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000095}
96
Douglas Gregor673ecd62009-04-15 16:35:07 +000097StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
98 void *Mem = C.Allocate(sizeof(StringLiteral)+
99 sizeof(SourceLocation)*(NumStrs-1),
100 llvm::alignof<StringLiteral>());
101 StringLiteral *SL = new (Mem) StringLiteral(QualType());
102 SL->StrData = 0;
103 SL->ByteLength = 0;
104 SL->NumConcatenated = NumStrs;
105 return SL;
106}
107
Anders Carlssona135fb42009-03-15 18:34:13 +0000108StringLiteral* StringLiteral::Clone(ASTContext &C) const {
109 return Create(C, StrData, ByteLength, IsWide, getType(),
110 TokLocs, NumConcatenated);
111}
Chris Lattner726e1682009-02-18 05:49:11 +0000112
Ted Kremenek6e94ef52009-02-06 19:55:15 +0000113void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000114 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +0000115 this->~StringLiteral();
116 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000117}
118
Douglas Gregor673ecd62009-04-15 16:35:07 +0000119void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
120 if (StrData)
121 C.Deallocate(const_cast<char*>(StrData));
122
123 char *AStrData = new (C, 1) char[Len];
124 memcpy(AStrData, Str, Len);
125 StrData = AStrData;
126 ByteLength = Len;
127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
130/// corresponds to, e.g. "sizeof" or "[pre]++".
131const char *UnaryOperator::getOpcodeStr(Opcode Op) {
132 switch (Op) {
133 default: assert(0 && "Unknown unary operator");
134 case PostInc: return "++";
135 case PostDec: return "--";
136 case PreInc: return "++";
137 case PreDec: return "--";
138 case AddrOf: return "&";
139 case Deref: return "*";
140 case Plus: return "+";
141 case Minus: return "-";
142 case Not: return "~";
143 case LNot: return "!";
144 case Real: return "__real";
145 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000147 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
149}
150
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000151UnaryOperator::Opcode
152UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
153 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000154 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000155 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
156 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
157 case OO_Amp: return AddrOf;
158 case OO_Star: return Deref;
159 case OO_Plus: return Plus;
160 case OO_Minus: return Minus;
161 case OO_Tilde: return Not;
162 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000163 }
164}
165
166OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
167 switch (Opc) {
168 case PostInc: case PreInc: return OO_PlusPlus;
169 case PostDec: case PreDec: return OO_MinusMinus;
170 case AddrOf: return OO_Amp;
171 case Deref: return OO_Star;
172 case Plus: return OO_Plus;
173 case Minus: return OO_Minus;
174 case Not: return OO_Tilde;
175 case LNot: return OO_Exclaim;
176 default: return OO_None;
177 }
178}
179
180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181//===----------------------------------------------------------------------===//
182// Postfix Operators.
183//===----------------------------------------------------------------------===//
184
Ted Kremenek668bf912009-02-09 20:51:47 +0000185CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000186 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000187 : Expr(SC, t,
188 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000189 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000190 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000191
192 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000193 SubExprs[FN] = fn;
194 for (unsigned i = 0; i != numargs; ++i)
195 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000196
Douglas Gregorb4609802008-11-14 16:09:21 +0000197 RParenLoc = rparenloc;
198}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000199
Ted Kremenek668bf912009-02-09 20:51:47 +0000200CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
201 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000202 : Expr(CallExprClass, t,
203 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000204 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000205 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000206
207 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000208 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000210 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 RParenLoc = rparenloc;
213}
214
Argyrios Kyrtzidisba0a9002009-07-14 03:19:21 +0000215CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
216 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000217 SubExprs = new (C) Stmt*[1];
218}
219
Ted Kremenek668bf912009-02-09 20:51:47 +0000220void CallExpr::Destroy(ASTContext& C) {
221 DestroyChildren(C);
222 if (SubExprs) C.Deallocate(SubExprs);
223 this->~CallExpr();
224 C.Deallocate(this);
225}
226
Zhongxing Xua0042542009-07-17 07:29:51 +0000227FunctionDecl *CallExpr::getDirectCallee() {
228 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000229 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000230 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000231
232 return 0;
233}
234
Chris Lattnerd18b3292007-12-28 05:25:02 +0000235/// setNumArgs - This changes the number of arguments present in this call.
236/// Any orphaned expressions are deleted by this, and any new operands are set
237/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000238void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000239 // No change, just return.
240 if (NumArgs == getNumArgs()) return;
241
242 // If shrinking # arguments, just delete the extras and forgot them.
243 if (NumArgs < getNumArgs()) {
244 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000245 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000246 this->NumArgs = NumArgs;
247 return;
248 }
249
250 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000251 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000252 // Copy over args.
253 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
254 NewSubExprs[i] = SubExprs[i];
255 // Null out new args.
256 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
257 NewSubExprs[i] = 0;
258
Douglas Gregor88c9a462009-04-17 21:46:47 +0000259 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000260 SubExprs = NewSubExprs;
261 this->NumArgs = NumArgs;
262}
263
Chris Lattnercb888962008-10-06 05:00:53 +0000264/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
265/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000266unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000267 // All simple function calls (e.g. func()) are implicitly cast to pointer to
268 // function. As a result, we try and obtain the DeclRefExpr from the
269 // ImplicitCastExpr.
270 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
271 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000272 return 0;
273
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000274 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
275 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000276 return 0;
277
Anders Carlssonbcba2012008-01-31 02:13:57 +0000278 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
279 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000280 return 0;
281
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000282 if (!FDecl->getIdentifier())
283 return 0;
284
Douglas Gregor3c385e52009-02-14 18:57:46 +0000285 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000286}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000287
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000288QualType CallExpr::getCallReturnType() const {
289 QualType CalleeType = getCallee()->getType();
Ted Kremenek1a1a6e22009-07-16 19:58:26 +0000290 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000291 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek1a1a6e22009-07-16 19:58:26 +0000292 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000293 CalleeType = BPT->getPointeeType();
294
295 const FunctionType *FnType = CalleeType->getAsFunctionType();
296 return FnType->getResultType();
297}
Chris Lattnercb888962008-10-06 05:00:53 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
300/// corresponds to, e.g. "<<=".
301const char *BinaryOperator::getOpcodeStr(Opcode Op) {
302 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000303 case PtrMemD: return ".*";
304 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 case Mul: return "*";
306 case Div: return "/";
307 case Rem: return "%";
308 case Add: return "+";
309 case Sub: return "-";
310 case Shl: return "<<";
311 case Shr: return ">>";
312 case LT: return "<";
313 case GT: return ">";
314 case LE: return "<=";
315 case GE: return ">=";
316 case EQ: return "==";
317 case NE: return "!=";
318 case And: return "&";
319 case Xor: return "^";
320 case Or: return "|";
321 case LAnd: return "&&";
322 case LOr: return "||";
323 case Assign: return "=";
324 case MulAssign: return "*=";
325 case DivAssign: return "/=";
326 case RemAssign: return "%=";
327 case AddAssign: return "+=";
328 case SubAssign: return "-=";
329 case ShlAssign: return "<<=";
330 case ShrAssign: return ">>=";
331 case AndAssign: return "&=";
332 case XorAssign: return "^=";
333 case OrAssign: return "|=";
334 case Comma: return ",";
335 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000336
337 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000338}
339
Douglas Gregor063daf62009-03-13 18:40:31 +0000340BinaryOperator::Opcode
341BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
342 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000343 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000344 case OO_Plus: return Add;
345 case OO_Minus: return Sub;
346 case OO_Star: return Mul;
347 case OO_Slash: return Div;
348 case OO_Percent: return Rem;
349 case OO_Caret: return Xor;
350 case OO_Amp: return And;
351 case OO_Pipe: return Or;
352 case OO_Equal: return Assign;
353 case OO_Less: return LT;
354 case OO_Greater: return GT;
355 case OO_PlusEqual: return AddAssign;
356 case OO_MinusEqual: return SubAssign;
357 case OO_StarEqual: return MulAssign;
358 case OO_SlashEqual: return DivAssign;
359 case OO_PercentEqual: return RemAssign;
360 case OO_CaretEqual: return XorAssign;
361 case OO_AmpEqual: return AndAssign;
362 case OO_PipeEqual: return OrAssign;
363 case OO_LessLess: return Shl;
364 case OO_GreaterGreater: return Shr;
365 case OO_LessLessEqual: return ShlAssign;
366 case OO_GreaterGreaterEqual: return ShrAssign;
367 case OO_EqualEqual: return EQ;
368 case OO_ExclaimEqual: return NE;
369 case OO_LessEqual: return LE;
370 case OO_GreaterEqual: return GE;
371 case OO_AmpAmp: return LAnd;
372 case OO_PipePipe: return LOr;
373 case OO_Comma: return Comma;
374 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000375 }
376}
377
378OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
379 static const OverloadedOperatorKind OverOps[] = {
380 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
381 OO_Star, OO_Slash, OO_Percent,
382 OO_Plus, OO_Minus,
383 OO_LessLess, OO_GreaterGreater,
384 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
385 OO_EqualEqual, OO_ExclaimEqual,
386 OO_Amp,
387 OO_Caret,
388 OO_Pipe,
389 OO_AmpAmp,
390 OO_PipePipe,
391 OO_Equal, OO_StarEqual,
392 OO_SlashEqual, OO_PercentEqual,
393 OO_PlusEqual, OO_MinusEqual,
394 OO_LessLessEqual, OO_GreaterGreaterEqual,
395 OO_AmpEqual, OO_CaretEqual,
396 OO_PipeEqual,
397 OO_Comma
398 };
399 return OverOps[Opc];
400}
401
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000402InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000403 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000404 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000405 : Expr(InitListExprClass, QualType(),
406 hasAnyTypeDependentArguments(initExprs, numInits),
407 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000408 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000409 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000410
411 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000412}
Reid Spencer5f016e22007-07-11 17:01:13 +0000413
Douglas Gregorfa219202009-03-20 23:58:33 +0000414void InitListExpr::reserveInits(unsigned NumInits) {
415 if (NumInits > InitExprs.size())
416 InitExprs.reserve(NumInits);
417}
418
Douglas Gregor4c678342009-01-28 21:54:33 +0000419void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000420 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000421 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000422 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000423 InitExprs.resize(NumInits, 0);
424}
425
426Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
427 if (Init >= InitExprs.size()) {
428 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
429 InitExprs.back() = expr;
430 return 0;
431 }
432
433 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
434 InitExprs[Init] = expr;
435 return Result;
436}
437
Steve Naroffbfdcae62008-09-04 15:31:07 +0000438/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000439///
440const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek1a1a6e22009-07-16 19:58:26 +0000441 return getType()->getAs<BlockPointerType>()->
Steve Naroff4eb206b2008-09-03 18:15:37 +0000442 getPointeeType()->getAsFunctionType();
443}
444
Steve Naroff56ee6892008-10-08 17:01:13 +0000445SourceLocation BlockExpr::getCaretLocation() const {
446 return TheBlock->getCaretLocation();
447}
Douglas Gregor72971342009-04-18 00:02:19 +0000448const Stmt *BlockExpr::getBody() const {
449 return TheBlock->getBody();
450}
451Stmt *BlockExpr::getBody() {
452 return TheBlock->getBody();
453}
Steve Naroff56ee6892008-10-08 17:01:13 +0000454
455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456//===----------------------------------------------------------------------===//
457// Generic Expression Routines
458//===----------------------------------------------------------------------===//
459
Chris Lattner026dc962009-02-14 07:37:35 +0000460/// isUnusedResultAWarning - Return true if this immediate expression should
461/// be warned about if the result is unused. If so, fill in Loc and Ranges
462/// with location to warn on and the source range[s] to report with the
463/// warning.
464bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000465 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000466 // Don't warn if the expr is type dependent. The type could end up
467 // instantiating to void.
468 if (isTypeDependent())
469 return false;
470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 switch (getStmtClass()) {
472 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000473 Loc = getExprLoc();
474 R1 = getSourceRange();
475 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000477 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000478 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 case UnaryOperatorClass: {
480 const UnaryOperator *UO = cast<UnaryOperator>(this);
481
482 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000483 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 case UnaryOperator::PostInc:
485 case UnaryOperator::PostDec:
486 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000487 case UnaryOperator::PreDec: // ++/--
488 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 case UnaryOperator::Deref:
490 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000491 if (getType().isVolatileQualified())
492 return false;
493 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 case UnaryOperator::Real:
495 case UnaryOperator::Imag:
496 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000497 if (UO->getSubExpr()->getType().isVolatileQualified())
498 return false;
499 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 case UnaryOperator::Extension:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000501 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 }
Chris Lattner026dc962009-02-14 07:37:35 +0000503 Loc = UO->getOperatorLoc();
504 R1 = UO->getSubExpr()->getSourceRange();
505 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000507 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000508 const BinaryOperator *BO = cast<BinaryOperator>(this);
509 // Consider comma to have side effects if the LHS or RHS does.
510 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000511 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
512 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000513
Chris Lattner026dc962009-02-14 07:37:35 +0000514 if (BO->isAssignmentOp())
515 return false;
516 Loc = BO->getOperatorLoc();
517 R1 = BO->getLHS()->getSourceRange();
518 R2 = BO->getRHS()->getSourceRange();
519 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000520 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000521 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000522 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000524 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000525 // The condition must be evaluated, but if either the LHS or RHS is a
526 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000527 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor68584ed2009-06-18 16:11:24 +0000528 if (Exp->getLHS() &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000529 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000530 return true;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000531 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000532 }
533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000535 // If the base pointer or element is to a volatile pointer/field, accessing
536 // it is a side effect.
537 if (getType().isVolatileQualified())
538 return false;
539 Loc = cast<MemberExpr>(this)->getMemberLoc();
540 R1 = SourceRange(Loc, Loc);
541 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
542 return true;
543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 case ArraySubscriptExprClass:
545 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000546 // it is a side effect.
547 if (getType().isVolatileQualified())
548 return false;
549 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
550 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
551 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
552 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000555 case CXXOperatorCallExprClass:
556 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000557 // If this is a direct call, get the callee.
558 const CallExpr *CE = cast<CallExpr>(this);
559 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
560 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
561 // If the callee has attribute pure, const, or warn_unused_result, warn
562 // about it. void foo() { strlen("bar"); } should warn.
563 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000564 if (FD->getAttr<WarnUnusedResultAttr>() ||
565 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000566 Loc = CE->getCallee()->getLocStart();
567 R1 = CE->getCallee()->getSourceRange();
568
569 if (unsigned NumArgs = CE->getNumArgs())
570 R2 = SourceRange(CE->getArg(0)->getLocStart(),
571 CE->getArg(NumArgs-1)->getLocEnd());
572 return true;
573 }
574 }
575 return false;
576 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000577 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000578 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000579 case StmtExprClass: {
580 // Statement exprs don't logically have side effects themselves, but are
581 // sometimes used in macros in ways that give them a type that is unused.
582 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
583 // however, if the result of the stmt expr is dead, we don't want to emit a
584 // warning.
585 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
586 if (!CS->body_empty())
587 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000588 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000589
590 Loc = cast<StmtExpr>(this)->getLParenLoc();
591 R1 = getSourceRange();
592 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000593 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000594 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000595 // If this is a cast to void, check the operand. Otherwise, the result of
596 // the cast is unused.
597 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000598 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000599 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000600 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
601 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
602 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000603 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // If this is a cast to void, check the operand. Otherwise, the result of
605 // the cast is unused.
606 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000607 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000608 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000609 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
610 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
611 return true;
612
Eli Friedman4be1f472008-05-19 21:24:43 +0000613 case ImplicitCastExprClass:
614 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000615 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000616 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000617
Chris Lattner04421082008-04-08 04:40:51 +0000618 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000619 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000620 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000621
622 case CXXNewExprClass:
623 // FIXME: In theory, there might be new expressions that don't have side
624 // effects (e.g. a placement new with an uninitialized POD).
625 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000626 return false;
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000627 case CXXExprWithTemporariesClass:
628 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000629 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000630 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000631}
632
Douglas Gregorba7e2102008-10-22 15:04:37 +0000633/// DeclCanBeLvalue - Determine whether the given declaration can be
634/// an lvalue. This is a helper routine for isLvalue.
635static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000636 // C++ [temp.param]p6:
637 // A non-type non-reference template-parameter is not an lvalue.
638 if (const NonTypeTemplateParmDecl *NTTParm
639 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
640 return NTTParm->getType()->isReferenceType();
641
Douglas Gregor44b43212008-12-11 16:49:14 +0000642 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000643 // C++ 3.10p2: An lvalue refers to an object or function.
644 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000645 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
646 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000647}
648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
650/// incomplete type other than void. Nonarray expressions that can be lvalues:
651/// - name, where name must be a variable
652/// - e[i]
653/// - (e), where e must be an lvalue
654/// - e.name, where e must be an lvalue
655/// - e->name
656/// - *e, the type of e cannot be a function type
657/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000658/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000659/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000660///
Chris Lattner28be73f2008-07-26 21:30:36 +0000661Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000662 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
663
664 isLvalueResult Res = isLvalueInternal(Ctx);
665 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
666 return Res;
667
Douglas Gregor98cd5992008-10-21 23:43:52 +0000668 // first, check the type (C99 6.3.2.1). Expressions with function
669 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000670 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 return LV_NotObjectType;
672
Steve Naroffacb818a2008-02-10 01:39:04 +0000673 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000674 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000675 return LV_IncompleteVoidType;
676
Eli Friedman53202852009-05-03 22:36:05 +0000677 return LV_Valid;
678}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000679
Eli Friedman53202852009-05-03 22:36:05 +0000680// Check whether the expression can be sanely treated like an l-value
681Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000683 case StringLiteralClass: // C99 6.5.1p4
684 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000685 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
687 // For vectors, make sure base is an lvalue (i.e. not a function call).
688 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000689 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000691 case DeclRefExprClass:
692 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000693 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
694 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 return LV_Valid;
696 break;
Chris Lattner41110242008-06-17 18:05:57 +0000697 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000698 case BlockDeclRefExprClass: {
699 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000700 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000701 return LV_Valid;
702 break;
703 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000704 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000706 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
707 NamedDecl *Member = m->getMemberDecl();
708 // C++ [expr.ref]p4:
709 // If E2 is declared to have type "reference to T", then E1.E2
710 // is an lvalue.
711 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
712 if (Value->getType()->isReferenceType())
713 return LV_Valid;
714
715 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000716 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000717 return LV_Valid;
718
719 // -- If E2 is a non-static data member [...]. If E1 is an
720 // lvalue, then E1.E2 is an lvalue.
721 if (isa<FieldDecl>(Member))
722 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
723
724 // -- If it refers to a static member function [...], then
725 // E1.E2 is an lvalue.
726 // -- Otherwise, if E1.E2 refers to a non-static member
727 // function [...], then E1.E2 is not an lvalue.
728 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
729 return Method->isStatic()? LV_Valid : LV_MemberFunction;
730
731 // -- If E2 is a member enumerator [...], the expression E1.E2
732 // is not an lvalue.
733 if (isa<EnumConstantDecl>(Member))
734 return LV_InvalidExpression;
735
736 // Not an lvalue.
737 return LV_InvalidExpression;
738 }
739
740 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000741 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000742 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000743 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000745 return LV_Valid; // C99 6.5.3p4
746
747 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000748 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
749 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000750 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000751
752 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
753 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
754 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
755 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000757 case ImplicitCastExprClass:
758 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
759 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000761 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000762 case BinaryOperatorClass:
763 case CompoundAssignOperatorClass: {
764 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000765
766 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
767 BinOp->getOpcode() == BinaryOperator::Comma)
768 return BinOp->getRHS()->isLvalue(Ctx);
769
Sebastian Redl22460502009-02-07 00:15:38 +0000770 // C++ [expr.mptr.oper]p6
771 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
772 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
773 !BinOp->getType()->isFunctionType())
774 return BinOp->getLHS()->isLvalue(Ctx);
775
Douglas Gregorbf3af052008-11-13 20:12:29 +0000776 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000777 return LV_InvalidExpression;
778
Douglas Gregorbf3af052008-11-13 20:12:29 +0000779 if (Ctx.getLangOptions().CPlusPlus)
780 // C++ [expr.ass]p1:
781 // The result of an assignment operation [...] is an lvalue.
782 return LV_Valid;
783
784
785 // C99 6.5.16:
786 // An assignment expression [...] is not an lvalue.
787 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000788 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000789 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000790 case CXXOperatorCallExprClass:
791 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000792 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000793 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000794 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000795 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
796 if (ReturnType->isLValueReferenceType())
797 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000798
Douglas Gregor9d293df2008-10-28 00:22:11 +0000799 break;
800 }
Steve Naroffe6386392007-12-05 04:00:10 +0000801 case CompoundLiteralExprClass: // C99 6.5.2.5p5
802 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000803 case ChooseExprClass:
804 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000805 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000806 case ExtVectorElementExprClass:
807 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000808 return LV_DuplicateVectorComponents;
809 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000810 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
811 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000812 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
813 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000814 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000815 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000816 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000817 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000818 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000819 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000820 case CXXConditionDeclExprClass:
821 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000822 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000823 case CXXFunctionalCastExprClass:
824 case CXXStaticCastExprClass:
825 case CXXDynamicCastExprClass:
826 case CXXReinterpretCastExprClass:
827 case CXXConstCastExprClass:
828 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000829 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000830 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
831 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000832 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
833 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000834 return LV_Valid;
835 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000836 case CXXTypeidExprClass:
837 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
838 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +0000839 case ConditionalOperatorClass: {
840 // Complicated handling is only for C++.
841 if (!Ctx.getLangOptions().CPlusPlus)
842 return LV_InvalidExpression;
843
844 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
845 // everywhere there's an object converted to an rvalue. Also, any other
846 // casts should be wrapped by ImplicitCastExprs. There's just the special
847 // case involving throws to work out.
848 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000849 Expr *True = Cond->getTrueExpr();
850 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +0000851 // C++0x 5.16p2
852 // If either the second or the third operand has type (cv) void, [...]
853 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000854 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +0000855 return LV_InvalidExpression;
856
857 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000858 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +0000859 return LV_InvalidExpression;
860
861 // That's it.
862 return LV_Valid;
863 }
864
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 default:
866 break;
867 }
868 return LV_InvalidExpression;
869}
870
871/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
872/// does not have an incomplete type, does not have a const-qualified type, and
873/// if it is a structure or union, does not have any member (including,
874/// recursively, any member or element of all contained aggregates or unions)
875/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000876Expr::isModifiableLvalueResult
877Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000878 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000879
880 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000881 case LV_Valid:
882 // C++ 3.10p11: Functions cannot be modified, but pointers to
883 // functions can be modifiable.
884 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
885 return MLV_NotObjectType;
886 break;
887
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 case LV_NotObjectType: return MLV_NotObjectType;
889 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000890 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000891 case LV_InvalidExpression:
892 // If the top level is a C-style cast, and the subexpression is a valid
893 // lvalue, then this is probably a use of the old-school "cast as lvalue"
894 // GCC extension. We don't support it, but we want to produce good
895 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000896 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
897 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
898 if (Loc)
899 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000900 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000901 }
902 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000903 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000904 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000906
907 // The following is illegal:
908 // void takeclosure(void (^C)(void));
909 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
910 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000911 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000912 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
913 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
914 return MLV_NotBlockQualified;
915 }
916
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000917 QualType CT = Ctx.getCanonicalType(getType());
918
919 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000921 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000923 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 return MLV_IncompleteType;
925
Ted Kremenek5cad1f72009-07-17 01:20:38 +0000926 if (const RecordType *r = CT->getAs<RecordType>()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 if (r->hasConstFields())
928 return MLV_ConstQualified;
929 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000930
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000931 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000932 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000933 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
934 if (KVCExpr->getSetterMethod() == 0)
935 return MLV_NoSetterProperty;
936 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 return MLV_Valid;
938}
939
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000940/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000941/// duration. This means that the address of this expression is a link-time
942/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000943bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000944 switch (getStmtClass()) {
945 default:
946 return false;
Steve Naroff3aaa4822009-04-16 19:02:57 +0000947 case BlockExprClass:
948 return true;
Chris Lattner4cc62712007-11-27 21:35:27 +0000949 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000950 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000951 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000952 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000953 case CompoundLiteralExprClass:
954 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000955 case DeclRefExprClass:
956 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000957 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
958 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000959 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000960 if (isa<FunctionDecl>(D))
961 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000962 return false;
963 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000964 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000965 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000966 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000967 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000968 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000969 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000970 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000971 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000972 case CXXDefaultArgExprClass:
973 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000974 }
975}
976
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000977/// isOBJCGCCandidate - Check if an expression is objc gc'able.
978///
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000979bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000980 switch (getStmtClass()) {
981 default:
982 return false;
983 case ObjCIvarRefExprClass:
984 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000985 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000986 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000987 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000988 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000989 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000990 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +0000991 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000992 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000993 case DeclRefExprClass:
994 case QualifiedDeclRefExprClass: {
995 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +0000996 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
997 if (VD->hasGlobalStorage())
998 return true;
999 QualType T = VD->getType();
1000 // dereferencing to an object pointer is always a gc'able candidate
1001 if (T->isPointerType() &&
Ted Kremenek1a1a6e22009-07-16 19:58:26 +00001002 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001003 return true;
1004
1005 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001006 return false;
1007 }
1008 case MemberExprClass: {
1009 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001010 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001011 }
1012 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001013 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001014 }
1015}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001016Expr* Expr::IgnoreParens() {
1017 Expr* E = this;
1018 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1019 E = P->getSubExpr();
1020
1021 return E;
1022}
1023
Chris Lattner56f34942008-02-13 01:02:39 +00001024/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1025/// or CastExprs or ImplicitCastExprs, returning their operand.
1026Expr *Expr::IgnoreParenCasts() {
1027 Expr *E = this;
1028 while (true) {
1029 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1030 E = P->getSubExpr();
1031 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1032 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001033 else
1034 return E;
1035 }
1036}
1037
Chris Lattnerecdd8412009-03-13 17:28:01 +00001038/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1039/// value (including ptr->int casts of the same size). Strip off any
1040/// ParenExpr or CastExprs, returning their operand.
1041Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1042 Expr *E = this;
1043 while (true) {
1044 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1045 E = P->getSubExpr();
1046 continue;
1047 }
1048
1049 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1050 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1051 // ptr<->int casts of the same width. We also ignore all identify casts.
1052 Expr *SE = P->getSubExpr();
1053
1054 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1055 E = SE;
1056 continue;
1057 }
1058
1059 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1060 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1061 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1062 E = SE;
1063 continue;
1064 }
1065 }
1066
1067 return E;
1068 }
1069}
1070
1071
Douglas Gregor898574e2008-12-05 23:32:09 +00001072/// hasAnyTypeDependentArguments - Determines if any of the expressions
1073/// in Exprs is type-dependent.
1074bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1075 for (unsigned I = 0; I < NumExprs; ++I)
1076 if (Exprs[I]->isTypeDependent())
1077 return true;
1078
1079 return false;
1080}
1081
1082/// hasAnyValueDependentArguments - Determines if any of the expressions
1083/// in Exprs is value-dependent.
1084bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1085 for (unsigned I = 0; I < NumExprs; ++I)
1086 if (Exprs[I]->isValueDependent())
1087 return true;
1088
1089 return false;
1090}
1091
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001092bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001093 // This function is attempting whether an expression is an initializer
1094 // which can be evaluated at compile-time. isEvaluatable handles most
1095 // of the cases, but it can't deal with some initializer-specific
1096 // expressions, and it can't deal with aggregates; we deal with those here,
1097 // and fall back to isEvaluatable for the other cases.
1098
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001099 // FIXME: This function assumes the variable being assigned to
1100 // isn't a reference type!
1101
Anders Carlssone8a32b82008-11-24 05:23:59 +00001102 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001103 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001104 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001105 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001106 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001107 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001108 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001109 // This handles gcc's extension that allows global initializers like
1110 // "struct x {int x;} x = (struct x) {};".
1111 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001112 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001113 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001114 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001115 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001116 // FIXME: This doesn't deal with fields with reference types correctly.
1117 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1118 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001119 const InitListExpr *Exp = cast<InitListExpr>(this);
1120 unsigned numInits = Exp->getNumInits();
1121 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001122 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001123 return false;
1124 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001125 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001126 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001127 case ImplicitValueInitExprClass:
1128 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001129 case ParenExprClass: {
1130 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1131 }
1132 case UnaryOperatorClass: {
1133 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1134 if (Exp->getOpcode() == UnaryOperator::Extension)
1135 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1136 break;
1137 }
Chris Lattner81045d82009-04-21 05:19:11 +00001138 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001139 case CStyleCastExprClass:
1140 // Handle casts with a destination that's a struct or union; this
1141 // deals with both the gcc no-op struct cast extension and the
1142 // cast-to-union extension.
1143 if (getType()->isRecordType())
1144 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1145 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001146 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001147 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001148}
1149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001151/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001152
1153/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1154/// comma, etc
1155///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001156/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1157/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1158/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001159
Eli Friedmane28d7192009-02-26 09:29:13 +00001160// CheckICE - This function does the fundamental ICE checking: the returned
1161// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1162// Note that to reduce code duplication, this helper does no evaluation
1163// itself; the caller checks whether the expression is evaluatable, and
1164// in the rare cases where CheckICE actually cares about the evaluated
1165// value, it calls into Evalute.
1166//
1167// Meanings of Val:
1168// 0: This expression is an ICE if it can be evaluated by Evaluate.
1169// 1: This expression is not an ICE, but if it isn't evaluated, it's
1170// a legal subexpression for an ICE. This return value is used to handle
1171// the comma operator in C99 mode.
1172// 2: This expression is not an ICE, and is not a legal subexpression for one.
1173
1174struct ICEDiag {
1175 unsigned Val;
1176 SourceLocation Loc;
1177
1178 public:
1179 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1180 ICEDiag() : Val(0) {}
1181};
1182
1183ICEDiag NoDiag() { return ICEDiag(); }
1184
Eli Friedman60ce9632009-02-27 04:07:58 +00001185static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1186 Expr::EvalResult EVResult;
1187 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1188 !EVResult.Val.isInt()) {
1189 return ICEDiag(2, E->getLocStart());
1190 }
1191 return NoDiag();
1192}
1193
Eli Friedmane28d7192009-02-26 09:29:13 +00001194static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001195 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001196 if (!E->getType()->isIntegralType()) {
1197 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001198 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001199
1200 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001202 return ICEDiag(2, E->getLocStart());
1203 case Expr::ParenExprClass:
1204 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1205 case Expr::IntegerLiteralClass:
1206 case Expr::CharacterLiteralClass:
1207 case Expr::CXXBoolLiteralExprClass:
1208 case Expr::CXXZeroInitValueExprClass:
1209 case Expr::TypesCompatibleExprClass:
1210 case Expr::UnaryTypeTraitExprClass:
1211 return NoDiag();
1212 case Expr::CallExprClass:
1213 case Expr::CXXOperatorCallExprClass: {
1214 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001215 if (CE->isBuiltinCall(Ctx))
1216 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001217 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001218 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001219 case Expr::DeclRefExprClass:
1220 case Expr::QualifiedDeclRefExprClass:
1221 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1222 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001223 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001224 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001225 // C++ 7.1.5.1p2
1226 // A variable of non-volatile const-qualified integral or enumeration
1227 // type initialized by an ICE can be used in ICEs.
1228 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001229 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001230 if (Dcl->isInitKnownICE()) {
1231 // We have already checked whether this subexpression is an
1232 // integral constant expression.
1233 if (Dcl->isInitICE())
1234 return NoDiag();
1235 else
1236 return ICEDiag(2, E->getLocStart());
1237 }
1238
1239 if (const Expr *Init = Dcl->getInit()) {
1240 ICEDiag Result = CheckICE(Init, Ctx);
1241 // Cache the result of the ICE test.
1242 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1243 return Result;
1244 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001245 }
1246 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001247 return ICEDiag(2, E->getLocStart());
1248 case Expr::UnaryOperatorClass: {
1249 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001252 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001254 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001258 case UnaryOperator::Real:
1259 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001260 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001261 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001262 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1263 // Evaluate matches the proposed gcc behavior for cases like
1264 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1265 // compliance: we should warn earlier for offsetof expressions with
1266 // array subscripts that aren't ICEs, and if the array subscripts
1267 // are ICEs, the value of the offsetof must be an integer constant.
1268 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001271 case Expr::SizeOfAlignOfExprClass: {
1272 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1273 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1274 return ICEDiag(2, E->getLocStart());
1275 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001277 case Expr::BinaryOperatorClass: {
1278 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 switch (Exp->getOpcode()) {
1280 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001281 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001285 case BinaryOperator::Add:
1286 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001289 case BinaryOperator::LT:
1290 case BinaryOperator::GT:
1291 case BinaryOperator::LE:
1292 case BinaryOperator::GE:
1293 case BinaryOperator::EQ:
1294 case BinaryOperator::NE:
1295 case BinaryOperator::And:
1296 case BinaryOperator::Xor:
1297 case BinaryOperator::Or:
1298 case BinaryOperator::Comma: {
1299 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1300 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001301 if (Exp->getOpcode() == BinaryOperator::Div ||
1302 Exp->getOpcode() == BinaryOperator::Rem) {
1303 // Evaluate gives an error for undefined Div/Rem, so make sure
1304 // we don't evaluate one.
1305 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1306 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1307 if (REval == 0)
1308 return ICEDiag(1, E->getLocStart());
1309 if (REval.isSigned() && REval.isAllOnesValue()) {
1310 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1311 if (LEval.isMinSignedValue())
1312 return ICEDiag(1, E->getLocStart());
1313 }
1314 }
1315 }
1316 if (Exp->getOpcode() == BinaryOperator::Comma) {
1317 if (Ctx.getLangOptions().C99) {
1318 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1319 // if it isn't evaluated.
1320 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1321 return ICEDiag(1, E->getLocStart());
1322 } else {
1323 // In both C89 and C++, commas in ICEs are illegal.
1324 return ICEDiag(2, E->getLocStart());
1325 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001326 }
1327 if (LHSResult.Val >= RHSResult.Val)
1328 return LHSResult;
1329 return RHSResult;
1330 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001332 case BinaryOperator::LOr: {
1333 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1334 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1335 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1336 // Rare case where the RHS has a comma "side-effect"; we need
1337 // to actually check the condition to see whether the side
1338 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001339 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001340 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001341 return RHSResult;
1342 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001343 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001344
Eli Friedmane28d7192009-02-26 09:29:13 +00001345 if (LHSResult.Val >= RHSResult.Val)
1346 return LHSResult;
1347 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001349 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001351 case Expr::ImplicitCastExprClass:
1352 case Expr::CStyleCastExprClass:
1353 case Expr::CXXFunctionalCastExprClass: {
1354 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1355 if (SubExpr->getType()->isIntegralType())
1356 return CheckICE(SubExpr, Ctx);
1357 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1358 return NoDiag();
1359 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001361 case Expr::ConditionalOperatorClass: {
1362 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001363 // If the condition (ignoring parens) is a __builtin_constant_p call,
1364 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001365 // expression, and it is fully evaluated. This is an important GNU
1366 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001367 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001368 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001369 Expr::EvalResult EVResult;
1370 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1371 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001372 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001373 }
1374 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001375 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001376 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1377 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1378 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1379 if (CondResult.Val == 2)
1380 return CondResult;
1381 if (TrueResult.Val == 2)
1382 return TrueResult;
1383 if (FalseResult.Val == 2)
1384 return FalseResult;
1385 if (CondResult.Val == 1)
1386 return CondResult;
1387 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1388 return NoDiag();
1389 // Rare case where the diagnostics depend on which side is evaluated
1390 // Note that if we get here, CondResult is 0, and at least one of
1391 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001392 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001393 return FalseResult;
1394 }
1395 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001397 case Expr::CXXDefaultArgExprClass:
1398 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001399 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001400 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001401 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001403}
Reid Spencer5f016e22007-07-11 17:01:13 +00001404
Eli Friedmane28d7192009-02-26 09:29:13 +00001405bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1406 SourceLocation *Loc, bool isEvaluated) const {
1407 ICEDiag d = CheckICE(this, Ctx);
1408 if (d.Val != 0) {
1409 if (Loc) *Loc = d.Loc;
1410 return false;
1411 }
1412 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001413 if (!Evaluate(EvalResult, Ctx))
1414 assert(0 && "ICE cannot be evaluated!");
1415 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1416 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001417 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 return true;
1419}
1420
Reid Spencer5f016e22007-07-11 17:01:13 +00001421/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1422/// integer constant expression with the value zero, or if this is one that is
1423/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001424bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1425{
Sebastian Redl07779722008-10-31 14:43:28 +00001426 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001427 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001428 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001429 // Check that it is a cast to void*.
Ted Kremenek1a1a6e22009-07-16 19:58:26 +00001430 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001431 QualType Pointee = PT->getPointeeType();
1432 if (Pointee.getCVRQualifiers() == 0 &&
1433 Pointee->isVoidType() && // to void*
1434 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001435 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001436 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001437 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001438 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1439 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001440 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001441 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1442 // Accept ((void*)0) as a null pointer constant, as many other
1443 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001444 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001445 } else if (const CXXDefaultArgExpr *DefaultArg
1446 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001447 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001448 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001449 } else if (isa<GNUNullExpr>(this)) {
1450 // The GNU __null extension is always a null pointer constant.
1451 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001452 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001453
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001454 // C++0x nullptr_t is always a null pointer constant.
1455 if (getType()->isNullPtrType())
1456 return true;
1457
Steve Naroffaa58f002008-01-14 16:10:57 +00001458 // This expression must be an integer type.
1459 if (!getType()->isIntegerType())
1460 return false;
1461
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 // If we have an integer constant expression, we need to *evaluate* it and
1463 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001464 llvm::APSInt Result;
1465 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001466}
Steve Naroff31a45842007-07-28 23:10:27 +00001467
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001468FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001469 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001470
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001471 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001472 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001473 if (Field->isBitField())
1474 return Field;
1475
1476 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1477 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1478 return BinOp->getLHS()->getBitField();
1479
1480 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001481}
1482
Chris Lattner2140e902009-02-16 22:14:05 +00001483/// isArrow - Return true if the base expression is a pointer to vector,
1484/// return false if the base expression is a vector.
1485bool ExtVectorElementExpr::isArrow() const {
1486 return getBase()->getType()->isPointerType();
1487}
1488
Nate Begeman213541a2008-04-18 23:10:10 +00001489unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001490 if (const VectorType *VT = getType()->getAsVectorType())
1491 return VT->getNumElements();
1492 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001493}
1494
Nate Begeman8a997642008-05-09 06:41:27 +00001495/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001496bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001497 const char *compStr = Accessor->getName();
1498 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001499
1500 // Halving swizzles do not contain duplicate elements.
1501 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1502 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1503 return false;
1504
1505 // Advance past s-char prefix on hex swizzles.
Nate Begeman131f4652009-06-25 21:06:09 +00001506 if (*compStr == 's' || *compStr == 'S') {
Nate Begeman190d6a22009-01-18 02:01:21 +00001507 compStr++;
1508 length--;
1509 }
Steve Narofffec0b492007-07-30 03:29:09 +00001510
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001511 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001512 const char *s = compStr+i;
1513 for (const char c = *s++; *s; s++)
1514 if (c == *s)
1515 return true;
1516 }
1517 return false;
1518}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001519
Nate Begeman8a997642008-05-09 06:41:27 +00001520/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001521void ExtVectorElementExpr::getEncodedElementAccess(
1522 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001523 const char *compStr = Accessor->getName();
Nate Begeman131f4652009-06-25 21:06:09 +00001524 if (*compStr == 's' || *compStr == 'S')
Nate Begeman353417a2009-01-18 01:47:54 +00001525 compStr++;
1526
1527 bool isHi = !strcmp(compStr, "hi");
1528 bool isLo = !strcmp(compStr, "lo");
1529 bool isEven = !strcmp(compStr, "even");
1530 bool isOdd = !strcmp(compStr, "odd");
1531
Nate Begeman8a997642008-05-09 06:41:27 +00001532 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1533 uint64_t Index;
1534
1535 if (isHi)
1536 Index = e + i;
1537 else if (isLo)
1538 Index = i;
1539 else if (isEven)
1540 Index = 2 * i;
1541 else if (isOdd)
1542 Index = 2 * i + 1;
1543 else
1544 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001545
Nate Begeman3b8d1162008-05-13 21:03:02 +00001546 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001547 }
Nate Begeman8a997642008-05-09 06:41:27 +00001548}
1549
Steve Naroff68d331a2007-09-27 14:38:14 +00001550// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001551ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, 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];
Steve Naroff68d331a2007-09-27 14:38:14 +00001559 SubExprs[RECEIVER] = receiver;
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
Anders Carlsson02d95ba2009-06-07 19:51:47 +00001568ObjCStringLiteral* ObjCStringLiteral::Clone(ASTContext &C) const {
1569 // Clone the string literal.
1570 StringLiteral *NewString =
1571 String ? cast<StringLiteral>(String)->Clone(C) : 0;
1572
1573 return new (C) ObjCStringLiteral(NewString, getType(), AtLoc);
1574}
1575
1576ObjCSelectorExpr *ObjCSelectorExpr::Clone(ASTContext &C) const {
1577 return new (C) ObjCSelectorExpr(getType(), SelName, AtLoc, RParenLoc);
1578}
1579
1580ObjCProtocolExpr *ObjCProtocolExpr::Clone(ASTContext &C) const {
Fariborz Jahanian262f9cf2009-06-21 18:26:03 +00001581 return new (C) ObjCProtocolExpr(getType(), TheProtocol, AtLoc, RParenLoc);
Anders Carlsson02d95ba2009-06-07 19:51:47 +00001582}
1583
Steve Naroff68d331a2007-09-27 14:38:14 +00001584// constructor for class messages.
1585// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001586ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001587 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001588 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001589 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001590 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001591 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001592 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001593 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001594 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001595 if (NumArgs) {
1596 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001597 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1598 }
Steve Naroff563477d2007-09-18 23:55:05 +00001599 LBracloc = LBrac;
1600 RBracloc = RBrac;
1601}
1602
Ted Kremenek4df728e2008-06-24 15:50:53 +00001603// constructor for class messages.
1604ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1605 QualType retType, ObjCMethodDecl *mproto,
1606 SourceLocation LBrac, SourceLocation RBrac,
1607 Expr **ArgExprs, unsigned nargs)
1608: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1609MethodProto(mproto) {
1610 NumArgs = nargs;
1611 SubExprs = new Stmt*[NumArgs+1];
1612 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1613 if (NumArgs) {
1614 for (unsigned i = 0; i != NumArgs; ++i)
1615 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1616 }
1617 LBracloc = LBrac;
1618 RBracloc = RBrac;
1619}
1620
1621ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1622 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1623 switch (x & Flags) {
1624 default:
1625 assert(false && "Invalid ObjCMessageExpr.");
1626 case IsInstMeth:
1627 return ClassInfo(0, 0);
1628 case IsClsMethDeclUnknown:
1629 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1630 case IsClsMethDeclKnown: {
1631 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1632 return ClassInfo(D, D->getIdentifier());
1633 }
1634 }
1635}
1636
Chris Lattner0389e6b2009-04-26 00:44:05 +00001637void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1638 if (CI.first == 0 && CI.second == 0)
1639 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1640 else if (CI.first == 0)
1641 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1642 else
1643 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1644}
1645
1646
Chris Lattner27437ca2007-10-25 00:29:32 +00001647bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001648 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001649}
1650
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001651void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1652 if (NumExprs)
1653 delete [] SubExprs;
1654
1655 SubExprs = new Stmt* [NumExprs];
1656 this->NumExprs = NumExprs;
1657 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1658}
1659
Sebastian Redl05189992008-11-11 17:56:53 +00001660void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1661 // Override default behavior of traversing children. If this has a type
1662 // operand and the type is a variable-length array, the child iteration
1663 // will iterate over the size expression. However, this expression belongs
1664 // to the type, not to this, so we don't want to delete it.
1665 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001666 if (isArgumentType()) {
1667 this->~SizeOfAlignOfExpr();
1668 C.Deallocate(this);
1669 }
Sebastian Redl05189992008-11-11 17:56:53 +00001670 else
1671 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001672}
1673
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001674//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001675// DesignatedInitExpr
1676//===----------------------------------------------------------------------===//
1677
1678IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1679 assert(Kind == FieldDesignator && "Only valid on a field designator");
1680 if (Field.NameOrField & 0x01)
1681 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1682 else
1683 return getField()->getIdentifier();
1684}
1685
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001686DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1687 const Designator *Designators,
1688 SourceLocation EqualOrColonLoc,
1689 bool GNUSyntax,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001690 Expr **IndexExprs,
1691 unsigned NumIndexExprs,
1692 Expr *Init)
1693 : Expr(DesignatedInitExprClass, Ty,
1694 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001695 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor9ea62762009-05-21 23:17:49 +00001696 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001697 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001698
1699 // Record the initializer itself.
1700 child_iterator Child = child_begin();
1701 *Child++ = Init;
1702
1703 // Copy the designators and their subexpressions, computing
1704 // value-dependence along the way.
1705 unsigned IndexIdx = 0;
1706 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001707 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001708
1709 if (this->Designators[I].isArrayDesignator()) {
1710 // Compute type- and value-dependence.
1711 Expr *Index = IndexExprs[IndexIdx];
1712 ValueDependent = ValueDependent ||
1713 Index->isTypeDependent() || Index->isValueDependent();
1714
1715 // Copy the index expressions into permanent storage.
1716 *Child++ = IndexExprs[IndexIdx++];
1717 } else if (this->Designators[I].isArrayRangeDesignator()) {
1718 // Compute type- and value-dependence.
1719 Expr *Start = IndexExprs[IndexIdx];
1720 Expr *End = IndexExprs[IndexIdx + 1];
1721 ValueDependent = ValueDependent ||
1722 Start->isTypeDependent() || Start->isValueDependent() ||
1723 End->isTypeDependent() || End->isValueDependent();
1724
1725 // Copy the start/end expressions into permanent storage.
1726 *Child++ = IndexExprs[IndexIdx++];
1727 *Child++ = IndexExprs[IndexIdx++];
1728 }
1729 }
1730
1731 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001732}
1733
Douglas Gregor05c13a32009-01-22 00:58:24 +00001734DesignatedInitExpr *
1735DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1736 unsigned NumDesignators,
1737 Expr **IndexExprs, unsigned NumIndexExprs,
1738 SourceLocation ColonOrEqualLoc,
1739 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001740 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001741 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001742 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1743 ColonOrEqualLoc, UsesColonSyntax,
1744 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001745}
1746
Douglas Gregord077d752009-04-16 00:55:48 +00001747DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1748 unsigned NumIndexExprs) {
1749 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1750 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1751 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1752}
1753
1754void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1755 unsigned NumDesigs) {
1756 if (Designators)
1757 delete [] Designators;
1758
1759 Designators = new Designator[NumDesigs];
1760 NumDesignators = NumDesigs;
1761 for (unsigned I = 0; I != NumDesigs; ++I)
1762 Designators[I] = Desigs[I];
1763}
1764
Douglas Gregor05c13a32009-01-22 00:58:24 +00001765SourceRange DesignatedInitExpr::getSourceRange() const {
1766 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001767 Designator &First =
1768 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001769 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001770 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001771 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1772 else
1773 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1774 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001775 StartLoc =
1776 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001777 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1778}
1779
Douglas Gregor05c13a32009-01-22 00:58:24 +00001780Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1781 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1782 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1783 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001784 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1785 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1786}
1787
1788Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1789 assert(D.Kind == Designator::ArrayRangeDesignator &&
1790 "Requires array range designator");
1791 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1792 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001793 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1794 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1795}
1796
1797Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1798 assert(D.Kind == Designator::ArrayRangeDesignator &&
1799 "Requires array range designator");
1800 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1801 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001802 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1803 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1804}
1805
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001806/// \brief Replaces the designator at index @p Idx with the series
1807/// of designators in [First, Last).
1808void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1809 const Designator *First,
1810 const Designator *Last) {
1811 unsigned NumNewDesignators = Last - First;
1812 if (NumNewDesignators == 0) {
1813 std::copy_backward(Designators + Idx + 1,
1814 Designators + NumDesignators,
1815 Designators + Idx);
1816 --NumNewDesignators;
1817 return;
1818 } else if (NumNewDesignators == 1) {
1819 Designators[Idx] = *First;
1820 return;
1821 }
1822
1823 Designator *NewDesignators
1824 = new Designator[NumDesignators - 1 + NumNewDesignators];
1825 std::copy(Designators, Designators + Idx, NewDesignators);
1826 std::copy(First, Last, NewDesignators + Idx);
1827 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1828 NewDesignators + Idx + NumNewDesignators);
1829 delete [] Designators;
1830 Designators = NewDesignators;
1831 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1832}
1833
1834void DesignatedInitExpr::Destroy(ASTContext &C) {
1835 delete [] Designators;
1836 Expr::Destroy(C);
1837}
1838
Douglas Gregor9ea62762009-05-21 23:17:49 +00001839ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1840 return new (C) ImplicitValueInitExpr(getType());
1841}
1842
Douglas Gregor05c13a32009-01-22 00:58:24 +00001843//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001844// ExprIterator.
1845//===----------------------------------------------------------------------===//
1846
1847Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1848Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1849Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1850const Expr* ConstExprIterator::operator[](size_t idx) const {
1851 return cast<Expr>(I[idx]);
1852}
1853const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1854const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1855
1856//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001857// Child Iterators for iterating over subexpressions/substatements
1858//===----------------------------------------------------------------------===//
1859
1860// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001861Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1862Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001863
Steve Naroff7779db42007-11-12 14:29:37 +00001864// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001865Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1866Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001867
Steve Naroffe3e9add2008-06-02 23:03:37 +00001868// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001869Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1870Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001871
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001872// ObjCKVCRefExpr
1873Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1874Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1875
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001876// ObjCSuperExpr
1877Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1878Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1879
Chris Lattnerd9f69102008-08-10 01:53:14 +00001880// PredefinedExpr
1881Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1882Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001883
1884// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001885Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1886Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001887
1888// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001889Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001890Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001891
1892// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001893Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1894Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001895
Chris Lattner5d661452007-08-26 03:42:43 +00001896// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001897Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1898Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001899
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001900// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001901Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1902Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001903
1904// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001905Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1906Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001907
1908// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001909Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1910Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001911
Sebastian Redl05189992008-11-11 17:56:53 +00001912// SizeOfAlignOfExpr
1913Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1914 // If this is of a type and the type is a VLA type (and not a typedef), the
1915 // size expression of the VLA needs to be treated as an executable expression.
1916 // Why isn't this weirdness documented better in StmtIterator?
1917 if (isArgumentType()) {
1918 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1919 getArgumentType().getTypePtr()))
1920 return child_iterator(T);
1921 return child_iterator();
1922 }
Sebastian Redld4575892008-12-03 23:17:54 +00001923 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001924}
Sebastian Redl05189992008-11-11 17:56:53 +00001925Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1926 if (isArgumentType())
1927 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001928 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001929}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001930
1931// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001932Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001933 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001934}
Ted Kremenek1237c672007-08-24 20:06:47 +00001935Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001936 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001937}
1938
1939// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001940Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001941 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001942}
Ted Kremenek1237c672007-08-24 20:06:47 +00001943Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001944 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001945}
Ted Kremenek1237c672007-08-24 20:06:47 +00001946
1947// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001948Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1949Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001950
Nate Begeman213541a2008-04-18 23:10:10 +00001951// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001952Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1953Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001954
1955// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001956Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1957Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001958
Ted Kremenek1237c672007-08-24 20:06:47 +00001959// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001960Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1961Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001962
1963// BinaryOperator
1964Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001965 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001966}
Ted Kremenek1237c672007-08-24 20:06:47 +00001967Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001968 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001969}
1970
1971// ConditionalOperator
1972Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001973 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001974}
Ted Kremenek1237c672007-08-24 20:06:47 +00001975Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001976 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001977}
1978
1979// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001980Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1981Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001982
Ted Kremenek1237c672007-08-24 20:06:47 +00001983// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001984Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1985Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001986
1987// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001988Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1989 return child_iterator();
1990}
1991
1992Stmt::child_iterator TypesCompatibleExpr::child_end() {
1993 return child_iterator();
1994}
Ted Kremenek1237c672007-08-24 20:06:47 +00001995
1996// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001997Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1998Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001999
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002000// GNUNullExpr
2001Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2002Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2003
Eli Friedmand38617c2008-05-14 19:38:39 +00002004// ShuffleVectorExpr
2005Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002006 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002007}
2008Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002009 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002010}
2011
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002012// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002013Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2014Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002015
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002016// InitListExpr
2017Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002018 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002019}
2020Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002021 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002022}
2023
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002024// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002025Stmt::child_iterator DesignatedInitExpr::child_begin() {
2026 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2027 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002028 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2029}
2030Stmt::child_iterator DesignatedInitExpr::child_end() {
2031 return child_iterator(&*child_begin() + NumSubExprs);
2032}
2033
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002034// ImplicitValueInitExpr
2035Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2036 return child_iterator();
2037}
2038
2039Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2040 return child_iterator();
2041}
2042
Ted Kremenek1237c672007-08-24 20:06:47 +00002043// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002044Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002045 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002046}
2047Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002048 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002049}
Ted Kremenek1237c672007-08-24 20:06:47 +00002050
2051// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002052Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2053Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002054
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002055// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002056Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2057 return child_iterator();
2058}
2059Stmt::child_iterator ObjCSelectorExpr::child_end() {
2060 return child_iterator();
2061}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002062
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002063// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002064Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2065 return child_iterator();
2066}
2067Stmt::child_iterator ObjCProtocolExpr::child_end() {
2068 return child_iterator();
2069}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002070
Steve Naroff563477d2007-09-18 23:55:05 +00002071// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00002072Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002073 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002074}
2075Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002076 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002077}
2078
Steve Naroff4eb206b2008-09-03 18:15:37 +00002079// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002080Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2081Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002082
Ted Kremenek9da13f92008-09-26 23:24:14 +00002083Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2084Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }