blob: daa98aeeabcdc5d1d56e768e02930f04cdfe741b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000025#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Primary Expressions.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerda8249e2008-06-07 22:13:43 +000032/// getValueAsApproximateDouble - This returns the value as an inaccurate
33/// double. Note that this may cause loss of precision, but is useful for
34/// debugging dumps, etc.
35double FloatingLiteral::getValueAsApproximateDouble() const {
36 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000037 bool ignored;
38 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
39 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000040 return V.convertToDouble();
41}
42
Chris Lattner2085fd62009-02-18 06:40:38 +000043StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
44 unsigned ByteLength, bool Wide,
45 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000046 const SourceLocation *Loc,
47 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000048 // Allocate enough space for the StringLiteral plus an array of locations for
49 // any concatenated string tokens.
50 void *Mem = C.Allocate(sizeof(StringLiteral)+
51 sizeof(SourceLocation)*(NumStrs-1),
52 llvm::alignof<StringLiteral>());
53 StringLiteral *SL = new (Mem) StringLiteral(Ty);
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000056 char *AStrData = new (C, 1) char[ByteLength];
57 memcpy(AStrData, StrData, ByteLength);
58 SL->StrData = AStrData;
59 SL->ByteLength = ByteLength;
60 SL->IsWide = Wide;
61 SL->TokLocs[0] = Loc[0];
62 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000063
Chris Lattner726e1682009-02-18 05:49:11 +000064 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000065 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
66 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000067}
68
Douglas Gregor673ecd62009-04-15 16:35:07 +000069StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
70 void *Mem = C.Allocate(sizeof(StringLiteral)+
71 sizeof(SourceLocation)*(NumStrs-1),
72 llvm::alignof<StringLiteral>());
73 StringLiteral *SL = new (Mem) StringLiteral(QualType());
74 SL->StrData = 0;
75 SL->ByteLength = 0;
76 SL->NumConcatenated = NumStrs;
77 return SL;
78}
79
Douglas Gregor42602bb2009-08-07 06:08:38 +000080void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000081 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +000082 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
Douglas Gregor673ecd62009-04-15 16:35:07 +000085void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
86 if (StrData)
87 C.Deallocate(const_cast<char*>(StrData));
88
89 char *AStrData = new (C, 1) char[Len];
90 memcpy(AStrData, Str, Len);
91 StrData = AStrData;
92 ByteLength = Len;
93}
94
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
96/// corresponds to, e.g. "sizeof" or "[pre]++".
97const char *UnaryOperator::getOpcodeStr(Opcode Op) {
98 switch (Op) {
99 default: assert(0 && "Unknown unary operator");
100 case PostInc: return "++";
101 case PostDec: return "--";
102 case PreInc: return "++";
103 case PreDec: return "--";
104 case AddrOf: return "&";
105 case Deref: return "*";
106 case Plus: return "+";
107 case Minus: return "-";
108 case Not: return "~";
109 case LNot: return "!";
110 case Real: return "__real";
111 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000113 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 }
115}
116
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000117UnaryOperator::Opcode
118UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
119 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000120 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000121 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
122 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
123 case OO_Amp: return AddrOf;
124 case OO_Star: return Deref;
125 case OO_Plus: return Plus;
126 case OO_Minus: return Minus;
127 case OO_Tilde: return Not;
128 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000129 }
130}
131
132OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
133 switch (Opc) {
134 case PostInc: case PreInc: return OO_PlusPlus;
135 case PostDec: case PreDec: return OO_MinusMinus;
136 case AddrOf: return OO_Amp;
137 case Deref: return OO_Star;
138 case Plus: return OO_Plus;
139 case Minus: return OO_Minus;
140 case Not: return OO_Tilde;
141 case LNot: return OO_Exclaim;
142 default: return OO_None;
143 }
144}
145
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147//===----------------------------------------------------------------------===//
148// Postfix Operators.
149//===----------------------------------------------------------------------===//
150
Ted Kremenek668bf912009-02-09 20:51:47 +0000151CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000152 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000153 : Expr(SC, t,
154 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000155 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000156 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000157
158 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000159 SubExprs[FN] = fn;
160 for (unsigned i = 0; i != numargs; ++i)
161 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000162
Douglas Gregorb4609802008-11-14 16:09:21 +0000163 RParenLoc = rparenloc;
164}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000165
Ted Kremenek668bf912009-02-09 20:51:47 +0000166CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
167 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000168 : Expr(CallExprClass, t,
169 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000170 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000171 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000172
173 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000174 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000176 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 RParenLoc = rparenloc;
179}
180
Argyrios Kyrtzidisba0a9002009-07-14 03:19:21 +0000181CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
182 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000183 SubExprs = new (C) Stmt*[1];
184}
185
Douglas Gregor42602bb2009-08-07 06:08:38 +0000186void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000187 DestroyChildren(C);
188 if (SubExprs) C.Deallocate(SubExprs);
189 this->~CallExpr();
190 C.Deallocate(this);
191}
192
Zhongxing Xua0042542009-07-17 07:29:51 +0000193FunctionDecl *CallExpr::getDirectCallee() {
194 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000195 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000196 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000197
198 return 0;
199}
200
Chris Lattnerd18b3292007-12-28 05:25:02 +0000201/// setNumArgs - This changes the number of arguments present in this call.
202/// Any orphaned expressions are deleted by this, and any new operands are set
203/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000204void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000205 // No change, just return.
206 if (NumArgs == getNumArgs()) return;
207
208 // If shrinking # arguments, just delete the extras and forgot them.
209 if (NumArgs < getNumArgs()) {
210 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000211 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000212 this->NumArgs = NumArgs;
213 return;
214 }
215
216 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000217 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000218 // Copy over args.
219 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
220 NewSubExprs[i] = SubExprs[i];
221 // Null out new args.
222 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
223 NewSubExprs[i] = 0;
224
Douglas Gregor88c9a462009-04-17 21:46:47 +0000225 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000226 SubExprs = NewSubExprs;
227 this->NumArgs = NumArgs;
228}
229
Chris Lattnercb888962008-10-06 05:00:53 +0000230/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
231/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000232unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000233 // All simple function calls (e.g. func()) are implicitly cast to pointer to
234 // function. As a result, we try and obtain the DeclRefExpr from the
235 // ImplicitCastExpr.
236 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
237 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000238 return 0;
239
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000240 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
241 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000242 return 0;
243
Anders Carlssonbcba2012008-01-31 02:13:57 +0000244 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
245 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000246 return 0;
247
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000248 if (!FDecl->getIdentifier())
249 return 0;
250
Douglas Gregor3c385e52009-02-14 18:57:46 +0000251 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000252}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000253
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000254QualType CallExpr::getCallReturnType() const {
255 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000256 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000257 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000258 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000259 CalleeType = BPT->getPointeeType();
260
261 const FunctionType *FnType = CalleeType->getAsFunctionType();
262 return FnType->getResultType();
263}
Chris Lattnercb888962008-10-06 05:00:53 +0000264
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000265MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
266 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregorc4bf26f2009-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 Gregor83f6faf2009-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 Gregorc4bf26f2009-09-01 00:37:14 +0000275 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregor83f6faf2009-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 Gregorc4bf26f2009-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 Gregor83f6faf2009-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 Gregorc4bf26f2009-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 Gregor83f6faf2009-08-31 23:41:50 +0000308 std::size_t Size = sizeof(MemberExpr);
309 if (qual != 0)
310 Size += sizeof(NameQualifier);
311
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000312 if (has_explicit)
313 Size += sizeof(ExplicitTemplateArgumentList) +
314 sizeof(TemplateArgument) * numtargs;
315
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000316 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000317 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
318 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000319 ty);
320}
321
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000322const char *CastExpr::getCastKindName() const {
323 switch (getCastKind()) {
324 case CastExpr::CK_Unknown:
325 return "Unknown";
326 case CastExpr::CK_BitCast:
327 return "BitCast";
328 case CastExpr::CK_NoOp:
329 return "NoOp";
330 case CastExpr::CK_DerivedToBase:
331 return "DerivedToBase";
332 case CastExpr::CK_Dynamic:
333 return "Dynamic";
334 case CastExpr::CK_ToUnion:
335 return "ToUnion";
336 case CastExpr::CK_ArrayToPointerDecay:
337 return "ArrayToPointerDecay";
338 case CastExpr::CK_FunctionToPointerDecay:
339 return "FunctionToPointerDecay";
340 case CastExpr::CK_NullToMemberPointer:
341 return "NullToMemberPointer";
342 case CastExpr::CK_BaseToDerivedMemberPointer:
343 return "BaseToDerivedMemberPointer";
344 case CastExpr::CK_UserDefinedConversion:
345 return "UserDefinedConversion";
346 case CastExpr::CK_ConstructorConversion:
347 return "ConstructorConversion";
348 }
349
350 assert(0 && "Unhandled cast kind!");
351 return 0;
352}
353
Reid Spencer5f016e22007-07-11 17:01:13 +0000354/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
355/// corresponds to, e.g. "<<=".
356const char *BinaryOperator::getOpcodeStr(Opcode Op) {
357 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000358 case PtrMemD: return ".*";
359 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 case Mul: return "*";
361 case Div: return "/";
362 case Rem: return "%";
363 case Add: return "+";
364 case Sub: return "-";
365 case Shl: return "<<";
366 case Shr: return ">>";
367 case LT: return "<";
368 case GT: return ">";
369 case LE: return "<=";
370 case GE: return ">=";
371 case EQ: return "==";
372 case NE: return "!=";
373 case And: return "&";
374 case Xor: return "^";
375 case Or: return "|";
376 case LAnd: return "&&";
377 case LOr: return "||";
378 case Assign: return "=";
379 case MulAssign: return "*=";
380 case DivAssign: return "/=";
381 case RemAssign: return "%=";
382 case AddAssign: return "+=";
383 case SubAssign: return "-=";
384 case ShlAssign: return "<<=";
385 case ShrAssign: return ">>=";
386 case AndAssign: return "&=";
387 case XorAssign: return "^=";
388 case OrAssign: return "|=";
389 case Comma: return ",";
390 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000391
392 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000393}
394
Douglas Gregor063daf62009-03-13 18:40:31 +0000395BinaryOperator::Opcode
396BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
397 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000398 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000399 case OO_Plus: return Add;
400 case OO_Minus: return Sub;
401 case OO_Star: return Mul;
402 case OO_Slash: return Div;
403 case OO_Percent: return Rem;
404 case OO_Caret: return Xor;
405 case OO_Amp: return And;
406 case OO_Pipe: return Or;
407 case OO_Equal: return Assign;
408 case OO_Less: return LT;
409 case OO_Greater: return GT;
410 case OO_PlusEqual: return AddAssign;
411 case OO_MinusEqual: return SubAssign;
412 case OO_StarEqual: return MulAssign;
413 case OO_SlashEqual: return DivAssign;
414 case OO_PercentEqual: return RemAssign;
415 case OO_CaretEqual: return XorAssign;
416 case OO_AmpEqual: return AndAssign;
417 case OO_PipeEqual: return OrAssign;
418 case OO_LessLess: return Shl;
419 case OO_GreaterGreater: return Shr;
420 case OO_LessLessEqual: return ShlAssign;
421 case OO_GreaterGreaterEqual: return ShrAssign;
422 case OO_EqualEqual: return EQ;
423 case OO_ExclaimEqual: return NE;
424 case OO_LessEqual: return LE;
425 case OO_GreaterEqual: return GE;
426 case OO_AmpAmp: return LAnd;
427 case OO_PipePipe: return LOr;
428 case OO_Comma: return Comma;
429 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000430 }
431}
432
433OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
434 static const OverloadedOperatorKind OverOps[] = {
435 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
436 OO_Star, OO_Slash, OO_Percent,
437 OO_Plus, OO_Minus,
438 OO_LessLess, OO_GreaterGreater,
439 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
440 OO_EqualEqual, OO_ExclaimEqual,
441 OO_Amp,
442 OO_Caret,
443 OO_Pipe,
444 OO_AmpAmp,
445 OO_PipePipe,
446 OO_Equal, OO_StarEqual,
447 OO_SlashEqual, OO_PercentEqual,
448 OO_PlusEqual, OO_MinusEqual,
449 OO_LessLessEqual, OO_GreaterGreaterEqual,
450 OO_AmpEqual, OO_CaretEqual,
451 OO_PipeEqual,
452 OO_Comma
453 };
454 return OverOps[Opc];
455}
456
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000457InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000458 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000459 SourceLocation rbraceloc)
Douglas Gregor9ea62762009-05-21 23:17:49 +0000460 : Expr(InitListExprClass, QualType(),
461 hasAnyTypeDependentArguments(initExprs, numInits),
462 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000463 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000464 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000465
466 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000467}
Reid Spencer5f016e22007-07-11 17:01:13 +0000468
Douglas Gregorfa219202009-03-20 23:58:33 +0000469void InitListExpr::reserveInits(unsigned NumInits) {
470 if (NumInits > InitExprs.size())
471 InitExprs.reserve(NumInits);
472}
473
Douglas Gregor4c678342009-01-28 21:54:33 +0000474void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000475 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000476 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000477 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000478 InitExprs.resize(NumInits, 0);
479}
480
481Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
482 if (Init >= InitExprs.size()) {
483 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
484 InitExprs.back() = expr;
485 return 0;
486 }
487
488 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
489 InitExprs[Init] = expr;
490 return Result;
491}
492
Steve Naroffbfdcae62008-09-04 15:31:07 +0000493/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000494///
495const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000496 return getType()->getAs<BlockPointerType>()->
Steve Naroff4eb206b2008-09-03 18:15:37 +0000497 getPointeeType()->getAsFunctionType();
498}
499
Steve Naroff56ee6892008-10-08 17:01:13 +0000500SourceLocation BlockExpr::getCaretLocation() const {
501 return TheBlock->getCaretLocation();
502}
Douglas Gregor72971342009-04-18 00:02:19 +0000503const Stmt *BlockExpr::getBody() const {
504 return TheBlock->getBody();
505}
506Stmt *BlockExpr::getBody() {
507 return TheBlock->getBody();
508}
Steve Naroff56ee6892008-10-08 17:01:13 +0000509
510
Reid Spencer5f016e22007-07-11 17:01:13 +0000511//===----------------------------------------------------------------------===//
512// Generic Expression Routines
513//===----------------------------------------------------------------------===//
514
Chris Lattner026dc962009-02-14 07:37:35 +0000515/// isUnusedResultAWarning - Return true if this immediate expression should
516/// be warned about if the result is unused. If so, fill in Loc and Ranges
517/// with location to warn on and the source range[s] to report with the
518/// warning.
519bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000520 SourceRange &R2) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000521 // Don't warn if the expr is type dependent. The type could end up
522 // instantiating to void.
523 if (isTypeDependent())
524 return false;
525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 switch (getStmtClass()) {
527 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000528 Loc = getExprLoc();
529 R1 = getSourceRange();
530 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000532 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000533 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 case UnaryOperatorClass: {
535 const UnaryOperator *UO = cast<UnaryOperator>(this);
536
537 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000538 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 case UnaryOperator::PostInc:
540 case UnaryOperator::PostDec:
541 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000542 case UnaryOperator::PreDec: // ++/--
543 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 case UnaryOperator::Deref:
545 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000546 if (getType().isVolatileQualified())
547 return false;
548 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 case UnaryOperator::Real:
550 case UnaryOperator::Imag:
551 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000552 if (UO->getSubExpr()->getType().isVolatileQualified())
553 return false;
554 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 case UnaryOperator::Extension:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000556 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 }
Chris Lattner026dc962009-02-14 07:37:35 +0000558 Loc = UO->getOperatorLoc();
559 R1 = UO->getSubExpr()->getSourceRange();
560 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000562 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000563 const BinaryOperator *BO = cast<BinaryOperator>(this);
564 // Consider comma to have side effects if the LHS or RHS does.
565 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
567 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000568
Chris Lattner026dc962009-02-14 07:37:35 +0000569 if (BO->isAssignmentOp())
570 return false;
571 Loc = BO->getOperatorLoc();
572 R1 = BO->getLHS()->getSourceRange();
573 R2 = BO->getRHS()->getSourceRange();
574 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000575 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000576 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000577 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000578
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000579 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000580 // The condition must be evaluated, but if either the LHS or RHS is a
581 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000582 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor68584ed2009-06-18 16:11:24 +0000583 if (Exp->getLHS() &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000584 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000585 return true;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000586 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000587 }
588
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000590 // If the base pointer or element is to a volatile pointer/field, accessing
591 // it is a side effect.
592 if (getType().isVolatileQualified())
593 return false;
594 Loc = cast<MemberExpr>(this)->getMemberLoc();
595 R1 = SourceRange(Loc, Loc);
596 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
597 return true;
598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 case ArraySubscriptExprClass:
600 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000601 // it is a side effect.
602 if (getType().isVolatileQualified())
603 return false;
604 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
605 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
606 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
607 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000608
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000610 case CXXOperatorCallExprClass:
611 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000612 // If this is a direct call, get the callee.
613 const CallExpr *CE = cast<CallExpr>(this);
614 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
615 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
616 // If the callee has attribute pure, const, or warn_unused_result, warn
617 // about it. void foo() { strlen("bar"); } should warn.
618 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000619 if (FD->getAttr<WarnUnusedResultAttr>() ||
620 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000621 Loc = CE->getCallee()->getLocStart();
622 R1 = CE->getCallee()->getSourceRange();
623
624 if (unsigned NumArgs = CE->getNumArgs())
625 R2 = SourceRange(CE->getArg(0)->getLocStart(),
626 CE->getArg(NumArgs-1)->getLocEnd());
627 return true;
628 }
629 }
630 return false;
631 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000632 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000633 return false;
Chris Lattnera50089e2009-08-16 16:45:18 +0000634
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000635 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000636#if 0
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000637 const ObjCImplicitSetterGetterRefExpr *Ref =
638 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000639 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000640 Loc = Ref->getLocation();
641 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
642 if (Ref->getBase())
643 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000644#else
645 Loc = getExprLoc();
646 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000647#endif
648 return true;
649 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000650 case StmtExprClass: {
651 // Statement exprs don't logically have side effects themselves, but are
652 // sometimes used in macros in ways that give them a type that is unused.
653 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
654 // however, if the result of the stmt expr is dead, we don't want to emit a
655 // warning.
656 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
657 if (!CS->body_empty())
658 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000659 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000660
661 Loc = cast<StmtExpr>(this)->getLParenLoc();
662 R1 = getSourceRange();
663 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000664 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000665 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000666 // If this is an explicit cast to void, allow it. People do this when they
667 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000668 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000669 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000670 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
671 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
672 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000673 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // If this is a cast to void, check the operand. Otherwise, the result of
675 // the cast is unused.
676 if (getType()->isVoidType())
Douglas Gregor68584ed2009-06-18 16:11:24 +0000677 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000678 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner026dc962009-02-14 07:37:35 +0000679 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
680 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
681 return true;
682
Eli Friedman4be1f472008-05-19 21:24:43 +0000683 case ImplicitCastExprClass:
684 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000685 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000686 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000687
Chris Lattner04421082008-04-08 04:40:51 +0000688 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000689 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000690 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000691
692 case CXXNewExprClass:
693 // FIXME: In theory, there might be new expressions that don't have side
694 // effects (e.g. a placement new with an uninitialized POD).
695 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000696 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000697 case CXXBindTemporaryExprClass:
698 return cast<CXXBindTemporaryExpr>(this)
699 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000700 case CXXExprWithTemporariesClass:
701 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000702 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000703 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
Douglas Gregorba7e2102008-10-22 15:04:37 +0000706/// DeclCanBeLvalue - Determine whether the given declaration can be
707/// an lvalue. This is a helper routine for isLvalue.
708static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000709 // C++ [temp.param]p6:
710 // A non-type non-reference template-parameter is not an lvalue.
711 if (const NonTypeTemplateParmDecl *NTTParm
712 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
713 return NTTParm->getType()->isReferenceType();
714
Douglas Gregor44b43212008-12-11 16:49:14 +0000715 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000716 // C++ 3.10p2: An lvalue refers to an object or function.
717 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor83314aa2009-07-08 20:55:45 +0000718 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
719 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000720}
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
723/// incomplete type other than void. Nonarray expressions that can be lvalues:
724/// - name, where name must be a variable
725/// - e[i]
726/// - (e), where e must be an lvalue
727/// - e.name, where e must be an lvalue
728/// - e->name
729/// - *e, the type of e cannot be a function type
730/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000731/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000732/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000733///
Chris Lattner28be73f2008-07-26 21:30:36 +0000734Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000735 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
736
737 isLvalueResult Res = isLvalueInternal(Ctx);
738 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
739 return Res;
740
Douglas Gregor98cd5992008-10-21 23:43:52 +0000741 // first, check the type (C99 6.3.2.1). Expressions with function
742 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000743 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 return LV_NotObjectType;
745
Steve Naroffacb818a2008-02-10 01:39:04 +0000746 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000747 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000748 return LV_IncompleteVoidType;
749
Eli Friedman53202852009-05-03 22:36:05 +0000750 return LV_Valid;
751}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000752
Eli Friedman53202852009-05-03 22:36:05 +0000753// Check whether the expression can be sanely treated like an l-value
754Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000756 case StringLiteralClass: // C99 6.5.1p4
757 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000758 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
760 // For vectors, make sure base is an lvalue (i.e. not a function call).
761 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000762 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000764 case DeclRefExprClass:
765 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000766 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
767 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 return LV_Valid;
769 break;
Chris Lattner41110242008-06-17 18:05:57 +0000770 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000771 case BlockDeclRefExprClass: {
772 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000773 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000774 return LV_Valid;
775 break;
776 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000777 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000779 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
780 NamedDecl *Member = m->getMemberDecl();
781 // C++ [expr.ref]p4:
782 // If E2 is declared to have type "reference to T", then E1.E2
783 // is an lvalue.
784 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
785 if (Value->getType()->isReferenceType())
786 return LV_Valid;
787
788 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000789 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000790 return LV_Valid;
791
792 // -- If E2 is a non-static data member [...]. If E1 is an
793 // lvalue, then E1.E2 is an lvalue.
794 if (isa<FieldDecl>(Member))
795 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
796
797 // -- If it refers to a static member function [...], then
798 // E1.E2 is an lvalue.
799 // -- Otherwise, if E1.E2 refers to a non-static member
800 // function [...], then E1.E2 is not an lvalue.
801 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
802 return Method->isStatic()? LV_Valid : LV_MemberFunction;
803
804 // -- If E2 is a member enumerator [...], the expression E1.E2
805 // is not an lvalue.
806 if (isa<EnumConstantDecl>(Member))
807 return LV_InvalidExpression;
808
809 // Not an lvalue.
810 return LV_InvalidExpression;
811 }
812
813 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000814 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000815 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000816 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000818 return LV_Valid; // C99 6.5.3p4
819
820 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000821 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
822 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000823 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000824
825 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
826 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
827 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
828 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000830 case ImplicitCastExprClass:
831 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
832 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000834 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000835 case BinaryOperatorClass:
836 case CompoundAssignOperatorClass: {
837 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000838
839 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
840 BinOp->getOpcode() == BinaryOperator::Comma)
841 return BinOp->getRHS()->isLvalue(Ctx);
842
Sebastian Redl22460502009-02-07 00:15:38 +0000843 // C++ [expr.mptr.oper]p6
844 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
845 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
846 !BinOp->getType()->isFunctionType())
847 return BinOp->getLHS()->isLvalue(Ctx);
848
Douglas Gregorbf3af052008-11-13 20:12:29 +0000849 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000850 return LV_InvalidExpression;
851
Douglas Gregorbf3af052008-11-13 20:12:29 +0000852 if (Ctx.getLangOptions().CPlusPlus)
853 // C++ [expr.ass]p1:
854 // The result of an assignment operation [...] is an lvalue.
855 return LV_Valid;
856
857
858 // C99 6.5.16:
859 // An assignment expression [...] is not an lvalue.
860 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000861 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000862 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000863 case CXXOperatorCallExprClass:
864 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000865 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000866 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000867 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000868 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
869 if (ReturnType->isLValueReferenceType())
870 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000871
Douglas Gregor9d293df2008-10-28 00:22:11 +0000872 break;
873 }
Steve Naroffe6386392007-12-05 04:00:10 +0000874 case CompoundLiteralExprClass: // C99 6.5.2.5p5
875 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000876 case ChooseExprClass:
877 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000878 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000879 case ExtVectorElementExprClass:
880 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000881 return LV_DuplicateVectorComponents;
882 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000883 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
884 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000885 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
886 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000887 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000888 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000889 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000890 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +0000891 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000892 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000893 case CXXConditionDeclExprClass:
894 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000895 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000896 case CXXFunctionalCastExprClass:
897 case CXXStaticCastExprClass:
898 case CXXDynamicCastExprClass:
899 case CXXReinterpretCastExprClass:
900 case CXXConstCastExprClass:
901 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000902 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000903 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
904 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000905 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
906 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000907 return LV_Valid;
908 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000909 case CXXTypeidExprClass:
910 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
911 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +0000912 case CXXBindTemporaryExprClass:
913 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
914 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +0000915 case ConditionalOperatorClass: {
916 // Complicated handling is only for C++.
917 if (!Ctx.getLangOptions().CPlusPlus)
918 return LV_InvalidExpression;
919
920 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
921 // everywhere there's an object converted to an rvalue. Also, any other
922 // casts should be wrapped by ImplicitCastExprs. There's just the special
923 // case involving throws to work out.
924 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000925 Expr *True = Cond->getTrueExpr();
926 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +0000927 // C++0x 5.16p2
928 // If either the second or the third operand has type (cv) void, [...]
929 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000930 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +0000931 return LV_InvalidExpression;
932
933 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +0000934 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +0000935 return LV_InvalidExpression;
936
937 // That's it.
938 return LV_Valid;
939 }
940
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 default:
942 break;
943 }
944 return LV_InvalidExpression;
945}
946
947/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
948/// does not have an incomplete type, does not have a const-qualified type, and
949/// if it is a structure or union, does not have any member (including,
950/// recursively, any member or element of all contained aggregates or unions)
951/// with a const-qualified type.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000952Expr::isModifiableLvalueResult
953Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +0000954 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000955
956 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000957 case LV_Valid:
958 // C++ 3.10p11: Functions cannot be modified, but pointers to
959 // functions can be modifiable.
960 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
961 return MLV_NotObjectType;
962 break;
963
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 case LV_NotObjectType: return MLV_NotObjectType;
965 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000966 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000967 case LV_InvalidExpression:
968 // If the top level is a C-style cast, and the subexpression is a valid
969 // lvalue, then this is probably a use of the old-school "cast as lvalue"
970 // GCC extension. We don't support it, but we want to produce good
971 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000972 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
973 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
974 if (Loc)
975 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +0000976 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +0000977 }
978 }
Chris Lattnerca354fa2008-11-17 19:51:54 +0000979 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000980 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000982
983 // The following is illegal:
984 // void takeclosure(void (^C)(void));
985 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
986 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000987 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000988 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
989 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
990 return MLV_NotBlockQualified;
991 }
992
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000993 QualType CT = Ctx.getCanonicalType(getType());
994
995 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000997 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000999 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 return MLV_IncompleteType;
1001
Ted Kremenek6217b802009-07-29 21:53:49 +00001002 if (const RecordType *r = CT->getAs<RecordType>()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 if (r->hasConstFields())
1004 return MLV_ConstQualified;
1005 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +00001006
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +00001007 // Assigning to an 'implicit' property?
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001008 else if (isa<ObjCImplicitSetterGetterRefExpr>(this)) {
1009 const ObjCImplicitSetterGetterRefExpr* Expr =
1010 cast<ObjCImplicitSetterGetterRefExpr>(this);
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001011 if (Expr->getSetterMethod() == 0)
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +00001012 return MLV_NoSetterProperty;
1013 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 return MLV_Valid;
1015}
1016
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001017/// isOBJCGCCandidate - Check if an expression is objc gc'able.
1018///
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001019bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001020 switch (getStmtClass()) {
1021 default:
1022 return false;
1023 case ObjCIvarRefExprClass:
1024 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001025 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001026 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001027 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001028 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001029 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001030 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001031 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001032 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001033 case DeclRefExprClass:
1034 case QualifiedDeclRefExprClass: {
1035 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001036 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1037 if (VD->hasGlobalStorage())
1038 return true;
1039 QualType T = VD->getType();
1040 // dereferencing to an object pointer is always a gc'able candidate
1041 if (T->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00001042 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001043 return true;
1044
1045 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001046 return false;
1047 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001048 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001049 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001050 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001051 }
1052 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001053 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001054 }
1055}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001056Expr* Expr::IgnoreParens() {
1057 Expr* E = this;
1058 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1059 E = P->getSubExpr();
1060
1061 return E;
1062}
1063
Chris Lattner56f34942008-02-13 01:02:39 +00001064/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1065/// or CastExprs or ImplicitCastExprs, returning their operand.
1066Expr *Expr::IgnoreParenCasts() {
1067 Expr *E = this;
1068 while (true) {
1069 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1070 E = P->getSubExpr();
1071 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1072 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001073 else
1074 return E;
1075 }
1076}
1077
Chris Lattnerecdd8412009-03-13 17:28:01 +00001078/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1079/// value (including ptr->int casts of the same size). Strip off any
1080/// ParenExpr or CastExprs, returning their operand.
1081Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1082 Expr *E = this;
1083 while (true) {
1084 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1085 E = P->getSubExpr();
1086 continue;
1087 }
1088
1089 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1090 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1091 // ptr<->int casts of the same width. We also ignore all identify casts.
1092 Expr *SE = P->getSubExpr();
1093
1094 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1095 E = SE;
1096 continue;
1097 }
1098
1099 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1100 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1101 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1102 E = SE;
1103 continue;
1104 }
1105 }
1106
1107 return E;
1108 }
1109}
1110
1111
Douglas Gregor898574e2008-12-05 23:32:09 +00001112/// hasAnyTypeDependentArguments - Determines if any of the expressions
1113/// in Exprs is type-dependent.
1114bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1115 for (unsigned I = 0; I < NumExprs; ++I)
1116 if (Exprs[I]->isTypeDependent())
1117 return true;
1118
1119 return false;
1120}
1121
1122/// hasAnyValueDependentArguments - Determines if any of the expressions
1123/// in Exprs is value-dependent.
1124bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1125 for (unsigned I = 0; I < NumExprs; ++I)
1126 if (Exprs[I]->isValueDependent())
1127 return true;
1128
1129 return false;
1130}
1131
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001132bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001133 // This function is attempting whether an expression is an initializer
1134 // which can be evaluated at compile-time. isEvaluatable handles most
1135 // of the cases, but it can't deal with some initializer-specific
1136 // expressions, and it can't deal with aggregates; we deal with those here,
1137 // and fall back to isEvaluatable for the other cases.
1138
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001139 // FIXME: This function assumes the variable being assigned to
1140 // isn't a reference type!
1141
Anders Carlssone8a32b82008-11-24 05:23:59 +00001142 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001143 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001144 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001145 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001146 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001147 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001148 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001149 // This handles gcc's extension that allows global initializers like
1150 // "struct x {int x;} x = (struct x) {};".
1151 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001152 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001153 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001154 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001155 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001156 // FIXME: This doesn't deal with fields with reference types correctly.
1157 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1158 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001159 const InitListExpr *Exp = cast<InitListExpr>(this);
1160 unsigned numInits = Exp->getNumInits();
1161 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001162 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001163 return false;
1164 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001165 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001166 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001167 case ImplicitValueInitExprClass:
1168 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001169 case ParenExprClass: {
1170 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1171 }
1172 case UnaryOperatorClass: {
1173 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1174 if (Exp->getOpcode() == UnaryOperator::Extension)
1175 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1176 break;
1177 }
Chris Lattner81045d82009-04-21 05:19:11 +00001178 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001179 case CStyleCastExprClass:
1180 // Handle casts with a destination that's a struct or union; this
1181 // deals with both the gcc no-op struct cast extension and the
1182 // cast-to-union extension.
1183 if (getType()->isRecordType())
1184 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1185 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001186 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001187 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001188}
1189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001191/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
1193/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1194/// comma, etc
1195///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001196/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1197/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1198/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001199
Eli Friedmane28d7192009-02-26 09:29:13 +00001200// CheckICE - This function does the fundamental ICE checking: the returned
1201// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1202// Note that to reduce code duplication, this helper does no evaluation
1203// itself; the caller checks whether the expression is evaluatable, and
1204// in the rare cases where CheckICE actually cares about the evaluated
1205// value, it calls into Evalute.
1206//
1207// Meanings of Val:
1208// 0: This expression is an ICE if it can be evaluated by Evaluate.
1209// 1: This expression is not an ICE, but if it isn't evaluated, it's
1210// a legal subexpression for an ICE. This return value is used to handle
1211// the comma operator in C99 mode.
1212// 2: This expression is not an ICE, and is not a legal subexpression for one.
1213
1214struct ICEDiag {
1215 unsigned Val;
1216 SourceLocation Loc;
1217
1218 public:
1219 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1220 ICEDiag() : Val(0) {}
1221};
1222
1223ICEDiag NoDiag() { return ICEDiag(); }
1224
Eli Friedman60ce9632009-02-27 04:07:58 +00001225static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1226 Expr::EvalResult EVResult;
1227 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1228 !EVResult.Val.isInt()) {
1229 return ICEDiag(2, E->getLocStart());
1230 }
1231 return NoDiag();
1232}
1233
Eli Friedmane28d7192009-02-26 09:29:13 +00001234static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001235 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001236 if (!E->getType()->isIntegralType()) {
1237 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001238 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001239
1240 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001242 return ICEDiag(2, E->getLocStart());
1243 case Expr::ParenExprClass:
1244 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1245 case Expr::IntegerLiteralClass:
1246 case Expr::CharacterLiteralClass:
1247 case Expr::CXXBoolLiteralExprClass:
1248 case Expr::CXXZeroInitValueExprClass:
1249 case Expr::TypesCompatibleExprClass:
1250 case Expr::UnaryTypeTraitExprClass:
1251 return NoDiag();
1252 case Expr::CallExprClass:
1253 case Expr::CXXOperatorCallExprClass: {
1254 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001255 if (CE->isBuiltinCall(Ctx))
1256 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001257 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001258 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001259 case Expr::DeclRefExprClass:
1260 case Expr::QualifiedDeclRefExprClass:
1261 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1262 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001263 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001264 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001265 // C++ 7.1.5.1p2
1266 // A variable of non-volatile const-qualified integral or enumeration
1267 // type initialized by an ICE can be used in ICEs.
1268 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001269 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor78d15832009-05-26 18:54:04 +00001270 if (Dcl->isInitKnownICE()) {
1271 // We have already checked whether this subexpression is an
1272 // integral constant expression.
1273 if (Dcl->isInitICE())
1274 return NoDiag();
1275 else
1276 return ICEDiag(2, E->getLocStart());
1277 }
1278
1279 if (const Expr *Init = Dcl->getInit()) {
1280 ICEDiag Result = CheckICE(Init, Ctx);
1281 // Cache the result of the ICE test.
1282 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1283 return Result;
1284 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001285 }
1286 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001287 return ICEDiag(2, E->getLocStart());
1288 case Expr::UnaryOperatorClass: {
1289 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001292 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001294 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001298 case UnaryOperator::Real:
1299 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001300 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001301 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001302 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1303 // Evaluate matches the proposed gcc behavior for cases like
1304 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1305 // compliance: we should warn earlier for offsetof expressions with
1306 // array subscripts that aren't ICEs, and if the array subscripts
1307 // are ICEs, the value of the offsetof must be an integer constant.
1308 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001311 case Expr::SizeOfAlignOfExprClass: {
1312 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1313 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1314 return ICEDiag(2, E->getLocStart());
1315 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001317 case Expr::BinaryOperatorClass: {
1318 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 switch (Exp->getOpcode()) {
1320 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001321 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001325 case BinaryOperator::Add:
1326 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001329 case BinaryOperator::LT:
1330 case BinaryOperator::GT:
1331 case BinaryOperator::LE:
1332 case BinaryOperator::GE:
1333 case BinaryOperator::EQ:
1334 case BinaryOperator::NE:
1335 case BinaryOperator::And:
1336 case BinaryOperator::Xor:
1337 case BinaryOperator::Or:
1338 case BinaryOperator::Comma: {
1339 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1340 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001341 if (Exp->getOpcode() == BinaryOperator::Div ||
1342 Exp->getOpcode() == BinaryOperator::Rem) {
1343 // Evaluate gives an error for undefined Div/Rem, so make sure
1344 // we don't evaluate one.
1345 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1346 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1347 if (REval == 0)
1348 return ICEDiag(1, E->getLocStart());
1349 if (REval.isSigned() && REval.isAllOnesValue()) {
1350 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1351 if (LEval.isMinSignedValue())
1352 return ICEDiag(1, E->getLocStart());
1353 }
1354 }
1355 }
1356 if (Exp->getOpcode() == BinaryOperator::Comma) {
1357 if (Ctx.getLangOptions().C99) {
1358 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1359 // if it isn't evaluated.
1360 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1361 return ICEDiag(1, E->getLocStart());
1362 } else {
1363 // In both C89 and C++, commas in ICEs are illegal.
1364 return ICEDiag(2, E->getLocStart());
1365 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001366 }
1367 if (LHSResult.Val >= RHSResult.Val)
1368 return LHSResult;
1369 return RHSResult;
1370 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001372 case BinaryOperator::LOr: {
1373 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1374 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1375 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1376 // Rare case where the RHS has a comma "side-effect"; we need
1377 // to actually check the condition to see whether the side
1378 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001379 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001380 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001381 return RHSResult;
1382 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001383 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001384
Eli Friedmane28d7192009-02-26 09:29:13 +00001385 if (LHSResult.Val >= RHSResult.Val)
1386 return LHSResult;
1387 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001389 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001391 case Expr::ImplicitCastExprClass:
1392 case Expr::CStyleCastExprClass:
1393 case Expr::CXXFunctionalCastExprClass: {
1394 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1395 if (SubExpr->getType()->isIntegralType())
1396 return CheckICE(SubExpr, Ctx);
1397 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1398 return NoDiag();
1399 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001401 case Expr::ConditionalOperatorClass: {
1402 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001403 // If the condition (ignoring parens) is a __builtin_constant_p call,
1404 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001405 // expression, and it is fully evaluated. This is an important GNU
1406 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001407 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001408 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001409 Expr::EvalResult EVResult;
1410 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1411 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001412 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001413 }
1414 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001415 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001416 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1417 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1418 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1419 if (CondResult.Val == 2)
1420 return CondResult;
1421 if (TrueResult.Val == 2)
1422 return TrueResult;
1423 if (FalseResult.Val == 2)
1424 return FalseResult;
1425 if (CondResult.Val == 1)
1426 return CondResult;
1427 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1428 return NoDiag();
1429 // Rare case where the diagnostics depend on which side is evaluated
1430 // Note that if we get here, CondResult is 0, and at least one of
1431 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001432 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001433 return FalseResult;
1434 }
1435 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001437 case Expr::CXXDefaultArgExprClass:
1438 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001439 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001440 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001441 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001443}
Reid Spencer5f016e22007-07-11 17:01:13 +00001444
Eli Friedmane28d7192009-02-26 09:29:13 +00001445bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1446 SourceLocation *Loc, bool isEvaluated) const {
1447 ICEDiag d = CheckICE(this, Ctx);
1448 if (d.Val != 0) {
1449 if (Loc) *Loc = d.Loc;
1450 return false;
1451 }
1452 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001453 if (!Evaluate(EvalResult, Ctx))
1454 assert(0 && "ICE cannot be evaluated!");
1455 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1456 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001457 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 return true;
1459}
1460
Reid Spencer5f016e22007-07-11 17:01:13 +00001461/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1462/// integer constant expression with the value zero, or if this is one that is
1463/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001464bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1465{
Sebastian Redl07779722008-10-31 14:43:28 +00001466 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001467 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001468 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001469 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001470 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001471 QualType Pointee = PT->getPointeeType();
1472 if (Pointee.getCVRQualifiers() == 0 &&
1473 Pointee->isVoidType() && // to void*
1474 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001475 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001476 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001478 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1479 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001480 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001481 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1482 // Accept ((void*)0) as a null pointer constant, as many other
1483 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001484 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001485 } else if (const CXXDefaultArgExpr *DefaultArg
1486 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001487 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001488 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001489 } else if (isa<GNUNullExpr>(this)) {
1490 // The GNU __null extension is always a null pointer constant.
1491 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001492 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001493
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001494 // C++0x nullptr_t is always a null pointer constant.
1495 if (getType()->isNullPtrType())
1496 return true;
1497
Steve Naroffaa58f002008-01-14 16:10:57 +00001498 // This expression must be an integer type.
1499 if (!getType()->isIntegerType())
1500 return false;
1501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 // If we have an integer constant expression, we need to *evaluate* it and
1503 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001504 llvm::APSInt Result;
1505 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001506}
Steve Naroff31a45842007-07-28 23:10:27 +00001507
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001508FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001509 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001510
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001511 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001512 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001513 if (Field->isBitField())
1514 return Field;
1515
1516 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1517 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1518 return BinOp->getLHS()->getBitField();
1519
1520 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001521}
1522
Chris Lattner2140e902009-02-16 22:14:05 +00001523/// isArrow - Return true if the base expression is a pointer to vector,
1524/// return false if the base expression is a vector.
1525bool ExtVectorElementExpr::isArrow() const {
1526 return getBase()->getType()->isPointerType();
1527}
1528
Nate Begeman213541a2008-04-18 23:10:10 +00001529unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001530 if (const VectorType *VT = getType()->getAsVectorType())
1531 return VT->getNumElements();
1532 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001533}
1534
Nate Begeman8a997642008-05-09 06:41:27 +00001535/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001536bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001537 const char *compStr = Accessor->getName();
1538 unsigned length = Accessor->getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001539
1540 // Halving swizzles do not contain duplicate elements.
1541 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1542 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1543 return false;
1544
1545 // Advance past s-char prefix on hex swizzles.
Nate Begeman131f4652009-06-25 21:06:09 +00001546 if (*compStr == 's' || *compStr == 'S') {
Nate Begeman190d6a22009-01-18 02:01:21 +00001547 compStr++;
1548 length--;
1549 }
Steve Narofffec0b492007-07-30 03:29:09 +00001550
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001551 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001552 const char *s = compStr+i;
1553 for (const char c = *s++; *s; s++)
1554 if (c == *s)
1555 return true;
1556 }
1557 return false;
1558}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001559
Nate Begeman8a997642008-05-09 06:41:27 +00001560/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001561void ExtVectorElementExpr::getEncodedElementAccess(
1562 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregord3c98a02009-04-15 23:02:49 +00001563 const char *compStr = Accessor->getName();
Nate Begeman131f4652009-06-25 21:06:09 +00001564 if (*compStr == 's' || *compStr == 'S')
Nate Begeman353417a2009-01-18 01:47:54 +00001565 compStr++;
1566
1567 bool isHi = !strcmp(compStr, "hi");
1568 bool isLo = !strcmp(compStr, "lo");
1569 bool isEven = !strcmp(compStr, "even");
1570 bool isOdd = !strcmp(compStr, "odd");
1571
Nate Begeman8a997642008-05-09 06:41:27 +00001572 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1573 uint64_t Index;
1574
1575 if (isHi)
1576 Index = e + i;
1577 else if (isLo)
1578 Index = i;
1579 else if (isEven)
1580 Index = 2 * i;
1581 else if (isOdd)
1582 Index = 2 * i + 1;
1583 else
1584 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001585
Nate Begeman3b8d1162008-05-13 21:03:02 +00001586 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001587 }
Nate Begeman8a997642008-05-09 06:41:27 +00001588}
1589
Steve Naroff68d331a2007-09-27 14:38:14 +00001590// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001591ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001592 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001593 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001594 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001595 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001596 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001597 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001598 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001599 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001600 if (NumArgs) {
1601 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001602 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1603 }
Steve Naroff563477d2007-09-18 23:55:05 +00001604 LBracloc = LBrac;
1605 RBracloc = RBrac;
1606}
1607
Steve Naroff68d331a2007-09-27 14:38:14 +00001608// constructor for class messages.
1609// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001610ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001612 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001613 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001614 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001615 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001616 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001617 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001618 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001619 if (NumArgs) {
1620 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001621 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1622 }
Steve Naroff563477d2007-09-18 23:55:05 +00001623 LBracloc = LBrac;
1624 RBracloc = RBrac;
1625}
1626
Ted Kremenek4df728e2008-06-24 15:50:53 +00001627// constructor for class messages.
1628ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1629 QualType retType, ObjCMethodDecl *mproto,
1630 SourceLocation LBrac, SourceLocation RBrac,
1631 Expr **ArgExprs, unsigned nargs)
1632: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1633MethodProto(mproto) {
1634 NumArgs = nargs;
1635 SubExprs = new Stmt*[NumArgs+1];
1636 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1637 if (NumArgs) {
1638 for (unsigned i = 0; i != NumArgs; ++i)
1639 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1640 }
1641 LBracloc = LBrac;
1642 RBracloc = RBrac;
1643}
1644
1645ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1646 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1647 switch (x & Flags) {
1648 default:
1649 assert(false && "Invalid ObjCMessageExpr.");
1650 case IsInstMeth:
1651 return ClassInfo(0, 0);
1652 case IsClsMethDeclUnknown:
1653 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1654 case IsClsMethDeclKnown: {
1655 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1656 return ClassInfo(D, D->getIdentifier());
1657 }
1658 }
1659}
1660
Chris Lattner0389e6b2009-04-26 00:44:05 +00001661void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1662 if (CI.first == 0 && CI.second == 0)
1663 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1664 else if (CI.first == 0)
1665 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1666 else
1667 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1668}
1669
1670
Chris Lattner27437ca2007-10-25 00:29:32 +00001671bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001672 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001673}
1674
Nate Begeman888376a2009-08-12 02:28:50 +00001675void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1676 unsigned NumExprs) {
1677 if (SubExprs) C.Deallocate(SubExprs);
1678
1679 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001680 this->NumExprs = NumExprs;
1681 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Nate Begeman888376a2009-08-12 02:28:50 +00001682}
1683
1684void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1685 DestroyChildren(C);
1686 if (SubExprs) C.Deallocate(SubExprs);
1687 this->~ShuffleVectorExpr();
1688 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001689}
1690
Douglas Gregor42602bb2009-08-07 06:08:38 +00001691void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00001692 // Override default behavior of traversing children. If this has a type
1693 // operand and the type is a variable-length array, the child iteration
1694 // will iterate over the size expression. However, this expression belongs
1695 // to the type, not to this, so we don't want to delete it.
1696 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001697 if (isArgumentType()) {
1698 this->~SizeOfAlignOfExpr();
1699 C.Deallocate(this);
1700 }
Sebastian Redl05189992008-11-11 17:56:53 +00001701 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00001702 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001703}
1704
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001705//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001706// DesignatedInitExpr
1707//===----------------------------------------------------------------------===//
1708
1709IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1710 assert(Kind == FieldDesignator && "Only valid on a field designator");
1711 if (Field.NameOrField & 0x01)
1712 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1713 else
1714 return getField()->getIdentifier();
1715}
1716
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001717DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1718 const Designator *Designators,
1719 SourceLocation EqualOrColonLoc,
1720 bool GNUSyntax,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001721 Expr **IndexExprs,
1722 unsigned NumIndexExprs,
1723 Expr *Init)
1724 : Expr(DesignatedInitExprClass, Ty,
1725 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001726 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor9ea62762009-05-21 23:17:49 +00001727 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001728 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001729
1730 // Record the initializer itself.
1731 child_iterator Child = child_begin();
1732 *Child++ = Init;
1733
1734 // Copy the designators and their subexpressions, computing
1735 // value-dependence along the way.
1736 unsigned IndexIdx = 0;
1737 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001738 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001739
1740 if (this->Designators[I].isArrayDesignator()) {
1741 // Compute type- and value-dependence.
1742 Expr *Index = IndexExprs[IndexIdx];
1743 ValueDependent = ValueDependent ||
1744 Index->isTypeDependent() || Index->isValueDependent();
1745
1746 // Copy the index expressions into permanent storage.
1747 *Child++ = IndexExprs[IndexIdx++];
1748 } else if (this->Designators[I].isArrayRangeDesignator()) {
1749 // Compute type- and value-dependence.
1750 Expr *Start = IndexExprs[IndexIdx];
1751 Expr *End = IndexExprs[IndexIdx + 1];
1752 ValueDependent = ValueDependent ||
1753 Start->isTypeDependent() || Start->isValueDependent() ||
1754 End->isTypeDependent() || End->isValueDependent();
1755
1756 // Copy the start/end expressions into permanent storage.
1757 *Child++ = IndexExprs[IndexIdx++];
1758 *Child++ = IndexExprs[IndexIdx++];
1759 }
1760 }
1761
1762 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001763}
1764
Douglas Gregor05c13a32009-01-22 00:58:24 +00001765DesignatedInitExpr *
1766DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1767 unsigned NumDesignators,
1768 Expr **IndexExprs, unsigned NumIndexExprs,
1769 SourceLocation ColonOrEqualLoc,
1770 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001771 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001772 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00001773 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1774 ColonOrEqualLoc, UsesColonSyntax,
1775 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001776}
1777
Douglas Gregord077d752009-04-16 00:55:48 +00001778DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1779 unsigned NumIndexExprs) {
1780 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1781 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1782 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1783}
1784
1785void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1786 unsigned NumDesigs) {
1787 if (Designators)
1788 delete [] Designators;
1789
1790 Designators = new Designator[NumDesigs];
1791 NumDesignators = NumDesigs;
1792 for (unsigned I = 0; I != NumDesigs; ++I)
1793 Designators[I] = Desigs[I];
1794}
1795
Douglas Gregor05c13a32009-01-22 00:58:24 +00001796SourceRange DesignatedInitExpr::getSourceRange() const {
1797 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001798 Designator &First =
1799 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001800 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001801 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001802 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1803 else
1804 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1805 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001806 StartLoc =
1807 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001808 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1809}
1810
Douglas Gregor05c13a32009-01-22 00:58:24 +00001811Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1812 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1813 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1814 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001815 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1816 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1817}
1818
1819Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1820 assert(D.Kind == Designator::ArrayRangeDesignator &&
1821 "Requires array range designator");
1822 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1823 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001824 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1825 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1826}
1827
1828Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1829 assert(D.Kind == Designator::ArrayRangeDesignator &&
1830 "Requires array range designator");
1831 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1832 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001833 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1834 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1835}
1836
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001837/// \brief Replaces the designator at index @p Idx with the series
1838/// of designators in [First, Last).
1839void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1840 const Designator *First,
1841 const Designator *Last) {
1842 unsigned NumNewDesignators = Last - First;
1843 if (NumNewDesignators == 0) {
1844 std::copy_backward(Designators + Idx + 1,
1845 Designators + NumDesignators,
1846 Designators + Idx);
1847 --NumNewDesignators;
1848 return;
1849 } else if (NumNewDesignators == 1) {
1850 Designators[Idx] = *First;
1851 return;
1852 }
1853
1854 Designator *NewDesignators
1855 = new Designator[NumDesignators - 1 + NumNewDesignators];
1856 std::copy(Designators, Designators + Idx, NewDesignators);
1857 std::copy(First, Last, NewDesignators + Idx);
1858 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1859 NewDesignators + Idx + NumNewDesignators);
1860 delete [] Designators;
1861 Designators = NewDesignators;
1862 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1863}
1864
Douglas Gregor42602bb2009-08-07 06:08:38 +00001865void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001866 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00001867 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001868}
1869
Nate Begeman2ef13e52009-08-10 23:49:36 +00001870ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
1871 Expr **exprs, unsigned nexprs,
1872 SourceLocation rparenloc)
1873: Expr(ParenListExprClass, QualType(),
1874 hasAnyTypeDependentArguments(exprs, nexprs),
1875 hasAnyValueDependentArguments(exprs, nexprs)),
1876 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
1877
1878 Exprs = new (C) Stmt*[nexprs];
1879 for (unsigned i = 0; i != nexprs; ++i)
1880 Exprs[i] = exprs[i];
1881}
1882
1883void ParenListExpr::DoDestroy(ASTContext& C) {
1884 DestroyChildren(C);
1885 if (Exprs) C.Deallocate(Exprs);
1886 this->~ParenListExpr();
1887 C.Deallocate(this);
1888}
1889
Douglas Gregor05c13a32009-01-22 00:58:24 +00001890//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001891// ExprIterator.
1892//===----------------------------------------------------------------------===//
1893
1894Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1895Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1896Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1897const Expr* ConstExprIterator::operator[](size_t idx) const {
1898 return cast<Expr>(I[idx]);
1899}
1900const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1901const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1902
1903//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001904// Child Iterators for iterating over subexpressions/substatements
1905//===----------------------------------------------------------------------===//
1906
1907// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001908Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1909Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001910
Steve Naroff7779db42007-11-12 14:29:37 +00001911// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001912Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1913Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001914
Steve Naroffe3e9add2008-06-02 23:03:37 +00001915// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001916Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1917Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001918
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001919// ObjCImplicitSetterGetterRefExpr
1920Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001921 return &Base;
1922}
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001923Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001924 return &Base+1;
1925}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001926
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001927// ObjCSuperExpr
1928Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1929Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1930
Steve Narofff242b1b2009-07-24 17:54:45 +00001931// ObjCIsaExpr
1932Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1933Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1934
Chris Lattnerd9f69102008-08-10 01:53:14 +00001935// PredefinedExpr
1936Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1937Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001938
1939// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001940Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1941Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001942
1943// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001944Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001945Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001946
1947// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001948Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1949Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001950
Chris Lattner5d661452007-08-26 03:42:43 +00001951// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001952Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1953Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001954
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001955// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001956Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1957Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001958
1959// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001960Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1961Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001962
1963// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001964Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1965Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001966
Sebastian Redl05189992008-11-11 17:56:53 +00001967// SizeOfAlignOfExpr
1968Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1969 // If this is of a type and the type is a VLA type (and not a typedef), the
1970 // size expression of the VLA needs to be treated as an executable expression.
1971 // Why isn't this weirdness documented better in StmtIterator?
1972 if (isArgumentType()) {
1973 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1974 getArgumentType().getTypePtr()))
1975 return child_iterator(T);
1976 return child_iterator();
1977 }
Sebastian Redld4575892008-12-03 23:17:54 +00001978 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001979}
Sebastian Redl05189992008-11-11 17:56:53 +00001980Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1981 if (isArgumentType())
1982 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001983 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001984}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001985
1986// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001987Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001988 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001989}
Ted Kremenek1237c672007-08-24 20:06:47 +00001990Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001991 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001992}
1993
1994// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001995Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001996 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001997}
Ted Kremenek1237c672007-08-24 20:06:47 +00001998Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001999 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002000}
Ted Kremenek1237c672007-08-24 20:06:47 +00002001
2002// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002003Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2004Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002005
Nate Begeman213541a2008-04-18 23:10:10 +00002006// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002007Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2008Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002009
2010// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002011Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2012Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002013
Ted Kremenek1237c672007-08-24 20:06:47 +00002014// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002015Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2016Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002017
2018// BinaryOperator
2019Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002020 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002021}
Ted Kremenek1237c672007-08-24 20:06:47 +00002022Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002023 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002024}
2025
2026// ConditionalOperator
2027Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002028 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002029}
Ted Kremenek1237c672007-08-24 20:06:47 +00002030Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002031 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002032}
2033
2034// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002035Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2036Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002037
Ted Kremenek1237c672007-08-24 20:06:47 +00002038// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002039Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2040Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002041
2042// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002043Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2044 return child_iterator();
2045}
2046
2047Stmt::child_iterator TypesCompatibleExpr::child_end() {
2048 return child_iterator();
2049}
Ted Kremenek1237c672007-08-24 20:06:47 +00002050
2051// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002052Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2053Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002054
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002055// GNUNullExpr
2056Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2057Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2058
Eli Friedmand38617c2008-05-14 19:38:39 +00002059// ShuffleVectorExpr
2060Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002061 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002062}
2063Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002064 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002065}
2066
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002067// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002068Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2069Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002070
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002071// InitListExpr
2072Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002073 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002074}
2075Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002076 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002077}
2078
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002079// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002080Stmt::child_iterator DesignatedInitExpr::child_begin() {
2081 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2082 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002083 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2084}
2085Stmt::child_iterator DesignatedInitExpr::child_end() {
2086 return child_iterator(&*child_begin() + NumSubExprs);
2087}
2088
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002089// ImplicitValueInitExpr
2090Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2091 return child_iterator();
2092}
2093
2094Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2095 return child_iterator();
2096}
2097
Nate Begeman2ef13e52009-08-10 23:49:36 +00002098// ParenListExpr
2099Stmt::child_iterator ParenListExpr::child_begin() {
2100 return &Exprs[0];
2101}
2102Stmt::child_iterator ParenListExpr::child_end() {
2103 return &Exprs[0]+NumExprs;
2104}
2105
Ted Kremenek1237c672007-08-24 20:06:47 +00002106// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002107Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002108 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002109}
2110Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002111 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002112}
Ted Kremenek1237c672007-08-24 20:06:47 +00002113
2114// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002115Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2116Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002117
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002118// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002119Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2120 return child_iterator();
2121}
2122Stmt::child_iterator ObjCSelectorExpr::child_end() {
2123 return child_iterator();
2124}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002125
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002126// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002127Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2128 return child_iterator();
2129}
2130Stmt::child_iterator ObjCProtocolExpr::child_end() {
2131 return child_iterator();
2132}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002133
Steve Naroff563477d2007-09-18 23:55:05 +00002134// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00002135Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002136 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002137}
2138Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002139 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002140}
2141
Steve Naroff4eb206b2008-09-03 18:15:37 +00002142// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002143Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2144Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002145
Ted Kremenek9da13f92008-09-26 23:24:14 +00002146Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2147Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }