blob: fa5ce9f0478fce0f7058210146ef4c44fc5d4758 [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
Chris Lattnere0391b22008-06-07 22:13:43 +000031/// getValueAsApproximateDouble - This returns the value as an inaccurate
32/// double. Note that this may cause loss of precision, but is useful for
33/// debugging dumps, etc.
34double FloatingLiteral::getValueAsApproximateDouble() const {
35 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000036 bool ignored;
37 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
38 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000039 return V.convertToDouble();
40}
41
Chris Lattneraa491192009-02-18 06:40:38 +000042StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
43 unsigned ByteLength, bool Wide,
44 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000045 const SourceLocation *Loc,
46 unsigned NumStrs) {
Chris Lattneraa491192009-02-18 06:40:38 +000047 // Allocate enough space for the StringLiteral plus an array of locations for
48 // any concatenated string tokens.
49 void *Mem = C.Allocate(sizeof(StringLiteral)+
50 sizeof(SourceLocation)*(NumStrs-1),
51 llvm::alignof<StringLiteral>());
52 StringLiteral *SL = new (Mem) StringLiteral(Ty);
53
Chris Lattner4b009652007-07-25 00:24:17 +000054 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000055 char *AStrData = new (C, 1) char[ByteLength];
56 memcpy(AStrData, StrData, ByteLength);
57 SL->StrData = AStrData;
58 SL->ByteLength = ByteLength;
59 SL->IsWide = Wide;
60 SL->TokLocs[0] = Loc[0];
61 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000062
Chris Lattnerc3144742009-02-18 05:49:11 +000063 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000064 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
65 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000066}
67
Douglas Gregor596e0932009-04-15 16:35:07 +000068StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
69 void *Mem = C.Allocate(sizeof(StringLiteral)+
70 sizeof(SourceLocation)*(NumStrs-1),
71 llvm::alignof<StringLiteral>());
72 StringLiteral *SL = new (Mem) StringLiteral(QualType());
73 SL->StrData = 0;
74 SL->ByteLength = 0;
75 SL->NumConcatenated = NumStrs;
76 return SL;
77}
78
Douglas Gregor53e8e4b2009-08-07 06:08:38 +000079void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000080 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor53e8e4b2009-08-07 06:08:38 +000081 Expr::DoDestroy(C);
Chris Lattner4b009652007-07-25 00:24:17 +000082}
83
Douglas Gregor596e0932009-04-15 16:35:07 +000084void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
85 if (StrData)
86 C.Deallocate(const_cast<char*>(StrData));
87
88 char *AStrData = new (C, 1) char[Len];
89 memcpy(AStrData, Str, Len);
90 StrData = AStrData;
91 ByteLength = Len;
92}
93
Chris Lattner4b009652007-07-25 00:24:17 +000094/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
95/// corresponds to, e.g. "sizeof" or "[pre]++".
96const char *UnaryOperator::getOpcodeStr(Opcode Op) {
97 switch (Op) {
98 default: assert(0 && "Unknown unary operator");
99 case PostInc: return "++";
100 case PostDec: return "--";
101 case PreInc: return "++";
102 case PreDec: return "--";
103 case AddrOf: return "&";
104 case Deref: return "*";
105 case Plus: return "+";
106 case Minus: return "-";
107 case Not: return "~";
108 case LNot: return "!";
109 case Real: return "__real";
110 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000111 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000112 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000113 }
114}
115
Douglas Gregorc78182d2009-03-13 23:49:33 +0000116UnaryOperator::Opcode
117UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
118 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000119 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000120 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
121 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
122 case OO_Amp: return AddrOf;
123 case OO_Star: return Deref;
124 case OO_Plus: return Plus;
125 case OO_Minus: return Minus;
126 case OO_Tilde: return Not;
127 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000128 }
129}
130
131OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
132 switch (Opc) {
133 case PostInc: case PreInc: return OO_PlusPlus;
134 case PostDec: case PreDec: return OO_MinusMinus;
135 case AddrOf: return OO_Amp;
136 case Deref: return OO_Star;
137 case Plus: return OO_Plus;
138 case Minus: return OO_Minus;
139 case Not: return OO_Tilde;
140 case LNot: return OO_Exclaim;
141 default: return OO_None;
142 }
143}
144
145
Chris Lattner4b009652007-07-25 00:24:17 +0000146//===----------------------------------------------------------------------===//
147// Postfix Operators.
148//===----------------------------------------------------------------------===//
149
Ted Kremenek362abcd2009-02-09 20:51:47 +0000150CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000151 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000152 : Expr(SC, t,
153 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000154 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000155 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000156
157 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000158 SubExprs[FN] = fn;
159 for (unsigned i = 0; i != numargs; ++i)
160 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000161
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000162 RParenLoc = rparenloc;
163}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000164
Ted Kremenek362abcd2009-02-09 20:51:47 +0000165CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
166 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000167 : Expr(CallExprClass, t,
168 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000169 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000170 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000171
172 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000173 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000174 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000175 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000176
Chris Lattner4b009652007-07-25 00:24:17 +0000177 RParenLoc = rparenloc;
178}
179
Argiris Kirtzidisa06a9da2009-07-14 03:19:21 +0000180CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
181 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000182 SubExprs = new (C) Stmt*[1];
183}
184
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000185void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000186 DestroyChildren(C);
187 if (SubExprs) C.Deallocate(SubExprs);
188 this->~CallExpr();
189 C.Deallocate(this);
190}
191
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000192FunctionDecl *CallExpr::getDirectCallee() {
193 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattnereb346742009-07-17 15:46:27 +0000194 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000195 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000196
197 return 0;
198}
199
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000200/// setNumArgs - This changes the number of arguments present in this call.
201/// Any orphaned expressions are deleted by this, and any new operands are set
202/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000203void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000204 // No change, just return.
205 if (NumArgs == getNumArgs()) return;
206
207 // If shrinking # arguments, just delete the extras and forgot them.
208 if (NumArgs < getNumArgs()) {
209 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000210 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000211 this->NumArgs = NumArgs;
212 return;
213 }
214
215 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar1a6ead22009-07-28 06:29:46 +0000216 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000217 // Copy over args.
218 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
219 NewSubExprs[i] = SubExprs[i];
220 // Null out new args.
221 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
222 NewSubExprs[i] = 0;
223
Douglas Gregorb9f63642009-04-17 21:46:47 +0000224 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000225 SubExprs = NewSubExprs;
226 this->NumArgs = NumArgs;
227}
228
Chris Lattnerc24915f2008-10-06 05:00:53 +0000229/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
230/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000231unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000232 // All simple function calls (e.g. func()) are implicitly cast to pointer to
233 // function. As a result, we try and obtain the DeclRefExpr from the
234 // ImplicitCastExpr.
235 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
236 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000237 return 0;
238
Steve Naroff44aec4c2008-01-31 01:07:12 +0000239 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
240 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000241 return 0;
242
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000243 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
244 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000245 return 0;
246
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000247 if (!FDecl->getIdentifier())
248 return 0;
249
Douglas Gregorb5af7382009-02-14 18:57:46 +0000250 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000251}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000252
Anders Carlsson66070902009-05-26 04:57:27 +0000253QualType CallExpr::getCallReturnType() const {
254 QualType CalleeType = getCallee()->getType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000255 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000256 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000257 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000258 CalleeType = BPT->getPointeeType();
259
260 const FunctionType *FnType = CalleeType->getAsFunctionType();
261 return FnType->getResultType();
262}
Chris Lattnerc24915f2008-10-06 05:00:53 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
265/// corresponds to, e.g. "<<=".
266const char *BinaryOperator::getOpcodeStr(Opcode Op) {
267 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000268 case PtrMemD: return ".*";
269 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000270 case Mul: return "*";
271 case Div: return "/";
272 case Rem: return "%";
273 case Add: return "+";
274 case Sub: return "-";
275 case Shl: return "<<";
276 case Shr: return ">>";
277 case LT: return "<";
278 case GT: return ">";
279 case LE: return "<=";
280 case GE: return ">=";
281 case EQ: return "==";
282 case NE: return "!=";
283 case And: return "&";
284 case Xor: return "^";
285 case Or: return "|";
286 case LAnd: return "&&";
287 case LOr: return "||";
288 case Assign: return "=";
289 case MulAssign: return "*=";
290 case DivAssign: return "/=";
291 case RemAssign: return "%=";
292 case AddAssign: return "+=";
293 case SubAssign: return "-=";
294 case ShlAssign: return "<<=";
295 case ShrAssign: return ">>=";
296 case AndAssign: return "&=";
297 case XorAssign: return "^=";
298 case OrAssign: return "|=";
299 case Comma: return ",";
300 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000301
302 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000305BinaryOperator::Opcode
306BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
307 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000308 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000309 case OO_Plus: return Add;
310 case OO_Minus: return Sub;
311 case OO_Star: return Mul;
312 case OO_Slash: return Div;
313 case OO_Percent: return Rem;
314 case OO_Caret: return Xor;
315 case OO_Amp: return And;
316 case OO_Pipe: return Or;
317 case OO_Equal: return Assign;
318 case OO_Less: return LT;
319 case OO_Greater: return GT;
320 case OO_PlusEqual: return AddAssign;
321 case OO_MinusEqual: return SubAssign;
322 case OO_StarEqual: return MulAssign;
323 case OO_SlashEqual: return DivAssign;
324 case OO_PercentEqual: return RemAssign;
325 case OO_CaretEqual: return XorAssign;
326 case OO_AmpEqual: return AndAssign;
327 case OO_PipeEqual: return OrAssign;
328 case OO_LessLess: return Shl;
329 case OO_GreaterGreater: return Shr;
330 case OO_LessLessEqual: return ShlAssign;
331 case OO_GreaterGreaterEqual: return ShrAssign;
332 case OO_EqualEqual: return EQ;
333 case OO_ExclaimEqual: return NE;
334 case OO_LessEqual: return LE;
335 case OO_GreaterEqual: return GE;
336 case OO_AmpAmp: return LAnd;
337 case OO_PipePipe: return LOr;
338 case OO_Comma: return Comma;
339 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000340 }
341}
342
343OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
344 static const OverloadedOperatorKind OverOps[] = {
345 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
346 OO_Star, OO_Slash, OO_Percent,
347 OO_Plus, OO_Minus,
348 OO_LessLess, OO_GreaterGreater,
349 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
350 OO_EqualEqual, OO_ExclaimEqual,
351 OO_Amp,
352 OO_Caret,
353 OO_Pipe,
354 OO_AmpAmp,
355 OO_PipePipe,
356 OO_Equal, OO_StarEqual,
357 OO_SlashEqual, OO_PercentEqual,
358 OO_PlusEqual, OO_MinusEqual,
359 OO_LessLessEqual, OO_GreaterGreaterEqual,
360 OO_AmpEqual, OO_CaretEqual,
361 OO_PipeEqual,
362 OO_Comma
363 };
364 return OverOps[Opc];
365}
366
Anders Carlsson762b7c72007-08-31 04:56:16 +0000367InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000368 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000369 SourceLocation rbraceloc)
Douglas Gregor3a7a06e2009-05-21 23:17:49 +0000370 : Expr(InitListExprClass, QualType(),
371 hasAnyTypeDependentArguments(initExprs, numInits),
372 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor82462762009-01-29 16:53:55 +0000373 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000374 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000375
376 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000377}
Chris Lattner4b009652007-07-25 00:24:17 +0000378
Douglas Gregoree0792c2009-03-20 23:58:33 +0000379void InitListExpr::reserveInits(unsigned NumInits) {
380 if (NumInits > InitExprs.size())
381 InitExprs.reserve(NumInits);
382}
383
Douglas Gregorf603b472009-01-28 21:54:33 +0000384void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000385 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000386 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000387 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000388 InitExprs.resize(NumInits, 0);
389}
390
391Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
392 if (Init >= InitExprs.size()) {
393 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
394 InitExprs.back() = expr;
395 return 0;
396 }
397
398 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
399 InitExprs[Init] = expr;
400 return Result;
401}
402
Steve Naroff6f373332008-09-04 15:31:07 +0000403/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000404///
405const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000406 return getType()->getAs<BlockPointerType>()->
Steve Naroff52a81c02008-09-03 18:15:37 +0000407 getPointeeType()->getAsFunctionType();
408}
409
Steve Naroff9ac456d2008-10-08 17:01:13 +0000410SourceLocation BlockExpr::getCaretLocation() const {
411 return TheBlock->getCaretLocation();
412}
Douglas Gregore3241e92009-04-18 00:02:19 +0000413const Stmt *BlockExpr::getBody() const {
414 return TheBlock->getBody();
415}
416Stmt *BlockExpr::getBody() {
417 return TheBlock->getBody();
418}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000419
420
Chris Lattner4b009652007-07-25 00:24:17 +0000421//===----------------------------------------------------------------------===//
422// Generic Expression Routines
423//===----------------------------------------------------------------------===//
424
Chris Lattnerd2c66552009-02-14 07:37:35 +0000425/// isUnusedResultAWarning - Return true if this immediate expression should
426/// be warned about if the result is unused. If so, fill in Loc and Ranges
427/// with location to warn on and the source range[s] to report with the
428/// warning.
429bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000430 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000431 // Don't warn if the expr is type dependent. The type could end up
432 // instantiating to void.
433 if (isTypeDependent())
434 return false;
435
Chris Lattner4b009652007-07-25 00:24:17 +0000436 switch (getStmtClass()) {
437 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000438 Loc = getExprLoc();
439 R1 = getSourceRange();
440 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000441 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000442 return cast<ParenExpr>(this)->getSubExpr()->
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000443 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000444 case UnaryOperatorClass: {
445 const UnaryOperator *UO = cast<UnaryOperator>(this);
446
447 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000448 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000449 case UnaryOperator::PostInc:
450 case UnaryOperator::PostDec:
451 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000452 case UnaryOperator::PreDec: // ++/--
453 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000454 case UnaryOperator::Deref:
455 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000456 if (getType().isVolatileQualified())
457 return false;
458 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000459 case UnaryOperator::Real:
460 case UnaryOperator::Imag:
461 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000462 if (UO->getSubExpr()->getType().isVolatileQualified())
463 return false;
464 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000465 case UnaryOperator::Extension:
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000466 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000467 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000468 Loc = UO->getOperatorLoc();
469 R1 = UO->getSubExpr()->getSourceRange();
470 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000471 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000472 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000473 const BinaryOperator *BO = cast<BinaryOperator>(this);
474 // Consider comma to have side effects if the LHS or RHS does.
475 if (BO->getOpcode() == BinaryOperator::Comma)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000476 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
477 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000478
Chris Lattnerd2c66552009-02-14 07:37:35 +0000479 if (BO->isAssignmentOp())
480 return false;
481 Loc = BO->getOperatorLoc();
482 R1 = BO->getLHS()->getSourceRange();
483 R2 = BO->getRHS()->getSourceRange();
484 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000485 }
Chris Lattner06078d22007-08-25 02:00:02 +0000486 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000487 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000488
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000489 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000490 // The condition must be evaluated, but if either the LHS or RHS is a
491 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000492 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000493 if (Exp->getLHS() &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000494 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000495 return true;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000496 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000497 }
498
Chris Lattner4b009652007-07-25 00:24:17 +0000499 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000500 // If the base pointer or element is to a volatile pointer/field, accessing
501 // it is a side effect.
502 if (getType().isVolatileQualified())
503 return false;
504 Loc = cast<MemberExpr>(this)->getMemberLoc();
505 R1 = SourceRange(Loc, Loc);
506 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
507 return true;
508
Chris Lattner4b009652007-07-25 00:24:17 +0000509 case ArraySubscriptExprClass:
510 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000511 // it is a side effect.
512 if (getType().isVolatileQualified())
513 return false;
514 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
515 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
516 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
517 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000518
Chris Lattner4b009652007-07-25 00:24:17 +0000519 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000520 case CXXOperatorCallExprClass:
521 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000522 // If this is a direct call, get the callee.
523 const CallExpr *CE = cast<CallExpr>(this);
524 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
525 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
526 // If the callee has attribute pure, const, or warn_unused_result, warn
527 // about it. void foo() { strlen("bar"); } should warn.
528 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000529 if (FD->getAttr<WarnUnusedResultAttr>() ||
530 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000531 Loc = CE->getCallee()->getLocStart();
532 R1 = CE->getCallee()->getSourceRange();
533
534 if (unsigned NumArgs = CE->getNumArgs())
535 R2 = SourceRange(CE->getArg(0)->getLocStart(),
536 CE->getArg(NumArgs-1)->getLocEnd());
537 return true;
538 }
539 }
540 return false;
541 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000542 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000543 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000544 case StmtExprClass: {
545 // Statement exprs don't logically have side effects themselves, but are
546 // sometimes used in macros in ways that give them a type that is unused.
547 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
548 // however, if the result of the stmt expr is dead, we don't want to emit a
549 // warning.
550 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
551 if (!CS->body_empty())
552 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000553 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000554
555 Loc = cast<StmtExpr>(this)->getLParenLoc();
556 R1 = getSourceRange();
557 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000558 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000559 case CStyleCastExprClass:
Chris Lattner34796b62009-07-28 18:25:28 +0000560 // If this is an explicit cast to void, allow it. People do this when they
561 // think they know what they're doing :).
Chris Lattnerd2c66552009-02-14 07:37:35 +0000562 if (getType()->isVoidType())
Chris Lattner34796b62009-07-28 18:25:28 +0000563 return false;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000564 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
565 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
566 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000567 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000568 // If this is a cast to void, check the operand. Otherwise, the result of
569 // the cast is unused.
570 if (getType()->isVoidType())
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000571 return cast<CastExpr>(this)->getSubExpr()
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000572 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000573 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
574 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
575 return true;
576
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000577 case ImplicitCastExprClass:
578 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000579 return cast<ImplicitCastExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000580 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000581
Chris Lattner3e254fb2008-04-08 04:40:51 +0000582 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000583 return cast<CXXDefaultArgExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000584 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000585
586 case CXXNewExprClass:
587 // FIXME: In theory, there might be new expressions that don't have side
588 // effects (e.g. a placement new with an uninitialized POD).
589 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000590 return false;
Anders Carlsson16497742009-08-16 04:11:06 +0000591 case CXXBindTemporaryExprClass:
592 return cast<CXXBindTemporaryExpr>(this)
593 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson18ca4772009-05-17 21:11:30 +0000594 case CXXExprWithTemporariesClass:
595 return cast<CXXExprWithTemporaries>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000596 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000597 }
Chris Lattner4b009652007-07-25 00:24:17 +0000598}
599
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000600/// DeclCanBeLvalue - Determine whether the given declaration can be
601/// an lvalue. This is a helper routine for isLvalue.
602static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000603 // C++ [temp.param]p6:
604 // A non-type non-reference template-parameter is not an lvalue.
605 if (const NonTypeTemplateParmDecl *NTTParm
606 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
607 return NTTParm->getType()->isReferenceType();
608
Douglas Gregor8acb7272008-12-11 16:49:14 +0000609 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000610 // C++ 3.10p2: An lvalue refers to an object or function.
611 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor62f78762009-07-08 20:55:45 +0000612 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
613 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000614}
615
Chris Lattner4b009652007-07-25 00:24:17 +0000616/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
617/// incomplete type other than void. Nonarray expressions that can be lvalues:
618/// - name, where name must be a variable
619/// - e[i]
620/// - (e), where e must be an lvalue
621/// - e.name, where e must be an lvalue
622/// - e->name
623/// - *e, the type of e cannot be a function type
624/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000625/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000626/// - reference type [C++ [expr]]
627///
Chris Lattner25168a52008-07-26 21:30:36 +0000628Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000629 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
630
631 isLvalueResult Res = isLvalueInternal(Ctx);
632 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
633 return Res;
634
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000635 // first, check the type (C99 6.3.2.1). Expressions with function
636 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor62f78762009-07-08 20:55:45 +0000637 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Chris Lattner4b009652007-07-25 00:24:17 +0000638 return LV_NotObjectType;
639
Steve Naroffec7736d2008-02-10 01:39:04 +0000640 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000641 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000642 return LV_IncompleteVoidType;
643
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000644 return LV_Valid;
645}
Chris Lattner4b009652007-07-25 00:24:17 +0000646
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000647// Check whether the expression can be sanely treated like an l-value
648Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000649 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000650 case StringLiteralClass: // C99 6.5.1p4
651 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000652 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000653 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
654 // For vectors, make sure base is an lvalue (i.e. not a function call).
655 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000656 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000657 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000658 case DeclRefExprClass:
659 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000660 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
661 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000662 return LV_Valid;
663 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000664 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000665 case BlockDeclRefExprClass: {
666 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000667 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000668 return LV_Valid;
669 break;
670 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000671 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000672 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000673 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
674 NamedDecl *Member = m->getMemberDecl();
675 // C++ [expr.ref]p4:
676 // If E2 is declared to have type "reference to T", then E1.E2
677 // is an lvalue.
678 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
679 if (Value->getType()->isReferenceType())
680 return LV_Valid;
681
682 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000683 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000684 return LV_Valid;
685
686 // -- If E2 is a non-static data member [...]. If E1 is an
687 // lvalue, then E1.E2 is an lvalue.
688 if (isa<FieldDecl>(Member))
689 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
690
691 // -- If it refers to a static member function [...], then
692 // E1.E2 is an lvalue.
693 // -- Otherwise, if E1.E2 refers to a non-static member
694 // function [...], then E1.E2 is not an lvalue.
695 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
696 return Method->isStatic()? LV_Valid : LV_MemberFunction;
697
698 // -- If E2 is a member enumerator [...], the expression E1.E2
699 // is not an lvalue.
700 if (isa<EnumConstantDecl>(Member))
701 return LV_InvalidExpression;
702
703 // Not an lvalue.
704 return LV_InvalidExpression;
705 }
706
707 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000708 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000709 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000710 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000711 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000712 return LV_Valid; // C99 6.5.3p4
713
714 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000715 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
716 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000717 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000718
719 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
720 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
721 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
722 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000723 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000724 case ImplicitCastExprClass:
725 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
726 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000727 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000728 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000729 case BinaryOperatorClass:
730 case CompoundAssignOperatorClass: {
731 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000732
733 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
734 BinOp->getOpcode() == BinaryOperator::Comma)
735 return BinOp->getRHS()->isLvalue(Ctx);
736
Sebastian Redl95216a62009-02-07 00:15:38 +0000737 // C++ [expr.mptr.oper]p6
738 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
739 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
740 !BinOp->getType()->isFunctionType())
741 return BinOp->getLHS()->isLvalue(Ctx);
742
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000743 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000744 return LV_InvalidExpression;
745
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000746 if (Ctx.getLangOptions().CPlusPlus)
747 // C++ [expr.ass]p1:
748 // The result of an assignment operation [...] is an lvalue.
749 return LV_Valid;
750
751
752 // C99 6.5.16:
753 // An assignment expression [...] is not an lvalue.
754 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000755 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000756 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000757 case CXXOperatorCallExprClass:
758 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000759 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000760 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000761 // is an lvalue reference.
Anders Carlsson66070902009-05-26 04:57:27 +0000762 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
763 if (ReturnType->isLValueReferenceType())
764 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000765
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000766 break;
767 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000768 case CompoundLiteralExprClass: // C99 6.5.2.5p5
769 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000770 case ChooseExprClass:
771 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000772 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000773 case ExtVectorElementExprClass:
774 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000775 return LV_DuplicateVectorComponents;
776 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000777 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
778 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000779 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
780 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000781 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000782 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000783 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000784 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000785 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000786 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000787 case CXXConditionDeclExprClass:
788 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000789 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000790 case CXXFunctionalCastExprClass:
791 case CXXStaticCastExprClass:
792 case CXXDynamicCastExprClass:
793 case CXXReinterpretCastExprClass:
794 case CXXConstCastExprClass:
795 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000796 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000797 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
798 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000799 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
800 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000801 return LV_Valid;
802 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000803 case CXXTypeidExprClass:
804 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
805 return LV_Valid;
Anders Carlssonb8ff3402009-08-16 03:42:12 +0000806 case CXXBindTemporaryExprClass:
807 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
808 isLvalueInternal(Ctx);
Sebastian Redld3169132009-04-17 16:30:52 +0000809 case ConditionalOperatorClass: {
810 // Complicated handling is only for C++.
811 if (!Ctx.getLangOptions().CPlusPlus)
812 return LV_InvalidExpression;
813
814 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
815 // everywhere there's an object converted to an rvalue. Also, any other
816 // casts should be wrapped by ImplicitCastExprs. There's just the special
817 // case involving throws to work out.
818 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000819 Expr *True = Cond->getTrueExpr();
820 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000821 // C++0x 5.16p2
822 // If either the second or the third operand has type (cv) void, [...]
823 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000824 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000825 return LV_InvalidExpression;
826
827 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000828 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000829 return LV_InvalidExpression;
830
831 // That's it.
832 return LV_Valid;
833 }
834
Chris Lattner4b009652007-07-25 00:24:17 +0000835 default:
836 break;
837 }
838 return LV_InvalidExpression;
839}
840
841/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
842/// does not have an incomplete type, does not have a const-qualified type, and
843/// if it is a structure or union, does not have any member (including,
844/// recursively, any member or element of all contained aggregates or unions)
845/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000846Expr::isModifiableLvalueResult
847Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000848 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000849
850 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000851 case LV_Valid:
852 // C++ 3.10p11: Functions cannot be modified, but pointers to
853 // functions can be modifiable.
854 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
855 return MLV_NotObjectType;
856 break;
857
Chris Lattner4b009652007-07-25 00:24:17 +0000858 case LV_NotObjectType: return MLV_NotObjectType;
859 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000860 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000861 case LV_InvalidExpression:
862 // If the top level is a C-style cast, and the subexpression is a valid
863 // lvalue, then this is probably a use of the old-school "cast as lvalue"
864 // GCC extension. We don't support it, but we want to produce good
865 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000866 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
867 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
868 if (Loc)
869 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000870 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000871 }
872 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000873 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000874 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000875 }
Eli Friedman91571da2009-03-22 23:26:56 +0000876
877 // The following is illegal:
878 // void takeclosure(void (^C)(void));
879 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
880 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000881 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000882 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
883 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
884 return MLV_NotBlockQualified;
885 }
886
Chris Lattnera1923f62008-08-04 07:31:14 +0000887 QualType CT = Ctx.getCanonicalType(getType());
888
889 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000890 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000891 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000892 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000893 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000894 return MLV_IncompleteType;
895
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000896 if (const RecordType *r = CT->getAs<RecordType>()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000897 if (r->hasConstFields())
898 return MLV_ConstQualified;
899 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000900
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000901 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000902 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000903 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
904 if (KVCExpr->getSetterMethod() == 0)
905 return MLV_NoSetterProperty;
906 }
Chris Lattner4b009652007-07-25 00:24:17 +0000907 return MLV_Valid;
908}
909
Ted Kremenek5778d622008-02-27 18:39:48 +0000910/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000911/// duration. This means that the address of this expression is a link-time
912/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000913bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000914 switch (getStmtClass()) {
915 default:
916 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000917 case BlockExprClass:
918 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000919 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000920 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000921 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000922 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000923 case CompoundLiteralExprClass:
924 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000925 case DeclRefExprClass:
926 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000927 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
928 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000929 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000930 if (isa<FunctionDecl>(D))
931 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000932 return false;
933 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000934 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000935 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000936 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000937 }
Chris Lattner743ec372007-11-27 21:35:27 +0000938 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000939 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000940 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000941 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000942 case CXXDefaultArgExprClass:
943 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000944 }
945}
946
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000947/// isOBJCGCCandidate - Check if an expression is objc gc'able.
948///
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000949bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000950 switch (getStmtClass()) {
951 default:
952 return false;
953 case ObjCIvarRefExprClass:
954 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000955 case Expr::UnaryOperatorClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000956 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000957 case ParenExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000958 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000959 case ImplicitCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000960 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000961 case CStyleCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000962 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000963 case DeclRefExprClass:
964 case QualifiedDeclRefExprClass: {
965 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000966 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
967 if (VD->hasGlobalStorage())
968 return true;
969 QualType T = VD->getType();
970 // dereferencing to an object pointer is always a gc'able candidate
971 if (T->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000972 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000973 return true;
974
975 }
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000976 return false;
977 }
978 case MemberExprClass: {
979 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000980 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000981 }
982 case ArraySubscriptExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000983 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000984 }
985}
Ted Kremenek87e30c52008-01-17 16:57:34 +0000986Expr* Expr::IgnoreParens() {
987 Expr* E = this;
988 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
989 E = P->getSubExpr();
990
991 return E;
992}
993
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000994/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
995/// or CastExprs or ImplicitCastExprs, returning their operand.
996Expr *Expr::IgnoreParenCasts() {
997 Expr *E = this;
998 while (true) {
999 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1000 E = P->getSubExpr();
1001 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1002 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001003 else
1004 return E;
1005 }
1006}
1007
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001008/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1009/// value (including ptr->int casts of the same size). Strip off any
1010/// ParenExpr or CastExprs, returning their operand.
1011Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1012 Expr *E = this;
1013 while (true) {
1014 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1015 E = P->getSubExpr();
1016 continue;
1017 }
1018
1019 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1020 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1021 // ptr<->int casts of the same width. We also ignore all identify casts.
1022 Expr *SE = P->getSubExpr();
1023
1024 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1025 E = SE;
1026 continue;
1027 }
1028
1029 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1030 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1031 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1032 E = SE;
1033 continue;
1034 }
1035 }
1036
1037 return E;
1038 }
1039}
1040
1041
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001042/// hasAnyTypeDependentArguments - Determines if any of the expressions
1043/// in Exprs is type-dependent.
1044bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1045 for (unsigned I = 0; I < NumExprs; ++I)
1046 if (Exprs[I]->isTypeDependent())
1047 return true;
1048
1049 return false;
1050}
1051
1052/// hasAnyValueDependentArguments - Determines if any of the expressions
1053/// in Exprs is value-dependent.
1054bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1055 for (unsigned I = 0; I < NumExprs; ++I)
1056 if (Exprs[I]->isValueDependent())
1057 return true;
1058
1059 return false;
1060}
1061
Eli Friedmandee41122009-01-25 02:32:41 +00001062bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001063 // This function is attempting whether an expression is an initializer
1064 // which can be evaluated at compile-time. isEvaluatable handles most
1065 // of the cases, but it can't deal with some initializer-specific
1066 // expressions, and it can't deal with aggregates; we deal with those here,
1067 // and fall back to isEvaluatable for the other cases.
1068
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001069 // FIXME: This function assumes the variable being assigned to
1070 // isn't a reference type!
1071
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001072 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001073 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001074 case StringLiteralClass:
Steve Naroff329ec222009-07-10 23:34:53 +00001075 case ObjCStringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001076 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001077 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001078 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001079 // This handles gcc's extension that allows global initializers like
1080 // "struct x {int x;} x = (struct x) {};".
1081 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001082 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001083 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001084 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001085 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001086 // FIXME: This doesn't deal with fields with reference types correctly.
1087 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1088 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001089 const InitListExpr *Exp = cast<InitListExpr>(this);
1090 unsigned numInits = Exp->getNumInits();
1091 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001092 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001093 return false;
1094 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001095 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001096 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001097 case ImplicitValueInitExprClass:
1098 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001099 case ParenExprClass: {
1100 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1101 }
1102 case UnaryOperatorClass: {
1103 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1104 if (Exp->getOpcode() == UnaryOperator::Extension)
1105 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1106 break;
1107 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001108 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001109 case CStyleCastExprClass:
1110 // Handle casts with a destination that's a struct or union; this
1111 // deals with both the gcc no-op struct cast extension and the
1112 // cast-to-union extension.
1113 if (getType()->isRecordType())
1114 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1115 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001116 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001117 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001118}
1119
Chris Lattner4b009652007-07-25 00:24:17 +00001120/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001121/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001122
1123/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1124/// comma, etc
1125///
Chris Lattner4b009652007-07-25 00:24:17 +00001126/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1127/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1128/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001129
Eli Friedman7beeda62009-02-26 09:29:13 +00001130// CheckICE - This function does the fundamental ICE checking: the returned
1131// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1132// Note that to reduce code duplication, this helper does no evaluation
1133// itself; the caller checks whether the expression is evaluatable, and
1134// in the rare cases where CheckICE actually cares about the evaluated
1135// value, it calls into Evalute.
1136//
1137// Meanings of Val:
1138// 0: This expression is an ICE if it can be evaluated by Evaluate.
1139// 1: This expression is not an ICE, but if it isn't evaluated, it's
1140// a legal subexpression for an ICE. This return value is used to handle
1141// the comma operator in C99 mode.
1142// 2: This expression is not an ICE, and is not a legal subexpression for one.
1143
1144struct ICEDiag {
1145 unsigned Val;
1146 SourceLocation Loc;
1147
1148 public:
1149 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1150 ICEDiag() : Val(0) {}
1151};
1152
1153ICEDiag NoDiag() { return ICEDiag(); }
1154
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001155static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1156 Expr::EvalResult EVResult;
1157 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1158 !EVResult.Val.isInt()) {
1159 return ICEDiag(2, E->getLocStart());
1160 }
1161 return NoDiag();
1162}
1163
Eli Friedman7beeda62009-02-26 09:29:13 +00001164static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001165 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001166 if (!E->getType()->isIntegralType()) {
1167 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001168 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001169
1170 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001171 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001172 return ICEDiag(2, E->getLocStart());
1173 case Expr::ParenExprClass:
1174 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1175 case Expr::IntegerLiteralClass:
1176 case Expr::CharacterLiteralClass:
1177 case Expr::CXXBoolLiteralExprClass:
1178 case Expr::CXXZeroInitValueExprClass:
1179 case Expr::TypesCompatibleExprClass:
1180 case Expr::UnaryTypeTraitExprClass:
1181 return NoDiag();
1182 case Expr::CallExprClass:
1183 case Expr::CXXOperatorCallExprClass: {
1184 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001185 if (CE->isBuiltinCall(Ctx))
1186 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001187 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001188 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001189 case Expr::DeclRefExprClass:
1190 case Expr::QualifiedDeclRefExprClass:
1191 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1192 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001193 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001194 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001195 // C++ 7.1.5.1p2
1196 // A variable of non-volatile const-qualified integral or enumeration
1197 // type initialized by an ICE can be used in ICEs.
1198 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001199 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor4833ff02009-05-26 18:54:04 +00001200 if (Dcl->isInitKnownICE()) {
1201 // We have already checked whether this subexpression is an
1202 // integral constant expression.
1203 if (Dcl->isInitICE())
1204 return NoDiag();
1205 else
1206 return ICEDiag(2, E->getLocStart());
1207 }
1208
1209 if (const Expr *Init = Dcl->getInit()) {
1210 ICEDiag Result = CheckICE(Init, Ctx);
1211 // Cache the result of the ICE test.
1212 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1213 return Result;
1214 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001215 }
1216 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001217 return ICEDiag(2, E->getLocStart());
1218 case Expr::UnaryOperatorClass: {
1219 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001220 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001221 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001222 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001223 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001224 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001225 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001226 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001227 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001228 case UnaryOperator::Real:
1229 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001230 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001231 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001232 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1233 // Evaluate matches the proposed gcc behavior for cases like
1234 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1235 // compliance: we should warn earlier for offsetof expressions with
1236 // array subscripts that aren't ICEs, and if the array subscripts
1237 // are ICEs, the value of the offsetof must be an integer constant.
1238 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001239 }
Chris Lattner4b009652007-07-25 00:24:17 +00001240 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001241 case Expr::SizeOfAlignOfExprClass: {
1242 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1243 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1244 return ICEDiag(2, E->getLocStart());
1245 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001246 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001247 case Expr::BinaryOperatorClass: {
1248 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001249 switch (Exp->getOpcode()) {
1250 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001251 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001252 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001253 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001254 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 case BinaryOperator::Add:
1256 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001257 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001258 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001259 case BinaryOperator::LT:
1260 case BinaryOperator::GT:
1261 case BinaryOperator::LE:
1262 case BinaryOperator::GE:
1263 case BinaryOperator::EQ:
1264 case BinaryOperator::NE:
1265 case BinaryOperator::And:
1266 case BinaryOperator::Xor:
1267 case BinaryOperator::Or:
1268 case BinaryOperator::Comma: {
1269 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1270 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001271 if (Exp->getOpcode() == BinaryOperator::Div ||
1272 Exp->getOpcode() == BinaryOperator::Rem) {
1273 // Evaluate gives an error for undefined Div/Rem, so make sure
1274 // we don't evaluate one.
1275 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1276 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1277 if (REval == 0)
1278 return ICEDiag(1, E->getLocStart());
1279 if (REval.isSigned() && REval.isAllOnesValue()) {
1280 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1281 if (LEval.isMinSignedValue())
1282 return ICEDiag(1, E->getLocStart());
1283 }
1284 }
1285 }
1286 if (Exp->getOpcode() == BinaryOperator::Comma) {
1287 if (Ctx.getLangOptions().C99) {
1288 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1289 // if it isn't evaluated.
1290 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1291 return ICEDiag(1, E->getLocStart());
1292 } else {
1293 // In both C89 and C++, commas in ICEs are illegal.
1294 return ICEDiag(2, E->getLocStart());
1295 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001296 }
1297 if (LHSResult.Val >= RHSResult.Val)
1298 return LHSResult;
1299 return RHSResult;
1300 }
Chris Lattner4b009652007-07-25 00:24:17 +00001301 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001302 case BinaryOperator::LOr: {
1303 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1304 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1305 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1306 // Rare case where the RHS has a comma "side-effect"; we need
1307 // to actually check the condition to see whether the side
1308 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001309 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001310 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001311 return RHSResult;
1312 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001313 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001314
Eli Friedman7beeda62009-02-26 09:29:13 +00001315 if (LHSResult.Val >= RHSResult.Val)
1316 return LHSResult;
1317 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001318 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001319 }
Chris Lattner4b009652007-07-25 00:24:17 +00001320 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001321 case Expr::ImplicitCastExprClass:
1322 case Expr::CStyleCastExprClass:
1323 case Expr::CXXFunctionalCastExprClass: {
1324 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1325 if (SubExpr->getType()->isIntegralType())
1326 return CheckICE(SubExpr, Ctx);
1327 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1328 return NoDiag();
1329 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001330 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001331 case Expr::ConditionalOperatorClass: {
1332 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001333 // If the condition (ignoring parens) is a __builtin_constant_p call,
1334 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001335 // expression, and it is fully evaluated. This is an important GNU
1336 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001337 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001338 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001339 Expr::EvalResult EVResult;
1340 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1341 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001342 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001343 }
1344 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001345 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001346 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1347 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1348 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1349 if (CondResult.Val == 2)
1350 return CondResult;
1351 if (TrueResult.Val == 2)
1352 return TrueResult;
1353 if (FalseResult.Val == 2)
1354 return FalseResult;
1355 if (CondResult.Val == 1)
1356 return CondResult;
1357 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1358 return NoDiag();
1359 // Rare case where the diagnostics depend on which side is evaluated
1360 // Note that if we get here, CondResult is 0, and at least one of
1361 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001362 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001363 return FalseResult;
1364 }
1365 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001366 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001367 case Expr::CXXDefaultArgExprClass:
1368 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001369 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001370 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001371 }
Chris Lattner4b009652007-07-25 00:24:17 +00001372 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001373}
Chris Lattner4b009652007-07-25 00:24:17 +00001374
Eli Friedman7beeda62009-02-26 09:29:13 +00001375bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1376 SourceLocation *Loc, bool isEvaluated) const {
1377 ICEDiag d = CheckICE(this, Ctx);
1378 if (d.Val != 0) {
1379 if (Loc) *Loc = d.Loc;
1380 return false;
1381 }
1382 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001383 if (!Evaluate(EvalResult, Ctx))
1384 assert(0 && "ICE cannot be evaluated!");
1385 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1386 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001387 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001388 return true;
1389}
1390
Chris Lattner4b009652007-07-25 00:24:17 +00001391/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1392/// integer constant expression with the value zero, or if this is one that is
1393/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001394bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1395{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001396 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001397 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001398 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001399 // Check that it is a cast to void*.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001400 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001401 QualType Pointee = PT->getPointeeType();
1402 if (Pointee.getCVRQualifiers() == 0 &&
1403 Pointee->isVoidType() && // to void*
1404 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001405 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001406 }
Chris Lattner4b009652007-07-25 00:24:17 +00001407 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001408 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1409 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001410 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001411 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1412 // Accept ((void*)0) as a null pointer constant, as many other
1413 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001414 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001415 } else if (const CXXDefaultArgExpr *DefaultArg
1416 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001417 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001418 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001419 } else if (isa<GNUNullExpr>(this)) {
1420 // The GNU __null extension is always a null pointer constant.
1421 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001422 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001423
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001424 // C++0x nullptr_t is always a null pointer constant.
1425 if (getType()->isNullPtrType())
1426 return true;
1427
Steve Naroffa2e53222008-01-14 16:10:57 +00001428 // This expression must be an integer type.
1429 if (!getType()->isIntegerType())
1430 return false;
1431
Chris Lattner4b009652007-07-25 00:24:17 +00001432 // If we have an integer constant expression, we need to *evaluate* it and
1433 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001434 llvm::APSInt Result;
1435 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001436}
Steve Naroffc11705f2007-07-28 23:10:27 +00001437
Douglas Gregor531434b2009-05-02 02:18:30 +00001438FieldDecl *Expr::getBitField() {
Douglas Gregor34909522009-07-06 15:38:40 +00001439 Expr *E = this->IgnoreParens();
Douglas Gregor531434b2009-05-02 02:18:30 +00001440
Douglas Gregor81c29152008-10-29 00:13:59 +00001441 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001442 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001443 if (Field->isBitField())
1444 return Field;
1445
1446 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1447 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1448 return BinOp->getLHS()->getBitField();
1449
1450 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001451}
1452
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001453/// isArrow - Return true if the base expression is a pointer to vector,
1454/// return false if the base expression is a vector.
1455bool ExtVectorElementExpr::isArrow() const {
1456 return getBase()->getType()->isPointerType();
1457}
1458
Nate Begemanaf6ed502008-04-18 23:10:10 +00001459unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001460 if (const VectorType *VT = getType()->getAsVectorType())
1461 return VT->getNumElements();
1462 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001463}
1464
Nate Begemanc8e51f82008-05-09 06:41:27 +00001465/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001466bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001467 const char *compStr = Accessor->getName();
1468 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001469
1470 // Halving swizzles do not contain duplicate elements.
1471 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1472 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1473 return false;
1474
1475 // Advance past s-char prefix on hex swizzles.
Nate Begemane2ed6f72009-06-25 21:06:09 +00001476 if (*compStr == 's' || *compStr == 'S') {
Nate Begemana8e117c2009-01-18 02:01:21 +00001477 compStr++;
1478 length--;
1479 }
Steve Naroffba67f692007-07-30 03:29:09 +00001480
Chris Lattner58d3fa52008-11-19 07:55:04 +00001481 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001482 const char *s = compStr+i;
1483 for (const char c = *s++; *s; s++)
1484 if (c == *s)
1485 return true;
1486 }
1487 return false;
1488}
Chris Lattner42158e72007-08-02 23:36:59 +00001489
Nate Begemanc8e51f82008-05-09 06:41:27 +00001490/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001491void ExtVectorElementExpr::getEncodedElementAccess(
1492 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001493 const char *compStr = Accessor->getName();
Nate Begemane2ed6f72009-06-25 21:06:09 +00001494 if (*compStr == 's' || *compStr == 'S')
Nate Begeman1486b502009-01-18 01:47:54 +00001495 compStr++;
1496
1497 bool isHi = !strcmp(compStr, "hi");
1498 bool isLo = !strcmp(compStr, "lo");
1499 bool isEven = !strcmp(compStr, "even");
1500 bool isOdd = !strcmp(compStr, "odd");
1501
Nate Begemanc8e51f82008-05-09 06:41:27 +00001502 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1503 uint64_t Index;
1504
1505 if (isHi)
1506 Index = e + i;
1507 else if (isLo)
1508 Index = i;
1509 else if (isEven)
1510 Index = 2 * i;
1511 else if (isOdd)
1512 Index = 2 * i + 1;
1513 else
1514 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001515
Nate Begemana1ae7442008-05-13 21:03:02 +00001516 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001517 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001518}
1519
Steve Naroff4ed9d662007-09-27 14:38:14 +00001520// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001521ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001522 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001523 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001524 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001525 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001526 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001527 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001528 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001529 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001530 if (NumArgs) {
1531 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001532 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1533 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001534 LBracloc = LBrac;
1535 RBracloc = RBrac;
1536}
1537
Steve Naroff4ed9d662007-09-27 14:38:14 +00001538// constructor for class messages.
1539// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001540ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001541 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001542 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001543 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001544 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001545 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001546 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001547 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001548 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001549 if (NumArgs) {
1550 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001551 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1552 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001553 LBracloc = LBrac;
1554 RBracloc = RBrac;
1555}
1556
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001557// constructor for class messages.
1558ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1559 QualType retType, ObjCMethodDecl *mproto,
1560 SourceLocation LBrac, SourceLocation RBrac,
1561 Expr **ArgExprs, unsigned nargs)
1562: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1563MethodProto(mproto) {
1564 NumArgs = nargs;
1565 SubExprs = new Stmt*[NumArgs+1];
1566 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1567 if (NumArgs) {
1568 for (unsigned i = 0; i != NumArgs; ++i)
1569 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1570 }
1571 LBracloc = LBrac;
1572 RBracloc = RBrac;
1573}
1574
1575ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1576 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1577 switch (x & Flags) {
1578 default:
1579 assert(false && "Invalid ObjCMessageExpr.");
1580 case IsInstMeth:
1581 return ClassInfo(0, 0);
1582 case IsClsMethDeclUnknown:
1583 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1584 case IsClsMethDeclKnown: {
1585 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1586 return ClassInfo(D, D->getIdentifier());
1587 }
1588 }
1589}
1590
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001591void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1592 if (CI.first == 0 && CI.second == 0)
1593 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1594 else if (CI.first == 0)
1595 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1596 else
1597 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1598}
1599
1600
Chris Lattnerf624cd22007-10-25 00:29:32 +00001601bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001602 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001603}
1604
Nate Begeman23a73022009-08-12 02:28:50 +00001605void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1606 unsigned NumExprs) {
1607 if (SubExprs) C.Deallocate(SubExprs);
1608
1609 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor725e94b2009-04-16 00:01:45 +00001610 this->NumExprs = NumExprs;
1611 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Nate Begeman23a73022009-08-12 02:28:50 +00001612}
1613
1614void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1615 DestroyChildren(C);
1616 if (SubExprs) C.Deallocate(SubExprs);
1617 this->~ShuffleVectorExpr();
1618 C.Deallocate(this);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001619}
1620
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001621void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001622 // Override default behavior of traversing children. If this has a type
1623 // operand and the type is a variable-length array, the child iteration
1624 // will iterate over the size expression. However, this expression belongs
1625 // to the type, not to this, so we don't want to delete it.
1626 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001627 if (isArgumentType()) {
1628 this->~SizeOfAlignOfExpr();
1629 C.Deallocate(this);
1630 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001631 else
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001632 Expr::DoDestroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001633}
1634
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001635//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001636// DesignatedInitExpr
1637//===----------------------------------------------------------------------===//
1638
1639IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1640 assert(Kind == FieldDesignator && "Only valid on a field designator");
1641 if (Field.NameOrField & 0x01)
1642 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1643 else
1644 return getField()->getIdentifier();
1645}
1646
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001647DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1648 const Designator *Designators,
1649 SourceLocation EqualOrColonLoc,
1650 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001651 Expr **IndexExprs,
1652 unsigned NumIndexExprs,
1653 Expr *Init)
1654 : Expr(DesignatedInitExprClass, Ty,
1655 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001656 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001657 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001658 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001659
1660 // Record the initializer itself.
1661 child_iterator Child = child_begin();
1662 *Child++ = Init;
1663
1664 // Copy the designators and their subexpressions, computing
1665 // value-dependence along the way.
1666 unsigned IndexIdx = 0;
1667 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001668 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001669
1670 if (this->Designators[I].isArrayDesignator()) {
1671 // Compute type- and value-dependence.
1672 Expr *Index = IndexExprs[IndexIdx];
1673 ValueDependent = ValueDependent ||
1674 Index->isTypeDependent() || Index->isValueDependent();
1675
1676 // Copy the index expressions into permanent storage.
1677 *Child++ = IndexExprs[IndexIdx++];
1678 } else if (this->Designators[I].isArrayRangeDesignator()) {
1679 // Compute type- and value-dependence.
1680 Expr *Start = IndexExprs[IndexIdx];
1681 Expr *End = IndexExprs[IndexIdx + 1];
1682 ValueDependent = ValueDependent ||
1683 Start->isTypeDependent() || Start->isValueDependent() ||
1684 End->isTypeDependent() || End->isValueDependent();
1685
1686 // Copy the start/end expressions into permanent storage.
1687 *Child++ = IndexExprs[IndexIdx++];
1688 *Child++ = IndexExprs[IndexIdx++];
1689 }
1690 }
1691
1692 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001693}
1694
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001695DesignatedInitExpr *
1696DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1697 unsigned NumDesignators,
1698 Expr **IndexExprs, unsigned NumIndexExprs,
1699 SourceLocation ColonOrEqualLoc,
1700 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001701 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001702 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001703 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1704 ColonOrEqualLoc, UsesColonSyntax,
1705 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001706}
1707
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001708DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1709 unsigned NumIndexExprs) {
1710 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1711 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1712 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1713}
1714
1715void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1716 unsigned NumDesigs) {
1717 if (Designators)
1718 delete [] Designators;
1719
1720 Designators = new Designator[NumDesigs];
1721 NumDesignators = NumDesigs;
1722 for (unsigned I = 0; I != NumDesigs; ++I)
1723 Designators[I] = Desigs[I];
1724}
1725
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001726SourceRange DesignatedInitExpr::getSourceRange() const {
1727 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001728 Designator &First =
1729 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001730 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001731 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001732 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1733 else
1734 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1735 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001736 StartLoc =
1737 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001738 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1739}
1740
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001741Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1742 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1743 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1744 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001745 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1746 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1747}
1748
1749Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1750 assert(D.Kind == Designator::ArrayRangeDesignator &&
1751 "Requires array range designator");
1752 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1753 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001754 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1755 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1756}
1757
1758Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1759 assert(D.Kind == Designator::ArrayRangeDesignator &&
1760 "Requires array range designator");
1761 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1762 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001763 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1764 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1765}
1766
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001767/// \brief Replaces the designator at index @p Idx with the series
1768/// of designators in [First, Last).
1769void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1770 const Designator *First,
1771 const Designator *Last) {
1772 unsigned NumNewDesignators = Last - First;
1773 if (NumNewDesignators == 0) {
1774 std::copy_backward(Designators + Idx + 1,
1775 Designators + NumDesignators,
1776 Designators + Idx);
1777 --NumNewDesignators;
1778 return;
1779 } else if (NumNewDesignators == 1) {
1780 Designators[Idx] = *First;
1781 return;
1782 }
1783
1784 Designator *NewDesignators
1785 = new Designator[NumDesignators - 1 + NumNewDesignators];
1786 std::copy(Designators, Designators + Idx, NewDesignators);
1787 std::copy(First, Last, NewDesignators + Idx);
1788 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1789 NewDesignators + Idx + NumNewDesignators);
1790 delete [] Designators;
1791 Designators = NewDesignators;
1792 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1793}
1794
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001795void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001796 delete [] Designators;
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001797 Expr::DoDestroy(C);
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001798}
1799
Nate Begemane85f43d2009-08-10 23:49:36 +00001800ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
1801 Expr **exprs, unsigned nexprs,
1802 SourceLocation rparenloc)
1803: Expr(ParenListExprClass, QualType(),
1804 hasAnyTypeDependentArguments(exprs, nexprs),
1805 hasAnyValueDependentArguments(exprs, nexprs)),
1806 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
1807
1808 Exprs = new (C) Stmt*[nexprs];
1809 for (unsigned i = 0; i != nexprs; ++i)
1810 Exprs[i] = exprs[i];
1811}
1812
1813void ParenListExpr::DoDestroy(ASTContext& C) {
1814 DestroyChildren(C);
1815 if (Exprs) C.Deallocate(Exprs);
1816 this->~ParenListExpr();
1817 C.Deallocate(this);
1818}
1819
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001820//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001821// ExprIterator.
1822//===----------------------------------------------------------------------===//
1823
1824Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1825Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1826Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1827const Expr* ConstExprIterator::operator[](size_t idx) const {
1828 return cast<Expr>(I[idx]);
1829}
1830const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1831const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1832
1833//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001834// Child Iterators for iterating over subexpressions/substatements
1835//===----------------------------------------------------------------------===//
1836
1837// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001838Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1839Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001840
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001841// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001842Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1843Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001844
Steve Naroff6f786252008-06-02 23:03:37 +00001845// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001846Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1847Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001848
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001849// ObjCKVCRefExpr
1850Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1851Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1852
Douglas Gregord8606632008-11-04 14:56:14 +00001853// ObjCSuperExpr
1854Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1855Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1856
Steve Naroff29d293b2009-07-24 17:54:45 +00001857// ObjCIsaExpr
1858Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1859Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1860
Chris Lattner69909292008-08-10 01:53:14 +00001861// PredefinedExpr
1862Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1863Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001864
1865// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001866Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1867Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001868
1869// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001870Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001871Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001872
1873// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001874Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1875Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001876
Chris Lattner1de66eb2007-08-26 03:42:43 +00001877// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001878Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1879Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001880
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001881// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001882Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1883Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001884
1885// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001886Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1887Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001888
1889// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001890Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1891Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001892
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001893// SizeOfAlignOfExpr
1894Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1895 // If this is of a type and the type is a VLA type (and not a typedef), the
1896 // size expression of the VLA needs to be treated as an executable expression.
1897 // Why isn't this weirdness documented better in StmtIterator?
1898 if (isArgumentType()) {
1899 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1900 getArgumentType().getTypePtr()))
1901 return child_iterator(T);
1902 return child_iterator();
1903 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001904 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001905}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001906Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1907 if (isArgumentType())
1908 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001909 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001910}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001911
1912// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001913Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001914 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001915}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001916Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001917 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001918}
1919
1920// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001921Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001922 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001923}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001924Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001925 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001926}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001927
1928// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001929Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1930Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001931
Nate Begemanaf6ed502008-04-18 23:10:10 +00001932// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001933Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1934Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001935
1936// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001937Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1938Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001939
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001940// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001941Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1942Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001943
1944// BinaryOperator
1945Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001946 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001947}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001948Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001949 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001950}
1951
1952// ConditionalOperator
1953Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001954 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001955}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001956Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001957 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001958}
1959
1960// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001961Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1962Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001963
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001964// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001965Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1966Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001967
1968// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001969Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1970 return child_iterator();
1971}
1972
1973Stmt::child_iterator TypesCompatibleExpr::child_end() {
1974 return child_iterator();
1975}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001976
1977// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001978Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1979Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001980
Douglas Gregorad4b3792008-11-29 04:51:27 +00001981// GNUNullExpr
1982Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1983Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1984
Eli Friedmand0e9d092008-05-14 19:38:39 +00001985// ShuffleVectorExpr
1986Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001987 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001988}
1989Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001990 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001991}
1992
Anders Carlsson36760332007-10-15 20:28:48 +00001993// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001994Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1995Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001996
Anders Carlsson762b7c72007-08-31 04:56:16 +00001997// InitListExpr
1998Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001999 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002000}
2001Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002002 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002003}
2004
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002005// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002006Stmt::child_iterator DesignatedInitExpr::child_begin() {
2007 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2008 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002009 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2010}
2011Stmt::child_iterator DesignatedInitExpr::child_end() {
2012 return child_iterator(&*child_begin() + NumSubExprs);
2013}
2014
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002015// ImplicitValueInitExpr
2016Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2017 return child_iterator();
2018}
2019
2020Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2021 return child_iterator();
2022}
2023
Nate Begemane85f43d2009-08-10 23:49:36 +00002024// ParenListExpr
2025Stmt::child_iterator ParenListExpr::child_begin() {
2026 return &Exprs[0];
2027}
2028Stmt::child_iterator ParenListExpr::child_end() {
2029 return &Exprs[0]+NumExprs;
2030}
2031
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002032// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00002033Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00002034 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00002035}
2036Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00002037 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00002038}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002039
2040// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002041Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2042Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002043
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002044// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002045Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2046 return child_iterator();
2047}
2048Stmt::child_iterator ObjCSelectorExpr::child_end() {
2049 return child_iterator();
2050}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002051
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002052// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002053Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2054 return child_iterator();
2055}
2056Stmt::child_iterator ObjCProtocolExpr::child_end() {
2057 return child_iterator();
2058}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002059
Steve Naroffc39ca262007-09-18 23:55:05 +00002060// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002061Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002062 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002063}
2064Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002065 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002066}
2067
Steve Naroff52a81c02008-09-03 18:15:37 +00002068// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002069Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2070Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002071
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002072Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2073Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }