blob: 2f7e3630fdc876c0ad7be79f446b5eb7c8571caf [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Anders Carlssona135fb42009-03-15 18:34:13 +000029IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
30 return new (C) IntegerLiteral(Value, getType(), Loc);
31}
32
Chris Lattnerda8249e2008-06-07 22:13:43 +000033/// getValueAsApproximateDouble - This returns the value as an inaccurate
34/// double. Note that this may cause loss of precision, but is useful for
35/// debugging dumps, etc.
36double FloatingLiteral::getValueAsApproximateDouble() const {
37 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000038 bool ignored;
39 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
40 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000041 return V.convertToDouble();
42}
43
Chris Lattner2085fd62009-02-18 06:40:38 +000044StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
45 unsigned ByteLength, bool Wide,
46 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000047 const SourceLocation *Loc,
48 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000049 // Allocate enough space for the StringLiteral plus an array of locations for
50 // any concatenated string tokens.
51 void *Mem = C.Allocate(sizeof(StringLiteral)+
52 sizeof(SourceLocation)*(NumStrs-1),
53 llvm::alignof<StringLiteral>());
54 StringLiteral *SL = new (Mem) StringLiteral(Ty);
55
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000057 char *AStrData = new (C, 1) char[ByteLength];
58 memcpy(AStrData, StrData, ByteLength);
59 SL->StrData = AStrData;
60 SL->ByteLength = ByteLength;
61 SL->IsWide = Wide;
62 SL->TokLocs[0] = Loc[0];
63 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
Chris Lattner726e1682009-02-18 05:49:11 +000065 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000066 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
67 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000068}
69
Anders Carlssona135fb42009-03-15 18:34:13 +000070StringLiteral* StringLiteral::Clone(ASTContext &C) const {
71 return Create(C, StrData, ByteLength, IsWide, getType(),
72 TokLocs, NumConcatenated);
73}
Chris Lattner726e1682009-02-18 05:49:11 +000074
Ted Kremenek6e94ef52009-02-06 19:55:15 +000075void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000076 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000077 this->~StringLiteral();
78 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
80
Reid Spencer5f016e22007-07-11 17:01:13 +000081/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
82/// corresponds to, e.g. "sizeof" or "[pre]++".
83const char *UnaryOperator::getOpcodeStr(Opcode Op) {
84 switch (Op) {
85 default: assert(0 && "Unknown unary operator");
86 case PostInc: return "++";
87 case PostDec: return "--";
88 case PreInc: return "++";
89 case PreDec: return "--";
90 case AddrOf: return "&";
91 case Deref: return "*";
92 case Plus: return "+";
93 case Minus: return "-";
94 case Not: return "~";
95 case LNot: return "!";
96 case Real: return "__real";
97 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +000098 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000099 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101}
102
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000103UnaryOperator::Opcode
104UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
105 switch (OO) {
106 case OO_PlusPlus: return Postfix? PostInc : PreInc;
107 case OO_MinusMinus: return Postfix? PostDec : PreDec;
108 case OO_Amp: return AddrOf;
109 case OO_Star: return Deref;
110 case OO_Plus: return Plus;
111 case OO_Minus: return Minus;
112 case OO_Tilde: return Not;
113 case OO_Exclaim: return LNot;
114 default: assert(false && "No unary operator for overloaded function");
115 }
116}
117
118OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
119 switch (Opc) {
120 case PostInc: case PreInc: return OO_PlusPlus;
121 case PostDec: case PreDec: return OO_MinusMinus;
122 case AddrOf: return OO_Amp;
123 case Deref: return OO_Star;
124 case Plus: return OO_Plus;
125 case Minus: return OO_Minus;
126 case Not: return OO_Tilde;
127 case LNot: return OO_Exclaim;
128 default: return OO_None;
129 }
130}
131
132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133//===----------------------------------------------------------------------===//
134// Postfix Operators.
135//===----------------------------------------------------------------------===//
136
Ted Kremenek668bf912009-02-09 20:51:47 +0000137CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000138 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000139 : Expr(SC, t,
140 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000141 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000142 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000143
144 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000145 SubExprs[FN] = fn;
146 for (unsigned i = 0; i != numargs; ++i)
147 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000148
Douglas Gregorb4609802008-11-14 16:09:21 +0000149 RParenLoc = rparenloc;
150}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000151
Ted Kremenek668bf912009-02-09 20:51:47 +0000152CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
153 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000154 : Expr(CallExprClass, t,
155 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000156 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000157 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000158
159 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000160 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000162 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 RParenLoc = rparenloc;
165}
166
Ted Kremenek668bf912009-02-09 20:51:47 +0000167void CallExpr::Destroy(ASTContext& C) {
168 DestroyChildren(C);
169 if (SubExprs) C.Deallocate(SubExprs);
170 this->~CallExpr();
171 C.Deallocate(this);
172}
173
Chris Lattnerd18b3292007-12-28 05:25:02 +0000174/// setNumArgs - This changes the number of arguments present in this call.
175/// Any orphaned expressions are deleted by this, and any new operands are set
176/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000177void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000178 // No change, just return.
179 if (NumArgs == getNumArgs()) return;
180
181 // If shrinking # arguments, just delete the extras and forgot them.
182 if (NumArgs < getNumArgs()) {
183 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000184 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000185 this->NumArgs = NumArgs;
186 return;
187 }
188
189 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000190 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000191 // Copy over args.
192 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
193 NewSubExprs[i] = SubExprs[i];
194 // Null out new args.
195 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
196 NewSubExprs[i] = 0;
197
Ted Kremenek8189cde2009-02-07 01:47:29 +0000198 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000199 SubExprs = NewSubExprs;
200 this->NumArgs = NumArgs;
201}
202
Chris Lattnercb888962008-10-06 05:00:53 +0000203/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
204/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000205unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000206 // All simple function calls (e.g. func()) are implicitly cast to pointer to
207 // function. As a result, we try and obtain the DeclRefExpr from the
208 // ImplicitCastExpr.
209 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
210 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000211 return 0;
212
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000213 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
214 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000215 return 0;
216
Anders Carlssonbcba2012008-01-31 02:13:57 +0000217 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
218 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000219 return 0;
220
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000221 if (!FDecl->getIdentifier())
222 return 0;
223
Douglas Gregor3c385e52009-02-14 18:57:46 +0000224 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000225}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000226
Chris Lattnercb888962008-10-06 05:00:53 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
229/// corresponds to, e.g. "<<=".
230const char *BinaryOperator::getOpcodeStr(Opcode Op) {
231 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000232 case PtrMemD: return ".*";
233 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 case Mul: return "*";
235 case Div: return "/";
236 case Rem: return "%";
237 case Add: return "+";
238 case Sub: return "-";
239 case Shl: return "<<";
240 case Shr: return ">>";
241 case LT: return "<";
242 case GT: return ">";
243 case LE: return "<=";
244 case GE: return ">=";
245 case EQ: return "==";
246 case NE: return "!=";
247 case And: return "&";
248 case Xor: return "^";
249 case Or: return "|";
250 case LAnd: return "&&";
251 case LOr: return "||";
252 case Assign: return "=";
253 case MulAssign: return "*=";
254 case DivAssign: return "/=";
255 case RemAssign: return "%=";
256 case AddAssign: return "+=";
257 case SubAssign: return "-=";
258 case ShlAssign: return "<<=";
259 case ShrAssign: return ">>=";
260 case AndAssign: return "&=";
261 case XorAssign: return "^=";
262 case OrAssign: return "|=";
263 case Comma: return ",";
264 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000265
266 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
Douglas Gregor063daf62009-03-13 18:40:31 +0000269BinaryOperator::Opcode
270BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
271 switch (OO) {
272 case OO_Plus: return Add;
273 case OO_Minus: return Sub;
274 case OO_Star: return Mul;
275 case OO_Slash: return Div;
276 case OO_Percent: return Rem;
277 case OO_Caret: return Xor;
278 case OO_Amp: return And;
279 case OO_Pipe: return Or;
280 case OO_Equal: return Assign;
281 case OO_Less: return LT;
282 case OO_Greater: return GT;
283 case OO_PlusEqual: return AddAssign;
284 case OO_MinusEqual: return SubAssign;
285 case OO_StarEqual: return MulAssign;
286 case OO_SlashEqual: return DivAssign;
287 case OO_PercentEqual: return RemAssign;
288 case OO_CaretEqual: return XorAssign;
289 case OO_AmpEqual: return AndAssign;
290 case OO_PipeEqual: return OrAssign;
291 case OO_LessLess: return Shl;
292 case OO_GreaterGreater: return Shr;
293 case OO_LessLessEqual: return ShlAssign;
294 case OO_GreaterGreaterEqual: return ShrAssign;
295 case OO_EqualEqual: return EQ;
296 case OO_ExclaimEqual: return NE;
297 case OO_LessEqual: return LE;
298 case OO_GreaterEqual: return GE;
299 case OO_AmpAmp: return LAnd;
300 case OO_PipePipe: return LOr;
301 case OO_Comma: return Comma;
302 case OO_ArrowStar: return PtrMemI;
303 default: assert(false && "Not an overloadable binary operator");
304 }
305}
306
307OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
308 static const OverloadedOperatorKind OverOps[] = {
309 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
310 OO_Star, OO_Slash, OO_Percent,
311 OO_Plus, OO_Minus,
312 OO_LessLess, OO_GreaterGreater,
313 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
314 OO_EqualEqual, OO_ExclaimEqual,
315 OO_Amp,
316 OO_Caret,
317 OO_Pipe,
318 OO_AmpAmp,
319 OO_PipePipe,
320 OO_Equal, OO_StarEqual,
321 OO_SlashEqual, OO_PercentEqual,
322 OO_PlusEqual, OO_MinusEqual,
323 OO_LessLessEqual, OO_GreaterGreaterEqual,
324 OO_AmpEqual, OO_CaretEqual,
325 OO_PipeEqual,
326 OO_Comma
327 };
328 return OverOps[Opc];
329}
330
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000331InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000332 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000333 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000334 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000335 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000336 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000337
338 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000339}
Reid Spencer5f016e22007-07-11 17:01:13 +0000340
Douglas Gregor4c678342009-01-28 21:54:33 +0000341void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000342 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000343 Idx < LastIdx; ++Idx)
Douglas Gregor4c678342009-01-28 21:54:33 +0000344 delete InitExprs[Idx];
345 InitExprs.resize(NumInits, 0);
346}
347
348Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
349 if (Init >= InitExprs.size()) {
350 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
351 InitExprs.back() = expr;
352 return 0;
353 }
354
355 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
356 InitExprs[Init] = expr;
357 return Result;
358}
359
Steve Naroffbfdcae62008-09-04 15:31:07 +0000360/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000361///
362const FunctionType *BlockExpr::getFunctionType() const {
363 return getType()->getAsBlockPointerType()->
364 getPointeeType()->getAsFunctionType();
365}
366
Steve Naroff56ee6892008-10-08 17:01:13 +0000367SourceLocation BlockExpr::getCaretLocation() const {
368 return TheBlock->getCaretLocation();
369}
370const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
371Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
372
373
Reid Spencer5f016e22007-07-11 17:01:13 +0000374//===----------------------------------------------------------------------===//
375// Generic Expression Routines
376//===----------------------------------------------------------------------===//
377
Chris Lattner026dc962009-02-14 07:37:35 +0000378/// isUnusedResultAWarning - Return true if this immediate expression should
379/// be warned about if the result is unused. If so, fill in Loc and Ranges
380/// with location to warn on and the source range[s] to report with the
381/// warning.
382bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
383 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 switch (getStmtClass()) {
385 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000386 Loc = getExprLoc();
387 R1 = getSourceRange();
388 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000390 return cast<ParenExpr>(this)->getSubExpr()->
391 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 case UnaryOperatorClass: {
393 const UnaryOperator *UO = cast<UnaryOperator>(this);
394
395 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000396 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 case UnaryOperator::PostInc:
398 case UnaryOperator::PostDec:
399 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000400 case UnaryOperator::PreDec: // ++/--
401 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 case UnaryOperator::Deref:
403 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000404 if (getType().isVolatileQualified())
405 return false;
406 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 case UnaryOperator::Real:
408 case UnaryOperator::Imag:
409 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000410 if (UO->getSubExpr()->getType().isVolatileQualified())
411 return false;
412 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000414 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 }
Chris Lattner026dc962009-02-14 07:37:35 +0000416 Loc = UO->getOperatorLoc();
417 R1 = UO->getSubExpr()->getSourceRange();
418 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000420 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000421 const BinaryOperator *BO = cast<BinaryOperator>(this);
422 // Consider comma to have side effects if the LHS or RHS does.
423 if (BO->getOpcode() == BinaryOperator::Comma)
424 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
425 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000426
Chris Lattner026dc962009-02-14 07:37:35 +0000427 if (BO->isAssignmentOp())
428 return false;
429 Loc = BO->getOperatorLoc();
430 R1 = BO->getLHS()->getSourceRange();
431 R2 = BO->getRHS()->getSourceRange();
432 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000433 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000434 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000435 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000436
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000437 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000438 // The condition must be evaluated, but if either the LHS or RHS is a
439 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000440 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000441 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000442 return true;
443 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000444 }
445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000447 // If the base pointer or element is to a volatile pointer/field, accessing
448 // it is a side effect.
449 if (getType().isVolatileQualified())
450 return false;
451 Loc = cast<MemberExpr>(this)->getMemberLoc();
452 R1 = SourceRange(Loc, Loc);
453 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
454 return true;
455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 case ArraySubscriptExprClass:
457 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000458 // it is a side effect.
459 if (getType().isVolatileQualified())
460 return false;
461 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
462 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
463 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
464 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000467 case CXXOperatorCallExprClass: {
468 // If this is a direct call, get the callee.
469 const CallExpr *CE = cast<CallExpr>(this);
470 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
471 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
472 // If the callee has attribute pure, const, or warn_unused_result, warn
473 // about it. void foo() { strlen("bar"); } should warn.
474 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
475 if (FD->getAttr<WarnUnusedResultAttr>() ||
476 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
477 Loc = CE->getCallee()->getLocStart();
478 R1 = CE->getCallee()->getSourceRange();
479
480 if (unsigned NumArgs = CE->getNumArgs())
481 R2 = SourceRange(CE->getArg(0)->getLocStart(),
482 CE->getArg(NumArgs-1)->getLocEnd());
483 return true;
484 }
485 }
486 return false;
487 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000488 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000489 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000490 case StmtExprClass: {
491 // Statement exprs don't logically have side effects themselves, but are
492 // sometimes used in macros in ways that give them a type that is unused.
493 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
494 // however, if the result of the stmt expr is dead, we don't want to emit a
495 // warning.
496 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
497 if (!CS->body_empty())
498 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000499 return E->isUnusedResultAWarning(Loc, R1, R2);
500
501 Loc = cast<StmtExpr>(this)->getLParenLoc();
502 R1 = getSourceRange();
503 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000504 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000505 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000506 // If this is a cast to void, check the operand. Otherwise, the result of
507 // the cast is unused.
508 if (getType()->isVoidType())
509 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
510 R1, R2);
511 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
512 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
513 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000514 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 // If this is a cast to void, check the operand. Otherwise, the result of
516 // the cast is unused.
517 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000518 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
519 R1, R2);
520 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
521 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
522 return true;
523
Eli Friedman4be1f472008-05-19 21:24:43 +0000524 case ImplicitCastExprClass:
525 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000526 return cast<ImplicitCastExpr>(this)
527 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000528
Chris Lattner04421082008-04-08 04:40:51 +0000529 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000530 return cast<CXXDefaultArgExpr>(this)
531 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000532
533 case CXXNewExprClass:
534 // FIXME: In theory, there might be new expressions that don't have side
535 // effects (e.g. a placement new with an uninitialized POD).
536 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000537 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000538 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000539}
540
Douglas Gregorba7e2102008-10-22 15:04:37 +0000541/// DeclCanBeLvalue - Determine whether the given declaration can be
542/// an lvalue. This is a helper routine for isLvalue.
543static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000544 // C++ [temp.param]p6:
545 // A non-type non-reference template-parameter is not an lvalue.
546 if (const NonTypeTemplateParmDecl *NTTParm
547 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
548 return NTTParm->getType()->isReferenceType();
549
Douglas Gregor44b43212008-12-11 16:49:14 +0000550 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000551 // C++ 3.10p2: An lvalue refers to an object or function.
552 (Ctx.getLangOptions().CPlusPlus &&
553 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
554}
555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
557/// incomplete type other than void. Nonarray expressions that can be lvalues:
558/// - name, where name must be a variable
559/// - e[i]
560/// - (e), where e must be an lvalue
561/// - e.name, where e must be an lvalue
562/// - e->name
563/// - *e, the type of e cannot be a function type
564/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000565/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000566/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000567///
Chris Lattner28be73f2008-07-26 21:30:36 +0000568Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000569 // first, check the type (C99 6.3.2.1). Expressions with function
570 // type in C are not lvalues, but they can be lvalues in C++.
571 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 return LV_NotObjectType;
573
Steve Naroffacb818a2008-02-10 01:39:04 +0000574 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000575 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000576 return LV_IncompleteVoidType;
577
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000578 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 // the type looks fine, now check the expression
581 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000582 case StringLiteralClass: // C99 6.5.1p4
583 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000584 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
586 // For vectors, make sure base is an lvalue (i.e. not a function call).
587 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000588 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000590 case DeclRefExprClass:
591 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000592 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
593 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return LV_Valid;
595 break;
Chris Lattner41110242008-06-17 18:05:57 +0000596 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000597 case BlockDeclRefExprClass: {
598 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000599 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000600 return LV_Valid;
601 break;
602 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000603 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000605 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
606 NamedDecl *Member = m->getMemberDecl();
607 // C++ [expr.ref]p4:
608 // If E2 is declared to have type "reference to T", then E1.E2
609 // is an lvalue.
610 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
611 if (Value->getType()->isReferenceType())
612 return LV_Valid;
613
614 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000615 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000616 return LV_Valid;
617
618 // -- If E2 is a non-static data member [...]. If E1 is an
619 // lvalue, then E1.E2 is an lvalue.
620 if (isa<FieldDecl>(Member))
621 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
622
623 // -- If it refers to a static member function [...], then
624 // E1.E2 is an lvalue.
625 // -- Otherwise, if E1.E2 refers to a non-static member
626 // function [...], then E1.E2 is not an lvalue.
627 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
628 return Method->isStatic()? LV_Valid : LV_MemberFunction;
629
630 // -- If E2 is a member enumerator [...], the expression E1.E2
631 // is not an lvalue.
632 if (isa<EnumConstantDecl>(Member))
633 return LV_InvalidExpression;
634
635 // Not an lvalue.
636 return LV_InvalidExpression;
637 }
638
639 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000640 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000641 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000642 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000644 return LV_Valid; // C99 6.5.3p4
645
646 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000647 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
648 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000649 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000650
651 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
652 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
653 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
654 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000656 case ImplicitCastExprClass:
657 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
658 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000660 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000661 case BinaryOperatorClass:
662 case CompoundAssignOperatorClass: {
663 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000664
665 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
666 BinOp->getOpcode() == BinaryOperator::Comma)
667 return BinOp->getRHS()->isLvalue(Ctx);
668
Sebastian Redl22460502009-02-07 00:15:38 +0000669 // C++ [expr.mptr.oper]p6
670 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
671 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
672 !BinOp->getType()->isFunctionType())
673 return BinOp->getLHS()->isLvalue(Ctx);
674
Douglas Gregorbf3af052008-11-13 20:12:29 +0000675 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000676 return LV_InvalidExpression;
677
Douglas Gregorbf3af052008-11-13 20:12:29 +0000678 if (Ctx.getLangOptions().CPlusPlus)
679 // C++ [expr.ass]p1:
680 // The result of an assignment operation [...] is an lvalue.
681 return LV_Valid;
682
683
684 // C99 6.5.16:
685 // An assignment expression [...] is not an lvalue.
686 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000687 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000688 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000689 case CXXOperatorCallExprClass:
690 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000691 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000692 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000693 // is an lvalue reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000694 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000695 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000696 CalleeType = FnTypePtr->getPointeeType();
697 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000698 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000699 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000700
Douglas Gregor9d293df2008-10-28 00:22:11 +0000701 break;
702 }
Steve Naroffe6386392007-12-05 04:00:10 +0000703 case CompoundLiteralExprClass: // C99 6.5.2.5p5
704 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000705 case ChooseExprClass:
706 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000707 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000708 case ExtVectorElementExprClass:
709 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000710 return LV_DuplicateVectorComponents;
711 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000712 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
713 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000714 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
715 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000716 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000717 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000718 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000719 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000720 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000721 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000722 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000723 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000724 case CXXConditionDeclExprClass:
725 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000726 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000727 case CXXFunctionalCastExprClass:
728 case CXXStaticCastExprClass:
729 case CXXDynamicCastExprClass:
730 case CXXReinterpretCastExprClass:
731 case CXXConstCastExprClass:
732 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000733 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000734 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
735 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000736 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
737 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000738 return LV_Valid;
739 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000740 case CXXTypeidExprClass:
741 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
742 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 default:
744 break;
745 }
746 return LV_InvalidExpression;
747}
748
749/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
750/// does not have an incomplete type, does not have a const-qualified type, and
751/// if it is a structure or union, does not have any member (including,
752/// recursively, any member or element of all contained aggregates or unions)
753/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000754Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
755 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000756
757 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000758 case LV_Valid:
759 // C++ 3.10p11: Functions cannot be modified, but pointers to
760 // functions can be modifiable.
761 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
762 return MLV_NotObjectType;
763 break;
764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 case LV_NotObjectType: return MLV_NotObjectType;
766 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000767 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000768 case LV_InvalidExpression:
769 // If the top level is a C-style cast, and the subexpression is a valid
770 // lvalue, then this is probably a use of the old-school "cast as lvalue"
771 // GCC extension. We don't support it, but we want to produce good
772 // diagnostics when it happens so that the user knows why.
773 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
774 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
775 return MLV_LValueCast;
776 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000777 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000779
780 QualType CT = Ctx.getCanonicalType(getType());
781
782 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000784 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000786 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 return MLV_IncompleteType;
788
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000789 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 if (r->hasConstFields())
791 return MLV_ConstQualified;
792 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000793 // The following is illegal:
794 // void takeclosure(void (^C)(void));
795 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
796 //
797 if (getStmtClass() == BlockDeclRefExprClass) {
798 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
799 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
800 return MLV_NotBlockQualified;
801 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000802
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000803 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000804 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000805 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
806 if (KVCExpr->getSetterMethod() == 0)
807 return MLV_NoSetterProperty;
808 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 return MLV_Valid;
810}
811
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000812/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000813/// duration. This means that the address of this expression is a link-time
814/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000815bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000816 switch (getStmtClass()) {
817 default:
818 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000819 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000820 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000821 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000822 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000823 case CompoundLiteralExprClass:
824 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000825 case DeclRefExprClass:
826 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000827 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
828 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000829 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000830 if (isa<FunctionDecl>(D))
831 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000832 return false;
833 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000834 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000835 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000836 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000837 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000838 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000839 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000840 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000841 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000842 case CXXDefaultArgExprClass:
843 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000844 }
845}
846
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000847/// isOBJCGCCandidate - Check if an expression is objc gc'able.
848///
849bool Expr::isOBJCGCCandidate() const {
850 switch (getStmtClass()) {
851 default:
852 return false;
853 case ObjCIvarRefExprClass:
854 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000855 case Expr::UnaryOperatorClass:
856 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000857 case ParenExprClass:
858 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
859 case ImplicitCastExprClass:
860 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
861 case DeclRefExprClass:
862 case QualifiedDeclRefExprClass: {
863 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
864 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
865 return VD->hasGlobalStorage();
866 return false;
867 }
868 case MemberExprClass: {
869 const MemberExpr *M = cast<MemberExpr>(this);
870 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
871 }
872 case ArraySubscriptExprClass:
873 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
874 }
875}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000876Expr* Expr::IgnoreParens() {
877 Expr* E = this;
878 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
879 E = P->getSubExpr();
880
881 return E;
882}
883
Chris Lattner56f34942008-02-13 01:02:39 +0000884/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
885/// or CastExprs or ImplicitCastExprs, returning their operand.
886Expr *Expr::IgnoreParenCasts() {
887 Expr *E = this;
888 while (true) {
889 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
890 E = P->getSubExpr();
891 else if (CastExpr *P = dyn_cast<CastExpr>(E))
892 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000893 else
894 return E;
895 }
896}
897
Chris Lattnerecdd8412009-03-13 17:28:01 +0000898/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
899/// value (including ptr->int casts of the same size). Strip off any
900/// ParenExpr or CastExprs, returning their operand.
901Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
902 Expr *E = this;
903 while (true) {
904 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
905 E = P->getSubExpr();
906 continue;
907 }
908
909 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
910 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
911 // ptr<->int casts of the same width. We also ignore all identify casts.
912 Expr *SE = P->getSubExpr();
913
914 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
915 E = SE;
916 continue;
917 }
918
919 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
920 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
921 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
922 E = SE;
923 continue;
924 }
925 }
926
927 return E;
928 }
929}
930
931
Douglas Gregor898574e2008-12-05 23:32:09 +0000932/// hasAnyTypeDependentArguments - Determines if any of the expressions
933/// in Exprs is type-dependent.
934bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
935 for (unsigned I = 0; I < NumExprs; ++I)
936 if (Exprs[I]->isTypeDependent())
937 return true;
938
939 return false;
940}
941
942/// hasAnyValueDependentArguments - Determines if any of the expressions
943/// in Exprs is value-dependent.
944bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
945 for (unsigned I = 0; I < NumExprs; ++I)
946 if (Exprs[I]->isValueDependent())
947 return true;
948
949 return false;
950}
951
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000952bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000953 // This function is attempting whether an expression is an initializer
954 // which can be evaluated at compile-time. isEvaluatable handles most
955 // of the cases, but it can't deal with some initializer-specific
956 // expressions, and it can't deal with aggregates; we deal with those here,
957 // and fall back to isEvaluatable for the other cases.
958
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000959 // FIXME: This function assumes the variable being assigned to
960 // isn't a reference type!
961
Anders Carlssone8a32b82008-11-24 05:23:59 +0000962 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000963 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000964 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000965 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000966 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000967 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000968 // This handles gcc's extension that allows global initializers like
969 // "struct x {int x;} x = (struct x) {};".
970 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +0000971 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000972 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000973 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000974 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000975 // FIXME: This doesn't deal with fields with reference types correctly.
976 // FIXME: This incorrectly allows pointers cast to integers to be assigned
977 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +0000978 const InitListExpr *Exp = cast<InitListExpr>(this);
979 unsigned numInits = Exp->getNumInits();
980 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000981 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000982 return false;
983 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000984 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000985 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000986 case ImplicitValueInitExprClass:
987 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000988 case ParenExprClass: {
989 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
990 }
991 case UnaryOperatorClass: {
992 const UnaryOperator* Exp = cast<UnaryOperator>(this);
993 if (Exp->getOpcode() == UnaryOperator::Extension)
994 return Exp->getSubExpr()->isConstantInitializer(Ctx);
995 break;
996 }
997 case CStyleCastExprClass:
998 // Handle casts with a destination that's a struct or union; this
999 // deals with both the gcc no-op struct cast extension and the
1000 // cast-to-union extension.
1001 if (getType()->isRecordType())
1002 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1003 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001004 }
1005
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001006 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001007}
1008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001010/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001011
1012/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1013/// comma, etc
1014///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001015/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1016/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1017/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001018
Eli Friedmane28d7192009-02-26 09:29:13 +00001019// CheckICE - This function does the fundamental ICE checking: the returned
1020// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1021// Note that to reduce code duplication, this helper does no evaluation
1022// itself; the caller checks whether the expression is evaluatable, and
1023// in the rare cases where CheckICE actually cares about the evaluated
1024// value, it calls into Evalute.
1025//
1026// Meanings of Val:
1027// 0: This expression is an ICE if it can be evaluated by Evaluate.
1028// 1: This expression is not an ICE, but if it isn't evaluated, it's
1029// a legal subexpression for an ICE. This return value is used to handle
1030// the comma operator in C99 mode.
1031// 2: This expression is not an ICE, and is not a legal subexpression for one.
1032
1033struct ICEDiag {
1034 unsigned Val;
1035 SourceLocation Loc;
1036
1037 public:
1038 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1039 ICEDiag() : Val(0) {}
1040};
1041
1042ICEDiag NoDiag() { return ICEDiag(); }
1043
Eli Friedman60ce9632009-02-27 04:07:58 +00001044static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1045 Expr::EvalResult EVResult;
1046 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1047 !EVResult.Val.isInt()) {
1048 return ICEDiag(2, E->getLocStart());
1049 }
1050 return NoDiag();
1051}
1052
Eli Friedmane28d7192009-02-26 09:29:13 +00001053static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001054 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001055 if (!E->getType()->isIntegralType()) {
1056 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001057 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001058
1059 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001061 return ICEDiag(2, E->getLocStart());
1062 case Expr::ParenExprClass:
1063 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1064 case Expr::IntegerLiteralClass:
1065 case Expr::CharacterLiteralClass:
1066 case Expr::CXXBoolLiteralExprClass:
1067 case Expr::CXXZeroInitValueExprClass:
1068 case Expr::TypesCompatibleExprClass:
1069 case Expr::UnaryTypeTraitExprClass:
1070 return NoDiag();
1071 case Expr::CallExprClass:
1072 case Expr::CXXOperatorCallExprClass: {
1073 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001074 if (CE->isBuiltinCall(Ctx))
1075 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001076 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001077 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001078 case Expr::DeclRefExprClass:
1079 case Expr::QualifiedDeclRefExprClass:
1080 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1081 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001082 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001083 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001084 // C++ 7.1.5.1p2
1085 // A variable of non-volatile const-qualified integral or enumeration
1086 // type initialized by an ICE can be used in ICEs.
1087 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001088 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001089 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +00001090 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001091 }
1092 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001093 return ICEDiag(2, E->getLocStart());
1094 case Expr::UnaryOperatorClass: {
1095 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001098 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001100 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001104 case UnaryOperator::Real:
1105 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001106 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001107 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001108 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1109 // Evaluate matches the proposed gcc behavior for cases like
1110 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1111 // compliance: we should warn earlier for offsetof expressions with
1112 // array subscripts that aren't ICEs, and if the array subscripts
1113 // are ICEs, the value of the offsetof must be an integer constant.
1114 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001117 case Expr::SizeOfAlignOfExprClass: {
1118 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1119 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1120 return ICEDiag(2, E->getLocStart());
1121 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001123 case Expr::BinaryOperatorClass: {
1124 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 switch (Exp->getOpcode()) {
1126 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001127 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001131 case BinaryOperator::Add:
1132 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001135 case BinaryOperator::LT:
1136 case BinaryOperator::GT:
1137 case BinaryOperator::LE:
1138 case BinaryOperator::GE:
1139 case BinaryOperator::EQ:
1140 case BinaryOperator::NE:
1141 case BinaryOperator::And:
1142 case BinaryOperator::Xor:
1143 case BinaryOperator::Or:
1144 case BinaryOperator::Comma: {
1145 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1146 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001147 if (Exp->getOpcode() == BinaryOperator::Div ||
1148 Exp->getOpcode() == BinaryOperator::Rem) {
1149 // Evaluate gives an error for undefined Div/Rem, so make sure
1150 // we don't evaluate one.
1151 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1152 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1153 if (REval == 0)
1154 return ICEDiag(1, E->getLocStart());
1155 if (REval.isSigned() && REval.isAllOnesValue()) {
1156 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1157 if (LEval.isMinSignedValue())
1158 return ICEDiag(1, E->getLocStart());
1159 }
1160 }
1161 }
1162 if (Exp->getOpcode() == BinaryOperator::Comma) {
1163 if (Ctx.getLangOptions().C99) {
1164 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1165 // if it isn't evaluated.
1166 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1167 return ICEDiag(1, E->getLocStart());
1168 } else {
1169 // In both C89 and C++, commas in ICEs are illegal.
1170 return ICEDiag(2, E->getLocStart());
1171 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001172 }
1173 if (LHSResult.Val >= RHSResult.Val)
1174 return LHSResult;
1175 return RHSResult;
1176 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001178 case BinaryOperator::LOr: {
1179 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1180 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1181 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1182 // Rare case where the RHS has a comma "side-effect"; we need
1183 // to actually check the condition to see whether the side
1184 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001185 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001186 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001187 return RHSResult;
1188 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001189 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001190
Eli Friedmane28d7192009-02-26 09:29:13 +00001191 if (LHSResult.Val >= RHSResult.Val)
1192 return LHSResult;
1193 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001195 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001197 case Expr::ImplicitCastExprClass:
1198 case Expr::CStyleCastExprClass:
1199 case Expr::CXXFunctionalCastExprClass: {
1200 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1201 if (SubExpr->getType()->isIntegralType())
1202 return CheckICE(SubExpr, Ctx);
1203 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1204 return NoDiag();
1205 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001207 case Expr::ConditionalOperatorClass: {
1208 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001209 // If the condition (ignoring parens) is a __builtin_constant_p call,
1210 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001211 // expression, and it is fully evaluated. This is an important GNU
1212 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001213 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001214 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001215 Expr::EvalResult EVResult;
1216 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1217 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001218 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001219 }
1220 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001221 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001222 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1223 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1224 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1225 if (CondResult.Val == 2)
1226 return CondResult;
1227 if (TrueResult.Val == 2)
1228 return TrueResult;
1229 if (FalseResult.Val == 2)
1230 return FalseResult;
1231 if (CondResult.Val == 1)
1232 return CondResult;
1233 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1234 return NoDiag();
1235 // Rare case where the diagnostics depend on which side is evaluated
1236 // Note that if we get here, CondResult is 0, and at least one of
1237 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001238 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001239 return FalseResult;
1240 }
1241 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001243 case Expr::CXXDefaultArgExprClass:
1244 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001245 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001246 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001247 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001249}
Reid Spencer5f016e22007-07-11 17:01:13 +00001250
Eli Friedmane28d7192009-02-26 09:29:13 +00001251bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1252 SourceLocation *Loc, bool isEvaluated) const {
1253 ICEDiag d = CheckICE(this, Ctx);
1254 if (d.Val != 0) {
1255 if (Loc) *Loc = d.Loc;
1256 return false;
1257 }
1258 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001259 if (!Evaluate(EvalResult, Ctx))
1260 assert(0 && "ICE cannot be evaluated!");
1261 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1262 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001263 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 return true;
1265}
1266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1268/// integer constant expression with the value zero, or if this is one that is
1269/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001270bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1271{
Sebastian Redl07779722008-10-31 14:43:28 +00001272 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001273 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001274 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001275 // Check that it is a cast to void*.
1276 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1277 QualType Pointee = PT->getPointeeType();
1278 if (Pointee.getCVRQualifiers() == 0 &&
1279 Pointee->isVoidType() && // to void*
1280 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001281 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001282 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001284 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1285 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001286 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001287 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1288 // Accept ((void*)0) as a null pointer constant, as many other
1289 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001290 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001291 } else if (const CXXDefaultArgExpr *DefaultArg
1292 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001293 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001294 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001295 } else if (isa<GNUNullExpr>(this)) {
1296 // The GNU __null extension is always a null pointer constant.
1297 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001298 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001299
Steve Naroffaa58f002008-01-14 16:10:57 +00001300 // This expression must be an integer type.
1301 if (!getType()->isIntegerType())
1302 return false;
1303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 // If we have an integer constant expression, we need to *evaluate* it and
1305 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001306 // FIXME: We should probably return false if we're compiling in strict mode
1307 // and Diag is not null (this indicates that the value was foldable but not
1308 // an ICE.
1309 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001310 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001311 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001312}
Steve Naroff31a45842007-07-28 23:10:27 +00001313
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001314/// isBitField - Return true if this expression is a bit-field.
1315bool Expr::isBitField() {
1316 Expr *E = this->IgnoreParenCasts();
1317 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001318 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1319 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001320 return false;
1321}
1322
Chris Lattner2140e902009-02-16 22:14:05 +00001323/// isArrow - Return true if the base expression is a pointer to vector,
1324/// return false if the base expression is a vector.
1325bool ExtVectorElementExpr::isArrow() const {
1326 return getBase()->getType()->isPointerType();
1327}
1328
Nate Begeman213541a2008-04-18 23:10:10 +00001329unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001330 if (const VectorType *VT = getType()->getAsVectorType())
1331 return VT->getNumElements();
1332 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001333}
1334
Nate Begeman8a997642008-05-09 06:41:27 +00001335/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001336bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001337 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001338 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001339
1340 // Halving swizzles do not contain duplicate elements.
1341 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1342 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1343 return false;
1344
1345 // Advance past s-char prefix on hex swizzles.
1346 if (*compStr == 's') {
1347 compStr++;
1348 length--;
1349 }
Steve Narofffec0b492007-07-30 03:29:09 +00001350
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001351 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001352 const char *s = compStr+i;
1353 for (const char c = *s++; *s; s++)
1354 if (c == *s)
1355 return true;
1356 }
1357 return false;
1358}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001359
Nate Begeman8a997642008-05-09 06:41:27 +00001360/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001361void ExtVectorElementExpr::getEncodedElementAccess(
1362 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001363 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001364 if (*compStr == 's')
1365 compStr++;
1366
1367 bool isHi = !strcmp(compStr, "hi");
1368 bool isLo = !strcmp(compStr, "lo");
1369 bool isEven = !strcmp(compStr, "even");
1370 bool isOdd = !strcmp(compStr, "odd");
1371
Nate Begeman8a997642008-05-09 06:41:27 +00001372 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1373 uint64_t Index;
1374
1375 if (isHi)
1376 Index = e + i;
1377 else if (isLo)
1378 Index = i;
1379 else if (isEven)
1380 Index = 2 * i;
1381 else if (isOdd)
1382 Index = 2 * i + 1;
1383 else
1384 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001385
Nate Begeman3b8d1162008-05-13 21:03:02 +00001386 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001387 }
Nate Begeman8a997642008-05-09 06:41:27 +00001388}
1389
Steve Naroff68d331a2007-09-27 14:38:14 +00001390// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001391ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001392 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001393 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001394 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001395 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001396 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001397 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001398 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001399 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001400 if (NumArgs) {
1401 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001402 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1403 }
Steve Naroff563477d2007-09-18 23:55:05 +00001404 LBracloc = LBrac;
1405 RBracloc = RBrac;
1406}
1407
Steve Naroff68d331a2007-09-27 14:38:14 +00001408// constructor for class messages.
1409// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001410ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001411 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001412 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001413 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001414 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001415 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001416 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001417 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001418 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001419 if (NumArgs) {
1420 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001421 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1422 }
Steve Naroff563477d2007-09-18 23:55:05 +00001423 LBracloc = LBrac;
1424 RBracloc = RBrac;
1425}
1426
Ted Kremenek4df728e2008-06-24 15:50:53 +00001427// constructor for class messages.
1428ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1429 QualType retType, ObjCMethodDecl *mproto,
1430 SourceLocation LBrac, SourceLocation RBrac,
1431 Expr **ArgExprs, unsigned nargs)
1432: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1433MethodProto(mproto) {
1434 NumArgs = nargs;
1435 SubExprs = new Stmt*[NumArgs+1];
1436 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1437 if (NumArgs) {
1438 for (unsigned i = 0; i != NumArgs; ++i)
1439 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1440 }
1441 LBracloc = LBrac;
1442 RBracloc = RBrac;
1443}
1444
1445ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1446 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1447 switch (x & Flags) {
1448 default:
1449 assert(false && "Invalid ObjCMessageExpr.");
1450 case IsInstMeth:
1451 return ClassInfo(0, 0);
1452 case IsClsMethDeclUnknown:
1453 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1454 case IsClsMethDeclKnown: {
1455 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1456 return ClassInfo(D, D->getIdentifier());
1457 }
1458 }
1459}
1460
Chris Lattner27437ca2007-10-25 00:29:32 +00001461bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001462 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001463}
1464
Sebastian Redl05189992008-11-11 17:56:53 +00001465void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1466 // Override default behavior of traversing children. If this has a type
1467 // operand and the type is a variable-length array, the child iteration
1468 // will iterate over the size expression. However, this expression belongs
1469 // to the type, not to this, so we don't want to delete it.
1470 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001471 if (isArgumentType()) {
1472 this->~SizeOfAlignOfExpr();
1473 C.Deallocate(this);
1474 }
Sebastian Redl05189992008-11-11 17:56:53 +00001475 else
1476 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001477}
1478
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001479//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001480// DesignatedInitExpr
1481//===----------------------------------------------------------------------===//
1482
1483IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1484 assert(Kind == FieldDesignator && "Only valid on a field designator");
1485 if (Field.NameOrField & 0x01)
1486 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1487 else
1488 return getField()->getIdentifier();
1489}
1490
1491DesignatedInitExpr *
1492DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1493 unsigned NumDesignators,
1494 Expr **IndexExprs, unsigned NumIndexExprs,
1495 SourceLocation ColonOrEqualLoc,
1496 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001497 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1498 sizeof(Designator) * NumDesignators +
1499 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001500 DesignatedInitExpr *DIE
1501 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1502 ColonOrEqualLoc, UsesColonSyntax,
1503 NumIndexExprs + 1);
1504
1505 // Fill in the designators
1506 unsigned ExpectedNumSubExprs = 0;
1507 designators_iterator Desig = DIE->designators_begin();
1508 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1509 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1510 if (Designators[Idx].isArrayDesignator())
1511 ++ExpectedNumSubExprs;
1512 else if (Designators[Idx].isArrayRangeDesignator())
1513 ExpectedNumSubExprs += 2;
1514 }
1515 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1516
1517 // Fill in the subexpressions, including the initializer expression.
1518 child_iterator Child = DIE->child_begin();
1519 *Child++ = Init;
1520 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1521 *Child = IndexExprs[Idx];
1522
1523 return DIE;
1524}
1525
1526SourceRange DesignatedInitExpr::getSourceRange() const {
1527 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001528 Designator &First =
1529 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001530 if (First.isFieldDesignator()) {
1531 if (UsesColonSyntax)
1532 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1533 else
1534 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1535 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001536 StartLoc =
1537 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001538 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1539}
1540
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001541DesignatedInitExpr::designators_iterator
1542DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001543 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1544 Ptr += sizeof(DesignatedInitExpr);
1545 return static_cast<Designator*>(static_cast<void*>(Ptr));
1546}
1547
1548DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1549 return designators_begin() + NumDesignators;
1550}
1551
1552Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1553 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1554 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1555 Ptr += sizeof(DesignatedInitExpr);
1556 Ptr += sizeof(Designator) * NumDesignators;
1557 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1558 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1559}
1560
1561Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1562 assert(D.Kind == Designator::ArrayRangeDesignator &&
1563 "Requires array range designator");
1564 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1565 Ptr += sizeof(DesignatedInitExpr);
1566 Ptr += sizeof(Designator) * NumDesignators;
1567 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1568 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1569}
1570
1571Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1572 assert(D.Kind == Designator::ArrayRangeDesignator &&
1573 "Requires array range designator");
1574 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1575 Ptr += sizeof(DesignatedInitExpr);
1576 Ptr += sizeof(Designator) * NumDesignators;
1577 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1578 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1579}
1580
1581//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001582// ExprIterator.
1583//===----------------------------------------------------------------------===//
1584
1585Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1586Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1587Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1588const Expr* ConstExprIterator::operator[](size_t idx) const {
1589 return cast<Expr>(I[idx]);
1590}
1591const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1592const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1593
1594//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001595// Child Iterators for iterating over subexpressions/substatements
1596//===----------------------------------------------------------------------===//
1597
1598// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001599Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1600Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001601
Steve Naroff7779db42007-11-12 14:29:37 +00001602// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001603Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1604Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001605
Steve Naroffe3e9add2008-06-02 23:03:37 +00001606// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001607Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1608Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001609
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001610// ObjCKVCRefExpr
1611Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1612Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1613
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001614// ObjCSuperExpr
1615Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1616Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1617
Chris Lattnerd9f69102008-08-10 01:53:14 +00001618// PredefinedExpr
1619Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1620Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001621
1622// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001623Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1624Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001625
1626// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001627Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001628Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001629
1630// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001631Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1632Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001633
Chris Lattner5d661452007-08-26 03:42:43 +00001634// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001635Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1636Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001637
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001638// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001639Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1640Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001641
1642// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001643Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1644Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001645
1646// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001647Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1648Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001649
Sebastian Redl05189992008-11-11 17:56:53 +00001650// SizeOfAlignOfExpr
1651Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1652 // If this is of a type and the type is a VLA type (and not a typedef), the
1653 // size expression of the VLA needs to be treated as an executable expression.
1654 // Why isn't this weirdness documented better in StmtIterator?
1655 if (isArgumentType()) {
1656 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1657 getArgumentType().getTypePtr()))
1658 return child_iterator(T);
1659 return child_iterator();
1660 }
Sebastian Redld4575892008-12-03 23:17:54 +00001661 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001662}
Sebastian Redl05189992008-11-11 17:56:53 +00001663Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1664 if (isArgumentType())
1665 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001666 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001667}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001668
1669// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001670Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001671 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001672}
Ted Kremenek1237c672007-08-24 20:06:47 +00001673Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001674 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001675}
1676
1677// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001678Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001679 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001680}
Ted Kremenek1237c672007-08-24 20:06:47 +00001681Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001682 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001683}
Ted Kremenek1237c672007-08-24 20:06:47 +00001684
1685// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001686Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1687Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001688
Nate Begeman213541a2008-04-18 23:10:10 +00001689// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001690Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1691Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001692
1693// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001694Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1695Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001696
Ted Kremenek1237c672007-08-24 20:06:47 +00001697// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001698Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1699Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001700
1701// BinaryOperator
1702Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001703 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001704}
Ted Kremenek1237c672007-08-24 20:06:47 +00001705Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001706 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001707}
1708
1709// ConditionalOperator
1710Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001711 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001712}
Ted Kremenek1237c672007-08-24 20:06:47 +00001713Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001714 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001715}
1716
1717// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001718Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1719Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001720
Ted Kremenek1237c672007-08-24 20:06:47 +00001721// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001722Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1723Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001724
1725// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001726Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1727 return child_iterator();
1728}
1729
1730Stmt::child_iterator TypesCompatibleExpr::child_end() {
1731 return child_iterator();
1732}
Ted Kremenek1237c672007-08-24 20:06:47 +00001733
1734// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001735Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1736Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001737
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001738// GNUNullExpr
1739Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1740Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1741
Eli Friedmand38617c2008-05-14 19:38:39 +00001742// ShuffleVectorExpr
1743Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001744 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001745}
1746Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001747 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001748}
1749
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001750// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001751Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1752Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001753
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001754// InitListExpr
1755Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001756 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001757}
1758Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001759 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001760}
1761
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001762// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001763Stmt::child_iterator DesignatedInitExpr::child_begin() {
1764 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1765 Ptr += sizeof(DesignatedInitExpr);
1766 Ptr += sizeof(Designator) * NumDesignators;
1767 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1768}
1769Stmt::child_iterator DesignatedInitExpr::child_end() {
1770 return child_iterator(&*child_begin() + NumSubExprs);
1771}
1772
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001773// ImplicitValueInitExpr
1774Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1775 return child_iterator();
1776}
1777
1778Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1779 return child_iterator();
1780}
1781
Ted Kremenek1237c672007-08-24 20:06:47 +00001782// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001783Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001784 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001785}
1786Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001787 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001788}
Ted Kremenek1237c672007-08-24 20:06:47 +00001789
1790// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001791Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1792Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001793
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001794// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001795Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1796 return child_iterator();
1797}
1798Stmt::child_iterator ObjCSelectorExpr::child_end() {
1799 return child_iterator();
1800}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001801
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001802// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001803Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1804 return child_iterator();
1805}
1806Stmt::child_iterator ObjCProtocolExpr::child_end() {
1807 return child_iterator();
1808}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001809
Steve Naroff563477d2007-09-18 23:55:05 +00001810// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001811Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001812 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001813}
1814Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001815 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001816}
1817
Steve Naroff4eb206b2008-09-03 18:15:37 +00001818// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001819Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1820Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001821
Ted Kremenek9da13f92008-09-26 23:24:14 +00001822Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1823Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }