blob: 0f3cbe18d9e923c5e58762c0c052d42ec099c5bf [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Anders Carlsson63f1ad92009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000023#include "clang/Basic/TargetInfo.h"
Douglas Gregorcc94ab72009-04-15 06:41:24 +000024#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Primary Expressions.
29//===----------------------------------------------------------------------===//
30
Sebastian Redlf80e2612009-05-16 18:50:46 +000031PredefinedExpr* PredefinedExpr::Clone(ASTContext &C) const {
32 return new (C) PredefinedExpr(Loc, getType(), Type);
33}
34
Anders Carlsson7f2e7442009-03-15 18:34:13 +000035IntegerLiteral* IntegerLiteral::Clone(ASTContext &C) const {
36 return new (C) IntegerLiteral(Value, getType(), Loc);
37}
38
Sebastian Redlf80e2612009-05-16 18:50:46 +000039CharacterLiteral* CharacterLiteral::Clone(ASTContext &C) const {
40 return new (C) CharacterLiteral(Value, IsWide, getType(), Loc);
41}
42
43FloatingLiteral* FloatingLiteral::Clone(ASTContext &C) const {
Chris Lattnerff1bf1a2009-06-29 17:34:55 +000044 return new (C) FloatingLiteral(Value, IsExact, getType(), Loc);
Sebastian Redlf80e2612009-05-16 18:50:46 +000045}
46
Douglas Gregor4a951342009-05-18 22:38:38 +000047ImaginaryLiteral* ImaginaryLiteral::Clone(ASTContext &C) const {
48 // FIXME: Use virtual Clone(), once it is available
49 Expr *ClonedVal = 0;
50 if (const IntegerLiteral *IntLit = dyn_cast<IntegerLiteral>(Val))
51 ClonedVal = IntLit->Clone(C);
52 else
53 ClonedVal = cast<FloatingLiteral>(Val)->Clone(C);
54 return new (C) ImaginaryLiteral(ClonedVal, getType());
55}
56
Sebastian Redlf80e2612009-05-16 18:50:46 +000057GNUNullExpr* GNUNullExpr::Clone(ASTContext &C) const {
58 return new (C) GNUNullExpr(getType(), TokenLoc);
59}
60
Chris Lattnere0391b22008-06-07 22:13:43 +000061/// getValueAsApproximateDouble - This returns the value as an inaccurate
62/// double. Note that this may cause loss of precision, but is useful for
63/// debugging dumps, etc.
64double FloatingLiteral::getValueAsApproximateDouble() const {
65 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000066 bool ignored;
67 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
68 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000069 return V.convertToDouble();
70}
71
Chris Lattneraa491192009-02-18 06:40:38 +000072StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
73 unsigned ByteLength, bool Wide,
74 QualType Ty,
Anders Carlsson7f2e7442009-03-15 18:34:13 +000075 const SourceLocation *Loc,
76 unsigned NumStrs) {
Chris Lattneraa491192009-02-18 06:40:38 +000077 // Allocate enough space for the StringLiteral plus an array of locations for
78 // any concatenated string tokens.
79 void *Mem = C.Allocate(sizeof(StringLiteral)+
80 sizeof(SourceLocation)*(NumStrs-1),
81 llvm::alignof<StringLiteral>());
82 StringLiteral *SL = new (Mem) StringLiteral(Ty);
83
Chris Lattner4b009652007-07-25 00:24:17 +000084 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattneraa491192009-02-18 06:40:38 +000085 char *AStrData = new (C, 1) char[ByteLength];
86 memcpy(AStrData, StrData, ByteLength);
87 SL->StrData = AStrData;
88 SL->ByteLength = ByteLength;
89 SL->IsWide = Wide;
90 SL->TokLocs[0] = Loc[0];
91 SL->NumConcatenated = NumStrs;
Chris Lattner4b009652007-07-25 00:24:17 +000092
Chris Lattnerc3144742009-02-18 05:49:11 +000093 if (NumStrs != 1)
Chris Lattneraa491192009-02-18 06:40:38 +000094 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
95 return SL;
Chris Lattnerc3144742009-02-18 05:49:11 +000096}
97
Douglas Gregor596e0932009-04-15 16:35:07 +000098StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
99 void *Mem = C.Allocate(sizeof(StringLiteral)+
100 sizeof(SourceLocation)*(NumStrs-1),
101 llvm::alignof<StringLiteral>());
102 StringLiteral *SL = new (Mem) StringLiteral(QualType());
103 SL->StrData = 0;
104 SL->ByteLength = 0;
105 SL->NumConcatenated = NumStrs;
106 return SL;
107}
108
Anders Carlsson7f2e7442009-03-15 18:34:13 +0000109StringLiteral* StringLiteral::Clone(ASTContext &C) const {
110 return Create(C, StrData, ByteLength, IsWide, getType(),
111 TokLocs, NumConcatenated);
112}
Chris Lattnerc3144742009-02-18 05:49:11 +0000113
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000114void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek0c97e042009-02-07 01:47:29 +0000115 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000116 Expr::DoDestroy(C);
Chris Lattner4b009652007-07-25 00:24:17 +0000117}
118
Douglas Gregor596e0932009-04-15 16:35:07 +0000119void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
120 if (StrData)
121 C.Deallocate(const_cast<char*>(StrData));
122
123 char *AStrData = new (C, 1) char[Len];
124 memcpy(AStrData, Str, Len);
125 StrData = AStrData;
126 ByteLength = Len;
127}
128
Chris Lattner4b009652007-07-25 00:24:17 +0000129/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
130/// corresponds to, e.g. "sizeof" or "[pre]++".
131const char *UnaryOperator::getOpcodeStr(Opcode Op) {
132 switch (Op) {
133 default: assert(0 && "Unknown unary operator");
134 case PostInc: return "++";
135 case PostDec: return "--";
136 case PreInc: return "++";
137 case PreDec: return "--";
138 case AddrOf: return "&";
139 case Deref: return "*";
140 case Plus: return "+";
141 case Minus: return "-";
142 case Not: return "~";
143 case LNot: return "!";
144 case Real: return "__real";
145 case Imag: return "__imag";
Chris Lattner4b009652007-07-25 00:24:17 +0000146 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000147 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149}
150
Douglas Gregorc78182d2009-03-13 23:49:33 +0000151UnaryOperator::Opcode
152UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
153 switch (OO) {
Douglas Gregorc78182d2009-03-13 23:49:33 +0000154 default: assert(false && "No unary operator for overloaded function");
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000155 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
156 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
157 case OO_Amp: return AddrOf;
158 case OO_Star: return Deref;
159 case OO_Plus: return Plus;
160 case OO_Minus: return Minus;
161 case OO_Tilde: return Not;
162 case OO_Exclaim: return LNot;
Douglas Gregorc78182d2009-03-13 23:49:33 +0000163 }
164}
165
166OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
167 switch (Opc) {
168 case PostInc: case PreInc: return OO_PlusPlus;
169 case PostDec: case PreDec: return OO_MinusMinus;
170 case AddrOf: return OO_Amp;
171 case Deref: return OO_Star;
172 case Plus: return OO_Plus;
173 case Minus: return OO_Minus;
174 case Not: return OO_Tilde;
175 case LNot: return OO_Exclaim;
176 default: return OO_None;
177 }
178}
179
180
Chris Lattner4b009652007-07-25 00:24:17 +0000181//===----------------------------------------------------------------------===//
182// Postfix Operators.
183//===----------------------------------------------------------------------===//
184
Ted Kremenek362abcd2009-02-09 20:51:47 +0000185CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek0c97e042009-02-07 01:47:29 +0000186 unsigned numargs, QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000187 : Expr(SC, t,
188 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000189 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000190 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000191
192 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000193 SubExprs[FN] = fn;
194 for (unsigned i = 0; i != numargs; ++i)
195 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000196
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000197 RParenLoc = rparenloc;
198}
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000199
Ted Kremenek362abcd2009-02-09 20:51:47 +0000200CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
201 QualType t, SourceLocation rparenloc)
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000202 : Expr(CallExprClass, t,
203 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000204 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000205 NumArgs(numargs) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000206
207 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000208 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000209 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000210 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek362abcd2009-02-09 20:51:47 +0000211
Chris Lattner4b009652007-07-25 00:24:17 +0000212 RParenLoc = rparenloc;
213}
214
Argiris Kirtzidisa06a9da2009-07-14 03:19:21 +0000215CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
216 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000217 SubExprs = new (C) Stmt*[1];
218}
219
Douglas Gregor53e8e4b2009-08-07 06:08:38 +0000220void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek362abcd2009-02-09 20:51:47 +0000221 DestroyChildren(C);
222 if (SubExprs) C.Deallocate(SubExprs);
223 this->~CallExpr();
224 C.Deallocate(this);
225}
226
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000227FunctionDecl *CallExpr::getDirectCallee() {
228 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattnereb346742009-07-17 15:46:27 +0000229 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000230 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +0000231
232 return 0;
233}
234
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000235/// setNumArgs - This changes the number of arguments present in this call.
236/// Any orphaned expressions are deleted by this, and any new operands are set
237/// to null.
Ted Kremenek0c97e042009-02-07 01:47:29 +0000238void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000239 // No change, just return.
240 if (NumArgs == getNumArgs()) return;
241
242 // If shrinking # arguments, just delete the extras and forgot them.
243 if (NumArgs < getNumArgs()) {
244 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek0c97e042009-02-07 01:47:29 +0000245 getArg(i)->Destroy(C);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000246 this->NumArgs = NumArgs;
247 return;
248 }
249
250 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar1a6ead22009-07-28 06:29:46 +0000251 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000252 // Copy over args.
253 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
254 NewSubExprs[i] = SubExprs[i];
255 // Null out new args.
256 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
257 NewSubExprs[i] = 0;
258
Douglas Gregorb9f63642009-04-17 21:46:47 +0000259 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000260 SubExprs = NewSubExprs;
261 this->NumArgs = NumArgs;
262}
263
Chris Lattnerc24915f2008-10-06 05:00:53 +0000264/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
265/// not, return 0.
Douglas Gregorb5af7382009-02-14 18:57:46 +0000266unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000267 // All simple function calls (e.g. func()) are implicitly cast to pointer to
268 // function. As a result, we try and obtain the DeclRefExpr from the
269 // ImplicitCastExpr.
270 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
271 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000272 return 0;
273
Steve Naroff44aec4c2008-01-31 01:07:12 +0000274 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
275 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000276 return 0;
277
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000278 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
279 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000280 return 0;
281
Douglas Gregorcf4a8892008-11-21 15:30:19 +0000282 if (!FDecl->getIdentifier())
283 return 0;
284
Douglas Gregorb5af7382009-02-14 18:57:46 +0000285 return FDecl->getBuiltinID(Context);
Chris Lattnerc24915f2008-10-06 05:00:53 +0000286}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000287
Anders Carlsson66070902009-05-26 04:57:27 +0000288QualType CallExpr::getCallReturnType() const {
289 QualType CalleeType = getCallee()->getType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000290 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000291 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000292 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson66070902009-05-26 04:57:27 +0000293 CalleeType = BPT->getPointeeType();
294
295 const FunctionType *FnType = CalleeType->getAsFunctionType();
296 return FnType->getResultType();
297}
Chris Lattnerc24915f2008-10-06 05:00:53 +0000298
Chris Lattner4b009652007-07-25 00:24:17 +0000299/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
300/// corresponds to, e.g. "<<=".
301const char *BinaryOperator::getOpcodeStr(Opcode Op) {
302 switch (Op) {
Douglas Gregor535f3122009-03-12 22:51:37 +0000303 case PtrMemD: return ".*";
304 case PtrMemI: return "->*";
Chris Lattner4b009652007-07-25 00:24:17 +0000305 case Mul: return "*";
306 case Div: return "/";
307 case Rem: return "%";
308 case Add: return "+";
309 case Sub: return "-";
310 case Shl: return "<<";
311 case Shr: return ">>";
312 case LT: return "<";
313 case GT: return ">";
314 case LE: return "<=";
315 case GE: return ">=";
316 case EQ: return "==";
317 case NE: return "!=";
318 case And: return "&";
319 case Xor: return "^";
320 case Or: return "|";
321 case LAnd: return "&&";
322 case LOr: return "||";
323 case Assign: return "=";
324 case MulAssign: return "*=";
325 case DivAssign: return "/=";
326 case RemAssign: return "%=";
327 case AddAssign: return "+=";
328 case SubAssign: return "-=";
329 case ShlAssign: return "<<=";
330 case ShrAssign: return ">>=";
331 case AndAssign: return "&=";
332 case XorAssign: return "^=";
333 case OrAssign: return "|=";
334 case Comma: return ",";
335 }
Douglas Gregor535f3122009-03-12 22:51:37 +0000336
337 return "";
Chris Lattner4b009652007-07-25 00:24:17 +0000338}
339
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000340BinaryOperator::Opcode
341BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
342 switch (OO) {
Chris Lattner6eea6bb2009-03-22 00:10:22 +0000343 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000344 case OO_Plus: return Add;
345 case OO_Minus: return Sub;
346 case OO_Star: return Mul;
347 case OO_Slash: return Div;
348 case OO_Percent: return Rem;
349 case OO_Caret: return Xor;
350 case OO_Amp: return And;
351 case OO_Pipe: return Or;
352 case OO_Equal: return Assign;
353 case OO_Less: return LT;
354 case OO_Greater: return GT;
355 case OO_PlusEqual: return AddAssign;
356 case OO_MinusEqual: return SubAssign;
357 case OO_StarEqual: return MulAssign;
358 case OO_SlashEqual: return DivAssign;
359 case OO_PercentEqual: return RemAssign;
360 case OO_CaretEqual: return XorAssign;
361 case OO_AmpEqual: return AndAssign;
362 case OO_PipeEqual: return OrAssign;
363 case OO_LessLess: return Shl;
364 case OO_GreaterGreater: return Shr;
365 case OO_LessLessEqual: return ShlAssign;
366 case OO_GreaterGreaterEqual: return ShrAssign;
367 case OO_EqualEqual: return EQ;
368 case OO_ExclaimEqual: return NE;
369 case OO_LessEqual: return LE;
370 case OO_GreaterEqual: return GE;
371 case OO_AmpAmp: return LAnd;
372 case OO_PipePipe: return LOr;
373 case OO_Comma: return Comma;
374 case OO_ArrowStar: return PtrMemI;
Douglas Gregor00fe3f62009-03-13 18:40:31 +0000375 }
376}
377
378OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
379 static const OverloadedOperatorKind OverOps[] = {
380 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
381 OO_Star, OO_Slash, OO_Percent,
382 OO_Plus, OO_Minus,
383 OO_LessLess, OO_GreaterGreater,
384 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
385 OO_EqualEqual, OO_ExclaimEqual,
386 OO_Amp,
387 OO_Caret,
388 OO_Pipe,
389 OO_AmpAmp,
390 OO_PipePipe,
391 OO_Equal, OO_StarEqual,
392 OO_SlashEqual, OO_PercentEqual,
393 OO_PlusEqual, OO_MinusEqual,
394 OO_LessLessEqual, OO_GreaterGreaterEqual,
395 OO_AmpEqual, OO_CaretEqual,
396 OO_PipeEqual,
397 OO_Comma
398 };
399 return OverOps[Opc];
400}
401
Anders Carlsson762b7c72007-08-31 04:56:16 +0000402InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner71ca8c82008-10-26 23:43:26 +0000403 Expr **initExprs, unsigned numInits,
Douglas Gregorf603b472009-01-28 21:54:33 +0000404 SourceLocation rbraceloc)
Douglas Gregor3a7a06e2009-05-21 23:17:49 +0000405 : Expr(InitListExprClass, QualType(),
406 hasAnyTypeDependentArguments(initExprs, numInits),
407 hasAnyValueDependentArguments(initExprs, numInits)),
Douglas Gregor82462762009-01-29 16:53:55 +0000408 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor9fddded2009-01-29 19:42:23 +0000409 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner71ca8c82008-10-26 23:43:26 +0000410
411 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000412}
Chris Lattner4b009652007-07-25 00:24:17 +0000413
Douglas Gregoree0792c2009-03-20 23:58:33 +0000414void InitListExpr::reserveInits(unsigned NumInits) {
415 if (NumInits > InitExprs.size())
416 InitExprs.reserve(NumInits);
417}
418
Douglas Gregorf603b472009-01-28 21:54:33 +0000419void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerb6eccdc2009-02-16 22:33:34 +0000420 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar20d4c882009-02-16 22:42:44 +0000421 Idx < LastIdx; ++Idx)
Douglas Gregor78e97132009-03-20 23:38:03 +0000422 InitExprs[Idx]->Destroy(Context);
Douglas Gregorf603b472009-01-28 21:54:33 +0000423 InitExprs.resize(NumInits, 0);
424}
425
426Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
427 if (Init >= InitExprs.size()) {
428 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
429 InitExprs.back() = expr;
430 return 0;
431 }
432
433 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
434 InitExprs[Init] = expr;
435 return Result;
436}
437
Steve Naroff6f373332008-09-04 15:31:07 +0000438/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000439///
440const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000441 return getType()->getAs<BlockPointerType>()->
Steve Naroff52a81c02008-09-03 18:15:37 +0000442 getPointeeType()->getAsFunctionType();
443}
444
Steve Naroff9ac456d2008-10-08 17:01:13 +0000445SourceLocation BlockExpr::getCaretLocation() const {
446 return TheBlock->getCaretLocation();
447}
Douglas Gregore3241e92009-04-18 00:02:19 +0000448const Stmt *BlockExpr::getBody() const {
449 return TheBlock->getBody();
450}
451Stmt *BlockExpr::getBody() {
452 return TheBlock->getBody();
453}
Steve Naroff9ac456d2008-10-08 17:01:13 +0000454
455
Chris Lattner4b009652007-07-25 00:24:17 +0000456//===----------------------------------------------------------------------===//
457// Generic Expression Routines
458//===----------------------------------------------------------------------===//
459
Chris Lattnerd2c66552009-02-14 07:37:35 +0000460/// isUnusedResultAWarning - Return true if this immediate expression should
461/// be warned about if the result is unused. If so, fill in Loc and Ranges
462/// with location to warn on and the source range[s] to report with the
463/// warning.
464bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000465 SourceRange &R2) const {
Anders Carlsson72d3c662009-05-15 23:10:19 +0000466 // Don't warn if the expr is type dependent. The type could end up
467 // instantiating to void.
468 if (isTypeDependent())
469 return false;
470
Chris Lattner4b009652007-07-25 00:24:17 +0000471 switch (getStmtClass()) {
472 default:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000473 Loc = getExprLoc();
474 R1 = getSourceRange();
475 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000476 case ParenExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000477 return cast<ParenExpr>(this)->getSubExpr()->
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000478 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000479 case UnaryOperatorClass: {
480 const UnaryOperator *UO = cast<UnaryOperator>(this);
481
482 switch (UO->getOpcode()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000483 default: break;
Chris Lattner4b009652007-07-25 00:24:17 +0000484 case UnaryOperator::PostInc:
485 case UnaryOperator::PostDec:
486 case UnaryOperator::PreInc:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000487 case UnaryOperator::PreDec: // ++/--
488 return false; // Not a warning.
Chris Lattner4b009652007-07-25 00:24:17 +0000489 case UnaryOperator::Deref:
490 // Dereferencing a volatile pointer is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000491 if (getType().isVolatileQualified())
492 return false;
493 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000494 case UnaryOperator::Real:
495 case UnaryOperator::Imag:
496 // accessing a piece of a volatile complex is a side-effect.
Chris Lattnerd2c66552009-02-14 07:37:35 +0000497 if (UO->getSubExpr()->getType().isVolatileQualified())
498 return false;
499 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000500 case UnaryOperator::Extension:
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000501 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner4b009652007-07-25 00:24:17 +0000502 }
Chris Lattnerd2c66552009-02-14 07:37:35 +0000503 Loc = UO->getOperatorLoc();
504 R1 = UO->getSubExpr()->getSourceRange();
505 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000506 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000507 case BinaryOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000508 const BinaryOperator *BO = cast<BinaryOperator>(this);
509 // Consider comma to have side effects if the LHS or RHS does.
510 if (BO->getOpcode() == BinaryOperator::Comma)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000511 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
512 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattneref95ffd2007-12-01 06:07:34 +0000513
Chris Lattnerd2c66552009-02-14 07:37:35 +0000514 if (BO->isAssignmentOp())
515 return false;
516 Loc = BO->getOperatorLoc();
517 R1 = BO->getLHS()->getSourceRange();
518 R2 = BO->getRHS()->getSourceRange();
519 return true;
Chris Lattneref95ffd2007-12-01 06:07:34 +0000520 }
Chris Lattner06078d22007-08-25 02:00:02 +0000521 case CompoundAssignOperatorClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000522 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000523
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000524 case ConditionalOperatorClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000525 // The condition must be evaluated, but if either the LHS or RHS is a
526 // warning, warn about them.
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000527 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000528 if (Exp->getLHS() &&
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000529 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattnerd2c66552009-02-14 07:37:35 +0000530 return true;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000531 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000532 }
533
Chris Lattner4b009652007-07-25 00:24:17 +0000534 case MemberExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000535 // If the base pointer or element is to a volatile pointer/field, accessing
536 // it is a side effect.
537 if (getType().isVolatileQualified())
538 return false;
539 Loc = cast<MemberExpr>(this)->getMemberLoc();
540 R1 = SourceRange(Loc, Loc);
541 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
542 return true;
543
Chris Lattner4b009652007-07-25 00:24:17 +0000544 case ArraySubscriptExprClass:
545 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattnerd2c66552009-02-14 07:37:35 +0000546 // it is a side effect.
547 if (getType().isVolatileQualified())
548 return false;
549 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
550 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
551 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
552 return true;
Eli Friedman21fd0292008-05-27 15:24:04 +0000553
Chris Lattner4b009652007-07-25 00:24:17 +0000554 case CallExprClass:
Eli Friedmane2846cf2009-04-29 16:35:53 +0000555 case CXXOperatorCallExprClass:
556 case CXXMemberCallExprClass: {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000557 // If this is a direct call, get the callee.
558 const CallExpr *CE = cast<CallExpr>(this);
559 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
560 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
561 // If the callee has attribute pure, const, or warn_unused_result, warn
562 // about it. void foo() { strlen("bar"); } should warn.
563 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000564 if (FD->getAttr<WarnUnusedResultAttr>() ||
565 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattnerd2c66552009-02-14 07:37:35 +0000566 Loc = CE->getCallee()->getLocStart();
567 R1 = CE->getCallee()->getSourceRange();
568
569 if (unsigned NumArgs = CE->getNumArgs())
570 R2 = SourceRange(CE->getArg(0)->getLocStart(),
571 CE->getArg(NumArgs-1)->getLocEnd());
572 return true;
573 }
574 }
575 return false;
576 }
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000577 case ObjCMessageExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000578 return false;
Chris Lattner200964f2008-07-26 19:51:01 +0000579 case StmtExprClass: {
580 // Statement exprs don't logically have side effects themselves, but are
581 // sometimes used in macros in ways that give them a type that is unused.
582 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
583 // however, if the result of the stmt expr is dead, we don't want to emit a
584 // warning.
585 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
586 if (!CS->body_empty())
587 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000588 return E->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000589
590 Loc = cast<StmtExpr>(this)->getLParenLoc();
591 R1 = getSourceRange();
592 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000593 }
Douglas Gregor035d0882008-10-28 15:36:24 +0000594 case CStyleCastExprClass:
Chris Lattner34796b62009-07-28 18:25:28 +0000595 // If this is an explicit cast to void, allow it. People do this when they
596 // think they know what they're doing :).
Chris Lattnerd2c66552009-02-14 07:37:35 +0000597 if (getType()->isVoidType())
Chris Lattner34796b62009-07-28 18:25:28 +0000598 return false;
Chris Lattnerd2c66552009-02-14 07:37:35 +0000599 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
600 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
601 return true;
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000602 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000603 // If this is a cast to void, check the operand. Otherwise, the result of
604 // the cast is unused.
605 if (getType()->isVoidType())
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000606 return cast<CastExpr>(this)->getSubExpr()
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000607 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattnerd2c66552009-02-14 07:37:35 +0000608 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
609 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
610 return true;
611
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000612 case ImplicitCastExprClass:
613 // Check the operand, since implicit casts are inserted by Sema
Chris Lattnerd2c66552009-02-14 07:37:35 +0000614 return cast<ImplicitCastExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000615 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000616
Chris Lattner3e254fb2008-04-08 04:40:51 +0000617 case CXXDefaultArgExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000618 return cast<CXXDefaultArgExpr>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000619 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000620
621 case CXXNewExprClass:
622 // FIXME: In theory, there might be new expressions that don't have side
623 // effects (e.g. a placement new with an uninitialized POD).
624 case CXXDeleteExprClass:
Chris Lattnerd2c66552009-02-14 07:37:35 +0000625 return false;
Anders Carlsson18ca4772009-05-17 21:11:30 +0000626 case CXXExprWithTemporariesClass:
627 return cast<CXXExprWithTemporaries>(this)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000628 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000629 }
Chris Lattner4b009652007-07-25 00:24:17 +0000630}
631
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000632/// DeclCanBeLvalue - Determine whether the given declaration can be
633/// an lvalue. This is a helper routine for isLvalue.
634static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregordd861062008-12-05 18:15:24 +0000635 // C++ [temp.param]p6:
636 // A non-type non-reference template-parameter is not an lvalue.
637 if (const NonTypeTemplateParmDecl *NTTParm
638 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
639 return NTTParm->getType()->isReferenceType();
640
Douglas Gregor8acb7272008-12-11 16:49:14 +0000641 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000642 // C++ 3.10p2: An lvalue refers to an object or function.
643 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor62f78762009-07-08 20:55:45 +0000644 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
645 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000646}
647
Chris Lattner4b009652007-07-25 00:24:17 +0000648/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
649/// incomplete type other than void. Nonarray expressions that can be lvalues:
650/// - name, where name must be a variable
651/// - e[i]
652/// - (e), where e must be an lvalue
653/// - e.name, where e must be an lvalue
654/// - e->name
655/// - *e, the type of e cannot be a function type
656/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000657/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000658/// - reference type [C++ [expr]]
659///
Chris Lattner25168a52008-07-26 21:30:36 +0000660Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000661 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
662
663 isLvalueResult Res = isLvalueInternal(Ctx);
664 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
665 return Res;
666
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000667 // first, check the type (C99 6.3.2.1). Expressions with function
668 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor62f78762009-07-08 20:55:45 +0000669 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Chris Lattner4b009652007-07-25 00:24:17 +0000670 return LV_NotObjectType;
671
Steve Naroffec7736d2008-02-10 01:39:04 +0000672 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000673 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000674 return LV_IncompleteVoidType;
675
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000676 return LV_Valid;
677}
Chris Lattner4b009652007-07-25 00:24:17 +0000678
Eli Friedmane7e4dac2009-05-03 22:36:05 +0000679// Check whether the expression can be sanely treated like an l-value
680Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000681 switch (getStmtClass()) {
Chris Lattnerc5d32632009-02-24 22:18:39 +0000682 case StringLiteralClass: // C99 6.5.1p4
683 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson9e933a22007-11-30 22:47:59 +0000684 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000685 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
686 // For vectors, make sure base is an lvalue (i.e. not a function call).
687 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000688 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000689 return LV_Valid;
Douglas Gregor566782a2009-01-06 05:10:23 +0000690 case DeclRefExprClass:
691 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4459bbe2008-10-22 15:04:37 +0000692 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
693 if (DeclCanBeLvalue(RefdDecl, Ctx))
Chris Lattner4b009652007-07-25 00:24:17 +0000694 return LV_Valid;
695 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000696 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000697 case BlockDeclRefExprClass: {
698 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000699 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000700 return LV_Valid;
701 break;
702 }
Douglas Gregor82d44772008-12-20 23:49:58 +0000703 case MemberExprClass: {
Chris Lattner4b009652007-07-25 00:24:17 +0000704 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor82d44772008-12-20 23:49:58 +0000705 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
706 NamedDecl *Member = m->getMemberDecl();
707 // C++ [expr.ref]p4:
708 // If E2 is declared to have type "reference to T", then E1.E2
709 // is an lvalue.
710 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
711 if (Value->getType()->isReferenceType())
712 return LV_Valid;
713
714 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor00660582009-03-11 20:22:50 +0000715 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor82d44772008-12-20 23:49:58 +0000716 return LV_Valid;
717
718 // -- If E2 is a non-static data member [...]. If E1 is an
719 // lvalue, then E1.E2 is an lvalue.
720 if (isa<FieldDecl>(Member))
721 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
722
723 // -- If it refers to a static member function [...], then
724 // E1.E2 is an lvalue.
725 // -- Otherwise, if E1.E2 refers to a non-static member
726 // function [...], then E1.E2 is not an lvalue.
727 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
728 return Method->isStatic()? LV_Valid : LV_MemberFunction;
729
730 // -- If E2 is a member enumerator [...], the expression E1.E2
731 // is not an lvalue.
732 if (isa<EnumConstantDecl>(Member))
733 return LV_InvalidExpression;
734
735 // Not an lvalue.
736 return LV_InvalidExpression;
737 }
738
739 // C99 6.5.2.3p4
Chris Lattner25168a52008-07-26 21:30:36 +0000740 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000741 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000742 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000743 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000744 return LV_Valid; // C99 6.5.3p4
745
746 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000747 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
748 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000749 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor4f6904d2008-11-19 15:42:04 +0000750
751 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
752 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
753 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
754 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000755 break;
Douglas Gregor70d26122008-11-12 17:17:38 +0000756 case ImplicitCastExprClass:
757 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
758 : LV_InvalidExpression;
Chris Lattner4b009652007-07-25 00:24:17 +0000759 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000760 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregor70d26122008-11-12 17:17:38 +0000761 case BinaryOperatorClass:
762 case CompoundAssignOperatorClass: {
763 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor80723c52008-11-19 17:17:41 +0000764
765 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
766 BinOp->getOpcode() == BinaryOperator::Comma)
767 return BinOp->getRHS()->isLvalue(Ctx);
768
Sebastian Redl95216a62009-02-07 00:15:38 +0000769 // C++ [expr.mptr.oper]p6
770 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
771 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
772 !BinOp->getType()->isFunctionType())
773 return BinOp->getLHS()->isLvalue(Ctx);
774
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000775 if (!BinOp->isAssignmentOp())
Douglas Gregor70d26122008-11-12 17:17:38 +0000776 return LV_InvalidExpression;
777
Douglas Gregor3d4492e2008-11-13 20:12:29 +0000778 if (Ctx.getLangOptions().CPlusPlus)
779 // C++ [expr.ass]p1:
780 // The result of an assignment operation [...] is an lvalue.
781 return LV_Valid;
782
783
784 // C99 6.5.16:
785 // An assignment expression [...] is not an lvalue.
786 return LV_InvalidExpression;
Douglas Gregor70d26122008-11-12 17:17:38 +0000787 }
Douglas Gregor65fedaf2008-11-14 16:09:21 +0000788 case CallExprClass:
Douglas Gregor3257fb52008-12-22 05:46:06 +0000789 case CXXOperatorCallExprClass:
790 case CXXMemberCallExprClass: {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000791 // C++0x [expr.call]p10
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000792 // A function call is an lvalue if and only if the result type
Sebastian Redlce6fff02009-03-16 23:22:08 +0000793 // is an lvalue reference.
Anders Carlsson66070902009-05-26 04:57:27 +0000794 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
795 if (ReturnType->isLValueReferenceType())
796 return LV_Valid;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000797
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000798 break;
799 }
Steve Naroffc7c66532007-12-05 04:00:10 +0000800 case CompoundLiteralExprClass: // C99 6.5.2.5p5
801 return LV_Valid;
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000802 case ChooseExprClass:
803 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmand540c112009-03-04 05:52:32 +0000804 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000805 case ExtVectorElementExprClass:
806 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000807 return LV_DuplicateVectorComponents;
808 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000809 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
810 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000811 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
812 return LV_Valid;
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +0000813 case ObjCKVCRefExprClass: // FIXME: check if read-only property.
Chris Lattnera5f779dc2008-12-12 05:35:08 +0000814 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000815 case PredefinedExprClass:
Douglas Gregora5b022a2008-11-04 14:32:21 +0000816 return LV_Valid;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000817 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000818 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000819 case CXXConditionDeclExprClass:
820 return LV_Valid;
Douglas Gregor035d0882008-10-28 15:36:24 +0000821 case CStyleCastExprClass:
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000822 case CXXFunctionalCastExprClass:
823 case CXXStaticCastExprClass:
824 case CXXDynamicCastExprClass:
825 case CXXReinterpretCastExprClass:
826 case CXXConstCastExprClass:
827 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redlce6fff02009-03-16 23:22:08 +0000828 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000829 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
830 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000831 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
832 isLValueReferenceType())
Douglas Gregor0d5d89d2008-10-28 00:22:11 +0000833 return LV_Valid;
834 break;
Sebastian Redlb93b49c2008-11-11 11:37:55 +0000835 case CXXTypeidExprClass:
836 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
837 return LV_Valid;
Sebastian Redld3169132009-04-17 16:30:52 +0000838 case ConditionalOperatorClass: {
839 // Complicated handling is only for C++.
840 if (!Ctx.getLangOptions().CPlusPlus)
841 return LV_InvalidExpression;
842
843 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
844 // everywhere there's an object converted to an rvalue. Also, any other
845 // casts should be wrapped by ImplicitCastExprs. There's just the special
846 // case involving throws to work out.
847 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor0816e822009-05-19 20:13:50 +0000848 Expr *True = Cond->getTrueExpr();
849 Expr *False = Cond->getFalseExpr();
Sebastian Redld3169132009-04-17 16:30:52 +0000850 // C++0x 5.16p2
851 // If either the second or the third operand has type (cv) void, [...]
852 // the result [...] is an rvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000853 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redld3169132009-04-17 16:30:52 +0000854 return LV_InvalidExpression;
855
856 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor0816e822009-05-19 20:13:50 +0000857 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redld3169132009-04-17 16:30:52 +0000858 return LV_InvalidExpression;
859
860 // That's it.
861 return LV_Valid;
862 }
863
Chris Lattner4b009652007-07-25 00:24:17 +0000864 default:
865 break;
866 }
867 return LV_InvalidExpression;
868}
869
870/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
871/// does not have an incomplete type, does not have a const-qualified type, and
872/// if it is a structure or union, does not have any member (including,
873/// recursively, any member or element of all contained aggregates or unions)
874/// with a const-qualified type.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000875Expr::isModifiableLvalueResult
876Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner25168a52008-07-26 21:30:36 +0000877 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000878
879 switch (lvalResult) {
Douglas Gregor26a4c5f2008-10-22 00:03:08 +0000880 case LV_Valid:
881 // C++ 3.10p11: Functions cannot be modified, but pointers to
882 // functions can be modifiable.
883 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
884 return MLV_NotObjectType;
885 break;
886
Chris Lattner4b009652007-07-25 00:24:17 +0000887 case LV_NotObjectType: return MLV_NotObjectType;
888 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000889 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner37fb9402008-11-17 19:51:54 +0000890 case LV_InvalidExpression:
891 // If the top level is a C-style cast, and the subexpression is a valid
892 // lvalue, then this is probably a use of the old-school "cast as lvalue"
893 // GCC extension. We don't support it, but we want to produce good
894 // diagnostics when it happens so that the user knows why.
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000895 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
896 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
897 if (Loc)
898 *Loc = CE->getLParenLoc();
Chris Lattner37fb9402008-11-17 19:51:54 +0000899 return MLV_LValueCast;
Daniel Dunbarf7fa9dc2009-04-15 00:08:05 +0000900 }
901 }
Chris Lattner37fb9402008-11-17 19:51:54 +0000902 return MLV_InvalidExpression;
Douglas Gregor82d44772008-12-20 23:49:58 +0000903 case LV_MemberFunction: return MLV_MemberFunction;
Chris Lattner4b009652007-07-25 00:24:17 +0000904 }
Eli Friedman91571da2009-03-22 23:26:56 +0000905
906 // The following is illegal:
907 // void takeclosure(void (^C)(void));
908 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
909 //
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000910 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedman91571da2009-03-22 23:26:56 +0000911 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
912 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
913 return MLV_NotBlockQualified;
914 }
915
Chris Lattnera1923f62008-08-04 07:31:14 +0000916 QualType CT = Ctx.getCanonicalType(getType());
917
918 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000919 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000920 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000921 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000922 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000923 return MLV_IncompleteType;
924
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000925 if (const RecordType *r = CT->getAs<RecordType>()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000926 if (r->hasConstFields())
927 return MLV_ConstQualified;
928 }
Fariborz Jahanianf96ee9e2009-01-12 19:55:42 +0000929
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000930 // Assigning to an 'implicit' property?
Chris Lattnerfb52eba2009-03-23 17:57:53 +0000931 else if (isa<ObjCKVCRefExpr>(this)) {
Fariborz Jahanianc05da422008-11-22 20:25:50 +0000932 const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this);
933 if (KVCExpr->getSetterMethod() == 0)
934 return MLV_NoSetterProperty;
935 }
Chris Lattner4b009652007-07-25 00:24:17 +0000936 return MLV_Valid;
937}
938
Ted Kremenek5778d622008-02-27 18:39:48 +0000939/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000940/// duration. This means that the address of this expression is a link-time
941/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000942bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000943 switch (getStmtClass()) {
944 default:
945 return false;
Steve Naroff7ceff372009-04-16 19:02:57 +0000946 case BlockExprClass:
947 return true;
Chris Lattner743ec372007-11-27 21:35:27 +0000948 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000949 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000950 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000951 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000952 case CompoundLiteralExprClass:
953 return cast<CompoundLiteralExpr>(this)->isFileScope();
Douglas Gregor566782a2009-01-06 05:10:23 +0000954 case DeclRefExprClass:
955 case QualifiedDeclRefExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000956 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
957 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000958 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000959 if (isa<FunctionDecl>(D))
960 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000961 return false;
962 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000963 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000964 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000965 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000966 }
Chris Lattner743ec372007-11-27 21:35:27 +0000967 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000968 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000969 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000970 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000971 case CXXDefaultArgExprClass:
972 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000973 }
974}
975
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000976/// isOBJCGCCandidate - Check if an expression is objc gc'able.
977///
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000978bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000979 switch (getStmtClass()) {
980 default:
981 return false;
982 case ObjCIvarRefExprClass:
983 return true;
Fariborz Jahanian2224fb22009-02-23 18:59:50 +0000984 case Expr::UnaryOperatorClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000985 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000986 case ParenExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000987 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000988 case ImplicitCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000989 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian2b6d9ed2009-05-05 23:28:21 +0000990 case CStyleCastExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000991 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +0000992 case DeclRefExprClass:
993 case QualifiedDeclRefExprClass: {
994 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian0ceca872009-06-01 21:29:32 +0000995 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
996 if (VD->hasGlobalStorage())
997 return true;
998 QualType T = VD->getType();
999 // dereferencing to an object pointer is always a gc'able candidate
1000 if (T->isPointerType() &&
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001001 T->getAs<PointerType>()->getPointeeType()->isObjCObjectPointerType())
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001002 return true;
1003
1004 }
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001005 return false;
1006 }
1007 case MemberExprClass: {
1008 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001009 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001010 }
1011 case ArraySubscriptExprClass:
Fariborz Jahanian0ceca872009-06-01 21:29:32 +00001012 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian0c195b92009-02-22 18:40:18 +00001013 }
1014}
Ted Kremenek87e30c52008-01-17 16:57:34 +00001015Expr* Expr::IgnoreParens() {
1016 Expr* E = this;
1017 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1018 E = P->getSubExpr();
1019
1020 return E;
1021}
1022
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001023/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1024/// or CastExprs or ImplicitCastExprs, returning their operand.
1025Expr *Expr::IgnoreParenCasts() {
1026 Expr *E = this;
1027 while (true) {
1028 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1029 E = P->getSubExpr();
1030 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1031 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +00001032 else
1033 return E;
1034 }
1035}
1036
Chris Lattnerab0b8b12009-03-13 17:28:01 +00001037/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1038/// value (including ptr->int casts of the same size). Strip off any
1039/// ParenExpr or CastExprs, returning their operand.
1040Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1041 Expr *E = this;
1042 while (true) {
1043 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1044 E = P->getSubExpr();
1045 continue;
1046 }
1047
1048 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1049 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1050 // ptr<->int casts of the same width. We also ignore all identify casts.
1051 Expr *SE = P->getSubExpr();
1052
1053 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1054 E = SE;
1055 continue;
1056 }
1057
1058 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1059 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1060 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1061 E = SE;
1062 continue;
1063 }
1064 }
1065
1066 return E;
1067 }
1068}
1069
1070
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001071/// hasAnyTypeDependentArguments - Determines if any of the expressions
1072/// in Exprs is type-dependent.
1073bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1074 for (unsigned I = 0; I < NumExprs; ++I)
1075 if (Exprs[I]->isTypeDependent())
1076 return true;
1077
1078 return false;
1079}
1080
1081/// hasAnyValueDependentArguments - Determines if any of the expressions
1082/// in Exprs is value-dependent.
1083bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1084 for (unsigned I = 0; I < NumExprs; ++I)
1085 if (Exprs[I]->isValueDependent())
1086 return true;
1087
1088 return false;
1089}
1090
Eli Friedmandee41122009-01-25 02:32:41 +00001091bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001092 // This function is attempting whether an expression is an initializer
1093 // which can be evaluated at compile-time. isEvaluatable handles most
1094 // of the cases, but it can't deal with some initializer-specific
1095 // expressions, and it can't deal with aggregates; we deal with those here,
1096 // and fall back to isEvaluatable for the other cases.
1097
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001098 // FIXME: This function assumes the variable being assigned to
1099 // isn't a reference type!
1100
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001101 switch (getStmtClass()) {
Eli Friedman2b0dec52009-01-25 03:12:18 +00001102 default: break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001103 case StringLiteralClass:
Steve Naroff329ec222009-07-10 23:34:53 +00001104 case ObjCStringLiteralClass:
Chris Lattnerc5d32632009-02-24 22:18:39 +00001105 case ObjCEncodeExprClass:
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001106 return true;
Nate Begemand6d2f772009-01-18 03:20:47 +00001107 case CompoundLiteralExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001108 // This handles gcc's extension that allows global initializers like
1109 // "struct x {int x;} x = (struct x) {};".
1110 // FIXME: This accepts other cases it shouldn't!
Nate Begemand6d2f772009-01-18 03:20:47 +00001111 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmandee41122009-01-25 02:32:41 +00001112 return Exp->isConstantInitializer(Ctx);
Nate Begemand6d2f772009-01-18 03:20:47 +00001113 }
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001114 case InitListExprClass: {
Eli Friedman3dc50ed2009-02-20 02:36:22 +00001115 // FIXME: This doesn't deal with fields with reference types correctly.
1116 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1117 // to bitfields.
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001118 const InitListExpr *Exp = cast<InitListExpr>(this);
1119 unsigned numInits = Exp->getNumInits();
1120 for (unsigned i = 0; i < numInits; i++) {
Eli Friedmandee41122009-01-25 02:32:41 +00001121 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001122 return false;
1123 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001124 return true;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001125 }
Douglas Gregorc9e012a2009-01-29 17:44:32 +00001126 case ImplicitValueInitExprClass:
1127 return true;
Eli Friedman2b0dec52009-01-25 03:12:18 +00001128 case ParenExprClass: {
1129 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1130 }
1131 case UnaryOperatorClass: {
1132 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1133 if (Exp->getOpcode() == UnaryOperator::Extension)
1134 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1135 break;
1136 }
Chris Lattner3e0eaf82009-04-21 05:19:11 +00001137 case ImplicitCastExprClass:
Eli Friedman2b0dec52009-01-25 03:12:18 +00001138 case CStyleCastExprClass:
1139 // Handle casts with a destination that's a struct or union; this
1140 // deals with both the gcc no-op struct cast extension and the
1141 // cast-to-union extension.
1142 if (getType()->isRecordType())
1143 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1144 break;
Anders Carlssona7fa2aa2008-11-24 05:23:59 +00001145 }
Eli Friedman2b0dec52009-01-25 03:12:18 +00001146 return isEvaluatable(Ctx);
Steve Naroff7c9d72d2007-09-02 20:30:18 +00001147}
1148
Chris Lattner4b009652007-07-25 00:24:17 +00001149/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman7beeda62009-02-26 09:29:13 +00001150/// an integer constant expression.
Chris Lattner4b009652007-07-25 00:24:17 +00001151
1152/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1153/// comma, etc
1154///
Chris Lattner4b009652007-07-25 00:24:17 +00001155/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1156/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1157/// cast+dereference.
Daniel Dunbar168b20c2009-02-18 00:47:45 +00001158
Eli Friedman7beeda62009-02-26 09:29:13 +00001159// CheckICE - This function does the fundamental ICE checking: the returned
1160// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1161// Note that to reduce code duplication, this helper does no evaluation
1162// itself; the caller checks whether the expression is evaluatable, and
1163// in the rare cases where CheckICE actually cares about the evaluated
1164// value, it calls into Evalute.
1165//
1166// Meanings of Val:
1167// 0: This expression is an ICE if it can be evaluated by Evaluate.
1168// 1: This expression is not an ICE, but if it isn't evaluated, it's
1169// a legal subexpression for an ICE. This return value is used to handle
1170// the comma operator in C99 mode.
1171// 2: This expression is not an ICE, and is not a legal subexpression for one.
1172
1173struct ICEDiag {
1174 unsigned Val;
1175 SourceLocation Loc;
1176
1177 public:
1178 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1179 ICEDiag() : Val(0) {}
1180};
1181
1182ICEDiag NoDiag() { return ICEDiag(); }
1183
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001184static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1185 Expr::EvalResult EVResult;
1186 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1187 !EVResult.Val.isInt()) {
1188 return ICEDiag(2, E->getLocStart());
1189 }
1190 return NoDiag();
1191}
1192
Eli Friedman7beeda62009-02-26 09:29:13 +00001193static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson8b842c52009-03-14 00:33:21 +00001194 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001195 if (!E->getType()->isIntegralType()) {
1196 return ICEDiag(2, E->getLocStart());
Eli Friedman14cc7542008-11-13 06:09:17 +00001197 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001198
1199 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001200 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001201 return ICEDiag(2, E->getLocStart());
1202 case Expr::ParenExprClass:
1203 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1204 case Expr::IntegerLiteralClass:
1205 case Expr::CharacterLiteralClass:
1206 case Expr::CXXBoolLiteralExprClass:
1207 case Expr::CXXZeroInitValueExprClass:
1208 case Expr::TypesCompatibleExprClass:
1209 case Expr::UnaryTypeTraitExprClass:
1210 return NoDiag();
1211 case Expr::CallExprClass:
1212 case Expr::CXXOperatorCallExprClass: {
1213 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001214 if (CE->isBuiltinCall(Ctx))
1215 return CheckEvalInICE(E, Ctx);
Eli Friedman7beeda62009-02-26 09:29:13 +00001216 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001217 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001218 case Expr::DeclRefExprClass:
1219 case Expr::QualifiedDeclRefExprClass:
1220 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1221 return NoDiag();
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001222 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman7beeda62009-02-26 09:29:13 +00001223 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001224 // C++ 7.1.5.1p2
1225 // A variable of non-volatile const-qualified integral or enumeration
1226 // type initialized by an ICE can be used in ICEs.
1227 if (const VarDecl *Dcl =
Eli Friedman7beeda62009-02-26 09:29:13 +00001228 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor4833ff02009-05-26 18:54:04 +00001229 if (Dcl->isInitKnownICE()) {
1230 // We have already checked whether this subexpression is an
1231 // integral constant expression.
1232 if (Dcl->isInitICE())
1233 return NoDiag();
1234 else
1235 return ICEDiag(2, E->getLocStart());
1236 }
1237
1238 if (const Expr *Init = Dcl->getInit()) {
1239 ICEDiag Result = CheckICE(Init, Ctx);
1240 // Cache the result of the ICE test.
1241 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1242 return Result;
1243 }
Sebastian Redl57ef46a2009-02-07 13:06:23 +00001244 }
1245 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001246 return ICEDiag(2, E->getLocStart());
1247 case Expr::UnaryOperatorClass: {
1248 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001249 switch (Exp->getOpcode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001250 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001251 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001252 case UnaryOperator::Extension:
Eli Friedman7beeda62009-02-26 09:29:13 +00001253 case UnaryOperator::LNot:
Chris Lattner4b009652007-07-25 00:24:17 +00001254 case UnaryOperator::Plus:
Chris Lattner4b009652007-07-25 00:24:17 +00001255 case UnaryOperator::Minus:
Chris Lattner4b009652007-07-25 00:24:17 +00001256 case UnaryOperator::Not:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001257 case UnaryOperator::Real:
1258 case UnaryOperator::Imag:
Eli Friedman7beeda62009-02-26 09:29:13 +00001259 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001260 case UnaryOperator::OffsetOf:
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001261 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1262 // Evaluate matches the proposed gcc behavior for cases like
1263 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1264 // compliance: we should warn earlier for offsetof expressions with
1265 // array subscripts that aren't ICEs, and if the array subscripts
1266 // are ICEs, the value of the offsetof must be an integer constant.
1267 return CheckEvalInICE(E, Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001268 }
Chris Lattner4b009652007-07-25 00:24:17 +00001269 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001270 case Expr::SizeOfAlignOfExprClass: {
1271 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1272 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1273 return ICEDiag(2, E->getLocStart());
1274 return NoDiag();
Chris Lattner4b009652007-07-25 00:24:17 +00001275 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001276 case Expr::BinaryOperatorClass: {
1277 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattner4b009652007-07-25 00:24:17 +00001278 switch (Exp->getOpcode()) {
1279 default:
Eli Friedman7beeda62009-02-26 09:29:13 +00001280 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001281 case BinaryOperator::Mul:
Chris Lattner4b009652007-07-25 00:24:17 +00001282 case BinaryOperator::Div:
Chris Lattner4b009652007-07-25 00:24:17 +00001283 case BinaryOperator::Rem:
Eli Friedman7beeda62009-02-26 09:29:13 +00001284 case BinaryOperator::Add:
1285 case BinaryOperator::Sub:
Chris Lattner4b009652007-07-25 00:24:17 +00001286 case BinaryOperator::Shl:
Chris Lattner4b009652007-07-25 00:24:17 +00001287 case BinaryOperator::Shr:
Eli Friedman7beeda62009-02-26 09:29:13 +00001288 case BinaryOperator::LT:
1289 case BinaryOperator::GT:
1290 case BinaryOperator::LE:
1291 case BinaryOperator::GE:
1292 case BinaryOperator::EQ:
1293 case BinaryOperator::NE:
1294 case BinaryOperator::And:
1295 case BinaryOperator::Xor:
1296 case BinaryOperator::Or:
1297 case BinaryOperator::Comma: {
1298 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1299 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001300 if (Exp->getOpcode() == BinaryOperator::Div ||
1301 Exp->getOpcode() == BinaryOperator::Rem) {
1302 // Evaluate gives an error for undefined Div/Rem, so make sure
1303 // we don't evaluate one.
1304 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1305 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1306 if (REval == 0)
1307 return ICEDiag(1, E->getLocStart());
1308 if (REval.isSigned() && REval.isAllOnesValue()) {
1309 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1310 if (LEval.isMinSignedValue())
1311 return ICEDiag(1, E->getLocStart());
1312 }
1313 }
1314 }
1315 if (Exp->getOpcode() == BinaryOperator::Comma) {
1316 if (Ctx.getLangOptions().C99) {
1317 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1318 // if it isn't evaluated.
1319 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1320 return ICEDiag(1, E->getLocStart());
1321 } else {
1322 // In both C89 and C++, commas in ICEs are illegal.
1323 return ICEDiag(2, E->getLocStart());
1324 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001325 }
1326 if (LHSResult.Val >= RHSResult.Val)
1327 return LHSResult;
1328 return RHSResult;
1329 }
Chris Lattner4b009652007-07-25 00:24:17 +00001330 case BinaryOperator::LAnd:
Eli Friedman7beeda62009-02-26 09:29:13 +00001331 case BinaryOperator::LOr: {
1332 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1333 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1334 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1335 // Rare case where the RHS has a comma "side-effect"; we need
1336 // to actually check the condition to see whether the side
1337 // with the comma is evaluated.
Eli Friedman7beeda62009-02-26 09:29:13 +00001338 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001339 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman7beeda62009-02-26 09:29:13 +00001340 return RHSResult;
1341 return NoDiag();
Eli Friedmanb2935ab2008-11-13 02:13:11 +00001342 }
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001343
Eli Friedman7beeda62009-02-26 09:29:13 +00001344 if (LHSResult.Val >= RHSResult.Val)
1345 return LHSResult;
1346 return RHSResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001347 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001348 }
Chris Lattner4b009652007-07-25 00:24:17 +00001349 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001350 case Expr::ImplicitCastExprClass:
1351 case Expr::CStyleCastExprClass:
1352 case Expr::CXXFunctionalCastExprClass: {
1353 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1354 if (SubExpr->getType()->isIntegralType())
1355 return CheckICE(SubExpr, Ctx);
1356 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1357 return NoDiag();
1358 return ICEDiag(2, E->getLocStart());
Chris Lattner4b009652007-07-25 00:24:17 +00001359 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001360 case Expr::ConditionalOperatorClass: {
1361 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Chris Lattner45e71bf2008-12-12 06:55:44 +00001362 // If the condition (ignoring parens) is a __builtin_constant_p call,
1363 // then only the true side is actually considered in an integer constant
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001364 // expression, and it is fully evaluated. This is an important GNU
1365 // extension. See GCC PR38377 for discussion.
Eli Friedman7beeda62009-02-26 09:29:13 +00001366 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregorb5af7382009-02-14 18:57:46 +00001367 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001368 Expr::EvalResult EVResult;
1369 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1370 !EVResult.Val.isInt()) {
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001371 return ICEDiag(2, E->getLocStart());
Eli Friedman7beeda62009-02-26 09:29:13 +00001372 }
1373 return NoDiag();
Chris Lattner2a6a5b32008-12-12 18:00:51 +00001374 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001375 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1376 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1377 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1378 if (CondResult.Val == 2)
1379 return CondResult;
1380 if (TrueResult.Val == 2)
1381 return TrueResult;
1382 if (FalseResult.Val == 2)
1383 return FalseResult;
1384 if (CondResult.Val == 1)
1385 return CondResult;
1386 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1387 return NoDiag();
1388 // Rare case where the diagnostics depend on which side is evaluated
1389 // Note that if we get here, CondResult is 0, and at least one of
1390 // TrueResult and FalseResult is non-zero.
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001391 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman7beeda62009-02-26 09:29:13 +00001392 return FalseResult;
1393 }
1394 return TrueResult;
Chris Lattner4b009652007-07-25 00:24:17 +00001395 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001396 case Expr::CXXDefaultArgExprClass:
1397 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001398 case Expr::ChooseExprClass: {
Eli Friedmand540c112009-03-04 05:52:32 +00001399 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001400 }
Chris Lattner4b009652007-07-25 00:24:17 +00001401 }
Eli Friedman7beeda62009-02-26 09:29:13 +00001402}
Chris Lattner4b009652007-07-25 00:24:17 +00001403
Eli Friedman7beeda62009-02-26 09:29:13 +00001404bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1405 SourceLocation *Loc, bool isEvaluated) const {
1406 ICEDiag d = CheckICE(this, Ctx);
1407 if (d.Val != 0) {
1408 if (Loc) *Loc = d.Loc;
1409 return false;
1410 }
1411 EvalResult EvalResult;
Eli Friedmand65bbbc2009-02-27 04:07:58 +00001412 if (!Evaluate(EvalResult, Ctx))
1413 assert(0 && "ICE cannot be evaluated!");
1414 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1415 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman7beeda62009-02-26 09:29:13 +00001416 Result = EvalResult.Val.getInt();
Chris Lattner4b009652007-07-25 00:24:17 +00001417 return true;
1418}
1419
Chris Lattner4b009652007-07-25 00:24:17 +00001420/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1421/// integer constant expression with the value zero, or if this is one that is
1422/// cast to void*.
Anders Carlsson2ce7c3d2008-12-01 02:13:57 +00001423bool Expr::isNullPointerConstant(ASTContext &Ctx) const
1424{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001425 // Strip off a cast to void*, if it exists. Except in C++.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001426 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl3768d272008-11-04 11:45:54 +00001427 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001428 // Check that it is a cast to void*.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +00001429 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001430 QualType Pointee = PT->getPointeeType();
1431 if (Pointee.getCVRQualifiers() == 0 &&
1432 Pointee->isVoidType() && // to void*
1433 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001434 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001435 }
Chris Lattner4b009652007-07-25 00:24:17 +00001436 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001437 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1438 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001439 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffa2e53222008-01-14 16:10:57 +00001440 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1441 // Accept ((void*)0) as a null pointer constant, as many other
1442 // implementations do.
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001443 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001444 } else if (const CXXDefaultArgExpr *DefaultArg
1445 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001446 // See through default argument expressions
Anders Carlssonf8aa8702008-12-01 06:28:23 +00001447 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregorad4b3792008-11-29 04:51:27 +00001448 } else if (isa<GNUNullExpr>(this)) {
1449 // The GNU __null extension is always a null pointer constant.
1450 return true;
Steve Narofff33a9852008-01-14 02:53:34 +00001451 }
Douglas Gregorad4b3792008-11-29 04:51:27 +00001452
Sebastian Redl5d0ead72009-05-10 18:38:11 +00001453 // C++0x nullptr_t is always a null pointer constant.
1454 if (getType()->isNullPtrType())
1455 return true;
1456
Steve Naroffa2e53222008-01-14 16:10:57 +00001457 // This expression must be an integer type.
1458 if (!getType()->isIntegerType())
1459 return false;
1460
Chris Lattner4b009652007-07-25 00:24:17 +00001461 // If we have an integer constant expression, we need to *evaluate* it and
1462 // test for the value 0.
Eli Friedman4b4e14b2009-04-25 22:37:12 +00001463 llvm::APSInt Result;
1464 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001465}
Steve Naroffc11705f2007-07-28 23:10:27 +00001466
Douglas Gregor531434b2009-05-02 02:18:30 +00001467FieldDecl *Expr::getBitField() {
Douglas Gregor34909522009-07-06 15:38:40 +00001468 Expr *E = this->IgnoreParens();
Douglas Gregor531434b2009-05-02 02:18:30 +00001469
Douglas Gregor81c29152008-10-29 00:13:59 +00001470 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor82d44772008-12-20 23:49:58 +00001471 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor531434b2009-05-02 02:18:30 +00001472 if (Field->isBitField())
1473 return Field;
1474
1475 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1476 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1477 return BinOp->getLHS()->getBitField();
1478
1479 return 0;
Douglas Gregor81c29152008-10-29 00:13:59 +00001480}
1481
Chris Lattner98e7fcc2009-02-16 22:14:05 +00001482/// isArrow - Return true if the base expression is a pointer to vector,
1483/// return false if the base expression is a vector.
1484bool ExtVectorElementExpr::isArrow() const {
1485 return getBase()->getType()->isPointerType();
1486}
1487
Nate Begemanaf6ed502008-04-18 23:10:10 +00001488unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001489 if (const VectorType *VT = getType()->getAsVectorType())
1490 return VT->getNumElements();
1491 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001492}
1493
Nate Begemanc8e51f82008-05-09 06:41:27 +00001494/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001495bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001496 const char *compStr = Accessor->getName();
1497 unsigned length = Accessor->getLength();
Nate Begemana8e117c2009-01-18 02:01:21 +00001498
1499 // Halving swizzles do not contain duplicate elements.
1500 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
1501 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1502 return false;
1503
1504 // Advance past s-char prefix on hex swizzles.
Nate Begemane2ed6f72009-06-25 21:06:09 +00001505 if (*compStr == 's' || *compStr == 'S') {
Nate Begemana8e117c2009-01-18 02:01:21 +00001506 compStr++;
1507 length--;
1508 }
Steve Naroffba67f692007-07-30 03:29:09 +00001509
Chris Lattner58d3fa52008-11-19 07:55:04 +00001510 for (unsigned i = 0; i != length-1; i++) {
Steve Naroffba67f692007-07-30 03:29:09 +00001511 const char *s = compStr+i;
1512 for (const char c = *s++; *s; s++)
1513 if (c == *s)
1514 return true;
1515 }
1516 return false;
1517}
Chris Lattner42158e72007-08-02 23:36:59 +00001518
Nate Begemanc8e51f82008-05-09 06:41:27 +00001519/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001520void ExtVectorElementExpr::getEncodedElementAccess(
1521 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregorec0b8292009-04-15 23:02:49 +00001522 const char *compStr = Accessor->getName();
Nate Begemane2ed6f72009-06-25 21:06:09 +00001523 if (*compStr == 's' || *compStr == 'S')
Nate Begeman1486b502009-01-18 01:47:54 +00001524 compStr++;
1525
1526 bool isHi = !strcmp(compStr, "hi");
1527 bool isLo = !strcmp(compStr, "lo");
1528 bool isEven = !strcmp(compStr, "even");
1529 bool isOdd = !strcmp(compStr, "odd");
1530
Nate Begemanc8e51f82008-05-09 06:41:27 +00001531 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1532 uint64_t Index;
1533
1534 if (isHi)
1535 Index = e + i;
1536 else if (isLo)
1537 Index = i;
1538 else if (isEven)
1539 Index = 2 * i;
1540 else if (isOdd)
1541 Index = 2 * i + 1;
1542 else
1543 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001544
Nate Begemana1ae7442008-05-13 21:03:02 +00001545 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001546 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001547}
1548
Steve Naroff4ed9d662007-09-27 14:38:14 +00001549// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001550ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001552 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001553 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001554 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001555 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001556 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001557 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001558 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001559 if (NumArgs) {
1560 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001561 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1562 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001563 LBracloc = LBrac;
1564 RBracloc = RBrac;
1565}
1566
Anders Carlsson0ebbcba2009-06-07 19:51:47 +00001567ObjCStringLiteral* ObjCStringLiteral::Clone(ASTContext &C) const {
1568 // Clone the string literal.
1569 StringLiteral *NewString =
1570 String ? cast<StringLiteral>(String)->Clone(C) : 0;
1571
1572 return new (C) ObjCStringLiteral(NewString, getType(), AtLoc);
1573}
1574
1575ObjCSelectorExpr *ObjCSelectorExpr::Clone(ASTContext &C) const {
1576 return new (C) ObjCSelectorExpr(getType(), SelName, AtLoc, RParenLoc);
1577}
1578
1579ObjCProtocolExpr *ObjCProtocolExpr::Clone(ASTContext &C) const {
Fariborz Jahanian77e15a52009-06-21 18:26:03 +00001580 return new (C) ObjCProtocolExpr(getType(), TheProtocol, AtLoc, RParenLoc);
Anders Carlsson0ebbcba2009-06-07 19:51:47 +00001581}
1582
Steve Naroff4ed9d662007-09-27 14:38:14 +00001583// constructor for class messages.
1584// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001585ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001586 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001587 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001588 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001589 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001590 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001591 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001592 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001593 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001594 if (NumArgs) {
1595 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001596 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1597 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001598 LBracloc = LBrac;
1599 RBracloc = RBrac;
1600}
1601
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001602// constructor for class messages.
1603ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1604 QualType retType, ObjCMethodDecl *mproto,
1605 SourceLocation LBrac, SourceLocation RBrac,
1606 Expr **ArgExprs, unsigned nargs)
1607: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1608MethodProto(mproto) {
1609 NumArgs = nargs;
1610 SubExprs = new Stmt*[NumArgs+1];
1611 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1612 if (NumArgs) {
1613 for (unsigned i = 0; i != NumArgs; ++i)
1614 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1615 }
1616 LBracloc = LBrac;
1617 RBracloc = RBrac;
1618}
1619
1620ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1621 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1622 switch (x & Flags) {
1623 default:
1624 assert(false && "Invalid ObjCMessageExpr.");
1625 case IsInstMeth:
1626 return ClassInfo(0, 0);
1627 case IsClsMethDeclUnknown:
1628 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1629 case IsClsMethDeclKnown: {
1630 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1631 return ClassInfo(D, D->getIdentifier());
1632 }
1633 }
1634}
1635
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001636void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1637 if (CI.first == 0 && CI.second == 0)
1638 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1639 else if (CI.first == 0)
1640 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1641 else
1642 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1643}
1644
1645
Chris Lattnerf624cd22007-10-25 00:29:32 +00001646bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman5255e7a2009-04-26 19:19:15 +00001647 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001648}
1649
Douglas Gregor725e94b2009-04-16 00:01:45 +00001650void ShuffleVectorExpr::setExprs(Expr ** Exprs, unsigned NumExprs) {
1651 if (NumExprs)
1652 delete [] SubExprs;
1653
1654 SubExprs = new Stmt* [NumExprs];
1655 this->NumExprs = NumExprs;
1656 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
1657}
1658
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001659void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001660 // Override default behavior of traversing children. If this has a type
1661 // operand and the type is a variable-length array, the child iteration
1662 // will iterate over the size expression. However, this expression belongs
1663 // to the type, not to this, so we don't want to delete it.
1664 // We still want to delete this expression.
Ted Kremenek0c97e042009-02-07 01:47:29 +00001665 if (isArgumentType()) {
1666 this->~SizeOfAlignOfExpr();
1667 C.Deallocate(this);
1668 }
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001669 else
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001670 Expr::DoDestroy(C);
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001671}
1672
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001673//===----------------------------------------------------------------------===//
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001674// DesignatedInitExpr
1675//===----------------------------------------------------------------------===//
1676
1677IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1678 assert(Kind == FieldDesignator && "Only valid on a field designator");
1679 if (Field.NameOrField & 0x01)
1680 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1681 else
1682 return getField()->getIdentifier();
1683}
1684
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001685DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
1686 const Designator *Designators,
1687 SourceLocation EqualOrColonLoc,
1688 bool GNUSyntax,
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001689 Expr **IndexExprs,
1690 unsigned NumIndexExprs,
1691 Expr *Init)
1692 : Expr(DesignatedInitExprClass, Ty,
1693 Init->isTypeDependent(), Init->isValueDependent()),
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001694 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001695 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001696 this->Designators = new Designator[NumDesignators];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001697
1698 // Record the initializer itself.
1699 child_iterator Child = child_begin();
1700 *Child++ = Init;
1701
1702 // Copy the designators and their subexpressions, computing
1703 // value-dependence along the way.
1704 unsigned IndexIdx = 0;
1705 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001706 this->Designators[I] = Designators[I];
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001707
1708 if (this->Designators[I].isArrayDesignator()) {
1709 // Compute type- and value-dependence.
1710 Expr *Index = IndexExprs[IndexIdx];
1711 ValueDependent = ValueDependent ||
1712 Index->isTypeDependent() || Index->isValueDependent();
1713
1714 // Copy the index expressions into permanent storage.
1715 *Child++ = IndexExprs[IndexIdx++];
1716 } else if (this->Designators[I].isArrayRangeDesignator()) {
1717 // Compute type- and value-dependence.
1718 Expr *Start = IndexExprs[IndexIdx];
1719 Expr *End = IndexExprs[IndexIdx + 1];
1720 ValueDependent = ValueDependent ||
1721 Start->isTypeDependent() || Start->isValueDependent() ||
1722 End->isTypeDependent() || End->isValueDependent();
1723
1724 // Copy the start/end expressions into permanent storage.
1725 *Child++ = IndexExprs[IndexIdx++];
1726 *Child++ = IndexExprs[IndexIdx++];
1727 }
1728 }
1729
1730 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001731}
1732
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001733DesignatedInitExpr *
1734DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
1735 unsigned NumDesignators,
1736 Expr **IndexExprs, unsigned NumIndexExprs,
1737 SourceLocation ColonOrEqualLoc,
1738 bool UsesColonSyntax, Expr *Init) {
Steve Naroff207b9ec2009-01-27 23:20:32 +00001739 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001740 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001741 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1742 ColonOrEqualLoc, UsesColonSyntax,
1743 IndexExprs, NumIndexExprs, Init);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001744}
1745
Douglas Gregor6710a3c2009-04-16 00:55:48 +00001746DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
1747 unsigned NumIndexExprs) {
1748 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1749 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1750 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1751}
1752
1753void DesignatedInitExpr::setDesignators(const Designator *Desigs,
1754 unsigned NumDesigs) {
1755 if (Designators)
1756 delete [] Designators;
1757
1758 Designators = new Designator[NumDesigs];
1759 NumDesignators = NumDesigs;
1760 for (unsigned I = 0; I != NumDesigs; ++I)
1761 Designators[I] = Desigs[I];
1762}
1763
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001764SourceRange DesignatedInitExpr::getSourceRange() const {
1765 SourceLocation StartLoc;
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001766 Designator &First =
1767 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001768 if (First.isFieldDesignator()) {
Douglas Gregor5f34f0e2009-03-28 00:41:23 +00001769 if (GNUSyntax)
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001770 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1771 else
1772 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1773 } else
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001774 StartLoc =
1775 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001776 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1777}
1778
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001779Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1780 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1781 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1782 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001783 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1784 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1785}
1786
1787Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
1788 assert(D.Kind == Designator::ArrayRangeDesignator &&
1789 "Requires array range designator");
1790 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1791 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001792 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1793 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1794}
1795
1796Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
1797 assert(D.Kind == Designator::ArrayRangeDesignator &&
1798 "Requires array range designator");
1799 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1800 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001801 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1802 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1803}
1804
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001805/// \brief Replaces the designator at index @p Idx with the series
1806/// of designators in [First, Last).
1807void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1808 const Designator *First,
1809 const Designator *Last) {
1810 unsigned NumNewDesignators = Last - First;
1811 if (NumNewDesignators == 0) {
1812 std::copy_backward(Designators + Idx + 1,
1813 Designators + NumDesignators,
1814 Designators + Idx);
1815 --NumNewDesignators;
1816 return;
1817 } else if (NumNewDesignators == 1) {
1818 Designators[Idx] = *First;
1819 return;
1820 }
1821
1822 Designator *NewDesignators
1823 = new Designator[NumDesignators - 1 + NumNewDesignators];
1824 std::copy(Designators, Designators + Idx, NewDesignators);
1825 std::copy(First, Last, NewDesignators + Idx);
1826 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1827 NewDesignators + Idx + NumNewDesignators);
1828 delete [] Designators;
1829 Designators = NewDesignators;
1830 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1831}
1832
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001833void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001834 delete [] Designators;
Douglas Gregor53e8e4b2009-08-07 06:08:38 +00001835 Expr::DoDestroy(C);
Douglas Gregorcc94ab72009-04-15 06:41:24 +00001836}
1837
Douglas Gregor3a7a06e2009-05-21 23:17:49 +00001838ImplicitValueInitExpr *ImplicitValueInitExpr::Clone(ASTContext &C) const {
1839 return new (C) ImplicitValueInitExpr(getType());
1840}
1841
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00001842//===----------------------------------------------------------------------===//
Ted Kremenekb30de272008-10-27 18:40:21 +00001843// ExprIterator.
1844//===----------------------------------------------------------------------===//
1845
1846Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1847Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1848Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1849const Expr* ConstExprIterator::operator[](size_t idx) const {
1850 return cast<Expr>(I[idx]);
1851}
1852const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1853const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1854
1855//===----------------------------------------------------------------------===//
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001856// Child Iterators for iterating over subexpressions/substatements
1857//===----------------------------------------------------------------------===//
1858
1859// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001860Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1861Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001862
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001863// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001864Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1865Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001866
Steve Naroff6f786252008-06-02 23:03:37 +00001867// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001868Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1869Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001870
Fariborz Jahanianf18d4c82008-11-22 18:39:36 +00001871// ObjCKVCRefExpr
1872Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; }
1873Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; }
1874
Douglas Gregord8606632008-11-04 14:56:14 +00001875// ObjCSuperExpr
1876Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1877Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1878
Steve Naroff29d293b2009-07-24 17:54:45 +00001879// ObjCIsaExpr
1880Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1881Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1882
Chris Lattner69909292008-08-10 01:53:14 +00001883// PredefinedExpr
1884Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1885Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001886
1887// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001888Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1889Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001890
1891// CharacterLiteral
Chris Lattnerb6eccdc2009-02-16 22:33:34 +00001892Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremeneka6478552007-10-18 23:28:49 +00001893Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001894
1895// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001896Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1897Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001898
Chris Lattner1de66eb2007-08-26 03:42:43 +00001899// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001900Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1901Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001902
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001903// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001904Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1905Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001906
1907// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001908Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1909Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001910
1911// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001912Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1913Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001914
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001915// SizeOfAlignOfExpr
1916Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
1917 // If this is of a type and the type is a VLA type (and not a typedef), the
1918 // size expression of the VLA needs to be treated as an executable expression.
1919 // Why isn't this weirdness documented better in StmtIterator?
1920 if (isArgumentType()) {
1921 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
1922 getArgumentType().getTypePtr()))
1923 return child_iterator(T);
1924 return child_iterator();
1925 }
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001926 return child_iterator(&Argument.Ex);
Ted Kremeneka6478552007-10-18 23:28:49 +00001927}
Sebastian Redl0cb7c872008-11-11 17:56:53 +00001928Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
1929 if (isArgumentType())
1930 return child_iterator();
Sebastian Redl9f81c3f2008-12-03 23:17:54 +00001931 return child_iterator(&Argument.Ex + 1);
Ted Kremeneka6478552007-10-18 23:28:49 +00001932}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001933
1934// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001935Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001936 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001937}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001938Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001939 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001940}
1941
1942// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001943Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001944 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001945}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001946Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001947 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001948}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001949
1950// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001951Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1952Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001953
Nate Begemanaf6ed502008-04-18 23:10:10 +00001954// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001955Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1956Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001957
1958// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001959Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1960Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001961
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001962// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001963Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1964Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001965
1966// BinaryOperator
1967Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001968 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001969}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001970Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001971 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001972}
1973
1974// ConditionalOperator
1975Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001976 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001977}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001978Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001979 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001980}
1981
1982// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001983Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1984Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001985
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001986// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001987Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1988Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001989
1990// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001991Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1992 return child_iterator();
1993}
1994
1995Stmt::child_iterator TypesCompatibleExpr::child_end() {
1996 return child_iterator();
1997}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001998
1999// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002000Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2001Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002002
Douglas Gregorad4b3792008-11-29 04:51:27 +00002003// GNUNullExpr
2004Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2005Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2006
Eli Friedmand0e9d092008-05-14 19:38:39 +00002007// ShuffleVectorExpr
2008Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002009 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00002010}
2011Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002012 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00002013}
2014
Anders Carlsson36760332007-10-15 20:28:48 +00002015// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00002016Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2017Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00002018
Anders Carlsson762b7c72007-08-31 04:56:16 +00002019// InitListExpr
2020Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002021 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002022}
2023Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002024 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00002025}
2026
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002027// DesignatedInitExpr
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002028Stmt::child_iterator DesignatedInitExpr::child_begin() {
2029 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2030 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregorc5a6bdc2009-01-22 00:58:24 +00002031 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2032}
2033Stmt::child_iterator DesignatedInitExpr::child_end() {
2034 return child_iterator(&*child_begin() + NumSubExprs);
2035}
2036
Douglas Gregorc9e012a2009-01-29 17:44:32 +00002037// ImplicitValueInitExpr
2038Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2039 return child_iterator();
2040}
2041
2042Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2043 return child_iterator();
2044}
2045
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002046// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00002047Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner2c499632009-02-18 06:53:08 +00002048 return &String;
Ted Kremeneka6478552007-10-18 23:28:49 +00002049}
2050Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner2c499632009-02-18 06:53:08 +00002051 return &String+1;
Ted Kremeneka6478552007-10-18 23:28:49 +00002052}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002053
2054// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002055Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2056Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00002057
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002058// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002059Stmt::child_iterator ObjCSelectorExpr::child_begin() {
2060 return child_iterator();
2061}
2062Stmt::child_iterator ObjCSelectorExpr::child_end() {
2063 return child_iterator();
2064}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002065
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002066// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00002067Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2068 return child_iterator();
2069}
2070Stmt::child_iterator ObjCProtocolExpr::child_end() {
2071 return child_iterator();
2072}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002073
Steve Naroffc39ca262007-09-18 23:55:05 +00002074// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00002075Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002076 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00002077}
2078Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00002079 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00002080}
2081
Steve Naroff52a81c02008-09-03 18:15:37 +00002082// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00002083Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2084Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00002085
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00002086Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2087Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }