blob: a69448e7bc8d1617f1a055037929d049336623e8 [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"
Douglas Gregorefccbec2009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson63f1ad92009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000025#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Primary Expressions.
30//===----------------------------------------------------------------------===//
31
Chris Lattnere0391b22008-06-07 22:13:43 +000032/// getValueAsApproximateDouble - This returns the value as an inaccurate
33/// double. Note that this may cause loss of precision, but is useful for
34/// debugging dumps, etc.
35double FloatingLiteral::getValueAsApproximateDouble() const {
36 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000037 bool ignored;
38 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
39 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000040 return V.convertToDouble();
41}
42
Chris Lattneraa491192009-02-18 06:40:38 +000043StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
44 unsigned ByteLength, bool Wide,
45 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000046 const SourceLocation *Loc,
47 unsigned NumStrs) {
Chris Lattneraa491192009-02-18 06:40:38 +000048 // Allocate enough space for the StringLiteral plus an array of locations for
49 // any concatenated string tokens.
50 void *Mem = C.Allocate(sizeof(StringLiteral)+
51 sizeof(SourceLocation)*(NumStrs-1),
52 llvm::alignof<StringLiteral>());
53 StringLiteral *SL = new (Mem) StringLiteral(Ty);
54
Chris Lattner4b009652007-07-25 00:24:17 +000055 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000056 char *AStrData = new (C, 1) char[ByteLength];
57 memcpy(AStrData, StrData, ByteLength);
58 SL->StrData = AStrData;
59 SL->ByteLength = ByteLength;
60 SL->IsWide = Wide;
61 SL->TokLocs[0] = Loc[0];
62 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000063
Chris Lattnerc3144742009-02-18 05:49:11 +000064 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000065 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
66 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000067}
68
Douglas Gregor596e0932009-04-15 16:35:07 +000069StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
70 void *Mem = C.Allocate(sizeof(StringLiteral)+
71 sizeof(SourceLocation)*(NumStrs-1),
72 llvm::alignof<StringLiteral>());
73 StringLiteral *SL = new (Mem) StringLiteral(QualType());
74 SL->StrData = 0;
75 SL->ByteLength = 0;
76 SL->NumConcatenated = NumStrs;
77 return SL;
78}
79
Douglas Gregor53e8e4b2009-08-07 06:08:38 +000080void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000081 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor53e8e4b2009-08-07 06:08:38 +000082 Expr::DoDestroy(C);
Chris Lattner4b009652007-07-25 00:24:17 +000083}
84
Douglas Gregor596e0932009-04-15 16:35:07 +000085void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
86 if (StrData)
87 C.Deallocate(const_cast<char*>(StrData));
88
89 char *AStrData = new (C, 1) char[Len];
90 memcpy(AStrData, Str, Len);
91 StrData = AStrData;
92 ByteLength = Len;
93}
94
Chris Lattner4b009652007-07-25 00:24:17 +000095/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
96/// corresponds to, e.g. "sizeof" or "[pre]++".
97const char *UnaryOperator::getOpcodeStr(Opcode Op) {
98 switch (Op) {
99 default: assert(0 && "Unknown unary operator");
100 case PostInc: return "++";
101 case PostDec: return "--";
102 case PreInc: return "++";
103 case PreDec: return "--";
104 case AddrOf: return "&";
105 case Deref: return "*";
106 case Plus: return "+";
107 case Minus: return "-";
108 case Not: return "~";
109 case LNot: return "!";
110 case Real: return "__real";
111 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000112 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000113 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000114 }
115}
116
Douglas Gregorc78182d2009-03-13 23:49:33 +0000117UnaryOperator::Opcode
118UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
119 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000120 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000121 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
122 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
123 case OO_Amp: return AddrOf;
124 case OO_Star: return Deref;
125 case OO_Plus: return Plus;
126 case OO_Minus: return Minus;
127 case OO_Tilde: return Not;
128 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000129 }
130}
131
132OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
133 switch (Opc) {
134 case PostInc: case PreInc: return OO_PlusPlus;
135 case PostDec: case PreDec: return OO_MinusMinus;
136 case AddrOf: return OO_Amp;
137 case Deref: return OO_Star;
138 case Plus: return OO_Plus;
139 case Minus: return OO_Minus;
140 case Not: return OO_Tilde;
141 case LNot: return OO_Exclaim;
142 default: return OO_None;
143 }
144}
145
146
Chris Lattner4b009652007-07-25 00:24:17 +0000147//===----------------------------------------------------------------------===//
148// Postfix Operators.
149//===----------------------------------------------------------------------===//
150
Ted Kremenek362abcd2009-02-09 20:51:47 +0000151CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000152 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000153 : Expr(SC, t,
154 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000155 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000156 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000157
158 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000159 SubExprs[FN] = fn;
160 for (unsigned i = 0; i != numargs; ++i)
161 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000162
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000163 RParenLoc = rparenloc;
164}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000165
Ted Kremenek362abcd2009-02-09 20:51:47 +0000166CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
167 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000168 : Expr(CallExprClass, t,
169 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000170 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000171 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000172
173 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000174 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000175 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000176 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000177
Chris Lattner4b009652007-07-25 00:24:17 +0000178 RParenLoc = rparenloc;
179}
180
Argiris Kirtzidisa06a9da2009-07-14 03:19:21 +0000181CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
182 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000183 SubExprs = new (C) Stmt*[1];
184}
185
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000186void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000187 DestroyChildren(C);
188 if (SubExprs) C.Deallocate(SubExprs);
189 this->~CallExpr();
190 C.Deallocate(this);
191}
192
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000193FunctionDecl *CallExpr::getDirectCallee() {
194 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattnereb346742009-07-17 15:46:27 +0000195 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000196 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000197
198 return 0;
199}
200
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000201/// setNumArgs - This changes the number of arguments present in this call.
202/// Any orphaned expressions are deleted by this, and any new operands are set
203/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000204void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000205 // No change, just return.
206 if (NumArgs == getNumArgs()) return;
207
208 // If shrinking # arguments, just delete the extras and forgot them.
209 if (NumArgs < getNumArgs()) {
210 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000211 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000212 this->NumArgs = NumArgs;
213 return;
214 }
215
216 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar1a6ead22009-07-28 06:29:46 +0000217 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000218 // Copy over args.
219 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
220 NewSubExprs[i] = SubExprs[i];
221 // Null out new args.
222 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
223 NewSubExprs[i] = 0;
224
Douglas Gregorb9f63642009-04-17 21:46:47 +0000225 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000226 SubExprs = NewSubExprs;
227 this->NumArgs = NumArgs;
228}
229
Chris Lattnerc24915f2008-10-06 05:00:53 +0000230/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
231/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000232unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000233 // All simple function calls (e.g. func()) are implicitly cast to pointer to
234 // function. As a result, we try and obtain the DeclRefExpr from the
235 // ImplicitCastExpr.
236 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
237 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000238 return 0;
239
Steve Naroff44aec4c2008-01-31 01:07:12 +0000240 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
241 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000242 return 0;
243
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000244 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
245 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000246 return 0;
247
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000248 if (!FDecl->getIdentifier())
249 return 0;
250
Douglas Gregorb5af7382009-02-14 18:57:46 +0000251 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000252}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000253
Anders Carlsson66070902009-05-26 04:57:27 +0000254QualType CallExpr::getCallReturnType() const {
255 QualType CalleeType = getCallee()->getType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000256 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000257 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000258 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000259 CalleeType = BPT->getPointeeType();
260
261 const FunctionType *FnType = CalleeType->getAsFunctionType();
262 return FnType->getResultType();
263}
Chris Lattnerc24915f2008-10-06 05:00:53 +0000264
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000265MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
266 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregord33e3282009-09-01 00:37:14 +0000267 SourceLocation l, bool has_explicit,
268 SourceLocation langle,
269 const TemplateArgument *targs, unsigned numtargs,
270 SourceLocation rangle, QualType ty)
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000271 : Expr(MemberExprClass, ty,
272 base->isTypeDependent() || (qual && qual->isDependent()),
273 base->isValueDependent() || (qual && qual->isDependent())),
274 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
Douglas Gregord33e3282009-09-01 00:37:14 +0000275 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000276 // Initialize the qualifier, if any.
277 if (HasQualifier) {
278 NameQualifier *NQ = getMemberQualifier();
279 NQ->NNS = qual;
280 NQ->Range = qualrange;
281 }
Douglas Gregord33e3282009-09-01 00:37:14 +0000282
283 // Initialize the explicit template argument list, if any.
284 if (HasExplicitTemplateArgumentList) {
285 ExplicitTemplateArgumentList *ETemplateArgs
286 = getExplicitTemplateArgumentList();
287 ETemplateArgs->LAngleLoc = langle;
288 ETemplateArgs->RAngleLoc = rangle;
289 ETemplateArgs->NumTemplateArgs = numtargs;
290
291 TemplateArgument *TemplateArgs = ETemplateArgs->getTemplateArgs();
292 for (unsigned I = 0; I < numtargs; ++I)
293 new (TemplateArgs + I) TemplateArgument(targs[I]);
294 }
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000295}
296
297MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
298 NestedNameSpecifier *qual,
299 SourceRange qualrange,
300 NamedDecl *memberdecl,
Douglas Gregord33e3282009-09-01 00:37:14 +0000301 SourceLocation l,
302 bool has_explicit,
303 SourceLocation langle,
304 const TemplateArgument *targs,
305 unsigned numtargs,
306 SourceLocation rangle,
307 QualType ty) {
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000308 std::size_t Size = sizeof(MemberExpr);
309 if (qual != 0)
310 Size += sizeof(NameQualifier);
311
Douglas Gregord33e3282009-09-01 00:37:14 +0000312 if (has_explicit)
313 Size += sizeof(ExplicitTemplateArgumentList) +
314 sizeof(TemplateArgument) * numtargs;
315
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000316 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregord33e3282009-09-01 00:37:14 +0000317 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
318 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000319 ty);
320}
321
Chris Lattner4b009652007-07-25 00:24:17 +0000322/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
323/// corresponds to, e.g. "<<=".
324const char *BinaryOperator::getOpcodeStr(Opcode Op) {
325 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000326 case PtrMemD: return ".*";
327 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000328 case Mul: return "*";
329 case Div: return "/";
330 case Rem: return "%";
331 case Add: return "+";
332 case Sub: return "-";
333 case Shl: return "<<";
334 case Shr: return ">>";
335 case LT: return "<";
336 case GT: return ">";
337 case LE: return "<=";
338 case GE: return ">=";
339 case EQ: return "==";
340 case NE: return "!=";
341 case And: return "&";
342 case Xor: return "^";
343 case Or: return "|";
344 case LAnd: return "&&";
345 case LOr: return "||";
346 case Assign: return "=";
347 case MulAssign: return "*=";
348 case DivAssign: return "/=";
349 case RemAssign: return "%=";
350 case AddAssign: return "+=";
351 case SubAssign: return "-=";
352 case ShlAssign: return "<<=";
353 case ShrAssign: return ">>=";
354 case AndAssign: return "&=";
355 case XorAssign: return "^=";
356 case OrAssign: return "|=";
357 case Comma: return ",";
358 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000359
360 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000361}
362
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000363BinaryOperator::Opcode
364BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
365 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000366 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000367 case OO_Plus: return Add;
368 case OO_Minus: return Sub;
369 case OO_Star: return Mul;
370 case OO_Slash: return Div;
371 case OO_Percent: return Rem;
372 case OO_Caret: return Xor;
373 case OO_Amp: return And;
374 case OO_Pipe: return Or;
375 case OO_Equal: return Assign;
376 case OO_Less: return LT;
377 case OO_Greater: return GT;
378 case OO_PlusEqual: return AddAssign;
379 case OO_MinusEqual: return SubAssign;
380 case OO_StarEqual: return MulAssign;
381 case OO_SlashEqual: return DivAssign;
382 case OO_PercentEqual: return RemAssign;
383 case OO_CaretEqual: return XorAssign;
384 case OO_AmpEqual: return AndAssign;
385 case OO_PipeEqual: return OrAssign;
386 case OO_LessLess: return Shl;
387 case OO_GreaterGreater: return Shr;
388 case OO_LessLessEqual: return ShlAssign;
389 case OO_GreaterGreaterEqual: return ShrAssign;
390 case OO_EqualEqual: return EQ;
391 case OO_ExclaimEqual: return NE;
392 case OO_LessEqual: return LE;
393 case OO_GreaterEqual: return GE;
394 case OO_AmpAmp: return LAnd;
395 case OO_PipePipe: return LOr;
396 case OO_Comma: return Comma;
397 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000398 }
399}
400
401OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
402 static const OverloadedOperatorKind OverOps[] = {
403 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
404 OO_Star, OO_Slash, OO_Percent,
405 OO_Plus, OO_Minus,
406 OO_LessLess, OO_GreaterGreater,
407 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
408 OO_EqualEqual, OO_ExclaimEqual,
409 OO_Amp,
410 OO_Caret,
411 OO_Pipe,
412 OO_AmpAmp,
413 OO_PipePipe,
414 OO_Equal, OO_StarEqual,
415 OO_SlashEqual, OO_PercentEqual,
416 OO_PlusEqual, OO_MinusEqual,
417 OO_LessLessEqual, OO_GreaterGreaterEqual,
418 OO_AmpEqual, OO_CaretEqual,
419 OO_PipeEqual,
420 OO_Comma
421 };
422 return OverOps[Opc];
423}
424
Anders Carlsson762b7c72007-08-31 04:56:16 +0000425InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000426 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000427 SourceLocation rbraceloc)
Douglas Gregor3a7a06e2009-05-21 23:17:49 +0000428 : Expr(InitListExprClass, QualType(),
429 hasAnyTypeDependentArguments(initExprs, numInits),
430 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor82462762009-01-29 16:53:55 +0000431 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000432 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000433
434 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000435}
Chris Lattner4b009652007-07-25 00:24:17 +0000436
Douglas Gregoree0792c2009-03-20 23:58:33 +0000437void InitListExpr::reserveInits(unsigned NumInits) {
438 if (NumInits > InitExprs.size())
439 InitExprs.reserve(NumInits);
440}
441
Douglas Gregorf603b472009-01-28 21:54:33 +0000442void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000443 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000444 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000445 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000446 InitExprs.resize(NumInits, 0);
447}
448
449Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
450 if (Init >= InitExprs.size()) {
451 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
452 InitExprs.back() = expr;
453 return 0;
454 }
455
456 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
457 InitExprs[Init] = expr;
458 return Result;
459}
460
Steve Naroff6f373332008-09-04 15:31:07 +0000461/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000462///
463const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000464 return getType()->getAs<BlockPointerType>()->
Steve Naroff52a81c02008-09-03 18:15:37 +0000465 getPointeeType()->getAsFunctionType();
466}
467
Steve Naroff9ac456d2008-10-08 17:01:13 +0000468SourceLocation BlockExpr::getCaretLocation() const {
469 return TheBlock->getCaretLocation();
470}
Douglas Gregore3241e92009-04-18 00:02:19 +0000471const Stmt *BlockExpr::getBody() const {
472 return TheBlock->getBody();
473}
474Stmt *BlockExpr::getBody() {
475 return TheBlock->getBody();
476}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000477
478
Chris Lattner4b009652007-07-25 00:24:17 +0000479//===----------------------------------------------------------------------===//
480// Generic Expression Routines
481//===----------------------------------------------------------------------===//
482
Chris Lattnerd2c66552009-02-14 07:37:35 +0000483/// isUnusedResultAWarning - Return true if this immediate expression should
484/// be warned about if the result is unused. If so, fill in Loc and Ranges
485/// with location to warn on and the source range[s] to report with the
486/// warning.
487bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000488 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000489 // Don't warn if the expr is type dependent. The type could end up
490 // instantiating to void.
491 if (isTypeDependent())
492 return false;
493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 switch (getStmtClass()) {
495 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000496 Loc = getExprLoc();
497 R1 = getSourceRange();
498 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000499 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000500 return cast<ParenExpr>(this)->getSubExpr()->
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000501 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000502 case UnaryOperatorClass: {
503 const UnaryOperator *UO = cast<UnaryOperator>(this);
504
505 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000506 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000507 case UnaryOperator::PostInc:
508 case UnaryOperator::PostDec:
509 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000510 case UnaryOperator::PreDec: // ++/--
511 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000512 case UnaryOperator::Deref:
513 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000514 if (getType().isVolatileQualified())
515 return false;
516 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000517 case UnaryOperator::Real:
518 case UnaryOperator::Imag:
519 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000520 if (UO->getSubExpr()->getType().isVolatileQualified())
521 return false;
522 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000523 case UnaryOperator::Extension:
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000524 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000525 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000526 Loc = UO->getOperatorLoc();
527 R1 = UO->getSubExpr()->getSourceRange();
528 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000529 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000530 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000531 const BinaryOperator *BO = cast<BinaryOperator>(this);
532 // Consider comma to have side effects if the LHS or RHS does.
533 if (BO->getOpcode() == BinaryOperator::Comma)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000534 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
535 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000536
Chris Lattnerd2c66552009-02-14 07:37:35 +0000537 if (BO->isAssignmentOp())
538 return false;
539 Loc = BO->getOperatorLoc();
540 R1 = BO->getLHS()->getSourceRange();
541 R2 = BO->getRHS()->getSourceRange();
542 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000543 }
Chris Lattner06078d22007-08-25 02:00:02 +0000544 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000545 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000546
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000547 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000548 // The condition must be evaluated, but if either the LHS or RHS is a
549 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000550 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000551 if (Exp->getLHS() &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000552 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000553 return true;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000554 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000555 }
556
Chris Lattner4b009652007-07-25 00:24:17 +0000557 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000558 // If the base pointer or element is to a volatile pointer/field, accessing
559 // it is a side effect.
560 if (getType().isVolatileQualified())
561 return false;
562 Loc = cast<MemberExpr>(this)->getMemberLoc();
563 R1 = SourceRange(Loc, Loc);
564 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
565 return true;
566
Chris Lattner4b009652007-07-25 00:24:17 +0000567 case ArraySubscriptExprClass:
568 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000569 // it is a side effect.
570 if (getType().isVolatileQualified())
571 return false;
572 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
573 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
574 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
575 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000576
Chris Lattner4b009652007-07-25 00:24:17 +0000577 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000578 case CXXOperatorCallExprClass:
579 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000580 // If this is a direct call, get the callee.
581 const CallExpr *CE = cast<CallExpr>(this);
582 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
583 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
584 // If the callee has attribute pure, const, or warn_unused_result, warn
585 // about it. void foo() { strlen("bar"); } should warn.
586 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000587 if (FD->getAttr<WarnUnusedResultAttr>() ||
588 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000589 Loc = CE->getCallee()->getLocStart();
590 R1 = CE->getCallee()->getSourceRange();
591
592 if (unsigned NumArgs = CE->getNumArgs())
593 R2 = SourceRange(CE->getArg(0)->getLocStart(),
594 CE->getArg(NumArgs-1)->getLocEnd());
595 return true;
596 }
597 }
598 return false;
599 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000600 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000601 return false;
Chris Lattner66930522009-08-16 16:45:18 +0000602
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000603 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattner66930522009-08-16 16:45:18 +0000604#if 0
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000605 const ObjCImplicitSetterGetterRefExpr *Ref =
606 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattner66930522009-08-16 16:45:18 +0000607 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian1c4da452009-08-18 20:50:23 +0000608 Loc = Ref->getLocation();
609 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
610 if (Ref->getBase())
611 R2 = Ref->getBase()->getSourceRange();
Chris Lattner3a8e32d2009-08-16 16:51:50 +0000612#else
613 Loc = getExprLoc();
614 R1 = getSourceRange();
Chris Lattner66930522009-08-16 16:45:18 +0000615#endif
616 return true;
617 }
Chris Lattner200964f2008-07-26 19:51:01 +0000618 case StmtExprClass: {
619 // Statement exprs don't logically have side effects themselves, but are
620 // sometimes used in macros in ways that give them a type that is unused.
621 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
622 // however, if the result of the stmt expr is dead, we don't want to emit a
623 // warning.
624 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
625 if (!CS->body_empty())
626 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000627 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000628
629 Loc = cast<StmtExpr>(this)->getLParenLoc();
630 R1 = getSourceRange();
631 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000632 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000633 case CStyleCastExprClass:
Chris Lattner34796b62009-07-28 18:25:28 +0000634 // If this is an explicit cast to void, allow it. People do this when they
635 // think they know what they're doing :).
Chris Lattnerd2c66552009-02-14 07:37:35 +0000636 if (getType()->isVoidType())
Chris Lattner34796b62009-07-28 18:25:28 +0000637 return false;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000638 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
639 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
640 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000641 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000642 // If this is a cast to void, check the operand. Otherwise, the result of
643 // the cast is unused.
644 if (getType()->isVoidType())
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000645 return cast<CastExpr>(this)->getSubExpr()
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000646 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000647 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
648 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
649 return true;
650
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000651 case ImplicitCastExprClass:
652 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000653 return cast<ImplicitCastExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000654 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000655
Chris Lattner3e254fb2008-04-08 04:40:51 +0000656 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000657 return cast<CXXDefaultArgExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000658 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000659
660 case CXXNewExprClass:
661 // FIXME: In theory, there might be new expressions that don't have side
662 // effects (e.g. a placement new with an uninitialized POD).
663 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000664 return false;
Anders Carlsson16497742009-08-16 04:11:06 +0000665 case CXXBindTemporaryExprClass:
666 return cast<CXXBindTemporaryExpr>(this)
667 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson18ca4772009-05-17 21:11:30 +0000668 case CXXExprWithTemporariesClass:
669 return cast<CXXExprWithTemporaries>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000670 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000671 }
Chris Lattner4b009652007-07-25 00:24:17 +0000672}
673
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000674/// DeclCanBeLvalue - Determine whether the given declaration can be
675/// an lvalue. This is a helper routine for isLvalue.
676static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000677 // C++ [temp.param]p6:
678 // A non-type non-reference template-parameter is not an lvalue.
679 if (const NonTypeTemplateParmDecl *NTTParm
680 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
681 return NTTParm->getType()->isReferenceType();
682
Douglas Gregor8acb7272008-12-11 16:49:14 +0000683 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000684 // C++ 3.10p2: An lvalue refers to an object or function.
685 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor62f78762009-07-08 20:55:45 +0000686 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
687 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000688}
689
Chris Lattner4b009652007-07-25 00:24:17 +0000690/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
691/// incomplete type other than void. Nonarray expressions that can be lvalues:
692/// - name, where name must be a variable
693/// - e[i]
694/// - (e), where e must be an lvalue
695/// - e.name, where e must be an lvalue
696/// - e->name
697/// - *e, the type of e cannot be a function type
698/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000699/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000700/// - reference type [C++ [expr]]
701///
Chris Lattner25168a52008-07-26 21:30:36 +0000702Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000703 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
704
705 isLvalueResult Res = isLvalueInternal(Ctx);
706 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
707 return Res;
708
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000709 // first, check the type (C99 6.3.2.1). Expressions with function
710 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor62f78762009-07-08 20:55:45 +0000711 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Chris Lattner4b009652007-07-25 00:24:17 +0000712 return LV_NotObjectType;
713
Steve Naroffec7736d2008-02-10 01:39:04 +0000714 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000715 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000716 return LV_IncompleteVoidType;
717
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000718 return LV_Valid;
719}
Chris Lattner4b009652007-07-25 00:24:17 +0000720
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000721// Check whether the expression can be sanely treated like an l-value
722Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000723 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000724 case StringLiteralClass: // C99 6.5.1p4
725 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000726 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000727 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
728 // For vectors, make sure base is an lvalue (i.e. not a function call).
729 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000730 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000731 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000732 case DeclRefExprClass:
733 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000734 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
735 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000736 return LV_Valid;
737 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000738 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000739 case BlockDeclRefExprClass: {
740 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000741 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000742 return LV_Valid;
743 break;
744 }
Douglas Gregorc1991bf2009-08-31 23:41:50 +0000745 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000746 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000747 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
748 NamedDecl *Member = m->getMemberDecl();
749 // C++ [expr.ref]p4:
750 // If E2 is declared to have type "reference to T", then E1.E2
751 // is an lvalue.
752 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
753 if (Value->getType()->isReferenceType())
754 return LV_Valid;
755
756 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000757 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000758 return LV_Valid;
759
760 // -- If E2 is a non-static data member [...]. If E1 is an
761 // lvalue, then E1.E2 is an lvalue.
762 if (isa<FieldDecl>(Member))
763 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
764
765 // -- If it refers to a static member function [...], then
766 // E1.E2 is an lvalue.
767 // -- Otherwise, if E1.E2 refers to a non-static member
768 // function [...], then E1.E2 is not an lvalue.
769 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
770 return Method->isStatic()? LV_Valid : LV_MemberFunction;
771
772 // -- If E2 is a member enumerator [...], the expression E1.E2
773 // is not an lvalue.
774 if (isa<EnumConstantDecl>(Member))
775 return LV_InvalidExpression;
776
777 // Not an lvalue.
778 return LV_InvalidExpression;
779 }
780
781 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000782 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000783 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000784 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000785 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000786 return LV_Valid; // C99 6.5.3p4
787
788 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000789 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
790 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000791 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000792
793 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
794 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
795 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
796 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000797 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000798 case ImplicitCastExprClass:
799 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
800 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000801 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000802 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000803 case BinaryOperatorClass:
804 case CompoundAssignOperatorClass: {
805 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000806
807 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
808 BinOp->getOpcode() == BinaryOperator::Comma)
809 return BinOp->getRHS()->isLvalue(Ctx);
810
Sebastian Redl95216a62009-02-07 00:15:38 +0000811 // C++ [expr.mptr.oper]p6
812 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
813 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
814 !BinOp->getType()->isFunctionType())
815 return BinOp->getLHS()->isLvalue(Ctx);
816
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000817 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000818 return LV_InvalidExpression;
819
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000820 if (Ctx.getLangOptions().CPlusPlus)
821 // C++ [expr.ass]p1:
822 // The result of an assignment operation [...] is an lvalue.
823 return LV_Valid;
824
825
826 // C99 6.5.16:
827 // An assignment expression [...] is not an lvalue.
828 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000829 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000830 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000831 case CXXOperatorCallExprClass:
832 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000833 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000834 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000835 // is an lvalue reference.
Anders Carlsson66070902009-05-26 04:57:27 +0000836 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
837 if (ReturnType->isLValueReferenceType())
838 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000839
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000840 break;
841 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000842 case CompoundLiteralExprClass: // C99 6.5.2.5p5
843 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000844 case ChooseExprClass:
845 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000846 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000847 case ExtVectorElementExprClass:
848 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000849 return LV_DuplicateVectorComponents;
850 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000851 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
852 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000853 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
854 return LV_Valid;
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000855 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000856 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000857 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000858 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000859 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000860 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000861 case CXXConditionDeclExprClass:
862 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000863 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000864 case CXXFunctionalCastExprClass:
865 case CXXStaticCastExprClass:
866 case CXXDynamicCastExprClass:
867 case CXXReinterpretCastExprClass:
868 case CXXConstCastExprClass:
869 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000870 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000871 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
872 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000873 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
874 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000875 return LV_Valid;
876 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000877 case CXXTypeidExprClass:
878 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
879 return LV_Valid;
Anders Carlssonb8ff3402009-08-16 03:42:12 +0000880 case CXXBindTemporaryExprClass:
881 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
882 isLvalueInternal(Ctx);
Sebastian Redld3169132009-04-17 16:30:52 +0000883 case ConditionalOperatorClass: {
884 // Complicated handling is only for C++.
885 if (!Ctx.getLangOptions().CPlusPlus)
886 return LV_InvalidExpression;
887
888 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
889 // everywhere there's an object converted to an rvalue. Also, any other
890 // casts should be wrapped by ImplicitCastExprs. There's just the special
891 // case involving throws to work out.
892 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000893 Expr *True = Cond->getTrueExpr();
894 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000895 // C++0x 5.16p2
896 // If either the second or the third operand has type (cv) void, [...]
897 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000898 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000899 return LV_InvalidExpression;
900
901 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000902 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000903 return LV_InvalidExpression;
904
905 // That's it.
906 return LV_Valid;
907 }
908
Chris Lattner4b009652007-07-25 00:24:17 +0000909 default:
910 break;
911 }
912 return LV_InvalidExpression;
913}
914
915/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
916/// does not have an incomplete type, does not have a const-qualified type, and
917/// if it is a structure or union, does not have any member (including,
918/// recursively, any member or element of all contained aggregates or unions)
919/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000920Expr::isModifiableLvalueResult
921Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000922 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000923
924 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000925 case LV_Valid:
926 // C++ 3.10p11: Functions cannot be modified, but pointers to
927 // functions can be modifiable.
928 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
929 return MLV_NotObjectType;
930 break;
931
Chris Lattner4b009652007-07-25 00:24:17 +0000932 case LV_NotObjectType: return MLV_NotObjectType;
933 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000934 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000935 case LV_InvalidExpression:
936 // If the top level is a C-style cast, and the subexpression is a valid
937 // lvalue, then this is probably a use of the old-school "cast as lvalue"
938 // GCC extension. We don't support it, but we want to produce good
939 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000940 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
941 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
942 if (Loc)
943 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000944 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000945 }
946 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000947 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000948 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000949 }
Eli Friedman91571da2009-03-22 23:26:56 +0000950
951 // The following is illegal:
952 // void takeclosure(void (^C)(void));
953 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
954 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000955 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000956 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
957 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
958 return MLV_NotBlockQualified;
959 }
960
Chris Lattnera1923f62008-08-04 07:31:14 +0000961 QualType CT = Ctx.getCanonicalType(getType());
962
963 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000964 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000965 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000966 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000967 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000968 return MLV_IncompleteType;
969
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000970 if (const RecordType *r = CT->getAs<RecordType>()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000971 if (r->hasConstFields())
972 return MLV_ConstQualified;
973 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000974
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000975 // Assigning to an 'implicit' property?
Fariborz Jahanian128cdc52009-08-20 17:02:02 +0000976 else if (isa<ObjCImplicitSetterGetterRefExpr>(this)) {
977 const ObjCImplicitSetterGetterRefExpr* Expr =
978 cast<ObjCImplicitSetterGetterRefExpr>(this);
Fariborz Jahanian1c4da452009-08-18 20:50:23 +0000979 if (Expr->getSetterMethod() == 0)
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000980 return MLV_NoSetterProperty;
981 }
Chris Lattner4b009652007-07-25 00:24:17 +0000982 return MLV_Valid;
983}
984
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000985/// isOBJCGCCandidate - Check if an expression is objc gc'able.
986///
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000987bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000988 switch (getStmtClass()) {
989 default:
990 return false;
991 case ObjCIvarRefExprClass:
992 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000993 case Expr::UnaryOperatorClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000994 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000995 case ParenExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000996 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000997 case ImplicitCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000998 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000999 case CStyleCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001000 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001001 case DeclRefExprClass:
1002 case QualifiedDeclRefExprClass: {
1003 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001004 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1005 if (VD->hasGlobalStorage())
1006 return true;
1007 QualType T = VD->getType();
1008 // dereferencing to an object pointer is always a gc'able candidate
1009 if (T->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001010 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001011 return true;
1012
1013 }
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001014 return false;
1015 }
Douglas Gregorc1991bf2009-08-31 23:41:50 +00001016 case MemberExprClass: {
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001017 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001018 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001019 }
1020 case ArraySubscriptExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001021 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001022 }
1023}
Ted Kremenek87e30c52008-01-17 16:57:34 +00001024Expr* Expr::IgnoreParens() {
1025 Expr* E = this;
1026 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1027 E = P->getSubExpr();
1028
1029 return E;
1030}
1031
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001032/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1033/// or CastExprs or ImplicitCastExprs, returning their operand.
1034Expr *Expr::IgnoreParenCasts() {
1035 Expr *E = this;
1036 while (true) {
1037 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1038 E = P->getSubExpr();
1039 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1040 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001041 else
1042 return E;
1043 }
1044}
1045
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001046/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1047/// value (including ptr->int casts of the same size). Strip off any
1048/// ParenExpr or CastExprs, returning their operand.
1049Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1050 Expr *E = this;
1051 while (true) {
1052 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1053 E = P->getSubExpr();
1054 continue;
1055 }
1056
1057 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1058 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1059 // ptr<->int casts of the same width. We also ignore all identify casts.
1060 Expr *SE = P->getSubExpr();
1061
1062 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1063 E = SE;
1064 continue;
1065 }
1066
1067 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1068 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1069 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1070 E = SE;
1071 continue;
1072 }
1073 }
1074
1075 return E;
1076 }
1077}
1078
1079
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001080/// hasAnyTypeDependentArguments - Determines if any of the expressions
1081/// in Exprs is type-dependent.
1082bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1083 for (unsigned I = 0; I < NumExprs; ++I)
1084 if (Exprs[I]->isTypeDependent())
1085 return true;
1086
1087 return false;
1088}
1089
1090/// hasAnyValueDependentArguments - Determines if any of the expressions
1091/// in Exprs is value-dependent.
1092bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1093 for (unsigned I = 0; I < NumExprs; ++I)
1094 if (Exprs[I]->isValueDependent())
1095 return true;
1096
1097 return false;
1098}
1099
Eli Friedmandee41122009-01-25 02:32:41 +00001100bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001101 // This function is attempting whether an expression is an initializer
1102 // which can be evaluated at compile-time. isEvaluatable handles most
1103 // of the cases, but it can't deal with some initializer-specific
1104 // expressions, and it can't deal with aggregates; we deal with those here,
1105 // and fall back to isEvaluatable for the other cases.
1106
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001107 // FIXME: This function assumes the variable being assigned to
1108 // isn't a reference type!
1109
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001110 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001111 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001112 case StringLiteralClass:
Steve Naroff329ec222009-07-10 23:34:53 +00001113 case ObjCStringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001114 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001115 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001116 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001117 // This handles gcc's extension that allows global initializers like
1118 // "struct x {int x;} x = (struct x) {};".
1119 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001120 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001121 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001122 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001123 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001124 // FIXME: This doesn't deal with fields with reference types correctly.
1125 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1126 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001127 const InitListExpr *Exp = cast<InitListExpr>(this);
1128 unsigned numInits = Exp->getNumInits();
1129 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001130 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001131 return false;
1132 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001133 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001134 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001135 case ImplicitValueInitExprClass:
1136 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001137 case ParenExprClass: {
1138 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1139 }
1140 case UnaryOperatorClass: {
1141 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1142 if (Exp->getOpcode() == UnaryOperator::Extension)
1143 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1144 break;
1145 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001146 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001147 case CStyleCastExprClass:
1148 // Handle casts with a destination that's a struct or union; this
1149 // deals with both the gcc no-op struct cast extension and the
1150 // cast-to-union extension.
1151 if (getType()->isRecordType())
1152 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1153 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001154 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001155 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001156}
1157
Chris Lattner4b009652007-07-25 00:24:17 +00001158/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001159/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001160
1161/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1162/// comma, etc
1163///
Chris Lattner4b009652007-07-25 00:24:17 +00001164/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1165/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1166/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001167
Eli Friedman7beeda62009-02-26 09:29:13 +00001168// CheckICE - This function does the fundamental ICE checking: the returned
1169// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1170// Note that to reduce code duplication, this helper does no evaluation
1171// itself; the caller checks whether the expression is evaluatable, and
1172// in the rare cases where CheckICE actually cares about the evaluated
1173// value, it calls into Evalute.
1174//
1175// Meanings of Val:
1176// 0: This expression is an ICE if it can be evaluated by Evaluate.
1177// 1: This expression is not an ICE, but if it isn't evaluated, it's
1178// a legal subexpression for an ICE. This return value is used to handle
1179// the comma operator in C99 mode.
1180// 2: This expression is not an ICE, and is not a legal subexpression for one.
1181
1182struct ICEDiag {
1183 unsigned Val;
1184 SourceLocation Loc;
1185
1186 public:
1187 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1188 ICEDiag() : Val(0) {}
1189};
1190
1191ICEDiag NoDiag() { return ICEDiag(); }
1192
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001193static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1194 Expr::EvalResult EVResult;
1195 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1196 !EVResult.Val.isInt()) {
1197 return ICEDiag(2, E->getLocStart());
1198 }
1199 return NoDiag();
1200}
1201
Eli Friedman7beeda62009-02-26 09:29:13 +00001202static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001203 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001204 if (!E->getType()->isIntegralType()) {
1205 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001206 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001207
1208 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001209 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001210 return ICEDiag(2, E->getLocStart());
1211 case Expr::ParenExprClass:
1212 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1213 case Expr::IntegerLiteralClass:
1214 case Expr::CharacterLiteralClass:
1215 case Expr::CXXBoolLiteralExprClass:
1216 case Expr::CXXZeroInitValueExprClass:
1217 case Expr::TypesCompatibleExprClass:
1218 case Expr::UnaryTypeTraitExprClass:
1219 return NoDiag();
1220 case Expr::CallExprClass:
1221 case Expr::CXXOperatorCallExprClass: {
1222 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001223 if (CE->isBuiltinCall(Ctx))
1224 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001225 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001226 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001227 case Expr::DeclRefExprClass:
1228 case Expr::QualifiedDeclRefExprClass:
1229 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1230 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001231 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001232 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001233 // C++ 7.1.5.1p2
1234 // A variable of non-volatile const-qualified integral or enumeration
1235 // type initialized by an ICE can be used in ICEs.
1236 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001237 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor4833ff02009-05-26 18:54:04 +00001238 if (Dcl->isInitKnownICE()) {
1239 // We have already checked whether this subexpression is an
1240 // integral constant expression.
1241 if (Dcl->isInitICE())
1242 return NoDiag();
1243 else
1244 return ICEDiag(2, E->getLocStart());
1245 }
1246
1247 if (const Expr *Init = Dcl->getInit()) {
1248 ICEDiag Result = CheckICE(Init, Ctx);
1249 // Cache the result of the ICE test.
1250 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1251 return Result;
1252 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001253 }
1254 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001255 return ICEDiag(2, E->getLocStart());
1256 case Expr::UnaryOperatorClass: {
1257 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001258 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001259 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001260 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001261 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001262 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001263 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001264 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001265 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001266 case UnaryOperator::Real:
1267 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001268 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001269 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001270 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1271 // Evaluate matches the proposed gcc behavior for cases like
1272 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1273 // compliance: we should warn earlier for offsetof expressions with
1274 // array subscripts that aren't ICEs, and if the array subscripts
1275 // are ICEs, the value of the offsetof must be an integer constant.
1276 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001277 }
Chris Lattner4b009652007-07-25 00:24:17 +00001278 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001279 case Expr::SizeOfAlignOfExprClass: {
1280 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1281 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1282 return ICEDiag(2, E->getLocStart());
1283 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001284 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001285 case Expr::BinaryOperatorClass: {
1286 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001287 switch (Exp->getOpcode()) {
1288 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001289 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001290 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001291 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001292 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001293 case BinaryOperator::Add:
1294 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001295 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001296 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001297 case BinaryOperator::LT:
1298 case BinaryOperator::GT:
1299 case BinaryOperator::LE:
1300 case BinaryOperator::GE:
1301 case BinaryOperator::EQ:
1302 case BinaryOperator::NE:
1303 case BinaryOperator::And:
1304 case BinaryOperator::Xor:
1305 case BinaryOperator::Or:
1306 case BinaryOperator::Comma: {
1307 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1308 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001309 if (Exp->getOpcode() == BinaryOperator::Div ||
1310 Exp->getOpcode() == BinaryOperator::Rem) {
1311 // Evaluate gives an error for undefined Div/Rem, so make sure
1312 // we don't evaluate one.
1313 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1314 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1315 if (REval == 0)
1316 return ICEDiag(1, E->getLocStart());
1317 if (REval.isSigned() && REval.isAllOnesValue()) {
1318 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1319 if (LEval.isMinSignedValue())
1320 return ICEDiag(1, E->getLocStart());
1321 }
1322 }
1323 }
1324 if (Exp->getOpcode() == BinaryOperator::Comma) {
1325 if (Ctx.getLangOptions().C99) {
1326 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1327 // if it isn't evaluated.
1328 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1329 return ICEDiag(1, E->getLocStart());
1330 } else {
1331 // In both C89 and C++, commas in ICEs are illegal.
1332 return ICEDiag(2, E->getLocStart());
1333 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001334 }
1335 if (LHSResult.Val >= RHSResult.Val)
1336 return LHSResult;
1337 return RHSResult;
1338 }
Chris Lattner4b009652007-07-25 00:24:17 +00001339 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001340 case BinaryOperator::LOr: {
1341 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1342 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1343 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1344 // Rare case where the RHS has a comma "side-effect"; we need
1345 // to actually check the condition to see whether the side
1346 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001347 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001348 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001349 return RHSResult;
1350 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001351 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001352
Eli Friedman7beeda62009-02-26 09:29:13 +00001353 if (LHSResult.Val >= RHSResult.Val)
1354 return LHSResult;
1355 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001356 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001357 }
Chris Lattner4b009652007-07-25 00:24:17 +00001358 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001359 case Expr::ImplicitCastExprClass:
1360 case Expr::CStyleCastExprClass:
1361 case Expr::CXXFunctionalCastExprClass: {
1362 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1363 if (SubExpr->getType()->isIntegralType())
1364 return CheckICE(SubExpr, Ctx);
1365 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1366 return NoDiag();
1367 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001368 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001369 case Expr::ConditionalOperatorClass: {
1370 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001371 // If the condition (ignoring parens) is a __builtin_constant_p call,
1372 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001373 // expression, and it is fully evaluated. This is an important GNU
1374 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001375 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001376 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001377 Expr::EvalResult EVResult;
1378 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1379 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001380 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001381 }
1382 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001383 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001384 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1385 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1386 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1387 if (CondResult.Val == 2)
1388 return CondResult;
1389 if (TrueResult.Val == 2)
1390 return TrueResult;
1391 if (FalseResult.Val == 2)
1392 return FalseResult;
1393 if (CondResult.Val == 1)
1394 return CondResult;
1395 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1396 return NoDiag();
1397 // Rare case where the diagnostics depend on which side is evaluated
1398 // Note that if we get here, CondResult is 0, and at least one of
1399 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001400 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001401 return FalseResult;
1402 }
1403 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001404 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001405 case Expr::CXXDefaultArgExprClass:
1406 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001407 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001408 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001409 }
Chris Lattner4b009652007-07-25 00:24:17 +00001410 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001411}
Chris Lattner4b009652007-07-25 00:24:17 +00001412
Eli Friedman7beeda62009-02-26 09:29:13 +00001413bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1414 SourceLocation *Loc, bool isEvaluated) const {
1415 ICEDiag d = CheckICE(this, Ctx);
1416 if (d.Val != 0) {
1417 if (Loc) *Loc = d.Loc;
1418 return false;
1419 }
1420 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001421 if (!Evaluate(EvalResult, Ctx))
1422 assert(0 && "ICE cannot be evaluated!");
1423 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1424 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001425 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001426 return true;
1427}
1428
Chris Lattner4b009652007-07-25 00:24:17 +00001429/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1430/// integer constant expression with the value zero, or if this is one that is
1431/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001432bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1433{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001434 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001435 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001436 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001437 // Check that it is a cast to void*.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001438 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001439 QualType Pointee = PT->getPointeeType();
1440 if (Pointee.getCVRQualifiers() == 0 &&
1441 Pointee->isVoidType() && // to void*
1442 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001443 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001444 }
Chris Lattner4b009652007-07-25 00:24:17 +00001445 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001446 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1447 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001448 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001449 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1450 // Accept ((void*)0) as a null pointer constant, as many other
1451 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001452 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001453 } else if (const CXXDefaultArgExpr *DefaultArg
1454 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001455 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001456 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001457 } else if (isa<GNUNullExpr>(this)) {
1458 // The GNU __null extension is always a null pointer constant.
1459 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001460 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001461
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001462 // C++0x nullptr_t is always a null pointer constant.
1463 if (getType()->isNullPtrType())
1464 return true;
1465
Steve Naroffa2e53222008-01-14 16:10:57 +00001466 // This expression must be an integer type.
1467 if (!getType()->isIntegerType())
1468 return false;
1469
Chris Lattner4b009652007-07-25 00:24:17 +00001470 // If we have an integer constant expression, we need to *evaluate* it and
1471 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001472 llvm::APSInt Result;
1473 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001474}
Steve Naroffc11705f2007-07-28 23:10:27 +00001475
Douglas Gregor531434b2009-05-02 02:18:30 +00001476FieldDecl *Expr::getBitField() {
Douglas Gregor34909522009-07-06 15:38:40 +00001477 Expr *E = this->IgnoreParens();
Douglas Gregor531434b2009-05-02 02:18:30 +00001478
Douglas Gregor81c29152008-10-29 00:13:59 +00001479 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001480 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001481 if (Field->isBitField())
1482 return Field;
1483
1484 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1485 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1486 return BinOp->getLHS()->getBitField();
1487
1488 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001489}
1490
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001491/// isArrow - Return true if the base expression is a pointer to vector,
1492/// return false if the base expression is a vector.
1493bool ExtVectorElementExpr::isArrow() const {
1494 return getBase()->getType()->isPointerType();
1495}
1496
Nate Begemanaf6ed502008-04-18 23:10:10 +00001497unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001498 if (const VectorType *VT = getType()->getAsVectorType())
1499 return VT->getNumElements();
1500 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001501}
1502
Nate Begemanc8e51f82008-05-09 06:41:27 +00001503/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001504bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001505 const char *compStr = Accessor->getName();
1506 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001507
1508 // Halving swizzles do not contain duplicate elements.
1509 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1510 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1511 return false;
1512
1513 // Advance past s-char prefix on hex swizzles.
Nate Begemane2ed6f72009-06-25 21:06:09 +00001514 if (*compStr == 's' || *compStr == 'S') {
Nate Begemana8e117c2009-01-18 02:01:21 +00001515 compStr++;
1516 length--;
1517 }
Steve Naroffba67f692007-07-30 03:29:09 +00001518
Chris Lattner58d3fa52008-11-19 07:55:04 +00001519 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001520 const char *s = compStr+i;
1521 for (const char c = *s++; *s; s++)
1522 if (c == *s)
1523 return true;
1524 }
1525 return false;
1526}
Chris Lattner42158e72007-08-02 23:36:59 +00001527
Nate Begemanc8e51f82008-05-09 06:41:27 +00001528/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001529void ExtVectorElementExpr::getEncodedElementAccess(
1530 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001531 const char *compStr = Accessor->getName();
Nate Begemane2ed6f72009-06-25 21:06:09 +00001532 if (*compStr == 's' || *compStr == 'S')
Nate Begeman1486b502009-01-18 01:47:54 +00001533 compStr++;
1534
1535 bool isHi = !strcmp(compStr, "hi");
1536 bool isLo = !strcmp(compStr, "lo");
1537 bool isEven = !strcmp(compStr, "even");
1538 bool isOdd = !strcmp(compStr, "odd");
1539
Nate Begemanc8e51f82008-05-09 06:41:27 +00001540 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1541 uint64_t Index;
1542
1543 if (isHi)
1544 Index = e + i;
1545 else if (isLo)
1546 Index = i;
1547 else if (isEven)
1548 Index = 2 * i;
1549 else if (isOdd)
1550 Index = 2 * i + 1;
1551 else
1552 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001553
Nate Begemana1ae7442008-05-13 21:03:02 +00001554 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001555 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001556}
1557
Steve Naroff4ed9d662007-09-27 14:38:14 +00001558// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001559ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001560 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001561 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001562 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001563 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001564 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001565 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001566 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001567 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001568 if (NumArgs) {
1569 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001570 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1571 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001572 LBracloc = LBrac;
1573 RBracloc = RBrac;
1574}
1575
Steve Naroff4ed9d662007-09-27 14:38:14 +00001576// constructor for class messages.
1577// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001578ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001579 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001580 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001581 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001582 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001583 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001584 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001585 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001586 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001587 if (NumArgs) {
1588 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001589 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1590 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001591 LBracloc = LBrac;
1592 RBracloc = RBrac;
1593}
1594
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001595// constructor for class messages.
1596ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1597 QualType retType, ObjCMethodDecl *mproto,
1598 SourceLocation LBrac, SourceLocation RBrac,
1599 Expr **ArgExprs, unsigned nargs)
1600: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1601MethodProto(mproto) {
1602 NumArgs = nargs;
1603 SubExprs = new Stmt*[NumArgs+1];
1604 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1605 if (NumArgs) {
1606 for (unsigned i = 0; i != NumArgs; ++i)
1607 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1608 }
1609 LBracloc = LBrac;
1610 RBracloc = RBrac;
1611}
1612
1613ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1614 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1615 switch (x & Flags) {
1616 default:
1617 assert(false && "Invalid ObjCMessageExpr.");
1618 case IsInstMeth:
1619 return ClassInfo(0, 0);
1620 case IsClsMethDeclUnknown:
1621 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1622 case IsClsMethDeclKnown: {
1623 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1624 return ClassInfo(D, D->getIdentifier());
1625 }
1626 }
1627}
1628
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001629void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1630 if (CI.first == 0 && CI.second == 0)
1631 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1632 else if (CI.first == 0)
1633 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1634 else
1635 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1636}
1637
1638
Chris Lattnerf624cd22007-10-25 00:29:32 +00001639bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001640 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001641}
1642
Nate Begeman23a73022009-08-12 02:28:50 +00001643void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1644 unsigned NumExprs) {
1645 if (SubExprs) C.Deallocate(SubExprs);
1646
1647 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor725e94b2009-04-16 00:01:45 +00001648 this->NumExprs = NumExprs;
1649 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Nate Begeman23a73022009-08-12 02:28:50 +00001650}
1651
1652void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1653 DestroyChildren(C);
1654 if (SubExprs) C.Deallocate(SubExprs);
1655 this->~ShuffleVectorExpr();
1656 C.Deallocate(this);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001657}
1658
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001659void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001660 // Override default behavior of traversing children. If this has a type
1661 // operand and the type is a variable-length array, the child iteration
1662 // will iterate over the size expression. However, this expression belongs
1663 // to the type, not to this, so we don't want to delete it.
1664 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001665 if (isArgumentType()) {
1666 this->~SizeOfAlignOfExpr();
1667 C.Deallocate(this);
1668 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001669 else
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001670 Expr::DoDestroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001671}
1672
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001673//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001674// DesignatedInitExpr
1675//===----------------------------------------------------------------------===//
1676
1677IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1678 assert(Kind == FieldDesignator && "Only valid on a field designator");
1679 if (Field.NameOrField & 0x01)
1680 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1681 else
1682 return getField()->getIdentifier();
1683}
1684
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001685DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1686 const Designator *Designators,
1687 SourceLocation EqualOrColonLoc,
1688 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001689 Expr **IndexExprs,
1690 unsigned NumIndexExprs,
1691 Expr *Init)
1692 : Expr(DesignatedInitExprClass, Ty,
1693 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001694 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001695 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001696 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001697
1698 // Record the initializer itself.
1699 child_iterator Child = child_begin();
1700 *Child++ = Init;
1701
1702 // Copy the designators and their subexpressions, computing
1703 // value-dependence along the way.
1704 unsigned IndexIdx = 0;
1705 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001706 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001707
1708 if (this->Designators[I].isArrayDesignator()) {
1709 // Compute type- and value-dependence.
1710 Expr *Index = IndexExprs[IndexIdx];
1711 ValueDependent = ValueDependent ||
1712 Index->isTypeDependent() || Index->isValueDependent();
1713
1714 // Copy the index expressions into permanent storage.
1715 *Child++ = IndexExprs[IndexIdx++];
1716 } else if (this->Designators[I].isArrayRangeDesignator()) {
1717 // Compute type- and value-dependence.
1718 Expr *Start = IndexExprs[IndexIdx];
1719 Expr *End = IndexExprs[IndexIdx + 1];
1720 ValueDependent = ValueDependent ||
1721 Start->isTypeDependent() || Start->isValueDependent() ||
1722 End->isTypeDependent() || End->isValueDependent();
1723
1724 // Copy the start/end expressions into permanent storage.
1725 *Child++ = IndexExprs[IndexIdx++];
1726 *Child++ = IndexExprs[IndexIdx++];
1727 }
1728 }
1729
1730 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001731}
1732
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001733DesignatedInitExpr *
1734DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1735 unsigned NumDesignators,
1736 Expr **IndexExprs, unsigned NumIndexExprs,
1737 SourceLocation ColonOrEqualLoc,
1738 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001739 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001740 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001741 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1742 ColonOrEqualLoc, UsesColonSyntax,
1743 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001744}
1745
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001746DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1747 unsigned NumIndexExprs) {
1748 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1749 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1750 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1751}
1752
1753void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1754 unsigned NumDesigs) {
1755 if (Designators)
1756 delete [] Designators;
1757
1758 Designators = new Designator[NumDesigs];
1759 NumDesignators = NumDesigs;
1760 for (unsigned I = 0; I != NumDesigs; ++I)
1761 Designators[I] = Desigs[I];
1762}
1763
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001764SourceRange DesignatedInitExpr::getSourceRange() const {
1765 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001766 Designator &First =
1767 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001768 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001769 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001770 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1771 else
1772 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1773 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001774 StartLoc =
1775 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001776 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1777}
1778
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001779Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1780 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1781 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1782 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001783 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1784 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1785}
1786
1787Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1788 assert(D.Kind == Designator::ArrayRangeDesignator &&
1789 "Requires array range designator");
1790 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1791 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001792 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1793 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1794}
1795
1796Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1797 assert(D.Kind == Designator::ArrayRangeDesignator &&
1798 "Requires array range designator");
1799 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1800 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001801 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1802 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1803}
1804
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001805/// \brief Replaces the designator at index @p Idx with the series
1806/// of designators in [First, Last).
1807void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1808 const Designator *First,
1809 const Designator *Last) {
1810 unsigned NumNewDesignators = Last - First;
1811 if (NumNewDesignators == 0) {
1812 std::copy_backward(Designators + Idx + 1,
1813 Designators + NumDesignators,
1814 Designators + Idx);
1815 --NumNewDesignators;
1816 return;
1817 } else if (NumNewDesignators == 1) {
1818 Designators[Idx] = *First;
1819 return;
1820 }
1821
1822 Designator *NewDesignators
1823 = new Designator[NumDesignators - 1 + NumNewDesignators];
1824 std::copy(Designators, Designators + Idx, NewDesignators);
1825 std::copy(First, Last, NewDesignators + Idx);
1826 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1827 NewDesignators + Idx + NumNewDesignators);
1828 delete [] Designators;
1829 Designators = NewDesignators;
1830 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1831}
1832
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001833void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001834 delete [] Designators;
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001835 Expr::DoDestroy(C);
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001836}
1837
Nate Begemane85f43d2009-08-10 23:49:36 +00001838ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
1839 Expr **exprs, unsigned nexprs,
1840 SourceLocation rparenloc)
1841: Expr(ParenListExprClass, QualType(),
1842 hasAnyTypeDependentArguments(exprs, nexprs),
1843 hasAnyValueDependentArguments(exprs, nexprs)),
1844 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
1845
1846 Exprs = new (C) Stmt*[nexprs];
1847 for (unsigned i = 0; i != nexprs; ++i)
1848 Exprs[i] = exprs[i];
1849}
1850
1851void ParenListExpr::DoDestroy(ASTContext& C) {
1852 DestroyChildren(C);
1853 if (Exprs) C.Deallocate(Exprs);
1854 this->~ParenListExpr();
1855 C.Deallocate(this);
1856}
1857
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001858//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001859// ExprIterator.
1860//===----------------------------------------------------------------------===//
1861
1862Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1863Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1864Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1865const Expr* ConstExprIterator::operator[](size_t idx) const {
1866 return cast<Expr>(I[idx]);
1867}
1868const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1869const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1870
1871//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001872// Child Iterators for iterating over subexpressions/substatements
1873//===----------------------------------------------------------------------===//
1874
1875// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001876Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1877Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001878
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001879// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001880Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1881Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001882
Steve Naroff6f786252008-06-02 23:03:37 +00001883// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001884Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1885Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001886
Fariborz Jahanian128cdc52009-08-20 17:02:02 +00001887// ObjCImplicitSetterGetterRefExpr
1888Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian1c4da452009-08-18 20:50:23 +00001889 return &Base;
1890}
Fariborz Jahanian128cdc52009-08-20 17:02:02 +00001891Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian1c4da452009-08-18 20:50:23 +00001892 return &Base+1;
1893}
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001894
Douglas Gregord8606632008-11-04 14:56:14 +00001895// ObjCSuperExpr
1896Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1897Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1898
Steve Naroff29d293b2009-07-24 17:54:45 +00001899// ObjCIsaExpr
1900Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1901Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1902
Chris Lattner69909292008-08-10 01:53:14 +00001903// PredefinedExpr
1904Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1905Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001906
1907// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001908Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1909Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001910
1911// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001912Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001913Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001914
1915// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001916Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1917Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001918
Chris Lattner1de66eb2007-08-26 03:42:43 +00001919// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001920Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1921Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001922
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001923// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001924Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1925Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001926
1927// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001928Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1929Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001930
1931// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001932Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1933Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001934
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001935// SizeOfAlignOfExpr
1936Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1937 // If this is of a type and the type is a VLA type (and not a typedef), the
1938 // size expression of the VLA needs to be treated as an executable expression.
1939 // Why isn't this weirdness documented better in StmtIterator?
1940 if (isArgumentType()) {
1941 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1942 getArgumentType().getTypePtr()))
1943 return child_iterator(T);
1944 return child_iterator();
1945 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001946 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001947}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001948Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1949 if (isArgumentType())
1950 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001951 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001952}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001953
1954// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001955Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001956 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001957}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001958Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001959 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001960}
1961
1962// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001963Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001964 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001965}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001966Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001967 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001968}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001969
1970// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001971Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1972Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001973
Nate Begemanaf6ed502008-04-18 23:10:10 +00001974// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001975Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1976Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001977
1978// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001979Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1980Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001981
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001982// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001983Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1984Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001985
1986// BinaryOperator
1987Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001988 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001989}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001990Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001991 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001992}
1993
1994// ConditionalOperator
1995Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001996 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001997}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001998Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001999 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002000}
2001
2002// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002003Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2004Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002005
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002006// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002007Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2008Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002009
2010// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002011Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2012 return child_iterator();
2013}
2014
2015Stmt::child_iterator TypesCompatibleExpr::child_end() {
2016 return child_iterator();
2017}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002018
2019// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002020Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2021Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002022
Douglas Gregorad4b3792008-11-29 04:51:27 +00002023// GNUNullExpr
2024Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2025Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2026
Eli Friedmand0e9d092008-05-14 19:38:39 +00002027// ShuffleVectorExpr
2028Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002029 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00002030}
2031Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002032 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00002033}
2034
Anders Carlsson36760332007-10-15 20:28:48 +00002035// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002036Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2037Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00002038
Anders Carlsson762b7c72007-08-31 04:56:16 +00002039// InitListExpr
2040Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002041 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002042}
2043Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002044 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002045}
2046
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002047// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002048Stmt::child_iterator DesignatedInitExpr::child_begin() {
2049 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2050 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002051 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2052}
2053Stmt::child_iterator DesignatedInitExpr::child_end() {
2054 return child_iterator(&*child_begin() + NumSubExprs);
2055}
2056
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002057// ImplicitValueInitExpr
2058Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2059 return child_iterator();
2060}
2061
2062Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2063 return child_iterator();
2064}
2065
Nate Begemane85f43d2009-08-10 23:49:36 +00002066// ParenListExpr
2067Stmt::child_iterator ParenListExpr::child_begin() {
2068 return &Exprs[0];
2069}
2070Stmt::child_iterator ParenListExpr::child_end() {
2071 return &Exprs[0]+NumExprs;
2072}
2073
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002074// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00002075Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00002076 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00002077}
2078Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00002079 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00002080}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002081
2082// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002083Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2084Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002085
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002086// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002087Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2088 return child_iterator();
2089}
2090Stmt::child_iterator ObjCSelectorExpr::child_end() {
2091 return child_iterator();
2092}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002093
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002094// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002095Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2096 return child_iterator();
2097}
2098Stmt::child_iterator ObjCProtocolExpr::child_end() {
2099 return child_iterator();
2100}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002101
Steve Naroffc39ca262007-09-18 23:55:05 +00002102// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002103Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002104 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002105}
2106Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002107 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002108}
2109
Steve Naroff52a81c02008-09-03 18:15:37 +00002110// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002111Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2112Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002113
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002114Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2115Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }