blob: 1168874a889d782e35e458ab00e02f801e400182 [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"
Richard Smith7a614d82011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000031#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
Chris Lattner2b334bb2010-04-16 23:34:13 +000034/// isKnownToHaveBooleanValue - Return true if this is an integer expression
35/// that is known to return 0 or 1. This happens for _Bool/bool expressions
36/// but also int expressions which are produced by things like comparisons in
37/// C.
38bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000039 const Expr *E = IgnoreParens();
40
Chris Lattner2b334bb2010-04-16 23:34:13 +000041 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000042 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000043 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000045
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000047 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000048 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Sean Huntc3021132010-05-05 15:23:54 +000054
John McCall6907fbe2010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000057 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000059
Peter Collingbournef111d932011-04-15 00:35:48 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 switch (BO->getOpcode()) {
62 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000071 return true;
Sean Huntc3021132010-05-05 15:23:54 +000072
John McCall2de56d12010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000079
John McCall2de56d12010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000082 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Sean Huntc3021132010-05-05 15:23:54 +000085
Peter Collingbournef111d932011-04-15 00:35:48 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000087 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000089
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 return false;
91}
92
John McCall63c00d72011-02-09 08:16:59 +000093// Amusing macro metaprogramming hack: check whether a class provides
94// a more specific implementation of getExprLoc().
95namespace {
96 /// This implementation is used when a class provides a custom
97 /// implementation of getExprLoc.
98 template <class E, class T>
99 SourceLocation getExprLocImpl(const Expr *expr,
100 SourceLocation (T::*v)() const) {
101 return static_cast<const E*>(expr)->getExprLoc();
102 }
103
104 /// This implementation is used when a class doesn't provide
105 /// a custom implementation of getExprLoc. Overload resolution
106 /// should pick it over the implementation above because it's
107 /// more specialized according to function template partial ordering.
108 template <class E>
109 SourceLocation getExprLocImpl(const Expr *expr,
110 SourceLocation (Expr::*v)() const) {
111 return static_cast<const E*>(expr)->getSourceRange().getBegin();
112 }
113}
114
115SourceLocation Expr::getExprLoc() const {
116 switch (getStmtClass()) {
117 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
118#define ABSTRACT_STMT(type)
119#define STMT(type, base) \
120 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
121#define EXPR(type, base) \
122 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
123#include "clang/AST/StmtNodes.inc"
124 }
125 llvm_unreachable("unknown statement kind");
126 return SourceLocation();
127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
John McCalld5532b62009-11-23 01:53:49 +0000133void ExplicitTemplateArgumentList::initializeFrom(
134 const TemplateArgumentListInfo &Info) {
135 LAngleLoc = Info.getLAngleLoc();
136 RAngleLoc = Info.getRAngleLoc();
137 NumTemplateArgs = Info.size();
138
139 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
140 for (unsigned i = 0; i != NumTemplateArgs; ++i)
141 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
142}
143
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000144void ExplicitTemplateArgumentList::initializeFrom(
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 const TemplateArgumentListInfo &Info,
146 bool &Dependent,
147 bool &InstantiationDependent,
148 bool &ContainsUnexpandedParameterPack) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000149 LAngleLoc = Info.getLAngleLoc();
150 RAngleLoc = Info.getRAngleLoc();
151 NumTemplateArgs = Info.size();
152
153 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
154 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
155 Dependent = Dependent || Info[i].getArgument().isDependent();
Douglas Gregor561f8122011-07-01 01:22:09 +0000156 InstantiationDependent = InstantiationDependent ||
157 Info[i].getArgument().isInstantiationDependent();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000158 ContainsUnexpandedParameterPack
159 = ContainsUnexpandedParameterPack ||
160 Info[i].getArgument().containsUnexpandedParameterPack();
161
162 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
163 }
164}
165
John McCalld5532b62009-11-23 01:53:49 +0000166void ExplicitTemplateArgumentList::copyInto(
167 TemplateArgumentListInfo &Info) const {
168 Info.setLAngleLoc(LAngleLoc);
169 Info.setRAngleLoc(RAngleLoc);
170 for (unsigned I = 0; I != NumTemplateArgs; ++I)
171 Info.addArgument(getTemplateArgs()[I]);
172}
173
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000174std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
175 return sizeof(ExplicitTemplateArgumentList) +
176 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
177}
178
John McCalld5532b62009-11-23 01:53:49 +0000179std::size_t ExplicitTemplateArgumentList::sizeFor(
180 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000181 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000182}
183
Douglas Gregor561f8122011-07-01 01:22:09 +0000184/// \brief Compute the type-, value-, and instantiation-dependence of a
185/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000186/// based on the declaration being referenced.
187static void computeDeclRefDependence(NamedDecl *D, QualType T,
188 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000189 bool &ValueDependent,
190 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000191 TypeDependent = false;
192 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000193 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000194
195 // (TD) C++ [temp.dep.expr]p3:
196 // An id-expression is type-dependent if it contains:
197 //
Sean Huntc3021132010-05-05 15:23:54 +0000198 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000199 //
200 // (VD) C++ [temp.dep.constexpr]p2:
201 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000202
Douglas Gregor0da76df2009-11-23 11:41:28 +0000203 // (TD) - an identifier that was declared with dependent type
204 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000205 if (T->isDependentType()) {
206 TypeDependent = true;
207 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000208 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000209 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000210 } else if (T->isInstantiationDependentType()) {
211 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000212 }
Douglas Gregord967e312011-01-19 21:52:31 +0000213
Douglas Gregor0da76df2009-11-23 11:41:28 +0000214 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000215 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000216 == DeclarationName::CXXConversionFunctionName) {
217 QualType T = D->getDeclName().getCXXNameType();
218 if (T->isDependentType()) {
219 TypeDependent = true;
220 ValueDependent = true;
221 InstantiationDependent = true;
222 return;
223 }
224
225 if (T->isInstantiationDependentType())
226 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000227 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000228
Douglas Gregor0da76df2009-11-23 11:41:28 +0000229 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000230 if (isa<NonTypeTemplateParmDecl>(D)) {
231 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000233 return;
234 }
235
Douglas Gregor0da76df2009-11-23 11:41:28 +0000236 // (VD) - a constant with integral or enumeration type and is
237 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000238 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000239 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000240 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000241 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000242 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000243 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 InstantiationDependent = true;
245 }
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000246 }
Douglas Gregord967e312011-01-19 21:52:31 +0000247
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000248 // (VD) - FIXME: Missing from the standard:
249 // - a member function or a static data member of the current
250 // instantiation
251 else if (Var->isStaticDataMember() &&
Douglas Gregor561f8122011-07-01 01:22:09 +0000252 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000253 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000254 InstantiationDependent = true;
255 }
Douglas Gregord967e312011-01-19 21:52:31 +0000256
257 return;
258 }
259
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000260 // (VD) - FIXME: Missing from the standard:
261 // - a member function or a static data member of the current
262 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000263 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
264 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000265 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000266 return;
267 }
268}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000269
Douglas Gregord967e312011-01-19 21:52:31 +0000270void DeclRefExpr::computeDependence() {
271 bool TypeDependent = false;
272 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000273 bool InstantiationDependent = false;
274 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
275 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000276
277 // (TD) C++ [temp.dep.expr]p3:
278 // An id-expression is type-dependent if it contains:
279 //
280 // and
281 //
282 // (VD) C++ [temp.dep.constexpr]p2:
283 // An identifier is value-dependent if it is:
284 if (!TypeDependent && !ValueDependent &&
285 hasExplicitTemplateArgs() &&
286 TemplateSpecializationType::anyDependentTemplateArguments(
287 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000288 getNumTemplateArgs(),
289 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000290 TypeDependent = true;
291 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000292 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000293 }
294
295 ExprBits.TypeDependent = TypeDependent;
296 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000297 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000298
Douglas Gregor10738d32010-12-23 23:51:58 +0000299 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000300 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000301 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000302}
303
Chandler Carruth3aa81402011-05-01 23:48:14 +0000304DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000305 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000306 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000308 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000309 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000310 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
311 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000312 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000313 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000314 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
315 if (FoundD)
316 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000317 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000318 if (TemplateArgs) {
319 bool Dependent = false;
320 bool InstantiationDependent = false;
321 bool ContainsUnexpandedParameterPack = false;
322 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
323 InstantiationDependent,
324 ContainsUnexpandedParameterPack);
325 if (InstantiationDependent)
326 setInstantiationDependent(true);
327 }
328
Abramo Bagnara25777432010-08-11 22:01:17 +0000329 computeDependence();
330}
331
Douglas Gregora2813ce2009-10-23 18:54:35 +0000332DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000333 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000334 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000335 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000336 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000337 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000339 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000340 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000341 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000342 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000343}
344
345DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000346 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000347 ValueDecl *D,
348 const DeclarationNameInfo &NameInfo,
349 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000350 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000351 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000352 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000353 // Filter out cases where the found Decl is the same as the value refenenced.
354 if (D == FoundD)
355 FoundD = 0;
356
Douglas Gregora2813ce2009-10-23 18:54:35 +0000357 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000358 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000359 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000360 if (FoundD)
361 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000362 if (TemplateArgs)
363 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000364
Chris Lattner32488542010-10-30 05:14:06 +0000365 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000366 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
367 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000368}
369
Chandler Carruth3aa81402011-05-01 23:48:14 +0000370DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000371 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000372 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000373 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000374 unsigned NumTemplateArgs) {
375 std::size_t Size = sizeof(DeclRefExpr);
376 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000377 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000378 if (HasFoundDecl)
379 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000380 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000381 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000382
Chris Lattner32488542010-10-30 05:14:06 +0000383 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000384 return new (Mem) DeclRefExpr(EmptyShell());
385}
386
Douglas Gregora2813ce2009-10-23 18:54:35 +0000387SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000389 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000390 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000391 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000392 R.setEnd(getRAngleLoc());
393 return R;
394}
395
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396// FIXME: Maybe this should use DeclPrinter with a special "print predefined
397// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000398std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
399 ASTContext &Context = CurrentDecl->getASTContext();
400
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000402 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000403 return FD->getNameAsString();
404
405 llvm::SmallString<256> Name;
406 llvm::raw_svector_ostream Out(Name);
407
408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000409 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000410 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000411 if (MD->isStatic())
412 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000413 }
414
415 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416
417 std::string Proto = FD->getQualifiedNameAsString(Policy);
418
John McCall183700f2009-09-21 23:43:11 +0000419 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000420 const FunctionProtoType *FT = 0;
421 if (FD->hasWrittenPrototype())
422 FT = dyn_cast<FunctionProtoType>(AFT);
423
424 Proto += "(";
425 if (FT) {
426 llvm::raw_string_ostream POut(Proto);
427 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
428 if (i) POut << ", ";
429 std::string Param;
430 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
431 POut << Param;
432 }
433
434 if (FT->isVariadic()) {
435 if (FD->getNumParams()) POut << ", ";
436 POut << "...";
437 }
438 }
439 Proto += ")";
440
Sam Weinig4eadcc52009-12-27 01:38:20 +0000441 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
442 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
443 if (ThisQuals.hasConst())
444 Proto += " const";
445 if (ThisQuals.hasVolatile())
446 Proto += " volatile";
447 }
448
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000449 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
450 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000451
452 Out << Proto;
453
454 Out.flush();
455 return Name.str().str();
456 }
457 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
458 llvm::SmallString<256> Name;
459 llvm::raw_svector_ostream Out(Name);
460 Out << (MD->isInstanceMethod() ? '-' : '+');
461 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000462
463 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
464 // a null check to avoid a crash.
465 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000466 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000467
Anders Carlsson3a082d82009-09-08 18:24:21 +0000468 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000469 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
470 Out << '(' << CID << ')';
471
Anders Carlsson3a082d82009-09-08 18:24:21 +0000472 Out << ' ';
473 Out << MD->getSelector().getAsString();
474 Out << ']';
475
476 Out.flush();
477 return Name.str().str();
478 }
479 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
480 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
481 return "top level";
482 }
483 return "";
484}
485
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000486void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
487 if (hasAllocation())
488 C.Deallocate(pVal);
489
490 BitWidth = Val.getBitWidth();
491 unsigned NumWords = Val.getNumWords();
492 const uint64_t* Words = Val.getRawData();
493 if (NumWords > 1) {
494 pVal = new (C) uint64_t[NumWords];
495 std::copy(Words, Words + NumWords, pVal);
496 } else if (NumWords == 1)
497 VAL = Words[0];
498 else
499 VAL = 0;
500}
501
502IntegerLiteral *
503IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
504 QualType type, SourceLocation l) {
505 return new (C) IntegerLiteral(C, V, type, l);
506}
507
508IntegerLiteral *
509IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
510 return new (C) IntegerLiteral(Empty);
511}
512
513FloatingLiteral *
514FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
515 bool isexact, QualType Type, SourceLocation L) {
516 return new (C) FloatingLiteral(C, V, isexact, Type, L);
517}
518
519FloatingLiteral *
520FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
521 return new (C) FloatingLiteral(Empty);
522}
523
Chris Lattnerda8249e2008-06-07 22:13:43 +0000524/// getValueAsApproximateDouble - This returns the value as an inaccurate
525/// double. Note that this may cause loss of precision, but is useful for
526/// debugging dumps, etc.
527double FloatingLiteral::getValueAsApproximateDouble() const {
528 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000529 bool ignored;
530 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
531 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000532 return V.convertToDouble();
533}
534
Chris Lattner5f9e2722011-07-23 10:55:15 +0000535StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000536 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000537 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000538 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000539 // Allocate enough space for the StringLiteral plus an array of locations for
540 // any concatenated string tokens.
541 void *Mem = C.Allocate(sizeof(StringLiteral)+
542 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000543 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000544 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 // OPTIMIZE: could allocate this appended to the StringLiteral.
Jay Foad65aa6882011-06-21 15:13:30 +0000547 char *AStrData = new (C, 1) char[Str.size()];
548 memcpy(AStrData, Str.data(), Str.size());
Chris Lattner2085fd62009-02-18 06:40:38 +0000549 SL->StrData = AStrData;
Jay Foad65aa6882011-06-21 15:13:30 +0000550 SL->ByteLength = Str.size();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000551 SL->Kind = Kind;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000552 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000553 SL->TokLocs[0] = Loc[0];
554 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
Chris Lattner726e1682009-02-18 05:49:11 +0000556 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000557 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
558 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000559}
560
Douglas Gregor673ecd62009-04-15 16:35:07 +0000561StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
562 void *Mem = C.Allocate(sizeof(StringLiteral)+
563 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000564 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000565 StringLiteral *SL = new (Mem) StringLiteral(QualType());
566 SL->StrData = 0;
567 SL->ByteLength = 0;
568 SL->NumConcatenated = NumStrs;
569 return SL;
570}
571
Chris Lattner5f9e2722011-07-23 10:55:15 +0000572void StringLiteral::setString(ASTContext &C, StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000573 char *AStrData = new (C, 1) char[Str.size()];
574 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000575 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000576 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000577}
578
Chris Lattner08f92e32010-11-17 07:37:15 +0000579/// getLocationOfByte - Return a source location that points to the specified
580/// byte of this string literal.
581///
582/// Strings are amazingly complex. They can be formed from multiple tokens and
583/// can have escape sequences in them in addition to the usual trigraph and
584/// escaped newline business. This routine handles this complexity.
585///
586SourceLocation StringLiteral::
587getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
588 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000589 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
590
Chris Lattner08f92e32010-11-17 07:37:15 +0000591 // Loop over all of the tokens in this string until we find the one that
592 // contains the byte we're looking for.
593 unsigned TokNo = 0;
594 while (1) {
595 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
596 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
597
598 // Get the spelling of the string so that we can get the data that makes up
599 // the string literal, not the identifier for the macro it is potentially
600 // expanded through.
601 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
602
603 // Re-lex the token to get its length and original spelling.
604 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
605 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000606 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000607 if (Invalid)
608 return StrTokSpellingLoc;
609
610 const char *StrData = Buffer.data()+LocInfo.second;
611
612 // Create a langops struct and enable trigraphs. This is sufficient for
613 // relexing tokens.
614 LangOptions LangOpts;
615 LangOpts.Trigraphs = true;
616
617 // Create a lexer starting at the beginning of this token.
618 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
619 Buffer.end());
620 Token TheTok;
621 TheLexer.LexFromRawLexer(TheTok);
622
623 // Use the StringLiteralParser to compute the length of the string in bytes.
624 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
625 unsigned TokNumBytes = SLP.GetStringLength();
626
627 // If the byte is in this token, return the location of the byte.
628 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000629 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000630 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
631
632 // Now that we know the offset of the token in the spelling, use the
633 // preprocessor to get the offset in the original source.
634 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
635 }
636
637 // Move to the next string token.
638 ++TokNo;
639 ByteNo -= TokNumBytes;
640 }
641}
642
643
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
646/// corresponds to, e.g. "sizeof" or "[pre]++".
647const char *UnaryOperator::getOpcodeStr(Opcode Op) {
648 switch (Op) {
649 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000650 case UO_PostInc: return "++";
651 case UO_PostDec: return "--";
652 case UO_PreInc: return "++";
653 case UO_PreDec: return "--";
654 case UO_AddrOf: return "&";
655 case UO_Deref: return "*";
656 case UO_Plus: return "+";
657 case UO_Minus: return "-";
658 case UO_Not: return "~";
659 case UO_LNot: return "!";
660 case UO_Real: return "__real";
661 case UO_Imag: return "__imag";
662 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 }
664}
665
John McCall2de56d12010-08-25 11:45:40 +0000666UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000667UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
668 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000669 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000670 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
671 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
672 case OO_Amp: return UO_AddrOf;
673 case OO_Star: return UO_Deref;
674 case OO_Plus: return UO_Plus;
675 case OO_Minus: return UO_Minus;
676 case OO_Tilde: return UO_Not;
677 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000678 }
679}
680
681OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
682 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000683 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
684 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
685 case UO_AddrOf: return OO_Amp;
686 case UO_Deref: return OO_Star;
687 case UO_Plus: return OO_Plus;
688 case UO_Minus: return OO_Minus;
689 case UO_Not: return OO_Tilde;
690 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000691 default: return OO_None;
692 }
693}
694
695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696//===----------------------------------------------------------------------===//
697// Postfix Operators.
698//===----------------------------------------------------------------------===//
699
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000700CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
701 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000702 SourceLocation rparenloc)
703 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000704 fn->isTypeDependent(),
705 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000706 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000707 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000708 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000710 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +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;
Douglas Gregor561f8122011-07-01 01:22:09 +0000717 if (args[i]->isInstantiationDependent())
718 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000719 if (args[i]->containsUnexpandedParameterPack())
720 ExprBits.ContainsUnexpandedParameterPack = true;
721
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000722 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000723 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000724
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000725 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000726 RParenLoc = rparenloc;
727}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000728
Ted Kremenek668bf912009-02-09 20:51:47 +0000729CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000730 QualType t, ExprValueKind VK, SourceLocation rparenloc)
731 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000732 fn->isTypeDependent(),
733 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000734 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000735 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000736 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000737
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000738 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000739 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000740 for (unsigned i = 0; i != numargs; ++i) {
741 if (args[i]->isTypeDependent())
742 ExprBits.TypeDependent = true;
743 if (args[i]->isValueDependent())
744 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000745 if (args[i]->isInstantiationDependent())
746 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000747 if (args[i]->containsUnexpandedParameterPack())
748 ExprBits.ContainsUnexpandedParameterPack = true;
749
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000750 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000751 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000752
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000753 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 RParenLoc = rparenloc;
755}
756
Mike Stump1eb44332009-09-09 15:08:12 +0000757CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
758 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000759 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000760 SubExprs = new (C) Stmt*[PREARGS_START];
761 CallExprBits.NumPreArgs = 0;
762}
763
764CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
765 EmptyShell Empty)
766 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
767 // FIXME: Why do we allocate this?
768 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
769 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000770}
771
Nuno Lopesd20254f2009-12-20 23:11:08 +0000772Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000773 Expr *CEE = getCallee()->IgnoreParenCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000774
775 while (SubstNonTypeTemplateParmExpr *NTTP
776 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
777 CEE = NTTP->getReplacement()->IgnoreParenCasts();
778 }
779
Sebastian Redl20012152010-09-10 20:55:30 +0000780 // If we're calling a dereference, look at the pointer instead.
781 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
782 if (BO->isPtrMemOp())
783 CEE = BO->getRHS()->IgnoreParenCasts();
784 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
785 if (UO->getOpcode() == UO_Deref)
786 CEE = UO->getSubExpr()->IgnoreParenCasts();
787 }
Chris Lattner6346f962009-07-17 15:46:27 +0000788 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000789 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000790 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
791 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000792
793 return 0;
794}
795
Nuno Lopesd20254f2009-12-20 23:11:08 +0000796FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000797 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000798}
799
Chris Lattnerd18b3292007-12-28 05:25:02 +0000800/// setNumArgs - This changes the number of arguments present in this call.
801/// Any orphaned expressions are deleted by this, and any new operands are set
802/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000803void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000804 // No change, just return.
805 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chris Lattnerd18b3292007-12-28 05:25:02 +0000807 // If shrinking # arguments, just delete the extras and forgot them.
808 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000809 this->NumArgs = NumArgs;
810 return;
811 }
812
813 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000814 unsigned NumPreArgs = getNumPreArgs();
815 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000816 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000817 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000818 NewSubExprs[i] = SubExprs[i];
819 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000820 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
821 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000822 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor88c9a462009-04-17 21:46:47 +0000824 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000825 SubExprs = NewSubExprs;
826 this->NumArgs = NumArgs;
827}
828
Chris Lattnercb888962008-10-06 05:00:53 +0000829/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
830/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000831unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000832 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000833 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000834 // ImplicitCastExpr.
835 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
836 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000837 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000839 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
840 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000841 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Anders Carlssonbcba2012008-01-31 02:13:57 +0000843 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
844 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000845 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000847 if (!FDecl->getIdentifier())
848 return 0;
849
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000850 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000851}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000852
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000853QualType CallExpr::getCallReturnType() const {
854 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000855 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000856 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000857 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000858 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000859 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
860 // This should never be overloaded and so should never return null.
861 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000862
John McCall864c0412011-04-26 20:42:42 +0000863 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000864 return FnType->getResultType();
865}
Chris Lattnercb888962008-10-06 05:00:53 +0000866
John McCall2882eca2011-02-21 06:23:05 +0000867SourceRange CallExpr::getSourceRange() const {
868 if (isa<CXXOperatorCallExpr>(this))
869 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
870
871 SourceLocation begin = getCallee()->getLocStart();
872 if (begin.isInvalid() && getNumArgs() > 0)
873 begin = getArg(0)->getLocStart();
874 SourceLocation end = getRParenLoc();
875 if (end.isInvalid() && getNumArgs() > 0)
876 end = getArg(getNumArgs() - 1)->getLocEnd();
877 return SourceRange(begin, end);
878}
879
Sean Huntc3021132010-05-05 15:23:54 +0000880OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000881 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000882 TypeSourceInfo *tsi,
883 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000884 Expr** exprsPtr, unsigned numExprs,
885 SourceLocation RParenLoc) {
886 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000887 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000888 sizeof(Expr*) * numExprs);
889
890 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
891 exprsPtr, numExprs, RParenLoc);
892}
893
894OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
895 unsigned numComps, unsigned numExprs) {
896 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
897 sizeof(OffsetOfNode) * numComps +
898 sizeof(Expr*) * numExprs);
899 return new (Mem) OffsetOfExpr(numComps, numExprs);
900}
901
Sean Huntc3021132010-05-05 15:23:54 +0000902OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000903 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000904 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000905 Expr** exprsPtr, unsigned numExprs,
906 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000907 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
908 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000909 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000910 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000911 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000912 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
913 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000914{
915 for(unsigned i = 0; i < numComps; ++i) {
916 setComponent(i, compsPtr[i]);
917 }
Sean Huntc3021132010-05-05 15:23:54 +0000918
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000919 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000920 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
921 ExprBits.ValueDependent = true;
922 if (exprsPtr[i]->containsUnexpandedParameterPack())
923 ExprBits.ContainsUnexpandedParameterPack = true;
924
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000925 setIndexExpr(i, exprsPtr[i]);
926 }
927}
928
929IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
930 assert(getKind() == Field || getKind() == Identifier);
931 if (getKind() == Field)
932 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000933
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000934 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
935}
936
Mike Stump1eb44332009-09-09 15:08:12 +0000937MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000938 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000939 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000940 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000941 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000942 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000943 QualType ty,
944 ExprValueKind vk,
945 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000946 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000947
Douglas Gregor40d96a62011-02-28 21:54:11 +0000948 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000949 founddecl.getDecl() != memberdecl ||
950 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000951 if (hasQualOrFound)
952 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
John McCalld5532b62009-11-23 01:53:49 +0000954 if (targs)
955 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Chris Lattner32488542010-10-30 05:14:06 +0000957 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000958 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
959 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000960
961 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000962 // FIXME: Wrong. We should be looking at the member declaration we found.
963 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000964 E->setValueDependent(true);
965 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000966 E->setInstantiationDependent(true);
967 }
968 else if (QualifierLoc &&
969 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
970 E->setInstantiationDependent(true);
971
John McCall6bb80172010-03-30 21:47:33 +0000972 E->HasQualifierOrFoundDecl = true;
973
974 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000975 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000976 NQ->FoundDecl = founddecl;
977 }
978
979 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000980 bool Dependent = false;
981 bool InstantiationDependent = false;
982 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000983 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000984 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
985 InstantiationDependent,
986 ContainsUnexpandedParameterPack);
987 if (InstantiationDependent)
988 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000989 }
990
991 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000992}
993
Douglas Gregor75e85042011-03-02 21:06:53 +0000994SourceRange MemberExpr::getSourceRange() const {
995 SourceLocation StartLoc;
996 if (isImplicitAccess()) {
997 if (hasQualifier())
998 StartLoc = getQualifierLoc().getBeginLoc();
999 else
1000 StartLoc = MemberLoc;
1001 } else {
1002 // FIXME: We don't want this to happen. Rather, we should be able to
1003 // detect all kinds of implicit accesses more cleanly.
1004 StartLoc = getBase()->getLocStart();
1005 if (StartLoc.isInvalid())
1006 StartLoc = MemberLoc;
1007 }
1008
1009 SourceLocation EndLoc =
1010 HasExplicitTemplateArgumentList? getRAngleLoc()
1011 : getMemberNameInfo().getEndLoc();
1012
1013 return SourceRange(StartLoc, EndLoc);
1014}
1015
John McCall1d9b3b22011-09-09 05:25:32 +00001016void CastExpr::CheckCastConsistency() const {
1017 switch (getCastKind()) {
1018 case CK_DerivedToBase:
1019 case CK_UncheckedDerivedToBase:
1020 case CK_DerivedToBaseMemberPointer:
1021 case CK_BaseToDerived:
1022 case CK_BaseToDerivedMemberPointer:
1023 assert(!path_empty() && "Cast kind should have a base path!");
1024 break;
1025
1026 case CK_CPointerToObjCPointerCast:
1027 assert(getType()->isObjCObjectPointerType());
1028 assert(getSubExpr()->getType()->isPointerType());
1029 goto CheckNoBasePath;
1030
1031 case CK_BlockPointerToObjCPointerCast:
1032 assert(getType()->isObjCObjectPointerType());
1033 assert(getSubExpr()->getType()->isBlockPointerType());
1034 goto CheckNoBasePath;
1035
1036 case CK_BitCast:
1037 // Arbitrary casts to C pointer types count as bitcasts.
1038 // Otherwise, we should only have block and ObjC pointer casts
1039 // here if they stay within the type kind.
1040 if (!getType()->isPointerType()) {
1041 assert(getType()->isObjCObjectPointerType() ==
1042 getSubExpr()->getType()->isObjCObjectPointerType());
1043 assert(getType()->isBlockPointerType() ==
1044 getSubExpr()->getType()->isBlockPointerType());
1045 }
1046 goto CheckNoBasePath;
1047
1048 case CK_AnyPointerToBlockPointerCast:
1049 assert(getType()->isBlockPointerType());
1050 assert(getSubExpr()->getType()->isAnyPointerType() &&
1051 !getSubExpr()->getType()->isBlockPointerType());
1052 goto CheckNoBasePath;
1053
1054 // These should not have an inheritance path.
1055 case CK_Dynamic:
1056 case CK_ToUnion:
1057 case CK_ArrayToPointerDecay:
1058 case CK_FunctionToPointerDecay:
1059 case CK_NullToMemberPointer:
1060 case CK_NullToPointer:
1061 case CK_ConstructorConversion:
1062 case CK_IntegralToPointer:
1063 case CK_PointerToIntegral:
1064 case CK_ToVoid:
1065 case CK_VectorSplat:
1066 case CK_IntegralCast:
1067 case CK_IntegralToFloating:
1068 case CK_FloatingToIntegral:
1069 case CK_FloatingCast:
1070 case CK_ObjCObjectLValueCast:
1071 case CK_FloatingRealToComplex:
1072 case CK_FloatingComplexToReal:
1073 case CK_FloatingComplexCast:
1074 case CK_FloatingComplexToIntegralComplex:
1075 case CK_IntegralRealToComplex:
1076 case CK_IntegralComplexToReal:
1077 case CK_IntegralComplexCast:
1078 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001079 case CK_ARCProduceObject:
1080 case CK_ARCConsumeObject:
1081 case CK_ARCReclaimReturnedObject:
1082 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001083 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1084 goto CheckNoBasePath;
1085
1086 case CK_Dependent:
1087 case CK_LValueToRValue:
1088 case CK_GetObjCProperty:
1089 case CK_NoOp:
1090 case CK_PointerToBoolean:
1091 case CK_IntegralToBoolean:
1092 case CK_FloatingToBoolean:
1093 case CK_MemberPointerToBoolean:
1094 case CK_FloatingComplexToBoolean:
1095 case CK_IntegralComplexToBoolean:
1096 case CK_LValueBitCast: // -> bool&
1097 case CK_UserDefinedConversion: // operator bool()
1098 CheckNoBasePath:
1099 assert(path_empty() && "Cast kind should not have a base path!");
1100 break;
1101 }
1102}
1103
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001104const char *CastExpr::getCastKindName() const {
1105 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001106 case CK_Dependent:
1107 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001108 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001109 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001110 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001111 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001112 case CK_LValueToRValue:
1113 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +00001114 case CK_GetObjCProperty:
1115 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +00001116 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001117 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001118 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001119 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001120 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001121 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001122 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001123 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001124 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001125 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001126 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001127 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001128 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001129 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001130 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001131 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001132 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001133 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001134 case CK_NullToPointer:
1135 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001136 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001137 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001138 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001139 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001140 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001141 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001142 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001143 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001144 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001145 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001146 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001147 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001148 case CK_PointerToBoolean:
1149 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001150 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001151 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001152 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001153 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001154 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001155 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001156 case CK_IntegralToBoolean:
1157 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001158 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001159 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001160 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001161 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001162 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001163 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001164 case CK_FloatingToBoolean:
1165 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001166 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001167 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001168 case CK_CPointerToObjCPointerCast:
1169 return "CPointerToObjCPointerCast";
1170 case CK_BlockPointerToObjCPointerCast:
1171 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001172 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001173 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001174 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001175 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001176 case CK_FloatingRealToComplex:
1177 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001178 case CK_FloatingComplexToReal:
1179 return "FloatingComplexToReal";
1180 case CK_FloatingComplexToBoolean:
1181 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001182 case CK_FloatingComplexCast:
1183 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001184 case CK_FloatingComplexToIntegralComplex:
1185 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001186 case CK_IntegralRealToComplex:
1187 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001188 case CK_IntegralComplexToReal:
1189 return "IntegralComplexToReal";
1190 case CK_IntegralComplexToBoolean:
1191 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001192 case CK_IntegralComplexCast:
1193 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001194 case CK_IntegralComplexToFloatingComplex:
1195 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001196 case CK_ARCConsumeObject:
1197 return "ARCConsumeObject";
1198 case CK_ARCProduceObject:
1199 return "ARCProduceObject";
1200 case CK_ARCReclaimReturnedObject:
1201 return "ARCReclaimReturnedObject";
1202 case CK_ARCExtendBlockObject:
1203 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
John McCall2bb5d002010-11-13 09:02:35 +00001206 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001207 return 0;
1208}
1209
Douglas Gregor6eef5192009-12-14 19:27:10 +00001210Expr *CastExpr::getSubExprAsWritten() {
1211 Expr *SubExpr = 0;
1212 CastExpr *E = this;
1213 do {
1214 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001215
1216 // Skip through reference binding to temporary.
1217 if (MaterializeTemporaryExpr *Materialize
1218 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1219 SubExpr = Materialize->GetTemporaryExpr();
1220
Douglas Gregor6eef5192009-12-14 19:27:10 +00001221 // Skip any temporary bindings; they're implicit.
1222 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1223 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001224
Douglas Gregor6eef5192009-12-14 19:27:10 +00001225 // Conversions by constructor and conversion functions have a
1226 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001227 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001228 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001229 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001230 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001231
Douglas Gregor6eef5192009-12-14 19:27:10 +00001232 // If the subexpression we're left with is an implicit cast, look
1233 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001234 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1235
Douglas Gregor6eef5192009-12-14 19:27:10 +00001236 return SubExpr;
1237}
1238
John McCallf871d0c2010-08-07 06:22:56 +00001239CXXBaseSpecifier **CastExpr::path_buffer() {
1240 switch (getStmtClass()) {
1241#define ABSTRACT_STMT(x)
1242#define CASTEXPR(Type, Base) \
1243 case Stmt::Type##Class: \
1244 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1245#define STMT(Type, Base)
1246#include "clang/AST/StmtNodes.inc"
1247 default:
1248 llvm_unreachable("non-cast expressions not possible here");
1249 return 0;
1250 }
1251}
1252
1253void CastExpr::setCastPath(const CXXCastPath &Path) {
1254 assert(Path.size() == path_size());
1255 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1256}
1257
1258ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1259 CastKind Kind, Expr *Operand,
1260 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001261 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001262 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1263 void *Buffer =
1264 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1265 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001266 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001267 if (PathSize) E->setCastPath(*BasePath);
1268 return E;
1269}
1270
1271ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1272 unsigned PathSize) {
1273 void *Buffer =
1274 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1275 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1276}
1277
1278
1279CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001280 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001281 const CXXCastPath *BasePath,
1282 TypeSourceInfo *WrittenTy,
1283 SourceLocation L, SourceLocation R) {
1284 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1285 void *Buffer =
1286 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1287 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001288 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001289 if (PathSize) E->setCastPath(*BasePath);
1290 return E;
1291}
1292
1293CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1294 void *Buffer =
1295 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1296 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1297}
1298
Reid Spencer5f016e22007-07-11 17:01:13 +00001299/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1300/// corresponds to, e.g. "<<=".
1301const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1302 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001303 case BO_PtrMemD: return ".*";
1304 case BO_PtrMemI: return "->*";
1305 case BO_Mul: return "*";
1306 case BO_Div: return "/";
1307 case BO_Rem: return "%";
1308 case BO_Add: return "+";
1309 case BO_Sub: return "-";
1310 case BO_Shl: return "<<";
1311 case BO_Shr: return ">>";
1312 case BO_LT: return "<";
1313 case BO_GT: return ">";
1314 case BO_LE: return "<=";
1315 case BO_GE: return ">=";
1316 case BO_EQ: return "==";
1317 case BO_NE: return "!=";
1318 case BO_And: return "&";
1319 case BO_Xor: return "^";
1320 case BO_Or: return "|";
1321 case BO_LAnd: return "&&";
1322 case BO_LOr: return "||";
1323 case BO_Assign: return "=";
1324 case BO_MulAssign: return "*=";
1325 case BO_DivAssign: return "/=";
1326 case BO_RemAssign: return "%=";
1327 case BO_AddAssign: return "+=";
1328 case BO_SubAssign: return "-=";
1329 case BO_ShlAssign: return "<<=";
1330 case BO_ShrAssign: return ">>=";
1331 case BO_AndAssign: return "&=";
1332 case BO_XorAssign: return "^=";
1333 case BO_OrAssign: return "|=";
1334 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001336
1337 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001338}
1339
John McCall2de56d12010-08-25 11:45:40 +00001340BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001341BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1342 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001343 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001344 case OO_Plus: return BO_Add;
1345 case OO_Minus: return BO_Sub;
1346 case OO_Star: return BO_Mul;
1347 case OO_Slash: return BO_Div;
1348 case OO_Percent: return BO_Rem;
1349 case OO_Caret: return BO_Xor;
1350 case OO_Amp: return BO_And;
1351 case OO_Pipe: return BO_Or;
1352 case OO_Equal: return BO_Assign;
1353 case OO_Less: return BO_LT;
1354 case OO_Greater: return BO_GT;
1355 case OO_PlusEqual: return BO_AddAssign;
1356 case OO_MinusEqual: return BO_SubAssign;
1357 case OO_StarEqual: return BO_MulAssign;
1358 case OO_SlashEqual: return BO_DivAssign;
1359 case OO_PercentEqual: return BO_RemAssign;
1360 case OO_CaretEqual: return BO_XorAssign;
1361 case OO_AmpEqual: return BO_AndAssign;
1362 case OO_PipeEqual: return BO_OrAssign;
1363 case OO_LessLess: return BO_Shl;
1364 case OO_GreaterGreater: return BO_Shr;
1365 case OO_LessLessEqual: return BO_ShlAssign;
1366 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1367 case OO_EqualEqual: return BO_EQ;
1368 case OO_ExclaimEqual: return BO_NE;
1369 case OO_LessEqual: return BO_LE;
1370 case OO_GreaterEqual: return BO_GE;
1371 case OO_AmpAmp: return BO_LAnd;
1372 case OO_PipePipe: return BO_LOr;
1373 case OO_Comma: return BO_Comma;
1374 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001375 }
1376}
1377
1378OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1379 static const OverloadedOperatorKind OverOps[] = {
1380 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1381 OO_Star, OO_Slash, OO_Percent,
1382 OO_Plus, OO_Minus,
1383 OO_LessLess, OO_GreaterGreater,
1384 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1385 OO_EqualEqual, OO_ExclaimEqual,
1386 OO_Amp,
1387 OO_Caret,
1388 OO_Pipe,
1389 OO_AmpAmp,
1390 OO_PipePipe,
1391 OO_Equal, OO_StarEqual,
1392 OO_SlashEqual, OO_PercentEqual,
1393 OO_PlusEqual, OO_MinusEqual,
1394 OO_LessLessEqual, OO_GreaterGreaterEqual,
1395 OO_AmpEqual, OO_CaretEqual,
1396 OO_PipeEqual,
1397 OO_Comma
1398 };
1399 return OverOps[Opc];
1400}
1401
Ted Kremenek709210f2010-04-13 23:39:13 +00001402InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001403 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001404 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001405 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001406 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001407 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001408 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001409 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001410{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001411 for (unsigned I = 0; I != numInits; ++I) {
1412 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001413 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001414 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001415 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001416 if (initExprs[I]->isInstantiationDependent())
1417 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001418 if (initExprs[I]->containsUnexpandedParameterPack())
1419 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001420 }
Sean Huntc3021132010-05-05 15:23:54 +00001421
Ted Kremenek709210f2010-04-13 23:39:13 +00001422 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001423}
Reid Spencer5f016e22007-07-11 17:01:13 +00001424
Ted Kremenek709210f2010-04-13 23:39:13 +00001425void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001426 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001427 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001428}
1429
Ted Kremenek709210f2010-04-13 23:39:13 +00001430void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001431 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001432}
1433
Ted Kremenek709210f2010-04-13 23:39:13 +00001434Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001435 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001436 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001437 InitExprs.back() = expr;
1438 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregor4c678342009-01-28 21:54:33 +00001441 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1442 InitExprs[Init] = expr;
1443 return Result;
1444}
1445
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001446void InitListExpr::setArrayFiller(Expr *filler) {
1447 ArrayFillerOrUnionFieldInit = filler;
1448 // Fill out any "holes" in the array due to designated initializers.
1449 Expr **inits = getInits();
1450 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1451 if (inits[i] == 0)
1452 inits[i] = filler;
1453}
1454
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001455SourceRange InitListExpr::getSourceRange() const {
1456 if (SyntacticForm)
1457 return SyntacticForm->getSourceRange();
1458 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1459 if (Beg.isInvalid()) {
1460 // Find the first non-null initializer.
1461 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1462 E = InitExprs.end();
1463 I != E; ++I) {
1464 if (Stmt *S = *I) {
1465 Beg = S->getLocStart();
1466 break;
1467 }
1468 }
1469 }
1470 if (End.isInvalid()) {
1471 // Find the first non-null initializer from the end.
1472 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1473 E = InitExprs.rend();
1474 I != E; ++I) {
1475 if (Stmt *S = *I) {
1476 End = S->getSourceRange().getEnd();
1477 break;
1478 }
1479 }
1480 }
1481 return SourceRange(Beg, End);
1482}
1483
Steve Naroffbfdcae62008-09-04 15:31:07 +00001484/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001485///
1486const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001487 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001488 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001489}
1490
Mike Stump1eb44332009-09-09 15:08:12 +00001491SourceLocation BlockExpr::getCaretLocation() const {
1492 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001493}
Mike Stump1eb44332009-09-09 15:08:12 +00001494const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001495 return TheBlock->getBody();
1496}
Mike Stump1eb44332009-09-09 15:08:12 +00001497Stmt *BlockExpr::getBody() {
1498 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001499}
Steve Naroff56ee6892008-10-08 17:01:13 +00001500
1501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502//===----------------------------------------------------------------------===//
1503// Generic Expression Routines
1504//===----------------------------------------------------------------------===//
1505
Chris Lattner026dc962009-02-14 07:37:35 +00001506/// isUnusedResultAWarning - Return true if this immediate expression should
1507/// be warned about if the result is unused. If so, fill in Loc and Ranges
1508/// with location to warn on and the source range[s] to report with the
1509/// warning.
1510bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001511 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001512 // Don't warn if the expr is type dependent. The type could end up
1513 // instantiating to void.
1514 if (isTypeDependent())
1515 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 switch (getStmtClass()) {
1518 default:
John McCall0faede62010-03-12 07:11:26 +00001519 if (getType()->isVoidType())
1520 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001521 Loc = getExprLoc();
1522 R1 = getSourceRange();
1523 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001525 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001526 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001527 case GenericSelectionExprClass:
1528 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1529 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 case UnaryOperatorClass: {
1531 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001534 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001535 case UO_PostInc:
1536 case UO_PostDec:
1537 case UO_PreInc:
1538 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001539 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001540 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001542 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001543 return false;
1544 break;
John McCall2de56d12010-08-25 11:45:40 +00001545 case UO_Real:
1546 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001548 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1549 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001550 return false;
1551 break;
John McCall2de56d12010-08-25 11:45:40 +00001552 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 }
Chris Lattner026dc962009-02-14 07:37:35 +00001555 Loc = UO->getOperatorLoc();
1556 R1 = UO->getSubExpr()->getSourceRange();
1557 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001559 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001560 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001561 switch (BO->getOpcode()) {
1562 default:
1563 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001564 // Consider the RHS of comma for side effects. LHS was checked by
1565 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001566 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001567 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1568 // lvalue-ness) of an assignment written in a macro.
1569 if (IntegerLiteral *IE =
1570 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1571 if (IE->getValue() == 0)
1572 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001573 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1574 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001575 case BO_LAnd:
1576 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001577 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1578 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1579 return false;
1580 break;
John McCallbf0ee352010-02-16 04:10:53 +00001581 }
Chris Lattner026dc962009-02-14 07:37:35 +00001582 if (BO->isAssignmentOp())
1583 return false;
1584 Loc = BO->getOperatorLoc();
1585 R1 = BO->getLHS()->getSourceRange();
1586 R2 = BO->getRHS()->getSourceRange();
1587 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001588 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001589 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001590 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001591 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001592
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001593 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001594 // If only one of the LHS or RHS is a warning, the operator might
1595 // be being used for control flow. Only warn if both the LHS and
1596 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001597 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001598 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1599 return false;
1600 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001601 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001602 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001603 }
1604
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001606 // If the base pointer or element is to a volatile pointer/field, accessing
1607 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001608 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001609 return false;
1610 Loc = cast<MemberExpr>(this)->getMemberLoc();
1611 R1 = SourceRange(Loc, Loc);
1612 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1613 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 case ArraySubscriptExprClass:
1616 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001617 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001618 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001619 return false;
1620 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1621 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1622 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1623 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001624
Chandler Carruth9b106832011-08-17 09:49:44 +00001625 case CXXOperatorCallExprClass: {
1626 // We warn about operator== and operator!= even when user-defined operator
1627 // overloads as there is no reasonable way to define these such that they
1628 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1629 // warning: these operators are commonly typo'ed, and so warning on them
1630 // provides additional value as well. If this list is updated,
1631 // DiagnoseUnusedComparison should be as well.
1632 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1633 if (Op->getOperator() == OO_EqualEqual ||
1634 Op->getOperator() == OO_ExclaimEqual)
1635 return true;
1636
1637 // Fallthrough for generic call handling.
1638 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001640 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001641 // If this is a direct call, get the callee.
1642 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001643 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001644 // If the callee has attribute pure, const, or warn_unused_result, warn
1645 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001646 //
1647 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1648 // updated to match for QoI.
1649 if (FD->getAttr<WarnUnusedResultAttr>() ||
1650 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1651 Loc = CE->getCallee()->getLocStart();
1652 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001654 if (unsigned NumArgs = CE->getNumArgs())
1655 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1656 CE->getArg(NumArgs-1)->getLocEnd());
1657 return true;
1658 }
Chris Lattner026dc962009-02-14 07:37:35 +00001659 }
1660 return false;
1661 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001662
1663 case CXXTemporaryObjectExprClass:
1664 case CXXConstructExprClass:
1665 return false;
1666
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001667 case ObjCMessageExprClass: {
1668 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001669 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1670 ME->isInstanceMessage() &&
1671 !ME->getType()->isVoidType() &&
1672 ME->getSelector().getIdentifierInfoForSlot(0) &&
1673 ME->getSelector().getIdentifierInfoForSlot(0)
1674 ->getName().startswith("init")) {
1675 Loc = getExprLoc();
1676 R1 = ME->getSourceRange();
1677 return true;
1678 }
1679
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001680 const ObjCMethodDecl *MD = ME->getMethodDecl();
1681 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1682 Loc = getExprLoc();
1683 return true;
1684 }
Chris Lattner026dc962009-02-14 07:37:35 +00001685 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001686 }
Mike Stump1eb44332009-09-09 15:08:12 +00001687
John McCall12f78a62010-12-02 01:19:52 +00001688 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001689 Loc = getExprLoc();
1690 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001691 return true;
John McCall12f78a62010-12-02 01:19:52 +00001692
Chris Lattner611b2ec2008-07-26 19:51:01 +00001693 case StmtExprClass: {
1694 // Statement exprs don't logically have side effects themselves, but are
1695 // sometimes used in macros in ways that give them a type that is unused.
1696 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1697 // however, if the result of the stmt expr is dead, we don't want to emit a
1698 // warning.
1699 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001700 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001701 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001702 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001703 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1704 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1705 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
John McCall0faede62010-03-12 07:11:26 +00001708 if (getType()->isVoidType())
1709 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001710 Loc = cast<StmtExpr>(this)->getLParenLoc();
1711 R1 = getSourceRange();
1712 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001713 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001714 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001715 // If this is an explicit cast to void, allow it. People do this when they
1716 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001717 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001718 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001719 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1720 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1721 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001722 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001723 if (getType()->isVoidType())
1724 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001725 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001726
Anders Carlsson58beed92009-11-17 17:11:23 +00001727 // If this is a cast to void or a constructor conversion, check the operand.
1728 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001729 if (CE->getCastKind() == CK_ToVoid ||
1730 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001731 return (cast<CastExpr>(this)->getSubExpr()
1732 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001733 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1734 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1735 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Eli Friedman4be1f472008-05-19 21:24:43 +00001738 case ImplicitCastExprClass:
1739 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001740 return (cast<ImplicitCastExpr>(this)
1741 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001742
Chris Lattner04421082008-04-08 04:40:51 +00001743 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001744 return (cast<CXXDefaultArgExpr>(this)
1745 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001746
1747 case CXXNewExprClass:
1748 // FIXME: In theory, there might be new expressions that don't have side
1749 // effects (e.g. a placement new with an uninitialized POD).
1750 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001751 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001752 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001753 return (cast<CXXBindTemporaryExpr>(this)
1754 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001755 case ExprWithCleanupsClass:
1756 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001757 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001758 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001759}
1760
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001761/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001762/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001763bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001764 const Expr *E = IgnoreParens();
1765 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001766 default:
1767 return false;
1768 case ObjCIvarRefExprClass:
1769 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001770 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001771 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001772 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001773 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001774 case MaterializeTemporaryExprClass:
1775 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1776 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001777 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001778 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001779 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001780 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001781 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1782 if (VD->hasGlobalStorage())
1783 return true;
1784 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001785 // dereferencing to a pointer is always a gc'able candidate,
1786 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001787 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001788 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001789 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001790 return false;
1791 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001792 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001793 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001794 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001795 }
1796 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001797 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001798 }
1799}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001800
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001801bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1802 if (isTypeDependent())
1803 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001804 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001805}
1806
John McCall864c0412011-04-26 20:42:42 +00001807QualType Expr::findBoundMemberType(const Expr *expr) {
1808 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1809
1810 // Bound member expressions are always one of these possibilities:
1811 // x->m x.m x->*y x.*y
1812 // (possibly parenthesized)
1813
1814 expr = expr->IgnoreParens();
1815 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1816 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1817 return mem->getMemberDecl()->getType();
1818 }
1819
1820 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1821 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1822 ->getPointeeType();
1823 assert(type->isFunctionType());
1824 return type;
1825 }
1826
1827 assert(isa<UnresolvedMemberExpr>(expr));
1828 return QualType();
1829}
1830
Sebastian Redl369e51f2010-09-10 20:55:33 +00001831static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1832 Expr::CanThrowResult CT2) {
1833 // CanThrowResult constants are ordered so that the maximum is the correct
1834 // merge result.
1835 return CT1 > CT2 ? CT1 : CT2;
1836}
1837
1838static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1839 Expr *E = const_cast<Expr*>(CE);
1840 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001841 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001842 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1843 }
1844 return R;
1845}
1846
Richard Smith7a614d82011-06-11 17:19:42 +00001847static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1848 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001849 bool NullThrows = true) {
1850 if (!D)
1851 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1852
1853 // See if we can get a function type from the decl somehow.
1854 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1855 if (!VD) // If we have no clue what we're calling, assume the worst.
1856 return Expr::CT_Can;
1857
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001858 // As an extension, we assume that __attribute__((nothrow)) functions don't
1859 // throw.
1860 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1861 return Expr::CT_Cannot;
1862
Sebastian Redl369e51f2010-09-10 20:55:33 +00001863 QualType T = VD->getType();
1864 const FunctionProtoType *FT;
1865 if ((FT = T->getAs<FunctionProtoType>())) {
1866 } else if (const PointerType *PT = T->getAs<PointerType>())
1867 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1868 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1869 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1870 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1871 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1872 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1873 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1874
1875 if (!FT)
1876 return Expr::CT_Can;
1877
Richard Smith7a614d82011-06-11 17:19:42 +00001878 if (FT->getExceptionSpecType() == EST_Delayed) {
1879 assert(isa<CXXConstructorDecl>(D) &&
1880 "only constructor exception specs can be unknown");
1881 Ctx.getDiagnostics().Report(E->getLocStart(),
1882 diag::err_exception_spec_unknown)
1883 << E->getSourceRange();
1884 return Expr::CT_Can;
1885 }
1886
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001887 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001888}
1889
1890static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1891 if (DC->isTypeDependent())
1892 return Expr::CT_Dependent;
1893
Sebastian Redl295995c2010-09-10 20:55:47 +00001894 if (!DC->getTypeAsWritten()->isReferenceType())
1895 return Expr::CT_Cannot;
1896
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001897 if (DC->getSubExpr()->isTypeDependent())
1898 return Expr::CT_Dependent;
1899
Sebastian Redl369e51f2010-09-10 20:55:33 +00001900 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1901}
1902
1903static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1904 const CXXTypeidExpr *DC) {
1905 if (DC->isTypeOperand())
1906 return Expr::CT_Cannot;
1907
1908 Expr *Op = DC->getExprOperand();
1909 if (Op->isTypeDependent())
1910 return Expr::CT_Dependent;
1911
1912 const RecordType *RT = Op->getType()->getAs<RecordType>();
1913 if (!RT)
1914 return Expr::CT_Cannot;
1915
1916 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1917 return Expr::CT_Cannot;
1918
1919 if (Op->Classify(C).isPRValue())
1920 return Expr::CT_Cannot;
1921
1922 return Expr::CT_Can;
1923}
1924
1925Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1926 // C++ [expr.unary.noexcept]p3:
1927 // [Can throw] if in a potentially-evaluated context the expression would
1928 // contain:
1929 switch (getStmtClass()) {
1930 case CXXThrowExprClass:
1931 // - a potentially evaluated throw-expression
1932 return CT_Can;
1933
1934 case CXXDynamicCastExprClass: {
1935 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1936 // where T is a reference type, that requires a run-time check
1937 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1938 if (CT == CT_Can)
1939 return CT;
1940 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1941 }
1942
1943 case CXXTypeidExprClass:
1944 // - a potentially evaluated typeid expression applied to a glvalue
1945 // expression whose type is a polymorphic class type
1946 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1947
1948 // - a potentially evaluated call to a function, member function, function
1949 // pointer, or member function pointer that does not have a non-throwing
1950 // exception-specification
1951 case CallExprClass:
1952 case CXXOperatorCallExprClass:
1953 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001954 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001955 CanThrowResult CT;
1956 if (isTypeDependent())
1957 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001958 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1959 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001960 else
Richard Smith7a614d82011-06-11 17:19:42 +00001961 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001962 if (CT == CT_Can)
1963 return CT;
1964 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1965 }
1966
Sebastian Redl295995c2010-09-10 20:55:47 +00001967 case CXXConstructExprClass:
1968 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001969 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001970 cast<CXXConstructExpr>(this)->getConstructor());
1971 if (CT == CT_Can)
1972 return CT;
1973 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1974 }
1975
1976 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001977 CanThrowResult CT;
1978 if (isTypeDependent())
1979 CT = CT_Dependent;
1980 else
1981 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001982 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1983 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001984 /*NullThrows*/false));
1985 if (CT == CT_Can)
1986 return CT;
1987 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1988 }
1989
1990 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001991 CanThrowResult CT;
1992 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1993 if (DTy.isNull() || DTy->isDependentType()) {
1994 CT = CT_Dependent;
1995 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00001996 CT = CanCalleeThrow(C, this,
1997 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001998 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1999 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002000 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002001 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002002 if (CT == CT_Can)
2003 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002004 }
2005 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2006 }
2007
2008 case CXXBindTemporaryExprClass: {
2009 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002010 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002011 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2012 if (CT == CT_Can)
2013 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002014 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2015 }
2016
2017 // ObjC message sends are like function calls, but never have exception
2018 // specs.
2019 case ObjCMessageExprClass:
2020 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002021 return CT_Can;
2022
2023 // Many other things have subexpressions, so we have to test those.
2024 // Some are simple:
2025 case ParenExprClass:
2026 case MemberExprClass:
2027 case CXXReinterpretCastExprClass:
2028 case CXXConstCastExprClass:
2029 case ConditionalOperatorClass:
2030 case CompoundLiteralExprClass:
2031 case ExtVectorElementExprClass:
2032 case InitListExprClass:
2033 case DesignatedInitExprClass:
2034 case ParenListExprClass:
2035 case VAArgExprClass:
2036 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002037 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002038 case ObjCIvarRefExprClass:
2039 case ObjCIsaExprClass:
2040 case ShuffleVectorExprClass:
2041 return CanSubExprsThrow(C, this);
2042
2043 // Some might be dependent for other reasons.
2044 case UnaryOperatorClass:
2045 case ArraySubscriptExprClass:
2046 case ImplicitCastExprClass:
2047 case CStyleCastExprClass:
2048 case CXXStaticCastExprClass:
2049 case CXXFunctionalCastExprClass:
2050 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002051 case CompoundAssignOperatorClass:
2052 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002053 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2054 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2055 }
2056
2057 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2058 case StmtExprClass:
2059 return CT_Can;
2060
2061 case ChooseExprClass:
2062 if (isTypeDependent() || isValueDependent())
2063 return CT_Dependent;
2064 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2065
Peter Collingbournef111d932011-04-15 00:35:48 +00002066 case GenericSelectionExprClass:
2067 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2068 return CT_Dependent;
2069 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2070
Sebastian Redl369e51f2010-09-10 20:55:33 +00002071 // Some expressions are always dependent.
2072 case DependentScopeDeclRefExprClass:
2073 case CXXUnresolvedConstructExprClass:
2074 case CXXDependentScopeMemberExprClass:
2075 return CT_Dependent;
2076
2077 default:
2078 // All other expressions don't have subexpressions, or else they are
2079 // unevaluated.
2080 return CT_Cannot;
2081 }
2082}
2083
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002084Expr* Expr::IgnoreParens() {
2085 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002086 while (true) {
2087 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2088 E = P->getSubExpr();
2089 continue;
2090 }
2091 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2092 if (P->getOpcode() == UO_Extension) {
2093 E = P->getSubExpr();
2094 continue;
2095 }
2096 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002097 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2098 if (!P->isResultDependent()) {
2099 E = P->getResultExpr();
2100 continue;
2101 }
2102 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002103 return E;
2104 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002105}
2106
Chris Lattner56f34942008-02-13 01:02:39 +00002107/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2108/// or CastExprs or ImplicitCastExprs, returning their operand.
2109Expr *Expr::IgnoreParenCasts() {
2110 Expr *E = this;
2111 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002112 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002113 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002114 continue;
2115 }
2116 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002117 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002118 continue;
2119 }
2120 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2121 if (P->getOpcode() == UO_Extension) {
2122 E = P->getSubExpr();
2123 continue;
2124 }
2125 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002126 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2127 if (!P->isResultDependent()) {
2128 E = P->getResultExpr();
2129 continue;
2130 }
2131 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002132 if (MaterializeTemporaryExpr *Materialize
2133 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2134 E = Materialize->GetTemporaryExpr();
2135 continue;
2136 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002137 if (SubstNonTypeTemplateParmExpr *NTTP
2138 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2139 E = NTTP->getReplacement();
2140 continue;
2141 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002142 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002143 }
2144}
2145
John McCall9c5d70c2010-12-04 08:24:19 +00002146/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2147/// casts. This is intended purely as a temporary workaround for code
2148/// that hasn't yet been rewritten to do the right thing about those
2149/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002150Expr *Expr::IgnoreParenLValueCasts() {
2151 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002152 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002153 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2154 E = P->getSubExpr();
2155 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002156 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002157 if (P->getCastKind() == CK_LValueToRValue) {
2158 E = P->getSubExpr();
2159 continue;
2160 }
John McCall9c5d70c2010-12-04 08:24:19 +00002161 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2162 if (P->getOpcode() == UO_Extension) {
2163 E = P->getSubExpr();
2164 continue;
2165 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002166 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2167 if (!P->isResultDependent()) {
2168 E = P->getResultExpr();
2169 continue;
2170 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002171 } else if (MaterializeTemporaryExpr *Materialize
2172 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2173 E = Materialize->GetTemporaryExpr();
2174 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002175 } else if (SubstNonTypeTemplateParmExpr *NTTP
2176 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2177 E = NTTP->getReplacement();
2178 continue;
John McCallf6a16482010-12-04 03:47:34 +00002179 }
2180 break;
2181 }
2182 return E;
2183}
2184
John McCall2fc46bf2010-05-05 22:59:52 +00002185Expr *Expr::IgnoreParenImpCasts() {
2186 Expr *E = this;
2187 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002188 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002189 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002190 continue;
2191 }
2192 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002193 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002194 continue;
2195 }
2196 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2197 if (P->getOpcode() == UO_Extension) {
2198 E = P->getSubExpr();
2199 continue;
2200 }
2201 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002202 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2203 if (!P->isResultDependent()) {
2204 E = P->getResultExpr();
2205 continue;
2206 }
2207 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002208 if (MaterializeTemporaryExpr *Materialize
2209 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2210 E = Materialize->GetTemporaryExpr();
2211 continue;
2212 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002213 if (SubstNonTypeTemplateParmExpr *NTTP
2214 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2215 E = NTTP->getReplacement();
2216 continue;
2217 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002218 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002219 }
2220}
2221
Hans Wennborg2f072b42011-06-09 17:06:51 +00002222Expr *Expr::IgnoreConversionOperator() {
2223 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002224 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002225 return MCE->getImplicitObjectArgument();
2226 }
2227 return this;
2228}
2229
Chris Lattnerecdd8412009-03-13 17:28:01 +00002230/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2231/// value (including ptr->int casts of the same size). Strip off any
2232/// ParenExpr or CastExprs, returning their operand.
2233Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2234 Expr *E = this;
2235 while (true) {
2236 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2237 E = P->getSubExpr();
2238 continue;
2239 }
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Chris Lattnerecdd8412009-03-13 17:28:01 +00002241 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2242 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002243 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002244 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Chris Lattnerecdd8412009-03-13 17:28:01 +00002246 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2247 E = SE;
2248 continue;
2249 }
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002251 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002252 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002253 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002254 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002255 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2256 E = SE;
2257 continue;
2258 }
2259 }
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002261 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2262 if (P->getOpcode() == UO_Extension) {
2263 E = P->getSubExpr();
2264 continue;
2265 }
2266 }
2267
Peter Collingbournef111d932011-04-15 00:35:48 +00002268 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2269 if (!P->isResultDependent()) {
2270 E = P->getResultExpr();
2271 continue;
2272 }
2273 }
2274
Douglas Gregorc0244c52011-09-08 17:56:33 +00002275 if (SubstNonTypeTemplateParmExpr *NTTP
2276 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2277 E = NTTP->getReplacement();
2278 continue;
2279 }
2280
Chris Lattnerecdd8412009-03-13 17:28:01 +00002281 return E;
2282 }
2283}
2284
Douglas Gregor6eef5192009-12-14 19:27:10 +00002285bool Expr::isDefaultArgument() const {
2286 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002287 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2288 E = M->GetTemporaryExpr();
2289
Douglas Gregor6eef5192009-12-14 19:27:10 +00002290 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2291 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002292
Douglas Gregor6eef5192009-12-14 19:27:10 +00002293 return isa<CXXDefaultArgExpr>(E);
2294}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002295
Douglas Gregor2f599792010-04-02 18:24:57 +00002296/// \brief Skip over any no-op casts and any temporary-binding
2297/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002298static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002299 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2300 E = M->GetTemporaryExpr();
2301
Douglas Gregor2f599792010-04-02 18:24:57 +00002302 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002303 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002304 E = ICE->getSubExpr();
2305 else
2306 break;
2307 }
2308
2309 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2310 E = BE->getSubExpr();
2311
2312 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002313 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002314 E = ICE->getSubExpr();
2315 else
2316 break;
2317 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002318
2319 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002320}
2321
John McCall558d2ab2010-09-15 10:14:12 +00002322/// isTemporaryObject - Determines if this expression produces a
2323/// temporary of the given class type.
2324bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2325 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2326 return false;
2327
Anders Carlssonf8b30152010-11-28 16:40:49 +00002328 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002329
John McCall58277b52010-09-15 20:59:13 +00002330 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002331 if (!E->Classify(C).isPRValue()) {
2332 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002333 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002334 return false;
2335 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002336
John McCall19e60ad2010-09-16 06:57:56 +00002337 // Black-list a few cases which yield pr-values of class type that don't
2338 // refer to temporaries of that type:
2339
2340 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002341 if (isa<ImplicitCastExpr>(E)) {
2342 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2343 case CK_DerivedToBase:
2344 case CK_UncheckedDerivedToBase:
2345 return false;
2346 default:
2347 break;
2348 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002349 }
2350
John McCall19e60ad2010-09-16 06:57:56 +00002351 // - member expressions (all)
2352 if (isa<MemberExpr>(E))
2353 return false;
2354
John McCall56ca35d2011-02-17 10:25:35 +00002355 // - opaque values (all)
2356 if (isa<OpaqueValueExpr>(E))
2357 return false;
2358
John McCall558d2ab2010-09-15 10:14:12 +00002359 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002360}
2361
Douglas Gregor75e85042011-03-02 21:06:53 +00002362bool Expr::isImplicitCXXThis() const {
2363 const Expr *E = this;
2364
2365 // Strip away parentheses and casts we don't care about.
2366 while (true) {
2367 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2368 E = Paren->getSubExpr();
2369 continue;
2370 }
2371
2372 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2373 if (ICE->getCastKind() == CK_NoOp ||
2374 ICE->getCastKind() == CK_LValueToRValue ||
2375 ICE->getCastKind() == CK_DerivedToBase ||
2376 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2377 E = ICE->getSubExpr();
2378 continue;
2379 }
2380 }
2381
2382 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2383 if (UnOp->getOpcode() == UO_Extension) {
2384 E = UnOp->getSubExpr();
2385 continue;
2386 }
2387 }
2388
Douglas Gregor03e80032011-06-21 17:03:29 +00002389 if (const MaterializeTemporaryExpr *M
2390 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2391 E = M->GetTemporaryExpr();
2392 continue;
2393 }
2394
Douglas Gregor75e85042011-03-02 21:06:53 +00002395 break;
2396 }
2397
2398 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2399 return This->isImplicit();
2400
2401 return false;
2402}
2403
Douglas Gregor898574e2008-12-05 23:32:09 +00002404/// hasAnyTypeDependentArguments - Determines if any of the expressions
2405/// in Exprs is type-dependent.
2406bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2407 for (unsigned I = 0; I < NumExprs; ++I)
2408 if (Exprs[I]->isTypeDependent())
2409 return true;
2410
2411 return false;
2412}
2413
2414/// hasAnyValueDependentArguments - Determines if any of the expressions
2415/// in Exprs is value-dependent.
2416bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2417 for (unsigned I = 0; I < NumExprs; ++I)
2418 if (Exprs[I]->isValueDependent())
2419 return true;
2420
2421 return false;
2422}
2423
John McCall4204f072010-08-02 21:13:48 +00002424bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002425 // This function is attempting whether an expression is an initializer
2426 // which can be evaluated at compile-time. isEvaluatable handles most
2427 // of the cases, but it can't deal with some initializer-specific
2428 // expressions, and it can't deal with aggregates; we deal with those here,
2429 // and fall back to isEvaluatable for the other cases.
2430
John McCall4204f072010-08-02 21:13:48 +00002431 // If we ever capture reference-binding directly in the AST, we can
2432 // kill the second parameter.
2433
2434 if (IsForRef) {
2435 EvalResult Result;
2436 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2437 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002438
Anders Carlssone8a32b82008-11-24 05:23:59 +00002439 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002440 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002441 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002442 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002443 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002444 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002445 case CXXTemporaryObjectExprClass:
2446 case CXXConstructExprClass: {
2447 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002448
2449 // Only if it's
2450 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002451 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002452 if (!CE->getNumArgs()) return true;
2453
2454 // 2) an elidable trivial copy construction of an operand which is
2455 // itself a constant initializer. Note that we consider the
2456 // operand on its own, *not* as a reference binding.
2457 return CE->isElidable() &&
2458 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002459 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002460 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002461 // This handles gcc's extension that allows global initializers like
2462 // "struct x {int x;} x = (struct x) {};".
2463 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002464 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002465 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002466 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002467 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002468 // FIXME: This doesn't deal with fields with reference types correctly.
2469 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2470 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002471 const InitListExpr *Exp = cast<InitListExpr>(this);
2472 unsigned numInits = Exp->getNumInits();
2473 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002474 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002475 return false;
2476 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002477 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002478 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002479 case ImplicitValueInitExprClass:
2480 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002481 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002482 return cast<ParenExpr>(this)->getSubExpr()
2483 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002484 case GenericSelectionExprClass:
2485 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2486 return false;
2487 return cast<GenericSelectionExpr>(this)->getResultExpr()
2488 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002489 case ChooseExprClass:
2490 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2491 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002492 case UnaryOperatorClass: {
2493 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002494 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002495 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002496 break;
2497 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002498 case BinaryOperatorClass: {
2499 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2500 // but this handles the common case.
2501 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002502 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002503 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2504 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2505 return true;
2506 break;
2507 }
John McCall4204f072010-08-02 21:13:48 +00002508 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002509 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002510 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002511 case CStyleCastExprClass:
2512 // Handle casts with a destination that's a struct or union; this
2513 // deals with both the gcc no-op struct cast extension and the
2514 // cast-to-union extension.
2515 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002516 return cast<CastExpr>(this)->getSubExpr()
2517 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002518
Chris Lattner430656e2009-10-13 22:12:09 +00002519 // Integer->integer casts can be handled here, which is important for
2520 // things like (int)(&&x-&&y). Scary but true.
2521 if (getType()->isIntegerType() &&
2522 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002523 return cast<CastExpr>(this)->getSubExpr()
2524 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002525
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002526 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002527
2528 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002529 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002530 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002531 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002532 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002533}
2534
Chandler Carruth82214a82011-02-18 23:54:50 +00002535/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2536/// pointer constant or not, as well as the specific kind of constant detected.
2537/// Null pointer constants can be integer constant expressions with the
2538/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2539/// (a GNU extension).
2540Expr::NullPointerConstantKind
2541Expr::isNullPointerConstant(ASTContext &Ctx,
2542 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002543 if (isValueDependent()) {
2544 switch (NPC) {
2545 case NPC_NeverValueDependent:
2546 assert(false && "Unexpected value dependent expression!");
2547 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002548
Douglas Gregorce940492009-09-25 04:25:58 +00002549 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002550 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2551 return NPCK_ZeroInteger;
2552 else
2553 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002554
Douglas Gregorce940492009-09-25 04:25:58 +00002555 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002556 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002557 }
2558 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002559
Sebastian Redl07779722008-10-31 14:43:28 +00002560 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002561 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002562 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002563 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002564 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002565 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002566 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002567 Pointee->isVoidType() && // to void*
2568 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002569 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002570 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002572 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2573 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002574 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002575 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2576 // Accept ((void*)0) as a null pointer constant, as many other
2577 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002578 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002579 } else if (const GenericSelectionExpr *GE =
2580 dyn_cast<GenericSelectionExpr>(this)) {
2581 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002582 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002583 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002584 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002585 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002586 } else if (isa<GNUNullExpr>(this)) {
2587 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002588 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002589 } else if (const MaterializeTemporaryExpr *M
2590 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2591 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002592 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002593
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002594 // C++0x nullptr_t is always a null pointer constant.
2595 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002596 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002597
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002598 if (const RecordType *UT = getType()->getAsUnionType())
2599 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2600 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2601 const Expr *InitExpr = CLE->getInitializer();
2602 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2603 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2604 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002605 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002606 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002607 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002608 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 // If we have an integer constant expression, we need to *evaluate* it and
2611 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002612 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002613 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2614
2615 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002616}
Steve Naroff31a45842007-07-28 23:10:27 +00002617
John McCallf6a16482010-12-04 03:47:34 +00002618/// \brief If this expression is an l-value for an Objective C
2619/// property, find the underlying property reference expression.
2620const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2621 const Expr *E = this;
2622 while (true) {
2623 assert((E->getValueKind() == VK_LValue &&
2624 E->getObjectKind() == OK_ObjCProperty) &&
2625 "expression is not a property reference");
2626 E = E->IgnoreParenCasts();
2627 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2628 if (BO->getOpcode() == BO_Comma) {
2629 E = BO->getRHS();
2630 continue;
2631 }
2632 }
2633
2634 break;
2635 }
2636
2637 return cast<ObjCPropertyRefExpr>(E);
2638}
2639
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002640FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002641 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002642
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002643 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002644 if (ICE->getCastKind() == CK_LValueToRValue ||
2645 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002646 E = ICE->getSubExpr()->IgnoreParens();
2647 else
2648 break;
2649 }
2650
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002651 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002652 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002653 if (Field->isBitField())
2654 return Field;
2655
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002656 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2657 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2658 if (Field->isBitField())
2659 return Field;
2660
Eli Friedman42068e92011-07-13 02:05:57 +00002661 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002662 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2663 return BinOp->getLHS()->getBitField();
2664
Eli Friedman42068e92011-07-13 02:05:57 +00002665 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2666 return BinOp->getRHS()->getBitField();
2667 }
2668
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002669 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002670}
2671
Anders Carlsson09380262010-01-31 17:18:49 +00002672bool Expr::refersToVectorElement() const {
2673 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002674
Anders Carlsson09380262010-01-31 17:18:49 +00002675 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002676 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002677 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002678 E = ICE->getSubExpr()->IgnoreParens();
2679 else
2680 break;
2681 }
Sean Huntc3021132010-05-05 15:23:54 +00002682
Anders Carlsson09380262010-01-31 17:18:49 +00002683 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2684 return ASE->getBase()->getType()->isVectorType();
2685
2686 if (isa<ExtVectorElementExpr>(E))
2687 return true;
2688
2689 return false;
2690}
2691
Chris Lattner2140e902009-02-16 22:14:05 +00002692/// isArrow - Return true if the base expression is a pointer to vector,
2693/// return false if the base expression is a vector.
2694bool ExtVectorElementExpr::isArrow() const {
2695 return getBase()->getType()->isPointerType();
2696}
2697
Nate Begeman213541a2008-04-18 23:10:10 +00002698unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002699 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002700 return VT->getNumElements();
2701 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002702}
2703
Nate Begeman8a997642008-05-09 06:41:27 +00002704/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002705bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002706 // FIXME: Refactor this code to an accessor on the AST node which returns the
2707 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002708 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002709
2710 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002711 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002712 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Nate Begeman190d6a22009-01-18 02:01:21 +00002714 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002715 if (Comp[0] == 's' || Comp[0] == 'S')
2716 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Daniel Dunbar15027422009-10-17 23:53:04 +00002718 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002719 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002720 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002721
Steve Narofffec0b492007-07-30 03:29:09 +00002722 return false;
2723}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002724
Nate Begeman8a997642008-05-09 06:41:27 +00002725/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002726void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002727 SmallVectorImpl<unsigned> &Elts) const {
2728 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002729 if (Comp[0] == 's' || Comp[0] == 'S')
2730 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002731
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002732 bool isHi = Comp == "hi";
2733 bool isLo = Comp == "lo";
2734 bool isEven = Comp == "even";
2735 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Nate Begeman8a997642008-05-09 06:41:27 +00002737 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2738 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Nate Begeman8a997642008-05-09 06:41:27 +00002740 if (isHi)
2741 Index = e + i;
2742 else if (isLo)
2743 Index = i;
2744 else if (isEven)
2745 Index = 2 * i;
2746 else if (isOdd)
2747 Index = 2 * i + 1;
2748 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002749 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002750
Nate Begeman3b8d1162008-05-13 21:03:02 +00002751 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002752 }
Nate Begeman8a997642008-05-09 06:41:27 +00002753}
2754
Douglas Gregor04badcf2010-04-21 00:45:42 +00002755ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002756 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002757 SourceLocation LBracLoc,
2758 SourceLocation SuperLoc,
2759 bool IsInstanceSuper,
2760 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002761 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002762 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002763 ObjCMethodDecl *Method,
2764 Expr **Args, unsigned NumArgs,
2765 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002766 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002767 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002768 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002769 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002770 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCallf85e1932011-06-15 23:02:42 +00002771 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002772 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2773 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002774 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002775{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002776 setReceiverPointer(SuperType.getAsOpaquePtr());
2777 if (NumArgs)
2778 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002779}
2780
Douglas Gregor04badcf2010-04-21 00:45:42 +00002781ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002782 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002783 SourceLocation LBracLoc,
2784 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002785 Selector Sel,
2786 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002787 ObjCMethodDecl *Method,
2788 Expr **Args, unsigned NumArgs,
2789 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002790 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002791 T->isDependentType(), T->isInstantiationDependentType(),
2792 T->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002793 NumArgs(NumArgs), Kind(Class),
2794 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002795 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2796 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002797 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002798{
2799 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002800 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002801 for (unsigned I = 0; I != NumArgs; ++I) {
2802 if (Args[I]->isTypeDependent())
2803 ExprBits.TypeDependent = true;
2804 if (Args[I]->isValueDependent())
2805 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002806 if (Args[I]->isInstantiationDependent())
2807 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002808 if (Args[I]->containsUnexpandedParameterPack())
2809 ExprBits.ContainsUnexpandedParameterPack = true;
2810
2811 MyArgs[I] = Args[I];
2812 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002813}
2814
Douglas Gregor04badcf2010-04-21 00:45:42 +00002815ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002816 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002817 SourceLocation LBracLoc,
2818 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002819 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002820 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002821 ObjCMethodDecl *Method,
2822 Expr **Args, unsigned NumArgs,
2823 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002824 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002825 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002826 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002827 Receiver->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002828 NumArgs(NumArgs), Kind(Instance),
2829 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002830 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2831 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002832 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002833{
2834 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002835 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002836 for (unsigned I = 0; I != NumArgs; ++I) {
2837 if (Args[I]->isTypeDependent())
2838 ExprBits.TypeDependent = true;
2839 if (Args[I]->isValueDependent())
2840 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002841 if (Args[I]->isInstantiationDependent())
2842 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002843 if (Args[I]->containsUnexpandedParameterPack())
2844 ExprBits.ContainsUnexpandedParameterPack = true;
2845
2846 MyArgs[I] = Args[I];
2847 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002848}
2849
Douglas Gregor04badcf2010-04-21 00:45:42 +00002850ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002851 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002852 SourceLocation LBracLoc,
2853 SourceLocation SuperLoc,
2854 bool IsInstanceSuper,
2855 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002856 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002857 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002858 ObjCMethodDecl *Method,
2859 Expr **Args, unsigned NumArgs,
2860 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002861 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002862 NumArgs * sizeof(Expr *);
2863 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002864 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002865 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002866 RBracLoc);
2867}
2868
2869ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002870 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002871 SourceLocation LBracLoc,
2872 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002873 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002874 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002875 ObjCMethodDecl *Method,
2876 Expr **Args, unsigned NumArgs,
2877 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002878 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002879 NumArgs * sizeof(Expr *);
2880 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002881 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2882 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002883}
2884
2885ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002886 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002887 SourceLocation LBracLoc,
2888 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002889 Selector Sel,
2890 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002891 ObjCMethodDecl *Method,
2892 Expr **Args, unsigned NumArgs,
2893 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002894 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002895 NumArgs * sizeof(Expr *);
2896 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002897 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2898 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002899}
2900
Sean Huntc3021132010-05-05 15:23:54 +00002901ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002902 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002903 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002904 NumArgs * sizeof(Expr *);
2905 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2906 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2907}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002908
2909SourceRange ObjCMessageExpr::getReceiverRange() const {
2910 switch (getReceiverKind()) {
2911 case Instance:
2912 return getInstanceReceiver()->getSourceRange();
2913
2914 case Class:
2915 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2916
2917 case SuperInstance:
2918 case SuperClass:
2919 return getSuperLoc();
2920 }
2921
2922 return SourceLocation();
2923}
2924
Douglas Gregor04badcf2010-04-21 00:45:42 +00002925Selector ObjCMessageExpr::getSelector() const {
2926 if (HasMethod)
2927 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2928 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002929 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002930}
2931
2932ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2933 switch (getReceiverKind()) {
2934 case Instance:
2935 if (const ObjCObjectPointerType *Ptr
2936 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2937 return Ptr->getInterfaceDecl();
2938 break;
2939
2940 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002941 if (const ObjCObjectType *Ty
2942 = getClassReceiver()->getAs<ObjCObjectType>())
2943 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002944 break;
2945
2946 case SuperInstance:
2947 if (const ObjCObjectPointerType *Ptr
2948 = getSuperType()->getAs<ObjCObjectPointerType>())
2949 return Ptr->getInterfaceDecl();
2950 break;
2951
2952 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002953 if (const ObjCObjectType *Iface
2954 = getSuperType()->getAs<ObjCObjectType>())
2955 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002956 break;
2957 }
2958
2959 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002960}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002961
Chris Lattner5f9e2722011-07-23 10:55:15 +00002962StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00002963 switch (getBridgeKind()) {
2964 case OBC_Bridge:
2965 return "__bridge";
2966 case OBC_BridgeTransfer:
2967 return "__bridge_transfer";
2968 case OBC_BridgeRetained:
2969 return "__bridge_retained";
2970 }
2971
2972 return "__bridge";
2973}
2974
Jay Foad4ba2a172011-01-12 09:06:06 +00002975bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002976 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002977}
2978
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002979ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2980 QualType Type, SourceLocation BLoc,
2981 SourceLocation RP)
2982 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2983 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002984 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002985 Type->containsUnexpandedParameterPack()),
2986 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2987{
2988 SubExprs = new (C) Stmt*[nexpr];
2989 for (unsigned i = 0; i < nexpr; i++) {
2990 if (args[i]->isTypeDependent())
2991 ExprBits.TypeDependent = true;
2992 if (args[i]->isValueDependent())
2993 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002994 if (args[i]->isInstantiationDependent())
2995 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002996 if (args[i]->containsUnexpandedParameterPack())
2997 ExprBits.ContainsUnexpandedParameterPack = true;
2998
2999 SubExprs[i] = args[i];
3000 }
3001}
3002
Nate Begeman888376a2009-08-12 02:28:50 +00003003void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3004 unsigned NumExprs) {
3005 if (SubExprs) C.Deallocate(SubExprs);
3006
3007 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003008 this->NumExprs = NumExprs;
3009 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003010}
Nate Begeman888376a2009-08-12 02:28:50 +00003011
Peter Collingbournef111d932011-04-15 00:35:48 +00003012GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3013 SourceLocation GenericLoc, Expr *ControllingExpr,
3014 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3015 unsigned NumAssocs, SourceLocation DefaultLoc,
3016 SourceLocation RParenLoc,
3017 bool ContainsUnexpandedParameterPack,
3018 unsigned ResultIndex)
3019 : Expr(GenericSelectionExprClass,
3020 AssocExprs[ResultIndex]->getType(),
3021 AssocExprs[ResultIndex]->getValueKind(),
3022 AssocExprs[ResultIndex]->getObjectKind(),
3023 AssocExprs[ResultIndex]->isTypeDependent(),
3024 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003025 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003026 ContainsUnexpandedParameterPack),
3027 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3028 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3029 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3030 RParenLoc(RParenLoc) {
3031 SubExprs[CONTROLLING] = ControllingExpr;
3032 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3033 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3034}
3035
3036GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3037 SourceLocation GenericLoc, Expr *ControllingExpr,
3038 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3039 unsigned NumAssocs, SourceLocation DefaultLoc,
3040 SourceLocation RParenLoc,
3041 bool ContainsUnexpandedParameterPack)
3042 : Expr(GenericSelectionExprClass,
3043 Context.DependentTy,
3044 VK_RValue,
3045 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003046 /*isTypeDependent=*/true,
3047 /*isValueDependent=*/true,
3048 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003049 ContainsUnexpandedParameterPack),
3050 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3051 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3052 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3053 RParenLoc(RParenLoc) {
3054 SubExprs[CONTROLLING] = ControllingExpr;
3055 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3056 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3057}
3058
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003059//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003060// DesignatedInitExpr
3061//===----------------------------------------------------------------------===//
3062
Chandler Carruthb1138242011-06-16 06:47:06 +00003063IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003064 assert(Kind == FieldDesignator && "Only valid on a field designator");
3065 if (Field.NameOrField & 0x01)
3066 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3067 else
3068 return getField()->getIdentifier();
3069}
3070
Sean Huntc3021132010-05-05 15:23:54 +00003071DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003072 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003073 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003074 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003075 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003076 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003077 unsigned NumIndexExprs,
3078 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003079 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003080 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003081 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003082 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003083 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003084 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3085 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003086 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003087
3088 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003089 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003090 *Child++ = Init;
3091
3092 // Copy the designators and their subexpressions, computing
3093 // value-dependence along the way.
3094 unsigned IndexIdx = 0;
3095 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003096 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003097
3098 if (this->Designators[I].isArrayDesignator()) {
3099 // Compute type- and value-dependence.
3100 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003101 if (Index->isTypeDependent() || Index->isValueDependent())
3102 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003103 if (Index->isInstantiationDependent())
3104 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003105 // Propagate unexpanded parameter packs.
3106 if (Index->containsUnexpandedParameterPack())
3107 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003108
3109 // Copy the index expressions into permanent storage.
3110 *Child++ = IndexExprs[IndexIdx++];
3111 } else if (this->Designators[I].isArrayRangeDesignator()) {
3112 // Compute type- and value-dependence.
3113 Expr *Start = IndexExprs[IndexIdx];
3114 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003115 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003116 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003117 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003118 ExprBits.InstantiationDependent = true;
3119 } else if (Start->isInstantiationDependent() ||
3120 End->isInstantiationDependent()) {
3121 ExprBits.InstantiationDependent = true;
3122 }
3123
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003124 // Propagate unexpanded parameter packs.
3125 if (Start->containsUnexpandedParameterPack() ||
3126 End->containsUnexpandedParameterPack())
3127 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003128
3129 // Copy the start/end expressions into permanent storage.
3130 *Child++ = IndexExprs[IndexIdx++];
3131 *Child++ = IndexExprs[IndexIdx++];
3132 }
3133 }
3134
3135 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003136}
3137
Douglas Gregor05c13a32009-01-22 00:58:24 +00003138DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003139DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003140 unsigned NumDesignators,
3141 Expr **IndexExprs, unsigned NumIndexExprs,
3142 SourceLocation ColonOrEqualLoc,
3143 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003144 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003145 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003146 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003147 ColonOrEqualLoc, UsesColonSyntax,
3148 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003149}
3150
Mike Stump1eb44332009-09-09 15:08:12 +00003151DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003152 unsigned NumIndexExprs) {
3153 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3154 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3155 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3156}
3157
Douglas Gregor319d57f2010-01-06 23:17:19 +00003158void DesignatedInitExpr::setDesignators(ASTContext &C,
3159 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003160 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003161 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003162 NumDesignators = NumDesigs;
3163 for (unsigned I = 0; I != NumDesigs; ++I)
3164 Designators[I] = Desigs[I];
3165}
3166
Abramo Bagnara24f46742011-03-16 15:08:46 +00003167SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3168 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3169 if (size() == 1)
3170 return DIE->getDesignator(0)->getSourceRange();
3171 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3172 DIE->getDesignator(size()-1)->getEndLocation());
3173}
3174
Douglas Gregor05c13a32009-01-22 00:58:24 +00003175SourceRange DesignatedInitExpr::getSourceRange() const {
3176 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003177 Designator &First =
3178 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003179 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003180 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003181 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3182 else
3183 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3184 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003185 StartLoc =
3186 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003187 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3188}
3189
Douglas Gregor05c13a32009-01-22 00:58:24 +00003190Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3191 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3192 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3193 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003194 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3195 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3196}
3197
3198Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003199 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003200 "Requires array range designator");
3201 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3202 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003203 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3204 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3205}
3206
3207Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003208 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003209 "Requires array range designator");
3210 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3211 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003212 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3213 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3214}
3215
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003216/// \brief Replaces the designator at index @p Idx with the series
3217/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003218void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003219 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003220 const Designator *Last) {
3221 unsigned NumNewDesignators = Last - First;
3222 if (NumNewDesignators == 0) {
3223 std::copy_backward(Designators + Idx + 1,
3224 Designators + NumDesignators,
3225 Designators + Idx);
3226 --NumNewDesignators;
3227 return;
3228 } else if (NumNewDesignators == 1) {
3229 Designators[Idx] = *First;
3230 return;
3231 }
3232
Mike Stump1eb44332009-09-09 15:08:12 +00003233 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003234 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003235 std::copy(Designators, Designators + Idx, NewDesignators);
3236 std::copy(First, Last, NewDesignators + Idx);
3237 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3238 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003239 Designators = NewDesignators;
3240 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3241}
3242
Mike Stump1eb44332009-09-09 15:08:12 +00003243ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003244 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003245 SourceLocation rparenloc, QualType T)
3246 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003247 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003248 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003249 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003250 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003251 for (unsigned i = 0; i != nexprs; ++i) {
3252 if (exprs[i]->isTypeDependent())
3253 ExprBits.TypeDependent = true;
3254 if (exprs[i]->isValueDependent())
3255 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003256 if (exprs[i]->isInstantiationDependent())
3257 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003258 if (exprs[i]->containsUnexpandedParameterPack())
3259 ExprBits.ContainsUnexpandedParameterPack = true;
3260
Nate Begeman2ef13e52009-08-10 23:49:36 +00003261 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003262 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003263}
3264
John McCalle996ffd2011-02-16 08:02:54 +00003265const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3266 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3267 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003268 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3269 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003270 e = cast<CXXConstructExpr>(e)->getArg(0);
3271 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3272 e = ice->getSubExpr();
3273 return cast<OpaqueValueExpr>(e);
3274}
3275
Douglas Gregor05c13a32009-01-22 00:58:24 +00003276//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003277// ExprIterator.
3278//===----------------------------------------------------------------------===//
3279
3280Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3281Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3282Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3283const Expr* ConstExprIterator::operator[](size_t idx) const {
3284 return cast<Expr>(I[idx]);
3285}
3286const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3287const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3288
3289//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003290// Child Iterators for iterating over subexpressions/substatements
3291//===----------------------------------------------------------------------===//
3292
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003293// UnaryExprOrTypeTraitExpr
3294Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003295 // If this is of a type and the type is a VLA type (and not a typedef), the
3296 // size expression of the VLA needs to be treated as an executable expression.
3297 // Why isn't this weirdness documented better in StmtIterator?
3298 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003299 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003300 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003301 return child_range(child_iterator(T), child_iterator());
3302 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003303 }
John McCall63c00d72011-02-09 08:16:59 +00003304 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003305}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003306
Steve Naroff563477d2007-09-18 23:55:05 +00003307// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003308Stmt::child_range ObjCMessageExpr::children() {
3309 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003310 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003311 begin = reinterpret_cast<Stmt **>(this + 1);
3312 else
3313 begin = reinterpret_cast<Stmt **>(getArgs());
3314 return child_range(begin,
3315 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003316}
3317
Steve Naroff4eb206b2008-09-03 18:15:37 +00003318// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003319BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003320 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003321 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003322 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003323 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003324 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003325{
Douglas Gregord967e312011-01-19 21:52:31 +00003326 bool TypeDependent = false;
3327 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003328 bool InstantiationDependent = false;
3329 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3330 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003331 ExprBits.TypeDependent = TypeDependent;
3332 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003333 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003334}