blob: efc37f768cfa65154abe61723a2eded0eb3af28e [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Anders Carlsson63f1ad92009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000023#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000024#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Primary Expressions.
29//===----------------------------------------------------------------------===//
30
Sebastian Redlf80e2612009-05-16 18:50:46 +000031PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
32 return new (C) PredefinedExpr(Loc, getType(), Type);
33}
34
Anders Carlsson7f2e7442009-03-15 18:34:13 +000035IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
36 return new (C) IntegerLiteral(Value, getType(), Loc);
37}
38
Sebastian Redlf80e2612009-05-16 18:50:46 +000039CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
40 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
41}
42
43FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
Chris Lattnerff1bf1a2009-06-29 17:34:55 +000044 return new (C) FloatingLiteral(Value, IsExact, getType(), Loc);
Sebastian Redlf80e2612009-05-16 18:50:46 +000045}
46
Douglas Gregor4a951342009-05-18 22:38:38 +000047ImaginaryLiteral* ImaginaryLiteral::Clone(ASTContext &C) const {
48 // FIXME: Use virtual Clone(), once it is available
49 Expr *ClonedVal = 0;
50 if (const IntegerLiteral *IntLit = dyn_cast<IntegerLiteral>(Val))
51 ClonedVal = IntLit->Clone(C);
52 else
53 ClonedVal = cast<FloatingLiteral>(Val)->Clone(C);
54 return new (C) ImaginaryLiteral(ClonedVal, getType());
55}
56
Sebastian Redlf80e2612009-05-16 18:50:46 +000057GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
58 return new (C) GNUNullExpr(getType(), TokenLoc);
59}
60
Chris Lattnere0391b22008-06-07 22:13:43 +000061/// getValueAsApproximateDouble - This returns the value as an inaccurate
62/// double. Note that this may cause loss of precision, but is useful for
63/// debugging dumps, etc.
64double FloatingLiteral::getValueAsApproximateDouble() const {
65 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000066 bool ignored;
67 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
68 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000069 return V.convertToDouble();
70}
71
Chris Lattneraa491192009-02-18 06:40:38 +000072StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
73 unsigned ByteLength, bool Wide,
74 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000075 const SourceLocation *Loc,
76 unsigned NumStrs) {
Chris Lattneraa491192009-02-18 06:40:38 +000077 // Allocate enough space for the StringLiteral plus an array of locations for
78 // any concatenated string tokens.
79 void *Mem = C.Allocate(sizeof(StringLiteral)+
80 sizeof(SourceLocation)*(NumStrs-1),
81 llvm::alignof<StringLiteral>());
82 StringLiteral *SL = new (Mem) StringLiteral(Ty);
83
Chris Lattner4b009652007-07-25 00:24:17 +000084 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000085 char *AStrData = new (C, 1) char[ByteLength];
86 memcpy(AStrData, StrData, ByteLength);
87 SL->StrData = AStrData;
88 SL->ByteLength = ByteLength;
89 SL->IsWide = Wide;
90 SL->TokLocs[0] = Loc[0];
91 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000092
Chris Lattnerc3144742009-02-18 05:49:11 +000093 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000094 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
95 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000096}
97
Douglas Gregor596e0932009-04-15 16:35:07 +000098StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
99 void *Mem = C.Allocate(sizeof(StringLiteral)+
100 sizeof(SourceLocation)*(NumStrs-1),
101 llvm::alignof<StringLiteral>());
102 StringLiteral *SL = new (Mem) StringLiteral(QualType());
103 SL->StrData = 0;
104 SL->ByteLength = 0;
105 SL->NumConcatenated = NumStrs;
106 return SL;
107}
108
Anders Carlsson7f2e7442009-03-15 18:34:13 +0000109StringLiteral* StringLiteral::Clone(ASTContext &C) const {
110 return Create(C, StrData, ByteLength, IsWide, getType(),
111 TokLocs, NumConcatenated);
112}
Chris Lattnerc3144742009-02-18 05:49:11 +0000113
Ted Kremenek4f530a92009-02-06 19:55:15 +0000114void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000115 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +0000116 this->~StringLiteral();
117 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +0000118}
119
Douglas Gregor596e0932009-04-15 16:35:07 +0000120void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
121 if (StrData)
122 C.Deallocate(const_cast<char*>(StrData));
123
124 char *AStrData = new (C, 1) char[Len];
125 memcpy(AStrData, Str, Len);
126 StrData = AStrData;
127 ByteLength = Len;
128}
129
Chris Lattner4b009652007-07-25 00:24:17 +0000130/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
131/// corresponds to, e.g. "sizeof" or "[pre]++".
132const char *UnaryOperator::getOpcodeStr(Opcode Op) {
133 switch (Op) {
134 default: assert(0 && "Unknown unary operator");
135 case PostInc: return "++";
136 case PostDec: return "--";
137 case PreInc: return "++";
138 case PreDec: return "--";
139 case AddrOf: return "&";
140 case Deref: return "*";
141 case Plus: return "+";
142 case Minus: return "-";
143 case Not: return "~";
144 case LNot: return "!";
145 case Real: return "__real";
146 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000147 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000148 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150}
151
Douglas Gregorc78182d2009-03-13 23:49:33 +0000152UnaryOperator::Opcode
153UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
154 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000155 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000156 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
157 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
158 case OO_Amp: return AddrOf;
159 case OO_Star: return Deref;
160 case OO_Plus: return Plus;
161 case OO_Minus: return Minus;
162 case OO_Tilde: return Not;
163 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000164 }
165}
166
167OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
168 switch (Opc) {
169 case PostInc: case PreInc: return OO_PlusPlus;
170 case PostDec: case PreDec: return OO_MinusMinus;
171 case AddrOf: return OO_Amp;
172 case Deref: return OO_Star;
173 case Plus: return OO_Plus;
174 case Minus: return OO_Minus;
175 case Not: return OO_Tilde;
176 case LNot: return OO_Exclaim;
177 default: return OO_None;
178 }
179}
180
181
Chris Lattner4b009652007-07-25 00:24:17 +0000182//===----------------------------------------------------------------------===//
183// Postfix Operators.
184//===----------------------------------------------------------------------===//
185
Ted Kremenek362abcd2009-02-09 20:51:47 +0000186CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000187 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000188 : Expr(SC, t,
189 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000190 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000191 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000192
193 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000194 SubExprs[FN] = fn;
195 for (unsigned i = 0; i != numargs; ++i)
196 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000197
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000198 RParenLoc = rparenloc;
199}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000200
Ted Kremenek362abcd2009-02-09 20:51:47 +0000201CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
202 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000203 : Expr(CallExprClass, t,
204 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000205 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000206 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000207
208 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000209 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000210 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000211 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000212
Chris Lattner4b009652007-07-25 00:24:17 +0000213 RParenLoc = rparenloc;
214}
215
Argiris Kirtzidisa06a9da2009-07-14 03:19:21 +0000216CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
217 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000218 SubExprs = new (C) Stmt*[1];
219}
220
Ted Kremenek362abcd2009-02-09 20:51:47 +0000221void CallExpr::Destroy(ASTContext& C) {
222 DestroyChildren(C);
223 if (SubExprs) C.Deallocate(SubExprs);
224 this->~CallExpr();
225 C.Deallocate(this);
226}
227
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000228FunctionDecl *CallExpr::getDirectCallee() {
229 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattnereb346742009-07-17 15:46:27 +0000230 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000231 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000232
233 return 0;
234}
235
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000236/// setNumArgs - This changes the number of arguments present in this call.
237/// Any orphaned expressions are deleted by this, and any new operands are set
238/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000239void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000240 // No change, just return.
241 if (NumArgs == getNumArgs()) return;
242
243 // If shrinking # arguments, just delete the extras and forgot them.
244 if (NumArgs < getNumArgs()) {
245 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000246 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000247 this->NumArgs = NumArgs;
248 return;
249 }
250
251 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000252 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000253 // Copy over args.
254 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
255 NewSubExprs[i] = SubExprs[i];
256 // Null out new args.
257 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
258 NewSubExprs[i] = 0;
259
Douglas Gregorb9f63642009-04-17 21:46:47 +0000260 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000261 SubExprs = NewSubExprs;
262 this->NumArgs = NumArgs;
263}
264
Chris Lattnerc24915f2008-10-06 05:00:53 +0000265/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
266/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000267unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000268 // All simple function calls (e.g. func()) are implicitly cast to pointer to
269 // function. As a result, we try and obtain the DeclRefExpr from the
270 // ImplicitCastExpr.
271 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
272 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000273 return 0;
274
Steve Naroff44aec4c2008-01-31 01:07:12 +0000275 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
276 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000277 return 0;
278
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000279 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
280 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000281 return 0;
282
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000283 if (!FDecl->getIdentifier())
284 return 0;
285
Douglas Gregorb5af7382009-02-14 18:57:46 +0000286 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000287}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000288
Anders Carlsson66070902009-05-26 04:57:27 +0000289QualType CallExpr::getCallReturnType() const {
290 QualType CalleeType = getCallee()->getType();
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000291 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Anders Carlsson66070902009-05-26 04:57:27 +0000292 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000293 else if (const BlockPointerType *BPT = CalleeType->getAsBlockPointerType())
Anders Carlsson66070902009-05-26 04:57:27 +0000294 CalleeType = BPT->getPointeeType();
295
296 const FunctionType *FnType = CalleeType->getAsFunctionType();
297 return FnType->getResultType();
298}
Chris Lattnerc24915f2008-10-06 05:00:53 +0000299
Chris Lattner4b009652007-07-25 00:24:17 +0000300/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
301/// corresponds to, e.g. "<<=".
302const char *BinaryOperator::getOpcodeStr(Opcode Op) {
303 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000304 case PtrMemD: return ".*";
305 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000306 case Mul: return "*";
307 case Div: return "/";
308 case Rem: return "%";
309 case Add: return "+";
310 case Sub: return "-";
311 case Shl: return "<<";
312 case Shr: return ">>";
313 case LT: return "<";
314 case GT: return ">";
315 case LE: return "<=";
316 case GE: return ">=";
317 case EQ: return "==";
318 case NE: return "!=";
319 case And: return "&";
320 case Xor: return "^";
321 case Or: return "|";
322 case LAnd: return "&&";
323 case LOr: return "||";
324 case Assign: return "=";
325 case MulAssign: return "*=";
326 case DivAssign: return "/=";
327 case RemAssign: return "%=";
328 case AddAssign: return "+=";
329 case SubAssign: return "-=";
330 case ShlAssign: return "<<=";
331 case ShrAssign: return ">>=";
332 case AndAssign: return "&=";
333 case XorAssign: return "^=";
334 case OrAssign: return "|=";
335 case Comma: return ",";
336 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000337
338 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000339}
340
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000341BinaryOperator::Opcode
342BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
343 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000344 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000345 case OO_Plus: return Add;
346 case OO_Minus: return Sub;
347 case OO_Star: return Mul;
348 case OO_Slash: return Div;
349 case OO_Percent: return Rem;
350 case OO_Caret: return Xor;
351 case OO_Amp: return And;
352 case OO_Pipe: return Or;
353 case OO_Equal: return Assign;
354 case OO_Less: return LT;
355 case OO_Greater: return GT;
356 case OO_PlusEqual: return AddAssign;
357 case OO_MinusEqual: return SubAssign;
358 case OO_StarEqual: return MulAssign;
359 case OO_SlashEqual: return DivAssign;
360 case OO_PercentEqual: return RemAssign;
361 case OO_CaretEqual: return XorAssign;
362 case OO_AmpEqual: return AndAssign;
363 case OO_PipeEqual: return OrAssign;
364 case OO_LessLess: return Shl;
365 case OO_GreaterGreater: return Shr;
366 case OO_LessLessEqual: return ShlAssign;
367 case OO_GreaterGreaterEqual: return ShrAssign;
368 case OO_EqualEqual: return EQ;
369 case OO_ExclaimEqual: return NE;
370 case OO_LessEqual: return LE;
371 case OO_GreaterEqual: return GE;
372 case OO_AmpAmp: return LAnd;
373 case OO_PipePipe: return LOr;
374 case OO_Comma: return Comma;
375 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000376 }
377}
378
379OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
380 static const OverloadedOperatorKind OverOps[] = {
381 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
382 OO_Star, OO_Slash, OO_Percent,
383 OO_Plus, OO_Minus,
384 OO_LessLess, OO_GreaterGreater,
385 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
386 OO_EqualEqual, OO_ExclaimEqual,
387 OO_Amp,
388 OO_Caret,
389 OO_Pipe,
390 OO_AmpAmp,
391 OO_PipePipe,
392 OO_Equal, OO_StarEqual,
393 OO_SlashEqual, OO_PercentEqual,
394 OO_PlusEqual, OO_MinusEqual,
395 OO_LessLessEqual, OO_GreaterGreaterEqual,
396 OO_AmpEqual, OO_CaretEqual,
397 OO_PipeEqual,
398 OO_Comma
399 };
400 return OverOps[Opc];
401}
402
Anders Carlsson762b7c72007-08-31 04:56:16 +0000403InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000404 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000405 SourceLocation rbraceloc)
Douglas Gregor3a7a06e2009-05-21 23:17:49 +0000406 : Expr(InitListExprClass, QualType(),
407 hasAnyTypeDependentArguments(initExprs, numInits),
408 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor82462762009-01-29 16:53:55 +0000409 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000410 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000411
412 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000413}
Chris Lattner4b009652007-07-25 00:24:17 +0000414
Douglas Gregoree0792c2009-03-20 23:58:33 +0000415void InitListExpr::reserveInits(unsigned NumInits) {
416 if (NumInits > InitExprs.size())
417 InitExprs.reserve(NumInits);
418}
419
Douglas Gregorf603b472009-01-28 21:54:33 +0000420void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000421 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000422 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000423 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000424 InitExprs.resize(NumInits, 0);
425}
426
427Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
428 if (Init >= InitExprs.size()) {
429 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
430 InitExprs.back() = expr;
431 return 0;
432 }
433
434 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
435 InitExprs[Init] = expr;
436 return Result;
437}
438
Steve Naroff6f373332008-09-04 15:31:07 +0000439/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000440///
441const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000442 return getType()->getAsBlockPointerType()->
Steve Naroff52a81c02008-09-03 18:15:37 +0000443 getPointeeType()->getAsFunctionType();
444}
445
Steve Naroff9ac456d2008-10-08 17:01:13 +0000446SourceLocation BlockExpr::getCaretLocation() const {
447 return TheBlock->getCaretLocation();
448}
Douglas Gregore3241e92009-04-18 00:02:19 +0000449const Stmt *BlockExpr::getBody() const {
450 return TheBlock->getBody();
451}
452Stmt *BlockExpr::getBody() {
453 return TheBlock->getBody();
454}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000455
456
Chris Lattner4b009652007-07-25 00:24:17 +0000457//===----------------------------------------------------------------------===//
458// Generic Expression Routines
459//===----------------------------------------------------------------------===//
460
Chris Lattnerd2c66552009-02-14 07:37:35 +0000461/// isUnusedResultAWarning - Return true if this immediate expression should
462/// be warned about if the result is unused. If so, fill in Loc and Ranges
463/// with location to warn on and the source range[s] to report with the
464/// warning.
465bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000466 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000467 // Don't warn if the expr is type dependent. The type could end up
468 // instantiating to void.
469 if (isTypeDependent())
470 return false;
471
Chris Lattner4b009652007-07-25 00:24:17 +0000472 switch (getStmtClass()) {
473 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000474 Loc = getExprLoc();
475 R1 = getSourceRange();
476 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000478 return cast<ParenExpr>(this)->getSubExpr()->
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000479 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000480 case UnaryOperatorClass: {
481 const UnaryOperator *UO = cast<UnaryOperator>(this);
482
483 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000484 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000485 case UnaryOperator::PostInc:
486 case UnaryOperator::PostDec:
487 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000488 case UnaryOperator::PreDec: // ++/--
489 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000490 case UnaryOperator::Deref:
491 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000492 if (getType().isVolatileQualified())
493 return false;
494 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000495 case UnaryOperator::Real:
496 case UnaryOperator::Imag:
497 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000498 if (UO->getSubExpr()->getType().isVolatileQualified())
499 return false;
500 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000501 case UnaryOperator::Extension:
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000502 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000503 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000504 Loc = UO->getOperatorLoc();
505 R1 = UO->getSubExpr()->getSourceRange();
506 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000507 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000508 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000509 const BinaryOperator *BO = cast<BinaryOperator>(this);
510 // Consider comma to have side effects if the LHS or RHS does.
511 if (BO->getOpcode() == BinaryOperator::Comma)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000512 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
513 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000514
Chris Lattnerd2c66552009-02-14 07:37:35 +0000515 if (BO->isAssignmentOp())
516 return false;
517 Loc = BO->getOperatorLoc();
518 R1 = BO->getLHS()->getSourceRange();
519 R2 = BO->getRHS()->getSourceRange();
520 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000521 }
Chris Lattner06078d22007-08-25 02:00:02 +0000522 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000523 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000524
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000525 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000526 // The condition must be evaluated, but if either the LHS or RHS is a
527 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000528 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000529 if (Exp->getLHS() &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000530 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000531 return true;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000532 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000533 }
534
Chris Lattner4b009652007-07-25 00:24:17 +0000535 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000536 // If the base pointer or element is to a volatile pointer/field, accessing
537 // it is a side effect.
538 if (getType().isVolatileQualified())
539 return false;
540 Loc = cast<MemberExpr>(this)->getMemberLoc();
541 R1 = SourceRange(Loc, Loc);
542 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
543 return true;
544
Chris Lattner4b009652007-07-25 00:24:17 +0000545 case ArraySubscriptExprClass:
546 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000547 // it is a side effect.
548 if (getType().isVolatileQualified())
549 return false;
550 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
551 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
552 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
553 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000554
Chris Lattner4b009652007-07-25 00:24:17 +0000555 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000556 case CXXOperatorCallExprClass:
557 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000558 // If this is a direct call, get the callee.
559 const CallExpr *CE = cast<CallExpr>(this);
560 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
561 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
562 // If the callee has attribute pure, const, or warn_unused_result, warn
563 // about it. void foo() { strlen("bar"); } should warn.
564 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000565 if (FD->getAttr<WarnUnusedResultAttr>() ||
566 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000567 Loc = CE->getCallee()->getLocStart();
568 R1 = CE->getCallee()->getSourceRange();
569
570 if (unsigned NumArgs = CE->getNumArgs())
571 R2 = SourceRange(CE->getArg(0)->getLocStart(),
572 CE->getArg(NumArgs-1)->getLocEnd());
573 return true;
574 }
575 }
576 return false;
577 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000578 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000579 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000580 case StmtExprClass: {
581 // Statement exprs don't logically have side effects themselves, but are
582 // sometimes used in macros in ways that give them a type that is unused.
583 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
584 // however, if the result of the stmt expr is dead, we don't want to emit a
585 // warning.
586 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
587 if (!CS->body_empty())
588 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000589 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000590
591 Loc = cast<StmtExpr>(this)->getLParenLoc();
592 R1 = getSourceRange();
593 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000594 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000595 case CStyleCastExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000596 // If this is a cast to void, check the operand. Otherwise, the result of
597 // the cast is unused.
598 if (getType()->isVoidType())
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000599 return cast<CastExpr>(this)->getSubExpr()
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000600 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000601 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
602 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
603 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000604 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000605 // If this is a cast to void, check the operand. Otherwise, the result of
606 // the cast is unused.
607 if (getType()->isVoidType())
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000608 return cast<CastExpr>(this)->getSubExpr()
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000609 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000610 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
611 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
612 return true;
613
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000614 case ImplicitCastExprClass:
615 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000616 return cast<ImplicitCastExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000617 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000618
Chris Lattner3e254fb2008-04-08 04:40:51 +0000619 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000620 return cast<CXXDefaultArgExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000621 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000622
623 case CXXNewExprClass:
624 // FIXME: In theory, there might be new expressions that don't have side
625 // effects (e.g. a placement new with an uninitialized POD).
626 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000627 return false;
Anders Carlsson18ca4772009-05-17 21:11:30 +0000628 case CXXExprWithTemporariesClass:
629 return cast<CXXExprWithTemporaries>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000630 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000631 }
Chris Lattner4b009652007-07-25 00:24:17 +0000632}
633
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000634/// DeclCanBeLvalue - Determine whether the given declaration can be
635/// an lvalue. This is a helper routine for isLvalue.
636static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000637 // C++ [temp.param]p6:
638 // A non-type non-reference template-parameter is not an lvalue.
639 if (const NonTypeTemplateParmDecl *NTTParm
640 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
641 return NTTParm->getType()->isReferenceType();
642
Douglas Gregor8acb7272008-12-11 16:49:14 +0000643 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000644 // C++ 3.10p2: An lvalue refers to an object or function.
645 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor62f78762009-07-08 20:55:45 +0000646 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
647 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000648}
649
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
651/// incomplete type other than void. Nonarray expressions that can be lvalues:
652/// - name, where name must be a variable
653/// - e[i]
654/// - (e), where e must be an lvalue
655/// - e.name, where e must be an lvalue
656/// - e->name
657/// - *e, the type of e cannot be a function type
658/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000659/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000660/// - reference type [C++ [expr]]
661///
Chris Lattner25168a52008-07-26 21:30:36 +0000662Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000663 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
664
665 isLvalueResult Res = isLvalueInternal(Ctx);
666 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
667 return Res;
668
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000669 // first, check the type (C99 6.3.2.1). Expressions with function
670 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor62f78762009-07-08 20:55:45 +0000671 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Chris Lattner4b009652007-07-25 00:24:17 +0000672 return LV_NotObjectType;
673
Steve Naroffec7736d2008-02-10 01:39:04 +0000674 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000675 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000676 return LV_IncompleteVoidType;
677
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000678 return LV_Valid;
679}
Chris Lattner4b009652007-07-25 00:24:17 +0000680
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000681// Check whether the expression can be sanely treated like an l-value
682Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000683 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000684 case StringLiteralClass: // C99 6.5.1p4
685 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000686 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000687 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
688 // For vectors, make sure base is an lvalue (i.e. not a function call).
689 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000690 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000691 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000692 case DeclRefExprClass:
693 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000694 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
695 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000696 return LV_Valid;
697 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000698 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000699 case BlockDeclRefExprClass: {
700 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000701 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000702 return LV_Valid;
703 break;
704 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000705 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000706 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000707 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
708 NamedDecl *Member = m->getMemberDecl();
709 // C++ [expr.ref]p4:
710 // If E2 is declared to have type "reference to T", then E1.E2
711 // is an lvalue.
712 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
713 if (Value->getType()->isReferenceType())
714 return LV_Valid;
715
716 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000717 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000718 return LV_Valid;
719
720 // -- If E2 is a non-static data member [...]. If E1 is an
721 // lvalue, then E1.E2 is an lvalue.
722 if (isa<FieldDecl>(Member))
723 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
724
725 // -- If it refers to a static member function [...], then
726 // E1.E2 is an lvalue.
727 // -- Otherwise, if E1.E2 refers to a non-static member
728 // function [...], then E1.E2 is not an lvalue.
729 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
730 return Method->isStatic()? LV_Valid : LV_MemberFunction;
731
732 // -- If E2 is a member enumerator [...], the expression E1.E2
733 // is not an lvalue.
734 if (isa<EnumConstantDecl>(Member))
735 return LV_InvalidExpression;
736
737 // Not an lvalue.
738 return LV_InvalidExpression;
739 }
740
741 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000742 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000743 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000744 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000745 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000746 return LV_Valid; // C99 6.5.3p4
747
748 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000749 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
750 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000751 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000752
753 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
754 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
755 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
756 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000757 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000758 case ImplicitCastExprClass:
759 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
760 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000761 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000762 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000763 case BinaryOperatorClass:
764 case CompoundAssignOperatorClass: {
765 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000766
767 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
768 BinOp->getOpcode() == BinaryOperator::Comma)
769 return BinOp->getRHS()->isLvalue(Ctx);
770
Sebastian Redl95216a62009-02-07 00:15:38 +0000771 // C++ [expr.mptr.oper]p6
772 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
773 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
774 !BinOp->getType()->isFunctionType())
775 return BinOp->getLHS()->isLvalue(Ctx);
776
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000777 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000778 return LV_InvalidExpression;
779
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000780 if (Ctx.getLangOptions().CPlusPlus)
781 // C++ [expr.ass]p1:
782 // The result of an assignment operation [...] is an lvalue.
783 return LV_Valid;
784
785
786 // C99 6.5.16:
787 // An assignment expression [...] is not an lvalue.
788 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000789 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000790 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000791 case CXXOperatorCallExprClass:
792 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000793 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000794 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000795 // is an lvalue reference.
Anders Carlsson66070902009-05-26 04:57:27 +0000796 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
797 if (ReturnType->isLValueReferenceType())
798 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000799
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000800 break;
801 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000802 case CompoundLiteralExprClass: // C99 6.5.2.5p5
803 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000804 case ChooseExprClass:
805 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000806 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000807 case ExtVectorElementExprClass:
808 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000809 return LV_DuplicateVectorComponents;
810 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000811 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
812 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000813 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
814 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000815 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000816 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000817 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000818 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000819 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000820 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000821 case CXXConditionDeclExprClass:
822 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000823 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000824 case CXXFunctionalCastExprClass:
825 case CXXStaticCastExprClass:
826 case CXXDynamicCastExprClass:
827 case CXXReinterpretCastExprClass:
828 case CXXConstCastExprClass:
829 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000830 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000831 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
832 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000833 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
834 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000835 return LV_Valid;
836 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000837 case CXXTypeidExprClass:
838 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
839 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000840 case ConditionalOperatorClass: {
841 // Complicated handling is only for C++.
842 if (!Ctx.getLangOptions().CPlusPlus)
843 return LV_InvalidExpression;
844
845 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
846 // everywhere there's an object converted to an rvalue. Also, any other
847 // casts should be wrapped by ImplicitCastExprs. There's just the special
848 // case involving throws to work out.
849 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000850 Expr *True = Cond->getTrueExpr();
851 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000852 // C++0x 5.16p2
853 // If either the second or the third operand has type (cv) void, [...]
854 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000855 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000856 return LV_InvalidExpression;
857
858 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000859 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000860 return LV_InvalidExpression;
861
862 // That's it.
863 return LV_Valid;
864 }
865
Chris Lattner4b009652007-07-25 00:24:17 +0000866 default:
867 break;
868 }
869 return LV_InvalidExpression;
870}
871
872/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
873/// does not have an incomplete type, does not have a const-qualified type, and
874/// if it is a structure or union, does not have any member (including,
875/// recursively, any member or element of all contained aggregates or unions)
876/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000877Expr::isModifiableLvalueResult
878Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000879 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000880
881 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000882 case LV_Valid:
883 // C++ 3.10p11: Functions cannot be modified, but pointers to
884 // functions can be modifiable.
885 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
886 return MLV_NotObjectType;
887 break;
888
Chris Lattner4b009652007-07-25 00:24:17 +0000889 case LV_NotObjectType: return MLV_NotObjectType;
890 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000891 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000892 case LV_InvalidExpression:
893 // If the top level is a C-style cast, and the subexpression is a valid
894 // lvalue, then this is probably a use of the old-school "cast as lvalue"
895 // GCC extension. We don't support it, but we want to produce good
896 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000897 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
898 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
899 if (Loc)
900 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000901 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000902 }
903 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000904 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000905 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000906 }
Eli Friedman91571da2009-03-22 23:26:56 +0000907
908 // The following is illegal:
909 // void takeclosure(void (^C)(void));
910 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
911 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000912 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000913 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
914 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
915 return MLV_NotBlockQualified;
916 }
917
Chris Lattnera1923f62008-08-04 07:31:14 +0000918 QualType CT = Ctx.getCanonicalType(getType());
919
920 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000921 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000922 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000923 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000924 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000925 return MLV_IncompleteType;
926
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000927 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000928 if (r->hasConstFields())
929 return MLV_ConstQualified;
930 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000931
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000932 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000933 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000934 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
935 if (KVCExpr->getSetterMethod() == 0)
936 return MLV_NoSetterProperty;
937 }
Chris Lattner4b009652007-07-25 00:24:17 +0000938 return MLV_Valid;
939}
940
Ted Kremenek5778d622008-02-27 18:39:48 +0000941/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000942/// duration. This means that the address of this expression is a link-time
943/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000944bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000945 switch (getStmtClass()) {
946 default:
947 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000948 case BlockExprClass:
949 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000950 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000951 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000952 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000953 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000954 case CompoundLiteralExprClass:
955 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000956 case DeclRefExprClass:
957 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000958 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
959 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000960 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000961 if (isa<FunctionDecl>(D))
962 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000963 return false;
964 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000965 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000966 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000967 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000968 }
Chris Lattner743ec372007-11-27 21:35:27 +0000969 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000970 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000971 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000972 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000973 case CXXDefaultArgExprClass:
974 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000975 }
976}
977
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000978/// isOBJCGCCandidate - Check if an expression is objc gc'able.
979///
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000980bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000981 switch (getStmtClass()) {
982 default:
983 return false;
984 case ObjCIvarRefExprClass:
985 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000986 case Expr::UnaryOperatorClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000987 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000988 case ParenExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000989 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000990 case ImplicitCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000991 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000992 case CStyleCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000993 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000994 case DeclRefExprClass:
995 case QualifiedDeclRefExprClass: {
996 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000997 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
998 if (VD->hasGlobalStorage())
999 return true;
1000 QualType T = VD->getType();
1001 // dereferencing to an object pointer is always a gc'able candidate
1002 if (T->isPointerType() &&
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00001003 T->getAsPointerType()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001004 return true;
1005
1006 }
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001007 return false;
1008 }
1009 case MemberExprClass: {
1010 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001011 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001012 }
1013 case ArraySubscriptExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001014 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001015 }
1016}
Ted Kremenek87e30c52008-01-17 16:57:34 +00001017Expr* Expr::IgnoreParens() {
1018 Expr* E = this;
1019 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1020 E = P->getSubExpr();
1021
1022 return E;
1023}
1024
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001025/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1026/// or CastExprs or ImplicitCastExprs, returning their operand.
1027Expr *Expr::IgnoreParenCasts() {
1028 Expr *E = this;
1029 while (true) {
1030 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1031 E = P->getSubExpr();
1032 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1033 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001034 else
1035 return E;
1036 }
1037}
1038
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001039/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1040/// value (including ptr->int casts of the same size). Strip off any
1041/// ParenExpr or CastExprs, returning their operand.
1042Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1043 Expr *E = this;
1044 while (true) {
1045 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1046 E = P->getSubExpr();
1047 continue;
1048 }
1049
1050 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1051 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1052 // ptr<->int casts of the same width. We also ignore all identify casts.
1053 Expr *SE = P->getSubExpr();
1054
1055 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1056 E = SE;
1057 continue;
1058 }
1059
1060 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1061 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1062 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1063 E = SE;
1064 continue;
1065 }
1066 }
1067
1068 return E;
1069 }
1070}
1071
1072
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001073/// hasAnyTypeDependentArguments - Determines if any of the expressions
1074/// in Exprs is type-dependent.
1075bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1076 for (unsigned I = 0; I < NumExprs; ++I)
1077 if (Exprs[I]->isTypeDependent())
1078 return true;
1079
1080 return false;
1081}
1082
1083/// hasAnyValueDependentArguments - Determines if any of the expressions
1084/// in Exprs is value-dependent.
1085bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1086 for (unsigned I = 0; I < NumExprs; ++I)
1087 if (Exprs[I]->isValueDependent())
1088 return true;
1089
1090 return false;
1091}
1092
Eli Friedmandee41122009-01-25 02:32:41 +00001093bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001094 // This function is attempting whether an expression is an initializer
1095 // which can be evaluated at compile-time. isEvaluatable handles most
1096 // of the cases, but it can't deal with some initializer-specific
1097 // expressions, and it can't deal with aggregates; we deal with those here,
1098 // and fall back to isEvaluatable for the other cases.
1099
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001100 // FIXME: This function assumes the variable being assigned to
1101 // isn't a reference type!
1102
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001103 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001104 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001105 case StringLiteralClass:
Steve Naroff329ec222009-07-10 23:34:53 +00001106 case ObjCStringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001107 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001108 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001109 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001110 // This handles gcc's extension that allows global initializers like
1111 // "struct x {int x;} x = (struct x) {};".
1112 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001113 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001114 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001115 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001116 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001117 // FIXME: This doesn't deal with fields with reference types correctly.
1118 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1119 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001120 const InitListExpr *Exp = cast<InitListExpr>(this);
1121 unsigned numInits = Exp->getNumInits();
1122 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001123 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001124 return false;
1125 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001126 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001127 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001128 case ImplicitValueInitExprClass:
1129 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001130 case ParenExprClass: {
1131 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1132 }
1133 case UnaryOperatorClass: {
1134 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1135 if (Exp->getOpcode() == UnaryOperator::Extension)
1136 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1137 break;
1138 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001139 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001140 case CStyleCastExprClass:
1141 // Handle casts with a destination that's a struct or union; this
1142 // deals with both the gcc no-op struct cast extension and the
1143 // cast-to-union extension.
1144 if (getType()->isRecordType())
1145 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1146 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001147 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001148 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001149}
1150
Chris Lattner4b009652007-07-25 00:24:17 +00001151/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001152/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001153
1154/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1155/// comma, etc
1156///
Chris Lattner4b009652007-07-25 00:24:17 +00001157/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1158/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1159/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001160
Eli Friedman7beeda62009-02-26 09:29:13 +00001161// CheckICE - This function does the fundamental ICE checking: the returned
1162// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1163// Note that to reduce code duplication, this helper does no evaluation
1164// itself; the caller checks whether the expression is evaluatable, and
1165// in the rare cases where CheckICE actually cares about the evaluated
1166// value, it calls into Evalute.
1167//
1168// Meanings of Val:
1169// 0: This expression is an ICE if it can be evaluated by Evaluate.
1170// 1: This expression is not an ICE, but if it isn't evaluated, it's
1171// a legal subexpression for an ICE. This return value is used to handle
1172// the comma operator in C99 mode.
1173// 2: This expression is not an ICE, and is not a legal subexpression for one.
1174
1175struct ICEDiag {
1176 unsigned Val;
1177 SourceLocation Loc;
1178
1179 public:
1180 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1181 ICEDiag() : Val(0) {}
1182};
1183
1184ICEDiag NoDiag() { return ICEDiag(); }
1185
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001186static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1187 Expr::EvalResult EVResult;
1188 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1189 !EVResult.Val.isInt()) {
1190 return ICEDiag(2, E->getLocStart());
1191 }
1192 return NoDiag();
1193}
1194
Eli Friedman7beeda62009-02-26 09:29:13 +00001195static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001196 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001197 if (!E->getType()->isIntegralType()) {
1198 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001199 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001200
1201 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001202 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001203 return ICEDiag(2, E->getLocStart());
1204 case Expr::ParenExprClass:
1205 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1206 case Expr::IntegerLiteralClass:
1207 case Expr::CharacterLiteralClass:
1208 case Expr::CXXBoolLiteralExprClass:
1209 case Expr::CXXZeroInitValueExprClass:
1210 case Expr::TypesCompatibleExprClass:
1211 case Expr::UnaryTypeTraitExprClass:
1212 return NoDiag();
1213 case Expr::CallExprClass:
1214 case Expr::CXXOperatorCallExprClass: {
1215 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001216 if (CE->isBuiltinCall(Ctx))
1217 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001218 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001219 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001220 case Expr::DeclRefExprClass:
1221 case Expr::QualifiedDeclRefExprClass:
1222 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1223 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001224 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001225 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001226 // C++ 7.1.5.1p2
1227 // A variable of non-volatile const-qualified integral or enumeration
1228 // type initialized by an ICE can be used in ICEs.
1229 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001230 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor4833ff02009-05-26 18:54:04 +00001231 if (Dcl->isInitKnownICE()) {
1232 // We have already checked whether this subexpression is an
1233 // integral constant expression.
1234 if (Dcl->isInitICE())
1235 return NoDiag();
1236 else
1237 return ICEDiag(2, E->getLocStart());
1238 }
1239
1240 if (const Expr *Init = Dcl->getInit()) {
1241 ICEDiag Result = CheckICE(Init, Ctx);
1242 // Cache the result of the ICE test.
1243 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1244 return Result;
1245 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001246 }
1247 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001248 return ICEDiag(2, E->getLocStart());
1249 case Expr::UnaryOperatorClass: {
1250 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001251 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001252 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001253 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001254 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001256 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001257 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001258 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001259 case UnaryOperator::Real:
1260 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001261 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001262 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001263 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1264 // Evaluate matches the proposed gcc behavior for cases like
1265 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1266 // compliance: we should warn earlier for offsetof expressions with
1267 // array subscripts that aren't ICEs, and if the array subscripts
1268 // are ICEs, the value of the offsetof must be an integer constant.
1269 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001270 }
Chris Lattner4b009652007-07-25 00:24:17 +00001271 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001272 case Expr::SizeOfAlignOfExprClass: {
1273 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1274 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1275 return ICEDiag(2, E->getLocStart());
1276 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001277 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001278 case Expr::BinaryOperatorClass: {
1279 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001280 switch (Exp->getOpcode()) {
1281 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001282 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001283 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001284 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001285 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001286 case BinaryOperator::Add:
1287 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001288 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001289 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001290 case BinaryOperator::LT:
1291 case BinaryOperator::GT:
1292 case BinaryOperator::LE:
1293 case BinaryOperator::GE:
1294 case BinaryOperator::EQ:
1295 case BinaryOperator::NE:
1296 case BinaryOperator::And:
1297 case BinaryOperator::Xor:
1298 case BinaryOperator::Or:
1299 case BinaryOperator::Comma: {
1300 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1301 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001302 if (Exp->getOpcode() == BinaryOperator::Div ||
1303 Exp->getOpcode() == BinaryOperator::Rem) {
1304 // Evaluate gives an error for undefined Div/Rem, so make sure
1305 // we don't evaluate one.
1306 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1307 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1308 if (REval == 0)
1309 return ICEDiag(1, E->getLocStart());
1310 if (REval.isSigned() && REval.isAllOnesValue()) {
1311 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1312 if (LEval.isMinSignedValue())
1313 return ICEDiag(1, E->getLocStart());
1314 }
1315 }
1316 }
1317 if (Exp->getOpcode() == BinaryOperator::Comma) {
1318 if (Ctx.getLangOptions().C99) {
1319 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1320 // if it isn't evaluated.
1321 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1322 return ICEDiag(1, E->getLocStart());
1323 } else {
1324 // In both C89 and C++, commas in ICEs are illegal.
1325 return ICEDiag(2, E->getLocStart());
1326 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001327 }
1328 if (LHSResult.Val >= RHSResult.Val)
1329 return LHSResult;
1330 return RHSResult;
1331 }
Chris Lattner4b009652007-07-25 00:24:17 +00001332 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001333 case BinaryOperator::LOr: {
1334 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1335 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1336 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1337 // Rare case where the RHS has a comma "side-effect"; we need
1338 // to actually check the condition to see whether the side
1339 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001340 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001341 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001342 return RHSResult;
1343 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001344 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001345
Eli Friedman7beeda62009-02-26 09:29:13 +00001346 if (LHSResult.Val >= RHSResult.Val)
1347 return LHSResult;
1348 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001349 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001350 }
Chris Lattner4b009652007-07-25 00:24:17 +00001351 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001352 case Expr::ImplicitCastExprClass:
1353 case Expr::CStyleCastExprClass:
1354 case Expr::CXXFunctionalCastExprClass: {
1355 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1356 if (SubExpr->getType()->isIntegralType())
1357 return CheckICE(SubExpr, Ctx);
1358 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1359 return NoDiag();
1360 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001361 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001362 case Expr::ConditionalOperatorClass: {
1363 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001364 // If the condition (ignoring parens) is a __builtin_constant_p call,
1365 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001366 // expression, and it is fully evaluated. This is an important GNU
1367 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001368 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001369 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001370 Expr::EvalResult EVResult;
1371 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1372 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001373 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001374 }
1375 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001376 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001377 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1378 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1379 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1380 if (CondResult.Val == 2)
1381 return CondResult;
1382 if (TrueResult.Val == 2)
1383 return TrueResult;
1384 if (FalseResult.Val == 2)
1385 return FalseResult;
1386 if (CondResult.Val == 1)
1387 return CondResult;
1388 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1389 return NoDiag();
1390 // Rare case where the diagnostics depend on which side is evaluated
1391 // Note that if we get here, CondResult is 0, and at least one of
1392 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001393 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001394 return FalseResult;
1395 }
1396 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001397 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001398 case Expr::CXXDefaultArgExprClass:
1399 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001400 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001401 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001402 }
Chris Lattner4b009652007-07-25 00:24:17 +00001403 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001404}
Chris Lattner4b009652007-07-25 00:24:17 +00001405
Eli Friedman7beeda62009-02-26 09:29:13 +00001406bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1407 SourceLocation *Loc, bool isEvaluated) const {
1408 ICEDiag d = CheckICE(this, Ctx);
1409 if (d.Val != 0) {
1410 if (Loc) *Loc = d.Loc;
1411 return false;
1412 }
1413 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001414 if (!Evaluate(EvalResult, Ctx))
1415 assert(0 && "ICE cannot be evaluated!");
1416 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1417 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001418 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001419 return true;
1420}
1421
Chris Lattner4b009652007-07-25 00:24:17 +00001422/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1423/// integer constant expression with the value zero, or if this is one that is
1424/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001425bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1426{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001427 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001428 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001429 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001430 // Check that it is a cast to void*.
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00001431 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001432 QualType Pointee = PT->getPointeeType();
1433 if (Pointee.getCVRQualifiers() == 0 &&
1434 Pointee->isVoidType() && // to void*
1435 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001436 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001437 }
Chris Lattner4b009652007-07-25 00:24:17 +00001438 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001439 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1440 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001441 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001442 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1443 // Accept ((void*)0) as a null pointer constant, as many other
1444 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001445 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001446 } else if (const CXXDefaultArgExpr *DefaultArg
1447 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001448 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001449 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001450 } else if (isa<GNUNullExpr>(this)) {
1451 // The GNU __null extension is always a null pointer constant.
1452 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001453 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001454
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001455 // C++0x nullptr_t is always a null pointer constant.
1456 if (getType()->isNullPtrType())
1457 return true;
1458
Steve Naroffa2e53222008-01-14 16:10:57 +00001459 // This expression must be an integer type.
1460 if (!getType()->isIntegerType())
1461 return false;
1462
Chris Lattner4b009652007-07-25 00:24:17 +00001463 // If we have an integer constant expression, we need to *evaluate* it and
1464 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001465 llvm::APSInt Result;
1466 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001467}
Steve Naroffc11705f2007-07-28 23:10:27 +00001468
Douglas Gregor531434b2009-05-02 02:18:30 +00001469FieldDecl *Expr::getBitField() {
Douglas Gregor34909522009-07-06 15:38:40 +00001470 Expr *E = this->IgnoreParens();
Douglas Gregor531434b2009-05-02 02:18:30 +00001471
Douglas Gregor81c29152008-10-29 00:13:59 +00001472 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001473 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001474 if (Field->isBitField())
1475 return Field;
1476
1477 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1478 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1479 return BinOp->getLHS()->getBitField();
1480
1481 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001482}
1483
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001484/// isArrow - Return true if the base expression is a pointer to vector,
1485/// return false if the base expression is a vector.
1486bool ExtVectorElementExpr::isArrow() const {
1487 return getBase()->getType()->isPointerType();
1488}
1489
Nate Begemanaf6ed502008-04-18 23:10:10 +00001490unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001491 if (const VectorType *VT = getType()->getAsVectorType())
1492 return VT->getNumElements();
1493 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001494}
1495
Nate Begemanc8e51f82008-05-09 06:41:27 +00001496/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001497bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001498 const char *compStr = Accessor->getName();
1499 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001500
1501 // Halving swizzles do not contain duplicate elements.
1502 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1503 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1504 return false;
1505
1506 // Advance past s-char prefix on hex swizzles.
Nate Begemane2ed6f72009-06-25 21:06:09 +00001507 if (*compStr == 's' || *compStr == 'S') {
Nate Begemana8e117c2009-01-18 02:01:21 +00001508 compStr++;
1509 length--;
1510 }
Steve Naroffba67f692007-07-30 03:29:09 +00001511
Chris Lattner58d3fa52008-11-19 07:55:04 +00001512 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001513 const char *s = compStr+i;
1514 for (const char c = *s++; *s; s++)
1515 if (c == *s)
1516 return true;
1517 }
1518 return false;
1519}
Chris Lattner42158e72007-08-02 23:36:59 +00001520
Nate Begemanc8e51f82008-05-09 06:41:27 +00001521/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001522void ExtVectorElementExpr::getEncodedElementAccess(
1523 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001524 const char *compStr = Accessor->getName();
Nate Begemane2ed6f72009-06-25 21:06:09 +00001525 if (*compStr == 's' || *compStr == 'S')
Nate Begeman1486b502009-01-18 01:47:54 +00001526 compStr++;
1527
1528 bool isHi = !strcmp(compStr, "hi");
1529 bool isLo = !strcmp(compStr, "lo");
1530 bool isEven = !strcmp(compStr, "even");
1531 bool isOdd = !strcmp(compStr, "odd");
1532
Nate Begemanc8e51f82008-05-09 06:41:27 +00001533 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1534 uint64_t Index;
1535
1536 if (isHi)
1537 Index = e + i;
1538 else if (isLo)
1539 Index = i;
1540 else if (isEven)
1541 Index = 2 * i;
1542 else if (isOdd)
1543 Index = 2 * i + 1;
1544 else
1545 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001546
Nate Begemana1ae7442008-05-13 21:03:02 +00001547 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001548 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001549}
1550
Steve Naroff4ed9d662007-09-27 14:38:14 +00001551// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001552ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001553 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001554 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001555 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001556 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001557 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001558 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001559 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001560 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001561 if (NumArgs) {
1562 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001563 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1564 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001565 LBracloc = LBrac;
1566 RBracloc = RBrac;
1567}
1568
Anders Carlsson0ebbcba2009-06-07 19:51:47 +00001569ObjCStringLiteral* ObjCStringLiteral::Clone(ASTContext &C) const {
1570 // Clone the string literal.
1571 StringLiteral *NewString =
1572 String ? cast<StringLiteral>(String)->Clone(C) : 0;
1573
1574 return new (C) ObjCStringLiteral(NewString, getType(), AtLoc);
1575}
1576
1577ObjCSelectorExpr *ObjCSelectorExpr::Clone(ASTContext &C) const {
1578 return new (C) ObjCSelectorExpr(getType(), SelName, AtLoc, RParenLoc);
1579}
1580
1581ObjCProtocolExpr *ObjCProtocolExpr::Clone(ASTContext &C) const {
Fariborz Jahanian77e15a52009-06-21 18:26:03 +00001582 return new (C) ObjCProtocolExpr(getType(), TheProtocol, AtLoc, RParenLoc);
Anders Carlsson0ebbcba2009-06-07 19:51:47 +00001583}
1584
Steve Naroff4ed9d662007-09-27 14:38:14 +00001585// constructor for class messages.
1586// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001587ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001588 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001589 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001590 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001591 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001592 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001593 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001594 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001595 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001596 if (NumArgs) {
1597 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001598 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1599 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001600 LBracloc = LBrac;
1601 RBracloc = RBrac;
1602}
1603
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001604// constructor for class messages.
1605ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1606 QualType retType, ObjCMethodDecl *mproto,
1607 SourceLocation LBrac, SourceLocation RBrac,
1608 Expr **ArgExprs, unsigned nargs)
1609: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1610MethodProto(mproto) {
1611 NumArgs = nargs;
1612 SubExprs = new Stmt*[NumArgs+1];
1613 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1614 if (NumArgs) {
1615 for (unsigned i = 0; i != NumArgs; ++i)
1616 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1617 }
1618 LBracloc = LBrac;
1619 RBracloc = RBrac;
1620}
1621
1622ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1623 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1624 switch (x & Flags) {
1625 default:
1626 assert(false && "Invalid ObjCMessageExpr.");
1627 case IsInstMeth:
1628 return ClassInfo(0, 0);
1629 case IsClsMethDeclUnknown:
1630 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1631 case IsClsMethDeclKnown: {
1632 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1633 return ClassInfo(D, D->getIdentifier());
1634 }
1635 }
1636}
1637
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001638void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1639 if (CI.first == 0 && CI.second == 0)
1640 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1641 else if (CI.first == 0)
1642 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1643 else
1644 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1645}
1646
1647
Chris Lattnerf624cd22007-10-25 00:29:32 +00001648bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001649 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001650}
1651
Douglas Gregor725e94b2009-04-16 00:01:45 +00001652void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1653 if (NumExprs)
1654 delete [] SubExprs;
1655
1656 SubExprs = new Stmt* [NumExprs];
1657 this->NumExprs = NumExprs;
1658 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1659}
1660
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001661void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1662 // Override default behavior of traversing children. If this has a type
1663 // operand and the type is a variable-length array, the child iteration
1664 // will iterate over the size expression. However, this expression belongs
1665 // to the type, not to this, so we don't want to delete it.
1666 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001667 if (isArgumentType()) {
1668 this->~SizeOfAlignOfExpr();
1669 C.Deallocate(this);
1670 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001671 else
1672 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001673}
1674
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001675//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001676// DesignatedInitExpr
1677//===----------------------------------------------------------------------===//
1678
1679IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1680 assert(Kind == FieldDesignator && "Only valid on a field designator");
1681 if (Field.NameOrField & 0x01)
1682 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1683 else
1684 return getField()->getIdentifier();
1685}
1686
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001687DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1688 const Designator *Designators,
1689 SourceLocation EqualOrColonLoc,
1690 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001691 Expr **IndexExprs,
1692 unsigned NumIndexExprs,
1693 Expr *Init)
1694 : Expr(DesignatedInitExprClass, Ty,
1695 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001696 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001697 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001698 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001699
1700 // Record the initializer itself.
1701 child_iterator Child = child_begin();
1702 *Child++ = Init;
1703
1704 // Copy the designators and their subexpressions, computing
1705 // value-dependence along the way.
1706 unsigned IndexIdx = 0;
1707 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001708 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001709
1710 if (this->Designators[I].isArrayDesignator()) {
1711 // Compute type- and value-dependence.
1712 Expr *Index = IndexExprs[IndexIdx];
1713 ValueDependent = ValueDependent ||
1714 Index->isTypeDependent() || Index->isValueDependent();
1715
1716 // Copy the index expressions into permanent storage.
1717 *Child++ = IndexExprs[IndexIdx++];
1718 } else if (this->Designators[I].isArrayRangeDesignator()) {
1719 // Compute type- and value-dependence.
1720 Expr *Start = IndexExprs[IndexIdx];
1721 Expr *End = IndexExprs[IndexIdx + 1];
1722 ValueDependent = ValueDependent ||
1723 Start->isTypeDependent() || Start->isValueDependent() ||
1724 End->isTypeDependent() || End->isValueDependent();
1725
1726 // Copy the start/end expressions into permanent storage.
1727 *Child++ = IndexExprs[IndexIdx++];
1728 *Child++ = IndexExprs[IndexIdx++];
1729 }
1730 }
1731
1732 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001733}
1734
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001735DesignatedInitExpr *
1736DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1737 unsigned NumDesignators,
1738 Expr **IndexExprs, unsigned NumIndexExprs,
1739 SourceLocation ColonOrEqualLoc,
1740 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001741 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001742 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001743 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1744 ColonOrEqualLoc, UsesColonSyntax,
1745 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001746}
1747
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001748DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1749 unsigned NumIndexExprs) {
1750 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1751 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1752 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1753}
1754
1755void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1756 unsigned NumDesigs) {
1757 if (Designators)
1758 delete [] Designators;
1759
1760 Designators = new Designator[NumDesigs];
1761 NumDesignators = NumDesigs;
1762 for (unsigned I = 0; I != NumDesigs; ++I)
1763 Designators[I] = Desigs[I];
1764}
1765
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001766SourceRange DesignatedInitExpr::getSourceRange() const {
1767 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001768 Designator &First =
1769 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001770 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001771 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001772 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1773 else
1774 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1775 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001776 StartLoc =
1777 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001778 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1779}
1780
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001781Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1782 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1783 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1784 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001785 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1786 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1787}
1788
1789Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1790 assert(D.Kind == Designator::ArrayRangeDesignator &&
1791 "Requires array range designator");
1792 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1793 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001794 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1795 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1796}
1797
1798Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1799 assert(D.Kind == Designator::ArrayRangeDesignator &&
1800 "Requires array range designator");
1801 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1802 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001803 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1804 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1805}
1806
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001807/// \brief Replaces the designator at index @p Idx with the series
1808/// of designators in [First, Last).
1809void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1810 const Designator *First,
1811 const Designator *Last) {
1812 unsigned NumNewDesignators = Last - First;
1813 if (NumNewDesignators == 0) {
1814 std::copy_backward(Designators + Idx + 1,
1815 Designators + NumDesignators,
1816 Designators + Idx);
1817 --NumNewDesignators;
1818 return;
1819 } else if (NumNewDesignators == 1) {
1820 Designators[Idx] = *First;
1821 return;
1822 }
1823
1824 Designator *NewDesignators
1825 = new Designator[NumDesignators - 1 + NumNewDesignators];
1826 std::copy(Designators, Designators + Idx, NewDesignators);
1827 std::copy(First, Last, NewDesignators + Idx);
1828 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1829 NewDesignators + Idx + NumNewDesignators);
1830 delete [] Designators;
1831 Designators = NewDesignators;
1832 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1833}
1834
1835void DesignatedInitExpr::Destroy(ASTContext &C) {
1836 delete [] Designators;
1837 Expr::Destroy(C);
1838}
1839
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001840ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1841 return new (C) ImplicitValueInitExpr(getType());
1842}
1843
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001844//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001845// ExprIterator.
1846//===----------------------------------------------------------------------===//
1847
1848Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1849Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1850Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1851const Expr* ConstExprIterator::operator[](size_t idx) const {
1852 return cast<Expr>(I[idx]);
1853}
1854const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1855const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1856
1857//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001858// Child Iterators for iterating over subexpressions/substatements
1859//===----------------------------------------------------------------------===//
1860
1861// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001862Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1863Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001864
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001865// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001866Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1867Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001868
Steve Naroff6f786252008-06-02 23:03:37 +00001869// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001870Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1871Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001872
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001873// ObjCKVCRefExpr
1874Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1875Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1876
Douglas Gregord8606632008-11-04 14:56:14 +00001877// ObjCSuperExpr
1878Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1879Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1880
Chris Lattner69909292008-08-10 01:53:14 +00001881// PredefinedExpr
1882Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1883Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001884
1885// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001886Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1887Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001888
1889// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001890Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001891Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001892
1893// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001894Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1895Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001896
Chris Lattner1de66eb2007-08-26 03:42:43 +00001897// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001898Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1899Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001900
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001901// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001902Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1903Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001904
1905// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001906Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1907Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001908
1909// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001910Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1911Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001912
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001913// SizeOfAlignOfExpr
1914Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1915 // If this is of a type and the type is a VLA type (and not a typedef), the
1916 // size expression of the VLA needs to be treated as an executable expression.
1917 // Why isn't this weirdness documented better in StmtIterator?
1918 if (isArgumentType()) {
1919 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1920 getArgumentType().getTypePtr()))
1921 return child_iterator(T);
1922 return child_iterator();
1923 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001924 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001925}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001926Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1927 if (isArgumentType())
1928 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001929 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001930}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001931
1932// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001933Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001934 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001935}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001936Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001937 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001938}
1939
1940// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001941Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001942 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001943}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001944Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001945 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001946}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001947
1948// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001949Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1950Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001951
Nate Begemanaf6ed502008-04-18 23:10:10 +00001952// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001953Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1954Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001955
1956// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001957Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1958Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001959
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001960// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001961Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1962Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001963
1964// BinaryOperator
1965Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001966 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001967}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001968Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001969 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001970}
1971
1972// ConditionalOperator
1973Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001974 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001975}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001976Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001977 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001978}
1979
1980// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001981Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1982Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001983
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001984// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001985Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1986Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001987
1988// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001989Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1990 return child_iterator();
1991}
1992
1993Stmt::child_iterator TypesCompatibleExpr::child_end() {
1994 return child_iterator();
1995}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001996
1997// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001998Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1999Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002000
Douglas Gregorad4b3792008-11-29 04:51:27 +00002001// GNUNullExpr
2002Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2003Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2004
Eli Friedmand0e9d092008-05-14 19:38:39 +00002005// ShuffleVectorExpr
2006Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002007 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00002008}
2009Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002010 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00002011}
2012
Anders Carlsson36760332007-10-15 20:28:48 +00002013// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002014Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2015Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00002016
Anders Carlsson762b7c72007-08-31 04:56:16 +00002017// InitListExpr
2018Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002019 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002020}
2021Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002022 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002023}
2024
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002025// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002026Stmt::child_iterator DesignatedInitExpr::child_begin() {
2027 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2028 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002029 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2030}
2031Stmt::child_iterator DesignatedInitExpr::child_end() {
2032 return child_iterator(&*child_begin() + NumSubExprs);
2033}
2034
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002035// ImplicitValueInitExpr
2036Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2037 return child_iterator();
2038}
2039
2040Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2041 return child_iterator();
2042}
2043
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002044// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00002045Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00002046 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00002047}
2048Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00002049 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00002050}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002051
2052// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002053Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2054Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002055
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002056// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002057Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2058 return child_iterator();
2059}
2060Stmt::child_iterator ObjCSelectorExpr::child_end() {
2061 return child_iterator();
2062}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002063
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002064// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002065Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2066 return child_iterator();
2067}
2068Stmt::child_iterator ObjCProtocolExpr::child_end() {
2069 return child_iterator();
2070}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002071
Steve Naroffc39ca262007-09-18 23:55:05 +00002072// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002073Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002074 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002075}
2076Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002077 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002078}
2079
Steve Naroff52a81c02008-09-03 18:15:37 +00002080// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002081Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2082Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002083
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002084Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2085Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }