blob: 1f6c2fab1b8ec3de327cc0497885ad59459489b6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000030#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattner2b334bb2010-04-16 23:34:13 +000033/// isKnownToHaveBooleanValue - Return true if this is an integer expression
34/// that is known to return 0 or 1. This happens for _Bool/bool expressions
35/// but also int expressions which are produced by things like comparisons in
36/// C.
37bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000038 const Expr *E = IgnoreParens();
39
Chris Lattner2b334bb2010-04-16 23:34:13 +000040 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000041 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000044
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000046 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000047 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 return UO->getSubExpr()->isKnownToHaveBooleanValue();
49 default:
50 return false;
51 }
52 }
Sean Huntc3021132010-05-05 15:23:54 +000053
John McCall6907fbe2010-06-12 01:56:02 +000054 // Only look through implicit casts. If the user writes
55 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000056 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000057 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000058
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 switch (BO->getOpcode()) {
61 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000062 case BO_LT: // Relational operators.
63 case BO_GT:
64 case BO_LE:
65 case BO_GE:
66 case BO_EQ: // Equality operators.
67 case BO_NE:
68 case BO_LAnd: // AND operator.
69 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000070 return true;
Sean Huntc3021132010-05-05 15:23:54 +000071
John McCall2de56d12010-08-25 11:45:40 +000072 case BO_And: // Bitwise AND operator.
73 case BO_Xor: // Bitwise XOR operator.
74 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000075 // Handle things like (x==2)|(y==12).
76 return BO->getLHS()->isKnownToHaveBooleanValue() &&
77 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000078
John McCall2de56d12010-08-25 11:45:40 +000079 case BO_Comma:
80 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000081 return BO->getRHS()->isKnownToHaveBooleanValue();
82 }
83 }
Sean Huntc3021132010-05-05 15:23:54 +000084
Peter Collingbournef111d932011-04-15 00:35:48 +000085 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
87 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000088
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return false;
90}
91
John McCall63c00d72011-02-09 08:16:59 +000092// Amusing macro metaprogramming hack: check whether a class provides
93// a more specific implementation of getExprLoc().
94namespace {
95 /// This implementation is used when a class provides a custom
96 /// implementation of getExprLoc.
97 template <class E, class T>
98 SourceLocation getExprLocImpl(const Expr *expr,
99 SourceLocation (T::*v)() const) {
100 return static_cast<const E*>(expr)->getExprLoc();
101 }
102
103 /// This implementation is used when a class doesn't provide
104 /// a custom implementation of getExprLoc. Overload resolution
105 /// should pick it over the implementation above because it's
106 /// more specialized according to function template partial ordering.
107 template <class E>
108 SourceLocation getExprLocImpl(const Expr *expr,
109 SourceLocation (Expr::*v)() const) {
110 return static_cast<const E*>(expr)->getSourceRange().getBegin();
111 }
112}
113
114SourceLocation Expr::getExprLoc() const {
115 switch (getStmtClass()) {
116 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
117#define ABSTRACT_STMT(type)
118#define STMT(type, base) \
119 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
120#define EXPR(type, base) \
121 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
122#include "clang/AST/StmtNodes.inc"
123 }
124 llvm_unreachable("unknown statement kind");
125 return SourceLocation();
126}
127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128//===----------------------------------------------------------------------===//
129// Primary Expressions.
130//===----------------------------------------------------------------------===//
131
John McCalld5532b62009-11-23 01:53:49 +0000132void ExplicitTemplateArgumentList::initializeFrom(
133 const TemplateArgumentListInfo &Info) {
134 LAngleLoc = Info.getLAngleLoc();
135 RAngleLoc = Info.getRAngleLoc();
136 NumTemplateArgs = Info.size();
137
138 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
139 for (unsigned i = 0; i != NumTemplateArgs; ++i)
140 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
141}
142
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000143void ExplicitTemplateArgumentList::initializeFrom(
144 const TemplateArgumentListInfo &Info,
145 bool &Dependent,
146 bool &ContainsUnexpandedParameterPack) {
147 LAngleLoc = Info.getLAngleLoc();
148 RAngleLoc = Info.getRAngleLoc();
149 NumTemplateArgs = Info.size();
150
151 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
152 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
153 Dependent = Dependent || Info[i].getArgument().isDependent();
154 ContainsUnexpandedParameterPack
155 = ContainsUnexpandedParameterPack ||
156 Info[i].getArgument().containsUnexpandedParameterPack();
157
158 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
159 }
160}
161
John McCalld5532b62009-11-23 01:53:49 +0000162void ExplicitTemplateArgumentList::copyInto(
163 TemplateArgumentListInfo &Info) const {
164 Info.setLAngleLoc(LAngleLoc);
165 Info.setRAngleLoc(RAngleLoc);
166 for (unsigned I = 0; I != NumTemplateArgs; ++I)
167 Info.addArgument(getTemplateArgs()[I]);
168}
169
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000170std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
171 return sizeof(ExplicitTemplateArgumentList) +
172 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
173}
174
John McCalld5532b62009-11-23 01:53:49 +0000175std::size_t ExplicitTemplateArgumentList::sizeFor(
176 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000177 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000178}
179
Douglas Gregord967e312011-01-19 21:52:31 +0000180/// \brief Compute the type- and value-dependence of a declaration reference
181/// based on the declaration being referenced.
182static void computeDeclRefDependence(NamedDecl *D, QualType T,
183 bool &TypeDependent,
184 bool &ValueDependent) {
185 TypeDependent = false;
186 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188
189 // (TD) C++ [temp.dep.expr]p3:
190 // An id-expression is type-dependent if it contains:
191 //
Sean Huntc3021132010-05-05 15:23:54 +0000192 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000193 //
194 // (VD) C++ [temp.dep.constexpr]p2:
195 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregor0da76df2009-11-23 11:41:28 +0000197 // (TD) - an identifier that was declared with dependent type
198 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000199 if (T->isDependentType()) {
200 TypeDependent = true;
201 ValueDependent = true;
202 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000203 }
Douglas Gregord967e312011-01-19 21:52:31 +0000204
Douglas Gregor0da76df2009-11-23 11:41:28 +0000205 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000206 if (D->getDeclName().getNameKind()
207 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000208 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000209 TypeDependent = true;
210 ValueDependent = true;
211 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000212 }
213 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000214 if (isa<NonTypeTemplateParmDecl>(D)) {
215 ValueDependent = true;
216 return;
217 }
218
Douglas Gregor0da76df2009-11-23 11:41:28 +0000219 // (VD) - a constant with integral or enumeration type and is
220 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000221 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000222 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000223 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000224 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000225 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000226 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000227 }
Douglas Gregord967e312011-01-19 21:52:31 +0000228
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000229 // (VD) - FIXME: Missing from the standard:
230 // - a member function or a static data member of the current
231 // instantiation
232 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000233 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000234 ValueDependent = true;
235
236 return;
237 }
238
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000239 // (VD) - FIXME: Missing from the standard:
240 // - a member function or a static data member of the current
241 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000242 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
243 ValueDependent = true;
244 return;
245 }
246}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247
Douglas Gregord967e312011-01-19 21:52:31 +0000248void DeclRefExpr::computeDependence() {
249 bool TypeDependent = false;
250 bool ValueDependent = false;
251 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
252
253 // (TD) C++ [temp.dep.expr]p3:
254 // An id-expression is type-dependent if it contains:
255 //
256 // and
257 //
258 // (VD) C++ [temp.dep.constexpr]p2:
259 // An identifier is value-dependent if it is:
260 if (!TypeDependent && !ValueDependent &&
261 hasExplicitTemplateArgs() &&
262 TemplateSpecializationType::anyDependentTemplateArguments(
263 getTemplateArgs(),
264 getNumTemplateArgs())) {
265 TypeDependent = true;
266 ValueDependent = true;
267 }
268
269 ExprBits.TypeDependent = TypeDependent;
270 ExprBits.ValueDependent = ValueDependent;
271
Douglas Gregor10738d32010-12-23 23:51:58 +0000272 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000273 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000274 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000275}
276
Douglas Gregor40d96a62011-02-28 21:54:11 +0000277DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000278 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000279 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000280 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000281 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000282 DecoratedD(D,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000283 (QualifierLoc? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000284 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000285 Loc(NameLoc) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000286 if (QualifierLoc) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000287 NameQualifier *NQ = getNameQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000288 NQ->QualifierLoc = QualifierLoc;
Douglas Gregora2813ce2009-10-23 18:54:35 +0000289 }
Sean Huntc3021132010-05-05 15:23:54 +0000290
John McCalld5532b62009-11-23 01:53:49 +0000291 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000292 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293
294 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000295}
296
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 ValueDecl *D, const DeclarationNameInfo &NameInfo,
299 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000300 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000301 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000302 DecoratedD(D,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000303 (QualifierLoc? HasQualifierFlag : 0) |
Abramo Bagnara25777432010-08-11 22:01:17 +0000304 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
305 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000306 if (QualifierLoc) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 NameQualifier *NQ = getNameQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000308 NQ->QualifierLoc = QualifierLoc;
Abramo Bagnara25777432010-08-11 22:01:17 +0000309 }
310
311 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000312 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000313
314 computeDependence();
315}
316
Douglas Gregora2813ce2009-10-23 18:54:35 +0000317DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000318 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000319 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000320 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000321 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000322 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000323 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000324 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000325 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000326 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000327}
328
329DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000330 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000331 ValueDecl *D,
332 const DeclarationNameInfo &NameInfo,
333 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000334 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000335 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000336 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000337 if (QualifierLoc != 0)
Douglas Gregora2813ce2009-10-23 18:54:35 +0000338 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000339
John McCalld5532b62009-11-23 01:53:49 +0000340 if (TemplateArgs)
341 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000342
Chris Lattner32488542010-10-30 05:14:06 +0000343 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Douglas Gregor40d96a62011-02-28 21:54:11 +0000344 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000345}
346
Douglas Gregordef03542011-02-04 12:01:24 +0000347DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
348 bool HasQualifier,
349 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 unsigned NumTemplateArgs) {
351 std::size_t Size = sizeof(DeclRefExpr);
352 if (HasQualifier)
353 Size += sizeof(NameQualifier);
354
Douglas Gregordef03542011-02-04 12:01:24 +0000355 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000356 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
357
Chris Lattner32488542010-10-30 05:14:06 +0000358 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000359 return new (Mem) DeclRefExpr(EmptyShell());
360}
361
Douglas Gregora2813ce2009-10-23 18:54:35 +0000362SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000363 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000364 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000365 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000366 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000367 R.setEnd(getRAngleLoc());
368 return R;
369}
370
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371// FIXME: Maybe this should use DeclPrinter with a special "print predefined
372// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000373std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
374 ASTContext &Context = CurrentDecl->getASTContext();
375
Anders Carlsson3a082d82009-09-08 18:24:21 +0000376 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000377 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000378 return FD->getNameAsString();
379
380 llvm::SmallString<256> Name;
381 llvm::raw_svector_ostream Out(Name);
382
383 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000384 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000385 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000386 if (MD->isStatic())
387 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000388 }
389
390 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000391
392 std::string Proto = FD->getQualifiedNameAsString(Policy);
393
John McCall183700f2009-09-21 23:43:11 +0000394 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000395 const FunctionProtoType *FT = 0;
396 if (FD->hasWrittenPrototype())
397 FT = dyn_cast<FunctionProtoType>(AFT);
398
399 Proto += "(";
400 if (FT) {
401 llvm::raw_string_ostream POut(Proto);
402 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
403 if (i) POut << ", ";
404 std::string Param;
405 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
406 POut << Param;
407 }
408
409 if (FT->isVariadic()) {
410 if (FD->getNumParams()) POut << ", ";
411 POut << "...";
412 }
413 }
414 Proto += ")";
415
Sam Weinig4eadcc52009-12-27 01:38:20 +0000416 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
417 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
418 if (ThisQuals.hasConst())
419 Proto += " const";
420 if (ThisQuals.hasVolatile())
421 Proto += " volatile";
422 }
423
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000424 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
425 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426
427 Out << Proto;
428
429 Out.flush();
430 return Name.str().str();
431 }
432 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
433 llvm::SmallString<256> Name;
434 llvm::raw_svector_ostream Out(Name);
435 Out << (MD->isInstanceMethod() ? '-' : '+');
436 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000437
438 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
439 // a null check to avoid a crash.
440 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000441 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000442
Anders Carlsson3a082d82009-09-08 18:24:21 +0000443 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000444 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
445 Out << '(' << CID << ')';
446
Anders Carlsson3a082d82009-09-08 18:24:21 +0000447 Out << ' ';
448 Out << MD->getSelector().getAsString();
449 Out << ']';
450
451 Out.flush();
452 return Name.str().str();
453 }
454 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
455 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
456 return "top level";
457 }
458 return "";
459}
460
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000461void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
462 if (hasAllocation())
463 C.Deallocate(pVal);
464
465 BitWidth = Val.getBitWidth();
466 unsigned NumWords = Val.getNumWords();
467 const uint64_t* Words = Val.getRawData();
468 if (NumWords > 1) {
469 pVal = new (C) uint64_t[NumWords];
470 std::copy(Words, Words + NumWords, pVal);
471 } else if (NumWords == 1)
472 VAL = Words[0];
473 else
474 VAL = 0;
475}
476
477IntegerLiteral *
478IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
479 QualType type, SourceLocation l) {
480 return new (C) IntegerLiteral(C, V, type, l);
481}
482
483IntegerLiteral *
484IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
485 return new (C) IntegerLiteral(Empty);
486}
487
488FloatingLiteral *
489FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
490 bool isexact, QualType Type, SourceLocation L) {
491 return new (C) FloatingLiteral(C, V, isexact, Type, L);
492}
493
494FloatingLiteral *
495FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
496 return new (C) FloatingLiteral(Empty);
497}
498
Chris Lattnerda8249e2008-06-07 22:13:43 +0000499/// getValueAsApproximateDouble - This returns the value as an inaccurate
500/// double. Note that this may cause loss of precision, but is useful for
501/// debugging dumps, etc.
502double FloatingLiteral::getValueAsApproximateDouble() const {
503 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000504 bool ignored;
505 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
506 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000507 return V.convertToDouble();
508}
509
Chris Lattner2085fd62009-02-18 06:40:38 +0000510StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
511 unsigned ByteLength, bool Wide,
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000512 bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000513 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000514 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000515 // Allocate enough space for the StringLiteral plus an array of locations for
516 // any concatenated string tokens.
517 void *Mem = C.Allocate(sizeof(StringLiteral)+
518 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000519 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000520 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000523 char *AStrData = new (C, 1) char[ByteLength];
524 memcpy(AStrData, StrData, ByteLength);
525 SL->StrData = AStrData;
526 SL->ByteLength = ByteLength;
527 SL->IsWide = Wide;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000528 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000529 SL->TokLocs[0] = Loc[0];
530 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000531
Chris Lattner726e1682009-02-18 05:49:11 +0000532 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000533 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
534 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000535}
536
Douglas Gregor673ecd62009-04-15 16:35:07 +0000537StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
538 void *Mem = C.Allocate(sizeof(StringLiteral)+
539 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000540 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000541 StringLiteral *SL = new (Mem) StringLiteral(QualType());
542 SL->StrData = 0;
543 SL->ByteLength = 0;
544 SL->NumConcatenated = NumStrs;
545 return SL;
546}
547
Daniel Dunbarb6480232009-09-22 03:27:33 +0000548void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000549 char *AStrData = new (C, 1) char[Str.size()];
550 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000551 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000552 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000553}
554
Chris Lattner08f92e32010-11-17 07:37:15 +0000555/// getLocationOfByte - Return a source location that points to the specified
556/// byte of this string literal.
557///
558/// Strings are amazingly complex. They can be formed from multiple tokens and
559/// can have escape sequences in them in addition to the usual trigraph and
560/// escaped newline business. This routine handles this complexity.
561///
562SourceLocation StringLiteral::
563getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
564 const LangOptions &Features, const TargetInfo &Target) const {
565 assert(!isWide() && "This doesn't work for wide strings yet");
566
567 // Loop over all of the tokens in this string until we find the one that
568 // contains the byte we're looking for.
569 unsigned TokNo = 0;
570 while (1) {
571 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
572 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
573
574 // Get the spelling of the string so that we can get the data that makes up
575 // the string literal, not the identifier for the macro it is potentially
576 // expanded through.
577 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
578
579 // Re-lex the token to get its length and original spelling.
580 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
581 bool Invalid = false;
582 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
583 if (Invalid)
584 return StrTokSpellingLoc;
585
586 const char *StrData = Buffer.data()+LocInfo.second;
587
588 // Create a langops struct and enable trigraphs. This is sufficient for
589 // relexing tokens.
590 LangOptions LangOpts;
591 LangOpts.Trigraphs = true;
592
593 // Create a lexer starting at the beginning of this token.
594 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
595 Buffer.end());
596 Token TheTok;
597 TheLexer.LexFromRawLexer(TheTok);
598
599 // Use the StringLiteralParser to compute the length of the string in bytes.
600 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
601 unsigned TokNumBytes = SLP.GetStringLength();
602
603 // If the byte is in this token, return the location of the byte.
604 if (ByteNo < TokNumBytes ||
605 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
606 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
607
608 // Now that we know the offset of the token in the spelling, use the
609 // preprocessor to get the offset in the original source.
610 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
611 }
612
613 // Move to the next string token.
614 ++TokNo;
615 ByteNo -= TokNumBytes;
616 }
617}
618
619
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
622/// corresponds to, e.g. "sizeof" or "[pre]++".
623const char *UnaryOperator::getOpcodeStr(Opcode Op) {
624 switch (Op) {
625 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000626 case UO_PostInc: return "++";
627 case UO_PostDec: return "--";
628 case UO_PreInc: return "++";
629 case UO_PreDec: return "--";
630 case UO_AddrOf: return "&";
631 case UO_Deref: return "*";
632 case UO_Plus: return "+";
633 case UO_Minus: return "-";
634 case UO_Not: return "~";
635 case UO_LNot: return "!";
636 case UO_Real: return "__real";
637 case UO_Imag: return "__imag";
638 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 }
640}
641
John McCall2de56d12010-08-25 11:45:40 +0000642UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000643UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
644 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000645 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000646 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
647 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
648 case OO_Amp: return UO_AddrOf;
649 case OO_Star: return UO_Deref;
650 case OO_Plus: return UO_Plus;
651 case OO_Minus: return UO_Minus;
652 case OO_Tilde: return UO_Not;
653 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000654 }
655}
656
657OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
658 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000659 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
660 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
661 case UO_AddrOf: return OO_Amp;
662 case UO_Deref: return OO_Star;
663 case UO_Plus: return OO_Plus;
664 case UO_Minus: return OO_Minus;
665 case UO_Not: return OO_Tilde;
666 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000667 default: return OO_None;
668 }
669}
670
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672//===----------------------------------------------------------------------===//
673// Postfix Operators.
674//===----------------------------------------------------------------------===//
675
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000676CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
677 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000678 SourceLocation rparenloc)
679 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000680 fn->isTypeDependent(),
681 fn->isValueDependent(),
682 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000683 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000685 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000686 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000687 for (unsigned i = 0; i != numargs; ++i) {
688 if (args[i]->isTypeDependent())
689 ExprBits.TypeDependent = true;
690 if (args[i]->isValueDependent())
691 ExprBits.ValueDependent = true;
692 if (args[i]->containsUnexpandedParameterPack())
693 ExprBits.ContainsUnexpandedParameterPack = true;
694
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000695 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000696 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000697
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000698 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000699 RParenLoc = rparenloc;
700}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000701
Ted Kremenek668bf912009-02-09 20:51:47 +0000702CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000703 QualType t, ExprValueKind VK, SourceLocation rparenloc)
704 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000705 fn->isTypeDependent(),
706 fn->isValueDependent(),
707 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000708 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000709
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000710 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000711 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000712 for (unsigned i = 0; i != numargs; ++i) {
713 if (args[i]->isTypeDependent())
714 ExprBits.TypeDependent = true;
715 if (args[i]->isValueDependent())
716 ExprBits.ValueDependent = true;
717 if (args[i]->containsUnexpandedParameterPack())
718 ExprBits.ContainsUnexpandedParameterPack = true;
719
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000720 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000721 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000722
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000723 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 RParenLoc = rparenloc;
725}
726
Mike Stump1eb44332009-09-09 15:08:12 +0000727CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
728 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000729 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000730 SubExprs = new (C) Stmt*[PREARGS_START];
731 CallExprBits.NumPreArgs = 0;
732}
733
734CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
735 EmptyShell Empty)
736 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
737 // FIXME: Why do we allocate this?
738 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
739 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000740}
741
Nuno Lopesd20254f2009-12-20 23:11:08 +0000742Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000743 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000744 // If we're calling a dereference, look at the pointer instead.
745 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
746 if (BO->isPtrMemOp())
747 CEE = BO->getRHS()->IgnoreParenCasts();
748 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
749 if (UO->getOpcode() == UO_Deref)
750 CEE = UO->getSubExpr()->IgnoreParenCasts();
751 }
Chris Lattner6346f962009-07-17 15:46:27 +0000752 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000753 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000754 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
755 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000756
757 return 0;
758}
759
Nuno Lopesd20254f2009-12-20 23:11:08 +0000760FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000761 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000762}
763
Chris Lattnerd18b3292007-12-28 05:25:02 +0000764/// setNumArgs - This changes the number of arguments present in this call.
765/// Any orphaned expressions are deleted by this, and any new operands are set
766/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000767void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000768 // No change, just return.
769 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattnerd18b3292007-12-28 05:25:02 +0000771 // If shrinking # arguments, just delete the extras and forgot them.
772 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000773 this->NumArgs = NumArgs;
774 return;
775 }
776
777 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000778 unsigned NumPreArgs = getNumPreArgs();
779 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000780 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000781 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000782 NewSubExprs[i] = SubExprs[i];
783 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000784 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
785 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000786 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor88c9a462009-04-17 21:46:47 +0000788 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000789 SubExprs = NewSubExprs;
790 this->NumArgs = NumArgs;
791}
792
Chris Lattnercb888962008-10-06 05:00:53 +0000793/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
794/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000795unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000796 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000797 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000798 // ImplicitCastExpr.
799 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
800 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000801 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000803 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
804 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000805 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Anders Carlssonbcba2012008-01-31 02:13:57 +0000807 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
808 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000809 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000811 if (!FDecl->getIdentifier())
812 return 0;
813
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000814 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000815}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000816
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000817QualType CallExpr::getCallReturnType() const {
818 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000819 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000820 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000821 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000822 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000823 else if (const MemberPointerType *MPT
824 = CalleeType->getAs<MemberPointerType>())
825 CalleeType = MPT->getPointeeType();
826
John McCall183700f2009-09-21 23:43:11 +0000827 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000828 return FnType->getResultType();
829}
Chris Lattnercb888962008-10-06 05:00:53 +0000830
John McCall2882eca2011-02-21 06:23:05 +0000831SourceRange CallExpr::getSourceRange() const {
832 if (isa<CXXOperatorCallExpr>(this))
833 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
834
835 SourceLocation begin = getCallee()->getLocStart();
836 if (begin.isInvalid() && getNumArgs() > 0)
837 begin = getArg(0)->getLocStart();
838 SourceLocation end = getRParenLoc();
839 if (end.isInvalid() && getNumArgs() > 0)
840 end = getArg(getNumArgs() - 1)->getLocEnd();
841 return SourceRange(begin, end);
842}
843
Sean Huntc3021132010-05-05 15:23:54 +0000844OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000845 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000846 TypeSourceInfo *tsi,
847 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000848 Expr** exprsPtr, unsigned numExprs,
849 SourceLocation RParenLoc) {
850 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000851 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000852 sizeof(Expr*) * numExprs);
853
854 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
855 exprsPtr, numExprs, RParenLoc);
856}
857
858OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
859 unsigned numComps, unsigned numExprs) {
860 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
861 sizeof(OffsetOfNode) * numComps +
862 sizeof(Expr*) * numExprs);
863 return new (Mem) OffsetOfExpr(numComps, numExprs);
864}
865
Sean Huntc3021132010-05-05 15:23:54 +0000866OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000867 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000868 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000869 Expr** exprsPtr, unsigned numExprs,
870 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000871 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
872 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000873 /*ValueDependent=*/tsi->getType()->isDependentType(),
874 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000875 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
876 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000877{
878 for(unsigned i = 0; i < numComps; ++i) {
879 setComponent(i, compsPtr[i]);
880 }
Sean Huntc3021132010-05-05 15:23:54 +0000881
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000882 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000883 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
884 ExprBits.ValueDependent = true;
885 if (exprsPtr[i]->containsUnexpandedParameterPack())
886 ExprBits.ContainsUnexpandedParameterPack = true;
887
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000888 setIndexExpr(i, exprsPtr[i]);
889 }
890}
891
892IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
893 assert(getKind() == Field || getKind() == Identifier);
894 if (getKind() == Field)
895 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000896
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000897 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
898}
899
Mike Stump1eb44332009-09-09 15:08:12 +0000900MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000901 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000902 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000903 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000904 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000905 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000906 QualType ty,
907 ExprValueKind vk,
908 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000909 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000910
Douglas Gregor40d96a62011-02-28 21:54:11 +0000911 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000912 founddecl.getDecl() != memberdecl ||
913 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000914 if (hasQualOrFound)
915 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
John McCalld5532b62009-11-23 01:53:49 +0000917 if (targs)
918 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner32488542010-10-30 05:14:06 +0000920 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000921 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
922 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000923
924 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000925 // FIXME: Wrong. We should be looking at the member declaration we found.
926 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000927 E->setValueDependent(true);
928 E->setTypeDependent(true);
929 }
930 E->HasQualifierOrFoundDecl = true;
931
932 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000933 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000934 NQ->FoundDecl = founddecl;
935 }
936
937 if (targs) {
938 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000939 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000940 }
941
942 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000943}
944
Douglas Gregor75e85042011-03-02 21:06:53 +0000945SourceRange MemberExpr::getSourceRange() const {
946 SourceLocation StartLoc;
947 if (isImplicitAccess()) {
948 if (hasQualifier())
949 StartLoc = getQualifierLoc().getBeginLoc();
950 else
951 StartLoc = MemberLoc;
952 } else {
953 // FIXME: We don't want this to happen. Rather, we should be able to
954 // detect all kinds of implicit accesses more cleanly.
955 StartLoc = getBase()->getLocStart();
956 if (StartLoc.isInvalid())
957 StartLoc = MemberLoc;
958 }
959
960 SourceLocation EndLoc =
961 HasExplicitTemplateArgumentList? getRAngleLoc()
962 : getMemberNameInfo().getEndLoc();
963
964 return SourceRange(StartLoc, EndLoc);
965}
966
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000967const char *CastExpr::getCastKindName() const {
968 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000969 case CK_Dependent:
970 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000971 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000972 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000973 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000974 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000975 case CK_LValueToRValue:
976 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000977 case CK_GetObjCProperty:
978 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000979 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000980 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000981 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000982 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000983 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000984 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000985 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000986 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000987 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000988 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000989 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000990 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000991 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000992 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000993 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000994 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000995 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000996 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000997 case CK_NullToPointer:
998 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000999 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001000 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001001 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001002 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001003 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001004 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001005 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001006 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001007 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001008 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001009 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001010 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001011 case CK_PointerToBoolean:
1012 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001013 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001014 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001015 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001016 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001017 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001018 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001019 case CK_IntegralToBoolean:
1020 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001021 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001022 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001023 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001024 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001025 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001026 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001027 case CK_FloatingToBoolean:
1028 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001029 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001030 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001031 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001032 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001033 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001034 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001035 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001036 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001037 case CK_FloatingRealToComplex:
1038 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001039 case CK_FloatingComplexToReal:
1040 return "FloatingComplexToReal";
1041 case CK_FloatingComplexToBoolean:
1042 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001043 case CK_FloatingComplexCast:
1044 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001045 case CK_FloatingComplexToIntegralComplex:
1046 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001047 case CK_IntegralRealToComplex:
1048 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001049 case CK_IntegralComplexToReal:
1050 return "IntegralComplexToReal";
1051 case CK_IntegralComplexToBoolean:
1052 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001053 case CK_IntegralComplexCast:
1054 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001055 case CK_IntegralComplexToFloatingComplex:
1056 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
John McCall2bb5d002010-11-13 09:02:35 +00001059 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001060 return 0;
1061}
1062
Douglas Gregor6eef5192009-12-14 19:27:10 +00001063Expr *CastExpr::getSubExprAsWritten() {
1064 Expr *SubExpr = 0;
1065 CastExpr *E = this;
1066 do {
1067 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001068
Douglas Gregor6eef5192009-12-14 19:27:10 +00001069 // Skip any temporary bindings; they're implicit.
1070 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1071 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001072
Douglas Gregor6eef5192009-12-14 19:27:10 +00001073 // Conversions by constructor and conversion functions have a
1074 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001075 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001076 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001077 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001078 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001079
Douglas Gregor6eef5192009-12-14 19:27:10 +00001080 // If the subexpression we're left with is an implicit cast, look
1081 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001082 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1083
Douglas Gregor6eef5192009-12-14 19:27:10 +00001084 return SubExpr;
1085}
1086
John McCallf871d0c2010-08-07 06:22:56 +00001087CXXBaseSpecifier **CastExpr::path_buffer() {
1088 switch (getStmtClass()) {
1089#define ABSTRACT_STMT(x)
1090#define CASTEXPR(Type, Base) \
1091 case Stmt::Type##Class: \
1092 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1093#define STMT(Type, Base)
1094#include "clang/AST/StmtNodes.inc"
1095 default:
1096 llvm_unreachable("non-cast expressions not possible here");
1097 return 0;
1098 }
1099}
1100
1101void CastExpr::setCastPath(const CXXCastPath &Path) {
1102 assert(Path.size() == path_size());
1103 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1104}
1105
1106ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1107 CastKind Kind, Expr *Operand,
1108 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001109 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001110 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1111 void *Buffer =
1112 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1113 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001114 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001115 if (PathSize) E->setCastPath(*BasePath);
1116 return E;
1117}
1118
1119ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1120 unsigned PathSize) {
1121 void *Buffer =
1122 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1123 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1124}
1125
1126
1127CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001128 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001129 const CXXCastPath *BasePath,
1130 TypeSourceInfo *WrittenTy,
1131 SourceLocation L, SourceLocation R) {
1132 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1133 void *Buffer =
1134 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1135 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001136 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001137 if (PathSize) E->setCastPath(*BasePath);
1138 return E;
1139}
1140
1141CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1142 void *Buffer =
1143 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1144 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1145}
1146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1148/// corresponds to, e.g. "<<=".
1149const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1150 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001151 case BO_PtrMemD: return ".*";
1152 case BO_PtrMemI: return "->*";
1153 case BO_Mul: return "*";
1154 case BO_Div: return "/";
1155 case BO_Rem: return "%";
1156 case BO_Add: return "+";
1157 case BO_Sub: return "-";
1158 case BO_Shl: return "<<";
1159 case BO_Shr: return ">>";
1160 case BO_LT: return "<";
1161 case BO_GT: return ">";
1162 case BO_LE: return "<=";
1163 case BO_GE: return ">=";
1164 case BO_EQ: return "==";
1165 case BO_NE: return "!=";
1166 case BO_And: return "&";
1167 case BO_Xor: return "^";
1168 case BO_Or: return "|";
1169 case BO_LAnd: return "&&";
1170 case BO_LOr: return "||";
1171 case BO_Assign: return "=";
1172 case BO_MulAssign: return "*=";
1173 case BO_DivAssign: return "/=";
1174 case BO_RemAssign: return "%=";
1175 case BO_AddAssign: return "+=";
1176 case BO_SubAssign: return "-=";
1177 case BO_ShlAssign: return "<<=";
1178 case BO_ShrAssign: return ">>=";
1179 case BO_AndAssign: return "&=";
1180 case BO_XorAssign: return "^=";
1181 case BO_OrAssign: return "|=";
1182 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001184
1185 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001186}
1187
John McCall2de56d12010-08-25 11:45:40 +00001188BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001189BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1190 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001191 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001192 case OO_Plus: return BO_Add;
1193 case OO_Minus: return BO_Sub;
1194 case OO_Star: return BO_Mul;
1195 case OO_Slash: return BO_Div;
1196 case OO_Percent: return BO_Rem;
1197 case OO_Caret: return BO_Xor;
1198 case OO_Amp: return BO_And;
1199 case OO_Pipe: return BO_Or;
1200 case OO_Equal: return BO_Assign;
1201 case OO_Less: return BO_LT;
1202 case OO_Greater: return BO_GT;
1203 case OO_PlusEqual: return BO_AddAssign;
1204 case OO_MinusEqual: return BO_SubAssign;
1205 case OO_StarEqual: return BO_MulAssign;
1206 case OO_SlashEqual: return BO_DivAssign;
1207 case OO_PercentEqual: return BO_RemAssign;
1208 case OO_CaretEqual: return BO_XorAssign;
1209 case OO_AmpEqual: return BO_AndAssign;
1210 case OO_PipeEqual: return BO_OrAssign;
1211 case OO_LessLess: return BO_Shl;
1212 case OO_GreaterGreater: return BO_Shr;
1213 case OO_LessLessEqual: return BO_ShlAssign;
1214 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1215 case OO_EqualEqual: return BO_EQ;
1216 case OO_ExclaimEqual: return BO_NE;
1217 case OO_LessEqual: return BO_LE;
1218 case OO_GreaterEqual: return BO_GE;
1219 case OO_AmpAmp: return BO_LAnd;
1220 case OO_PipePipe: return BO_LOr;
1221 case OO_Comma: return BO_Comma;
1222 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001223 }
1224}
1225
1226OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1227 static const OverloadedOperatorKind OverOps[] = {
1228 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1229 OO_Star, OO_Slash, OO_Percent,
1230 OO_Plus, OO_Minus,
1231 OO_LessLess, OO_GreaterGreater,
1232 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1233 OO_EqualEqual, OO_ExclaimEqual,
1234 OO_Amp,
1235 OO_Caret,
1236 OO_Pipe,
1237 OO_AmpAmp,
1238 OO_PipePipe,
1239 OO_Equal, OO_StarEqual,
1240 OO_SlashEqual, OO_PercentEqual,
1241 OO_PlusEqual, OO_MinusEqual,
1242 OO_LessLessEqual, OO_GreaterGreaterEqual,
1243 OO_AmpEqual, OO_CaretEqual,
1244 OO_PipeEqual,
1245 OO_Comma
1246 };
1247 return OverOps[Opc];
1248}
1249
Ted Kremenek709210f2010-04-13 23:39:13 +00001250InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001251 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001252 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001253 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1254 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001255 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001256 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001257 UnionFieldInit(0), HadArrayRangeDesignator(false)
1258{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001259 for (unsigned I = 0; I != numInits; ++I) {
1260 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001261 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001262 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001263 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001264 if (initExprs[I]->containsUnexpandedParameterPack())
1265 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001266 }
Sean Huntc3021132010-05-05 15:23:54 +00001267
Ted Kremenek709210f2010-04-13 23:39:13 +00001268 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001269}
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
Ted Kremenek709210f2010-04-13 23:39:13 +00001271void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001272 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001273 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001274}
1275
Ted Kremenek709210f2010-04-13 23:39:13 +00001276void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001277 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001278}
1279
Ted Kremenek709210f2010-04-13 23:39:13 +00001280Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001281 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001282 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001283 InitExprs.back() = expr;
1284 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Douglas Gregor4c678342009-01-28 21:54:33 +00001287 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1288 InitExprs[Init] = expr;
1289 return Result;
1290}
1291
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001292SourceRange InitListExpr::getSourceRange() const {
1293 if (SyntacticForm)
1294 return SyntacticForm->getSourceRange();
1295 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1296 if (Beg.isInvalid()) {
1297 // Find the first non-null initializer.
1298 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1299 E = InitExprs.end();
1300 I != E; ++I) {
1301 if (Stmt *S = *I) {
1302 Beg = S->getLocStart();
1303 break;
1304 }
1305 }
1306 }
1307 if (End.isInvalid()) {
1308 // Find the first non-null initializer from the end.
1309 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1310 E = InitExprs.rend();
1311 I != E; ++I) {
1312 if (Stmt *S = *I) {
1313 End = S->getSourceRange().getEnd();
1314 break;
1315 }
1316 }
1317 }
1318 return SourceRange(Beg, End);
1319}
1320
Steve Naroffbfdcae62008-09-04 15:31:07 +00001321/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001322///
1323const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001324 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001325 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001326}
1327
Mike Stump1eb44332009-09-09 15:08:12 +00001328SourceLocation BlockExpr::getCaretLocation() const {
1329 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001330}
Mike Stump1eb44332009-09-09 15:08:12 +00001331const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001332 return TheBlock->getBody();
1333}
Mike Stump1eb44332009-09-09 15:08:12 +00001334Stmt *BlockExpr::getBody() {
1335 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001336}
Steve Naroff56ee6892008-10-08 17:01:13 +00001337
1338
Reid Spencer5f016e22007-07-11 17:01:13 +00001339//===----------------------------------------------------------------------===//
1340// Generic Expression Routines
1341//===----------------------------------------------------------------------===//
1342
Chris Lattner026dc962009-02-14 07:37:35 +00001343/// isUnusedResultAWarning - Return true if this immediate expression should
1344/// be warned about if the result is unused. If so, fill in Loc and Ranges
1345/// with location to warn on and the source range[s] to report with the
1346/// warning.
1347bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001348 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001349 // Don't warn if the expr is type dependent. The type could end up
1350 // instantiating to void.
1351 if (isTypeDependent())
1352 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 switch (getStmtClass()) {
1355 default:
John McCall0faede62010-03-12 07:11:26 +00001356 if (getType()->isVoidType())
1357 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001358 Loc = getExprLoc();
1359 R1 = getSourceRange();
1360 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001362 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001363 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001364 case GenericSelectionExprClass:
1365 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1366 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 case UnaryOperatorClass: {
1368 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001371 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001372 case UO_PostInc:
1373 case UO_PostDec:
1374 case UO_PreInc:
1375 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001376 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001377 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001379 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001380 return false;
1381 break;
John McCall2de56d12010-08-25 11:45:40 +00001382 case UO_Real:
1383 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001385 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1386 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001387 return false;
1388 break;
John McCall2de56d12010-08-25 11:45:40 +00001389 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001390 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 }
Chris Lattner026dc962009-02-14 07:37:35 +00001392 Loc = UO->getOperatorLoc();
1393 R1 = UO->getSubExpr()->getSourceRange();
1394 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001396 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001397 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001398 switch (BO->getOpcode()) {
1399 default:
1400 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001401 // Consider the RHS of comma for side effects. LHS was checked by
1402 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001403 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001404 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1405 // lvalue-ness) of an assignment written in a macro.
1406 if (IntegerLiteral *IE =
1407 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1408 if (IE->getValue() == 0)
1409 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001410 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1411 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001412 case BO_LAnd:
1413 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001414 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1415 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1416 return false;
1417 break;
John McCallbf0ee352010-02-16 04:10:53 +00001418 }
Chris Lattner026dc962009-02-14 07:37:35 +00001419 if (BO->isAssignmentOp())
1420 return false;
1421 Loc = BO->getOperatorLoc();
1422 R1 = BO->getLHS()->getSourceRange();
1423 R2 = BO->getRHS()->getSourceRange();
1424 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001425 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001426 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001427 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001428 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001429
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001430 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001431 // If only one of the LHS or RHS is a warning, the operator might
1432 // be being used for control flow. Only warn if both the LHS and
1433 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001434 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001435 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1436 return false;
1437 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001438 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001439 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001440 }
1441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001443 // If the base pointer or element is to a volatile pointer/field, accessing
1444 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001445 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001446 return false;
1447 Loc = cast<MemberExpr>(this)->getMemberLoc();
1448 R1 = SourceRange(Loc, Loc);
1449 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1450 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 case ArraySubscriptExprClass:
1453 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001454 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001455 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001456 return false;
1457 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1458 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1459 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1460 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001461
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001463 case CXXOperatorCallExprClass:
1464 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001465 // If this is a direct call, get the callee.
1466 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001467 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001468 // If the callee has attribute pure, const, or warn_unused_result, warn
1469 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001470 //
1471 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1472 // updated to match for QoI.
1473 if (FD->getAttr<WarnUnusedResultAttr>() ||
1474 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1475 Loc = CE->getCallee()->getLocStart();
1476 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001478 if (unsigned NumArgs = CE->getNumArgs())
1479 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1480 CE->getArg(NumArgs-1)->getLocEnd());
1481 return true;
1482 }
Chris Lattner026dc962009-02-14 07:37:35 +00001483 }
1484 return false;
1485 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001486
1487 case CXXTemporaryObjectExprClass:
1488 case CXXConstructExprClass:
1489 return false;
1490
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001491 case ObjCMessageExprClass: {
1492 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1493 const ObjCMethodDecl *MD = ME->getMethodDecl();
1494 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1495 Loc = getExprLoc();
1496 return true;
1497 }
Chris Lattner026dc962009-02-14 07:37:35 +00001498 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001499 }
Mike Stump1eb44332009-09-09 15:08:12 +00001500
John McCall12f78a62010-12-02 01:19:52 +00001501 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001502 Loc = getExprLoc();
1503 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001504 return true;
John McCall12f78a62010-12-02 01:19:52 +00001505
Chris Lattner611b2ec2008-07-26 19:51:01 +00001506 case StmtExprClass: {
1507 // Statement exprs don't logically have side effects themselves, but are
1508 // sometimes used in macros in ways that give them a type that is unused.
1509 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1510 // however, if the result of the stmt expr is dead, we don't want to emit a
1511 // warning.
1512 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001513 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001514 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001515 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001516 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1517 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1518 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
John McCall0faede62010-03-12 07:11:26 +00001521 if (getType()->isVoidType())
1522 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001523 Loc = cast<StmtExpr>(this)->getLParenLoc();
1524 R1 = getSourceRange();
1525 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001526 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001527 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001528 // If this is an explicit cast to void, allow it. People do this when they
1529 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001530 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001531 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001532 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1533 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1534 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001535 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001536 if (getType()->isVoidType())
1537 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001538 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001539
Anders Carlsson58beed92009-11-17 17:11:23 +00001540 // If this is a cast to void or a constructor conversion, check the operand.
1541 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001542 if (CE->getCastKind() == CK_ToVoid ||
1543 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001544 return (cast<CastExpr>(this)->getSubExpr()
1545 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001546 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1547 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1548 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Eli Friedman4be1f472008-05-19 21:24:43 +00001551 case ImplicitCastExprClass:
1552 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return (cast<ImplicitCastExpr>(this)
1554 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001555
Chris Lattner04421082008-04-08 04:40:51 +00001556 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001557 return (cast<CXXDefaultArgExpr>(this)
1558 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001559
1560 case CXXNewExprClass:
1561 // FIXME: In theory, there might be new expressions that don't have side
1562 // effects (e.g. a placement new with an uninitialized POD).
1563 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001564 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001565 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001566 return (cast<CXXBindTemporaryExpr>(this)
1567 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001568 case ExprWithCleanupsClass:
1569 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001570 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001571 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001572}
1573
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001574/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001575/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001576bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001577 const Expr *E = IgnoreParens();
1578 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001579 default:
1580 return false;
1581 case ObjCIvarRefExprClass:
1582 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001583 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001584 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001585 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001586 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001587 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001588 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001589 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001590 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001591 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1592 if (VD->hasGlobalStorage())
1593 return true;
1594 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001595 // dereferencing to a pointer is always a gc'able candidate,
1596 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001597 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001598 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001599 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001600 return false;
1601 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001602 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001603 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001604 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001605 }
1606 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001607 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001608 }
1609}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001610
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001611bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1612 if (isTypeDependent())
1613 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001614 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001615}
1616
Sebastian Redl369e51f2010-09-10 20:55:33 +00001617static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1618 Expr::CanThrowResult CT2) {
1619 // CanThrowResult constants are ordered so that the maximum is the correct
1620 // merge result.
1621 return CT1 > CT2 ? CT1 : CT2;
1622}
1623
1624static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1625 Expr *E = const_cast<Expr*>(CE);
1626 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001627 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001628 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1629 }
1630 return R;
1631}
1632
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001633static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001634 bool NullThrows = true) {
1635 if (!D)
1636 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1637
1638 // See if we can get a function type from the decl somehow.
1639 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1640 if (!VD) // If we have no clue what we're calling, assume the worst.
1641 return Expr::CT_Can;
1642
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001643 // As an extension, we assume that __attribute__((nothrow)) functions don't
1644 // throw.
1645 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1646 return Expr::CT_Cannot;
1647
Sebastian Redl369e51f2010-09-10 20:55:33 +00001648 QualType T = VD->getType();
1649 const FunctionProtoType *FT;
1650 if ((FT = T->getAs<FunctionProtoType>())) {
1651 } else if (const PointerType *PT = T->getAs<PointerType>())
1652 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1653 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1654 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1655 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1656 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1657 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1658 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1659
1660 if (!FT)
1661 return Expr::CT_Can;
1662
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001663 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001664}
1665
1666static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1667 if (DC->isTypeDependent())
1668 return Expr::CT_Dependent;
1669
Sebastian Redl295995c2010-09-10 20:55:47 +00001670 if (!DC->getTypeAsWritten()->isReferenceType())
1671 return Expr::CT_Cannot;
1672
Sebastian Redl369e51f2010-09-10 20:55:33 +00001673 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1674}
1675
1676static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1677 const CXXTypeidExpr *DC) {
1678 if (DC->isTypeOperand())
1679 return Expr::CT_Cannot;
1680
1681 Expr *Op = DC->getExprOperand();
1682 if (Op->isTypeDependent())
1683 return Expr::CT_Dependent;
1684
1685 const RecordType *RT = Op->getType()->getAs<RecordType>();
1686 if (!RT)
1687 return Expr::CT_Cannot;
1688
1689 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1690 return Expr::CT_Cannot;
1691
1692 if (Op->Classify(C).isPRValue())
1693 return Expr::CT_Cannot;
1694
1695 return Expr::CT_Can;
1696}
1697
1698Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1699 // C++ [expr.unary.noexcept]p3:
1700 // [Can throw] if in a potentially-evaluated context the expression would
1701 // contain:
1702 switch (getStmtClass()) {
1703 case CXXThrowExprClass:
1704 // - a potentially evaluated throw-expression
1705 return CT_Can;
1706
1707 case CXXDynamicCastExprClass: {
1708 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1709 // where T is a reference type, that requires a run-time check
1710 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1711 if (CT == CT_Can)
1712 return CT;
1713 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1714 }
1715
1716 case CXXTypeidExprClass:
1717 // - a potentially evaluated typeid expression applied to a glvalue
1718 // expression whose type is a polymorphic class type
1719 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1720
1721 // - a potentially evaluated call to a function, member function, function
1722 // pointer, or member function pointer that does not have a non-throwing
1723 // exception-specification
1724 case CallExprClass:
1725 case CXXOperatorCallExprClass:
1726 case CXXMemberCallExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001727 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001728 if (CT == CT_Can)
1729 return CT;
1730 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1731 }
1732
Sebastian Redl295995c2010-09-10 20:55:47 +00001733 case CXXConstructExprClass:
1734 case CXXTemporaryObjectExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001735 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001736 cast<CXXConstructExpr>(this)->getConstructor());
1737 if (CT == CT_Can)
1738 return CT;
1739 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1740 }
1741
1742 case CXXNewExprClass: {
1743 CanThrowResult CT = MergeCanThrow(
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001744 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1745 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001746 /*NullThrows*/false));
1747 if (CT == CT_Can)
1748 return CT;
1749 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1750 }
1751
1752 case CXXDeleteExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001753 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001754 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1755 if (CT == CT_Can)
1756 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001757 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1758 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1759 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1760 Arg = Cast->getSubExpr();
1761 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1762 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001763 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001764 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1765 if (CT2 == CT_Can)
1766 return CT2;
1767 CT = MergeCanThrow(CT, CT2);
1768 }
1769 }
1770 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1771 }
1772
1773 case CXXBindTemporaryExprClass: {
1774 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001775 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001776 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1777 if (CT == CT_Can)
1778 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001779 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1780 }
1781
1782 // ObjC message sends are like function calls, but never have exception
1783 // specs.
1784 case ObjCMessageExprClass:
1785 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001786 return CT_Can;
1787
1788 // Many other things have subexpressions, so we have to test those.
1789 // Some are simple:
1790 case ParenExprClass:
1791 case MemberExprClass:
1792 case CXXReinterpretCastExprClass:
1793 case CXXConstCastExprClass:
1794 case ConditionalOperatorClass:
1795 case CompoundLiteralExprClass:
1796 case ExtVectorElementExprClass:
1797 case InitListExprClass:
1798 case DesignatedInitExprClass:
1799 case ParenListExprClass:
1800 case VAArgExprClass:
1801 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001802 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001803 case ObjCIvarRefExprClass:
1804 case ObjCIsaExprClass:
1805 case ShuffleVectorExprClass:
1806 return CanSubExprsThrow(C, this);
1807
1808 // Some might be dependent for other reasons.
1809 case UnaryOperatorClass:
1810 case ArraySubscriptExprClass:
1811 case ImplicitCastExprClass:
1812 case CStyleCastExprClass:
1813 case CXXStaticCastExprClass:
1814 case CXXFunctionalCastExprClass:
1815 case BinaryOperatorClass:
1816 case CompoundAssignOperatorClass: {
1817 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1818 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1819 }
1820
1821 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1822 case StmtExprClass:
1823 return CT_Can;
1824
1825 case ChooseExprClass:
1826 if (isTypeDependent() || isValueDependent())
1827 return CT_Dependent;
1828 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1829
Peter Collingbournef111d932011-04-15 00:35:48 +00001830 case GenericSelectionExprClass:
1831 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1832 return CT_Dependent;
1833 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1834
Sebastian Redl369e51f2010-09-10 20:55:33 +00001835 // Some expressions are always dependent.
1836 case DependentScopeDeclRefExprClass:
1837 case CXXUnresolvedConstructExprClass:
1838 case CXXDependentScopeMemberExprClass:
1839 return CT_Dependent;
1840
1841 default:
1842 // All other expressions don't have subexpressions, or else they are
1843 // unevaluated.
1844 return CT_Cannot;
1845 }
1846}
1847
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001848Expr* Expr::IgnoreParens() {
1849 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001850 while (true) {
1851 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1852 E = P->getSubExpr();
1853 continue;
1854 }
1855 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1856 if (P->getOpcode() == UO_Extension) {
1857 E = P->getSubExpr();
1858 continue;
1859 }
1860 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001861 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1862 if (!P->isResultDependent()) {
1863 E = P->getResultExpr();
1864 continue;
1865 }
1866 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001867 return E;
1868 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001869}
1870
Chris Lattner56f34942008-02-13 01:02:39 +00001871/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1872/// or CastExprs or ImplicitCastExprs, returning their operand.
1873Expr *Expr::IgnoreParenCasts() {
1874 Expr *E = this;
1875 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001876 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001877 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001878 continue;
1879 }
1880 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001881 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001882 continue;
1883 }
1884 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1885 if (P->getOpcode() == UO_Extension) {
1886 E = P->getSubExpr();
1887 continue;
1888 }
1889 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001890 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1891 if (!P->isResultDependent()) {
1892 E = P->getResultExpr();
1893 continue;
1894 }
1895 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001896 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001897 }
1898}
1899
John McCall9c5d70c2010-12-04 08:24:19 +00001900/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1901/// casts. This is intended purely as a temporary workaround for code
1902/// that hasn't yet been rewritten to do the right thing about those
1903/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001904Expr *Expr::IgnoreParenLValueCasts() {
1905 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001906 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001907 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1908 E = P->getSubExpr();
1909 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001910 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001911 if (P->getCastKind() == CK_LValueToRValue) {
1912 E = P->getSubExpr();
1913 continue;
1914 }
John McCall9c5d70c2010-12-04 08:24:19 +00001915 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1916 if (P->getOpcode() == UO_Extension) {
1917 E = P->getSubExpr();
1918 continue;
1919 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001920 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1921 if (!P->isResultDependent()) {
1922 E = P->getResultExpr();
1923 continue;
1924 }
John McCallf6a16482010-12-04 03:47:34 +00001925 }
1926 break;
1927 }
1928 return E;
1929}
1930
John McCall2fc46bf2010-05-05 22:59:52 +00001931Expr *Expr::IgnoreParenImpCasts() {
1932 Expr *E = this;
1933 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001934 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001935 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001936 continue;
1937 }
1938 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001939 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001940 continue;
1941 }
1942 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1943 if (P->getOpcode() == UO_Extension) {
1944 E = P->getSubExpr();
1945 continue;
1946 }
1947 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001948 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1949 if (!P->isResultDependent()) {
1950 E = P->getResultExpr();
1951 continue;
1952 }
1953 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001954 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001955 }
1956}
1957
Chris Lattnerecdd8412009-03-13 17:28:01 +00001958/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1959/// value (including ptr->int casts of the same size). Strip off any
1960/// ParenExpr or CastExprs, returning their operand.
1961Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1962 Expr *E = this;
1963 while (true) {
1964 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1965 E = P->getSubExpr();
1966 continue;
1967 }
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Chris Lattnerecdd8412009-03-13 17:28:01 +00001969 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1970 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001971 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001972 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Chris Lattnerecdd8412009-03-13 17:28:01 +00001974 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1975 E = SE;
1976 continue;
1977 }
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001979 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001980 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001981 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001982 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001983 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1984 E = SE;
1985 continue;
1986 }
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001989 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1990 if (P->getOpcode() == UO_Extension) {
1991 E = P->getSubExpr();
1992 continue;
1993 }
1994 }
1995
Peter Collingbournef111d932011-04-15 00:35:48 +00001996 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1997 if (!P->isResultDependent()) {
1998 E = P->getResultExpr();
1999 continue;
2000 }
2001 }
2002
Chris Lattnerecdd8412009-03-13 17:28:01 +00002003 return E;
2004 }
2005}
2006
Douglas Gregor6eef5192009-12-14 19:27:10 +00002007bool Expr::isDefaultArgument() const {
2008 const Expr *E = this;
2009 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2010 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002011
Douglas Gregor6eef5192009-12-14 19:27:10 +00002012 return isa<CXXDefaultArgExpr>(E);
2013}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002014
Douglas Gregor2f599792010-04-02 18:24:57 +00002015/// \brief Skip over any no-op casts and any temporary-binding
2016/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002017static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002018 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002019 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002020 E = ICE->getSubExpr();
2021 else
2022 break;
2023 }
2024
2025 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2026 E = BE->getSubExpr();
2027
2028 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002029 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002030 E = ICE->getSubExpr();
2031 else
2032 break;
2033 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002034
2035 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002036}
2037
John McCall558d2ab2010-09-15 10:14:12 +00002038/// isTemporaryObject - Determines if this expression produces a
2039/// temporary of the given class type.
2040bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2041 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2042 return false;
2043
Anders Carlssonf8b30152010-11-28 16:40:49 +00002044 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002045
John McCall58277b52010-09-15 20:59:13 +00002046 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002047 if (!E->Classify(C).isPRValue()) {
2048 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002049 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002050 return false;
2051 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002052
John McCall19e60ad2010-09-16 06:57:56 +00002053 // Black-list a few cases which yield pr-values of class type that don't
2054 // refer to temporaries of that type:
2055
2056 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002057 if (isa<ImplicitCastExpr>(E)) {
2058 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2059 case CK_DerivedToBase:
2060 case CK_UncheckedDerivedToBase:
2061 return false;
2062 default:
2063 break;
2064 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002065 }
2066
John McCall19e60ad2010-09-16 06:57:56 +00002067 // - member expressions (all)
2068 if (isa<MemberExpr>(E))
2069 return false;
2070
John McCall56ca35d2011-02-17 10:25:35 +00002071 // - opaque values (all)
2072 if (isa<OpaqueValueExpr>(E))
2073 return false;
2074
John McCall558d2ab2010-09-15 10:14:12 +00002075 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002076}
2077
Douglas Gregor75e85042011-03-02 21:06:53 +00002078bool Expr::isImplicitCXXThis() const {
2079 const Expr *E = this;
2080
2081 // Strip away parentheses and casts we don't care about.
2082 while (true) {
2083 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2084 E = Paren->getSubExpr();
2085 continue;
2086 }
2087
2088 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2089 if (ICE->getCastKind() == CK_NoOp ||
2090 ICE->getCastKind() == CK_LValueToRValue ||
2091 ICE->getCastKind() == CK_DerivedToBase ||
2092 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2093 E = ICE->getSubExpr();
2094 continue;
2095 }
2096 }
2097
2098 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2099 if (UnOp->getOpcode() == UO_Extension) {
2100 E = UnOp->getSubExpr();
2101 continue;
2102 }
2103 }
2104
2105 break;
2106 }
2107
2108 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2109 return This->isImplicit();
2110
2111 return false;
2112}
2113
Douglas Gregor898574e2008-12-05 23:32:09 +00002114/// hasAnyTypeDependentArguments - Determines if any of the expressions
2115/// in Exprs is type-dependent.
2116bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2117 for (unsigned I = 0; I < NumExprs; ++I)
2118 if (Exprs[I]->isTypeDependent())
2119 return true;
2120
2121 return false;
2122}
2123
2124/// hasAnyValueDependentArguments - Determines if any of the expressions
2125/// in Exprs is value-dependent.
2126bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2127 for (unsigned I = 0; I < NumExprs; ++I)
2128 if (Exprs[I]->isValueDependent())
2129 return true;
2130
2131 return false;
2132}
2133
John McCall4204f072010-08-02 21:13:48 +00002134bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002135 // This function is attempting whether an expression is an initializer
2136 // which can be evaluated at compile-time. isEvaluatable handles most
2137 // of the cases, but it can't deal with some initializer-specific
2138 // expressions, and it can't deal with aggregates; we deal with those here,
2139 // and fall back to isEvaluatable for the other cases.
2140
John McCall4204f072010-08-02 21:13:48 +00002141 // If we ever capture reference-binding directly in the AST, we can
2142 // kill the second parameter.
2143
2144 if (IsForRef) {
2145 EvalResult Result;
2146 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2147 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002148
Anders Carlssone8a32b82008-11-24 05:23:59 +00002149 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002150 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002151 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002152 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002153 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002154 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002155 case CXXTemporaryObjectExprClass:
2156 case CXXConstructExprClass: {
2157 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002158
2159 // Only if it's
2160 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002161 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002162 if (!CE->getNumArgs()) return true;
2163
2164 // 2) an elidable trivial copy construction of an operand which is
2165 // itself a constant initializer. Note that we consider the
2166 // operand on its own, *not* as a reference binding.
2167 return CE->isElidable() &&
2168 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002169 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002170 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002171 // This handles gcc's extension that allows global initializers like
2172 // "struct x {int x;} x = (struct x) {};".
2173 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002174 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002175 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002176 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002177 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002178 // FIXME: This doesn't deal with fields with reference types correctly.
2179 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2180 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002181 const InitListExpr *Exp = cast<InitListExpr>(this);
2182 unsigned numInits = Exp->getNumInits();
2183 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002184 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002185 return false;
2186 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002187 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002188 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002189 case ImplicitValueInitExprClass:
2190 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002191 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002192 return cast<ParenExpr>(this)->getSubExpr()
2193 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002194 case GenericSelectionExprClass:
2195 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2196 return false;
2197 return cast<GenericSelectionExpr>(this)->getResultExpr()
2198 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002199 case ChooseExprClass:
2200 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2201 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002202 case UnaryOperatorClass: {
2203 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002204 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002205 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002206 break;
2207 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002208 case BinaryOperatorClass: {
2209 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2210 // but this handles the common case.
2211 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002212 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002213 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2214 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2215 return true;
2216 break;
2217 }
John McCall4204f072010-08-02 21:13:48 +00002218 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002219 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002220 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002221 case CStyleCastExprClass:
2222 // Handle casts with a destination that's a struct or union; this
2223 // deals with both the gcc no-op struct cast extension and the
2224 // cast-to-union extension.
2225 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002226 return cast<CastExpr>(this)->getSubExpr()
2227 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002228
Chris Lattner430656e2009-10-13 22:12:09 +00002229 // Integer->integer casts can be handled here, which is important for
2230 // things like (int)(&&x-&&y). Scary but true.
2231 if (getType()->isIntegerType() &&
2232 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002233 return cast<CastExpr>(this)->getSubExpr()
2234 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002235
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002236 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002237 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002238 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002239}
2240
Chandler Carruth82214a82011-02-18 23:54:50 +00002241/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2242/// pointer constant or not, as well as the specific kind of constant detected.
2243/// Null pointer constants can be integer constant expressions with the
2244/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2245/// (a GNU extension).
2246Expr::NullPointerConstantKind
2247Expr::isNullPointerConstant(ASTContext &Ctx,
2248 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002249 if (isValueDependent()) {
2250 switch (NPC) {
2251 case NPC_NeverValueDependent:
2252 assert(false && "Unexpected value dependent expression!");
2253 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002254
Douglas Gregorce940492009-09-25 04:25:58 +00002255 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002256 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2257 return NPCK_ZeroInteger;
2258 else
2259 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002260
Douglas Gregorce940492009-09-25 04:25:58 +00002261 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002262 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002263 }
2264 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002265
Sebastian Redl07779722008-10-31 14:43:28 +00002266 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002267 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002268 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002269 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002270 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002271 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002272 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002273 Pointee->isVoidType() && // to void*
2274 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002275 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002276 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002278 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2279 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002280 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002281 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2282 // Accept ((void*)0) as a null pointer constant, as many other
2283 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002284 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002285 } else if (const GenericSelectionExpr *GE =
2286 dyn_cast<GenericSelectionExpr>(this)) {
2287 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002288 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002289 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002290 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002291 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002292 } else if (isa<GNUNullExpr>(this)) {
2293 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002294 return NPCK_GNUNull;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002295 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002296
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002297 // C++0x nullptr_t is always a null pointer constant.
2298 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002299 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002300
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002301 if (const RecordType *UT = getType()->getAsUnionType())
2302 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2303 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2304 const Expr *InitExpr = CLE->getInitializer();
2305 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2306 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2307 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002308 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002309 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002310 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002311 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Reid Spencer5f016e22007-07-11 17:01:13 +00002313 // If we have an integer constant expression, we need to *evaluate* it and
2314 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002315 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002316 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2317
2318 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002319}
Steve Naroff31a45842007-07-28 23:10:27 +00002320
John McCallf6a16482010-12-04 03:47:34 +00002321/// \brief If this expression is an l-value for an Objective C
2322/// property, find the underlying property reference expression.
2323const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2324 const Expr *E = this;
2325 while (true) {
2326 assert((E->getValueKind() == VK_LValue &&
2327 E->getObjectKind() == OK_ObjCProperty) &&
2328 "expression is not a property reference");
2329 E = E->IgnoreParenCasts();
2330 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2331 if (BO->getOpcode() == BO_Comma) {
2332 E = BO->getRHS();
2333 continue;
2334 }
2335 }
2336
2337 break;
2338 }
2339
2340 return cast<ObjCPropertyRefExpr>(E);
2341}
2342
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002343FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002344 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002345
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002346 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002347 if (ICE->getCastKind() == CK_LValueToRValue ||
2348 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002349 E = ICE->getSubExpr()->IgnoreParens();
2350 else
2351 break;
2352 }
2353
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002354 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002355 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002356 if (Field->isBitField())
2357 return Field;
2358
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002359 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2360 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2361 if (Field->isBitField())
2362 return Field;
2363
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002364 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2365 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2366 return BinOp->getLHS()->getBitField();
2367
2368 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002369}
2370
Anders Carlsson09380262010-01-31 17:18:49 +00002371bool Expr::refersToVectorElement() const {
2372 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002373
Anders Carlsson09380262010-01-31 17:18:49 +00002374 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002375 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002376 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002377 E = ICE->getSubExpr()->IgnoreParens();
2378 else
2379 break;
2380 }
Sean Huntc3021132010-05-05 15:23:54 +00002381
Anders Carlsson09380262010-01-31 17:18:49 +00002382 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2383 return ASE->getBase()->getType()->isVectorType();
2384
2385 if (isa<ExtVectorElementExpr>(E))
2386 return true;
2387
2388 return false;
2389}
2390
Chris Lattner2140e902009-02-16 22:14:05 +00002391/// isArrow - Return true if the base expression is a pointer to vector,
2392/// return false if the base expression is a vector.
2393bool ExtVectorElementExpr::isArrow() const {
2394 return getBase()->getType()->isPointerType();
2395}
2396
Nate Begeman213541a2008-04-18 23:10:10 +00002397unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002398 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002399 return VT->getNumElements();
2400 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002401}
2402
Nate Begeman8a997642008-05-09 06:41:27 +00002403/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002404bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002405 // FIXME: Refactor this code to an accessor on the AST node which returns the
2406 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002407 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002408
2409 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002410 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002411 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002412
Nate Begeman190d6a22009-01-18 02:01:21 +00002413 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002414 if (Comp[0] == 's' || Comp[0] == 'S')
2415 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Daniel Dunbar15027422009-10-17 23:53:04 +00002417 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2418 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002419 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002420
Steve Narofffec0b492007-07-30 03:29:09 +00002421 return false;
2422}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002423
Nate Begeman8a997642008-05-09 06:41:27 +00002424/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002425void ExtVectorElementExpr::getEncodedElementAccess(
2426 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002427 llvm::StringRef Comp = Accessor->getName();
2428 if (Comp[0] == 's' || Comp[0] == 'S')
2429 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002431 bool isHi = Comp == "hi";
2432 bool isLo = Comp == "lo";
2433 bool isEven = Comp == "even";
2434 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Nate Begeman8a997642008-05-09 06:41:27 +00002436 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2437 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Nate Begeman8a997642008-05-09 06:41:27 +00002439 if (isHi)
2440 Index = e + i;
2441 else if (isLo)
2442 Index = i;
2443 else if (isEven)
2444 Index = 2 * i;
2445 else if (isOdd)
2446 Index = 2 * i + 1;
2447 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002448 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002449
Nate Begeman3b8d1162008-05-13 21:03:02 +00002450 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002451 }
Nate Begeman8a997642008-05-09 06:41:27 +00002452}
2453
Douglas Gregor04badcf2010-04-21 00:45:42 +00002454ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002455 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002456 SourceLocation LBracLoc,
2457 SourceLocation SuperLoc,
2458 bool IsInstanceSuper,
2459 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002460 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002461 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002462 ObjCMethodDecl *Method,
2463 Expr **Args, unsigned NumArgs,
2464 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002465 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002466 /*TypeDependent=*/false, /*ValueDependent=*/false,
2467 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002468 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2469 HasMethod(Method != 0), SuperLoc(SuperLoc),
2470 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2471 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002472 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002473{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002474 setReceiverPointer(SuperType.getAsOpaquePtr());
2475 if (NumArgs)
2476 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002477}
2478
Douglas Gregor04badcf2010-04-21 00:45:42 +00002479ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002480 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002481 SourceLocation LBracLoc,
2482 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002483 Selector Sel,
2484 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002485 ObjCMethodDecl *Method,
2486 Expr **Args, unsigned NumArgs,
2487 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002488 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002489 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002490 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2491 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2492 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002493 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002494{
2495 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002496 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002497 for (unsigned I = 0; I != NumArgs; ++I) {
2498 if (Args[I]->isTypeDependent())
2499 ExprBits.TypeDependent = true;
2500 if (Args[I]->isValueDependent())
2501 ExprBits.ValueDependent = true;
2502 if (Args[I]->containsUnexpandedParameterPack())
2503 ExprBits.ContainsUnexpandedParameterPack = true;
2504
2505 MyArgs[I] = Args[I];
2506 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002507}
2508
Douglas Gregor04badcf2010-04-21 00:45:42 +00002509ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002510 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002511 SourceLocation LBracLoc,
2512 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002513 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002514 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002515 ObjCMethodDecl *Method,
2516 Expr **Args, unsigned NumArgs,
2517 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002518 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002519 Receiver->isTypeDependent(),
2520 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002521 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2522 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2523 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002524 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002525{
2526 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002527 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002528 for (unsigned I = 0; I != NumArgs; ++I) {
2529 if (Args[I]->isTypeDependent())
2530 ExprBits.TypeDependent = true;
2531 if (Args[I]->isValueDependent())
2532 ExprBits.ValueDependent = true;
2533 if (Args[I]->containsUnexpandedParameterPack())
2534 ExprBits.ContainsUnexpandedParameterPack = true;
2535
2536 MyArgs[I] = Args[I];
2537 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002538}
2539
Douglas Gregor04badcf2010-04-21 00:45:42 +00002540ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002541 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002542 SourceLocation LBracLoc,
2543 SourceLocation SuperLoc,
2544 bool IsInstanceSuper,
2545 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002546 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002547 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002548 ObjCMethodDecl *Method,
2549 Expr **Args, unsigned NumArgs,
2550 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002551 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002552 NumArgs * sizeof(Expr *);
2553 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002554 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002555 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002556 RBracLoc);
2557}
2558
2559ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002560 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002561 SourceLocation LBracLoc,
2562 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002563 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002564 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002565 ObjCMethodDecl *Method,
2566 Expr **Args, unsigned NumArgs,
2567 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002568 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002569 NumArgs * sizeof(Expr *);
2570 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002571 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2572 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002573}
2574
2575ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002576 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002577 SourceLocation LBracLoc,
2578 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002579 Selector Sel,
2580 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002581 ObjCMethodDecl *Method,
2582 Expr **Args, unsigned NumArgs,
2583 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002584 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002585 NumArgs * sizeof(Expr *);
2586 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002587 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2588 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002589}
2590
Sean Huntc3021132010-05-05 15:23:54 +00002591ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002592 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002593 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002594 NumArgs * sizeof(Expr *);
2595 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2596 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2597}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002598
2599SourceRange ObjCMessageExpr::getReceiverRange() const {
2600 switch (getReceiverKind()) {
2601 case Instance:
2602 return getInstanceReceiver()->getSourceRange();
2603
2604 case Class:
2605 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2606
2607 case SuperInstance:
2608 case SuperClass:
2609 return getSuperLoc();
2610 }
2611
2612 return SourceLocation();
2613}
2614
Douglas Gregor04badcf2010-04-21 00:45:42 +00002615Selector ObjCMessageExpr::getSelector() const {
2616 if (HasMethod)
2617 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2618 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002619 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002620}
2621
2622ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2623 switch (getReceiverKind()) {
2624 case Instance:
2625 if (const ObjCObjectPointerType *Ptr
2626 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2627 return Ptr->getInterfaceDecl();
2628 break;
2629
2630 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002631 if (const ObjCObjectType *Ty
2632 = getClassReceiver()->getAs<ObjCObjectType>())
2633 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002634 break;
2635
2636 case SuperInstance:
2637 if (const ObjCObjectPointerType *Ptr
2638 = getSuperType()->getAs<ObjCObjectPointerType>())
2639 return Ptr->getInterfaceDecl();
2640 break;
2641
2642 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002643 if (const ObjCObjectType *Iface
2644 = getSuperType()->getAs<ObjCObjectType>())
2645 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002646 break;
2647 }
2648
2649 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002650}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002651
Jay Foad4ba2a172011-01-12 09:06:06 +00002652bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002653 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002654}
2655
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002656ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2657 QualType Type, SourceLocation BLoc,
2658 SourceLocation RP)
2659 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2660 Type->isDependentType(), Type->isDependentType(),
2661 Type->containsUnexpandedParameterPack()),
2662 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2663{
2664 SubExprs = new (C) Stmt*[nexpr];
2665 for (unsigned i = 0; i < nexpr; i++) {
2666 if (args[i]->isTypeDependent())
2667 ExprBits.TypeDependent = true;
2668 if (args[i]->isValueDependent())
2669 ExprBits.ValueDependent = true;
2670 if (args[i]->containsUnexpandedParameterPack())
2671 ExprBits.ContainsUnexpandedParameterPack = true;
2672
2673 SubExprs[i] = args[i];
2674 }
2675}
2676
Nate Begeman888376a2009-08-12 02:28:50 +00002677void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2678 unsigned NumExprs) {
2679 if (SubExprs) C.Deallocate(SubExprs);
2680
2681 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002682 this->NumExprs = NumExprs;
2683 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002684}
Nate Begeman888376a2009-08-12 02:28:50 +00002685
Peter Collingbournef111d932011-04-15 00:35:48 +00002686GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2687 SourceLocation GenericLoc, Expr *ControllingExpr,
2688 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2689 unsigned NumAssocs, SourceLocation DefaultLoc,
2690 SourceLocation RParenLoc,
2691 bool ContainsUnexpandedParameterPack,
2692 unsigned ResultIndex)
2693 : Expr(GenericSelectionExprClass,
2694 AssocExprs[ResultIndex]->getType(),
2695 AssocExprs[ResultIndex]->getValueKind(),
2696 AssocExprs[ResultIndex]->getObjectKind(),
2697 AssocExprs[ResultIndex]->isTypeDependent(),
2698 AssocExprs[ResultIndex]->isValueDependent(),
2699 ContainsUnexpandedParameterPack),
2700 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2701 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2702 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2703 RParenLoc(RParenLoc) {
2704 SubExprs[CONTROLLING] = ControllingExpr;
2705 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2706 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2707}
2708
2709GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2710 SourceLocation GenericLoc, Expr *ControllingExpr,
2711 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2712 unsigned NumAssocs, SourceLocation DefaultLoc,
2713 SourceLocation RParenLoc,
2714 bool ContainsUnexpandedParameterPack)
2715 : Expr(GenericSelectionExprClass,
2716 Context.DependentTy,
2717 VK_RValue,
2718 OK_Ordinary,
2719 /*isTypeDependent=*/ true,
2720 /*isValueDependent=*/ true,
2721 ContainsUnexpandedParameterPack),
2722 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2723 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2724 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2725 RParenLoc(RParenLoc) {
2726 SubExprs[CONTROLLING] = ControllingExpr;
2727 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2728 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2729}
2730
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002731//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002732// DesignatedInitExpr
2733//===----------------------------------------------------------------------===//
2734
2735IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2736 assert(Kind == FieldDesignator && "Only valid on a field designator");
2737 if (Field.NameOrField & 0x01)
2738 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2739 else
2740 return getField()->getIdentifier();
2741}
2742
Sean Huntc3021132010-05-05 15:23:54 +00002743DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002744 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002745 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002746 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002747 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002748 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002749 unsigned NumIndexExprs,
2750 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002751 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002752 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002753 Init->isTypeDependent(), Init->isValueDependent(),
2754 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002755 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2756 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002757 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002758
2759 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002760 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002761 *Child++ = Init;
2762
2763 // Copy the designators and their subexpressions, computing
2764 // value-dependence along the way.
2765 unsigned IndexIdx = 0;
2766 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002767 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002768
2769 if (this->Designators[I].isArrayDesignator()) {
2770 // Compute type- and value-dependence.
2771 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002772 if (Index->isTypeDependent() || Index->isValueDependent())
2773 ExprBits.ValueDependent = true;
2774
2775 // Propagate unexpanded parameter packs.
2776 if (Index->containsUnexpandedParameterPack())
2777 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002778
2779 // Copy the index expressions into permanent storage.
2780 *Child++ = IndexExprs[IndexIdx++];
2781 } else if (this->Designators[I].isArrayRangeDesignator()) {
2782 // Compute type- and value-dependence.
2783 Expr *Start = IndexExprs[IndexIdx];
2784 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002785 if (Start->isTypeDependent() || Start->isValueDependent() ||
2786 End->isTypeDependent() || End->isValueDependent())
2787 ExprBits.ValueDependent = true;
2788
2789 // Propagate unexpanded parameter packs.
2790 if (Start->containsUnexpandedParameterPack() ||
2791 End->containsUnexpandedParameterPack())
2792 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002793
2794 // Copy the start/end expressions into permanent storage.
2795 *Child++ = IndexExprs[IndexIdx++];
2796 *Child++ = IndexExprs[IndexIdx++];
2797 }
2798 }
2799
2800 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002801}
2802
Douglas Gregor05c13a32009-01-22 00:58:24 +00002803DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002804DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002805 unsigned NumDesignators,
2806 Expr **IndexExprs, unsigned NumIndexExprs,
2807 SourceLocation ColonOrEqualLoc,
2808 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002809 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002810 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002811 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002812 ColonOrEqualLoc, UsesColonSyntax,
2813 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002814}
2815
Mike Stump1eb44332009-09-09 15:08:12 +00002816DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002817 unsigned NumIndexExprs) {
2818 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2819 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2820 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2821}
2822
Douglas Gregor319d57f2010-01-06 23:17:19 +00002823void DesignatedInitExpr::setDesignators(ASTContext &C,
2824 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002825 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002826 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002827 NumDesignators = NumDesigs;
2828 for (unsigned I = 0; I != NumDesigs; ++I)
2829 Designators[I] = Desigs[I];
2830}
2831
Abramo Bagnara24f46742011-03-16 15:08:46 +00002832SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2833 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2834 if (size() == 1)
2835 return DIE->getDesignator(0)->getSourceRange();
2836 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2837 DIE->getDesignator(size()-1)->getEndLocation());
2838}
2839
Douglas Gregor05c13a32009-01-22 00:58:24 +00002840SourceRange DesignatedInitExpr::getSourceRange() const {
2841 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002842 Designator &First =
2843 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002844 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002845 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002846 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2847 else
2848 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2849 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002850 StartLoc =
2851 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002852 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2853}
2854
Douglas Gregor05c13a32009-01-22 00:58:24 +00002855Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2856 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2857 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2858 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002859 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2860 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2861}
2862
2863Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002864 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002865 "Requires array range designator");
2866 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2867 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002868 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2869 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2870}
2871
2872Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002873 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002874 "Requires array range designator");
2875 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2876 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002877 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2878 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2879}
2880
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002881/// \brief Replaces the designator at index @p Idx with the series
2882/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002883void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002884 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002885 const Designator *Last) {
2886 unsigned NumNewDesignators = Last - First;
2887 if (NumNewDesignators == 0) {
2888 std::copy_backward(Designators + Idx + 1,
2889 Designators + NumDesignators,
2890 Designators + Idx);
2891 --NumNewDesignators;
2892 return;
2893 } else if (NumNewDesignators == 1) {
2894 Designators[Idx] = *First;
2895 return;
2896 }
2897
Mike Stump1eb44332009-09-09 15:08:12 +00002898 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002899 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002900 std::copy(Designators, Designators + Idx, NewDesignators);
2901 std::copy(First, Last, NewDesignators + Idx);
2902 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2903 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002904 Designators = NewDesignators;
2905 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2906}
2907
Mike Stump1eb44332009-09-09 15:08:12 +00002908ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002909 Expr **exprs, unsigned nexprs,
2910 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002911 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2912 false, false, false),
2913 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Nate Begeman2ef13e52009-08-10 23:49:36 +00002915 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002916 for (unsigned i = 0; i != nexprs; ++i) {
2917 if (exprs[i]->isTypeDependent())
2918 ExprBits.TypeDependent = true;
2919 if (exprs[i]->isValueDependent())
2920 ExprBits.ValueDependent = true;
2921 if (exprs[i]->containsUnexpandedParameterPack())
2922 ExprBits.ContainsUnexpandedParameterPack = true;
2923
Nate Begeman2ef13e52009-08-10 23:49:36 +00002924 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002925 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002926}
2927
John McCalle996ffd2011-02-16 08:02:54 +00002928const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2929 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2930 e = ewc->getSubExpr();
2931 e = cast<CXXConstructExpr>(e)->getArg(0);
2932 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2933 e = ice->getSubExpr();
2934 return cast<OpaqueValueExpr>(e);
2935}
2936
Douglas Gregor05c13a32009-01-22 00:58:24 +00002937//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002938// ExprIterator.
2939//===----------------------------------------------------------------------===//
2940
2941Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2942Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2943Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2944const Expr* ConstExprIterator::operator[](size_t idx) const {
2945 return cast<Expr>(I[idx]);
2946}
2947const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2948const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2949
2950//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002951// Child Iterators for iterating over subexpressions/substatements
2952//===----------------------------------------------------------------------===//
2953
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002954// UnaryExprOrTypeTraitExpr
2955Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00002956 // If this is of a type and the type is a VLA type (and not a typedef), the
2957 // size expression of the VLA needs to be treated as an executable expression.
2958 // Why isn't this weirdness documented better in StmtIterator?
2959 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002960 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002961 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00002962 return child_range(child_iterator(T), child_iterator());
2963 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00002964 }
John McCall63c00d72011-02-09 08:16:59 +00002965 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002966}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002967
Steve Naroff563477d2007-09-18 23:55:05 +00002968// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00002969Stmt::child_range ObjCMessageExpr::children() {
2970 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00002971 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00002972 begin = reinterpret_cast<Stmt **>(this + 1);
2973 else
2974 begin = reinterpret_cast<Stmt **>(getArgs());
2975 return child_range(begin,
2976 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00002977}
2978
Steve Naroff4eb206b2008-09-03 18:15:37 +00002979// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00002980BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002981 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00002982 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00002983 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002984 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00002985 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00002986{
Douglas Gregord967e312011-01-19 21:52:31 +00002987 bool TypeDependent = false;
2988 bool ValueDependent = false;
2989 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2990 ExprBits.TypeDependent = TypeDependent;
2991 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00002992}
2993