blob: 08ab5440b16fa7212100464b86ee2575960e3bd8 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000016#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000022#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Primary Expressions.
27//===----------------------------------------------------------------------===//
28
Anders Carlssona135fb42009-03-15 18:34:13 +000029IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
30 return new (C) IntegerLiteral(Value, getType(), Loc);
31}
32
Chris Lattnerda8249e2008-06-07 22:13:43 +000033/// getValueAsApproximateDouble - This returns the value as an inaccurate
34/// double. Note that this may cause loss of precision, but is useful for
35/// debugging dumps, etc.
36double FloatingLiteral::getValueAsApproximateDouble() const {
37 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +000038 bool ignored;
39 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
40 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +000041 return V.convertToDouble();
42}
43
Chris Lattner2085fd62009-02-18 06:40:38 +000044StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
45 unsigned ByteLength, bool Wide,
46 QualType Ty,
Anders Carlssona135fb42009-03-15 18:34:13 +000047 const SourceLocation *Loc,
48 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +000049 // Allocate enough space for the StringLiteral plus an array of locations for
50 // any concatenated string tokens.
51 void *Mem = C.Allocate(sizeof(StringLiteral)+
52 sizeof(SourceLocation)*(NumStrs-1),
53 llvm::alignof<StringLiteral>());
54 StringLiteral *SL = new (Mem) StringLiteral(Ty);
55
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +000057 char *AStrData = new (C, 1) char[ByteLength];
58 memcpy(AStrData, StrData, ByteLength);
59 SL->StrData = AStrData;
60 SL->ByteLength = ByteLength;
61 SL->IsWide = Wide;
62 SL->TokLocs[0] = Loc[0];
63 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +000064
Chris Lattner726e1682009-02-18 05:49:11 +000065 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +000066 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
67 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +000068}
69
Anders Carlssona135fb42009-03-15 18:34:13 +000070StringLiteral* StringLiteral::Clone(ASTContext &C) const {
71 return Create(C, StrData, ByteLength, IsWide, getType(),
72 TokLocs, NumConcatenated);
73}
Chris Lattner726e1682009-02-18 05:49:11 +000074
Ted Kremenek6e94ef52009-02-06 19:55:15 +000075void StringLiteral::Destroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +000076 C.Deallocate(const_cast<char*>(StrData));
Ted Kremenek353ffce2009-02-09 17:10:09 +000077 this->~StringLiteral();
78 C.Deallocate(this);
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
80
Reid Spencer5f016e22007-07-11 17:01:13 +000081/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
82/// corresponds to, e.g. "sizeof" or "[pre]++".
83const char *UnaryOperator::getOpcodeStr(Opcode Op) {
84 switch (Op) {
85 default: assert(0 && "Unknown unary operator");
86 case PostInc: return "++";
87 case PostDec: return "--";
88 case PreInc: return "++";
89 case PreDec: return "--";
90 case AddrOf: return "&";
91 case Deref: return "*";
92 case Plus: return "+";
93 case Minus: return "-";
94 case Not: return "~";
95 case LNot: return "!";
96 case Real: return "__real";
97 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +000098 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +000099 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101}
102
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000103UnaryOperator::Opcode
104UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
105 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000106 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000107 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
108 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
109 case OO_Amp: return AddrOf;
110 case OO_Star: return Deref;
111 case OO_Plus: return Plus;
112 case OO_Minus: return Minus;
113 case OO_Tilde: return Not;
114 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000115 }
116}
117
118OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
119 switch (Opc) {
120 case PostInc: case PreInc: return OO_PlusPlus;
121 case PostDec: case PreDec: return OO_MinusMinus;
122 case AddrOf: return OO_Amp;
123 case Deref: return OO_Star;
124 case Plus: return OO_Plus;
125 case Minus: return OO_Minus;
126 case Not: return OO_Tilde;
127 case LNot: return OO_Exclaim;
128 default: return OO_None;
129 }
130}
131
132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133//===----------------------------------------------------------------------===//
134// Postfix Operators.
135//===----------------------------------------------------------------------===//
136
Ted Kremenek668bf912009-02-09 20:51:47 +0000137CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000138 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000139 : Expr(SC, t,
140 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000141 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000142 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000143
144 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000145 SubExprs[FN] = fn;
146 for (unsigned i = 0; i != numargs; ++i)
147 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000148
Douglas Gregorb4609802008-11-14 16:09:21 +0000149 RParenLoc = rparenloc;
150}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000151
Ted Kremenek668bf912009-02-09 20:51:47 +0000152CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
153 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000154 : Expr(CallExprClass, t,
155 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000156 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000157 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000158
159 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000160 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000162 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 RParenLoc = rparenloc;
165}
166
Ted Kremenek668bf912009-02-09 20:51:47 +0000167void CallExpr::Destroy(ASTContext& C) {
168 DestroyChildren(C);
169 if (SubExprs) C.Deallocate(SubExprs);
170 this->~CallExpr();
171 C.Deallocate(this);
172}
173
Chris Lattnerd18b3292007-12-28 05:25:02 +0000174/// setNumArgs - This changes the number of arguments present in this call.
175/// Any orphaned expressions are deleted by this, and any new operands are set
176/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000177void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000178 // No change, just return.
179 if (NumArgs == getNumArgs()) return;
180
181 // If shrinking # arguments, just delete the extras and forgot them.
182 if (NumArgs < getNumArgs()) {
183 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000184 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000185 this->NumArgs = NumArgs;
186 return;
187 }
188
189 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek55499762008-06-17 02:43:46 +0000190 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000191 // Copy over args.
192 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
193 NewSubExprs[i] = SubExprs[i];
194 // Null out new args.
195 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
196 NewSubExprs[i] = 0;
197
Ted Kremenek8189cde2009-02-07 01:47:29 +0000198 delete [] SubExprs;
Chris Lattnerd18b3292007-12-28 05:25:02 +0000199 SubExprs = NewSubExprs;
200 this->NumArgs = NumArgs;
201}
202
Chris Lattnercb888962008-10-06 05:00:53 +0000203/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
204/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000205unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000206 // All simple function calls (e.g. func()) are implicitly cast to pointer to
207 // function. As a result, we try and obtain the DeclRefExpr from the
208 // ImplicitCastExpr.
209 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
210 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000211 return 0;
212
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000213 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
214 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000215 return 0;
216
Anders Carlssonbcba2012008-01-31 02:13:57 +0000217 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
218 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000219 return 0;
220
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000221 if (!FDecl->getIdentifier())
222 return 0;
223
Douglas Gregor3c385e52009-02-14 18:57:46 +0000224 return FDecl->getBuiltinID(Context);
Chris Lattnercb888962008-10-06 05:00:53 +0000225}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000226
Chris Lattnercb888962008-10-06 05:00:53 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
229/// corresponds to, e.g. "<<=".
230const char *BinaryOperator::getOpcodeStr(Opcode Op) {
231 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000232 case PtrMemD: return ".*";
233 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 case Mul: return "*";
235 case Div: return "/";
236 case Rem: return "%";
237 case Add: return "+";
238 case Sub: return "-";
239 case Shl: return "<<";
240 case Shr: return ">>";
241 case LT: return "<";
242 case GT: return ">";
243 case LE: return "<=";
244 case GE: return ">=";
245 case EQ: return "==";
246 case NE: return "!=";
247 case And: return "&";
248 case Xor: return "^";
249 case Or: return "|";
250 case LAnd: return "&&";
251 case LOr: return "||";
252 case Assign: return "=";
253 case MulAssign: return "*=";
254 case DivAssign: return "/=";
255 case RemAssign: return "%=";
256 case AddAssign: return "+=";
257 case SubAssign: return "-=";
258 case ShlAssign: return "<<=";
259 case ShrAssign: return ">>=";
260 case AndAssign: return "&=";
261 case XorAssign: return "^=";
262 case OrAssign: return "|=";
263 case Comma: return ",";
264 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000265
266 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
Douglas Gregor063daf62009-03-13 18:40:31 +0000269BinaryOperator::Opcode
270BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
271 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000272 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000273 case OO_Plus: return Add;
274 case OO_Minus: return Sub;
275 case OO_Star: return Mul;
276 case OO_Slash: return Div;
277 case OO_Percent: return Rem;
278 case OO_Caret: return Xor;
279 case OO_Amp: return And;
280 case OO_Pipe: return Or;
281 case OO_Equal: return Assign;
282 case OO_Less: return LT;
283 case OO_Greater: return GT;
284 case OO_PlusEqual: return AddAssign;
285 case OO_MinusEqual: return SubAssign;
286 case OO_StarEqual: return MulAssign;
287 case OO_SlashEqual: return DivAssign;
288 case OO_PercentEqual: return RemAssign;
289 case OO_CaretEqual: return XorAssign;
290 case OO_AmpEqual: return AndAssign;
291 case OO_PipeEqual: return OrAssign;
292 case OO_LessLess: return Shl;
293 case OO_GreaterGreater: return Shr;
294 case OO_LessLessEqual: return ShlAssign;
295 case OO_GreaterGreaterEqual: return ShrAssign;
296 case OO_EqualEqual: return EQ;
297 case OO_ExclaimEqual: return NE;
298 case OO_LessEqual: return LE;
299 case OO_GreaterEqual: return GE;
300 case OO_AmpAmp: return LAnd;
301 case OO_PipePipe: return LOr;
302 case OO_Comma: return Comma;
303 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000304 }
305}
306
307OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
308 static const OverloadedOperatorKind OverOps[] = {
309 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
310 OO_Star, OO_Slash, OO_Percent,
311 OO_Plus, OO_Minus,
312 OO_LessLess, OO_GreaterGreater,
313 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
314 OO_EqualEqual, OO_ExclaimEqual,
315 OO_Amp,
316 OO_Caret,
317 OO_Pipe,
318 OO_AmpAmp,
319 OO_PipePipe,
320 OO_Equal, OO_StarEqual,
321 OO_SlashEqual, OO_PercentEqual,
322 OO_PlusEqual, OO_MinusEqual,
323 OO_LessLessEqual, OO_GreaterGreaterEqual,
324 OO_AmpEqual, OO_CaretEqual,
325 OO_PipeEqual,
326 OO_Comma
327 };
328 return OverOps[Opc];
329}
330
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000331InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000332 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000333 SourceLocation rbraceloc)
Steve Naroffc5ae8992008-05-01 02:04:18 +0000334 : Expr(InitListExprClass, QualType()),
Douglas Gregor0bb76892009-01-29 16:53:55 +0000335 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregora9c87802009-01-29 19:42:23 +0000336 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner418f6c72008-10-26 23:43:26 +0000337
338 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000339}
Reid Spencer5f016e22007-07-11 17:01:13 +0000340
Douglas Gregorfa219202009-03-20 23:58:33 +0000341void InitListExpr::reserveInits(unsigned NumInits) {
342 if (NumInits > InitExprs.size())
343 InitExprs.reserve(NumInits);
344}
345
Douglas Gregor4c678342009-01-28 21:54:33 +0000346void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000347 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000348 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000349 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000350 InitExprs.resize(NumInits, 0);
351}
352
353Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
354 if (Init >= InitExprs.size()) {
355 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
356 InitExprs.back() = expr;
357 return 0;
358 }
359
360 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
361 InitExprs[Init] = expr;
362 return Result;
363}
364
Steve Naroffbfdcae62008-09-04 15:31:07 +0000365/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000366///
367const FunctionType *BlockExpr::getFunctionType() const {
368 return getType()->getAsBlockPointerType()->
369 getPointeeType()->getAsFunctionType();
370}
371
Steve Naroff56ee6892008-10-08 17:01:13 +0000372SourceLocation BlockExpr::getCaretLocation() const {
373 return TheBlock->getCaretLocation();
374}
375const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
376Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
377
378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379//===----------------------------------------------------------------------===//
380// Generic Expression Routines
381//===----------------------------------------------------------------------===//
382
Chris Lattner026dc962009-02-14 07:37:35 +0000383/// isUnusedResultAWarning - Return true if this immediate expression should
384/// be warned about if the result is unused. If so, fill in Loc and Ranges
385/// with location to warn on and the source range[s] to report with the
386/// warning.
387bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
388 SourceRange &R2) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 switch (getStmtClass()) {
390 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000391 Loc = getExprLoc();
392 R1 = getSourceRange();
393 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000395 return cast<ParenExpr>(this)->getSubExpr()->
396 isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 case UnaryOperatorClass: {
398 const UnaryOperator *UO = cast<UnaryOperator>(this);
399
400 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000401 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 case UnaryOperator::PostInc:
403 case UnaryOperator::PostDec:
404 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000405 case UnaryOperator::PreDec: // ++/--
406 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 case UnaryOperator::Deref:
408 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000409 if (getType().isVolatileQualified())
410 return false;
411 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 case UnaryOperator::Real:
413 case UnaryOperator::Imag:
414 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner026dc962009-02-14 07:37:35 +0000415 if (UO->getSubExpr()->getType().isVolatileQualified())
416 return false;
417 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 case UnaryOperator::Extension:
Chris Lattner026dc962009-02-14 07:37:35 +0000419 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 }
Chris Lattner026dc962009-02-14 07:37:35 +0000421 Loc = UO->getOperatorLoc();
422 R1 = UO->getSubExpr()->getSourceRange();
423 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000425 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000426 const BinaryOperator *BO = cast<BinaryOperator>(this);
427 // Consider comma to have side effects if the LHS or RHS does.
428 if (BO->getOpcode() == BinaryOperator::Comma)
429 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
430 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnere7716e62007-12-01 06:07:34 +0000431
Chris Lattner026dc962009-02-14 07:37:35 +0000432 if (BO->isAssignmentOp())
433 return false;
434 Loc = BO->getOperatorLoc();
435 R1 = BO->getLHS()->getSourceRange();
436 R2 = BO->getRHS()->getSourceRange();
437 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000438 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000439 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000440 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000441
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000442 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000443 // The condition must be evaluated, but if either the LHS or RHS is a
444 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000445 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stumpbefbcf42009-02-27 03:16:57 +0000446 if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner026dc962009-02-14 07:37:35 +0000447 return true;
448 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000449 }
450
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000452 // If the base pointer or element is to a volatile pointer/field, accessing
453 // it is a side effect.
454 if (getType().isVolatileQualified())
455 return false;
456 Loc = cast<MemberExpr>(this)->getMemberLoc();
457 R1 = SourceRange(Loc, Loc);
458 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
459 return true;
460
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 case ArraySubscriptExprClass:
462 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000463 // it is a side effect.
464 if (getType().isVolatileQualified())
465 return false;
466 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
467 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
468 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
469 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 case CallExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000472 case CXXOperatorCallExprClass: {
473 // If this is a direct call, get the callee.
474 const CallExpr *CE = cast<CallExpr>(this);
475 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
476 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
477 // If the callee has attribute pure, const, or warn_unused_result, warn
478 // about it. void foo() { strlen("bar"); } should warn.
479 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
480 if (FD->getAttr<WarnUnusedResultAttr>() ||
481 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
482 Loc = CE->getCallee()->getLocStart();
483 R1 = CE->getCallee()->getSourceRange();
484
485 if (unsigned NumArgs = CE->getNumArgs())
486 R2 = SourceRange(CE->getArg(0)->getLocStart(),
487 CE->getArg(NumArgs-1)->getLocEnd());
488 return true;
489 }
490 }
491 return false;
492 }
Chris Lattnera9c01022007-09-26 22:06:30 +0000493 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000494 return false;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000495 case StmtExprClass: {
496 // Statement exprs don't logically have side effects themselves, but are
497 // sometimes used in macros in ways that give them a type that is unused.
498 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
499 // however, if the result of the stmt expr is dead, we don't want to emit a
500 // warning.
501 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
502 if (!CS->body_empty())
503 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Chris Lattner026dc962009-02-14 07:37:35 +0000504 return E->isUnusedResultAWarning(Loc, R1, R2);
505
506 Loc = cast<StmtExpr>(this)->getLParenLoc();
507 R1 = getSourceRange();
508 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000509 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000510 case CStyleCastExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000511 // If this is a cast to void, check the operand. Otherwise, the result of
512 // the cast is unused.
513 if (getType()->isVoidType())
514 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
515 R1, R2);
516 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
517 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
518 return true;
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000519 case CXXFunctionalCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // If this is a cast to void, check the operand. Otherwise, the result of
521 // the cast is unused.
522 if (getType()->isVoidType())
Chris Lattner026dc962009-02-14 07:37:35 +0000523 return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc,
524 R1, R2);
525 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
526 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
527 return true;
528
Eli Friedman4be1f472008-05-19 21:24:43 +0000529 case ImplicitCastExprClass:
530 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner026dc962009-02-14 07:37:35 +0000531 return cast<ImplicitCastExpr>(this)
532 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedman4be1f472008-05-19 21:24:43 +0000533
Chris Lattner04421082008-04-08 04:40:51 +0000534 case CXXDefaultArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000535 return cast<CXXDefaultArgExpr>(this)
536 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000537
538 case CXXNewExprClass:
539 // FIXME: In theory, there might be new expressions that don't have side
540 // effects (e.g. a placement new with an uninitialized POD).
541 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000542 return false;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000543 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000544}
545
Douglas Gregorba7e2102008-10-22 15:04:37 +0000546/// DeclCanBeLvalue - Determine whether the given declaration can be
547/// an lvalue. This is a helper routine for isLvalue.
548static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000549 // C++ [temp.param]p6:
550 // A non-type non-reference template-parameter is not an lvalue.
551 if (const NonTypeTemplateParmDecl *NTTParm
552 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
553 return NTTParm->getType()->isReferenceType();
554
Douglas Gregor44b43212008-12-11 16:49:14 +0000555 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000556 // C++ 3.10p2: An lvalue refers to an object or function.
557 (Ctx.getLangOptions().CPlusPlus &&
558 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl)));
559}
560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
562/// incomplete type other than void. Nonarray expressions that can be lvalues:
563/// - name, where name must be a variable
564/// - e[i]
565/// - (e), where e must be an lvalue
566/// - e.name, where e must be an lvalue
567/// - e->name
568/// - *e, the type of e cannot be a function type
569/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000570/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000571/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000572///
Chris Lattner28be73f2008-07-26 21:30:36 +0000573Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor98cd5992008-10-21 23:43:52 +0000574 // first, check the type (C99 6.3.2.1). Expressions with function
575 // type in C are not lvalues, but they can be lvalues in C++.
576 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 return LV_NotObjectType;
578
Steve Naroffacb818a2008-02-10 01:39:04 +0000579 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner28be73f2008-07-26 21:30:36 +0000580 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000581 return LV_IncompleteVoidType;
582
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000583 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // the type looks fine, now check the expression
586 switch (getStmtClass()) {
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000587 case StringLiteralClass: // C99 6.5.1p4
588 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000589 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
591 // For vectors, make sure base is an lvalue (i.e. not a function call).
592 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000593 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return LV_Valid;
Douglas Gregor1a49af92009-01-06 05:10:23 +0000595 case DeclRefExprClass:
596 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000597 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
598 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return LV_Valid;
600 break;
Chris Lattner41110242008-06-17 18:05:57 +0000601 }
Steve Naroffdd972f22008-09-05 22:11:13 +0000602 case BlockDeclRefExprClass: {
603 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +0000604 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +0000605 return LV_Valid;
606 break;
607 }
Douglas Gregor86f19402008-12-20 23:49:58 +0000608 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +0000610 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
611 NamedDecl *Member = m->getMemberDecl();
612 // C++ [expr.ref]p4:
613 // If E2 is declared to have type "reference to T", then E1.E2
614 // is an lvalue.
615 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
616 if (Value->getType()->isReferenceType())
617 return LV_Valid;
618
619 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +0000620 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +0000621 return LV_Valid;
622
623 // -- If E2 is a non-static data member [...]. If E1 is an
624 // lvalue, then E1.E2 is an lvalue.
625 if (isa<FieldDecl>(Member))
626 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
627
628 // -- If it refers to a static member function [...], then
629 // E1.E2 is an lvalue.
630 // -- Otherwise, if E1.E2 refers to a non-static member
631 // function [...], then E1.E2 is not an lvalue.
632 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
633 return Method->isStatic()? LV_Valid : LV_MemberFunction;
634
635 // -- If E2 is a member enumerator [...], the expression E1.E2
636 // is not an lvalue.
637 if (isa<EnumConstantDecl>(Member))
638 return LV_InvalidExpression;
639
640 // Not an lvalue.
641 return LV_InvalidExpression;
642 }
643
644 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +0000645 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000646 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000647 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000649 return LV_Valid; // C99 6.5.3p4
650
651 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +0000652 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
653 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +0000654 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +0000655
656 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
657 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
658 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
659 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000661 case ImplicitCastExprClass:
662 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
663 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +0000665 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000666 case BinaryOperatorClass:
667 case CompoundAssignOperatorClass: {
668 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +0000669
670 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
671 BinOp->getOpcode() == BinaryOperator::Comma)
672 return BinOp->getRHS()->isLvalue(Ctx);
673
Sebastian Redl22460502009-02-07 00:15:38 +0000674 // C++ [expr.mptr.oper]p6
675 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
676 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
677 !BinOp->getType()->isFunctionType())
678 return BinOp->getLHS()->isLvalue(Ctx);
679
Douglas Gregorbf3af052008-11-13 20:12:29 +0000680 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000681 return LV_InvalidExpression;
682
Douglas Gregorbf3af052008-11-13 20:12:29 +0000683 if (Ctx.getLangOptions().CPlusPlus)
684 // C++ [expr.ass]p1:
685 // The result of an assignment operation [...] is an lvalue.
686 return LV_Valid;
687
688
689 // C99 6.5.16:
690 // An assignment expression [...] is not an lvalue.
691 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000692 }
Douglas Gregorb4609802008-11-14 16:09:21 +0000693 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +0000694 case CXXOperatorCallExprClass:
695 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000696 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +0000697 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000698 // is an lvalue reference.
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000699 QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType();
Douglas Gregor9d293df2008-10-28 00:22:11 +0000700 if (const PointerType *FnTypePtr = CalleeType->getAsPointerType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000701 CalleeType = FnTypePtr->getPointeeType();
702 if (const FunctionType *FnType = CalleeType->getAsFunctionType())
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000703 if (FnType->getResultType()->isLValueReferenceType())
Douglas Gregor88a35142008-12-22 05:46:06 +0000704 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000705
Douglas Gregor9d293df2008-10-28 00:22:11 +0000706 break;
707 }
Steve Naroffe6386392007-12-05 04:00:10 +0000708 case CompoundLiteralExprClass: // C99 6.5.2.5p5
709 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +0000710 case ChooseExprClass:
711 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +0000712 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +0000713 case ExtVectorElementExprClass:
714 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000715 return LV_DuplicateVectorComponents;
716 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000717 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
718 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +0000719 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
720 return LV_Valid;
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000721 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +0000722 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000723 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +0000724 return LV_Valid;
Douglas Gregor9d293df2008-10-28 00:22:11 +0000725 case VAArgExprClass:
Daniel Dunbaradadd8d2009-02-12 09:21:08 +0000726 return LV_NotObjectType;
Chris Lattner04421082008-04-08 04:40:51 +0000727 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +0000728 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis24b41fa2008-09-11 04:22:26 +0000729 case CXXConditionDeclExprClass:
730 return LV_Valid;
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000731 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +0000732 case CXXFunctionalCastExprClass:
733 case CXXStaticCastExprClass:
734 case CXXDynamicCastExprClass:
735 case CXXReinterpretCastExprClass:
736 case CXXConstCastExprClass:
737 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000738 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +0000739 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
740 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000741 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
742 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +0000743 return LV_Valid;
744 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +0000745 case CXXTypeidExprClass:
746 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
747 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 default:
749 break;
750 }
751 return LV_InvalidExpression;
752}
753
754/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
755/// does not have an incomplete type, does not have a const-qualified type, and
756/// if it is a structure or union, does not have any member (including,
757/// recursively, any member or element of all contained aggregates or unions)
758/// with a const-qualified type.
Chris Lattner28be73f2008-07-26 21:30:36 +0000759Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
760 isLvalueResult lvalResult = isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761
762 switch (lvalResult) {
Douglas Gregorae8d4672008-10-22 00:03:08 +0000763 case LV_Valid:
764 // C++ 3.10p11: Functions cannot be modified, but pointers to
765 // functions can be modifiable.
766 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
767 return MLV_NotObjectType;
768 break;
769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 case LV_NotObjectType: return MLV_NotObjectType;
771 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000772 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +0000773 case LV_InvalidExpression:
774 // If the top level is a C-style cast, and the subexpression is a valid
775 // lvalue, then this is probably a use of the old-school "cast as lvalue"
776 // GCC extension. We don't support it, but we want to produce good
777 // diagnostics when it happens so that the user knows why.
778 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
779 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
780 return MLV_LValueCast;
781 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +0000782 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 }
Eli Friedman04831aa2009-03-22 23:26:56 +0000784
785 // The following is illegal:
786 // void takeclosure(void (^C)(void));
787 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
788 //
Chris Lattner7fd09952009-03-23 17:57:53 +0000789 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +0000790 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
791 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
792 return MLV_NotBlockQualified;
793 }
794
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000795 QualType CT = Ctx.getCanonicalType(getType());
796
797 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000799 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000801 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 return MLV_IncompleteType;
803
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000804 if (const RecordType *r = CT->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 if (r->hasConstFields())
806 return MLV_ConstQualified;
807 }
Fariborz Jahaniand1fa6442009-01-12 19:55:42 +0000808
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000809 // Assigning to an 'implicit' property?
Chris Lattner7fd09952009-03-23 17:57:53 +0000810 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianba8d2d62008-11-22 20:25:50 +0000811 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
812 if (KVCExpr->getSetterMethod() == 0)
813 return MLV_NoSetterProperty;
814 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 return MLV_Valid;
816}
817
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000818/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner4cc62712007-11-27 21:35:27 +0000819/// duration. This means that the address of this expression is a link-time
820/// constant.
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000821bool Expr::hasGlobalStorage() const {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000822 switch (getStmtClass()) {
823 default:
824 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000825 case ParenExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000826 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner4cc62712007-11-27 21:35:27 +0000827 case ImplicitCastExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000828 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffe9b12192008-01-14 18:19:28 +0000829 case CompoundLiteralExprClass:
830 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor1a49af92009-01-06 05:10:23 +0000831 case DeclRefExprClass:
832 case QualifiedDeclRefExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000833 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
834 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000835 return VD->hasGlobalStorage();
Seo Sanghyeon63f067f2008-04-04 09:45:30 +0000836 if (isa<FunctionDecl>(D))
837 return true;
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000838 return false;
839 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000840 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000841 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000842 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000843 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000844 case ArraySubscriptExprClass:
Ted Kremenek2e5f54a2008-02-27 18:39:48 +0000845 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattnerd9f69102008-08-10 01:53:14 +0000846 case PredefinedExprClass:
Chris Lattnerfa28b302008-01-12 08:14:25 +0000847 return true;
Chris Lattner04421082008-04-08 04:40:51 +0000848 case CXXDefaultArgExprClass:
849 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000850 }
851}
852
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000853/// isOBJCGCCandidate - Check if an expression is objc gc'able.
854///
855bool Expr::isOBJCGCCandidate() const {
856 switch (getStmtClass()) {
857 default:
858 return false;
859 case ObjCIvarRefExprClass:
860 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +0000861 case Expr::UnaryOperatorClass:
862 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate();
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +0000863 case ParenExprClass:
864 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate();
865 case ImplicitCastExprClass:
866 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate();
867 case DeclRefExprClass:
868 case QualifiedDeclRefExprClass: {
869 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
870 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
871 return VD->hasGlobalStorage();
872 return false;
873 }
874 case MemberExprClass: {
875 const MemberExpr *M = cast<MemberExpr>(this);
876 return !M->isArrow() && M->getBase()->isOBJCGCCandidate();
877 }
878 case ArraySubscriptExprClass:
879 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate();
880 }
881}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +0000882Expr* Expr::IgnoreParens() {
883 Expr* E = this;
884 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
885 E = P->getSubExpr();
886
887 return E;
888}
889
Chris Lattner56f34942008-02-13 01:02:39 +0000890/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
891/// or CastExprs or ImplicitCastExprs, returning their operand.
892Expr *Expr::IgnoreParenCasts() {
893 Expr *E = this;
894 while (true) {
895 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
896 E = P->getSubExpr();
897 else if (CastExpr *P = dyn_cast<CastExpr>(E))
898 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +0000899 else
900 return E;
901 }
902}
903
Chris Lattnerecdd8412009-03-13 17:28:01 +0000904/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
905/// value (including ptr->int casts of the same size). Strip off any
906/// ParenExpr or CastExprs, returning their operand.
907Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
908 Expr *E = this;
909 while (true) {
910 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
911 E = P->getSubExpr();
912 continue;
913 }
914
915 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
916 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
917 // ptr<->int casts of the same width. We also ignore all identify casts.
918 Expr *SE = P->getSubExpr();
919
920 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
921 E = SE;
922 continue;
923 }
924
925 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
926 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
927 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
928 E = SE;
929 continue;
930 }
931 }
932
933 return E;
934 }
935}
936
937
Douglas Gregor898574e2008-12-05 23:32:09 +0000938/// hasAnyTypeDependentArguments - Determines if any of the expressions
939/// in Exprs is type-dependent.
940bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
941 for (unsigned I = 0; I < NumExprs; ++I)
942 if (Exprs[I]->isTypeDependent())
943 return true;
944
945 return false;
946}
947
948/// hasAnyValueDependentArguments - Determines if any of the expressions
949/// in Exprs is value-dependent.
950bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
951 for (unsigned I = 0; I < NumExprs; ++I)
952 if (Exprs[I]->isValueDependent())
953 return true;
954
955 return false;
956}
957
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000958bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000959 // This function is attempting whether an expression is an initializer
960 // which can be evaluated at compile-time. isEvaluatable handles most
961 // of the cases, but it can't deal with some initializer-specific
962 // expressions, and it can't deal with aggregates; we deal with those here,
963 // and fall back to isEvaluatable for the other cases.
964
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000965 // FIXME: This function assumes the variable being assigned to
966 // isn't a reference type!
967
Anders Carlssone8a32b82008-11-24 05:23:59 +0000968 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000969 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000970 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000971 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +0000972 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +0000973 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000974 // This handles gcc's extension that allows global initializers like
975 // "struct x {int x;} x = (struct x) {};".
976 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +0000977 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000978 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +0000979 }
Anders Carlssone8a32b82008-11-24 05:23:59 +0000980 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +0000981 // FIXME: This doesn't deal with fields with reference types correctly.
982 // FIXME: This incorrectly allows pointers cast to integers to be assigned
983 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +0000984 const InitListExpr *Exp = cast<InitListExpr>(this);
985 unsigned numInits = Exp->getNumInits();
986 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000987 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +0000988 return false;
989 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000990 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +0000991 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000992 case ImplicitValueInitExprClass:
993 return true;
Eli Friedmanc39dc9a2009-01-25 03:12:18 +0000994 case ParenExprClass: {
995 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
996 }
997 case UnaryOperatorClass: {
998 const UnaryOperator* Exp = cast<UnaryOperator>(this);
999 if (Exp->getOpcode() == UnaryOperator::Extension)
1000 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1001 break;
1002 }
1003 case CStyleCastExprClass:
1004 // Handle casts with a destination that's a struct or union; this
1005 // deals with both the gcc no-op struct cast extension and the
1006 // cast-to-union extension.
1007 if (getType()->isRecordType())
1008 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1009 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001010 }
1011
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001012 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001013}
1014
Reid Spencer5f016e22007-07-11 17:01:13 +00001015/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001016/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001017
1018/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1019/// comma, etc
1020///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001021/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1022/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1023/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001024
Eli Friedmane28d7192009-02-26 09:29:13 +00001025// CheckICE - This function does the fundamental ICE checking: the returned
1026// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1027// Note that to reduce code duplication, this helper does no evaluation
1028// itself; the caller checks whether the expression is evaluatable, and
1029// in the rare cases where CheckICE actually cares about the evaluated
1030// value, it calls into Evalute.
1031//
1032// Meanings of Val:
1033// 0: This expression is an ICE if it can be evaluated by Evaluate.
1034// 1: This expression is not an ICE, but if it isn't evaluated, it's
1035// a legal subexpression for an ICE. This return value is used to handle
1036// the comma operator in C99 mode.
1037// 2: This expression is not an ICE, and is not a legal subexpression for one.
1038
1039struct ICEDiag {
1040 unsigned Val;
1041 SourceLocation Loc;
1042
1043 public:
1044 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1045 ICEDiag() : Val(0) {}
1046};
1047
1048ICEDiag NoDiag() { return ICEDiag(); }
1049
Eli Friedman60ce9632009-02-27 04:07:58 +00001050static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1051 Expr::EvalResult EVResult;
1052 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1053 !EVResult.Val.isInt()) {
1054 return ICEDiag(2, E->getLocStart());
1055 }
1056 return NoDiag();
1057}
1058
Eli Friedmane28d7192009-02-26 09:29:13 +00001059static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001060 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001061 if (!E->getType()->isIntegralType()) {
1062 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001063 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001064
1065 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001067 return ICEDiag(2, E->getLocStart());
1068 case Expr::ParenExprClass:
1069 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1070 case Expr::IntegerLiteralClass:
1071 case Expr::CharacterLiteralClass:
1072 case Expr::CXXBoolLiteralExprClass:
1073 case Expr::CXXZeroInitValueExprClass:
1074 case Expr::TypesCompatibleExprClass:
1075 case Expr::UnaryTypeTraitExprClass:
1076 return NoDiag();
1077 case Expr::CallExprClass:
1078 case Expr::CXXOperatorCallExprClass: {
1079 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001080 if (CE->isBuiltinCall(Ctx))
1081 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001082 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001083 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001084 case Expr::DeclRefExprClass:
1085 case Expr::QualifiedDeclRefExprClass:
1086 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1087 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001088 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedmane28d7192009-02-26 09:29:13 +00001089 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001090 // C++ 7.1.5.1p2
1091 // A variable of non-volatile const-qualified integral or enumeration
1092 // type initialized by an ICE can be used in ICEs.
1093 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001094 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001095 if (const Expr *Init = Dcl->getInit())
Eli Friedmane28d7192009-02-26 09:29:13 +00001096 return CheckICE(Init, Ctx);
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001097 }
1098 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001099 return ICEDiag(2, E->getLocStart());
1100 case Expr::UnaryOperatorClass: {
1101 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 switch (Exp->getOpcode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001104 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001106 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001110 case UnaryOperator::Real:
1111 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001112 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001113 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001114 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1115 // Evaluate matches the proposed gcc behavior for cases like
1116 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1117 // compliance: we should warn earlier for offsetof expressions with
1118 // array subscripts that aren't ICEs, and if the array subscripts
1119 // are ICEs, the value of the offsetof must be an integer constant.
1120 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001123 case Expr::SizeOfAlignOfExprClass: {
1124 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1125 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1126 return ICEDiag(2, E->getLocStart());
1127 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001129 case Expr::BinaryOperatorClass: {
1130 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 switch (Exp->getOpcode()) {
1132 default:
Eli Friedmane28d7192009-02-26 09:29:13 +00001133 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001137 case BinaryOperator::Add:
1138 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001141 case BinaryOperator::LT:
1142 case BinaryOperator::GT:
1143 case BinaryOperator::LE:
1144 case BinaryOperator::GE:
1145 case BinaryOperator::EQ:
1146 case BinaryOperator::NE:
1147 case BinaryOperator::And:
1148 case BinaryOperator::Xor:
1149 case BinaryOperator::Or:
1150 case BinaryOperator::Comma: {
1151 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1152 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001153 if (Exp->getOpcode() == BinaryOperator::Div ||
1154 Exp->getOpcode() == BinaryOperator::Rem) {
1155 // Evaluate gives an error for undefined Div/Rem, so make sure
1156 // we don't evaluate one.
1157 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1158 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1159 if (REval == 0)
1160 return ICEDiag(1, E->getLocStart());
1161 if (REval.isSigned() && REval.isAllOnesValue()) {
1162 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1163 if (LEval.isMinSignedValue())
1164 return ICEDiag(1, E->getLocStart());
1165 }
1166 }
1167 }
1168 if (Exp->getOpcode() == BinaryOperator::Comma) {
1169 if (Ctx.getLangOptions().C99) {
1170 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1171 // if it isn't evaluated.
1172 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1173 return ICEDiag(1, E->getLocStart());
1174 } else {
1175 // In both C89 and C++, commas in ICEs are illegal.
1176 return ICEDiag(2, E->getLocStart());
1177 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001178 }
1179 if (LHSResult.Val >= RHSResult.Val)
1180 return LHSResult;
1181 return RHSResult;
1182 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001184 case BinaryOperator::LOr: {
1185 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1186 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1187 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1188 // Rare case where the RHS has a comma "side-effect"; we need
1189 // to actually check the condition to see whether the side
1190 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001191 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001192 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001193 return RHSResult;
1194 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001195 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001196
Eli Friedmane28d7192009-02-26 09:29:13 +00001197 if (LHSResult.Val >= RHSResult.Val)
1198 return LHSResult;
1199 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001201 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001203 case Expr::ImplicitCastExprClass:
1204 case Expr::CStyleCastExprClass:
1205 case Expr::CXXFunctionalCastExprClass: {
1206 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1207 if (SubExpr->getType()->isIntegralType())
1208 return CheckICE(SubExpr, Ctx);
1209 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1210 return NoDiag();
1211 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001213 case Expr::ConditionalOperatorClass: {
1214 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner28daa532008-12-12 06:55:44 +00001215 // If the condition (ignoring parens) is a __builtin_constant_p call,
1216 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001217 // expression, and it is fully evaluated. This is an important GNU
1218 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001219 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001220 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001221 Expr::EvalResult EVResult;
1222 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1223 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001224 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001225 }
1226 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001227 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001228 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1229 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1230 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1231 if (CondResult.Val == 2)
1232 return CondResult;
1233 if (TrueResult.Val == 2)
1234 return TrueResult;
1235 if (FalseResult.Val == 2)
1236 return FalseResult;
1237 if (CondResult.Val == 1)
1238 return CondResult;
1239 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1240 return NoDiag();
1241 // Rare case where the diagnostics depend on which side is evaluated
1242 // Note that if we get here, CondResult is 0, and at least one of
1243 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001244 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001245 return FalseResult;
1246 }
1247 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001249 case Expr::CXXDefaultArgExprClass:
1250 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001251 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001252 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001253 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001255}
Reid Spencer5f016e22007-07-11 17:01:13 +00001256
Eli Friedmane28d7192009-02-26 09:29:13 +00001257bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1258 SourceLocation *Loc, bool isEvaluated) const {
1259 ICEDiag d = CheckICE(this, Ctx);
1260 if (d.Val != 0) {
1261 if (Loc) *Loc = d.Loc;
1262 return false;
1263 }
1264 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001265 if (!Evaluate(EvalResult, Ctx))
1266 assert(0 && "ICE cannot be evaluated!");
1267 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1268 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001269 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 return true;
1271}
1272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1274/// integer constant expression with the value zero, or if this is one that is
1275/// cast to void*.
Anders Carlssonefa9b382008-12-01 02:13:57 +00001276bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1277{
Sebastian Redl07779722008-10-31 14:43:28 +00001278 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001279 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001280 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001281 // Check that it is a cast to void*.
1282 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
1283 QualType Pointee = PT->getPointeeType();
1284 if (Pointee.getCVRQualifiers() == 0 &&
1285 Pointee->isVoidType() && // to void*
1286 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssond2652772008-12-01 06:28:23 +00001287 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl07779722008-10-31 14:43:28 +00001288 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001290 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1291 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssond2652772008-12-01 06:28:23 +00001292 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffaa58f002008-01-14 16:10:57 +00001293 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1294 // Accept ((void*)0) as a null pointer constant, as many other
1295 // implementations do.
Anders Carlssond2652772008-12-01 06:28:23 +00001296 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner8123a952008-04-10 02:22:51 +00001297 } else if (const CXXDefaultArgExpr *DefaultArg
1298 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001299 // See through default argument expressions
Anders Carlssond2652772008-12-01 06:28:23 +00001300 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001301 } else if (isa<GNUNullExpr>(this)) {
1302 // The GNU __null extension is always a null pointer constant.
1303 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001304 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001305
Steve Naroffaa58f002008-01-14 16:10:57 +00001306 // This expression must be an integer type.
1307 if (!getType()->isIntegerType())
1308 return false;
1309
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 // If we have an integer constant expression, we need to *evaluate* it and
1311 // test for the value 0.
Anders Carlssond2652772008-12-01 06:28:23 +00001312 // FIXME: We should probably return false if we're compiling in strict mode
1313 // and Diag is not null (this indicates that the value was foldable but not
1314 // an ICE.
1315 EvalResult Result;
Anders Carlssonefa9b382008-12-01 02:13:57 +00001316 return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
Anders Carlssond2652772008-12-01 06:28:23 +00001317 Result.Val.isInt() && Result.Val.getInt() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001318}
Steve Naroff31a45842007-07-28 23:10:27 +00001319
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001320/// isBitField - Return true if this expression is a bit-field.
1321bool Expr::isBitField() {
1322 Expr *E = this->IgnoreParenCasts();
1323 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001324 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
1325 return Field->isBitField();
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001326 return false;
1327}
1328
Chris Lattner2140e902009-02-16 22:14:05 +00001329/// isArrow - Return true if the base expression is a pointer to vector,
1330/// return false if the base expression is a vector.
1331bool ExtVectorElementExpr::isArrow() const {
1332 return getBase()->getType()->isPointerType();
1333}
1334
Nate Begeman213541a2008-04-18 23:10:10 +00001335unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begeman8a997642008-05-09 06:41:27 +00001336 if (const VectorType *VT = getType()->getAsVectorType())
1337 return VT->getNumElements();
1338 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001339}
1340
Nate Begeman8a997642008-05-09 06:41:27 +00001341/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001342bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +00001343 const char *compStr = Accessor.getName();
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001344 unsigned length = Accessor.getLength();
Nate Begeman190d6a22009-01-18 02:01:21 +00001345
1346 // Halving swizzles do not contain duplicate elements.
1347 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1348 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1349 return false;
1350
1351 // Advance past s-char prefix on hex swizzles.
1352 if (*compStr == 's') {
1353 compStr++;
1354 length--;
1355 }
Steve Narofffec0b492007-07-30 03:29:09 +00001356
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001357 for (unsigned i = 0; i != length-1; i++) {
Steve Narofffec0b492007-07-30 03:29:09 +00001358 const char *s = compStr+i;
1359 for (const char c = *s++; *s; s++)
1360 if (c == *s)
1361 return true;
1362 }
1363 return false;
1364}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001365
Nate Begeman8a997642008-05-09 06:41:27 +00001366/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001367void ExtVectorElementExpr::getEncodedElementAccess(
1368 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner7e3e9b12008-11-19 07:55:04 +00001369 const char *compStr = Accessor.getName();
Nate Begeman353417a2009-01-18 01:47:54 +00001370 if (*compStr == 's')
1371 compStr++;
1372
1373 bool isHi = !strcmp(compStr, "hi");
1374 bool isLo = !strcmp(compStr, "lo");
1375 bool isEven = !strcmp(compStr, "even");
1376 bool isOdd = !strcmp(compStr, "odd");
1377
Nate Begeman8a997642008-05-09 06:41:27 +00001378 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1379 uint64_t Index;
1380
1381 if (isHi)
1382 Index = e + i;
1383 else if (isLo)
1384 Index = i;
1385 else if (isEven)
1386 Index = 2 * i;
1387 else if (isOdd)
1388 Index = 2 * i + 1;
1389 else
1390 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001391
Nate Begeman3b8d1162008-05-13 21:03:02 +00001392 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001393 }
Nate Begeman8a997642008-05-09 06:41:27 +00001394}
1395
Steve Naroff68d331a2007-09-27 14:38:14 +00001396// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001397ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001398 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001399 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001400 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001401 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001402 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001403 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001404 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001405 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001406 if (NumArgs) {
1407 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001408 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1409 }
Steve Naroff563477d2007-09-18 23:55:05 +00001410 LBracloc = LBrac;
1411 RBracloc = RBrac;
1412}
1413
Steve Naroff68d331a2007-09-27 14:38:14 +00001414// constructor for class messages.
1415// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001416ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001417 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001418 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001419 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001420 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001421 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001422 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001423 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001424 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001425 if (NumArgs) {
1426 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001427 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1428 }
Steve Naroff563477d2007-09-18 23:55:05 +00001429 LBracloc = LBrac;
1430 RBracloc = RBrac;
1431}
1432
Ted Kremenek4df728e2008-06-24 15:50:53 +00001433// constructor for class messages.
1434ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1435 QualType retType, ObjCMethodDecl *mproto,
1436 SourceLocation LBrac, SourceLocation RBrac,
1437 Expr **ArgExprs, unsigned nargs)
1438: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1439MethodProto(mproto) {
1440 NumArgs = nargs;
1441 SubExprs = new Stmt*[NumArgs+1];
1442 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1443 if (NumArgs) {
1444 for (unsigned i = 0; i != NumArgs; ++i)
1445 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1446 }
1447 LBracloc = LBrac;
1448 RBracloc = RBrac;
1449}
1450
1451ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1452 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1453 switch (x & Flags) {
1454 default:
1455 assert(false && "Invalid ObjCMessageExpr.");
1456 case IsInstMeth:
1457 return ClassInfo(0, 0);
1458 case IsClsMethDeclUnknown:
1459 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1460 case IsClsMethDeclKnown: {
1461 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1462 return ClassInfo(D, D->getIdentifier());
1463 }
1464 }
1465}
1466
Chris Lattner27437ca2007-10-25 00:29:32 +00001467bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar32442bb2008-08-13 23:47:13 +00001468 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001469}
1470
Sebastian Redl05189992008-11-11 17:56:53 +00001471void SizeOfAlignOfExpr::Destroy(ASTContext& C) {
1472 // Override default behavior of traversing children. If this has a type
1473 // operand and the type is a variable-length array, the child iteration
1474 // will iterate over the size expression. However, this expression belongs
1475 // to the type, not to this, so we don't want to delete it.
1476 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001477 if (isArgumentType()) {
1478 this->~SizeOfAlignOfExpr();
1479 C.Deallocate(this);
1480 }
Sebastian Redl05189992008-11-11 17:56:53 +00001481 else
1482 Expr::Destroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001483}
1484
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001485//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001486// DesignatedInitExpr
1487//===----------------------------------------------------------------------===//
1488
1489IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1490 assert(Kind == FieldDesignator && "Only valid on a field designator");
1491 if (Field.NameOrField & 0x01)
1492 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1493 else
1494 return getField()->getIdentifier();
1495}
1496
1497DesignatedInitExpr *
1498DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1499 unsigned NumDesignators,
1500 Expr **IndexExprs, unsigned NumIndexExprs,
1501 SourceLocation ColonOrEqualLoc,
1502 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001503 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1504 sizeof(Designator) * NumDesignators +
1505 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001506 DesignatedInitExpr *DIE
1507 = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators,
1508 ColonOrEqualLoc, UsesColonSyntax,
1509 NumIndexExprs + 1);
1510
1511 // Fill in the designators
1512 unsigned ExpectedNumSubExprs = 0;
1513 designators_iterator Desig = DIE->designators_begin();
1514 for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) {
1515 new (static_cast<void*>(Desig)) Designator(Designators[Idx]);
1516 if (Designators[Idx].isArrayDesignator())
1517 ++ExpectedNumSubExprs;
1518 else if (Designators[Idx].isArrayRangeDesignator())
1519 ExpectedNumSubExprs += 2;
1520 }
1521 assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!");
1522
1523 // Fill in the subexpressions, including the initializer expression.
1524 child_iterator Child = DIE->child_begin();
1525 *Child++ = Init;
1526 for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child)
1527 *Child = IndexExprs[Idx];
1528
1529 return DIE;
1530}
1531
1532SourceRange DesignatedInitExpr::getSourceRange() const {
1533 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001534 Designator &First =
1535 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001536 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001537 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001538 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1539 else
1540 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1541 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001542 StartLoc =
1543 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001544 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1545}
1546
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001547DesignatedInitExpr::designators_iterator
1548DesignatedInitExpr::designators_begin() {
Douglas Gregor05c13a32009-01-22 00:58:24 +00001549 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1550 Ptr += sizeof(DesignatedInitExpr);
1551 return static_cast<Designator*>(static_cast<void*>(Ptr));
1552}
1553
1554DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() {
1555 return designators_begin() + NumDesignators;
1556}
1557
1558Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1559 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1560 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1561 Ptr += sizeof(DesignatedInitExpr);
1562 Ptr += sizeof(Designator) * NumDesignators;
1563 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1564 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1565}
1566
1567Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1568 assert(D.Kind == Designator::ArrayRangeDesignator &&
1569 "Requires array range designator");
1570 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1571 Ptr += sizeof(DesignatedInitExpr);
1572 Ptr += sizeof(Designator) * NumDesignators;
1573 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1574 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1575}
1576
1577Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1578 assert(D.Kind == Designator::ArrayRangeDesignator &&
1579 "Requires array range designator");
1580 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1581 Ptr += sizeof(DesignatedInitExpr);
1582 Ptr += sizeof(Designator) * NumDesignators;
1583 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1584 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1585}
1586
1587//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001588// ExprIterator.
1589//===----------------------------------------------------------------------===//
1590
1591Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1592Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1593Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1594const Expr* ConstExprIterator::operator[](size_t idx) const {
1595 return cast<Expr>(I[idx]);
1596}
1597const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1598const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1599
1600//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001601// Child Iterators for iterating over subexpressions/substatements
1602//===----------------------------------------------------------------------===//
1603
1604// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001605Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1606Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001607
Steve Naroff7779db42007-11-12 14:29:37 +00001608// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001609Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1610Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00001611
Steve Naroffe3e9add2008-06-02 23:03:37 +00001612// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001613Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1614Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00001615
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00001616// ObjCKVCRefExpr
1617Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1618Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1619
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001620// ObjCSuperExpr
1621Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1622Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1623
Chris Lattnerd9f69102008-08-10 01:53:14 +00001624// PredefinedExpr
1625Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1626Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001627
1628// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001629Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1630Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001631
1632// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001633Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00001634Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001635
1636// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001637Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1638Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001639
Chris Lattner5d661452007-08-26 03:42:43 +00001640// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00001641Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1642Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00001643
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001644// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001645Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1646Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001647
1648// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001649Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1650Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001651
1652// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00001653Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1654Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001655
Sebastian Redl05189992008-11-11 17:56:53 +00001656// SizeOfAlignOfExpr
1657Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1658 // If this is of a type and the type is a VLA type (and not a typedef), the
1659 // size expression of the VLA needs to be treated as an executable expression.
1660 // Why isn't this weirdness documented better in StmtIterator?
1661 if (isArgumentType()) {
1662 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1663 getArgumentType().getTypePtr()))
1664 return child_iterator(T);
1665 return child_iterator();
1666 }
Sebastian Redld4575892008-12-03 23:17:54 +00001667 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001668}
Sebastian Redl05189992008-11-11 17:56:53 +00001669Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1670 if (isArgumentType())
1671 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00001672 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00001673}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001674
1675// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001676Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001677 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001678}
Ted Kremenek1237c672007-08-24 20:06:47 +00001679Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001680 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001681}
1682
1683// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001684Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001685 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001686}
Ted Kremenek1237c672007-08-24 20:06:47 +00001687Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001688 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001689}
Ted Kremenek1237c672007-08-24 20:06:47 +00001690
1691// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001692Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1693Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001694
Nate Begeman213541a2008-04-18 23:10:10 +00001695// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001696Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1697Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001698
1699// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001700Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1701Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001702
Ted Kremenek1237c672007-08-24 20:06:47 +00001703// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001704Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1705Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001706
1707// BinaryOperator
1708Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001709 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001710}
Ted Kremenek1237c672007-08-24 20:06:47 +00001711Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001712 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001713}
1714
1715// ConditionalOperator
1716Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001717 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00001718}
Ted Kremenek1237c672007-08-24 20:06:47 +00001719Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001720 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001721}
1722
1723// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001724Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1725Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001726
Ted Kremenek1237c672007-08-24 20:06:47 +00001727// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001728Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1729Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001730
1731// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001732Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1733 return child_iterator();
1734}
1735
1736Stmt::child_iterator TypesCompatibleExpr::child_end() {
1737 return child_iterator();
1738}
Ted Kremenek1237c672007-08-24 20:06:47 +00001739
1740// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001741Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1742Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00001743
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001744// GNUNullExpr
1745Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
1746Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
1747
Eli Friedmand38617c2008-05-14 19:38:39 +00001748// ShuffleVectorExpr
1749Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001750 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00001751}
1752Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001753 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00001754}
1755
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001756// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00001757Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1758Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001759
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001760// InitListExpr
1761Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001762 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001763}
1764Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001765 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001766}
1767
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001768// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00001769Stmt::child_iterator DesignatedInitExpr::child_begin() {
1770 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1771 Ptr += sizeof(DesignatedInitExpr);
1772 Ptr += sizeof(Designator) * NumDesignators;
1773 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1774}
1775Stmt::child_iterator DesignatedInitExpr::child_end() {
1776 return child_iterator(&*child_begin() + NumSubExprs);
1777}
1778
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001779// ImplicitValueInitExpr
1780Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
1781 return child_iterator();
1782}
1783
1784Stmt::child_iterator ImplicitValueInitExpr::child_end() {
1785 return child_iterator();
1786}
1787
Ted Kremenek1237c672007-08-24 20:06:47 +00001788// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001789Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001790 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001791}
1792Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00001793 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00001794}
Ted Kremenek1237c672007-08-24 20:06:47 +00001795
1796// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001797Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1798Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001799
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001800// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001801Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1802 return child_iterator();
1803}
1804Stmt::child_iterator ObjCSelectorExpr::child_end() {
1805 return child_iterator();
1806}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001807
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001808// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001809Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1810 return child_iterator();
1811}
1812Stmt::child_iterator ObjCProtocolExpr::child_end() {
1813 return child_iterator();
1814}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001815
Steve Naroff563477d2007-09-18 23:55:05 +00001816// ObjCMessageExpr
Ted Kremenekea958e572008-05-01 17:26:20 +00001817Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00001818 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00001819}
1820Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00001821 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00001822}
1823
Steve Naroff4eb206b2008-09-03 18:15:37 +00001824// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00001825Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1826Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00001827
Ted Kremenek9da13f92008-09-26 23:24:14 +00001828Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1829Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }