blob: 273b5ed72f797c761ab38634911f07fe992ad1af [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
Chris Lattnerda8249e2008-06-07 22:13:43 +000029/// getValueAsApproximateDouble - This returns the value as an inaccurate
30/// double. Note that this may cause loss of precision, but is useful for
31/// debugging dumps, etc.
32double FloatingLiteral::getValueAsApproximateDouble() const {
33 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
Chris Lattner2085fd62009-02-18 06:40:38 +000040StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
41 unsigned ByteLength, bool Wide,
42 QualType Ty,
43 SourceLocation *Loc, unsigned NumStrs) {
44 // Allocate enough space for the StringLiteral plus an array of locations for
45 // any concatenated string tokens.
46 void *Mem = C.Allocate(sizeof(StringLiteral)+
47 sizeof(SourceLocation)*(NumStrs-1),
48 llvm::alignof<StringLiteral>());
49 StringLiteral *SL = new (Mem) StringLiteral(Ty);
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000052 char *AStrData = new (C, 1) char[ByteLength];
53 memcpy(AStrData, StrData, ByteLength);
54 SL->StrData = AStrData;
55 SL->ByteLength = ByteLength;
56 SL->IsWide = Wide;
57 SL->TokLocs[0] = Loc[0];
58 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000059
Chris Lattner726e1682009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000063}
64
65
Ted Kremenek6e94ef52009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000070}
71
Reid Spencer5f016e22007-07-11 17:01:13 +000072/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
73/// corresponds to, e.g. "sizeof" or "[pre]++".
74const char *UnaryOperator::getOpcodeStr(Opcode Op) {
75 switch (Op) {
76 default: assert(0 && "Unknown unary operator");
77 case PostInc: return "++";
78 case PostDec: return "--";
79 case PreInc: return "++";
80 case PreDec: return "--";
81 case AddrOf: return "&";
82 case Deref: return "*";
83 case Plus: return "+";
84 case Minus: return "-";
85 case Not: return "~";
86 case LNot: return "!";
87 case Real: return "__real";
88 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +000089 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000091 }
92}
93
Douglas Gregorbc736fc2009-03-13 23:49:33 +000094UnaryOperator::Opcode
95UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
96 switch (OO) {
97 case OO_PlusPlus: return Postfix? PostInc : PreInc;
98 case OO_MinusMinus: return Postfix? PostDec : PreDec;
99 case OO_Amp: return AddrOf;
100 case OO_Star: return Deref;
101 case OO_Plus: return Plus;
102 case OO_Minus: return Minus;
103 case OO_Tilde: return Not;
104 case OO_Exclaim: return LNot;
105 default: assert(false && "No unary operator for overloaded function");
106 }
107}
108
109OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
110 switch (Opc) {
111 case PostInc: case PreInc: return OO_PlusPlus;
112 case PostDec: case PreDec: return OO_MinusMinus;
113 case AddrOf: return OO_Amp;
114 case Deref: return OO_Star;
115 case Plus: return OO_Plus;
116 case Minus: return OO_Minus;
117 case Not: return OO_Tilde;
118 case LNot: return OO_Exclaim;
119 default: return OO_None;
120 }
121}
122
123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124//===----------------------------------------------------------------------===//
125// Postfix Operators.
126//===----------------------------------------------------------------------===//
127
Ted Kremenek668bf912009-02-09 20:51:47 +0000128CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000129 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000130 : Expr(SC, t,
131 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000132 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000133 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000134
135 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000136 SubExprs[FN] = fn;
137 for (unsigned i = 0; i != numargs; ++i)
138 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000139
Douglas Gregorb4609802008-11-14 16:09:21 +0000140 RParenLoc = rparenloc;
141}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000142
Ted Kremenek668bf912009-02-09 20:51:47 +0000143CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
144 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000145 : Expr(CallExprClass, t,
146 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000147 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000148 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000149
150 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000151 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000153 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 RParenLoc = rparenloc;
156}
157
Ted Kremenek668bf912009-02-09 20:51:47 +0000158void CallExpr::Destroy(ASTContext& C) {
159 DestroyChildren(C);
160 if (SubExprs) C.Deallocate(SubExprs);
161 this->~CallExpr();
162 C.Deallocate(this);
163}
164
Chris Lattnerd18b3292007-12-28 05:25:02 +0000165/// setNumArgs - This changes the number of arguments present in this call.
166/// Any orphaned expressions are deleted by this, and any new operands are set
167/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000168void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000169 // No change, just return.
170 if (NumArgs == getNumArgs()) return;
171
172 // If shrinking # arguments, just delete the extras and forgot them.
173 if (NumArgs < getNumArgs()) {
174 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000175 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000176 this->NumArgs = NumArgs;
177 return;
178 }
179
180 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000181 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000182 // Copy over args.
183 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
184 NewSubExprs[i] = SubExprs[i];
185 // Null out new args.
186 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
187 NewSubExprs[i] = 0;
188
Ted Kremenek8189cde2009-02-07 01:47:29 +0000189 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000190 SubExprs = NewSubExprs;
191 this->NumArgs = NumArgs;
192}
193
Chris Lattnercb888962008-10-06 05:00:53 +0000194/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
195/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000196unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000197 // All simple function calls (e.g. func()) are implicitly cast to pointer to
198 // function. As a result, we try and obtain the DeclRefExpr from the
199 // ImplicitCastExpr.
200 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
201 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000202 return 0;
203
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000204 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
205 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000206 return 0;
207
Anders Carlssonbcba2012008-01-31 02:13:57 +0000208 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
209 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000210 return 0;
211
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000212 if (!FDecl->getIdentifier())
213 return 0;
214
Douglas Gregor3c385e52009-02-14 18:57:46 +0000215 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000216}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000217
Chris Lattnercb888962008-10-06 05:00:53 +0000218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
220/// corresponds to, e.g. "<<=".
221const char *BinaryOperator::getOpcodeStr(Opcode Op) {
222 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000223 case PtrMemD: return ".*";
224 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 case Mul: return "*";
226 case Div: return "/";
227 case Rem: return "%";
228 case Add: return "+";
229 case Sub: return "-";
230 case Shl: return "<<";
231 case Shr: return ">>";
232 case LT: return "<";
233 case GT: return ">";
234 case LE: return "<=";
235 case GE: return ">=";
236 case EQ: return "==";
237 case NE: return "!=";
238 case And: return "&";
239 case Xor: return "^";
240 case Or: return "|";
241 case LAnd: return "&&";
242 case LOr: return "||";
243 case Assign: return "=";
244 case MulAssign: return "*=";
245 case DivAssign: return "/=";
246 case RemAssign: return "%=";
247 case AddAssign: return "+=";
248 case SubAssign: return "-=";
249 case ShlAssign: return "<<=";
250 case ShrAssign: return ">>=";
251 case AndAssign: return "&=";
252 case XorAssign: return "^=";
253 case OrAssign: return "|=";
254 case Comma: return ",";
255 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000256
257 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
Douglas Gregor063daf62009-03-13 18:40:31 +0000260BinaryOperator::Opcode
261BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
262 switch (OO) {
263 case OO_Plus: return Add;
264 case OO_Minus: return Sub;
265 case OO_Star: return Mul;
266 case OO_Slash: return Div;
267 case OO_Percent: return Rem;
268 case OO_Caret: return Xor;
269 case OO_Amp: return And;
270 case OO_Pipe: return Or;
271 case OO_Equal: return Assign;
272 case OO_Less: return LT;
273 case OO_Greater: return GT;
274 case OO_PlusEqual: return AddAssign;
275 case OO_MinusEqual: return SubAssign;
276 case OO_StarEqual: return MulAssign;
277 case OO_SlashEqual: return DivAssign;
278 case OO_PercentEqual: return RemAssign;
279 case OO_CaretEqual: return XorAssign;
280 case OO_AmpEqual: return AndAssign;
281 case OO_PipeEqual: return OrAssign;
282 case OO_LessLess: return Shl;
283 case OO_GreaterGreater: return Shr;
284 case OO_LessLessEqual: return ShlAssign;
285 case OO_GreaterGreaterEqual: return ShrAssign;
286 case OO_EqualEqual: return EQ;
287 case OO_ExclaimEqual: return NE;
288 case OO_LessEqual: return LE;
289 case OO_GreaterEqual: return GE;
290 case OO_AmpAmp: return LAnd;
291 case OO_PipePipe: return LOr;
292 case OO_Comma: return Comma;
293 case OO_ArrowStar: return PtrMemI;
294 default: assert(false && "Not an overloadable binary operator");
295 }
296}
297
298OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
299 static const OverloadedOperatorKind OverOps[] = {
300 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
301 OO_Star, OO_Slash, OO_Percent,
302 OO_Plus, OO_Minus,
303 OO_LessLess, OO_GreaterGreater,
304 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
305 OO_EqualEqual, OO_ExclaimEqual,
306 OO_Amp,
307 OO_Caret,
308 OO_Pipe,
309 OO_AmpAmp,
310 OO_PipePipe,
311 OO_Equal, OO_StarEqual,
312 OO_SlashEqual, OO_PercentEqual,
313 OO_PlusEqual, OO_MinusEqual,
314 OO_LessLessEqual, OO_GreaterGreaterEqual,
315 OO_AmpEqual, OO_CaretEqual,
316 OO_PipeEqual,
317 OO_Comma
318 };
319 return OverOps[Opc];
320}
321
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000322InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000323 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000324 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000325 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000326 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000327 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000328
329 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000330}
Reid Spencer5f016e22007-07-11 17:01:13 +0000331
Douglas Gregor4c678342009-01-28 21:54:33 +0000332void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000333 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000334 Idx < LastIdx; ++Idx)
Douglas Gregor4c678342009-01-28 21:54:33 +0000335 delete InitExprs[Idx];
336 InitExprs.resize(NumInits, 0);
337}
338
339Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
340 if (Init >= InitExprs.size()) {
341 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
342 InitExprs.back() = expr;
343 return 0;
344 }
345
346 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
347 InitExprs[Init] = expr;
348 return Result;
349}
350
Steve Naroffbfdcae62008-09-04 15:31:07 +0000351/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000352///
353const FunctionType *BlockExpr::getFunctionType() const {
354 return getType()->getAsBlockPointerType()->
355 getPointeeType()->getAsFunctionType();
356}
357
Steve Naroff56ee6892008-10-08 17:01:13 +0000358SourceLocation BlockExpr::getCaretLocation() const {
359 return TheBlock->getCaretLocation();
360}
361const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
362Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
363
364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365//===----------------------------------------------------------------------===//
366// Generic Expression Routines
367//===----------------------------------------------------------------------===//
368
Chris Lattner026dc962009-02-14 07:37:35 +0000369/// isUnusedResultAWarning - Return true if this immediate expression should
370/// be warned about if the result is unused. If so, fill in Loc and Ranges
371/// with location to warn on and the source range[s] to report with the
372/// warning.
373bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
374 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 switch (getStmtClass()) {
376 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000377 Loc = getExprLoc();
378 R1 = getSourceRange();
379 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000381 return cast<ParenExpr>(this)->getSubExpr()->
382 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 case UnaryOperatorClass: {
384 const UnaryOperator *UO = cast<UnaryOperator>(this);
385
386 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000387 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 case UnaryOperator::PostInc:
389 case UnaryOperator::PostDec:
390 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000391 case UnaryOperator::PreDec: // ++/--
392 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 case UnaryOperator::Deref:
394 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000395 if (getType().isVolatileQualified())
396 return false;
397 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 case UnaryOperator::Real:
399 case UnaryOperator::Imag:
400 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000401 if (UO->getSubExpr()->getType().isVolatileQualified())
402 return false;
403 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000405 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 }
Chris Lattner026dc962009-02-14 07:37:35 +0000407 Loc = UO->getOperatorLoc();
408 R1 = UO->getSubExpr()->getSourceRange();
409 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000411 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000412 const BinaryOperator *BO = cast<BinaryOperator>(this);
413 // Consider comma to have side effects if the LHS or RHS does.
414 if (BO->getOpcode() == BinaryOperator::Comma)
415 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
416 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000417
Chris Lattner026dc962009-02-14 07:37:35 +0000418 if (BO->isAssignmentOp())
419 return false;
420 Loc = BO->getOperatorLoc();
421 R1 = BO->getLHS()->getSourceRange();
422 R2 = BO->getRHS()->getSourceRange();
423 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000424 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000425 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000426 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000428 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000429 // The condition must be evaluated, but if either the LHS or RHS is a
430 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000431 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000432 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000433 return true;
434 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000435 }
436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000438 // If the base pointer or element is to a volatile pointer/field, accessing
439 // it is a side effect.
440 if (getType().isVolatileQualified())
441 return false;
442 Loc = cast<MemberExpr>(this)->getMemberLoc();
443 R1 = SourceRange(Loc, Loc);
444 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
445 return true;
446
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 case ArraySubscriptExprClass:
448 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000449 // it is a side effect.
450 if (getType().isVolatileQualified())
451 return false;
452 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
453 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
454 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
455 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000458 case CXXOperatorCallExprClass: {
459 // If this is a direct call, get the callee.
460 const CallExpr *CE = cast<CallExpr>(this);
461 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
462 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
463 // If the callee has attribute pure, const, or warn_unused_result, warn
464 // about it. void foo() { strlen("bar"); } should warn.
465 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
466 if (FD->getAttr<WarnUnusedResultAttr>() ||
467 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
468 Loc = CE->getCallee()->getLocStart();
469 R1 = CE->getCallee()->getSourceRange();
470
471 if (unsigned NumArgs = CE->getNumArgs())
472 R2 = SourceRange(CE->getArg(0)->getLocStart(),
473 CE->getArg(NumArgs-1)->getLocEnd());
474 return true;
475 }
476 }
477 return false;
478 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000479 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000480 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000481 case StmtExprClass: {
482 // Statement exprs don't logically have side effects themselves, but are
483 // sometimes used in macros in ways that give them a type that is unused.
484 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
485 // however, if the result of the stmt expr is dead, we don't want to emit a
486 // warning.
487 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
488 if (!CS->body_empty())
489 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000490 return E->isUnusedResultAWarning(Loc, R1, R2);
491
492 Loc = cast<StmtExpr>(this)->getLParenLoc();
493 R1 = getSourceRange();
494 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000495 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000496 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000497 // If this is a cast to void, check the operand. Otherwise, the result of
498 // the cast is unused.
499 if (getType()->isVoidType())
500 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
501 R1, R2);
502 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
503 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
504 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000505 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 // If this is a cast to void, check the operand. Otherwise, the result of
507 // the cast is unused.
508 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000509 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
510 R1, R2);
511 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
512 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
513 return true;
514
Eli Friedman4be1f472008-05-19 21:24:43 +0000515 case ImplicitCastExprClass:
516 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000517 return cast<ImplicitCastExpr>(this)
518 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000519
Chris Lattner04421082008-04-08 04:40:51 +0000520 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000521 return cast<CXXDefaultArgExpr>(this)
522 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000523
524 case CXXNewExprClass:
525 // FIXME: In theory, there might be new expressions that don't have side
526 // effects (e.g. a placement new with an uninitialized POD).
527 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000528 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000529 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000530}
531
Douglas Gregorba7e2102008-10-22 15:04:37 +0000532/// DeclCanBeLvalue - Determine whether the given declaration can be
533/// an lvalue. This is a helper routine for isLvalue.
534static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000535 // C++ [temp.param]p6:
536 // A non-type non-reference template-parameter is not an lvalue.
537 if (const NonTypeTemplateParmDecl *NTTParm
538 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
539 return NTTParm->getType()->isReferenceType();
540
Douglas Gregor44b43212008-12-11 16:49:14 +0000541 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000542 // C++ 3.10p2: An lvalue refers to an object or function.
543 (Ctx.getLangOptions().CPlusPlus &&
544 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
545}
546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
548/// incomplete type other than void. Nonarray expressions that can be lvalues:
549/// - name, where name must be a variable
550/// - e[i]
551/// - (e), where e must be an lvalue
552/// - e.name, where e must be an lvalue
553/// - e->name
554/// - *e, the type of e cannot be a function type
555/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000556/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000557/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000558///
Chris Lattner28be73f2008-07-26 21:30:36 +0000559Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000560 // first, check the type (C99 6.3.2.1). Expressions with function
561 // type in C are not lvalues, but they can be lvalues in C++.
562 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 return LV_NotObjectType;
564
Steve Naroffacb818a2008-02-10 01:39:04 +0000565 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000566 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000567 return LV_IncompleteVoidType;
568
Douglas Gregor98cd5992008-10-21 23:43:52 +0000569 /// FIXME: Expressions can't have reference type, so the following
570 /// isn't needed.
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000571 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000572 return LV_Valid;
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // the type looks fine, now check the expression
575 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000576 case StringLiteralClass: // C99 6.5.1p4
577 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000578 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
580 // For vectors, make sure base is an lvalue (i.e. not a function call).
581 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000582 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000584 case DeclRefExprClass:
585 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000586 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
587 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 return LV_Valid;
589 break;
Chris Lattner41110242008-06-17 18:05:57 +0000590 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000591 case BlockDeclRefExprClass: {
592 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000593 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000594 return LV_Valid;
595 break;
596 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000597 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000599 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
600 NamedDecl *Member = m->getMemberDecl();
601 // C++ [expr.ref]p4:
602 // If E2 is declared to have type "reference to T", then E1.E2
603 // is an lvalue.
604 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
605 if (Value->getType()->isReferenceType())
606 return LV_Valid;
607
608 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000609 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000610 return LV_Valid;
611
612 // -- If E2 is a non-static data member [...]. If E1 is an
613 // lvalue, then E1.E2 is an lvalue.
614 if (isa<FieldDecl>(Member))
615 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
616
617 // -- If it refers to a static member function [...], then
618 // E1.E2 is an lvalue.
619 // -- Otherwise, if E1.E2 refers to a non-static member
620 // function [...], then E1.E2 is not an lvalue.
621 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
622 return Method->isStatic()? LV_Valid : LV_MemberFunction;
623
624 // -- If E2 is a member enumerator [...], the expression E1.E2
625 // is not an lvalue.
626 if (isa<EnumConstantDecl>(Member))
627 return LV_InvalidExpression;
628
629 // Not an lvalue.
630 return LV_InvalidExpression;
631 }
632
633 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000634 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000635 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000636 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000638 return LV_Valid; // C99 6.5.3p4
639
640 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000641 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
642 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000643 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000644
645 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
646 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
647 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
648 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000650 case ImplicitCastExprClass:
651 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
652 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000654 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000655 case BinaryOperatorClass:
656 case CompoundAssignOperatorClass: {
657 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000658
659 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
660 BinOp->getOpcode() == BinaryOperator::Comma)
661 return BinOp->getRHS()->isLvalue(Ctx);
662
Sebastian Redl22460502009-02-07 00:15:38 +0000663 // C++ [expr.mptr.oper]p6
664 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
665 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
666 !BinOp->getType()->isFunctionType())
667 return BinOp->getLHS()->isLvalue(Ctx);
668
Douglas Gregorbf3af052008-11-13 20:12:29 +0000669 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000670 return LV_InvalidExpression;
671
Douglas Gregorbf3af052008-11-13 20:12:29 +0000672 if (Ctx.getLangOptions().CPlusPlus)
673 // C++ [expr.ass]p1:
674 // The result of an assignment operation [...] is an lvalue.
675 return LV_Valid;
676
677
678 // C99 6.5.16:
679 // An assignment expression [...] is not an lvalue.
680 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000681 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000682 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000683 case CXXOperatorCallExprClass:
684 case CXXMemberCallExprClass: {
Douglas Gregor9d293df2008-10-28 00:22:11 +0000685 // C++ [expr.call]p10:
686 // A function call is an lvalue if and only if the result type
687 // is a reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000688 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000689 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000690 CalleeType = FnTypePtr->getPointeeType();
691 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
692 if (FnType->getResultType()->isReferenceType())
693 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000694
695 break;
696 }
Steve Naroffe6386392007-12-05 04:00:10 +0000697 case CompoundLiteralExprClass: // C99 6.5.2.5p5
698 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000699 case ChooseExprClass:
700 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000701 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000702 case ExtVectorElementExprClass:
703 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000704 return LV_DuplicateVectorComponents;
705 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000706 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
707 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000708 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
709 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000710 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000711 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000712 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000713 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000714 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000715 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000716 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000717 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000718 case CXXConditionDeclExprClass:
719 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000720 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000721 case CXXFunctionalCastExprClass:
722 case CXXStaticCastExprClass:
723 case CXXDynamicCastExprClass:
724 case CXXReinterpretCastExprClass:
725 case CXXConstCastExprClass:
726 // The result of an explicit cast is an lvalue if the type we are
727 // casting to is a reference type. See C++ [expr.cast]p1,
728 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
729 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
730 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType())
731 return LV_Valid;
732 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000733 case CXXTypeidExprClass:
734 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
735 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 default:
737 break;
738 }
739 return LV_InvalidExpression;
740}
741
742/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
743/// does not have an incomplete type, does not have a const-qualified type, and
744/// if it is a structure or union, does not have any member (including,
745/// recursively, any member or element of all contained aggregates or unions)
746/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000747Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
748 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
750 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000751 case LV_Valid:
752 // C++ 3.10p11: Functions cannot be modified, but pointers to
753 // functions can be modifiable.
754 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
755 return MLV_NotObjectType;
756 break;
757
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 case LV_NotObjectType: return MLV_NotObjectType;
759 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000760 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000761 case LV_InvalidExpression:
762 // If the top level is a C-style cast, and the subexpression is a valid
763 // lvalue, then this is probably a use of the old-school "cast as lvalue"
764 // GCC extension. We don't support it, but we want to produce good
765 // diagnostics when it happens so that the user knows why.
766 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
767 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
768 return MLV_LValueCast;
769 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000770 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 }
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000772
773 QualType CT = Ctx.getCanonicalType(getType());
774
775 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000777 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000779 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 return MLV_IncompleteType;
781
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000782 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 if (r->hasConstFields())
784 return MLV_ConstQualified;
785 }
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000786 // The following is illegal:
787 // void takeclosure(void (^C)(void));
788 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
789 //
790 if (getStmtClass() == BlockDeclRefExprClass) {
791 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
792 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
793 return MLV_NotBlockQualified;
794 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000795
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000796 // Assigning to an 'implicit' property?
Fariborz Jahanian6669db92008-11-25 17:56:43 +0000797 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000798 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
799 if (KVCExpr->getSetterMethod() == 0)
800 return MLV_NoSetterProperty;
801 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 return MLV_Valid;
803}
804
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000805/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000806/// duration. This means that the address of this expression is a link-time
807/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000808bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000809 switch (getStmtClass()) {
810 default:
811 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000812 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000813 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000814 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000815 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000816 case CompoundLiteralExprClass:
817 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000818 case DeclRefExprClass:
819 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000820 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
821 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000822 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000823 if (isa<FunctionDecl>(D))
824 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000825 return false;
826 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000827 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000828 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000829 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000830 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000831 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000832 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000833 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000834 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000835 case CXXDefaultArgExprClass:
836 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000837 }
838}
839
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000840/// isOBJCGCCandidate - Check if an expression is objc gc'able.
841///
842bool Expr::isOBJCGCCandidate() const {
843 switch (getStmtClass()) {
844 default:
845 return false;
846 case ObjCIvarRefExprClass:
847 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000848 case Expr::UnaryOperatorClass:
849 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000850 case ParenExprClass:
851 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
852 case ImplicitCastExprClass:
853 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
854 case DeclRefExprClass:
855 case QualifiedDeclRefExprClass: {
856 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
857 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
858 return VD->hasGlobalStorage();
859 return false;
860 }
861 case MemberExprClass: {
862 const MemberExpr *M = cast<MemberExpr>(this);
863 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
864 }
865 case ArraySubscriptExprClass:
866 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
867 }
868}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000869Expr* Expr::IgnoreParens() {
870 Expr* E = this;
871 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
872 E = P->getSubExpr();
873
874 return E;
875}
876
Chris Lattner56f34942008-02-13 01:02:39 +0000877/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
878/// or CastExprs or ImplicitCastExprs, returning their operand.
879Expr *Expr::IgnoreParenCasts() {
880 Expr *E = this;
881 while (true) {
882 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
883 E = P->getSubExpr();
884 else if (CastExpr *P = dyn_cast<CastExpr>(E))
885 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000886 else
887 return E;
888 }
889}
890
Chris Lattnerecdd8412009-03-13 17:28:01 +0000891/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
892/// value (including ptr->int casts of the same size). Strip off any
893/// ParenExpr or CastExprs, returning their operand.
894Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
895 Expr *E = this;
896 while (true) {
897 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
898 E = P->getSubExpr();
899 continue;
900 }
901
902 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
903 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
904 // ptr<->int casts of the same width. We also ignore all identify casts.
905 Expr *SE = P->getSubExpr();
906
907 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
908 E = SE;
909 continue;
910 }
911
912 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
913 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
914 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
915 E = SE;
916 continue;
917 }
918 }
919
920 return E;
921 }
922}
923
924
Douglas Gregor898574e2008-12-05 23:32:09 +0000925/// hasAnyTypeDependentArguments - Determines if any of the expressions
926/// in Exprs is type-dependent.
927bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
928 for (unsigned I = 0; I < NumExprs; ++I)
929 if (Exprs[I]->isTypeDependent())
930 return true;
931
932 return false;
933}
934
935/// hasAnyValueDependentArguments - Determines if any of the expressions
936/// in Exprs is value-dependent.
937bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
938 for (unsigned I = 0; I < NumExprs; ++I)
939 if (Exprs[I]->isValueDependent())
940 return true;
941
942 return false;
943}
944
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000945bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000946 // This function is attempting whether an expression is an initializer
947 // which can be evaluated at compile-time. isEvaluatable handles most
948 // of the cases, but it can't deal with some initializer-specific
949 // expressions, and it can't deal with aggregates; we deal with those here,
950 // and fall back to isEvaluatable for the other cases.
951
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000952 // FIXME: This function assumes the variable being assigned to
953 // isn't a reference type!
954
Anders Carlssone8a32b82008-11-24 05:23:59 +0000955 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000956 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000957 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000958 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000959 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000960 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000961 // This handles gcc's extension that allows global initializers like
962 // "struct x {int x;} x = (struct x) {};".
963 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +0000964 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000965 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000966 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000967 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000968 // FIXME: This doesn't deal with fields with reference types correctly.
969 // FIXME: This incorrectly allows pointers cast to integers to be assigned
970 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +0000971 const InitListExpr *Exp = cast<InitListExpr>(this);
972 unsigned numInits = Exp->getNumInits();
973 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000974 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000975 return false;
976 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000977 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000978 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000979 case ImplicitValueInitExprClass:
980 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000981 case ParenExprClass: {
982 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
983 }
984 case UnaryOperatorClass: {
985 const UnaryOperator* Exp = cast<UnaryOperator>(this);
986 if (Exp->getOpcode() == UnaryOperator::Extension)
987 return Exp->getSubExpr()->isConstantInitializer(Ctx);
988 break;
989 }
990 case CStyleCastExprClass:
991 // Handle casts with a destination that's a struct or union; this
992 // deals with both the gcc no-op struct cast extension and the
993 // cast-to-union extension.
994 if (getType()->isRecordType())
995 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
996 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000997 }
998
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000999 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001000}
1001
Reid Spencer5f016e22007-07-11 17:01:13 +00001002/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001003/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001004
1005/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1006/// comma, etc
1007///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001008/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1009/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1010/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001011
Eli Friedmane28d7192009-02-26 09:29:13 +00001012// CheckICE - This function does the fundamental ICE checking: the returned
1013// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1014// Note that to reduce code duplication, this helper does no evaluation
1015// itself; the caller checks whether the expression is evaluatable, and
1016// in the rare cases where CheckICE actually cares about the evaluated
1017// value, it calls into Evalute.
1018//
1019// Meanings of Val:
1020// 0: This expression is an ICE if it can be evaluated by Evaluate.
1021// 1: This expression is not an ICE, but if it isn't evaluated, it's
1022// a legal subexpression for an ICE. This return value is used to handle
1023// the comma operator in C99 mode.
1024// 2: This expression is not an ICE, and is not a legal subexpression for one.
1025
1026struct ICEDiag {
1027 unsigned Val;
1028 SourceLocation Loc;
1029
1030 public:
1031 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1032 ICEDiag() : Val(0) {}
1033};
1034
1035ICEDiag NoDiag() { return ICEDiag(); }
1036
Eli Friedman60ce9632009-02-27 04:07:58 +00001037static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1038 Expr::EvalResult EVResult;
1039 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1040 !EVResult.Val.isInt()) {
1041 return ICEDiag(2, E->getLocStart());
1042 }
1043 return NoDiag();
1044}
1045
Eli Friedmane28d7192009-02-26 09:29:13 +00001046static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
1047 if (!E->getType()->isIntegralType()) {
1048 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001049 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001050
1051 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001053 return ICEDiag(2, E->getLocStart());
1054 case Expr::ParenExprClass:
1055 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1056 case Expr::IntegerLiteralClass:
1057 case Expr::CharacterLiteralClass:
1058 case Expr::CXXBoolLiteralExprClass:
1059 case Expr::CXXZeroInitValueExprClass:
1060 case Expr::TypesCompatibleExprClass:
1061 case Expr::UnaryTypeTraitExprClass:
1062 return NoDiag();
1063 case Expr::CallExprClass:
1064 case Expr::CXXOperatorCallExprClass: {
1065 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001066 if (CE->isBuiltinCall(Ctx))
1067 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001068 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001069 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001070 case Expr::DeclRefExprClass:
1071 case Expr::QualifiedDeclRefExprClass:
1072 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1073 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001074 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001075 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001076 // C++ 7.1.5.1p2
1077 // A variable of non-volatile const-qualified integral or enumeration
1078 // type initialized by an ICE can be used in ICEs.
1079 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001080 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001081 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +00001082 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001083 }
1084 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001085 return ICEDiag(2, E->getLocStart());
1086 case Expr::UnaryOperatorClass: {
1087 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001090 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001092 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001094 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001096 case UnaryOperator::Real:
1097 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001098 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001099 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001100 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1101 // Evaluate matches the proposed gcc behavior for cases like
1102 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1103 // compliance: we should warn earlier for offsetof expressions with
1104 // array subscripts that aren't ICEs, and if the array subscripts
1105 // are ICEs, the value of the offsetof must be an integer constant.
1106 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001109 case Expr::SizeOfAlignOfExprClass: {
1110 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1111 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1112 return ICEDiag(2, E->getLocStart());
1113 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001115 case Expr::BinaryOperatorClass: {
1116 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 switch (Exp->getOpcode()) {
1118 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001119 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001123 case BinaryOperator::Add:
1124 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001127 case BinaryOperator::LT:
1128 case BinaryOperator::GT:
1129 case BinaryOperator::LE:
1130 case BinaryOperator::GE:
1131 case BinaryOperator::EQ:
1132 case BinaryOperator::NE:
1133 case BinaryOperator::And:
1134 case BinaryOperator::Xor:
1135 case BinaryOperator::Or:
1136 case BinaryOperator::Comma: {
1137 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1138 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001139 if (Exp->getOpcode() == BinaryOperator::Div ||
1140 Exp->getOpcode() == BinaryOperator::Rem) {
1141 // Evaluate gives an error for undefined Div/Rem, so make sure
1142 // we don't evaluate one.
1143 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1144 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1145 if (REval == 0)
1146 return ICEDiag(1, E->getLocStart());
1147 if (REval.isSigned() && REval.isAllOnesValue()) {
1148 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1149 if (LEval.isMinSignedValue())
1150 return ICEDiag(1, E->getLocStart());
1151 }
1152 }
1153 }
1154 if (Exp->getOpcode() == BinaryOperator::Comma) {
1155 if (Ctx.getLangOptions().C99) {
1156 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1157 // if it isn't evaluated.
1158 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1159 return ICEDiag(1, E->getLocStart());
1160 } else {
1161 // In both C89 and C++, commas in ICEs are illegal.
1162 return ICEDiag(2, E->getLocStart());
1163 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001164 }
1165 if (LHSResult.Val >= RHSResult.Val)
1166 return LHSResult;
1167 return RHSResult;
1168 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001170 case BinaryOperator::LOr: {
1171 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1172 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1173 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1174 // Rare case where the RHS has a comma "side-effect"; we need
1175 // to actually check the condition to see whether the side
1176 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001177 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001178 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001179 return RHSResult;
1180 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001181 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001182
Eli Friedmane28d7192009-02-26 09:29:13 +00001183 if (LHSResult.Val >= RHSResult.Val)
1184 return LHSResult;
1185 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001187 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001189 case Expr::ImplicitCastExprClass:
1190 case Expr::CStyleCastExprClass:
1191 case Expr::CXXFunctionalCastExprClass: {
1192 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1193 if (SubExpr->getType()->isIntegralType())
1194 return CheckICE(SubExpr, Ctx);
1195 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1196 return NoDiag();
1197 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001199 case Expr::ConditionalOperatorClass: {
1200 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001201 // If the condition (ignoring parens) is a __builtin_constant_p call,
1202 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001203 // expression, and it is fully evaluated. This is an important GNU
1204 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001205 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001206 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001207 Expr::EvalResult EVResult;
1208 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1209 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001210 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001211 }
1212 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001213 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001214 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1215 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1216 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1217 if (CondResult.Val == 2)
1218 return CondResult;
1219 if (TrueResult.Val == 2)
1220 return TrueResult;
1221 if (FalseResult.Val == 2)
1222 return FalseResult;
1223 if (CondResult.Val == 1)
1224 return CondResult;
1225 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1226 return NoDiag();
1227 // Rare case where the diagnostics depend on which side is evaluated
1228 // Note that if we get here, CondResult is 0, and at least one of
1229 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001230 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001231 return FalseResult;
1232 }
1233 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001235 case Expr::CXXDefaultArgExprClass:
1236 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001237 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001238 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001239 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001241}
Reid Spencer5f016e22007-07-11 17:01:13 +00001242
Eli Friedmane28d7192009-02-26 09:29:13 +00001243bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1244 SourceLocation *Loc, bool isEvaluated) const {
1245 ICEDiag d = CheckICE(this, Ctx);
1246 if (d.Val != 0) {
1247 if (Loc) *Loc = d.Loc;
1248 return false;
1249 }
1250 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001251 if (!Evaluate(EvalResult, Ctx))
1252 assert(0 && "ICE cannot be evaluated!");
1253 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1254 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001255 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 return true;
1257}
1258
Reid Spencer5f016e22007-07-11 17:01:13 +00001259/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1260/// integer constant expression with the value zero, or if this is one that is
1261/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001262bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1263{
Sebastian Redl07779722008-10-31 14:43:28 +00001264 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001265 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001266 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001267 // Check that it is a cast to void*.
1268 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1269 QualType Pointee = PT->getPointeeType();
1270 if (Pointee.getCVRQualifiers() == 0 &&
1271 Pointee->isVoidType() && // to void*
1272 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001273 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001274 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001276 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1277 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001278 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001279 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1280 // Accept ((void*)0) as a null pointer constant, as many other
1281 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001282 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001283 } else if (const CXXDefaultArgExpr *DefaultArg
1284 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001285 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001286 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001287 } else if (isa<GNUNullExpr>(this)) {
1288 // The GNU __null extension is always a null pointer constant.
1289 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001290 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001291
Steve Naroffaa58f002008-01-14 16:10:57 +00001292 // This expression must be an integer type.
1293 if (!getType()->isIntegerType())
1294 return false;
1295
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 // If we have an integer constant expression, we need to *evaluate* it and
1297 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001298 // FIXME: We should probably return false if we're compiling in strict mode
1299 // and Diag is not null (this indicates that the value was foldable but not
1300 // an ICE.
1301 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001302 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001303 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001304}
Steve Naroff31a45842007-07-28 23:10:27 +00001305
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001306/// isBitField - Return true if this expression is a bit-field.
1307bool Expr::isBitField() {
1308 Expr *E = this->IgnoreParenCasts();
1309 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001310 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1311 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001312 return false;
1313}
1314
Chris Lattner2140e902009-02-16 22:14:05 +00001315/// isArrow - Return true if the base expression is a pointer to vector,
1316/// return false if the base expression is a vector.
1317bool ExtVectorElementExpr::isArrow() const {
1318 return getBase()->getType()->isPointerType();
1319}
1320
Nate Begeman213541a2008-04-18 23:10:10 +00001321unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001322 if (const VectorType *VT = getType()->getAsVectorType())
1323 return VT->getNumElements();
1324 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001325}
1326
Nate Begeman8a997642008-05-09 06:41:27 +00001327/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001328bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001329 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001330 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001331
1332 // Halving swizzles do not contain duplicate elements.
1333 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1334 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1335 return false;
1336
1337 // Advance past s-char prefix on hex swizzles.
1338 if (*compStr == 's') {
1339 compStr++;
1340 length--;
1341 }
Steve Narofffec0b492007-07-30 03:29:09 +00001342
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001343 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001344 const char *s = compStr+i;
1345 for (const char c = *s++; *s; s++)
1346 if (c == *s)
1347 return true;
1348 }
1349 return false;
1350}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001351
Nate Begeman8a997642008-05-09 06:41:27 +00001352/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001353void ExtVectorElementExpr::getEncodedElementAccess(
1354 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001355 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001356 if (*compStr == 's')
1357 compStr++;
1358
1359 bool isHi = !strcmp(compStr, "hi");
1360 bool isLo = !strcmp(compStr, "lo");
1361 bool isEven = !strcmp(compStr, "even");
1362 bool isOdd = !strcmp(compStr, "odd");
1363
Nate Begeman8a997642008-05-09 06:41:27 +00001364 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1365 uint64_t Index;
1366
1367 if (isHi)
1368 Index = e + i;
1369 else if (isLo)
1370 Index = i;
1371 else if (isEven)
1372 Index = 2 * i;
1373 else if (isOdd)
1374 Index = 2 * i + 1;
1375 else
1376 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001377
Nate Begeman3b8d1162008-05-13 21:03:02 +00001378 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001379 }
Nate Begeman8a997642008-05-09 06:41:27 +00001380}
1381
Steve Naroff68d331a2007-09-27 14:38:14 +00001382// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001383ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001385 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001386 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001387 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001388 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001389 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001390 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001391 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001392 if (NumArgs) {
1393 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001394 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1395 }
Steve Naroff563477d2007-09-18 23:55:05 +00001396 LBracloc = LBrac;
1397 RBracloc = RBrac;
1398}
1399
Steve Naroff68d331a2007-09-27 14:38:14 +00001400// constructor for class messages.
1401// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001402ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001403 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001404 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001405 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001406 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001407 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001408 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001409 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001410 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001411 if (NumArgs) {
1412 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001413 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1414 }
Steve Naroff563477d2007-09-18 23:55:05 +00001415 LBracloc = LBrac;
1416 RBracloc = RBrac;
1417}
1418
Ted Kremenek4df728e2008-06-24 15:50:53 +00001419// constructor for class messages.
1420ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1421 QualType retType, ObjCMethodDecl *mproto,
1422 SourceLocation LBrac, SourceLocation RBrac,
1423 Expr **ArgExprs, unsigned nargs)
1424: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1425MethodProto(mproto) {
1426 NumArgs = nargs;
1427 SubExprs = new Stmt*[NumArgs+1];
1428 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1429 if (NumArgs) {
1430 for (unsigned i = 0; i != NumArgs; ++i)
1431 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1432 }
1433 LBracloc = LBrac;
1434 RBracloc = RBrac;
1435}
1436
1437ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1438 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1439 switch (x & Flags) {
1440 default:
1441 assert(false && "Invalid ObjCMessageExpr.");
1442 case IsInstMeth:
1443 return ClassInfo(0, 0);
1444 case IsClsMethDeclUnknown:
1445 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1446 case IsClsMethDeclKnown: {
1447 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1448 return ClassInfo(D, D->getIdentifier());
1449 }
1450 }
1451}
1452
Chris Lattner27437ca2007-10-25 00:29:32 +00001453bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001454 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001455}
1456
Sebastian Redl05189992008-11-11 17:56:53 +00001457void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1458 // Override default behavior of traversing children. If this has a type
1459 // operand and the type is a variable-length array, the child iteration
1460 // will iterate over the size expression. However, this expression belongs
1461 // to the type, not to this, so we don't want to delete it.
1462 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001463 if (isArgumentType()) {
1464 this->~SizeOfAlignOfExpr();
1465 C.Deallocate(this);
1466 }
Sebastian Redl05189992008-11-11 17:56:53 +00001467 else
1468 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001469}
1470
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001471//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001472// DesignatedInitExpr
1473//===----------------------------------------------------------------------===//
1474
1475IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1476 assert(Kind == FieldDesignator && "Only valid on a field designator");
1477 if (Field.NameOrField & 0x01)
1478 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1479 else
1480 return getField()->getIdentifier();
1481}
1482
1483DesignatedInitExpr *
1484DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1485 unsigned NumDesignators,
1486 Expr **IndexExprs, unsigned NumIndexExprs,
1487 SourceLocation ColonOrEqualLoc,
1488 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001489 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1490 sizeof(Designator) * NumDesignators +
1491 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001492 DesignatedInitExpr *DIE
1493 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1494 ColonOrEqualLoc, UsesColonSyntax,
1495 NumIndexExprs + 1);
1496
1497 // Fill in the designators
1498 unsigned ExpectedNumSubExprs = 0;
1499 designators_iterator Desig = DIE->designators_begin();
1500 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1501 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1502 if (Designators[Idx].isArrayDesignator())
1503 ++ExpectedNumSubExprs;
1504 else if (Designators[Idx].isArrayRangeDesignator())
1505 ExpectedNumSubExprs += 2;
1506 }
1507 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1508
1509 // Fill in the subexpressions, including the initializer expression.
1510 child_iterator Child = DIE->child_begin();
1511 *Child++ = Init;
1512 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1513 *Child = IndexExprs[Idx];
1514
1515 return DIE;
1516}
1517
1518SourceRange DesignatedInitExpr::getSourceRange() const {
1519 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001520 Designator &First =
1521 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001522 if (First.isFieldDesignator()) {
1523 if (UsesColonSyntax)
1524 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1525 else
1526 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1527 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001528 StartLoc =
1529 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001530 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1531}
1532
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001533DesignatedInitExpr::designators_iterator
1534DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001535 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1536 Ptr += sizeof(DesignatedInitExpr);
1537 return static_cast<Designator*>(static_cast<void*>(Ptr));
1538}
1539
1540DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1541 return designators_begin() + NumDesignators;
1542}
1543
1544Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1545 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1546 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1547 Ptr += sizeof(DesignatedInitExpr);
1548 Ptr += sizeof(Designator) * NumDesignators;
1549 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1550 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1551}
1552
1553Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1554 assert(D.Kind == Designator::ArrayRangeDesignator &&
1555 "Requires array range designator");
1556 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1557 Ptr += sizeof(DesignatedInitExpr);
1558 Ptr += sizeof(Designator) * NumDesignators;
1559 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1560 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1561}
1562
1563Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1564 assert(D.Kind == Designator::ArrayRangeDesignator &&
1565 "Requires array range designator");
1566 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1567 Ptr += sizeof(DesignatedInitExpr);
1568 Ptr += sizeof(Designator) * NumDesignators;
1569 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1570 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1571}
1572
1573//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001574// ExprIterator.
1575//===----------------------------------------------------------------------===//
1576
1577Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1578Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1579Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1580const Expr* ConstExprIterator::operator[](size_t idx) const {
1581 return cast<Expr>(I[idx]);
1582}
1583const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1584const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1585
1586//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001587// Child Iterators for iterating over subexpressions/substatements
1588//===----------------------------------------------------------------------===//
1589
1590// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001591Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1592Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001593
Steve Naroff7779db42007-11-12 14:29:37 +00001594// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001595Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1596Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001597
Steve Naroffe3e9add2008-06-02 23:03:37 +00001598// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001599Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1600Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001601
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001602// ObjCKVCRefExpr
1603Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1604Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1605
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001606// ObjCSuperExpr
1607Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1608Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1609
Chris Lattnerd9f69102008-08-10 01:53:14 +00001610// PredefinedExpr
1611Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1612Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001613
1614// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001615Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1616Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001617
1618// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001619Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001620Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001621
1622// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001623Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1624Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001625
Chris Lattner5d661452007-08-26 03:42:43 +00001626// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001627Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1628Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001629
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001630// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001631Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1632Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001633
1634// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001635Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1636Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001637
1638// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001639Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1640Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001641
Sebastian Redl05189992008-11-11 17:56:53 +00001642// SizeOfAlignOfExpr
1643Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1644 // If this is of a type and the type is a VLA type (and not a typedef), the
1645 // size expression of the VLA needs to be treated as an executable expression.
1646 // Why isn't this weirdness documented better in StmtIterator?
1647 if (isArgumentType()) {
1648 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1649 getArgumentType().getTypePtr()))
1650 return child_iterator(T);
1651 return child_iterator();
1652 }
Sebastian Redld4575892008-12-03 23:17:54 +00001653 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001654}
Sebastian Redl05189992008-11-11 17:56:53 +00001655Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1656 if (isArgumentType())
1657 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001658 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001659}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001660
1661// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001662Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001663 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001664}
Ted Kremenek1237c672007-08-24 20:06:47 +00001665Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001666 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001667}
1668
1669// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001670Stmt::child_iterator CallExpr::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 CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001674 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001675}
Ted Kremenek1237c672007-08-24 20:06:47 +00001676
1677// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001678Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1679Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001680
Nate Begeman213541a2008-04-18 23:10:10 +00001681// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001682Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1683Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001684
1685// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001686Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1687Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001688
Ted Kremenek1237c672007-08-24 20:06:47 +00001689// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001690Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1691Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001692
1693// BinaryOperator
1694Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001695 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001696}
Ted Kremenek1237c672007-08-24 20:06:47 +00001697Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001698 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001699}
1700
1701// ConditionalOperator
1702Stmt::child_iterator ConditionalOperator::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 ConditionalOperator::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// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001710Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1711Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001712
Ted Kremenek1237c672007-08-24 20:06:47 +00001713// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001714Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1715Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001716
1717// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001718Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1719 return child_iterator();
1720}
1721
1722Stmt::child_iterator TypesCompatibleExpr::child_end() {
1723 return child_iterator();
1724}
Ted Kremenek1237c672007-08-24 20:06:47 +00001725
1726// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001727Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1728Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001729
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001730// GNUNullExpr
1731Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1732Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1733
Eli Friedmand38617c2008-05-14 19:38:39 +00001734// ShuffleVectorExpr
1735Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001736 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001737}
1738Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001739 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001740}
1741
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001742// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001743Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1744Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001745
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001746// InitListExpr
1747Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001748 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001749}
1750Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001751 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001752}
1753
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001754// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001755Stmt::child_iterator DesignatedInitExpr::child_begin() {
1756 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1757 Ptr += sizeof(DesignatedInitExpr);
1758 Ptr += sizeof(Designator) * NumDesignators;
1759 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1760}
1761Stmt::child_iterator DesignatedInitExpr::child_end() {
1762 return child_iterator(&*child_begin() + NumSubExprs);
1763}
1764
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001765// ImplicitValueInitExpr
1766Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1767 return child_iterator();
1768}
1769
1770Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1771 return child_iterator();
1772}
1773
Ted Kremenek1237c672007-08-24 20:06:47 +00001774// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001775Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001776 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001777}
1778Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001779 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001780}
Ted Kremenek1237c672007-08-24 20:06:47 +00001781
1782// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001783Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1784Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001785
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001786// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001787Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1788 return child_iterator();
1789}
1790Stmt::child_iterator ObjCSelectorExpr::child_end() {
1791 return child_iterator();
1792}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001793
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001794// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001795Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1796 return child_iterator();
1797}
1798Stmt::child_iterator ObjCProtocolExpr::child_end() {
1799 return child_iterator();
1800}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001801
Steve Naroff563477d2007-09-18 23:55:05 +00001802// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001803Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001804 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001805}
1806Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001807 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001808}
1809
Steve Naroff4eb206b2008-09-03 18:15:37 +00001810// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001811Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1812Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001813
Ted Kremenek9da13f92008-09-26 23:24:14 +00001814Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1815Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }