blob: 987213907e4561c4b789f71a433f028677027ccc [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(
145 const TemplateArgumentListInfo &Info,
146 bool &Dependent,
147 bool &ContainsUnexpandedParameterPack) {
148 LAngleLoc = Info.getLAngleLoc();
149 RAngleLoc = Info.getRAngleLoc();
150 NumTemplateArgs = Info.size();
151
152 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
153 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
154 Dependent = Dependent || Info[i].getArgument().isDependent();
155 ContainsUnexpandedParameterPack
156 = ContainsUnexpandedParameterPack ||
157 Info[i].getArgument().containsUnexpandedParameterPack();
158
159 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
160 }
161}
162
John McCalld5532b62009-11-23 01:53:49 +0000163void ExplicitTemplateArgumentList::copyInto(
164 TemplateArgumentListInfo &Info) const {
165 Info.setLAngleLoc(LAngleLoc);
166 Info.setRAngleLoc(RAngleLoc);
167 for (unsigned I = 0; I != NumTemplateArgs; ++I)
168 Info.addArgument(getTemplateArgs()[I]);
169}
170
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000171std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
172 return sizeof(ExplicitTemplateArgumentList) +
173 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
174}
175
John McCalld5532b62009-11-23 01:53:49 +0000176std::size_t ExplicitTemplateArgumentList::sizeFor(
177 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000178 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000179}
180
Douglas Gregord967e312011-01-19 21:52:31 +0000181/// \brief Compute the type- and value-dependence of a declaration reference
182/// based on the declaration being referenced.
183static void computeDeclRefDependence(NamedDecl *D, QualType T,
184 bool &TypeDependent,
185 bool &ValueDependent) {
186 TypeDependent = false;
187 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000188
Douglas Gregor0da76df2009-11-23 11:41:28 +0000189
190 // (TD) C++ [temp.dep.expr]p3:
191 // An id-expression is type-dependent if it contains:
192 //
Sean Huntc3021132010-05-05 15:23:54 +0000193 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000194 //
195 // (VD) C++ [temp.dep.constexpr]p2:
196 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000197
Douglas Gregor0da76df2009-11-23 11:41:28 +0000198 // (TD) - an identifier that was declared with dependent type
199 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000200 if (T->isDependentType()) {
201 TypeDependent = true;
202 ValueDependent = true;
203 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000204 }
Douglas Gregord967e312011-01-19 21:52:31 +0000205
Douglas Gregor0da76df2009-11-23 11:41:28 +0000206 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000207 if (D->getDeclName().getNameKind()
208 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000209 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000210 TypeDependent = true;
211 ValueDependent = true;
212 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000213 }
214 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000215 if (isa<NonTypeTemplateParmDecl>(D)) {
216 ValueDependent = true;
217 return;
218 }
219
Douglas Gregor0da76df2009-11-23 11:41:28 +0000220 // (VD) - a constant with integral or enumeration type and is
221 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000222 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000223 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000224 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000225 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000226 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000227 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000228 }
Douglas Gregord967e312011-01-19 21:52:31 +0000229
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000230 // (VD) - FIXME: Missing from the standard:
231 // - a member function or a static data member of the current
232 // instantiation
233 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000234 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000235 ValueDependent = true;
236
237 return;
238 }
239
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000240 // (VD) - FIXME: Missing from the standard:
241 // - a member function or a static data member of the current
242 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000243 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
244 ValueDependent = true;
245 return;
246 }
247}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000248
Douglas Gregord967e312011-01-19 21:52:31 +0000249void DeclRefExpr::computeDependence() {
250 bool TypeDependent = false;
251 bool ValueDependent = false;
252 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
253
254 // (TD) C++ [temp.dep.expr]p3:
255 // An id-expression is type-dependent if it contains:
256 //
257 // and
258 //
259 // (VD) C++ [temp.dep.constexpr]p2:
260 // An identifier is value-dependent if it is:
261 if (!TypeDependent && !ValueDependent &&
262 hasExplicitTemplateArgs() &&
263 TemplateSpecializationType::anyDependentTemplateArguments(
264 getTemplateArgs(),
265 getNumTemplateArgs())) {
266 TypeDependent = true;
267 ValueDependent = true;
268 }
269
270 ExprBits.TypeDependent = TypeDependent;
271 ExprBits.ValueDependent = ValueDependent;
272
Douglas Gregor10738d32010-12-23 23:51:58 +0000273 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000274 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000275 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000276}
277
Chandler Carruth3aa81402011-05-01 23:48:14 +0000278DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000279 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000280 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000281 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000282 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000283 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000284 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
285 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000286 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000287 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000288 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
289 if (FoundD)
290 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000291 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Abramo Bagnara25777432010-08-11 22:01:17 +0000292 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000293 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000294
295 computeDependence();
296}
297
Douglas Gregora2813ce2009-10-23 18:54:35 +0000298DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000299 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000300 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000301 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000302 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000303 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000304 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000305 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000306 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000308 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000309}
310
311DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000312 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000313 ValueDecl *D,
314 const DeclarationNameInfo &NameInfo,
315 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000316 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000317 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000318 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000319 // Filter out cases where the found Decl is the same as the value refenenced.
320 if (D == FoundD)
321 FoundD = 0;
322
Douglas Gregora2813ce2009-10-23 18:54:35 +0000323 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000324 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000325 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000326 if (FoundD)
327 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000328 if (TemplateArgs)
329 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000330
Chris Lattner32488542010-10-30 05:14:06 +0000331 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000332 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
333 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000334}
335
Chandler Carruth3aa81402011-05-01 23:48:14 +0000336DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000337 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000339 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000340 unsigned NumTemplateArgs) {
341 std::size_t Size = sizeof(DeclRefExpr);
342 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000343 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000344 if (HasFoundDecl)
345 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000346 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000347 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000348
Chris Lattner32488542010-10-30 05:14:06 +0000349 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 return new (Mem) DeclRefExpr(EmptyShell());
351}
352
Douglas Gregora2813ce2009-10-23 18:54:35 +0000353SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000354 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000355 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000356 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000357 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000358 R.setEnd(getRAngleLoc());
359 return R;
360}
361
Anders Carlsson3a082d82009-09-08 18:24:21 +0000362// FIXME: Maybe this should use DeclPrinter with a special "print predefined
363// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000364std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
365 ASTContext &Context = CurrentDecl->getASTContext();
366
Anders Carlsson3a082d82009-09-08 18:24:21 +0000367 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000368 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000369 return FD->getNameAsString();
370
371 llvm::SmallString<256> Name;
372 llvm::raw_svector_ostream Out(Name);
373
374 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000375 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000376 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000377 if (MD->isStatic())
378 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000379 }
380
381 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000382
383 std::string Proto = FD->getQualifiedNameAsString(Policy);
384
John McCall183700f2009-09-21 23:43:11 +0000385 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000386 const FunctionProtoType *FT = 0;
387 if (FD->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
389
390 Proto += "(";
391 if (FT) {
392 llvm::raw_string_ostream POut(Proto);
393 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
394 if (i) POut << ", ";
395 std::string Param;
396 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
397 POut << Param;
398 }
399
400 if (FT->isVariadic()) {
401 if (FD->getNumParams()) POut << ", ";
402 POut << "...";
403 }
404 }
405 Proto += ")";
406
Sam Weinig4eadcc52009-12-27 01:38:20 +0000407 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
408 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
409 if (ThisQuals.hasConst())
410 Proto += " const";
411 if (ThisQuals.hasVolatile())
412 Proto += " volatile";
413 }
414
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000415 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
416 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000417
418 Out << Proto;
419
420 Out.flush();
421 return Name.str().str();
422 }
423 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
424 llvm::SmallString<256> Name;
425 llvm::raw_svector_ostream Out(Name);
426 Out << (MD->isInstanceMethod() ? '-' : '+');
427 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000428
429 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
430 // a null check to avoid a crash.
431 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000432 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000433
Anders Carlsson3a082d82009-09-08 18:24:21 +0000434 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000435 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
436 Out << '(' << CID << ')';
437
Anders Carlsson3a082d82009-09-08 18:24:21 +0000438 Out << ' ';
439 Out << MD->getSelector().getAsString();
440 Out << ']';
441
442 Out.flush();
443 return Name.str().str();
444 }
445 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
446 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
447 return "top level";
448 }
449 return "";
450}
451
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000452void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
453 if (hasAllocation())
454 C.Deallocate(pVal);
455
456 BitWidth = Val.getBitWidth();
457 unsigned NumWords = Val.getNumWords();
458 const uint64_t* Words = Val.getRawData();
459 if (NumWords > 1) {
460 pVal = new (C) uint64_t[NumWords];
461 std::copy(Words, Words + NumWords, pVal);
462 } else if (NumWords == 1)
463 VAL = Words[0];
464 else
465 VAL = 0;
466}
467
468IntegerLiteral *
469IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
470 QualType type, SourceLocation l) {
471 return new (C) IntegerLiteral(C, V, type, l);
472}
473
474IntegerLiteral *
475IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
476 return new (C) IntegerLiteral(Empty);
477}
478
479FloatingLiteral *
480FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
481 bool isexact, QualType Type, SourceLocation L) {
482 return new (C) FloatingLiteral(C, V, isexact, Type, L);
483}
484
485FloatingLiteral *
486FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
487 return new (C) FloatingLiteral(Empty);
488}
489
Chris Lattnerda8249e2008-06-07 22:13:43 +0000490/// getValueAsApproximateDouble - This returns the value as an inaccurate
491/// double. Note that this may cause loss of precision, but is useful for
492/// debugging dumps, etc.
493double FloatingLiteral::getValueAsApproximateDouble() const {
494 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000495 bool ignored;
496 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
497 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000498 return V.convertToDouble();
499}
500
Chris Lattner2085fd62009-02-18 06:40:38 +0000501StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
502 unsigned ByteLength, bool Wide,
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000503 bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000504 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000505 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000506 // Allocate enough space for the StringLiteral plus an array of locations for
507 // any concatenated string tokens.
508 void *Mem = C.Allocate(sizeof(StringLiteral)+
509 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000510 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000511 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000514 char *AStrData = new (C, 1) char[ByteLength];
515 memcpy(AStrData, StrData, ByteLength);
516 SL->StrData = AStrData;
517 SL->ByteLength = ByteLength;
518 SL->IsWide = Wide;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000519 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000520 SL->TokLocs[0] = Loc[0];
521 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000522
Chris Lattner726e1682009-02-18 05:49:11 +0000523 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000524 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
525 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000526}
527
Douglas Gregor673ecd62009-04-15 16:35:07 +0000528StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
529 void *Mem = C.Allocate(sizeof(StringLiteral)+
530 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000531 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000532 StringLiteral *SL = new (Mem) StringLiteral(QualType());
533 SL->StrData = 0;
534 SL->ByteLength = 0;
535 SL->NumConcatenated = NumStrs;
536 return SL;
537}
538
Daniel Dunbarb6480232009-09-22 03:27:33 +0000539void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000540 char *AStrData = new (C, 1) char[Str.size()];
541 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000542 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000543 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000544}
545
Chris Lattner08f92e32010-11-17 07:37:15 +0000546/// getLocationOfByte - Return a source location that points to the specified
547/// byte of this string literal.
548///
549/// Strings are amazingly complex. They can be formed from multiple tokens and
550/// can have escape sequences in them in addition to the usual trigraph and
551/// escaped newline business. This routine handles this complexity.
552///
553SourceLocation StringLiteral::
554getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
555 const LangOptions &Features, const TargetInfo &Target) const {
556 assert(!isWide() && "This doesn't work for wide strings yet");
557
558 // Loop over all of the tokens in this string until we find the one that
559 // contains the byte we're looking for.
560 unsigned TokNo = 0;
561 while (1) {
562 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
563 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
564
565 // Get the spelling of the string so that we can get the data that makes up
566 // the string literal, not the identifier for the macro it is potentially
567 // expanded through.
568 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
569
570 // Re-lex the token to get its length and original spelling.
571 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
572 bool Invalid = false;
573 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
574 if (Invalid)
575 return StrTokSpellingLoc;
576
577 const char *StrData = Buffer.data()+LocInfo.second;
578
579 // Create a langops struct and enable trigraphs. This is sufficient for
580 // relexing tokens.
581 LangOptions LangOpts;
582 LangOpts.Trigraphs = true;
583
584 // Create a lexer starting at the beginning of this token.
585 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
586 Buffer.end());
587 Token TheTok;
588 TheLexer.LexFromRawLexer(TheTok);
589
590 // Use the StringLiteralParser to compute the length of the string in bytes.
591 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
592 unsigned TokNumBytes = SLP.GetStringLength();
593
594 // If the byte is in this token, return the location of the byte.
595 if (ByteNo < TokNumBytes ||
596 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
597 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
598
599 // Now that we know the offset of the token in the spelling, use the
600 // preprocessor to get the offset in the original source.
601 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
602 }
603
604 // Move to the next string token.
605 ++TokNo;
606 ByteNo -= TokNumBytes;
607 }
608}
609
610
611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
613/// corresponds to, e.g. "sizeof" or "[pre]++".
614const char *UnaryOperator::getOpcodeStr(Opcode Op) {
615 switch (Op) {
616 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000617 case UO_PostInc: return "++";
618 case UO_PostDec: return "--";
619 case UO_PreInc: return "++";
620 case UO_PreDec: return "--";
621 case UO_AddrOf: return "&";
622 case UO_Deref: return "*";
623 case UO_Plus: return "+";
624 case UO_Minus: return "-";
625 case UO_Not: return "~";
626 case UO_LNot: return "!";
627 case UO_Real: return "__real";
628 case UO_Imag: return "__imag";
629 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 }
631}
632
John McCall2de56d12010-08-25 11:45:40 +0000633UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000634UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
635 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000636 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000637 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
638 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
639 case OO_Amp: return UO_AddrOf;
640 case OO_Star: return UO_Deref;
641 case OO_Plus: return UO_Plus;
642 case OO_Minus: return UO_Minus;
643 case OO_Tilde: return UO_Not;
644 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000645 }
646}
647
648OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
649 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000650 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
651 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
652 case UO_AddrOf: return OO_Amp;
653 case UO_Deref: return OO_Star;
654 case UO_Plus: return OO_Plus;
655 case UO_Minus: return OO_Minus;
656 case UO_Not: return OO_Tilde;
657 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000658 default: return OO_None;
659 }
660}
661
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663//===----------------------------------------------------------------------===//
664// Postfix Operators.
665//===----------------------------------------------------------------------===//
666
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000667CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
668 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000669 SourceLocation rparenloc)
670 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000671 fn->isTypeDependent(),
672 fn->isValueDependent(),
673 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000674 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000676 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000677 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000678 for (unsigned i = 0; i != numargs; ++i) {
679 if (args[i]->isTypeDependent())
680 ExprBits.TypeDependent = true;
681 if (args[i]->isValueDependent())
682 ExprBits.ValueDependent = true;
683 if (args[i]->containsUnexpandedParameterPack())
684 ExprBits.ContainsUnexpandedParameterPack = true;
685
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000686 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000687 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000688
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000689 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000690 RParenLoc = rparenloc;
691}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000692
Ted Kremenek668bf912009-02-09 20:51:47 +0000693CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000694 QualType t, ExprValueKind VK, SourceLocation rparenloc)
695 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000696 fn->isTypeDependent(),
697 fn->isValueDependent(),
698 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000699 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000700
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000701 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000702 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000703 for (unsigned i = 0; i != numargs; ++i) {
704 if (args[i]->isTypeDependent())
705 ExprBits.TypeDependent = true;
706 if (args[i]->isValueDependent())
707 ExprBits.ValueDependent = true;
708 if (args[i]->containsUnexpandedParameterPack())
709 ExprBits.ContainsUnexpandedParameterPack = true;
710
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000711 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000712 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000713
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000714 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 RParenLoc = rparenloc;
716}
717
Mike Stump1eb44332009-09-09 15:08:12 +0000718CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
719 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000720 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000721 SubExprs = new (C) Stmt*[PREARGS_START];
722 CallExprBits.NumPreArgs = 0;
723}
724
725CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
726 EmptyShell Empty)
727 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
728 // FIXME: Why do we allocate this?
729 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
730 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000731}
732
Nuno Lopesd20254f2009-12-20 23:11:08 +0000733Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000734 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000735 // If we're calling a dereference, look at the pointer instead.
736 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
737 if (BO->isPtrMemOp())
738 CEE = BO->getRHS()->IgnoreParenCasts();
739 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
740 if (UO->getOpcode() == UO_Deref)
741 CEE = UO->getSubExpr()->IgnoreParenCasts();
742 }
Chris Lattner6346f962009-07-17 15:46:27 +0000743 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000744 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000745 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
746 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000747
748 return 0;
749}
750
Nuno Lopesd20254f2009-12-20 23:11:08 +0000751FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000752 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000753}
754
Chris Lattnerd18b3292007-12-28 05:25:02 +0000755/// setNumArgs - This changes the number of arguments present in this call.
756/// Any orphaned expressions are deleted by this, and any new operands are set
757/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000758void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000759 // No change, just return.
760 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattnerd18b3292007-12-28 05:25:02 +0000762 // If shrinking # arguments, just delete the extras and forgot them.
763 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000764 this->NumArgs = NumArgs;
765 return;
766 }
767
768 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000769 unsigned NumPreArgs = getNumPreArgs();
770 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000771 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000772 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000773 NewSubExprs[i] = SubExprs[i];
774 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000775 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
776 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000777 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Douglas Gregor88c9a462009-04-17 21:46:47 +0000779 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000780 SubExprs = NewSubExprs;
781 this->NumArgs = NumArgs;
782}
783
Chris Lattnercb888962008-10-06 05:00:53 +0000784/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
785/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000786unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000787 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000788 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000789 // ImplicitCastExpr.
790 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
791 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000792 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000794 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
795 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000796 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Anders Carlssonbcba2012008-01-31 02:13:57 +0000798 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
799 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000800 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000802 if (!FDecl->getIdentifier())
803 return 0;
804
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000805 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000806}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000807
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000808QualType CallExpr::getCallReturnType() const {
809 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000810 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000811 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000812 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000813 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000814 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
815 // This should never be overloaded and so should never return null.
816 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000817
John McCall864c0412011-04-26 20:42:42 +0000818 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000819 return FnType->getResultType();
820}
Chris Lattnercb888962008-10-06 05:00:53 +0000821
John McCall2882eca2011-02-21 06:23:05 +0000822SourceRange CallExpr::getSourceRange() const {
823 if (isa<CXXOperatorCallExpr>(this))
824 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
825
826 SourceLocation begin = getCallee()->getLocStart();
827 if (begin.isInvalid() && getNumArgs() > 0)
828 begin = getArg(0)->getLocStart();
829 SourceLocation end = getRParenLoc();
830 if (end.isInvalid() && getNumArgs() > 0)
831 end = getArg(getNumArgs() - 1)->getLocEnd();
832 return SourceRange(begin, end);
833}
834
Sean Huntc3021132010-05-05 15:23:54 +0000835OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000836 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000837 TypeSourceInfo *tsi,
838 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000839 Expr** exprsPtr, unsigned numExprs,
840 SourceLocation RParenLoc) {
841 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000842 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000843 sizeof(Expr*) * numExprs);
844
845 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
846 exprsPtr, numExprs, RParenLoc);
847}
848
849OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
850 unsigned numComps, unsigned numExprs) {
851 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
852 sizeof(OffsetOfNode) * numComps +
853 sizeof(Expr*) * numExprs);
854 return new (Mem) OffsetOfExpr(numComps, numExprs);
855}
856
Sean Huntc3021132010-05-05 15:23:54 +0000857OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000858 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000859 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000860 Expr** exprsPtr, unsigned numExprs,
861 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000862 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
863 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000864 /*ValueDependent=*/tsi->getType()->isDependentType(),
865 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000866 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
867 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000868{
869 for(unsigned i = 0; i < numComps; ++i) {
870 setComponent(i, compsPtr[i]);
871 }
Sean Huntc3021132010-05-05 15:23:54 +0000872
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000873 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000874 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
875 ExprBits.ValueDependent = true;
876 if (exprsPtr[i]->containsUnexpandedParameterPack())
877 ExprBits.ContainsUnexpandedParameterPack = true;
878
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000879 setIndexExpr(i, exprsPtr[i]);
880 }
881}
882
883IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
884 assert(getKind() == Field || getKind() == Identifier);
885 if (getKind() == Field)
886 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000887
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000888 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
889}
890
Mike Stump1eb44332009-09-09 15:08:12 +0000891MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000892 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000893 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000894 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000895 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000896 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000897 QualType ty,
898 ExprValueKind vk,
899 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000900 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000901
Douglas Gregor40d96a62011-02-28 21:54:11 +0000902 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000903 founddecl.getDecl() != memberdecl ||
904 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000905 if (hasQualOrFound)
906 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
John McCalld5532b62009-11-23 01:53:49 +0000908 if (targs)
909 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chris Lattner32488542010-10-30 05:14:06 +0000911 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000912 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
913 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000914
915 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000916 // FIXME: Wrong. We should be looking at the member declaration we found.
917 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000918 E->setValueDependent(true);
919 E->setTypeDependent(true);
920 }
921 E->HasQualifierOrFoundDecl = true;
922
923 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000924 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000925 NQ->FoundDecl = founddecl;
926 }
927
928 if (targs) {
929 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000930 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000931 }
932
933 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000934}
935
Douglas Gregor75e85042011-03-02 21:06:53 +0000936SourceRange MemberExpr::getSourceRange() const {
937 SourceLocation StartLoc;
938 if (isImplicitAccess()) {
939 if (hasQualifier())
940 StartLoc = getQualifierLoc().getBeginLoc();
941 else
942 StartLoc = MemberLoc;
943 } else {
944 // FIXME: We don't want this to happen. Rather, we should be able to
945 // detect all kinds of implicit accesses more cleanly.
946 StartLoc = getBase()->getLocStart();
947 if (StartLoc.isInvalid())
948 StartLoc = MemberLoc;
949 }
950
951 SourceLocation EndLoc =
952 HasExplicitTemplateArgumentList? getRAngleLoc()
953 : getMemberNameInfo().getEndLoc();
954
955 return SourceRange(StartLoc, EndLoc);
956}
957
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000958const char *CastExpr::getCastKindName() const {
959 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000960 case CK_Dependent:
961 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000962 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000963 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000964 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000965 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000966 case CK_LValueToRValue:
967 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000968 case CK_GetObjCProperty:
969 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000970 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000971 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000972 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000973 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000974 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000975 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000976 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000977 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000978 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000979 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000980 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000981 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000982 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000983 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000984 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000985 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000986 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000987 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000988 case CK_NullToPointer:
989 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000990 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000991 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000992 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000993 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000994 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000995 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000996 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000997 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000998 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000999 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001000 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001001 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001002 case CK_PointerToBoolean:
1003 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001004 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001005 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001006 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001007 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001008 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001009 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001010 case CK_IntegralToBoolean:
1011 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001012 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001013 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001014 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001015 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001016 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001017 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001018 case CK_FloatingToBoolean:
1019 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001020 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001021 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001022 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001023 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001024 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001025 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001026 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001027 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001028 case CK_FloatingRealToComplex:
1029 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001030 case CK_FloatingComplexToReal:
1031 return "FloatingComplexToReal";
1032 case CK_FloatingComplexToBoolean:
1033 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001034 case CK_FloatingComplexCast:
1035 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001036 case CK_FloatingComplexToIntegralComplex:
1037 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001038 case CK_IntegralRealToComplex:
1039 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001040 case CK_IntegralComplexToReal:
1041 return "IntegralComplexToReal";
1042 case CK_IntegralComplexToBoolean:
1043 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001044 case CK_IntegralComplexCast:
1045 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001046 case CK_IntegralComplexToFloatingComplex:
1047 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
John McCall2bb5d002010-11-13 09:02:35 +00001050 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001051 return 0;
1052}
1053
Douglas Gregor6eef5192009-12-14 19:27:10 +00001054Expr *CastExpr::getSubExprAsWritten() {
1055 Expr *SubExpr = 0;
1056 CastExpr *E = this;
1057 do {
1058 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001059
Douglas Gregor6eef5192009-12-14 19:27:10 +00001060 // Skip any temporary bindings; they're implicit.
1061 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1062 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001063
Douglas Gregor6eef5192009-12-14 19:27:10 +00001064 // Conversions by constructor and conversion functions have a
1065 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001066 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001067 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001068 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001069 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001070
Douglas Gregor6eef5192009-12-14 19:27:10 +00001071 // If the subexpression we're left with is an implicit cast, look
1072 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001073 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1074
Douglas Gregor6eef5192009-12-14 19:27:10 +00001075 return SubExpr;
1076}
1077
John McCallf871d0c2010-08-07 06:22:56 +00001078CXXBaseSpecifier **CastExpr::path_buffer() {
1079 switch (getStmtClass()) {
1080#define ABSTRACT_STMT(x)
1081#define CASTEXPR(Type, Base) \
1082 case Stmt::Type##Class: \
1083 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1084#define STMT(Type, Base)
1085#include "clang/AST/StmtNodes.inc"
1086 default:
1087 llvm_unreachable("non-cast expressions not possible here");
1088 return 0;
1089 }
1090}
1091
1092void CastExpr::setCastPath(const CXXCastPath &Path) {
1093 assert(Path.size() == path_size());
1094 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1095}
1096
1097ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1098 CastKind Kind, Expr *Operand,
1099 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001100 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001101 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1102 void *Buffer =
1103 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1104 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001105 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001106 if (PathSize) E->setCastPath(*BasePath);
1107 return E;
1108}
1109
1110ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1111 unsigned PathSize) {
1112 void *Buffer =
1113 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1114 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1115}
1116
1117
1118CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001119 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001120 const CXXCastPath *BasePath,
1121 TypeSourceInfo *WrittenTy,
1122 SourceLocation L, SourceLocation R) {
1123 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1124 void *Buffer =
1125 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1126 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001127 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001128 if (PathSize) E->setCastPath(*BasePath);
1129 return E;
1130}
1131
1132CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1133 void *Buffer =
1134 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1135 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1136}
1137
Reid Spencer5f016e22007-07-11 17:01:13 +00001138/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1139/// corresponds to, e.g. "<<=".
1140const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1141 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001142 case BO_PtrMemD: return ".*";
1143 case BO_PtrMemI: return "->*";
1144 case BO_Mul: return "*";
1145 case BO_Div: return "/";
1146 case BO_Rem: return "%";
1147 case BO_Add: return "+";
1148 case BO_Sub: return "-";
1149 case BO_Shl: return "<<";
1150 case BO_Shr: return ">>";
1151 case BO_LT: return "<";
1152 case BO_GT: return ">";
1153 case BO_LE: return "<=";
1154 case BO_GE: return ">=";
1155 case BO_EQ: return "==";
1156 case BO_NE: return "!=";
1157 case BO_And: return "&";
1158 case BO_Xor: return "^";
1159 case BO_Or: return "|";
1160 case BO_LAnd: return "&&";
1161 case BO_LOr: return "||";
1162 case BO_Assign: return "=";
1163 case BO_MulAssign: return "*=";
1164 case BO_DivAssign: return "/=";
1165 case BO_RemAssign: return "%=";
1166 case BO_AddAssign: return "+=";
1167 case BO_SubAssign: return "-=";
1168 case BO_ShlAssign: return "<<=";
1169 case BO_ShrAssign: return ">>=";
1170 case BO_AndAssign: return "&=";
1171 case BO_XorAssign: return "^=";
1172 case BO_OrAssign: return "|=";
1173 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001175
1176 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001177}
1178
John McCall2de56d12010-08-25 11:45:40 +00001179BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001180BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1181 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001182 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001183 case OO_Plus: return BO_Add;
1184 case OO_Minus: return BO_Sub;
1185 case OO_Star: return BO_Mul;
1186 case OO_Slash: return BO_Div;
1187 case OO_Percent: return BO_Rem;
1188 case OO_Caret: return BO_Xor;
1189 case OO_Amp: return BO_And;
1190 case OO_Pipe: return BO_Or;
1191 case OO_Equal: return BO_Assign;
1192 case OO_Less: return BO_LT;
1193 case OO_Greater: return BO_GT;
1194 case OO_PlusEqual: return BO_AddAssign;
1195 case OO_MinusEqual: return BO_SubAssign;
1196 case OO_StarEqual: return BO_MulAssign;
1197 case OO_SlashEqual: return BO_DivAssign;
1198 case OO_PercentEqual: return BO_RemAssign;
1199 case OO_CaretEqual: return BO_XorAssign;
1200 case OO_AmpEqual: return BO_AndAssign;
1201 case OO_PipeEqual: return BO_OrAssign;
1202 case OO_LessLess: return BO_Shl;
1203 case OO_GreaterGreater: return BO_Shr;
1204 case OO_LessLessEqual: return BO_ShlAssign;
1205 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1206 case OO_EqualEqual: return BO_EQ;
1207 case OO_ExclaimEqual: return BO_NE;
1208 case OO_LessEqual: return BO_LE;
1209 case OO_GreaterEqual: return BO_GE;
1210 case OO_AmpAmp: return BO_LAnd;
1211 case OO_PipePipe: return BO_LOr;
1212 case OO_Comma: return BO_Comma;
1213 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001214 }
1215}
1216
1217OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1218 static const OverloadedOperatorKind OverOps[] = {
1219 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1220 OO_Star, OO_Slash, OO_Percent,
1221 OO_Plus, OO_Minus,
1222 OO_LessLess, OO_GreaterGreater,
1223 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1224 OO_EqualEqual, OO_ExclaimEqual,
1225 OO_Amp,
1226 OO_Caret,
1227 OO_Pipe,
1228 OO_AmpAmp,
1229 OO_PipePipe,
1230 OO_Equal, OO_StarEqual,
1231 OO_SlashEqual, OO_PercentEqual,
1232 OO_PlusEqual, OO_MinusEqual,
1233 OO_LessLessEqual, OO_GreaterGreaterEqual,
1234 OO_AmpEqual, OO_CaretEqual,
1235 OO_PipeEqual,
1236 OO_Comma
1237 };
1238 return OverOps[Opc];
1239}
1240
Ted Kremenek709210f2010-04-13 23:39:13 +00001241InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001242 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001243 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001244 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1245 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001246 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001247 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001248 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001249{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001250 for (unsigned I = 0; I != numInits; ++I) {
1251 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001252 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001253 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001254 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001255 if (initExprs[I]->containsUnexpandedParameterPack())
1256 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001257 }
Sean Huntc3021132010-05-05 15:23:54 +00001258
Ted Kremenek709210f2010-04-13 23:39:13 +00001259 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001260}
Reid Spencer5f016e22007-07-11 17:01:13 +00001261
Ted Kremenek709210f2010-04-13 23:39:13 +00001262void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001263 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001264 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001265}
1266
Ted Kremenek709210f2010-04-13 23:39:13 +00001267void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001268 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001269}
1270
Ted Kremenek709210f2010-04-13 23:39:13 +00001271Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001272 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001273 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001274 InitExprs.back() = expr;
1275 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregor4c678342009-01-28 21:54:33 +00001278 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1279 InitExprs[Init] = expr;
1280 return Result;
1281}
1282
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001283void InitListExpr::setArrayFiller(Expr *filler) {
1284 ArrayFillerOrUnionFieldInit = filler;
1285 // Fill out any "holes" in the array due to designated initializers.
1286 Expr **inits = getInits();
1287 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1288 if (inits[i] == 0)
1289 inits[i] = filler;
1290}
1291
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001292SourceRange InitListExpr::getSourceRange() const {
1293 if (SyntacticForm)
1294 return SyntacticForm->getSourceRange();
1295 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1296 if (Beg.isInvalid()) {
1297 // Find the first non-null initializer.
1298 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1299 E = InitExprs.end();
1300 I != E; ++I) {
1301 if (Stmt *S = *I) {
1302 Beg = S->getLocStart();
1303 break;
1304 }
1305 }
1306 }
1307 if (End.isInvalid()) {
1308 // Find the first non-null initializer from the end.
1309 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1310 E = InitExprs.rend();
1311 I != E; ++I) {
1312 if (Stmt *S = *I) {
1313 End = S->getSourceRange().getEnd();
1314 break;
1315 }
1316 }
1317 }
1318 return SourceRange(Beg, End);
1319}
1320
Steve Naroffbfdcae62008-09-04 15:31:07 +00001321/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001322///
1323const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001324 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001325 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001326}
1327
Mike Stump1eb44332009-09-09 15:08:12 +00001328SourceLocation BlockExpr::getCaretLocation() const {
1329 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001330}
Mike Stump1eb44332009-09-09 15:08:12 +00001331const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001332 return TheBlock->getBody();
1333}
Mike Stump1eb44332009-09-09 15:08:12 +00001334Stmt *BlockExpr::getBody() {
1335 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001336}
Steve Naroff56ee6892008-10-08 17:01:13 +00001337
1338
Reid Spencer5f016e22007-07-11 17:01:13 +00001339//===----------------------------------------------------------------------===//
1340// Generic Expression Routines
1341//===----------------------------------------------------------------------===//
1342
Chris Lattner026dc962009-02-14 07:37:35 +00001343/// isUnusedResultAWarning - Return true if this immediate expression should
1344/// be warned about if the result is unused. If so, fill in Loc and Ranges
1345/// with location to warn on and the source range[s] to report with the
1346/// warning.
1347bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001348 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001349 // Don't warn if the expr is type dependent. The type could end up
1350 // instantiating to void.
1351 if (isTypeDependent())
1352 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 switch (getStmtClass()) {
1355 default:
John McCall0faede62010-03-12 07:11:26 +00001356 if (getType()->isVoidType())
1357 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001358 Loc = getExprLoc();
1359 R1 = getSourceRange();
1360 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001362 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001363 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001364 case GenericSelectionExprClass:
1365 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1366 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 case UnaryOperatorClass: {
1368 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001371 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001372 case UO_PostInc:
1373 case UO_PostDec:
1374 case UO_PreInc:
1375 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001376 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001377 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001379 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001380 return false;
1381 break;
John McCall2de56d12010-08-25 11:45:40 +00001382 case UO_Real:
1383 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001385 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1386 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001387 return false;
1388 break;
John McCall2de56d12010-08-25 11:45:40 +00001389 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001390 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 }
Chris Lattner026dc962009-02-14 07:37:35 +00001392 Loc = UO->getOperatorLoc();
1393 R1 = UO->getSubExpr()->getSourceRange();
1394 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001396 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001397 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001398 switch (BO->getOpcode()) {
1399 default:
1400 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001401 // Consider the RHS of comma for side effects. LHS was checked by
1402 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001403 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001404 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1405 // lvalue-ness) of an assignment written in a macro.
1406 if (IntegerLiteral *IE =
1407 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1408 if (IE->getValue() == 0)
1409 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001410 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1411 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001412 case BO_LAnd:
1413 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001414 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1415 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1416 return false;
1417 break;
John McCallbf0ee352010-02-16 04:10:53 +00001418 }
Chris Lattner026dc962009-02-14 07:37:35 +00001419 if (BO->isAssignmentOp())
1420 return false;
1421 Loc = BO->getOperatorLoc();
1422 R1 = BO->getLHS()->getSourceRange();
1423 R2 = BO->getRHS()->getSourceRange();
1424 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001425 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001426 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001427 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001428 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001429
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001430 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001431 // If only one of the LHS or RHS is a warning, the operator might
1432 // be being used for control flow. Only warn if both the LHS and
1433 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001434 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001435 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1436 return false;
1437 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001438 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001439 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001440 }
1441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001443 // If the base pointer or element is to a volatile pointer/field, accessing
1444 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001445 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001446 return false;
1447 Loc = cast<MemberExpr>(this)->getMemberLoc();
1448 R1 = SourceRange(Loc, Loc);
1449 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1450 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 case ArraySubscriptExprClass:
1453 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001454 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001455 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001456 return false;
1457 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1458 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1459 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1460 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001461
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001463 case CXXOperatorCallExprClass:
1464 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001465 // If this is a direct call, get the callee.
1466 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001467 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001468 // If the callee has attribute pure, const, or warn_unused_result, warn
1469 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001470 //
1471 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1472 // updated to match for QoI.
1473 if (FD->getAttr<WarnUnusedResultAttr>() ||
1474 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1475 Loc = CE->getCallee()->getLocStart();
1476 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001478 if (unsigned NumArgs = CE->getNumArgs())
1479 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1480 CE->getArg(NumArgs-1)->getLocEnd());
1481 return true;
1482 }
Chris Lattner026dc962009-02-14 07:37:35 +00001483 }
1484 return false;
1485 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001486
1487 case CXXTemporaryObjectExprClass:
1488 case CXXConstructExprClass:
1489 return false;
1490
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001491 case ObjCMessageExprClass: {
1492 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1493 const ObjCMethodDecl *MD = ME->getMethodDecl();
1494 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1495 Loc = getExprLoc();
1496 return true;
1497 }
Chris Lattner026dc962009-02-14 07:37:35 +00001498 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001499 }
Mike Stump1eb44332009-09-09 15:08:12 +00001500
John McCall12f78a62010-12-02 01:19:52 +00001501 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001502 Loc = getExprLoc();
1503 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001504 return true;
John McCall12f78a62010-12-02 01:19:52 +00001505
Chris Lattner611b2ec2008-07-26 19:51:01 +00001506 case StmtExprClass: {
1507 // Statement exprs don't logically have side effects themselves, but are
1508 // sometimes used in macros in ways that give them a type that is unused.
1509 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1510 // however, if the result of the stmt expr is dead, we don't want to emit a
1511 // warning.
1512 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001513 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001514 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001515 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001516 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1517 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1518 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
John McCall0faede62010-03-12 07:11:26 +00001521 if (getType()->isVoidType())
1522 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001523 Loc = cast<StmtExpr>(this)->getLParenLoc();
1524 R1 = getSourceRange();
1525 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001526 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001527 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001528 // If this is an explicit cast to void, allow it. People do this when they
1529 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001530 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001531 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001532 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1533 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1534 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001535 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001536 if (getType()->isVoidType())
1537 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001538 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001539
Anders Carlsson58beed92009-11-17 17:11:23 +00001540 // If this is a cast to void or a constructor conversion, check the operand.
1541 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001542 if (CE->getCastKind() == CK_ToVoid ||
1543 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001544 return (cast<CastExpr>(this)->getSubExpr()
1545 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001546 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1547 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1548 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Eli Friedman4be1f472008-05-19 21:24:43 +00001551 case ImplicitCastExprClass:
1552 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return (cast<ImplicitCastExpr>(this)
1554 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001555
Chris Lattner04421082008-04-08 04:40:51 +00001556 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001557 return (cast<CXXDefaultArgExpr>(this)
1558 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001559
1560 case CXXNewExprClass:
1561 // FIXME: In theory, there might be new expressions that don't have side
1562 // effects (e.g. a placement new with an uninitialized POD).
1563 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001564 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001565 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001566 return (cast<CXXBindTemporaryExpr>(this)
1567 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001568 case ExprWithCleanupsClass:
1569 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001570 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001571 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001572}
1573
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001574/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001575/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001576bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001577 const Expr *E = IgnoreParens();
1578 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001579 default:
1580 return false;
1581 case ObjCIvarRefExprClass:
1582 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001583 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001584 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001585 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001586 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001587 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001588 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001589 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001590 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001591 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1592 if (VD->hasGlobalStorage())
1593 return true;
1594 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001595 // dereferencing to a pointer is always a gc'able candidate,
1596 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001597 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001598 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001599 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001600 return false;
1601 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001602 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001603 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001604 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001605 }
1606 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001607 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001608 }
1609}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001610
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001611bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1612 if (isTypeDependent())
1613 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001614 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001615}
1616
John McCall864c0412011-04-26 20:42:42 +00001617QualType Expr::findBoundMemberType(const Expr *expr) {
1618 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1619
1620 // Bound member expressions are always one of these possibilities:
1621 // x->m x.m x->*y x.*y
1622 // (possibly parenthesized)
1623
1624 expr = expr->IgnoreParens();
1625 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1626 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1627 return mem->getMemberDecl()->getType();
1628 }
1629
1630 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1631 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1632 ->getPointeeType();
1633 assert(type->isFunctionType());
1634 return type;
1635 }
1636
1637 assert(isa<UnresolvedMemberExpr>(expr));
1638 return QualType();
1639}
1640
Sebastian Redl369e51f2010-09-10 20:55:33 +00001641static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1642 Expr::CanThrowResult CT2) {
1643 // CanThrowResult constants are ordered so that the maximum is the correct
1644 // merge result.
1645 return CT1 > CT2 ? CT1 : CT2;
1646}
1647
1648static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1649 Expr *E = const_cast<Expr*>(CE);
1650 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001651 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001652 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1653 }
1654 return R;
1655}
1656
Richard Smith7a614d82011-06-11 17:19:42 +00001657static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1658 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001659 bool NullThrows = true) {
1660 if (!D)
1661 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1662
1663 // See if we can get a function type from the decl somehow.
1664 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1665 if (!VD) // If we have no clue what we're calling, assume the worst.
1666 return Expr::CT_Can;
1667
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001668 // As an extension, we assume that __attribute__((nothrow)) functions don't
1669 // throw.
1670 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1671 return Expr::CT_Cannot;
1672
Sebastian Redl369e51f2010-09-10 20:55:33 +00001673 QualType T = VD->getType();
1674 const FunctionProtoType *FT;
1675 if ((FT = T->getAs<FunctionProtoType>())) {
1676 } else if (const PointerType *PT = T->getAs<PointerType>())
1677 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1678 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1679 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1680 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1681 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1682 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1683 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1684
1685 if (!FT)
1686 return Expr::CT_Can;
1687
Richard Smith7a614d82011-06-11 17:19:42 +00001688 if (FT->getExceptionSpecType() == EST_Delayed) {
1689 assert(isa<CXXConstructorDecl>(D) &&
1690 "only constructor exception specs can be unknown");
1691 Ctx.getDiagnostics().Report(E->getLocStart(),
1692 diag::err_exception_spec_unknown)
1693 << E->getSourceRange();
1694 return Expr::CT_Can;
1695 }
1696
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001697 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001698}
1699
1700static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1701 if (DC->isTypeDependent())
1702 return Expr::CT_Dependent;
1703
Sebastian Redl295995c2010-09-10 20:55:47 +00001704 if (!DC->getTypeAsWritten()->isReferenceType())
1705 return Expr::CT_Cannot;
1706
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001707 if (DC->getSubExpr()->isTypeDependent())
1708 return Expr::CT_Dependent;
1709
Sebastian Redl369e51f2010-09-10 20:55:33 +00001710 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1711}
1712
1713static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1714 const CXXTypeidExpr *DC) {
1715 if (DC->isTypeOperand())
1716 return Expr::CT_Cannot;
1717
1718 Expr *Op = DC->getExprOperand();
1719 if (Op->isTypeDependent())
1720 return Expr::CT_Dependent;
1721
1722 const RecordType *RT = Op->getType()->getAs<RecordType>();
1723 if (!RT)
1724 return Expr::CT_Cannot;
1725
1726 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1727 return Expr::CT_Cannot;
1728
1729 if (Op->Classify(C).isPRValue())
1730 return Expr::CT_Cannot;
1731
1732 return Expr::CT_Can;
1733}
1734
1735Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1736 // C++ [expr.unary.noexcept]p3:
1737 // [Can throw] if in a potentially-evaluated context the expression would
1738 // contain:
1739 switch (getStmtClass()) {
1740 case CXXThrowExprClass:
1741 // - a potentially evaluated throw-expression
1742 return CT_Can;
1743
1744 case CXXDynamicCastExprClass: {
1745 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1746 // where T is a reference type, that requires a run-time check
1747 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1748 if (CT == CT_Can)
1749 return CT;
1750 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1751 }
1752
1753 case CXXTypeidExprClass:
1754 // - a potentially evaluated typeid expression applied to a glvalue
1755 // expression whose type is a polymorphic class type
1756 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1757
1758 // - a potentially evaluated call to a function, member function, function
1759 // pointer, or member function pointer that does not have a non-throwing
1760 // exception-specification
1761 case CallExprClass:
1762 case CXXOperatorCallExprClass:
1763 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001764 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001765 CanThrowResult CT;
1766 if (isTypeDependent())
1767 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001768 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1769 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001770 else
Richard Smith7a614d82011-06-11 17:19:42 +00001771 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001772 if (CT == CT_Can)
1773 return CT;
1774 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1775 }
1776
Sebastian Redl295995c2010-09-10 20:55:47 +00001777 case CXXConstructExprClass:
1778 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001779 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001780 cast<CXXConstructExpr>(this)->getConstructor());
1781 if (CT == CT_Can)
1782 return CT;
1783 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1784 }
1785
1786 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001787 CanThrowResult CT;
1788 if (isTypeDependent())
1789 CT = CT_Dependent;
1790 else
1791 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001792 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1793 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001794 /*NullThrows*/false));
1795 if (CT == CT_Can)
1796 return CT;
1797 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1798 }
1799
1800 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001801 CanThrowResult CT;
1802 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1803 if (DTy.isNull() || DTy->isDependentType()) {
1804 CT = CT_Dependent;
1805 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00001806 CT = CanCalleeThrow(C, this,
1807 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001808 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1809 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00001810 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001811 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001812 if (CT == CT_Can)
1813 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001814 }
1815 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1816 }
1817
1818 case CXXBindTemporaryExprClass: {
1819 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00001820 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001821 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1822 if (CT == CT_Can)
1823 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001824 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1825 }
1826
1827 // ObjC message sends are like function calls, but never have exception
1828 // specs.
1829 case ObjCMessageExprClass:
1830 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001831 return CT_Can;
1832
1833 // Many other things have subexpressions, so we have to test those.
1834 // Some are simple:
1835 case ParenExprClass:
1836 case MemberExprClass:
1837 case CXXReinterpretCastExprClass:
1838 case CXXConstCastExprClass:
1839 case ConditionalOperatorClass:
1840 case CompoundLiteralExprClass:
1841 case ExtVectorElementExprClass:
1842 case InitListExprClass:
1843 case DesignatedInitExprClass:
1844 case ParenListExprClass:
1845 case VAArgExprClass:
1846 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001847 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001848 case ObjCIvarRefExprClass:
1849 case ObjCIsaExprClass:
1850 case ShuffleVectorExprClass:
1851 return CanSubExprsThrow(C, this);
1852
1853 // Some might be dependent for other reasons.
1854 case UnaryOperatorClass:
1855 case ArraySubscriptExprClass:
1856 case ImplicitCastExprClass:
1857 case CStyleCastExprClass:
1858 case CXXStaticCastExprClass:
1859 case CXXFunctionalCastExprClass:
1860 case BinaryOperatorClass:
1861 case CompoundAssignOperatorClass: {
1862 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1863 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1864 }
1865
1866 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1867 case StmtExprClass:
1868 return CT_Can;
1869
1870 case ChooseExprClass:
1871 if (isTypeDependent() || isValueDependent())
1872 return CT_Dependent;
1873 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1874
Peter Collingbournef111d932011-04-15 00:35:48 +00001875 case GenericSelectionExprClass:
1876 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1877 return CT_Dependent;
1878 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1879
Sebastian Redl369e51f2010-09-10 20:55:33 +00001880 // Some expressions are always dependent.
1881 case DependentScopeDeclRefExprClass:
1882 case CXXUnresolvedConstructExprClass:
1883 case CXXDependentScopeMemberExprClass:
1884 return CT_Dependent;
1885
1886 default:
1887 // All other expressions don't have subexpressions, or else they are
1888 // unevaluated.
1889 return CT_Cannot;
1890 }
1891}
1892
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001893Expr* Expr::IgnoreParens() {
1894 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001895 while (true) {
1896 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1897 E = P->getSubExpr();
1898 continue;
1899 }
1900 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1901 if (P->getOpcode() == UO_Extension) {
1902 E = P->getSubExpr();
1903 continue;
1904 }
1905 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001906 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1907 if (!P->isResultDependent()) {
1908 E = P->getResultExpr();
1909 continue;
1910 }
1911 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001912 return E;
1913 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001914}
1915
Chris Lattner56f34942008-02-13 01:02:39 +00001916/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1917/// or CastExprs or ImplicitCastExprs, returning their operand.
1918Expr *Expr::IgnoreParenCasts() {
1919 Expr *E = this;
1920 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001921 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001922 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001923 continue;
1924 }
1925 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001926 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001927 continue;
1928 }
1929 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1930 if (P->getOpcode() == UO_Extension) {
1931 E = P->getSubExpr();
1932 continue;
1933 }
1934 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001935 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1936 if (!P->isResultDependent()) {
1937 E = P->getResultExpr();
1938 continue;
1939 }
1940 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001941 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001942 }
1943}
1944
John McCall9c5d70c2010-12-04 08:24:19 +00001945/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1946/// casts. This is intended purely as a temporary workaround for code
1947/// that hasn't yet been rewritten to do the right thing about those
1948/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001949Expr *Expr::IgnoreParenLValueCasts() {
1950 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001951 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001952 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1953 E = P->getSubExpr();
1954 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001955 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001956 if (P->getCastKind() == CK_LValueToRValue) {
1957 E = P->getSubExpr();
1958 continue;
1959 }
John McCall9c5d70c2010-12-04 08:24:19 +00001960 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1961 if (P->getOpcode() == UO_Extension) {
1962 E = P->getSubExpr();
1963 continue;
1964 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001965 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1966 if (!P->isResultDependent()) {
1967 E = P->getResultExpr();
1968 continue;
1969 }
John McCallf6a16482010-12-04 03:47:34 +00001970 }
1971 break;
1972 }
1973 return E;
1974}
1975
John McCall2fc46bf2010-05-05 22:59:52 +00001976Expr *Expr::IgnoreParenImpCasts() {
1977 Expr *E = this;
1978 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001979 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001980 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001981 continue;
1982 }
1983 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001984 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001985 continue;
1986 }
1987 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1988 if (P->getOpcode() == UO_Extension) {
1989 E = P->getSubExpr();
1990 continue;
1991 }
1992 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001993 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1994 if (!P->isResultDependent()) {
1995 E = P->getResultExpr();
1996 continue;
1997 }
1998 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001999 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002000 }
2001}
2002
Hans Wennborg2f072b42011-06-09 17:06:51 +00002003Expr *Expr::IgnoreConversionOperator() {
2004 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
2005 if (isa<CXXConversionDecl>(MCE->getMethodDecl()))
2006 return MCE->getImplicitObjectArgument();
2007 }
2008 return this;
2009}
2010
Chris Lattnerecdd8412009-03-13 17:28:01 +00002011/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2012/// value (including ptr->int casts of the same size). Strip off any
2013/// ParenExpr or CastExprs, returning their operand.
2014Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2015 Expr *E = this;
2016 while (true) {
2017 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2018 E = P->getSubExpr();
2019 continue;
2020 }
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Chris Lattnerecdd8412009-03-13 17:28:01 +00002022 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2023 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002024 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002025 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Chris Lattnerecdd8412009-03-13 17:28:01 +00002027 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2028 E = SE;
2029 continue;
2030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002032 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002033 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002034 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002035 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002036 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2037 E = SE;
2038 continue;
2039 }
2040 }
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002042 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2043 if (P->getOpcode() == UO_Extension) {
2044 E = P->getSubExpr();
2045 continue;
2046 }
2047 }
2048
Peter Collingbournef111d932011-04-15 00:35:48 +00002049 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2050 if (!P->isResultDependent()) {
2051 E = P->getResultExpr();
2052 continue;
2053 }
2054 }
2055
Chris Lattnerecdd8412009-03-13 17:28:01 +00002056 return E;
2057 }
2058}
2059
Douglas Gregor6eef5192009-12-14 19:27:10 +00002060bool Expr::isDefaultArgument() const {
2061 const Expr *E = this;
2062 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2063 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002064
Douglas Gregor6eef5192009-12-14 19:27:10 +00002065 return isa<CXXDefaultArgExpr>(E);
2066}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002067
Douglas Gregor2f599792010-04-02 18:24:57 +00002068/// \brief Skip over any no-op casts and any temporary-binding
2069/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002070static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002071 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002072 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002073 E = ICE->getSubExpr();
2074 else
2075 break;
2076 }
2077
2078 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2079 E = BE->getSubExpr();
2080
2081 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002082 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002083 E = ICE->getSubExpr();
2084 else
2085 break;
2086 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002087
2088 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002089}
2090
John McCall558d2ab2010-09-15 10:14:12 +00002091/// isTemporaryObject - Determines if this expression produces a
2092/// temporary of the given class type.
2093bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2094 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2095 return false;
2096
Anders Carlssonf8b30152010-11-28 16:40:49 +00002097 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002098
John McCall58277b52010-09-15 20:59:13 +00002099 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002100 if (!E->Classify(C).isPRValue()) {
2101 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002102 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002103 return false;
2104 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002105
John McCall19e60ad2010-09-16 06:57:56 +00002106 // Black-list a few cases which yield pr-values of class type that don't
2107 // refer to temporaries of that type:
2108
2109 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002110 if (isa<ImplicitCastExpr>(E)) {
2111 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2112 case CK_DerivedToBase:
2113 case CK_UncheckedDerivedToBase:
2114 return false;
2115 default:
2116 break;
2117 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002118 }
2119
John McCall19e60ad2010-09-16 06:57:56 +00002120 // - member expressions (all)
2121 if (isa<MemberExpr>(E))
2122 return false;
2123
John McCall56ca35d2011-02-17 10:25:35 +00002124 // - opaque values (all)
2125 if (isa<OpaqueValueExpr>(E))
2126 return false;
2127
John McCall558d2ab2010-09-15 10:14:12 +00002128 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002129}
2130
Douglas Gregor75e85042011-03-02 21:06:53 +00002131bool Expr::isImplicitCXXThis() const {
2132 const Expr *E = this;
2133
2134 // Strip away parentheses and casts we don't care about.
2135 while (true) {
2136 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2137 E = Paren->getSubExpr();
2138 continue;
2139 }
2140
2141 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2142 if (ICE->getCastKind() == CK_NoOp ||
2143 ICE->getCastKind() == CK_LValueToRValue ||
2144 ICE->getCastKind() == CK_DerivedToBase ||
2145 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2146 E = ICE->getSubExpr();
2147 continue;
2148 }
2149 }
2150
2151 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2152 if (UnOp->getOpcode() == UO_Extension) {
2153 E = UnOp->getSubExpr();
2154 continue;
2155 }
2156 }
2157
2158 break;
2159 }
2160
2161 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2162 return This->isImplicit();
2163
2164 return false;
2165}
2166
Douglas Gregor898574e2008-12-05 23:32:09 +00002167/// hasAnyTypeDependentArguments - Determines if any of the expressions
2168/// in Exprs is type-dependent.
2169bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2170 for (unsigned I = 0; I < NumExprs; ++I)
2171 if (Exprs[I]->isTypeDependent())
2172 return true;
2173
2174 return false;
2175}
2176
2177/// hasAnyValueDependentArguments - Determines if any of the expressions
2178/// in Exprs is value-dependent.
2179bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2180 for (unsigned I = 0; I < NumExprs; ++I)
2181 if (Exprs[I]->isValueDependent())
2182 return true;
2183
2184 return false;
2185}
2186
John McCall4204f072010-08-02 21:13:48 +00002187bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002188 // This function is attempting whether an expression is an initializer
2189 // which can be evaluated at compile-time. isEvaluatable handles most
2190 // of the cases, but it can't deal with some initializer-specific
2191 // expressions, and it can't deal with aggregates; we deal with those here,
2192 // and fall back to isEvaluatable for the other cases.
2193
John McCall4204f072010-08-02 21:13:48 +00002194 // If we ever capture reference-binding directly in the AST, we can
2195 // kill the second parameter.
2196
2197 if (IsForRef) {
2198 EvalResult Result;
2199 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2200 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002201
Anders Carlssone8a32b82008-11-24 05:23:59 +00002202 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002203 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002204 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002205 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002206 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002207 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002208 case CXXTemporaryObjectExprClass:
2209 case CXXConstructExprClass: {
2210 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002211
2212 // Only if it's
2213 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002214 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002215 if (!CE->getNumArgs()) return true;
2216
2217 // 2) an elidable trivial copy construction of an operand which is
2218 // itself a constant initializer. Note that we consider the
2219 // operand on its own, *not* as a reference binding.
2220 return CE->isElidable() &&
2221 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002222 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002223 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002224 // This handles gcc's extension that allows global initializers like
2225 // "struct x {int x;} x = (struct x) {};".
2226 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002227 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002228 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002229 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002230 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002231 // FIXME: This doesn't deal with fields with reference types correctly.
2232 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2233 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002234 const InitListExpr *Exp = cast<InitListExpr>(this);
2235 unsigned numInits = Exp->getNumInits();
2236 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002237 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002238 return false;
2239 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002240 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002241 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002242 case ImplicitValueInitExprClass:
2243 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002244 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002245 return cast<ParenExpr>(this)->getSubExpr()
2246 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002247 case GenericSelectionExprClass:
2248 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2249 return false;
2250 return cast<GenericSelectionExpr>(this)->getResultExpr()
2251 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002252 case ChooseExprClass:
2253 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2254 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002255 case UnaryOperatorClass: {
2256 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002257 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002258 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002259 break;
2260 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002261 case BinaryOperatorClass: {
2262 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2263 // but this handles the common case.
2264 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002265 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002266 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2267 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2268 return true;
2269 break;
2270 }
John McCall4204f072010-08-02 21:13:48 +00002271 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002272 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002273 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002274 case CStyleCastExprClass:
2275 // Handle casts with a destination that's a struct or union; this
2276 // deals with both the gcc no-op struct cast extension and the
2277 // cast-to-union extension.
2278 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002279 return cast<CastExpr>(this)->getSubExpr()
2280 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002281
Chris Lattner430656e2009-10-13 22:12:09 +00002282 // Integer->integer casts can be handled here, which is important for
2283 // things like (int)(&&x-&&y). Scary but true.
2284 if (getType()->isIntegerType() &&
2285 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002286 return cast<CastExpr>(this)->getSubExpr()
2287 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002288
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002289 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002290 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002291 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002292}
2293
Chandler Carruth82214a82011-02-18 23:54:50 +00002294/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2295/// pointer constant or not, as well as the specific kind of constant detected.
2296/// Null pointer constants can be integer constant expressions with the
2297/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2298/// (a GNU extension).
2299Expr::NullPointerConstantKind
2300Expr::isNullPointerConstant(ASTContext &Ctx,
2301 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002302 if (isValueDependent()) {
2303 switch (NPC) {
2304 case NPC_NeverValueDependent:
2305 assert(false && "Unexpected value dependent expression!");
2306 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002307
Douglas Gregorce940492009-09-25 04:25:58 +00002308 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002309 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2310 return NPCK_ZeroInteger;
2311 else
2312 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002313
Douglas Gregorce940492009-09-25 04:25:58 +00002314 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002315 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002316 }
2317 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002318
Sebastian Redl07779722008-10-31 14:43:28 +00002319 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002320 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002321 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002322 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002323 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002324 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002325 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002326 Pointee->isVoidType() && // to void*
2327 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002328 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002329 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002331 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2332 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002333 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002334 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2335 // Accept ((void*)0) as a null pointer constant, as many other
2336 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002337 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002338 } else if (const GenericSelectionExpr *GE =
2339 dyn_cast<GenericSelectionExpr>(this)) {
2340 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002341 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002342 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002343 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002344 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002345 } else if (isa<GNUNullExpr>(this)) {
2346 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002347 return NPCK_GNUNull;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002348 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002349
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002350 // C++0x nullptr_t is always a null pointer constant.
2351 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002352 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002353
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002354 if (const RecordType *UT = getType()->getAsUnionType())
2355 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2356 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2357 const Expr *InitExpr = CLE->getInitializer();
2358 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2359 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2360 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002361 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002362 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002363 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002364 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Reid Spencer5f016e22007-07-11 17:01:13 +00002366 // If we have an integer constant expression, we need to *evaluate* it and
2367 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002368 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002369 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2370
2371 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002372}
Steve Naroff31a45842007-07-28 23:10:27 +00002373
John McCallf6a16482010-12-04 03:47:34 +00002374/// \brief If this expression is an l-value for an Objective C
2375/// property, find the underlying property reference expression.
2376const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2377 const Expr *E = this;
2378 while (true) {
2379 assert((E->getValueKind() == VK_LValue &&
2380 E->getObjectKind() == OK_ObjCProperty) &&
2381 "expression is not a property reference");
2382 E = E->IgnoreParenCasts();
2383 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2384 if (BO->getOpcode() == BO_Comma) {
2385 E = BO->getRHS();
2386 continue;
2387 }
2388 }
2389
2390 break;
2391 }
2392
2393 return cast<ObjCPropertyRefExpr>(E);
2394}
2395
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002396FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002397 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002398
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002399 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002400 if (ICE->getCastKind() == CK_LValueToRValue ||
2401 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002402 E = ICE->getSubExpr()->IgnoreParens();
2403 else
2404 break;
2405 }
2406
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002407 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002408 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002409 if (Field->isBitField())
2410 return Field;
2411
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002412 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2413 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2414 if (Field->isBitField())
2415 return Field;
2416
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002417 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2418 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2419 return BinOp->getLHS()->getBitField();
2420
2421 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002422}
2423
Anders Carlsson09380262010-01-31 17:18:49 +00002424bool Expr::refersToVectorElement() const {
2425 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002426
Anders Carlsson09380262010-01-31 17:18:49 +00002427 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002428 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002429 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002430 E = ICE->getSubExpr()->IgnoreParens();
2431 else
2432 break;
2433 }
Sean Huntc3021132010-05-05 15:23:54 +00002434
Anders Carlsson09380262010-01-31 17:18:49 +00002435 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2436 return ASE->getBase()->getType()->isVectorType();
2437
2438 if (isa<ExtVectorElementExpr>(E))
2439 return true;
2440
2441 return false;
2442}
2443
Chris Lattner2140e902009-02-16 22:14:05 +00002444/// isArrow - Return true if the base expression is a pointer to vector,
2445/// return false if the base expression is a vector.
2446bool ExtVectorElementExpr::isArrow() const {
2447 return getBase()->getType()->isPointerType();
2448}
2449
Nate Begeman213541a2008-04-18 23:10:10 +00002450unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002451 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002452 return VT->getNumElements();
2453 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002454}
2455
Nate Begeman8a997642008-05-09 06:41:27 +00002456/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002457bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002458 // FIXME: Refactor this code to an accessor on the AST node which returns the
2459 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002460 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002461
2462 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002463 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002464 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Nate Begeman190d6a22009-01-18 02:01:21 +00002466 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002467 if (Comp[0] == 's' || Comp[0] == 'S')
2468 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Daniel Dunbar15027422009-10-17 23:53:04 +00002470 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2471 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002472 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002473
Steve Narofffec0b492007-07-30 03:29:09 +00002474 return false;
2475}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002476
Nate Begeman8a997642008-05-09 06:41:27 +00002477/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002478void ExtVectorElementExpr::getEncodedElementAccess(
2479 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002480 llvm::StringRef Comp = Accessor->getName();
2481 if (Comp[0] == 's' || Comp[0] == 'S')
2482 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002484 bool isHi = Comp == "hi";
2485 bool isLo = Comp == "lo";
2486 bool isEven = Comp == "even";
2487 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Nate Begeman8a997642008-05-09 06:41:27 +00002489 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2490 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Nate Begeman8a997642008-05-09 06:41:27 +00002492 if (isHi)
2493 Index = e + i;
2494 else if (isLo)
2495 Index = i;
2496 else if (isEven)
2497 Index = 2 * i;
2498 else if (isOdd)
2499 Index = 2 * i + 1;
2500 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002501 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002502
Nate Begeman3b8d1162008-05-13 21:03:02 +00002503 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002504 }
Nate Begeman8a997642008-05-09 06:41:27 +00002505}
2506
Douglas Gregor04badcf2010-04-21 00:45:42 +00002507ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002508 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002509 SourceLocation LBracLoc,
2510 SourceLocation SuperLoc,
2511 bool IsInstanceSuper,
2512 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002513 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002514 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002515 ObjCMethodDecl *Method,
2516 Expr **Args, unsigned NumArgs,
2517 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002518 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002519 /*TypeDependent=*/false, /*ValueDependent=*/false,
2520 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002521 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2522 HasMethod(Method != 0), SuperLoc(SuperLoc),
2523 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2524 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002525 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002526{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002527 setReceiverPointer(SuperType.getAsOpaquePtr());
2528 if (NumArgs)
2529 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002530}
2531
Douglas Gregor04badcf2010-04-21 00:45:42 +00002532ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002533 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002534 SourceLocation LBracLoc,
2535 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002536 Selector Sel,
2537 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002538 ObjCMethodDecl *Method,
2539 Expr **Args, unsigned NumArgs,
2540 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002541 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002542 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002543 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2544 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2545 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002546 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002547{
2548 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002549 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002550 for (unsigned I = 0; I != NumArgs; ++I) {
2551 if (Args[I]->isTypeDependent())
2552 ExprBits.TypeDependent = true;
2553 if (Args[I]->isValueDependent())
2554 ExprBits.ValueDependent = true;
2555 if (Args[I]->containsUnexpandedParameterPack())
2556 ExprBits.ContainsUnexpandedParameterPack = true;
2557
2558 MyArgs[I] = Args[I];
2559 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002560}
2561
Douglas Gregor04badcf2010-04-21 00:45:42 +00002562ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002563 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002564 SourceLocation LBracLoc,
2565 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002566 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002567 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002568 ObjCMethodDecl *Method,
2569 Expr **Args, unsigned NumArgs,
2570 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002571 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002572 Receiver->isTypeDependent(),
2573 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002574 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2575 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2576 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002577 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002578{
2579 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002580 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002581 for (unsigned I = 0; I != NumArgs; ++I) {
2582 if (Args[I]->isTypeDependent())
2583 ExprBits.TypeDependent = true;
2584 if (Args[I]->isValueDependent())
2585 ExprBits.ValueDependent = true;
2586 if (Args[I]->containsUnexpandedParameterPack())
2587 ExprBits.ContainsUnexpandedParameterPack = true;
2588
2589 MyArgs[I] = Args[I];
2590 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002591}
2592
Douglas Gregor04badcf2010-04-21 00:45:42 +00002593ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002594 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002595 SourceLocation LBracLoc,
2596 SourceLocation SuperLoc,
2597 bool IsInstanceSuper,
2598 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002599 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002600 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002601 ObjCMethodDecl *Method,
2602 Expr **Args, unsigned NumArgs,
2603 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002604 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002605 NumArgs * sizeof(Expr *);
2606 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002607 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002608 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002609 RBracLoc);
2610}
2611
2612ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002613 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002614 SourceLocation LBracLoc,
2615 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002616 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002617 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002618 ObjCMethodDecl *Method,
2619 Expr **Args, unsigned NumArgs,
2620 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002621 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002622 NumArgs * sizeof(Expr *);
2623 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002624 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2625 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002626}
2627
2628ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002629 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002630 SourceLocation LBracLoc,
2631 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002632 Selector Sel,
2633 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002634 ObjCMethodDecl *Method,
2635 Expr **Args, unsigned NumArgs,
2636 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002637 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002638 NumArgs * sizeof(Expr *);
2639 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002640 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2641 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002642}
2643
Sean Huntc3021132010-05-05 15:23:54 +00002644ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002645 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002646 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002647 NumArgs * sizeof(Expr *);
2648 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2649 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2650}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002651
2652SourceRange ObjCMessageExpr::getReceiverRange() const {
2653 switch (getReceiverKind()) {
2654 case Instance:
2655 return getInstanceReceiver()->getSourceRange();
2656
2657 case Class:
2658 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2659
2660 case SuperInstance:
2661 case SuperClass:
2662 return getSuperLoc();
2663 }
2664
2665 return SourceLocation();
2666}
2667
Douglas Gregor04badcf2010-04-21 00:45:42 +00002668Selector ObjCMessageExpr::getSelector() const {
2669 if (HasMethod)
2670 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2671 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002672 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002673}
2674
2675ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2676 switch (getReceiverKind()) {
2677 case Instance:
2678 if (const ObjCObjectPointerType *Ptr
2679 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2680 return Ptr->getInterfaceDecl();
2681 break;
2682
2683 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002684 if (const ObjCObjectType *Ty
2685 = getClassReceiver()->getAs<ObjCObjectType>())
2686 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002687 break;
2688
2689 case SuperInstance:
2690 if (const ObjCObjectPointerType *Ptr
2691 = getSuperType()->getAs<ObjCObjectPointerType>())
2692 return Ptr->getInterfaceDecl();
2693 break;
2694
2695 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002696 if (const ObjCObjectType *Iface
2697 = getSuperType()->getAs<ObjCObjectType>())
2698 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002699 break;
2700 }
2701
2702 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002703}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002704
Jay Foad4ba2a172011-01-12 09:06:06 +00002705bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002706 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002707}
2708
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002709ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2710 QualType Type, SourceLocation BLoc,
2711 SourceLocation RP)
2712 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2713 Type->isDependentType(), Type->isDependentType(),
2714 Type->containsUnexpandedParameterPack()),
2715 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2716{
2717 SubExprs = new (C) Stmt*[nexpr];
2718 for (unsigned i = 0; i < nexpr; i++) {
2719 if (args[i]->isTypeDependent())
2720 ExprBits.TypeDependent = true;
2721 if (args[i]->isValueDependent())
2722 ExprBits.ValueDependent = true;
2723 if (args[i]->containsUnexpandedParameterPack())
2724 ExprBits.ContainsUnexpandedParameterPack = true;
2725
2726 SubExprs[i] = args[i];
2727 }
2728}
2729
Nate Begeman888376a2009-08-12 02:28:50 +00002730void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2731 unsigned NumExprs) {
2732 if (SubExprs) C.Deallocate(SubExprs);
2733
2734 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002735 this->NumExprs = NumExprs;
2736 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002737}
Nate Begeman888376a2009-08-12 02:28:50 +00002738
Peter Collingbournef111d932011-04-15 00:35:48 +00002739GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2740 SourceLocation GenericLoc, Expr *ControllingExpr,
2741 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2742 unsigned NumAssocs, SourceLocation DefaultLoc,
2743 SourceLocation RParenLoc,
2744 bool ContainsUnexpandedParameterPack,
2745 unsigned ResultIndex)
2746 : Expr(GenericSelectionExprClass,
2747 AssocExprs[ResultIndex]->getType(),
2748 AssocExprs[ResultIndex]->getValueKind(),
2749 AssocExprs[ResultIndex]->getObjectKind(),
2750 AssocExprs[ResultIndex]->isTypeDependent(),
2751 AssocExprs[ResultIndex]->isValueDependent(),
2752 ContainsUnexpandedParameterPack),
2753 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2754 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2755 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2756 RParenLoc(RParenLoc) {
2757 SubExprs[CONTROLLING] = ControllingExpr;
2758 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2759 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2760}
2761
2762GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2763 SourceLocation GenericLoc, Expr *ControllingExpr,
2764 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2765 unsigned NumAssocs, SourceLocation DefaultLoc,
2766 SourceLocation RParenLoc,
2767 bool ContainsUnexpandedParameterPack)
2768 : Expr(GenericSelectionExprClass,
2769 Context.DependentTy,
2770 VK_RValue,
2771 OK_Ordinary,
2772 /*isTypeDependent=*/ true,
2773 /*isValueDependent=*/ true,
2774 ContainsUnexpandedParameterPack),
2775 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2776 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2777 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2778 RParenLoc(RParenLoc) {
2779 SubExprs[CONTROLLING] = ControllingExpr;
2780 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2781 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2782}
2783
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002784//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002785// DesignatedInitExpr
2786//===----------------------------------------------------------------------===//
2787
2788IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2789 assert(Kind == FieldDesignator && "Only valid on a field designator");
2790 if (Field.NameOrField & 0x01)
2791 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2792 else
2793 return getField()->getIdentifier();
2794}
2795
Sean Huntc3021132010-05-05 15:23:54 +00002796DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002797 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002798 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002799 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002800 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002801 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002802 unsigned NumIndexExprs,
2803 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002804 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002805 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002806 Init->isTypeDependent(), Init->isValueDependent(),
2807 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002808 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2809 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002810 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002811
2812 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002813 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002814 *Child++ = Init;
2815
2816 // Copy the designators and their subexpressions, computing
2817 // value-dependence along the way.
2818 unsigned IndexIdx = 0;
2819 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002820 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002821
2822 if (this->Designators[I].isArrayDesignator()) {
2823 // Compute type- and value-dependence.
2824 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002825 if (Index->isTypeDependent() || Index->isValueDependent())
2826 ExprBits.ValueDependent = true;
2827
2828 // Propagate unexpanded parameter packs.
2829 if (Index->containsUnexpandedParameterPack())
2830 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002831
2832 // Copy the index expressions into permanent storage.
2833 *Child++ = IndexExprs[IndexIdx++];
2834 } else if (this->Designators[I].isArrayRangeDesignator()) {
2835 // Compute type- and value-dependence.
2836 Expr *Start = IndexExprs[IndexIdx];
2837 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002838 if (Start->isTypeDependent() || Start->isValueDependent() ||
2839 End->isTypeDependent() || End->isValueDependent())
2840 ExprBits.ValueDependent = true;
2841
2842 // Propagate unexpanded parameter packs.
2843 if (Start->containsUnexpandedParameterPack() ||
2844 End->containsUnexpandedParameterPack())
2845 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002846
2847 // Copy the start/end expressions into permanent storage.
2848 *Child++ = IndexExprs[IndexIdx++];
2849 *Child++ = IndexExprs[IndexIdx++];
2850 }
2851 }
2852
2853 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002854}
2855
Douglas Gregor05c13a32009-01-22 00:58:24 +00002856DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002857DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002858 unsigned NumDesignators,
2859 Expr **IndexExprs, unsigned NumIndexExprs,
2860 SourceLocation ColonOrEqualLoc,
2861 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002862 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002863 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002864 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002865 ColonOrEqualLoc, UsesColonSyntax,
2866 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002867}
2868
Mike Stump1eb44332009-09-09 15:08:12 +00002869DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002870 unsigned NumIndexExprs) {
2871 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2872 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2873 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2874}
2875
Douglas Gregor319d57f2010-01-06 23:17:19 +00002876void DesignatedInitExpr::setDesignators(ASTContext &C,
2877 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002878 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002879 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002880 NumDesignators = NumDesigs;
2881 for (unsigned I = 0; I != NumDesigs; ++I)
2882 Designators[I] = Desigs[I];
2883}
2884
Abramo Bagnara24f46742011-03-16 15:08:46 +00002885SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2886 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2887 if (size() == 1)
2888 return DIE->getDesignator(0)->getSourceRange();
2889 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2890 DIE->getDesignator(size()-1)->getEndLocation());
2891}
2892
Douglas Gregor05c13a32009-01-22 00:58:24 +00002893SourceRange DesignatedInitExpr::getSourceRange() const {
2894 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002895 Designator &First =
2896 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002897 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002898 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002899 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2900 else
2901 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2902 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002903 StartLoc =
2904 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002905 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2906}
2907
Douglas Gregor05c13a32009-01-22 00:58:24 +00002908Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2909 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2910 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2911 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002912 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2913 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2914}
2915
2916Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002917 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002918 "Requires array range designator");
2919 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2920 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002921 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2922 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2923}
2924
2925Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002926 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002927 "Requires array range designator");
2928 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2929 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002930 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2931 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2932}
2933
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002934/// \brief Replaces the designator at index @p Idx with the series
2935/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002936void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002937 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002938 const Designator *Last) {
2939 unsigned NumNewDesignators = Last - First;
2940 if (NumNewDesignators == 0) {
2941 std::copy_backward(Designators + Idx + 1,
2942 Designators + NumDesignators,
2943 Designators + Idx);
2944 --NumNewDesignators;
2945 return;
2946 } else if (NumNewDesignators == 1) {
2947 Designators[Idx] = *First;
2948 return;
2949 }
2950
Mike Stump1eb44332009-09-09 15:08:12 +00002951 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002952 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002953 std::copy(Designators, Designators + Idx, NewDesignators);
2954 std::copy(First, Last, NewDesignators + Idx);
2955 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2956 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002957 Designators = NewDesignators;
2958 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2959}
2960
Mike Stump1eb44332009-09-09 15:08:12 +00002961ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002962 Expr **exprs, unsigned nexprs,
2963 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002964 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2965 false, false, false),
2966 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002967
Nate Begeman2ef13e52009-08-10 23:49:36 +00002968 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002969 for (unsigned i = 0; i != nexprs; ++i) {
2970 if (exprs[i]->isTypeDependent())
2971 ExprBits.TypeDependent = true;
2972 if (exprs[i]->isValueDependent())
2973 ExprBits.ValueDependent = true;
2974 if (exprs[i]->containsUnexpandedParameterPack())
2975 ExprBits.ContainsUnexpandedParameterPack = true;
2976
Nate Begeman2ef13e52009-08-10 23:49:36 +00002977 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002978 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002979}
2980
John McCalle996ffd2011-02-16 08:02:54 +00002981const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2982 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2983 e = ewc->getSubExpr();
2984 e = cast<CXXConstructExpr>(e)->getArg(0);
2985 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2986 e = ice->getSubExpr();
2987 return cast<OpaqueValueExpr>(e);
2988}
2989
Douglas Gregor05c13a32009-01-22 00:58:24 +00002990//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002991// ExprIterator.
2992//===----------------------------------------------------------------------===//
2993
2994Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2995Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2996Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2997const Expr* ConstExprIterator::operator[](size_t idx) const {
2998 return cast<Expr>(I[idx]);
2999}
3000const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3001const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3002
3003//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003004// Child Iterators for iterating over subexpressions/substatements
3005//===----------------------------------------------------------------------===//
3006
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003007// UnaryExprOrTypeTraitExpr
3008Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003009 // If this is of a type and the type is a VLA type (and not a typedef), the
3010 // size expression of the VLA needs to be treated as an executable expression.
3011 // Why isn't this weirdness documented better in StmtIterator?
3012 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003013 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003014 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003015 return child_range(child_iterator(T), child_iterator());
3016 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003017 }
John McCall63c00d72011-02-09 08:16:59 +00003018 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003019}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003020
Steve Naroff563477d2007-09-18 23:55:05 +00003021// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003022Stmt::child_range ObjCMessageExpr::children() {
3023 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003024 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003025 begin = reinterpret_cast<Stmt **>(this + 1);
3026 else
3027 begin = reinterpret_cast<Stmt **>(getArgs());
3028 return child_range(begin,
3029 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003030}
3031
Steve Naroff4eb206b2008-09-03 18:15:37 +00003032// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003033BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003034 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003035 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00003036 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003037 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003038 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003039{
Douglas Gregord967e312011-01-19 21:52:31 +00003040 bool TypeDependent = false;
3041 bool ValueDependent = false;
3042 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3043 ExprBits.TypeDependent = TypeDependent;
3044 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003045}