blob: e34412e19092cc57aa863004ca01435ea0834f97 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Chris Lattnere0391b22008-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 Johannesen2461f612008-10-09 23:02:32 +000034 bool ignored;
35 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
36 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000037 return V.convertToDouble();
38}
39
Chris Lattneraa491192009-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
Chris Lattner4b009652007-07-25 00:24:17 +000051 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-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;
Chris Lattner4b009652007-07-25 00:24:17 +000059
Chris Lattnerc3144742009-02-18 05:49:11 +000060 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000061 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
62 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000063}
64
65
Ted Kremenek4f530a92009-02-06 19:55:15 +000066void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +000067 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek32d760b2009-02-09 17:10:09 +000068 this->~StringLiteral();
69 C.Deallocate(this);
Chris Lattner4b009652007-07-25 00:24:17 +000070}
71
Chris Lattner4b009652007-07-25 00:24:17 +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";
Chris Lattner4b009652007-07-25 00:24:17 +000089 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000090 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000091 }
92}
93
Douglas Gregorc78182d2009-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
Chris Lattner4b009652007-07-25 00:24:17 +0000124//===----------------------------------------------------------------------===//
125// Postfix Operators.
126//===----------------------------------------------------------------------===//
127
Ted Kremenek362abcd2009-02-09 20:51:47 +0000128CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000129 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000130 : Expr(SC, t,
131 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000132 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000133 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000134
135 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-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 Kremenek362abcd2009-02-09 20:51:47 +0000139
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000140 RParenLoc = rparenloc;
141}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000142
Ted Kremenek362abcd2009-02-09 20:51:47 +0000143CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
144 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000145 : Expr(CallExprClass, t,
146 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000147 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000148 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000149
150 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000151 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000152 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000153 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000154
Chris Lattner4b009652007-07-25 00:24:17 +0000155 RParenLoc = rparenloc;
156}
157
Ted Kremenek362abcd2009-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 Lattnerc257c0d2007-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 Kremenek0c97e042009-02-07 01:47:29 +0000168void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-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 Kremenek0c97e042009-02-07 01:47:29 +0000175 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-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 Kremenek2719e982008-06-17 02:43:46 +0000181 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-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 Kremenek0c97e042009-02-07 01:47:29 +0000189 delete [] SubExprs;
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000190 SubExprs = NewSubExprs;
191 this->NumArgs = NumArgs;
192}
193
Chris Lattnerc24915f2008-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 Gregorb5af7382009-02-14 18:57:46 +0000196unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-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 Lattnerc24915f2008-10-06 05:00:53 +0000202 return 0;
203
Steve Naroff44aec4c2008-01-31 01:07:12 +0000204 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
205 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000206 return 0;
207
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000208 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
209 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000210 return 0;
211
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000212 if (!FDecl->getIdentifier())
213 return 0;
214
Douglas Gregorb5af7382009-02-14 18:57:46 +0000215 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000216}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000217
Chris Lattnerc24915f2008-10-06 05:00:53 +0000218
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor535f3122009-03-12 22:51:37 +0000223 case PtrMemD: return ".*";
224 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor535f3122009-03-12 22:51:37 +0000256
257 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000258}
259
Douglas Gregor00fe3f62009-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 Carlsson762b7c72007-08-31 04:56:16 +0000322InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000323 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000324 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000325 : Expr(InitListExprClass, QualType()),
Douglas Gregor82462762009-01-29 16:53:55 +0000326 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000327 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000328
329 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000330}
Chris Lattner4b009652007-07-25 00:24:17 +0000331
Douglas Gregorf603b472009-01-28 21:54:33 +0000332void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000333 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000334 Idx < LastIdx; ++Idx)
Douglas Gregorf603b472009-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 Naroff6f373332008-09-04 15:31:07 +0000351/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000352///
353const FunctionType *BlockExpr::getFunctionType() const {
354 return getType()->getAsBlockPointerType()->
355 getPointeeType()->getAsFunctionType();
356}
357
Steve Naroff9ac456d2008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000365//===----------------------------------------------------------------------===//
366// Generic Expression Routines
367//===----------------------------------------------------------------------===//
368
Chris Lattnerd2c66552009-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 {
Chris Lattner4b009652007-07-25 00:24:17 +0000375 switch (getStmtClass()) {
376 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000377 Loc = getExprLoc();
378 R1 = getSourceRange();
379 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000380 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000381 return cast<ParenExpr>(this)->getSubExpr()->
382 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000383 case UnaryOperatorClass: {
384 const UnaryOperator *UO = cast<UnaryOperator>(this);
385
386 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000387 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000388 case UnaryOperator::PostInc:
389 case UnaryOperator::PostDec:
390 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000391 case UnaryOperator::PreDec: // ++/--
392 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000393 case UnaryOperator::Deref:
394 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000395 if (getType().isVolatileQualified())
396 return false;
397 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000398 case UnaryOperator::Real:
399 case UnaryOperator::Imag:
400 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000401 if (UO->getSubExpr()->getType().isVolatileQualified())
402 return false;
403 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000404 case UnaryOperator::Extension:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000405 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000406 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000407 Loc = UO->getOperatorLoc();
408 R1 = UO->getSubExpr()->getSourceRange();
409 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000410 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000411 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-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 Lattneref95ffd2007-12-01 06:07:34 +0000417
Chris Lattnerd2c66552009-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 Lattneref95ffd2007-12-01 06:07:34 +0000424 }
Chris Lattner06078d22007-08-25 02:00:02 +0000425 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000426 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000427
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000428 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-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 Jahanian363c59b2007-12-01 19:58:28 +0000431 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump399ad182009-02-27 03:16:57 +0000432 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000433 return true;
434 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000435 }
436
Chris Lattner4b009652007-07-25 00:24:17 +0000437 case MemberExprClass:
Chris Lattnerd2c66552009-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
Chris Lattner4b009652007-07-25 00:24:17 +0000447 case ArraySubscriptExprClass:
448 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-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 Friedman21fd0292008-05-27 15:24:04 +0000456
Chris Lattner4b009652007-07-25 00:24:17 +0000457 case CallExprClass:
Chris Lattnerd2c66552009-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 Lattner99f5f0b2007-09-26 22:06:30 +0000479 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000480 return false;
Chris Lattner200964f2008-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 Lattnerd2c66552009-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 Lattner200964f2008-07-26 19:51:01 +0000495 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000496 case CStyleCastExprClass:
Chris Lattnerd2c66552009-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;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000505 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerd2c66552009-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 Friedmanb924c7f2008-05-19 21:24:43 +0000515 case ImplicitCastExprClass:
516 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000517 return cast<ImplicitCastExpr>(this)
518 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000519
Chris Lattner3e254fb2008-04-08 04:40:51 +0000520 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000521 return cast<CXXDefaultArgExpr>(this)
522 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-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 Lattnerd2c66552009-02-14 07:37:35 +0000528 return false;
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000529 }
Chris Lattner4b009652007-07-25 00:24:17 +0000530}
531
Douglas Gregor4459bbe2008-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 Gregordd861062008-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 Gregor8acb7272008-12-11 16:49:14 +0000541 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner5bf72022007-10-30 22:53:42 +0000556/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000557/// - reference type [C++ [expr]]
558///
Chris Lattner25168a52008-07-26 21:30:36 +0000559Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-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())
Chris Lattner4b009652007-07-25 00:24:17 +0000563 return LV_NotObjectType;
564
Steve Naroffec7736d2008-02-10 01:39:04 +0000565 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000566 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000567 return LV_IncompleteVoidType;
568
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000569 /// FIXME: Expressions can't have reference type, so the following
570 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000571 if (TR->isReferenceType()) // C++ [expr]
572 return LV_Valid;
573
574 // the type looks fine, now check the expression
575 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000576 case StringLiteralClass: // C99 6.5.1p4
577 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000578 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner25168a52008-07-26 21:30:36 +0000582 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000583 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000584 case DeclRefExprClass:
585 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000586 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
587 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000588 return LV_Valid;
589 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000590 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000591 case BlockDeclRefExprClass: {
592 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000593 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000594 return LV_Valid;
595 break;
596 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000597 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000598 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-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 Gregor00660582009-03-11 20:22:50 +0000609 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-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 Lattner25168a52008-07-26 21:30:36 +0000634 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000635 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000636 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000637 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000638 return LV_Valid; // C99 6.5.3p4
639
640 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000641 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
642 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000643 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000649 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000650 case ImplicitCastExprClass:
651 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
652 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000653 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000654 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000655 case BinaryOperatorClass:
656 case CompoundAssignOperatorClass: {
657 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-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 Redl95216a62009-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 Gregor3d4492e2008-11-13 20:12:29 +0000669 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000670 return LV_InvalidExpression;
671
Douglas Gregor3d4492e2008-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 Gregor70d26122008-11-12 17:17:38 +0000681 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000682 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000683 case CXXOperatorCallExprClass:
684 case CXXMemberCallExprClass: {
Douglas Gregor0d5d89d2008-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 Gregor81c29152008-10-29 00:13:59 +0000688 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000689 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor3257fb52008-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 Gregor0d5d89d2008-10-28 00:22:11 +0000694
695 break;
696 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000697 case CompoundLiteralExprClass: // C99 6.5.2.5p5
698 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000699 case ChooseExprClass:
700 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000701 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000702 case ExtVectorElementExprClass:
703 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000704 return LV_DuplicateVectorComponents;
705 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000706 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
707 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000708 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
709 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000710 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000711 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000712 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000713 return LV_Valid;
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000714 case VAArgExprClass:
Daniel Dunbar09a82e32009-02-12 09:21:08 +0000715 return LV_NotObjectType;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000716 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000717 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000718 case CXXConditionDeclExprClass:
719 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000720 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-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 Redlb93b49c2008-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;
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner25168a52008-07-26 21:30:36 +0000747Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
748 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000749
750 switch (lvalResult) {
Douglas Gregor26a4c5f2008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000758 case LV_NotObjectType: return MLV_NotObjectType;
759 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000760 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-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 Gregor82d44772008-12-20 23:49:58 +0000770 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000771 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000772
773 QualType CT = Ctx.getCanonicalType(getType());
774
775 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000776 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000777 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000778 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000779 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000780 return MLV_IncompleteType;
781
Chris Lattnera1923f62008-08-04 07:31:14 +0000782 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000783 if (r->hasConstFields())
784 return MLV_ConstQualified;
785 }
Steve Naroff076d6cb2008-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 Jahanianf96ee9e2009-01-12 19:55:42 +0000795
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000796 // Assigning to an 'implicit' property?
Fariborz Jahanian48b1a132008-11-25 17:56:43 +0000797 else if (getStmtClass() == ObjCKVCRefExprClass) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000798 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
799 if (KVCExpr->getSetterMethod() == 0)
800 return MLV_NoSetterProperty;
801 }
Chris Lattner4b009652007-07-25 00:24:17 +0000802 return MLV_Valid;
803}
804
Ted Kremenek5778d622008-02-27 18:39:48 +0000805/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000806/// duration. This means that the address of this expression is a link-time
807/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000808bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000809 switch (getStmtClass()) {
810 default:
811 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000812 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000813 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000814 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000815 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000816 case CompoundLiteralExprClass:
817 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000818 case DeclRefExprClass:
819 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000820 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
821 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000822 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000823 if (isa<FunctionDecl>(D))
824 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000825 return false;
826 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000827 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000828 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000829 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000830 }
Chris Lattner743ec372007-11-27 21:35:27 +0000831 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000832 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000833 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000834 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000835 case CXXDefaultArgExprClass:
836 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000837 }
838}
839
Fariborz Jahanian0c195b92009-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 Jahanian2224fb22009-02-23 18:59:50 +0000848 case Expr::UnaryOperatorClass:
849 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian0c195b92009-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 Kremenek87e30c52008-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 Lattner7a48d9c2008-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 Lattner7a48d9c2008-02-13 01:02:39 +0000886 else
887 return E;
888 }
889}
890
Chris Lattnerab0b8b12009-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 Gregor1b21c7f2008-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 Friedmandee41122009-01-25 02:32:41 +0000945bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-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 Friedman3dc50ed2009-02-20 02:36:22 +0000952 // FIXME: This function assumes the variable being assigned to
953 // isn't a reference type!
954
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000955 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +0000956 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000957 case StringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +0000958 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000959 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +0000960 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-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 Begemand6d2f772009-01-18 03:20:47 +0000964 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +0000965 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +0000966 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000967 case InitListExprClass: {
Eli Friedman3dc50ed2009-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 Carlssona7fa2aa2008-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 Friedmandee41122009-01-25 02:32:41 +0000974 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000975 return false;
976 }
Eli Friedman2b0dec52009-01-25 03:12:18 +0000977 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +0000978 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +0000979 case ImplicitValueInitExprClass:
980 return true;
Eli Friedman2b0dec52009-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 Carlssona7fa2aa2008-11-24 05:23:59 +0000997 }
998
Eli Friedman2b0dec52009-01-25 03:12:18 +0000999 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001000}
1001
Chris Lattner4b009652007-07-25 00:24:17 +00001002/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001003/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001004
1005/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1006/// comma, etc
1007///
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbar168b20c2009-02-18 00:47:45 +00001011
Eli Friedman7beeda62009-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 Friedmand65bbbc2009-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 Friedman7beeda62009-02-26 09:29:13 +00001046static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001047 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001048 if (!E->getType()->isIntegralType()) {
1049 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001050 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001051
1052 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001053 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001054 return ICEDiag(2, E->getLocStart());
1055 case Expr::ParenExprClass:
1056 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1057 case Expr::IntegerLiteralClass:
1058 case Expr::CharacterLiteralClass:
1059 case Expr::CXXBoolLiteralExprClass:
1060 case Expr::CXXZeroInitValueExprClass:
1061 case Expr::TypesCompatibleExprClass:
1062 case Expr::UnaryTypeTraitExprClass:
1063 return NoDiag();
1064 case Expr::CallExprClass:
1065 case Expr::CXXOperatorCallExprClass: {
1066 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001067 if (CE->isBuiltinCall(Ctx))
1068 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001069 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001070 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001071 case Expr::DeclRefExprClass:
1072 case Expr::QualifiedDeclRefExprClass:
1073 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1074 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001075 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001076 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001077 // C++ 7.1.5.1p2
1078 // A variable of non-volatile const-qualified integral or enumeration
1079 // type initialized by an ICE can be used in ICEs.
1080 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001081 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001082 if (const Expr *Init = Dcl->getInit())
Eli Friedman7beeda62009-02-26 09:29:13 +00001083 return CheckICE(Init, Ctx);
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001084 }
1085 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001086 return ICEDiag(2, E->getLocStart());
1087 case Expr::UnaryOperatorClass: {
1088 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001089 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001090 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001091 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001092 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001093 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001094 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001095 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001096 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001097 case UnaryOperator::Real:
1098 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001099 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001100 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001101 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1102 // Evaluate matches the proposed gcc behavior for cases like
1103 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1104 // compliance: we should warn earlier for offsetof expressions with
1105 // array subscripts that aren't ICEs, and if the array subscripts
1106 // are ICEs, the value of the offsetof must be an integer constant.
1107 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001108 }
Chris Lattner4b009652007-07-25 00:24:17 +00001109 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001110 case Expr::SizeOfAlignOfExprClass: {
1111 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1112 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1113 return ICEDiag(2, E->getLocStart());
1114 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001115 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001116 case Expr::BinaryOperatorClass: {
1117 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001118 switch (Exp->getOpcode()) {
1119 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001120 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001121 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001122 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001123 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001124 case BinaryOperator::Add:
1125 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001126 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001127 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001128 case BinaryOperator::LT:
1129 case BinaryOperator::GT:
1130 case BinaryOperator::LE:
1131 case BinaryOperator::GE:
1132 case BinaryOperator::EQ:
1133 case BinaryOperator::NE:
1134 case BinaryOperator::And:
1135 case BinaryOperator::Xor:
1136 case BinaryOperator::Or:
1137 case BinaryOperator::Comma: {
1138 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1139 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001140 if (Exp->getOpcode() == BinaryOperator::Div ||
1141 Exp->getOpcode() == BinaryOperator::Rem) {
1142 // Evaluate gives an error for undefined Div/Rem, so make sure
1143 // we don't evaluate one.
1144 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1145 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1146 if (REval == 0)
1147 return ICEDiag(1, E->getLocStart());
1148 if (REval.isSigned() && REval.isAllOnesValue()) {
1149 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1150 if (LEval.isMinSignedValue())
1151 return ICEDiag(1, E->getLocStart());
1152 }
1153 }
1154 }
1155 if (Exp->getOpcode() == BinaryOperator::Comma) {
1156 if (Ctx.getLangOptions().C99) {
1157 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1158 // if it isn't evaluated.
1159 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1160 return ICEDiag(1, E->getLocStart());
1161 } else {
1162 // In both C89 and C++, commas in ICEs are illegal.
1163 return ICEDiag(2, E->getLocStart());
1164 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001165 }
1166 if (LHSResult.Val >= RHSResult.Val)
1167 return LHSResult;
1168 return RHSResult;
1169 }
Chris Lattner4b009652007-07-25 00:24:17 +00001170 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001171 case BinaryOperator::LOr: {
1172 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1173 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1174 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1175 // Rare case where the RHS has a comma "side-effect"; we need
1176 // to actually check the condition to see whether the side
1177 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001178 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001179 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001180 return RHSResult;
1181 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001182 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001183
Eli Friedman7beeda62009-02-26 09:29:13 +00001184 if (LHSResult.Val >= RHSResult.Val)
1185 return LHSResult;
1186 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001187 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001188 }
Chris Lattner4b009652007-07-25 00:24:17 +00001189 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001190 case Expr::ImplicitCastExprClass:
1191 case Expr::CStyleCastExprClass:
1192 case Expr::CXXFunctionalCastExprClass: {
1193 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1194 if (SubExpr->getType()->isIntegralType())
1195 return CheckICE(SubExpr, Ctx);
1196 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1197 return NoDiag();
1198 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001199 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001200 case Expr::ConditionalOperatorClass: {
1201 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001202 // If the condition (ignoring parens) is a __builtin_constant_p call,
1203 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001204 // expression, and it is fully evaluated. This is an important GNU
1205 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001206 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001207 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001208 Expr::EvalResult EVResult;
1209 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1210 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001211 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001212 }
1213 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001214 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001215 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1216 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1217 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1218 if (CondResult.Val == 2)
1219 return CondResult;
1220 if (TrueResult.Val == 2)
1221 return TrueResult;
1222 if (FalseResult.Val == 2)
1223 return FalseResult;
1224 if (CondResult.Val == 1)
1225 return CondResult;
1226 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1227 return NoDiag();
1228 // Rare case where the diagnostics depend on which side is evaluated
1229 // Note that if we get here, CondResult is 0, and at least one of
1230 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001231 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001232 return FalseResult;
1233 }
1234 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001235 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001236 case Expr::CXXDefaultArgExprClass:
1237 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001238 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001239 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001240 }
Chris Lattner4b009652007-07-25 00:24:17 +00001241 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001242}
Chris Lattner4b009652007-07-25 00:24:17 +00001243
Eli Friedman7beeda62009-02-26 09:29:13 +00001244bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1245 SourceLocation *Loc, bool isEvaluated) const {
1246 ICEDiag d = CheckICE(this, Ctx);
1247 if (d.Val != 0) {
1248 if (Loc) *Loc = d.Loc;
1249 return false;
1250 }
1251 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001252 if (!Evaluate(EvalResult, Ctx))
1253 assert(0 && "ICE cannot be evaluated!");
1254 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1255 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001256 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001257 return true;
1258}
1259
Chris Lattner4b009652007-07-25 00:24:17 +00001260/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1261/// integer constant expression with the value zero, or if this is one that is
1262/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001263bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1264{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001265 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001266 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001267 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001268 // Check that it is a cast to void*.
1269 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1270 QualType Pointee = PT->getPointeeType();
1271 if (Pointee.getCVRQualifiers() == 0 &&
1272 Pointee->isVoidType() && // to void*
1273 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001274 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001275 }
Chris Lattner4b009652007-07-25 00:24:17 +00001276 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001277 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1278 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001279 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001280 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1281 // Accept ((void*)0) as a null pointer constant, as many other
1282 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001283 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001284 } else if (const CXXDefaultArgExpr *DefaultArg
1285 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001286 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001287 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001288 } else if (isa<GNUNullExpr>(this)) {
1289 // The GNU __null extension is always a null pointer constant.
1290 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001291 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001292
Steve Naroffa2e53222008-01-14 16:10:57 +00001293 // This expression must be an integer type.
1294 if (!getType()->isIntegerType())
1295 return false;
1296
Chris Lattner4b009652007-07-25 00:24:17 +00001297 // If we have an integer constant expression, we need to *evaluate* it and
1298 // test for the value 0.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001299 // FIXME: We should probably return false if we're compiling in strict mode
1300 // and Diag is not null (this indicates that the value was foldable but not
1301 // an ICE.
1302 EvalResult Result;
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001303 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001304 Result.Val.isInt() && Result.Val.getInt() == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001305}
Steve Naroffc11705f2007-07-28 23:10:27 +00001306
Douglas Gregor81c29152008-10-29 00:13:59 +00001307/// isBitField - Return true if this expression is a bit-field.
1308bool Expr::isBitField() {
1309 Expr *E = this->IgnoreParenCasts();
1310 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001311 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1312 return Field->isBitField();
Douglas Gregor81c29152008-10-29 00:13:59 +00001313 return false;
1314}
1315
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001316/// isArrow - Return true if the base expression is a pointer to vector,
1317/// return false if the base expression is a vector.
1318bool ExtVectorElementExpr::isArrow() const {
1319 return getBase()->getType()->isPointerType();
1320}
1321
Nate Begemanaf6ed502008-04-18 23:10:10 +00001322unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001323 if (const VectorType *VT = getType()->getAsVectorType())
1324 return VT->getNumElements();
1325 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001326}
1327
Nate Begemanc8e51f82008-05-09 06:41:27 +00001328/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001329bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001330 const char *compStr = Accessor.getName();
Chris Lattner58d3fa52008-11-19 07:55:04 +00001331 unsigned length = Accessor.getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001332
1333 // Halving swizzles do not contain duplicate elements.
1334 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1335 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1336 return false;
1337
1338 // Advance past s-char prefix on hex swizzles.
1339 if (*compStr == 's') {
1340 compStr++;
1341 length--;
1342 }
Steve Naroffba67f692007-07-30 03:29:09 +00001343
Chris Lattner58d3fa52008-11-19 07:55:04 +00001344 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001345 const char *s = compStr+i;
1346 for (const char c = *s++; *s; s++)
1347 if (c == *s)
1348 return true;
1349 }
1350 return false;
1351}
Chris Lattner42158e72007-08-02 23:36:59 +00001352
Nate Begemanc8e51f82008-05-09 06:41:27 +00001353/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001354void ExtVectorElementExpr::getEncodedElementAccess(
1355 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner58d3fa52008-11-19 07:55:04 +00001356 const char *compStr = Accessor.getName();
Nate Begeman1486b502009-01-18 01:47:54 +00001357 if (*compStr == 's')
1358 compStr++;
1359
1360 bool isHi = !strcmp(compStr, "hi");
1361 bool isLo = !strcmp(compStr, "lo");
1362 bool isEven = !strcmp(compStr, "even");
1363 bool isOdd = !strcmp(compStr, "odd");
1364
Nate Begemanc8e51f82008-05-09 06:41:27 +00001365 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1366 uint64_t Index;
1367
1368 if (isHi)
1369 Index = e + i;
1370 else if (isLo)
1371 Index = i;
1372 else if (isEven)
1373 Index = 2 * i;
1374 else if (isOdd)
1375 Index = 2 * i + 1;
1376 else
1377 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001378
Nate Begemana1ae7442008-05-13 21:03:02 +00001379 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001380 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001381}
1382
Steve Naroff4ed9d662007-09-27 14:38:14 +00001383// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001384ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001385 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001386 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001387 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001388 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001389 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001390 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001391 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001392 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001393 if (NumArgs) {
1394 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001395 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1396 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001397 LBracloc = LBrac;
1398 RBracloc = RBrac;
1399}
1400
Steve Naroff4ed9d662007-09-27 14:38:14 +00001401// constructor for class messages.
1402// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001403ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001404 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001405 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001406 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001407 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001408 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001409 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001410 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001411 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001412 if (NumArgs) {
1413 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001414 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1415 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001416 LBracloc = LBrac;
1417 RBracloc = RBrac;
1418}
1419
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001420// constructor for class messages.
1421ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1422 QualType retType, ObjCMethodDecl *mproto,
1423 SourceLocation LBrac, SourceLocation RBrac,
1424 Expr **ArgExprs, unsigned nargs)
1425: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1426MethodProto(mproto) {
1427 NumArgs = nargs;
1428 SubExprs = new Stmt*[NumArgs+1];
1429 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1430 if (NumArgs) {
1431 for (unsigned i = 0; i != NumArgs; ++i)
1432 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1433 }
1434 LBracloc = LBrac;
1435 RBracloc = RBrac;
1436}
1437
1438ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1439 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1440 switch (x & Flags) {
1441 default:
1442 assert(false && "Invalid ObjCMessageExpr.");
1443 case IsInstMeth:
1444 return ClassInfo(0, 0);
1445 case IsClsMethDeclUnknown:
1446 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1447 case IsClsMethDeclKnown: {
1448 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1449 return ClassInfo(D, D->getIdentifier());
1450 }
1451 }
1452}
1453
Chris Lattnerf624cd22007-10-25 00:29:32 +00001454bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001455 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001456}
1457
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001458void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1459 // Override default behavior of traversing children. If this has a type
1460 // operand and the type is a variable-length array, the child iteration
1461 // will iterate over the size expression. However, this expression belongs
1462 // to the type, not to this, so we don't want to delete it.
1463 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001464 if (isArgumentType()) {
1465 this->~SizeOfAlignOfExpr();
1466 C.Deallocate(this);
1467 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001468 else
1469 Expr::Destroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001470}
1471
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001472//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001473// DesignatedInitExpr
1474//===----------------------------------------------------------------------===//
1475
1476IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1477 assert(Kind == FieldDesignator && "Only valid on a field designator");
1478 if (Field.NameOrField & 0x01)
1479 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1480 else
1481 return getField()->getIdentifier();
1482}
1483
1484DesignatedInitExpr *
1485DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1486 unsigned NumDesignators,
1487 Expr **IndexExprs, unsigned NumIndexExprs,
1488 SourceLocation ColonOrEqualLoc,
1489 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001490 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1491 sizeof(Designator) * NumDesignators +
1492 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001493 DesignatedInitExpr *DIE
1494 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1495 ColonOrEqualLoc, UsesColonSyntax,
1496 NumIndexExprs + 1);
1497
1498 // Fill in the designators
1499 unsigned ExpectedNumSubExprs = 0;
1500 designators_iterator Desig = DIE->designators_begin();
1501 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1502 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1503 if (Designators[Idx].isArrayDesignator())
1504 ++ExpectedNumSubExprs;
1505 else if (Designators[Idx].isArrayRangeDesignator())
1506 ExpectedNumSubExprs += 2;
1507 }
1508 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1509
1510 // Fill in the subexpressions, including the initializer expression.
1511 child_iterator Child = DIE->child_begin();
1512 *Child++ = Init;
1513 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1514 *Child = IndexExprs[Idx];
1515
1516 return DIE;
1517}
1518
1519SourceRange DesignatedInitExpr::getSourceRange() const {
1520 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001521 Designator &First =
1522 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001523 if (First.isFieldDesignator()) {
1524 if (UsesColonSyntax)
1525 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1526 else
1527 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1528 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001529 StartLoc =
1530 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001531 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1532}
1533
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001534DesignatedInitExpr::designators_iterator
1535DesignatedInitExpr::designators_begin() {
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001536 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1537 Ptr += sizeof(DesignatedInitExpr);
1538 return static_cast<Designator*>(static_cast<void*>(Ptr));
1539}
1540
1541DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1542 return designators_begin() + NumDesignators;
1543}
1544
1545Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1546 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1547 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1548 Ptr += sizeof(DesignatedInitExpr);
1549 Ptr += sizeof(Designator) * NumDesignators;
1550 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1551 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1552}
1553
1554Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1555 assert(D.Kind == Designator::ArrayRangeDesignator &&
1556 "Requires array range designator");
1557 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1558 Ptr += sizeof(DesignatedInitExpr);
1559 Ptr += sizeof(Designator) * NumDesignators;
1560 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1561 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1562}
1563
1564Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1565 assert(D.Kind == Designator::ArrayRangeDesignator &&
1566 "Requires array range designator");
1567 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1568 Ptr += sizeof(DesignatedInitExpr);
1569 Ptr += sizeof(Designator) * NumDesignators;
1570 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1571 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1572}
1573
1574//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001575// ExprIterator.
1576//===----------------------------------------------------------------------===//
1577
1578Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1579Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1580Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1581const Expr* ConstExprIterator::operator[](size_t idx) const {
1582 return cast<Expr>(I[idx]);
1583}
1584const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1585const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1586
1587//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001588// Child Iterators for iterating over subexpressions/substatements
1589//===----------------------------------------------------------------------===//
1590
1591// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001592Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1593Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001594
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001595// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001596Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1597Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001598
Steve Naroff6f786252008-06-02 23:03:37 +00001599// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001600Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1601Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001602
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001603// ObjCKVCRefExpr
1604Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1605Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1606
Douglas Gregord8606632008-11-04 14:56:14 +00001607// ObjCSuperExpr
1608Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1609Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1610
Chris Lattner69909292008-08-10 01:53:14 +00001611// PredefinedExpr
1612Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1613Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001614
1615// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001616Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1617Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001618
1619// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001620Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001621Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001622
1623// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001624Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1625Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001626
Chris Lattner1de66eb2007-08-26 03:42:43 +00001627// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001628Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1629Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001630
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001631// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001632Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1633Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001634
1635// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001636Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1637Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001638
1639// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001640Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1641Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001642
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001643// SizeOfAlignOfExpr
1644Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1645 // If this is of a type and the type is a VLA type (and not a typedef), the
1646 // size expression of the VLA needs to be treated as an executable expression.
1647 // Why isn't this weirdness documented better in StmtIterator?
1648 if (isArgumentType()) {
1649 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1650 getArgumentType().getTypePtr()))
1651 return child_iterator(T);
1652 return child_iterator();
1653 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001654 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001655}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001656Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1657 if (isArgumentType())
1658 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001659 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001660}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001661
1662// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001663Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001664 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001665}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001666Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001667 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001668}
1669
1670// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001671Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001672 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001673}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001674Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001675 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001676}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001677
1678// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001679Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1680Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001681
Nate Begemanaf6ed502008-04-18 23:10:10 +00001682// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001683Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1684Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001685
1686// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001687Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1688Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001689
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001690// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001691Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1692Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001693
1694// BinaryOperator
1695Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001696 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001697}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001698Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001699 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001700}
1701
1702// ConditionalOperator
1703Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001704 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001705}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001706Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001707 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001708}
1709
1710// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001711Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1712Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001713
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001714// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001715Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1716Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001717
1718// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001719Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1720 return child_iterator();
1721}
1722
1723Stmt::child_iterator TypesCompatibleExpr::child_end() {
1724 return child_iterator();
1725}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001726
1727// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001728Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1729Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001730
Douglas Gregorad4b3792008-11-29 04:51:27 +00001731// GNUNullExpr
1732Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1733Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1734
Eli Friedmand0e9d092008-05-14 19:38:39 +00001735// ShuffleVectorExpr
1736Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001737 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001738}
1739Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001740 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001741}
1742
Anders Carlsson36760332007-10-15 20:28:48 +00001743// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001744Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1745Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001746
Anders Carlsson762b7c72007-08-31 04:56:16 +00001747// InitListExpr
1748Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001749 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001750}
1751Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001752 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001753}
1754
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001755// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001756Stmt::child_iterator DesignatedInitExpr::child_begin() {
1757 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1758 Ptr += sizeof(DesignatedInitExpr);
1759 Ptr += sizeof(Designator) * NumDesignators;
1760 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1761}
1762Stmt::child_iterator DesignatedInitExpr::child_end() {
1763 return child_iterator(&*child_begin() + NumSubExprs);
1764}
1765
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001766// ImplicitValueInitExpr
1767Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1768 return child_iterator();
1769}
1770
1771Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1772 return child_iterator();
1773}
1774
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001775// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001776Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00001777 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00001778}
1779Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00001780 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00001781}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001782
1783// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001784Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1785Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001786
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001787// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001788Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1789 return child_iterator();
1790}
1791Stmt::child_iterator ObjCSelectorExpr::child_end() {
1792 return child_iterator();
1793}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001794
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001795// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001796Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1797 return child_iterator();
1798}
1799Stmt::child_iterator ObjCProtocolExpr::child_end() {
1800 return child_iterator();
1801}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001802
Steve Naroffc39ca262007-09-18 23:55:05 +00001803// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001804Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001805 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001806}
1807Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001808 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001809}
1810
Steve Naroff52a81c02008-09-03 18:15:37 +00001811// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001812Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1813Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001814
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001815Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1816Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }