blob: fb79f4d0f244f9bf13ec0c65d613a5d98b8a9487 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000026#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000027using namespace clang;
28
Chris Lattner0eedafe2006-08-24 04:56:27 +000029//===----------------------------------------------------------------------===//
30// Primary Expressions.
31//===----------------------------------------------------------------------===//
32
Anders Carlsson2fb08242009-09-08 18:24:21 +000033// FIXME: Maybe this should use DeclPrinter with a special "print predefined
34// expr" policy instead.
35std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
36 const Decl *CurrentDecl) {
37 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
38 if (IT != PrettyFunction)
39 return FD->getNameAsString();
40
41 llvm::SmallString<256> Name;
42 llvm::raw_svector_ostream Out(Name);
43
44 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
45 if (MD->isVirtual())
46 Out << "virtual ";
47 }
48
49 PrintingPolicy Policy(Context.getLangOptions());
50 Policy.SuppressTagKind = true;
51
52 std::string Proto = FD->getQualifiedNameAsString(Policy);
53
54 const FunctionType *AFT = FD->getType()->getAsFunctionType();
55 const FunctionProtoType *FT = 0;
56 if (FD->hasWrittenPrototype())
57 FT = dyn_cast<FunctionProtoType>(AFT);
58
59 Proto += "(";
60 if (FT) {
61 llvm::raw_string_ostream POut(Proto);
62 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
63 if (i) POut << ", ";
64 std::string Param;
65 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
66 POut << Param;
67 }
68
69 if (FT->isVariadic()) {
70 if (FD->getNumParams()) POut << ", ";
71 POut << "...";
72 }
73 }
74 Proto += ")";
75
76 AFT->getResultType().getAsStringInternal(Proto, Policy);
77
78 Out << Proto;
79
80 Out.flush();
81 return Name.str().str();
82 }
83 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
84 llvm::SmallString<256> Name;
85 llvm::raw_svector_ostream Out(Name);
86 Out << (MD->isInstanceMethod() ? '-' : '+');
87 Out << '[';
88 Out << MD->getClassInterface()->getNameAsString();
89 if (const ObjCCategoryImplDecl *CID =
90 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
91 Out << '(';
92 Out << CID->getNameAsString();
93 Out << ')';
94 }
95 Out << ' ';
96 Out << MD->getSelector().getAsString();
97 Out << ']';
98
99 Out.flush();
100 return Name.str().str();
101 }
102 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
103 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
104 return "top level";
105 }
106 return "";
107}
108
Chris Lattnera0173132008-06-07 22:13:43 +0000109/// getValueAsApproximateDouble - This returns the value as an inaccurate
110/// double. Note that this may cause loss of precision, but is useful for
111/// debugging dumps, etc.
112double FloatingLiteral::getValueAsApproximateDouble() const {
113 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000114 bool ignored;
115 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
116 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000117 return V.convertToDouble();
118}
119
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000120StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
121 unsigned ByteLength, bool Wide,
122 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000123 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000124 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000125 // Allocate enough space for the StringLiteral plus an array of locations for
126 // any concatenated string tokens.
127 void *Mem = C.Allocate(sizeof(StringLiteral)+
128 sizeof(SourceLocation)*(NumStrs-1),
129 llvm::alignof<StringLiteral>());
130 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroffdf7855b2007-02-21 23:46:25 +0000132 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000133 char *AStrData = new (C, 1) char[ByteLength];
134 memcpy(AStrData, StrData, ByteLength);
135 SL->StrData = AStrData;
136 SL->ByteLength = ByteLength;
137 SL->IsWide = Wide;
138 SL->TokLocs[0] = Loc[0];
139 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000140
Chris Lattner630970d2009-02-18 05:49:11 +0000141 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000142 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
143 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000144}
145
Douglas Gregor958dfc92009-04-15 16:35:07 +0000146StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
147 void *Mem = C.Allocate(sizeof(StringLiteral)+
148 sizeof(SourceLocation)*(NumStrs-1),
149 llvm::alignof<StringLiteral>());
150 StringLiteral *SL = new (Mem) StringLiteral(QualType());
151 SL->StrData = 0;
152 SL->ByteLength = 0;
153 SL->NumConcatenated = NumStrs;
154 return SL;
155}
156
Douglas Gregore26a2852009-08-07 06:08:38 +0000157void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000158 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000159 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000160}
161
Douglas Gregor958dfc92009-04-15 16:35:07 +0000162void StringLiteral::setStrData(ASTContext &C, const char *Str, unsigned Len) {
163 if (StrData)
164 C.Deallocate(const_cast<char*>(StrData));
165
166 char *AStrData = new (C, 1) char[Len];
167 memcpy(AStrData, Str, Len);
168 StrData = AStrData;
169 ByteLength = Len;
170}
171
Chris Lattner1b926492006-08-23 06:42:10 +0000172/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
173/// corresponds to, e.g. "sizeof" or "[pre]++".
174const char *UnaryOperator::getOpcodeStr(Opcode Op) {
175 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000176 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000177 case PostInc: return "++";
178 case PostDec: return "--";
179 case PreInc: return "++";
180 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000181 case AddrOf: return "&";
182 case Deref: return "*";
183 case Plus: return "+";
184 case Minus: return "-";
185 case Not: return "~";
186 case LNot: return "!";
187 case Real: return "__real";
188 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000189 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000190 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000191 }
192}
193
Mike Stump11289f42009-09-09 15:08:12 +0000194UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000195UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
196 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000197 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000198 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
199 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
200 case OO_Amp: return AddrOf;
201 case OO_Star: return Deref;
202 case OO_Plus: return Plus;
203 case OO_Minus: return Minus;
204 case OO_Tilde: return Not;
205 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000206 }
207}
208
209OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
210 switch (Opc) {
211 case PostInc: case PreInc: return OO_PlusPlus;
212 case PostDec: case PreDec: return OO_MinusMinus;
213 case AddrOf: return OO_Amp;
214 case Deref: return OO_Star;
215 case Plus: return OO_Plus;
216 case Minus: return OO_Minus;
217 case Not: return OO_Tilde;
218 case LNot: return OO_Exclaim;
219 default: return OO_None;
220 }
221}
222
223
Chris Lattner0eedafe2006-08-24 04:56:27 +0000224//===----------------------------------------------------------------------===//
225// Postfix Operators.
226//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000227
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000228CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000229 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000230 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000231 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000232 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000233 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000235 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000236 SubExprs[FN] = fn;
237 for (unsigned i = 0; i != numargs; ++i)
238 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000239
Douglas Gregor993603d2008-11-14 16:09:21 +0000240 RParenLoc = rparenloc;
241}
Nate Begeman1e36a852008-01-17 17:46:27 +0000242
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000243CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
244 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000245 : Expr(CallExprClass, t,
246 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000247 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000248 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000249
250 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000251 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000252 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000253 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000254
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000255 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000256}
257
Mike Stump11289f42009-09-09 15:08:12 +0000258CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
259 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000260 SubExprs = new (C) Stmt*[1];
261}
262
Douglas Gregore26a2852009-08-07 06:08:38 +0000263void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000264 DestroyChildren(C);
265 if (SubExprs) C.Deallocate(SubExprs);
266 this->~CallExpr();
267 C.Deallocate(this);
268}
269
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000270FunctionDecl *CallExpr::getDirectCallee() {
271 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000272 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000273 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000274
275 return 0;
276}
277
Chris Lattnere4407ed2007-12-28 05:25:02 +0000278/// setNumArgs - This changes the number of arguments present in this call.
279/// Any orphaned expressions are deleted by this, and any new operands are set
280/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000281void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000282 // No change, just return.
283 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattnere4407ed2007-12-28 05:25:02 +0000285 // If shrinking # arguments, just delete the extras and forgot them.
286 if (NumArgs < getNumArgs()) {
287 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000288 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000289 this->NumArgs = NumArgs;
290 return;
291 }
292
293 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000294 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000295 // Copy over args.
296 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
297 NewSubExprs[i] = SubExprs[i];
298 // Null out new args.
299 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
300 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregorba6e5572009-04-17 21:46:47 +0000302 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000303 SubExprs = NewSubExprs;
304 this->NumArgs = NumArgs;
305}
306
Chris Lattner01ff98a2008-10-06 05:00:53 +0000307/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
308/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000309unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000310 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000311 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000312 // ImplicitCastExpr.
313 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
314 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000315 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Steve Narofff6e3b3292008-01-31 01:07:12 +0000317 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
318 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000319 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000320
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000321 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
322 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000323 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000325 if (!FDecl->getIdentifier())
326 return 0;
327
Douglas Gregore711f702009-02-14 18:57:46 +0000328 return FDecl->getBuiltinID(Context);
Chris Lattner01ff98a2008-10-06 05:00:53 +0000329}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000330
Anders Carlsson00a27592009-05-26 04:57:27 +0000331QualType CallExpr::getCallReturnType() const {
332 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000333 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000334 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000335 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000336 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson00a27592009-05-26 04:57:27 +0000338 const FunctionType *FnType = CalleeType->getAsFunctionType();
339 return FnType->getResultType();
340}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000341
Mike Stump11289f42009-09-09 15:08:12 +0000342MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
343 SourceRange qualrange, NamedDecl *memberdecl,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000344 SourceLocation l, bool has_explicit,
345 SourceLocation langle,
346 const TemplateArgument *targs, unsigned numtargs,
347 SourceLocation rangle, QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000348 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000349 base->isTypeDependent() || (qual && qual->isDependent()),
350 base->isValueDependent() || (qual && qual->isDependent())),
351 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000352 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000353 // Initialize the qualifier, if any.
354 if (HasQualifier) {
355 NameQualifier *NQ = getMemberQualifier();
356 NQ->NNS = qual;
357 NQ->Range = qualrange;
358 }
Mike Stump11289f42009-09-09 15:08:12 +0000359
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000360 // Initialize the explicit template argument list, if any.
361 if (HasExplicitTemplateArgumentList) {
Mike Stump11289f42009-09-09 15:08:12 +0000362 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000363 = getExplicitTemplateArgumentList();
364 ETemplateArgs->LAngleLoc = langle;
365 ETemplateArgs->RAngleLoc = rangle;
366 ETemplateArgs->NumTemplateArgs = numtargs;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000368 TemplateArgument *TemplateArgs = ETemplateArgs->getTemplateArgs();
369 for (unsigned I = 0; I < numtargs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000370 new (TemplateArgs + I) TemplateArgument(targs[I]);
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000371 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000372}
373
Mike Stump11289f42009-09-09 15:08:12 +0000374MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
375 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000376 SourceRange qualrange,
Mike Stump11289f42009-09-09 15:08:12 +0000377 NamedDecl *memberdecl,
378 SourceLocation l,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000379 bool has_explicit,
380 SourceLocation langle,
381 const TemplateArgument *targs,
382 unsigned numtargs,
383 SourceLocation rangle,
384 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000385 std::size_t Size = sizeof(MemberExpr);
386 if (qual != 0)
387 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000388
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000389 if (has_explicit)
Mike Stump11289f42009-09-09 15:08:12 +0000390 Size += sizeof(ExplicitTemplateArgumentList) +
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000391 sizeof(TemplateArgument) * numtargs;
Mike Stump11289f42009-09-09 15:08:12 +0000392
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000393 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000394 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
395 has_explicit, langle, targs, numtargs, rangle,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000396 ty);
397}
398
Anders Carlsson496335e2009-09-03 00:59:21 +0000399const char *CastExpr::getCastKindName() const {
400 switch (getCastKind()) {
401 case CastExpr::CK_Unknown:
402 return "Unknown";
403 case CastExpr::CK_BitCast:
404 return "BitCast";
405 case CastExpr::CK_NoOp:
406 return "NoOp";
407 case CastExpr::CK_DerivedToBase:
408 return "DerivedToBase";
409 case CastExpr::CK_Dynamic:
410 return "Dynamic";
411 case CastExpr::CK_ToUnion:
412 return "ToUnion";
413 case CastExpr::CK_ArrayToPointerDecay:
414 return "ArrayToPointerDecay";
415 case CastExpr::CK_FunctionToPointerDecay:
416 return "FunctionToPointerDecay";
417 case CastExpr::CK_NullToMemberPointer:
418 return "NullToMemberPointer";
419 case CastExpr::CK_BaseToDerivedMemberPointer:
420 return "BaseToDerivedMemberPointer";
421 case CastExpr::CK_UserDefinedConversion:
422 return "UserDefinedConversion";
423 case CastExpr::CK_ConstructorConversion:
424 return "ConstructorConversion";
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Anders Carlsson496335e2009-09-03 00:59:21 +0000427 assert(0 && "Unhandled cast kind!");
428 return 0;
429}
430
Chris Lattner1b926492006-08-23 06:42:10 +0000431/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
432/// corresponds to, e.g. "<<=".
433const char *BinaryOperator::getOpcodeStr(Opcode Op) {
434 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000435 case PtrMemD: return ".*";
436 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000437 case Mul: return "*";
438 case Div: return "/";
439 case Rem: return "%";
440 case Add: return "+";
441 case Sub: return "-";
442 case Shl: return "<<";
443 case Shr: return ">>";
444 case LT: return "<";
445 case GT: return ">";
446 case LE: return "<=";
447 case GE: return ">=";
448 case EQ: return "==";
449 case NE: return "!=";
450 case And: return "&";
451 case Xor: return "^";
452 case Or: return "|";
453 case LAnd: return "&&";
454 case LOr: return "||";
455 case Assign: return "=";
456 case MulAssign: return "*=";
457 case DivAssign: return "/=";
458 case RemAssign: return "%=";
459 case AddAssign: return "+=";
460 case SubAssign: return "-=";
461 case ShlAssign: return "<<=";
462 case ShrAssign: return ">>=";
463 case AndAssign: return "&=";
464 case XorAssign: return "^=";
465 case OrAssign: return "|=";
466 case Comma: return ",";
467 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000468
469 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000470}
Steve Naroff47500512007-04-19 23:00:49 +0000471
Mike Stump11289f42009-09-09 15:08:12 +0000472BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000473BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
474 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000475 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000476 case OO_Plus: return Add;
477 case OO_Minus: return Sub;
478 case OO_Star: return Mul;
479 case OO_Slash: return Div;
480 case OO_Percent: return Rem;
481 case OO_Caret: return Xor;
482 case OO_Amp: return And;
483 case OO_Pipe: return Or;
484 case OO_Equal: return Assign;
485 case OO_Less: return LT;
486 case OO_Greater: return GT;
487 case OO_PlusEqual: return AddAssign;
488 case OO_MinusEqual: return SubAssign;
489 case OO_StarEqual: return MulAssign;
490 case OO_SlashEqual: return DivAssign;
491 case OO_PercentEqual: return RemAssign;
492 case OO_CaretEqual: return XorAssign;
493 case OO_AmpEqual: return AndAssign;
494 case OO_PipeEqual: return OrAssign;
495 case OO_LessLess: return Shl;
496 case OO_GreaterGreater: return Shr;
497 case OO_LessLessEqual: return ShlAssign;
498 case OO_GreaterGreaterEqual: return ShrAssign;
499 case OO_EqualEqual: return EQ;
500 case OO_ExclaimEqual: return NE;
501 case OO_LessEqual: return LE;
502 case OO_GreaterEqual: return GE;
503 case OO_AmpAmp: return LAnd;
504 case OO_PipePipe: return LOr;
505 case OO_Comma: return Comma;
506 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000507 }
508}
509
510OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
511 static const OverloadedOperatorKind OverOps[] = {
512 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
513 OO_Star, OO_Slash, OO_Percent,
514 OO_Plus, OO_Minus,
515 OO_LessLess, OO_GreaterGreater,
516 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
517 OO_EqualEqual, OO_ExclaimEqual,
518 OO_Amp,
519 OO_Caret,
520 OO_Pipe,
521 OO_AmpAmp,
522 OO_PipePipe,
523 OO_Equal, OO_StarEqual,
524 OO_SlashEqual, OO_PercentEqual,
525 OO_PlusEqual, OO_MinusEqual,
526 OO_LessLessEqual, OO_GreaterGreaterEqual,
527 OO_AmpEqual, OO_CaretEqual,
528 OO_PipeEqual,
529 OO_Comma
530 };
531 return OverOps[Opc];
532}
533
Mike Stump11289f42009-09-09 15:08:12 +0000534InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000535 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000536 SourceLocation rbraceloc)
Douglas Gregorca1aeec2009-05-21 23:17:49 +0000537 : Expr(InitListExprClass, QualType(),
538 hasAnyTypeDependentArguments(initExprs, numInits),
539 hasAnyValueDependentArguments(initExprs, numInits)),
Mike Stump11289f42009-09-09 15:08:12 +0000540 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000541 UnionFieldInit(0), HadArrayRangeDesignator(false) {
Chris Lattner07d754a2008-10-26 23:43:26 +0000542
543 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000544}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000545
Douglas Gregor6d00c992009-03-20 23:58:33 +0000546void InitListExpr::reserveInits(unsigned NumInits) {
547 if (NumInits > InitExprs.size())
548 InitExprs.reserve(NumInits);
549}
550
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000551void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattner8ba22472009-02-16 22:33:34 +0000552 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar45a2a202009-02-16 22:42:44 +0000553 Idx < LastIdx; ++Idx)
Douglas Gregor52a47e92009-03-20 23:38:03 +0000554 InitExprs[Idx]->Destroy(Context);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000555 InitExprs.resize(NumInits, 0);
556}
557
558Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
559 if (Init >= InitExprs.size()) {
560 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
561 InitExprs.back() = expr;
562 return 0;
563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000565 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
566 InitExprs[Init] = expr;
567 return Result;
568}
569
Steve Naroff991e99d2008-09-04 15:31:07 +0000570/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000571///
572const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000573 return getType()->getAs<BlockPointerType>()->
Steve Naroffc540d662008-09-03 18:15:37 +0000574 getPointeeType()->getAsFunctionType();
575}
576
Mike Stump11289f42009-09-09 15:08:12 +0000577SourceLocation BlockExpr::getCaretLocation() const {
578 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000579}
Mike Stump11289f42009-09-09 15:08:12 +0000580const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000581 return TheBlock->getBody();
582}
Mike Stump11289f42009-09-09 15:08:12 +0000583Stmt *BlockExpr::getBody() {
584 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000585}
Steve Naroff415d3d52008-10-08 17:01:13 +0000586
587
Chris Lattner1ec5f562007-06-27 05:38:08 +0000588//===----------------------------------------------------------------------===//
589// Generic Expression Routines
590//===----------------------------------------------------------------------===//
591
Chris Lattner237f2752009-02-14 07:37:35 +0000592/// isUnusedResultAWarning - Return true if this immediate expression should
593/// be warned about if the result is unused. If so, fill in Loc and Ranges
594/// with location to warn on and the source range[s] to report with the
595/// warning.
596bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000597 SourceRange &R2) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000598 // Don't warn if the expr is type dependent. The type could end up
599 // instantiating to void.
600 if (isTypeDependent())
601 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner1ec5f562007-06-27 05:38:08 +0000603 switch (getStmtClass()) {
604 default:
Chris Lattner237f2752009-02-14 07:37:35 +0000605 Loc = getExprLoc();
606 R1 = getSourceRange();
607 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000608 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000609 return cast<ParenExpr>(this)->getSubExpr()->
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000610 isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000611 case UnaryOperatorClass: {
612 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner1ec5f562007-06-27 05:38:08 +0000614 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000615 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000616 case UnaryOperator::PostInc:
617 case UnaryOperator::PostDec:
618 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000619 case UnaryOperator::PreDec: // ++/--
620 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000621 case UnaryOperator::Deref:
622 // Dereferencing a volatile pointer is a side-effect.
Chris Lattner237f2752009-02-14 07:37:35 +0000623 if (getType().isVolatileQualified())
624 return false;
625 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000626 case UnaryOperator::Real:
627 case UnaryOperator::Imag:
628 // accessing a piece of a volatile complex is a side-effect.
Chris Lattner237f2752009-02-14 07:37:35 +0000629 if (UO->getSubExpr()->getType().isVolatileQualified())
630 return false;
631 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000632 case UnaryOperator::Extension:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000633 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000634 }
Chris Lattner237f2752009-02-14 07:37:35 +0000635 Loc = UO->getOperatorLoc();
636 R1 = UO->getSubExpr()->getSourceRange();
637 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000638 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000639 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000640 const BinaryOperator *BO = cast<BinaryOperator>(this);
641 // Consider comma to have side effects if the LHS or RHS does.
642 if (BO->getOpcode() == BinaryOperator::Comma)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000643 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) ||
644 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner237f2752009-02-14 07:37:35 +0000646 if (BO->isAssignmentOp())
647 return false;
648 Loc = BO->getOperatorLoc();
649 R1 = BO->getLHS()->getSourceRange();
650 R2 = BO->getRHS()->getSourceRange();
651 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000652 }
Chris Lattner86928112007-08-25 02:00:02 +0000653 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000654 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000655
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000656 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000657 // The condition must be evaluated, but if either the LHS or RHS is a
658 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000659 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000660 if (Exp->getLHS() &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000661 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2))
Chris Lattner237f2752009-02-14 07:37:35 +0000662 return true;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000663 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000664 }
665
Chris Lattnera44d1162007-06-27 05:58:59 +0000666 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000667 // If the base pointer or element is to a volatile pointer/field, accessing
668 // it is a side effect.
669 if (getType().isVolatileQualified())
670 return false;
671 Loc = cast<MemberExpr>(this)->getMemberLoc();
672 R1 = SourceRange(Loc, Loc);
673 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
674 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000675
Chris Lattner1ec5f562007-06-27 05:38:08 +0000676 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000677 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000678 // it is a side effect.
679 if (getType().isVolatileQualified())
680 return false;
681 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
682 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
683 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
684 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000685
Chris Lattner1ec5f562007-06-27 05:38:08 +0000686 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000687 case CXXOperatorCallExprClass:
688 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000689 // If this is a direct call, get the callee.
690 const CallExpr *CE = cast<CallExpr>(this);
691 const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts();
692 if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) {
693 // If the callee has attribute pure, const, or warn_unused_result, warn
694 // about it. void foo() { strlen("bar"); } should warn.
695 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl()))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000696 if (FD->getAttr<WarnUnusedResultAttr>() ||
697 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000698 Loc = CE->getCallee()->getLocStart();
699 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattner237f2752009-02-14 07:37:35 +0000701 if (unsigned NumArgs = CE->getNumArgs())
702 R2 = SourceRange(CE->getArg(0)->getLocStart(),
703 CE->getArg(NumArgs-1)->getLocEnd());
704 return true;
705 }
706 }
707 return false;
708 }
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000709 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000710 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000711
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000712 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000713#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000714 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000715 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000716 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000717 Loc = Ref->getLocation();
718 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
719 if (Ref->getBase())
720 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000721#else
722 Loc = getExprLoc();
723 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000724#endif
725 return true;
726 }
Chris Lattner944d3062008-07-26 19:51:01 +0000727 case StmtExprClass: {
728 // Statement exprs don't logically have side effects themselves, but are
729 // sometimes used in macros in ways that give them a type that is unused.
730 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
731 // however, if the result of the stmt expr is dead, we don't want to emit a
732 // warning.
733 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
734 if (!CS->body_empty())
735 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000736 return E->isUnusedResultAWarning(Loc, R1, R2);
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattner237f2752009-02-14 07:37:35 +0000738 Loc = cast<StmtExpr>(this)->getLParenLoc();
739 R1 = getSourceRange();
740 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000741 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000742 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000743 // If this is an explicit cast to void, allow it. People do this when they
744 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000745 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000746 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000747 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
748 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
749 return true;
Argyrios Kyrtzidis857fcc22008-08-22 15:38:55 +0000750 case CXXFunctionalCastExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000751 // If this is a cast to void, check the operand. Otherwise, the result of
752 // the cast is unused.
753 if (getType()->isVoidType())
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000754 return cast<CastExpr>(this)->getSubExpr()
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000755 ->isUnusedResultAWarning(Loc, R1, R2);
Chris Lattner237f2752009-02-14 07:37:35 +0000756 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
757 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
758 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000759
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000760 case ImplicitCastExprClass:
761 // Check the operand, since implicit casts are inserted by Sema
Chris Lattner237f2752009-02-14 07:37:35 +0000762 return cast<ImplicitCastExpr>(this)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000763 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000764
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000765 case CXXDefaultArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000766 return cast<CXXDefaultArgExpr>(this)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000767 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000768
769 case CXXNewExprClass:
770 // FIXME: In theory, there might be new expressions that don't have side
771 // effects (e.g. a placement new with an uninitialized POD).
772 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000773 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000774 case CXXBindTemporaryExprClass:
775 return cast<CXXBindTemporaryExpr>(this)
776 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Anders Carlsson24824e52009-05-17 21:11:30 +0000777 case CXXExprWithTemporariesClass:
778 return cast<CXXExprWithTemporaries>(this)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000779 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000780 }
Chris Lattner1ec5f562007-06-27 05:38:08 +0000781}
782
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000783/// DeclCanBeLvalue - Determine whether the given declaration can be
784/// an lvalue. This is a helper routine for isLvalue.
785static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000786 // C++ [temp.param]p6:
787 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000788 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +0000789 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
790 return NTTParm->getType()->isReferenceType();
791
Douglas Gregor91f84212008-12-11 16:49:14 +0000792 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000793 // C++ 3.10p2: An lvalue refers to an object or function.
794 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor9b146582009-07-08 20:55:45 +0000795 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
796 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000797}
798
Steve Naroff475cca02007-05-14 17:19:29 +0000799/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
800/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000801/// - name, where name must be a variable
802/// - e[i]
803/// - (e), where e must be an lvalue
804/// - e.name, where e must be an lvalue
805/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000806/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000807/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +0000808/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000809/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000810///
Chris Lattner67315442008-07-26 21:30:36 +0000811Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000812 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
813
814 isLvalueResult Res = isLvalueInternal(Ctx);
815 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
816 return Res;
817
Douglas Gregor9a657932008-10-21 23:43:52 +0000818 // first, check the type (C99 6.3.2.1). Expressions with function
819 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +0000820 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +0000821 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000822
Steve Naroff1018ea32008-02-10 01:39:04 +0000823 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner67315442008-07-26 21:30:36 +0000824 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +0000825 return LV_IncompleteVoidType;
826
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000827 return LV_Valid;
828}
Bill Wendlingdfc81072007-07-17 03:52:31 +0000829
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000830// Check whether the expression can be sanely treated like an l-value
831Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +0000832 switch (getStmtClass()) {
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000833 case StringLiteralClass: // C99 6.5.1p4
834 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +0000835 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000836 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000837 // For vectors, make sure base is an lvalue (i.e. not a function call).
838 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +0000839 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +0000840 return LV_Valid;
Mike Stump11289f42009-09-09 15:08:12 +0000841 case DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000842 case QualifiedDeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000843 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
844 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +0000845 return LV_Valid;
846 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000847 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000848 case BlockDeclRefExprClass: {
849 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +0000850 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000851 return LV_Valid;
852 break;
853 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000854 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +0000855 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000856 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
857 NamedDecl *Member = m->getMemberDecl();
858 // C++ [expr.ref]p4:
859 // If E2 is declared to have type "reference to T", then E1.E2
860 // is an lvalue.
861 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
862 if (Value->getType()->isReferenceType())
863 return LV_Valid;
864
865 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +0000866 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000867 return LV_Valid;
868
869 // -- If E2 is a non-static data member [...]. If E1 is an
870 // lvalue, then E1.E2 is an lvalue.
871 if (isa<FieldDecl>(Member))
872 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
873
874 // -- If it refers to a static member function [...], then
875 // E1.E2 is an lvalue.
876 // -- Otherwise, if E1.E2 refers to a non-static member
877 // function [...], then E1.E2 is not an lvalue.
878 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
879 return Method->isStatic()? LV_Valid : LV_MemberFunction;
880
881 // -- If E2 is a member enumerator [...], the expression E1.E2
882 // is not an lvalue.
883 if (isa<EnumConstantDecl>(Member))
884 return LV_InvalidExpression;
885
886 // Not an lvalue.
887 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +0000888 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +0000889
890 // C99 6.5.2.3p4
Chris Lattner67315442008-07-26 21:30:36 +0000891 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000892 }
Chris Lattner595db862007-10-30 22:53:42 +0000893 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +0000894 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +0000895 return LV_Valid; // C99 6.5.3p4
896
897 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +0000898 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
899 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +0000900 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +0000901
902 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
903 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
904 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
905 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +0000906 break;
Douglas Gregora11693b2008-11-12 17:17:38 +0000907 case ImplicitCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +0000908 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregora11693b2008-11-12 17:17:38 +0000909 : LV_InvalidExpression;
Steve Naroff475cca02007-05-14 17:19:29 +0000910 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +0000911 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +0000912 case BinaryOperatorClass:
913 case CompoundAssignOperatorClass: {
914 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +0000915
916 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
917 BinOp->getOpcode() == BinaryOperator::Comma)
918 return BinOp->getRHS()->isLvalue(Ctx);
919
Sebastian Redl112a97662009-02-07 00:15:38 +0000920 // C++ [expr.mptr.oper]p6
921 if ((BinOp->getOpcode() == BinaryOperator::PtrMemD ||
922 BinOp->getOpcode() == BinaryOperator::PtrMemI) &&
923 !BinOp->getType()->isFunctionType())
924 return BinOp->getLHS()->isLvalue(Ctx);
925
Douglas Gregor58e008d2008-11-13 20:12:29 +0000926 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +0000927 return LV_InvalidExpression;
928
Douglas Gregor58e008d2008-11-13 20:12:29 +0000929 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +0000930 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +0000931 // The result of an assignment operation [...] is an lvalue.
932 return LV_Valid;
933
934
935 // C99 6.5.16:
936 // An assignment expression [...] is not an lvalue.
937 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +0000938 }
Mike Stump11289f42009-09-09 15:08:12 +0000939 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000940 case CXXOperatorCallExprClass:
941 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000942 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +0000943 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000944 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +0000945 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
946 if (ReturnType->isLValueReferenceType())
947 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000948
Douglas Gregor6b754842008-10-28 00:22:11 +0000949 break;
950 }
Steve Naroff2644aaf2007-12-05 04:00:10 +0000951 case CompoundLiteralExprClass: // C99 6.5.2.5p5
952 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +0000953 case ChooseExprClass:
954 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000955 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +0000956 case ExtVectorElementExprClass:
957 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +0000958 return LV_DuplicateVectorComponents;
959 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +0000960 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
961 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +0000962 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
963 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000964 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +0000965 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +0000966 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +0000967 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000968 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +0000969 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis0fdbd6c2008-09-11 04:22:26 +0000970 case CXXConditionDeclExprClass:
971 return LV_Valid;
Douglas Gregorf19b2312008-10-28 15:36:24 +0000972 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +0000973 case CXXFunctionalCastExprClass:
974 case CXXStaticCastExprClass:
975 case CXXDynamicCastExprClass:
976 case CXXReinterpretCastExprClass:
977 case CXXConstCastExprClass:
978 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000979 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +0000980 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
981 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000982 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
983 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +0000984 return LV_Valid;
985 break;
Sebastian Redlc4704762008-11-11 11:37:55 +0000986 case CXXTypeidExprClass:
987 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
988 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +0000989 case CXXBindTemporaryExprClass:
990 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
991 isLvalueInternal(Ctx);
Sebastian Redl5775af1a2009-04-17 16:30:52 +0000992 case ConditionalOperatorClass: {
993 // Complicated handling is only for C++.
994 if (!Ctx.getLangOptions().CPlusPlus)
995 return LV_InvalidExpression;
996
997 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
998 // everywhere there's an object converted to an rvalue. Also, any other
999 // casts should be wrapped by ImplicitCastExprs. There's just the special
1000 // case involving throws to work out.
1001 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001002 Expr *True = Cond->getTrueExpr();
1003 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001004 // C++0x 5.16p2
1005 // If either the second or the third operand has type (cv) void, [...]
1006 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001007 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001008 return LV_InvalidExpression;
1009
1010 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001011 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001012 return LV_InvalidExpression;
1013
1014 // That's it.
1015 return LV_Valid;
1016 }
1017
Steve Naroff9358c712007-05-27 23:58:33 +00001018 default:
1019 break;
Steve Naroff47500512007-04-19 23:00:49 +00001020 }
Steve Naroff9358c712007-05-27 23:58:33 +00001021 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001022}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001023
Steve Naroff475cca02007-05-14 17:19:29 +00001024/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1025/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001026/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001027/// recursively, any member or element of all contained aggregates or unions)
1028/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001029Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001030Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001031 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001032
Steve Naroff9358c712007-05-27 23:58:33 +00001033 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001034 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001035 // C++ 3.10p11: Functions cannot be modified, but pointers to
1036 // functions can be modifiable.
1037 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1038 return MLV_NotObjectType;
1039 break;
1040
Chris Lattner1ec5f562007-06-27 05:38:08 +00001041 case LV_NotObjectType: return MLV_NotObjectType;
1042 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001043 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001044 case LV_InvalidExpression:
1045 // If the top level is a C-style cast, and the subexpression is a valid
1046 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1047 // GCC extension. We don't support it, but we want to produce good
1048 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001049 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1050 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1051 if (Loc)
1052 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001053 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001054 }
1055 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001056 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001057 case LV_MemberFunction: return MLV_MemberFunction;
Steve Naroff9358c712007-05-27 23:58:33 +00001058 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001059
1060 // The following is illegal:
1061 // void takeclosure(void (^C)(void));
1062 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1063 //
Chris Lattner656711a2009-03-23 17:57:53 +00001064 if (isa<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001065 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
1066 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1067 return MLV_NotBlockQualified;
1068 }
1069
Chris Lattner7adf0762008-08-04 07:31:14 +00001070 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001071
Chris Lattner7adf0762008-08-04 07:31:14 +00001072 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001073 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001074 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001075 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001076 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001077 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001079 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001080 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001081 return MLV_ConstQualified;
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Fariborz Jahanian5118c412008-11-22 20:25:50 +00001084 // Assigning to an 'implicit' property?
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001085 else if (isa<ObjCImplicitSetterGetterRefExpr>(this)) {
Mike Stump11289f42009-09-09 15:08:12 +00001086 const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001087 cast<ObjCImplicitSetterGetterRefExpr>(this);
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001088 if (Expr->getSetterMethod() == 0)
Fariborz Jahanian5118c412008-11-22 20:25:50 +00001089 return MLV_NoSetterProperty;
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001092}
1093
Fariborz Jahanian07735332009-02-22 18:40:18 +00001094/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001095/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001096bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001097 switch (getStmtClass()) {
1098 default:
1099 return false;
1100 case ObjCIvarRefExprClass:
1101 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001102 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001103 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001104 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001105 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001106 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001107 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001108 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001109 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001110 case DeclRefExprClass:
1111 case QualifiedDeclRefExprClass: {
1112 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001113 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1114 if (VD->hasGlobalStorage())
1115 return true;
1116 QualType T = VD->getType();
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001117 // dereferencing to a pointer is always a gc'able candidate
1118 return T->isPointerType();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001119 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001120 return false;
1121 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001122 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001123 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001124 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001125 }
1126 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001127 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001128 }
1129}
Ted Kremenekfff70962008-01-17 16:57:34 +00001130Expr* Expr::IgnoreParens() {
1131 Expr* E = this;
1132 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1133 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001134
Ted Kremenekfff70962008-01-17 16:57:34 +00001135 return E;
1136}
1137
Chris Lattnerf2660962008-02-13 01:02:39 +00001138/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1139/// or CastExprs or ImplicitCastExprs, returning their operand.
1140Expr *Expr::IgnoreParenCasts() {
1141 Expr *E = this;
1142 while (true) {
1143 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1144 E = P->getSubExpr();
1145 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1146 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001147 else
1148 return E;
1149 }
1150}
1151
Chris Lattneref26c772009-03-13 17:28:01 +00001152/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1153/// value (including ptr->int casts of the same size). Strip off any
1154/// ParenExpr or CastExprs, returning their operand.
1155Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1156 Expr *E = this;
1157 while (true) {
1158 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1159 E = P->getSubExpr();
1160 continue;
1161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattneref26c772009-03-13 17:28:01 +00001163 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1164 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1165 // ptr<->int casts of the same width. We also ignore all identify casts.
1166 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattneref26c772009-03-13 17:28:01 +00001168 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1169 E = SE;
1170 continue;
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattneref26c772009-03-13 17:28:01 +00001173 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1174 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1175 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1176 E = SE;
1177 continue;
1178 }
1179 }
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattneref26c772009-03-13 17:28:01 +00001181 return E;
1182 }
1183}
1184
1185
Douglas Gregor4619e432008-12-05 23:32:09 +00001186/// hasAnyTypeDependentArguments - Determines if any of the expressions
1187/// in Exprs is type-dependent.
1188bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1189 for (unsigned I = 0; I < NumExprs; ++I)
1190 if (Exprs[I]->isTypeDependent())
1191 return true;
1192
1193 return false;
1194}
1195
1196/// hasAnyValueDependentArguments - Determines if any of the expressions
1197/// in Exprs is value-dependent.
1198bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1199 for (unsigned I = 0; I < NumExprs; ++I)
1200 if (Exprs[I]->isValueDependent())
1201 return true;
1202
1203 return false;
1204}
1205
Eli Friedman7139af42009-01-25 02:32:41 +00001206bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001207 // This function is attempting whether an expression is an initializer
1208 // which can be evaluated at compile-time. isEvaluatable handles most
1209 // of the cases, but it can't deal with some initializer-specific
1210 // expressions, and it can't deal with aggregates; we deal with those here,
1211 // and fall back to isEvaluatable for the other cases.
1212
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001213 // FIXME: This function assumes the variable being assigned to
1214 // isn't a reference type!
1215
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001216 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001217 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001218 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001219 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001220 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001221 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001222 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001223 // This handles gcc's extension that allows global initializers like
1224 // "struct x {int x;} x = (struct x) {};".
1225 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001226 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001227 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001228 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001229 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001230 // FIXME: This doesn't deal with fields with reference types correctly.
1231 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1232 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001233 const InitListExpr *Exp = cast<InitListExpr>(this);
1234 unsigned numInits = Exp->getNumInits();
1235 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001236 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001237 return false;
1238 }
Eli Friedman384da272009-01-25 03:12:18 +00001239 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001240 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001241 case ImplicitValueInitExprClass:
1242 return true;
Eli Friedman384da272009-01-25 03:12:18 +00001243 case ParenExprClass: {
1244 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1245 }
1246 case UnaryOperatorClass: {
1247 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1248 if (Exp->getOpcode() == UnaryOperator::Extension)
1249 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1250 break;
1251 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001252 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001253 case CStyleCastExprClass:
1254 // Handle casts with a destination that's a struct or union; this
1255 // deals with both the gcc no-op struct cast extension and the
1256 // cast-to-union extension.
1257 if (getType()->isRecordType())
1258 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1259 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001260 }
Eli Friedman384da272009-01-25 03:12:18 +00001261 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001262}
1263
Chris Lattner1f4479e2007-06-05 04:15:44 +00001264/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001265/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001266
1267/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1268/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001269///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001270/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1271/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1272/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001273
Eli Friedman98c56a42009-02-26 09:29:13 +00001274// CheckICE - This function does the fundamental ICE checking: the returned
1275// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1276// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001277// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001278// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001279// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001280//
1281// Meanings of Val:
1282// 0: This expression is an ICE if it can be evaluated by Evaluate.
1283// 1: This expression is not an ICE, but if it isn't evaluated, it's
1284// a legal subexpression for an ICE. This return value is used to handle
1285// the comma operator in C99 mode.
1286// 2: This expression is not an ICE, and is not a legal subexpression for one.
1287
1288struct ICEDiag {
1289 unsigned Val;
1290 SourceLocation Loc;
1291
1292 public:
1293 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1294 ICEDiag() : Val(0) {}
1295};
1296
1297ICEDiag NoDiag() { return ICEDiag(); }
1298
Eli Friedman90afd3d2009-02-27 04:07:58 +00001299static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1300 Expr::EvalResult EVResult;
1301 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1302 !EVResult.Val.isInt()) {
1303 return ICEDiag(2, E->getLocStart());
1304 }
1305 return NoDiag();
1306}
1307
Eli Friedman98c56a42009-02-26 09:29:13 +00001308static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001309 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001310 if (!E->getType()->isIntegralType()) {
1311 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001312 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001313
1314 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001315#define STMT(Node, Base) case Expr::Node##Class:
1316#define EXPR(Node, Base)
1317#include "clang/AST/StmtNodes.def"
1318 case Expr::PredefinedExprClass:
1319 case Expr::FloatingLiteralClass:
1320 case Expr::ImaginaryLiteralClass:
1321 case Expr::StringLiteralClass:
1322 case Expr::ArraySubscriptExprClass:
1323 case Expr::MemberExprClass:
1324 case Expr::CompoundAssignOperatorClass:
1325 case Expr::CompoundLiteralExprClass:
1326 case Expr::ExtVectorElementExprClass:
1327 case Expr::InitListExprClass:
1328 case Expr::DesignatedInitExprClass:
1329 case Expr::ImplicitValueInitExprClass:
1330 case Expr::ParenListExprClass:
1331 case Expr::VAArgExprClass:
1332 case Expr::AddrLabelExprClass:
1333 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001334 case Expr::CXXMemberCallExprClass:
1335 case Expr::CXXDynamicCastExprClass:
1336 case Expr::CXXTypeidExprClass:
1337 case Expr::CXXNullPtrLiteralExprClass:
1338 case Expr::CXXThisExprClass:
1339 case Expr::CXXThrowExprClass:
1340 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1341 case Expr::CXXNewExprClass:
1342 case Expr::CXXDeleteExprClass:
1343 case Expr::CXXPseudoDestructorExprClass:
1344 case Expr::UnresolvedFunctionNameExprClass:
1345 case Expr::UnresolvedDeclRefExprClass:
1346 case Expr::TemplateIdRefExprClass:
1347 case Expr::CXXConstructExprClass:
1348 case Expr::CXXBindTemporaryExprClass:
1349 case Expr::CXXExprWithTemporariesClass:
1350 case Expr::CXXTemporaryObjectExprClass:
1351 case Expr::CXXUnresolvedConstructExprClass:
1352 case Expr::CXXUnresolvedMemberExprClass:
1353 case Expr::ObjCStringLiteralClass:
1354 case Expr::ObjCEncodeExprClass:
1355 case Expr::ObjCMessageExprClass:
1356 case Expr::ObjCSelectorExprClass:
1357 case Expr::ObjCProtocolExprClass:
1358 case Expr::ObjCIvarRefExprClass:
1359 case Expr::ObjCPropertyRefExprClass:
1360 case Expr::ObjCImplicitSetterGetterRefExprClass:
1361 case Expr::ObjCSuperExprClass:
1362 case Expr::ObjCIsaExprClass:
1363 case Expr::ShuffleVectorExprClass:
1364 case Expr::BlockExprClass:
1365 case Expr::BlockDeclRefExprClass:
1366 case Expr::NoStmtClass:
1367 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001368 return ICEDiag(2, E->getLocStart());
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001369
Douglas Gregor73341c42009-09-11 00:18:58 +00001370 case Expr::GNUNullExprClass:
1371 // GCC considers the GNU __null value to be an integral constant expression.
1372 return NoDiag();
1373
Eli Friedman98c56a42009-02-26 09:29:13 +00001374 case Expr::ParenExprClass:
1375 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1376 case Expr::IntegerLiteralClass:
1377 case Expr::CharacterLiteralClass:
1378 case Expr::CXXBoolLiteralExprClass:
1379 case Expr::CXXZeroInitValueExprClass:
1380 case Expr::TypesCompatibleExprClass:
1381 case Expr::UnaryTypeTraitExprClass:
1382 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001383 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001384 case Expr::CXXOperatorCallExprClass: {
1385 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001386 if (CE->isBuiltinCall(Ctx))
1387 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001388 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001389 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001390 case Expr::DeclRefExprClass:
1391 case Expr::QualifiedDeclRefExprClass:
1392 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1393 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001394 if (Ctx.getLangOptions().CPlusPlus &&
Eli Friedman98c56a42009-02-26 09:29:13 +00001395 E->getType().getCVRQualifiers() == QualType::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001396 // C++ 7.1.5.1p2
1397 // A variable of non-volatile const-qualified integral or enumeration
1398 // type initialized by an ICE can be used in ICEs.
1399 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001400 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001401 if (Dcl->isInitKnownICE()) {
1402 // We have already checked whether this subexpression is an
1403 // integral constant expression.
1404 if (Dcl->isInitICE())
1405 return NoDiag();
1406 else
1407 return ICEDiag(2, E->getLocStart());
1408 }
1409
1410 if (const Expr *Init = Dcl->getInit()) {
1411 ICEDiag Result = CheckICE(Init, Ctx);
1412 // Cache the result of the ICE test.
1413 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1414 return Result;
1415 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001416 }
1417 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001418 return ICEDiag(2, E->getLocStart());
1419 case Expr::UnaryOperatorClass: {
1420 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001421 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001422 case UnaryOperator::PostInc:
1423 case UnaryOperator::PostDec:
1424 case UnaryOperator::PreInc:
1425 case UnaryOperator::PreDec:
1426 case UnaryOperator::AddrOf:
1427 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001428 return ICEDiag(2, E->getLocStart());
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001429
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001430 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001431 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001432 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001433 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001434 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001435 case UnaryOperator::Real:
1436 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001437 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001438 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001439 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1440 // Evaluate matches the proposed gcc behavior for cases like
1441 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1442 // compliance: we should warn earlier for offsetof expressions with
1443 // array subscripts that aren't ICEs, and if the array subscripts
1444 // are ICEs, the value of the offsetof must be an integer constant.
1445 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001446 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001447 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001448 case Expr::SizeOfAlignOfExprClass: {
1449 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1450 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1451 return ICEDiag(2, E->getLocStart());
1452 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001453 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001454 case Expr::BinaryOperatorClass: {
1455 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001456 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001457 case BinaryOperator::PtrMemD:
1458 case BinaryOperator::PtrMemI:
1459 case BinaryOperator::Assign:
1460 case BinaryOperator::MulAssign:
1461 case BinaryOperator::DivAssign:
1462 case BinaryOperator::RemAssign:
1463 case BinaryOperator::AddAssign:
1464 case BinaryOperator::SubAssign:
1465 case BinaryOperator::ShlAssign:
1466 case BinaryOperator::ShrAssign:
1467 case BinaryOperator::AndAssign:
1468 case BinaryOperator::XorAssign:
1469 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001470 return ICEDiag(2, E->getLocStart());
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001471
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001472 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001473 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001474 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001475 case BinaryOperator::Add:
1476 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001477 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001478 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001479 case BinaryOperator::LT:
1480 case BinaryOperator::GT:
1481 case BinaryOperator::LE:
1482 case BinaryOperator::GE:
1483 case BinaryOperator::EQ:
1484 case BinaryOperator::NE:
1485 case BinaryOperator::And:
1486 case BinaryOperator::Xor:
1487 case BinaryOperator::Or:
1488 case BinaryOperator::Comma: {
1489 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1490 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001491 if (Exp->getOpcode() == BinaryOperator::Div ||
1492 Exp->getOpcode() == BinaryOperator::Rem) {
1493 // Evaluate gives an error for undefined Div/Rem, so make sure
1494 // we don't evaluate one.
1495 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1496 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1497 if (REval == 0)
1498 return ICEDiag(1, E->getLocStart());
1499 if (REval.isSigned() && REval.isAllOnesValue()) {
1500 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1501 if (LEval.isMinSignedValue())
1502 return ICEDiag(1, E->getLocStart());
1503 }
1504 }
1505 }
1506 if (Exp->getOpcode() == BinaryOperator::Comma) {
1507 if (Ctx.getLangOptions().C99) {
1508 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1509 // if it isn't evaluated.
1510 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1511 return ICEDiag(1, E->getLocStart());
1512 } else {
1513 // In both C89 and C++, commas in ICEs are illegal.
1514 return ICEDiag(2, E->getLocStart());
1515 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001516 }
1517 if (LHSResult.Val >= RHSResult.Val)
1518 return LHSResult;
1519 return RHSResult;
1520 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001521 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001522 case BinaryOperator::LOr: {
1523 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1524 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1525 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1526 // Rare case where the RHS has a comma "side-effect"; we need
1527 // to actually check the condition to see whether the side
1528 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001529 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001530 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001531 return RHSResult;
1532 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001533 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001534
Eli Friedman98c56a42009-02-26 09:29:13 +00001535 if (LHSResult.Val >= RHSResult.Val)
1536 return LHSResult;
1537 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001538 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001539 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001540 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001541 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001542 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001543 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001544 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001545 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001546 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001547 case Expr::CXXStaticCastExprClass:
1548 case Expr::CXXReinterpretCastExprClass:
1549 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001550 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1551 if (SubExpr->getType()->isIntegralType())
1552 return CheckICE(SubExpr, Ctx);
1553 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1554 return NoDiag();
1555 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001556 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001557 case Expr::ConditionalOperatorClass: {
1558 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001559 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001560 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001561 // expression, and it is fully evaluated. This is an important GNU
1562 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001563 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001564 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001565 Expr::EvalResult EVResult;
1566 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1567 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001568 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001569 }
1570 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001571 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001572 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1573 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1574 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1575 if (CondResult.Val == 2)
1576 return CondResult;
1577 if (TrueResult.Val == 2)
1578 return TrueResult;
1579 if (FalseResult.Val == 2)
1580 return FalseResult;
1581 if (CondResult.Val == 1)
1582 return CondResult;
1583 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1584 return NoDiag();
1585 // Rare case where the diagnostics depend on which side is evaluated
1586 // Note that if we get here, CondResult is 0, and at least one of
1587 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001588 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001589 return FalseResult;
1590 }
1591 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001592 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001593 case Expr::CXXDefaultArgExprClass:
1594 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001595 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001596 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001597 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001598 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001599
1600 // Silence a GCC warning
1601 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001602}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001603
Eli Friedman98c56a42009-02-26 09:29:13 +00001604bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1605 SourceLocation *Loc, bool isEvaluated) const {
1606 ICEDiag d = CheckICE(this, Ctx);
1607 if (d.Val != 0) {
1608 if (Loc) *Loc = d.Loc;
1609 return false;
1610 }
1611 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001612 if (!Evaluate(EvalResult, Ctx))
1613 assert(0 && "ICE cannot be evaluated!");
1614 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1615 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001616 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001617 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001618}
1619
Chris Lattner7eef9192007-05-24 01:23:49 +00001620/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1621/// integer constant expression with the value zero, or if this is one that is
1622/// cast to void*.
Mike Stump11289f42009-09-09 15:08:12 +00001623bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001624 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001625 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001626 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001627 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001628 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001629 QualType Pointee = PT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001630 if (Pointee.getCVRQualifiers() == 0 &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001631 Pointee->isVoidType() && // to void*
1632 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Anders Carlssoneade3ad2008-12-01 06:28:23 +00001633 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001634 }
Steve Naroffada7d422007-05-20 17:54:12 +00001635 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001636 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1637 // Ignore the ImplicitCastExpr type entirely.
Anders Carlssoneade3ad2008-12-01 06:28:23 +00001638 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroff4871fe02008-01-14 16:10:57 +00001639 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1640 // Accept ((void*)0) as a null pointer constant, as many other
1641 // implementations do.
Anders Carlssoneade3ad2008-12-01 06:28:23 +00001642 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001643 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001644 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001645 // See through default argument expressions
Anders Carlssoneade3ad2008-12-01 06:28:23 +00001646 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001647 } else if (isa<GNUNullExpr>(this)) {
1648 // The GNU __null extension is always a null pointer constant.
1649 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001650 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001651
Sebastian Redl576fd422009-05-10 18:38:11 +00001652 // C++0x nullptr_t is always a null pointer constant.
1653 if (getType()->isNullPtrType())
1654 return true;
1655
Steve Naroff4871fe02008-01-14 16:10:57 +00001656 // This expression must be an integer type.
1657 if (!getType()->isIntegerType())
1658 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001659
Chris Lattner1abbd412007-06-08 17:58:43 +00001660 // If we have an integer constant expression, we need to *evaluate* it and
1661 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001662 llvm::APSInt Result;
1663 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001664}
Steve Narofff7a5da12007-07-28 23:10:27 +00001665
Douglas Gregor71235ec2009-05-02 02:18:30 +00001666FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001667 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001668
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001669 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001670 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001671 if (Field->isBitField())
1672 return Field;
1673
1674 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1675 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1676 return BinOp->getLHS()->getBitField();
1677
1678 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001679}
1680
Chris Lattnerb8211f62009-02-16 22:14:05 +00001681/// isArrow - Return true if the base expression is a pointer to vector,
1682/// return false if the base expression is a vector.
1683bool ExtVectorElementExpr::isArrow() const {
1684 return getBase()->getType()->isPointerType();
1685}
1686
Nate Begemance4d7fc2008-04-18 23:10:10 +00001687unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanf322eab2008-05-09 06:41:27 +00001688 if (const VectorType *VT = getType()->getAsVectorType())
1689 return VT->getNumElements();
1690 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001691}
1692
Nate Begemanf322eab2008-05-09 06:41:27 +00001693/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001694bool ExtVectorElementExpr::containsDuplicateElements() const {
Douglas Gregor2ebf87172009-04-15 23:02:49 +00001695 const char *compStr = Accessor->getName();
1696 unsigned length = Accessor->getLength();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001697
1698 // Halving swizzles do not contain duplicate elements.
Mike Stump11289f42009-09-09 15:08:12 +00001699 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
Nate Begeman7e5185b2009-01-18 02:01:21 +00001700 !strcmp(compStr, "even") || !strcmp(compStr, "odd"))
1701 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001702
Nate Begeman7e5185b2009-01-18 02:01:21 +00001703 // Advance past s-char prefix on hex swizzles.
Nate Begeman0359e122009-06-25 21:06:09 +00001704 if (*compStr == 's' || *compStr == 'S') {
Nate Begeman7e5185b2009-01-18 02:01:21 +00001705 compStr++;
1706 length--;
1707 }
Mike Stump11289f42009-09-09 15:08:12 +00001708
Chris Lattner6ef2bc42008-11-19 07:55:04 +00001709 for (unsigned i = 0; i != length-1; i++) {
Steve Naroff0d595ca2007-07-30 03:29:09 +00001710 const char *s = compStr+i;
1711 for (const char c = *s++; *s; s++)
Mike Stump11289f42009-09-09 15:08:12 +00001712 if (c == *s)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001713 return true;
1714 }
1715 return false;
1716}
Chris Lattner885b4952007-08-02 23:36:59 +00001717
Nate Begemanf322eab2008-05-09 06:41:27 +00001718/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001719void ExtVectorElementExpr::getEncodedElementAccess(
1720 llvm::SmallVectorImpl<unsigned> &Elts) const {
Douglas Gregor2ebf87172009-04-15 23:02:49 +00001721 const char *compStr = Accessor->getName();
Nate Begeman0359e122009-06-25 21:06:09 +00001722 if (*compStr == 's' || *compStr == 'S')
Nate Begemanbb70bf62009-01-18 01:47:54 +00001723 compStr++;
Mike Stump11289f42009-09-09 15:08:12 +00001724
Nate Begemanbb70bf62009-01-18 01:47:54 +00001725 bool isHi = !strcmp(compStr, "hi");
1726 bool isLo = !strcmp(compStr, "lo");
1727 bool isEven = !strcmp(compStr, "even");
1728 bool isOdd = !strcmp(compStr, "odd");
Mike Stump11289f42009-09-09 15:08:12 +00001729
Nate Begemanf322eab2008-05-09 06:41:27 +00001730 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1731 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001732
Nate Begemanf322eab2008-05-09 06:41:27 +00001733 if (isHi)
1734 Index = e + i;
1735 else if (isLo)
1736 Index = i;
1737 else if (isEven)
1738 Index = 2 * i;
1739 else if (isOdd)
1740 Index = 2 * i + 1;
1741 else
1742 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001743
Nate Begemand3862152008-05-13 21:03:02 +00001744 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001745 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001746}
1747
Steve Narofff73590d2007-09-27 14:38:14 +00001748// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00001749ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001750 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001751 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001752 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001753 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001754 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001755 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001756 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00001757 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001758 if (NumArgs) {
1759 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001760 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1761 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001762 LBracloc = LBrac;
1763 RBracloc = RBrac;
1764}
1765
Mike Stump11289f42009-09-09 15:08:12 +00001766// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00001767// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00001768ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001769 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001770 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001771 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001772 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001773 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001774 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001775 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001776 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001777 if (NumArgs) {
1778 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001779 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1780 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001781 LBracloc = LBrac;
1782 RBracloc = RBrac;
1783}
1784
Mike Stump11289f42009-09-09 15:08:12 +00001785// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001786ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1787 QualType retType, ObjCMethodDecl *mproto,
1788 SourceLocation LBrac, SourceLocation RBrac,
1789 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001790: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001791MethodProto(mproto) {
1792 NumArgs = nargs;
1793 SubExprs = new Stmt*[NumArgs+1];
1794 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1795 if (NumArgs) {
1796 for (unsigned i = 0; i != NumArgs; ++i)
1797 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1798 }
1799 LBracloc = LBrac;
1800 RBracloc = RBrac;
1801}
1802
1803ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1804 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1805 switch (x & Flags) {
1806 default:
1807 assert(false && "Invalid ObjCMessageExpr.");
1808 case IsInstMeth:
1809 return ClassInfo(0, 0);
1810 case IsClsMethDeclUnknown:
1811 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1812 case IsClsMethDeclKnown: {
1813 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1814 return ClassInfo(D, D->getIdentifier());
1815 }
1816 }
1817}
1818
Chris Lattner7ec71da2009-04-26 00:44:05 +00001819void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
1820 if (CI.first == 0 && CI.second == 0)
1821 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
1822 else if (CI.first == 0)
1823 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
1824 else
1825 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
1826}
1827
1828
Chris Lattner35e564e2007-10-25 00:29:32 +00001829bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001830 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001831}
1832
Nate Begeman48745922009-08-12 02:28:50 +00001833void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1834 unsigned NumExprs) {
1835 if (SubExprs) C.Deallocate(SubExprs);
1836
1837 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001838 this->NumExprs = NumExprs;
1839 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001840}
Nate Begeman48745922009-08-12 02:28:50 +00001841
1842void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1843 DestroyChildren(C);
1844 if (SubExprs) C.Deallocate(SubExprs);
1845 this->~ShuffleVectorExpr();
1846 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00001847}
1848
Douglas Gregore26a2852009-08-07 06:08:38 +00001849void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00001850 // Override default behavior of traversing children. If this has a type
1851 // operand and the type is a variable-length array, the child iteration
1852 // will iterate over the size expression. However, this expression belongs
1853 // to the type, not to this, so we don't want to delete it.
1854 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00001855 if (isArgumentType()) {
1856 this->~SizeOfAlignOfExpr();
1857 C.Deallocate(this);
1858 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001859 else
Douglas Gregore26a2852009-08-07 06:08:38 +00001860 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00001861}
1862
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001863//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001864// DesignatedInitExpr
1865//===----------------------------------------------------------------------===//
1866
1867IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1868 assert(Kind == FieldDesignator && "Only valid on a field designator");
1869 if (Field.NameOrField & 0x01)
1870 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1871 else
1872 return getField()->getIdentifier();
1873}
1874
Mike Stump11289f42009-09-09 15:08:12 +00001875DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001876 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001877 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001878 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001879 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001880 unsigned NumIndexExprs,
1881 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001882 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001883 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001884 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1885 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001886 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001887
1888 // Record the initializer itself.
1889 child_iterator Child = child_begin();
1890 *Child++ = Init;
1891
1892 // Copy the designators and their subexpressions, computing
1893 // value-dependence along the way.
1894 unsigned IndexIdx = 0;
1895 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001896 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001897
1898 if (this->Designators[I].isArrayDesignator()) {
1899 // Compute type- and value-dependence.
1900 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001901 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001902 Index->isTypeDependent() || Index->isValueDependent();
1903
1904 // Copy the index expressions into permanent storage.
1905 *Child++ = IndexExprs[IndexIdx++];
1906 } else if (this->Designators[I].isArrayRangeDesignator()) {
1907 // Compute type- and value-dependence.
1908 Expr *Start = IndexExprs[IndexIdx];
1909 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001910 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001911 Start->isTypeDependent() || Start->isValueDependent() ||
1912 End->isTypeDependent() || End->isValueDependent();
1913
1914 // Copy the start/end expressions into permanent storage.
1915 *Child++ = IndexExprs[IndexIdx++];
1916 *Child++ = IndexExprs[IndexIdx++];
1917 }
1918 }
1919
1920 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001921}
1922
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001923DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001924DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001925 unsigned NumDesignators,
1926 Expr **IndexExprs, unsigned NumIndexExprs,
1927 SourceLocation ColonOrEqualLoc,
1928 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001929 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001930 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001931 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
1932 ColonOrEqualLoc, UsesColonSyntax,
1933 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001934}
1935
Mike Stump11289f42009-09-09 15:08:12 +00001936DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001937 unsigned NumIndexExprs) {
1938 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1939 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1940 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1941}
1942
Mike Stump11289f42009-09-09 15:08:12 +00001943void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001944 unsigned NumDesigs) {
1945 if (Designators)
1946 delete [] Designators;
1947
1948 Designators = new Designator[NumDesigs];
1949 NumDesignators = NumDesigs;
1950 for (unsigned I = 0; I != NumDesigs; ++I)
1951 Designators[I] = Desigs[I];
1952}
1953
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001954SourceRange DesignatedInitExpr::getSourceRange() const {
1955 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00001956 Designator &First =
1957 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001958 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001959 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001960 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1961 else
1962 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1963 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00001964 StartLoc =
1965 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001966 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1967}
1968
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001969Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1970 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1971 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1972 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001973 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1974 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1975}
1976
1977Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001978 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001979 "Requires array range designator");
1980 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1981 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001982 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1983 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1984}
1985
1986Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001987 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001988 "Requires array range designator");
1989 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1990 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001991 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1992 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1993}
1994
Douglas Gregord5846a12009-04-15 06:41:24 +00001995/// \brief Replaces the designator at index @p Idx with the series
1996/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00001997void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
1998 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00001999 const Designator *Last) {
2000 unsigned NumNewDesignators = Last - First;
2001 if (NumNewDesignators == 0) {
2002 std::copy_backward(Designators + Idx + 1,
2003 Designators + NumDesignators,
2004 Designators + Idx);
2005 --NumNewDesignators;
2006 return;
2007 } else if (NumNewDesignators == 1) {
2008 Designators[Idx] = *First;
2009 return;
2010 }
2011
Mike Stump11289f42009-09-09 15:08:12 +00002012 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002013 = new Designator[NumDesignators - 1 + NumNewDesignators];
2014 std::copy(Designators, Designators + Idx, NewDesignators);
2015 std::copy(First, Last, NewDesignators + Idx);
2016 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2017 NewDesignators + Idx + NumNewDesignators);
2018 delete [] Designators;
2019 Designators = NewDesignators;
2020 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2021}
2022
Douglas Gregore26a2852009-08-07 06:08:38 +00002023void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002024 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002025 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002026}
2027
Mike Stump11289f42009-09-09 15:08:12 +00002028ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002029 Expr **exprs, unsigned nexprs,
2030 SourceLocation rparenloc)
2031: Expr(ParenListExprClass, QualType(),
2032 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002033 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002034 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002035
Nate Begeman5ec4b312009-08-10 23:49:36 +00002036 Exprs = new (C) Stmt*[nexprs];
2037 for (unsigned i = 0; i != nexprs; ++i)
2038 Exprs[i] = exprs[i];
2039}
2040
2041void ParenListExpr::DoDestroy(ASTContext& C) {
2042 DestroyChildren(C);
2043 if (Exprs) C.Deallocate(Exprs);
2044 this->~ParenListExpr();
2045 C.Deallocate(this);
2046}
2047
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002048//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002049// ExprIterator.
2050//===----------------------------------------------------------------------===//
2051
2052Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2053Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2054Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2055const Expr* ConstExprIterator::operator[](size_t idx) const {
2056 return cast<Expr>(I[idx]);
2057}
2058const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2059const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2060
2061//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002062// Child Iterators for iterating over subexpressions/substatements
2063//===----------------------------------------------------------------------===//
2064
2065// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002066Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2067Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002068
Steve Naroffe46504b2007-11-12 14:29:37 +00002069// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002070Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2071Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002072
Steve Naroffebf4cb42008-06-02 23:03:37 +00002073// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002074Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2075Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002076
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002077// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002078Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2079 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002080}
Mike Stump11289f42009-09-09 15:08:12 +00002081Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2082 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002083}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002084
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002085// ObjCSuperExpr
2086Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2087Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2088
Steve Naroffe87026a2009-07-24 17:54:45 +00002089// ObjCIsaExpr
2090Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2091Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2092
Chris Lattner6307f192008-08-10 01:53:14 +00002093// PredefinedExpr
2094Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2095Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002096
2097// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002098Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2099Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002100
2101// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002102Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002103Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002104
2105// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002106Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2107Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002108
Chris Lattner1c20a172007-08-26 03:42:43 +00002109// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002110Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2111Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002112
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002113// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002114Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2115Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002116
2117// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002118Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2119Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002120
2121// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002122Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2123Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002124
Sebastian Redl6f282892008-11-11 17:56:53 +00002125// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002126Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002127 // If this is of a type and the type is a VLA type (and not a typedef), the
2128 // size expression of the VLA needs to be treated as an executable expression.
2129 // Why isn't this weirdness documented better in StmtIterator?
2130 if (isArgumentType()) {
2131 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2132 getArgumentType().getTypePtr()))
2133 return child_iterator(T);
2134 return child_iterator();
2135 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002136 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002137}
Sebastian Redl6f282892008-11-11 17:56:53 +00002138Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2139 if (isArgumentType())
2140 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002141 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002142}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002143
2144// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002145Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002146 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002147}
Ted Kremenek23702b62007-08-24 20:06:47 +00002148Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002149 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002150}
2151
2152// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002153Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002154 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002155}
Ted Kremenek23702b62007-08-24 20:06:47 +00002156Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002157 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002158}
Ted Kremenek23702b62007-08-24 20:06:47 +00002159
2160// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002161Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2162Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002163
Nate Begemance4d7fc2008-04-18 23:10:10 +00002164// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002165Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2166Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002167
2168// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002169Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2170Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002171
Ted Kremenek23702b62007-08-24 20:06:47 +00002172// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002173Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2174Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002175
2176// BinaryOperator
2177Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002178 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002179}
Ted Kremenek23702b62007-08-24 20:06:47 +00002180Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002181 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002182}
2183
2184// ConditionalOperator
2185Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002186 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002187}
Ted Kremenek23702b62007-08-24 20:06:47 +00002188Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002189 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002190}
2191
2192// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002193Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2194Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002195
Ted Kremenek23702b62007-08-24 20:06:47 +00002196// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002197Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2198Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002199
2200// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002201Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2202 return child_iterator();
2203}
2204
2205Stmt::child_iterator TypesCompatibleExpr::child_end() {
2206 return child_iterator();
2207}
Ted Kremenek23702b62007-08-24 20:06:47 +00002208
2209// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002210Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2211Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002212
Douglas Gregor3be4b122008-11-29 04:51:27 +00002213// GNUNullExpr
2214Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2215Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2216
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002217// ShuffleVectorExpr
2218Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002219 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002220}
2221Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002222 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002223}
2224
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002225// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002226Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2227Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002228
Anders Carlsson4692db02007-08-31 04:56:16 +00002229// InitListExpr
2230Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002231 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002232}
2233Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002234 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002235}
2236
Douglas Gregor0202cb42009-01-29 17:44:32 +00002237// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002238Stmt::child_iterator DesignatedInitExpr::child_begin() {
2239 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2240 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002241 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2242}
2243Stmt::child_iterator DesignatedInitExpr::child_end() {
2244 return child_iterator(&*child_begin() + NumSubExprs);
2245}
2246
Douglas Gregor0202cb42009-01-29 17:44:32 +00002247// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002248Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2249 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002250}
2251
Mike Stump11289f42009-09-09 15:08:12 +00002252Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2253 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002254}
2255
Nate Begeman5ec4b312009-08-10 23:49:36 +00002256// ParenListExpr
2257Stmt::child_iterator ParenListExpr::child_begin() {
2258 return &Exprs[0];
2259}
2260Stmt::child_iterator ParenListExpr::child_end() {
2261 return &Exprs[0]+NumExprs;
2262}
2263
Ted Kremenek23702b62007-08-24 20:06:47 +00002264// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002265Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002266 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002267}
2268Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002269 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002270}
Ted Kremenek23702b62007-08-24 20:06:47 +00002271
2272// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002273Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2274Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002275
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002276// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002277Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002278 return child_iterator();
2279}
2280Stmt::child_iterator ObjCSelectorExpr::child_end() {
2281 return child_iterator();
2282}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002283
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002284// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002285Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2286 return child_iterator();
2287}
2288Stmt::child_iterator ObjCProtocolExpr::child_end() {
2289 return child_iterator();
2290}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002291
Steve Naroffd54978b2007-09-18 23:55:05 +00002292// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002293Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002294 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002295}
2296Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002297 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002298}
2299
Steve Naroffc540d662008-09-03 18:15:37 +00002300// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002301Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2302Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002303
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002304Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2305Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }