blob: 1d0319f60142476a71bebd3ee112252a56146828 [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
Jay Foad65aa6882011-06-21 15:13:30 +0000501StringLiteral *StringLiteral::Create(ASTContext &C, llvm::StringRef Str,
502 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.
Jay Foad65aa6882011-06-21 15:13:30 +0000514 char *AStrData = new (C, 1) char[Str.size()];
515 memcpy(AStrData, Str.data(), Str.size());
Chris Lattner2085fd62009-02-18 06:40:38 +0000516 SL->StrData = AStrData;
Jay Foad65aa6882011-06-21 15:13:30 +0000517 SL->ByteLength = Str.size();
Chris Lattner2085fd62009-02-18 06:40:38 +0000518 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 ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000596 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000597 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";
John McCallf85e1932011-06-15 23:02:42 +00001048 case CK_ObjCConsumeObject:
1049 return "ObjCConsumeObject";
1050 case CK_ObjCProduceObject:
1051 return "ObjCProduceObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
John McCall2bb5d002010-11-13 09:02:35 +00001054 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001055 return 0;
1056}
1057
Douglas Gregor6eef5192009-12-14 19:27:10 +00001058Expr *CastExpr::getSubExprAsWritten() {
1059 Expr *SubExpr = 0;
1060 CastExpr *E = this;
1061 do {
1062 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001063
1064 // Skip through reference binding to temporary.
1065 if (MaterializeTemporaryExpr *Materialize
1066 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1067 SubExpr = Materialize->GetTemporaryExpr();
1068
Douglas Gregor6eef5192009-12-14 19:27:10 +00001069 // Skip any temporary bindings; they're implicit.
1070 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1071 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001072
Douglas Gregor6eef5192009-12-14 19:27:10 +00001073 // Conversions by constructor and conversion functions have a
1074 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001075 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001076 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001077 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001078 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001079
Douglas Gregor6eef5192009-12-14 19:27:10 +00001080 // If the subexpression we're left with is an implicit cast, look
1081 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001082 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1083
Douglas Gregor6eef5192009-12-14 19:27:10 +00001084 return SubExpr;
1085}
1086
John McCallf871d0c2010-08-07 06:22:56 +00001087CXXBaseSpecifier **CastExpr::path_buffer() {
1088 switch (getStmtClass()) {
1089#define ABSTRACT_STMT(x)
1090#define CASTEXPR(Type, Base) \
1091 case Stmt::Type##Class: \
1092 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1093#define STMT(Type, Base)
1094#include "clang/AST/StmtNodes.inc"
1095 default:
1096 llvm_unreachable("non-cast expressions not possible here");
1097 return 0;
1098 }
1099}
1100
1101void CastExpr::setCastPath(const CXXCastPath &Path) {
1102 assert(Path.size() == path_size());
1103 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1104}
1105
1106ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1107 CastKind Kind, Expr *Operand,
1108 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001109 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001110 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1111 void *Buffer =
1112 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1113 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001114 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001115 if (PathSize) E->setCastPath(*BasePath);
1116 return E;
1117}
1118
1119ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1120 unsigned PathSize) {
1121 void *Buffer =
1122 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1123 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1124}
1125
1126
1127CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001128 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001129 const CXXCastPath *BasePath,
1130 TypeSourceInfo *WrittenTy,
1131 SourceLocation L, SourceLocation R) {
1132 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1133 void *Buffer =
1134 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1135 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001136 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001137 if (PathSize) E->setCastPath(*BasePath);
1138 return E;
1139}
1140
1141CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1142 void *Buffer =
1143 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1144 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1145}
1146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1148/// corresponds to, e.g. "<<=".
1149const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1150 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001151 case BO_PtrMemD: return ".*";
1152 case BO_PtrMemI: return "->*";
1153 case BO_Mul: return "*";
1154 case BO_Div: return "/";
1155 case BO_Rem: return "%";
1156 case BO_Add: return "+";
1157 case BO_Sub: return "-";
1158 case BO_Shl: return "<<";
1159 case BO_Shr: return ">>";
1160 case BO_LT: return "<";
1161 case BO_GT: return ">";
1162 case BO_LE: return "<=";
1163 case BO_GE: return ">=";
1164 case BO_EQ: return "==";
1165 case BO_NE: return "!=";
1166 case BO_And: return "&";
1167 case BO_Xor: return "^";
1168 case BO_Or: return "|";
1169 case BO_LAnd: return "&&";
1170 case BO_LOr: return "||";
1171 case BO_Assign: return "=";
1172 case BO_MulAssign: return "*=";
1173 case BO_DivAssign: return "/=";
1174 case BO_RemAssign: return "%=";
1175 case BO_AddAssign: return "+=";
1176 case BO_SubAssign: return "-=";
1177 case BO_ShlAssign: return "<<=";
1178 case BO_ShrAssign: return ">>=";
1179 case BO_AndAssign: return "&=";
1180 case BO_XorAssign: return "^=";
1181 case BO_OrAssign: return "|=";
1182 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001184
1185 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001186}
1187
John McCall2de56d12010-08-25 11:45:40 +00001188BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001189BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1190 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001191 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001192 case OO_Plus: return BO_Add;
1193 case OO_Minus: return BO_Sub;
1194 case OO_Star: return BO_Mul;
1195 case OO_Slash: return BO_Div;
1196 case OO_Percent: return BO_Rem;
1197 case OO_Caret: return BO_Xor;
1198 case OO_Amp: return BO_And;
1199 case OO_Pipe: return BO_Or;
1200 case OO_Equal: return BO_Assign;
1201 case OO_Less: return BO_LT;
1202 case OO_Greater: return BO_GT;
1203 case OO_PlusEqual: return BO_AddAssign;
1204 case OO_MinusEqual: return BO_SubAssign;
1205 case OO_StarEqual: return BO_MulAssign;
1206 case OO_SlashEqual: return BO_DivAssign;
1207 case OO_PercentEqual: return BO_RemAssign;
1208 case OO_CaretEqual: return BO_XorAssign;
1209 case OO_AmpEqual: return BO_AndAssign;
1210 case OO_PipeEqual: return BO_OrAssign;
1211 case OO_LessLess: return BO_Shl;
1212 case OO_GreaterGreater: return BO_Shr;
1213 case OO_LessLessEqual: return BO_ShlAssign;
1214 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1215 case OO_EqualEqual: return BO_EQ;
1216 case OO_ExclaimEqual: return BO_NE;
1217 case OO_LessEqual: return BO_LE;
1218 case OO_GreaterEqual: return BO_GE;
1219 case OO_AmpAmp: return BO_LAnd;
1220 case OO_PipePipe: return BO_LOr;
1221 case OO_Comma: return BO_Comma;
1222 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001223 }
1224}
1225
1226OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1227 static const OverloadedOperatorKind OverOps[] = {
1228 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1229 OO_Star, OO_Slash, OO_Percent,
1230 OO_Plus, OO_Minus,
1231 OO_LessLess, OO_GreaterGreater,
1232 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1233 OO_EqualEqual, OO_ExclaimEqual,
1234 OO_Amp,
1235 OO_Caret,
1236 OO_Pipe,
1237 OO_AmpAmp,
1238 OO_PipePipe,
1239 OO_Equal, OO_StarEqual,
1240 OO_SlashEqual, OO_PercentEqual,
1241 OO_PlusEqual, OO_MinusEqual,
1242 OO_LessLessEqual, OO_GreaterGreaterEqual,
1243 OO_AmpEqual, OO_CaretEqual,
1244 OO_PipeEqual,
1245 OO_Comma
1246 };
1247 return OverOps[Opc];
1248}
1249
Ted Kremenek709210f2010-04-13 23:39:13 +00001250InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001251 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001252 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001253 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1254 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001255 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001256 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001257 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001258{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001259 for (unsigned I = 0; I != numInits; ++I) {
1260 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001261 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001262 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001263 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001264 if (initExprs[I]->containsUnexpandedParameterPack())
1265 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001266 }
Sean Huntc3021132010-05-05 15:23:54 +00001267
Ted Kremenek709210f2010-04-13 23:39:13 +00001268 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001269}
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
Ted Kremenek709210f2010-04-13 23:39:13 +00001271void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001272 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001273 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001274}
1275
Ted Kremenek709210f2010-04-13 23:39:13 +00001276void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001277 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001278}
1279
Ted Kremenek709210f2010-04-13 23:39:13 +00001280Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001281 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001282 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001283 InitExprs.back() = expr;
1284 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Douglas Gregor4c678342009-01-28 21:54:33 +00001287 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1288 InitExprs[Init] = expr;
1289 return Result;
1290}
1291
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001292void InitListExpr::setArrayFiller(Expr *filler) {
1293 ArrayFillerOrUnionFieldInit = filler;
1294 // Fill out any "holes" in the array due to designated initializers.
1295 Expr **inits = getInits();
1296 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1297 if (inits[i] == 0)
1298 inits[i] = filler;
1299}
1300
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001301SourceRange InitListExpr::getSourceRange() const {
1302 if (SyntacticForm)
1303 return SyntacticForm->getSourceRange();
1304 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1305 if (Beg.isInvalid()) {
1306 // Find the first non-null initializer.
1307 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1308 E = InitExprs.end();
1309 I != E; ++I) {
1310 if (Stmt *S = *I) {
1311 Beg = S->getLocStart();
1312 break;
1313 }
1314 }
1315 }
1316 if (End.isInvalid()) {
1317 // Find the first non-null initializer from the end.
1318 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1319 E = InitExprs.rend();
1320 I != E; ++I) {
1321 if (Stmt *S = *I) {
1322 End = S->getSourceRange().getEnd();
1323 break;
1324 }
1325 }
1326 }
1327 return SourceRange(Beg, End);
1328}
1329
Steve Naroffbfdcae62008-09-04 15:31:07 +00001330/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001331///
1332const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001333 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001334 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001335}
1336
Mike Stump1eb44332009-09-09 15:08:12 +00001337SourceLocation BlockExpr::getCaretLocation() const {
1338 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001339}
Mike Stump1eb44332009-09-09 15:08:12 +00001340const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001341 return TheBlock->getBody();
1342}
Mike Stump1eb44332009-09-09 15:08:12 +00001343Stmt *BlockExpr::getBody() {
1344 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001345}
Steve Naroff56ee6892008-10-08 17:01:13 +00001346
1347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348//===----------------------------------------------------------------------===//
1349// Generic Expression Routines
1350//===----------------------------------------------------------------------===//
1351
Chris Lattner026dc962009-02-14 07:37:35 +00001352/// isUnusedResultAWarning - Return true if this immediate expression should
1353/// be warned about if the result is unused. If so, fill in Loc and Ranges
1354/// with location to warn on and the source range[s] to report with the
1355/// warning.
1356bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001357 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001358 // Don't warn if the expr is type dependent. The type could end up
1359 // instantiating to void.
1360 if (isTypeDependent())
1361 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 switch (getStmtClass()) {
1364 default:
John McCall0faede62010-03-12 07:11:26 +00001365 if (getType()->isVoidType())
1366 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001367 Loc = getExprLoc();
1368 R1 = getSourceRange();
1369 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001371 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001372 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001373 case GenericSelectionExprClass:
1374 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1375 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 case UnaryOperatorClass: {
1377 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001380 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001381 case UO_PostInc:
1382 case UO_PostDec:
1383 case UO_PreInc:
1384 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001385 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001386 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001388 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001389 return false;
1390 break;
John McCall2de56d12010-08-25 11:45:40 +00001391 case UO_Real:
1392 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001394 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1395 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001396 return false;
1397 break;
John McCall2de56d12010-08-25 11:45:40 +00001398 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001399 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 }
Chris Lattner026dc962009-02-14 07:37:35 +00001401 Loc = UO->getOperatorLoc();
1402 R1 = UO->getSubExpr()->getSourceRange();
1403 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001405 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001406 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001407 switch (BO->getOpcode()) {
1408 default:
1409 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001410 // Consider the RHS of comma for side effects. LHS was checked by
1411 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001412 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001413 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1414 // lvalue-ness) of an assignment written in a macro.
1415 if (IntegerLiteral *IE =
1416 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1417 if (IE->getValue() == 0)
1418 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001419 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1420 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001421 case BO_LAnd:
1422 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001423 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1424 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1425 return false;
1426 break;
John McCallbf0ee352010-02-16 04:10:53 +00001427 }
Chris Lattner026dc962009-02-14 07:37:35 +00001428 if (BO->isAssignmentOp())
1429 return false;
1430 Loc = BO->getOperatorLoc();
1431 R1 = BO->getLHS()->getSourceRange();
1432 R2 = BO->getRHS()->getSourceRange();
1433 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001434 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001435 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001436 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001437 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001438
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001439 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001440 // If only one of the LHS or RHS is a warning, the operator might
1441 // be being used for control flow. Only warn if both the LHS and
1442 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001443 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001444 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1445 return false;
1446 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001447 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001448 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001449 }
1450
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001452 // If the base pointer or element is to a volatile pointer/field, accessing
1453 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001454 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001455 return false;
1456 Loc = cast<MemberExpr>(this)->getMemberLoc();
1457 R1 = SourceRange(Loc, Loc);
1458 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 case ArraySubscriptExprClass:
1462 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001463 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001464 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001465 return false;
1466 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1467 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1468 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1469 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001470
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001472 case CXXOperatorCallExprClass:
1473 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001474 // If this is a direct call, get the callee.
1475 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001476 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001477 // If the callee has attribute pure, const, or warn_unused_result, warn
1478 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001479 //
1480 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1481 // updated to match for QoI.
1482 if (FD->getAttr<WarnUnusedResultAttr>() ||
1483 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1484 Loc = CE->getCallee()->getLocStart();
1485 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001487 if (unsigned NumArgs = CE->getNumArgs())
1488 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1489 CE->getArg(NumArgs-1)->getLocEnd());
1490 return true;
1491 }
Chris Lattner026dc962009-02-14 07:37:35 +00001492 }
1493 return false;
1494 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001495
1496 case CXXTemporaryObjectExprClass:
1497 case CXXConstructExprClass:
1498 return false;
1499
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001500 case ObjCMessageExprClass: {
1501 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001502 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1503 ME->isInstanceMessage() &&
1504 !ME->getType()->isVoidType() &&
1505 ME->getSelector().getIdentifierInfoForSlot(0) &&
1506 ME->getSelector().getIdentifierInfoForSlot(0)
1507 ->getName().startswith("init")) {
1508 Loc = getExprLoc();
1509 R1 = ME->getSourceRange();
1510 return true;
1511 }
1512
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001513 const ObjCMethodDecl *MD = ME->getMethodDecl();
1514 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1515 Loc = getExprLoc();
1516 return true;
1517 }
Chris Lattner026dc962009-02-14 07:37:35 +00001518 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
John McCall12f78a62010-12-02 01:19:52 +00001521 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001522 Loc = getExprLoc();
1523 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001524 return true;
John McCall12f78a62010-12-02 01:19:52 +00001525
Chris Lattner611b2ec2008-07-26 19:51:01 +00001526 case StmtExprClass: {
1527 // Statement exprs don't logically have side effects themselves, but are
1528 // sometimes used in macros in ways that give them a type that is unused.
1529 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1530 // however, if the result of the stmt expr is dead, we don't want to emit a
1531 // warning.
1532 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001533 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001534 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001535 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001536 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1537 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1538 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1539 }
Mike Stump1eb44332009-09-09 15:08:12 +00001540
John McCall0faede62010-03-12 07:11:26 +00001541 if (getType()->isVoidType())
1542 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001543 Loc = cast<StmtExpr>(this)->getLParenLoc();
1544 R1 = getSourceRange();
1545 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001546 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001547 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001548 // If this is an explicit cast to void, allow it. People do this when they
1549 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001550 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001551 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001552 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1553 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1554 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001555 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001556 if (getType()->isVoidType())
1557 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001558 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001559
Anders Carlsson58beed92009-11-17 17:11:23 +00001560 // If this is a cast to void or a constructor conversion, check the operand.
1561 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001562 if (CE->getCastKind() == CK_ToVoid ||
1563 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001564 return (cast<CastExpr>(this)->getSubExpr()
1565 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001566 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1567 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1568 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Eli Friedman4be1f472008-05-19 21:24:43 +00001571 case ImplicitCastExprClass:
1572 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001573 return (cast<ImplicitCastExpr>(this)
1574 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001575
Chris Lattner04421082008-04-08 04:40:51 +00001576 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001577 return (cast<CXXDefaultArgExpr>(this)
1578 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001579
1580 case CXXNewExprClass:
1581 // FIXME: In theory, there might be new expressions that don't have side
1582 // effects (e.g. a placement new with an uninitialized POD).
1583 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001584 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001585 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001586 return (cast<CXXBindTemporaryExpr>(this)
1587 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001588 case ExprWithCleanupsClass:
1589 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001590 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001591 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001592}
1593
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001594/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001595/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001596bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001597 const Expr *E = IgnoreParens();
1598 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001599 default:
1600 return false;
1601 case ObjCIvarRefExprClass:
1602 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001603 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001604 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001605 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001606 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001607 case MaterializeTemporaryExprClass:
1608 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1609 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001610 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001611 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001612 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001613 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001614 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1615 if (VD->hasGlobalStorage())
1616 return true;
1617 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001618 // dereferencing to a pointer is always a gc'able candidate,
1619 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001620 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001621 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001622 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001623 return false;
1624 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001625 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001626 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001627 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001628 }
1629 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001630 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001631 }
1632}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001633
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001634bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1635 if (isTypeDependent())
1636 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001637 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001638}
1639
John McCall864c0412011-04-26 20:42:42 +00001640QualType Expr::findBoundMemberType(const Expr *expr) {
1641 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1642
1643 // Bound member expressions are always one of these possibilities:
1644 // x->m x.m x->*y x.*y
1645 // (possibly parenthesized)
1646
1647 expr = expr->IgnoreParens();
1648 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1649 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1650 return mem->getMemberDecl()->getType();
1651 }
1652
1653 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1654 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1655 ->getPointeeType();
1656 assert(type->isFunctionType());
1657 return type;
1658 }
1659
1660 assert(isa<UnresolvedMemberExpr>(expr));
1661 return QualType();
1662}
1663
Sebastian Redl369e51f2010-09-10 20:55:33 +00001664static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1665 Expr::CanThrowResult CT2) {
1666 // CanThrowResult constants are ordered so that the maximum is the correct
1667 // merge result.
1668 return CT1 > CT2 ? CT1 : CT2;
1669}
1670
1671static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1672 Expr *E = const_cast<Expr*>(CE);
1673 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001674 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001675 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1676 }
1677 return R;
1678}
1679
Richard Smith7a614d82011-06-11 17:19:42 +00001680static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1681 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001682 bool NullThrows = true) {
1683 if (!D)
1684 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1685
1686 // See if we can get a function type from the decl somehow.
1687 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1688 if (!VD) // If we have no clue what we're calling, assume the worst.
1689 return Expr::CT_Can;
1690
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001691 // As an extension, we assume that __attribute__((nothrow)) functions don't
1692 // throw.
1693 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1694 return Expr::CT_Cannot;
1695
Sebastian Redl369e51f2010-09-10 20:55:33 +00001696 QualType T = VD->getType();
1697 const FunctionProtoType *FT;
1698 if ((FT = T->getAs<FunctionProtoType>())) {
1699 } else if (const PointerType *PT = T->getAs<PointerType>())
1700 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1701 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1702 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1703 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1704 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1705 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1706 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1707
1708 if (!FT)
1709 return Expr::CT_Can;
1710
Richard Smith7a614d82011-06-11 17:19:42 +00001711 if (FT->getExceptionSpecType() == EST_Delayed) {
1712 assert(isa<CXXConstructorDecl>(D) &&
1713 "only constructor exception specs can be unknown");
1714 Ctx.getDiagnostics().Report(E->getLocStart(),
1715 diag::err_exception_spec_unknown)
1716 << E->getSourceRange();
1717 return Expr::CT_Can;
1718 }
1719
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001720 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001721}
1722
1723static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1724 if (DC->isTypeDependent())
1725 return Expr::CT_Dependent;
1726
Sebastian Redl295995c2010-09-10 20:55:47 +00001727 if (!DC->getTypeAsWritten()->isReferenceType())
1728 return Expr::CT_Cannot;
1729
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001730 if (DC->getSubExpr()->isTypeDependent())
1731 return Expr::CT_Dependent;
1732
Sebastian Redl369e51f2010-09-10 20:55:33 +00001733 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1734}
1735
1736static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1737 const CXXTypeidExpr *DC) {
1738 if (DC->isTypeOperand())
1739 return Expr::CT_Cannot;
1740
1741 Expr *Op = DC->getExprOperand();
1742 if (Op->isTypeDependent())
1743 return Expr::CT_Dependent;
1744
1745 const RecordType *RT = Op->getType()->getAs<RecordType>();
1746 if (!RT)
1747 return Expr::CT_Cannot;
1748
1749 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1750 return Expr::CT_Cannot;
1751
1752 if (Op->Classify(C).isPRValue())
1753 return Expr::CT_Cannot;
1754
1755 return Expr::CT_Can;
1756}
1757
1758Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1759 // C++ [expr.unary.noexcept]p3:
1760 // [Can throw] if in a potentially-evaluated context the expression would
1761 // contain:
1762 switch (getStmtClass()) {
1763 case CXXThrowExprClass:
1764 // - a potentially evaluated throw-expression
1765 return CT_Can;
1766
1767 case CXXDynamicCastExprClass: {
1768 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1769 // where T is a reference type, that requires a run-time check
1770 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1771 if (CT == CT_Can)
1772 return CT;
1773 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1774 }
1775
1776 case CXXTypeidExprClass:
1777 // - a potentially evaluated typeid expression applied to a glvalue
1778 // expression whose type is a polymorphic class type
1779 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1780
1781 // - a potentially evaluated call to a function, member function, function
1782 // pointer, or member function pointer that does not have a non-throwing
1783 // exception-specification
1784 case CallExprClass:
1785 case CXXOperatorCallExprClass:
1786 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001787 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001788 CanThrowResult CT;
1789 if (isTypeDependent())
1790 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001791 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1792 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001793 else
Richard Smith7a614d82011-06-11 17:19:42 +00001794 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001795 if (CT == CT_Can)
1796 return CT;
1797 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1798 }
1799
Sebastian Redl295995c2010-09-10 20:55:47 +00001800 case CXXConstructExprClass:
1801 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001802 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001803 cast<CXXConstructExpr>(this)->getConstructor());
1804 if (CT == CT_Can)
1805 return CT;
1806 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1807 }
1808
1809 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001810 CanThrowResult CT;
1811 if (isTypeDependent())
1812 CT = CT_Dependent;
1813 else
1814 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001815 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1816 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001817 /*NullThrows*/false));
1818 if (CT == CT_Can)
1819 return CT;
1820 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1821 }
1822
1823 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001824 CanThrowResult CT;
1825 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1826 if (DTy.isNull() || DTy->isDependentType()) {
1827 CT = CT_Dependent;
1828 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00001829 CT = CanCalleeThrow(C, this,
1830 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001831 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1832 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00001833 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001834 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001835 if (CT == CT_Can)
1836 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001837 }
1838 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1839 }
1840
1841 case CXXBindTemporaryExprClass: {
1842 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00001843 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001844 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1845 if (CT == CT_Can)
1846 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001847 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1848 }
1849
1850 // ObjC message sends are like function calls, but never have exception
1851 // specs.
1852 case ObjCMessageExprClass:
1853 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001854 return CT_Can;
1855
1856 // Many other things have subexpressions, so we have to test those.
1857 // Some are simple:
1858 case ParenExprClass:
1859 case MemberExprClass:
1860 case CXXReinterpretCastExprClass:
1861 case CXXConstCastExprClass:
1862 case ConditionalOperatorClass:
1863 case CompoundLiteralExprClass:
1864 case ExtVectorElementExprClass:
1865 case InitListExprClass:
1866 case DesignatedInitExprClass:
1867 case ParenListExprClass:
1868 case VAArgExprClass:
1869 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001870 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001871 case ObjCIvarRefExprClass:
1872 case ObjCIsaExprClass:
1873 case ShuffleVectorExprClass:
1874 return CanSubExprsThrow(C, this);
1875
1876 // Some might be dependent for other reasons.
1877 case UnaryOperatorClass:
1878 case ArraySubscriptExprClass:
1879 case ImplicitCastExprClass:
1880 case CStyleCastExprClass:
1881 case CXXStaticCastExprClass:
1882 case CXXFunctionalCastExprClass:
1883 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00001884 case CompoundAssignOperatorClass:
1885 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001886 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1887 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1888 }
1889
1890 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1891 case StmtExprClass:
1892 return CT_Can;
1893
1894 case ChooseExprClass:
1895 if (isTypeDependent() || isValueDependent())
1896 return CT_Dependent;
1897 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1898
Peter Collingbournef111d932011-04-15 00:35:48 +00001899 case GenericSelectionExprClass:
1900 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1901 return CT_Dependent;
1902 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1903
Sebastian Redl369e51f2010-09-10 20:55:33 +00001904 // Some expressions are always dependent.
1905 case DependentScopeDeclRefExprClass:
1906 case CXXUnresolvedConstructExprClass:
1907 case CXXDependentScopeMemberExprClass:
1908 return CT_Dependent;
1909
1910 default:
1911 // All other expressions don't have subexpressions, or else they are
1912 // unevaluated.
1913 return CT_Cannot;
1914 }
1915}
1916
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001917Expr* Expr::IgnoreParens() {
1918 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001919 while (true) {
1920 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1921 E = P->getSubExpr();
1922 continue;
1923 }
1924 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1925 if (P->getOpcode() == UO_Extension) {
1926 E = P->getSubExpr();
1927 continue;
1928 }
1929 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001930 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1931 if (!P->isResultDependent()) {
1932 E = P->getResultExpr();
1933 continue;
1934 }
1935 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001936 return E;
1937 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001938}
1939
Chris Lattner56f34942008-02-13 01:02:39 +00001940/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1941/// or CastExprs or ImplicitCastExprs, returning their operand.
1942Expr *Expr::IgnoreParenCasts() {
1943 Expr *E = this;
1944 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001945 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001946 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001947 continue;
1948 }
1949 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001950 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001951 continue;
1952 }
1953 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1954 if (P->getOpcode() == UO_Extension) {
1955 E = P->getSubExpr();
1956 continue;
1957 }
1958 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001959 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1960 if (!P->isResultDependent()) {
1961 E = P->getResultExpr();
1962 continue;
1963 }
1964 }
Douglas Gregor03e80032011-06-21 17:03:29 +00001965 if (MaterializeTemporaryExpr *Materialize
1966 = dyn_cast<MaterializeTemporaryExpr>(E)) {
1967 E = Materialize->GetTemporaryExpr();
1968 continue;
1969 }
1970
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001971 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001972 }
1973}
1974
John McCall9c5d70c2010-12-04 08:24:19 +00001975/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1976/// casts. This is intended purely as a temporary workaround for code
1977/// that hasn't yet been rewritten to do the right thing about those
1978/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001979Expr *Expr::IgnoreParenLValueCasts() {
1980 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001981 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001982 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1983 E = P->getSubExpr();
1984 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001985 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001986 if (P->getCastKind() == CK_LValueToRValue) {
1987 E = P->getSubExpr();
1988 continue;
1989 }
John McCall9c5d70c2010-12-04 08:24:19 +00001990 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1991 if (P->getOpcode() == UO_Extension) {
1992 E = P->getSubExpr();
1993 continue;
1994 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001995 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1996 if (!P->isResultDependent()) {
1997 E = P->getResultExpr();
1998 continue;
1999 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002000 } else if (MaterializeTemporaryExpr *Materialize
2001 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2002 E = Materialize->GetTemporaryExpr();
2003 continue;
John McCallf6a16482010-12-04 03:47:34 +00002004 }
2005 break;
2006 }
2007 return E;
2008}
2009
John McCall2fc46bf2010-05-05 22:59:52 +00002010Expr *Expr::IgnoreParenImpCasts() {
2011 Expr *E = this;
2012 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002013 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002014 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002015 continue;
2016 }
2017 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002018 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002019 continue;
2020 }
2021 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2022 if (P->getOpcode() == UO_Extension) {
2023 E = P->getSubExpr();
2024 continue;
2025 }
2026 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002027 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2028 if (!P->isResultDependent()) {
2029 E = P->getResultExpr();
2030 continue;
2031 }
2032 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002033 if (MaterializeTemporaryExpr *Materialize
2034 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2035 E = Materialize->GetTemporaryExpr();
2036 continue;
2037 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002038 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002039 }
2040}
2041
Hans Wennborg2f072b42011-06-09 17:06:51 +00002042Expr *Expr::IgnoreConversionOperator() {
2043 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002044 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002045 return MCE->getImplicitObjectArgument();
2046 }
2047 return this;
2048}
2049
Chris Lattnerecdd8412009-03-13 17:28:01 +00002050/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2051/// value (including ptr->int casts of the same size). Strip off any
2052/// ParenExpr or CastExprs, returning their operand.
2053Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2054 Expr *E = this;
2055 while (true) {
2056 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2057 E = P->getSubExpr();
2058 continue;
2059 }
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Chris Lattnerecdd8412009-03-13 17:28:01 +00002061 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2062 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002063 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002064 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Chris Lattnerecdd8412009-03-13 17:28:01 +00002066 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2067 E = SE;
2068 continue;
2069 }
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002071 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002072 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002073 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002074 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002075 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2076 E = SE;
2077 continue;
2078 }
2079 }
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002081 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2082 if (P->getOpcode() == UO_Extension) {
2083 E = P->getSubExpr();
2084 continue;
2085 }
2086 }
2087
Peter Collingbournef111d932011-04-15 00:35:48 +00002088 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2089 if (!P->isResultDependent()) {
2090 E = P->getResultExpr();
2091 continue;
2092 }
2093 }
2094
Chris Lattnerecdd8412009-03-13 17:28:01 +00002095 return E;
2096 }
2097}
2098
Douglas Gregor6eef5192009-12-14 19:27:10 +00002099bool Expr::isDefaultArgument() const {
2100 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002101 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2102 E = M->GetTemporaryExpr();
2103
Douglas Gregor6eef5192009-12-14 19:27:10 +00002104 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2105 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002106
Douglas Gregor6eef5192009-12-14 19:27:10 +00002107 return isa<CXXDefaultArgExpr>(E);
2108}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002109
Douglas Gregor2f599792010-04-02 18:24:57 +00002110/// \brief Skip over any no-op casts and any temporary-binding
2111/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002112static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002113 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2114 E = M->GetTemporaryExpr();
2115
Douglas Gregor2f599792010-04-02 18:24:57 +00002116 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002117 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002118 E = ICE->getSubExpr();
2119 else
2120 break;
2121 }
2122
2123 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2124 E = BE->getSubExpr();
2125
2126 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002127 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002128 E = ICE->getSubExpr();
2129 else
2130 break;
2131 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002132
2133 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002134}
2135
John McCall558d2ab2010-09-15 10:14:12 +00002136/// isTemporaryObject - Determines if this expression produces a
2137/// temporary of the given class type.
2138bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2139 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2140 return false;
2141
Anders Carlssonf8b30152010-11-28 16:40:49 +00002142 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002143
John McCall58277b52010-09-15 20:59:13 +00002144 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002145 if (!E->Classify(C).isPRValue()) {
2146 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002147 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002148 return false;
2149 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002150
John McCall19e60ad2010-09-16 06:57:56 +00002151 // Black-list a few cases which yield pr-values of class type that don't
2152 // refer to temporaries of that type:
2153
2154 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002155 if (isa<ImplicitCastExpr>(E)) {
2156 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2157 case CK_DerivedToBase:
2158 case CK_UncheckedDerivedToBase:
2159 return false;
2160 default:
2161 break;
2162 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002163 }
2164
John McCall19e60ad2010-09-16 06:57:56 +00002165 // - member expressions (all)
2166 if (isa<MemberExpr>(E))
2167 return false;
2168
John McCall56ca35d2011-02-17 10:25:35 +00002169 // - opaque values (all)
2170 if (isa<OpaqueValueExpr>(E))
2171 return false;
2172
John McCall558d2ab2010-09-15 10:14:12 +00002173 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002174}
2175
Douglas Gregor75e85042011-03-02 21:06:53 +00002176bool Expr::isImplicitCXXThis() const {
2177 const Expr *E = this;
2178
2179 // Strip away parentheses and casts we don't care about.
2180 while (true) {
2181 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2182 E = Paren->getSubExpr();
2183 continue;
2184 }
2185
2186 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2187 if (ICE->getCastKind() == CK_NoOp ||
2188 ICE->getCastKind() == CK_LValueToRValue ||
2189 ICE->getCastKind() == CK_DerivedToBase ||
2190 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2191 E = ICE->getSubExpr();
2192 continue;
2193 }
2194 }
2195
2196 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2197 if (UnOp->getOpcode() == UO_Extension) {
2198 E = UnOp->getSubExpr();
2199 continue;
2200 }
2201 }
2202
Douglas Gregor03e80032011-06-21 17:03:29 +00002203 if (const MaterializeTemporaryExpr *M
2204 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2205 E = M->GetTemporaryExpr();
2206 continue;
2207 }
2208
Douglas Gregor75e85042011-03-02 21:06:53 +00002209 break;
2210 }
2211
2212 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2213 return This->isImplicit();
2214
2215 return false;
2216}
2217
Douglas Gregor898574e2008-12-05 23:32:09 +00002218/// hasAnyTypeDependentArguments - Determines if any of the expressions
2219/// in Exprs is type-dependent.
2220bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2221 for (unsigned I = 0; I < NumExprs; ++I)
2222 if (Exprs[I]->isTypeDependent())
2223 return true;
2224
2225 return false;
2226}
2227
2228/// hasAnyValueDependentArguments - Determines if any of the expressions
2229/// in Exprs is value-dependent.
2230bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2231 for (unsigned I = 0; I < NumExprs; ++I)
2232 if (Exprs[I]->isValueDependent())
2233 return true;
2234
2235 return false;
2236}
2237
John McCall4204f072010-08-02 21:13:48 +00002238bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002239 // This function is attempting whether an expression is an initializer
2240 // which can be evaluated at compile-time. isEvaluatable handles most
2241 // of the cases, but it can't deal with some initializer-specific
2242 // expressions, and it can't deal with aggregates; we deal with those here,
2243 // and fall back to isEvaluatable for the other cases.
2244
John McCall4204f072010-08-02 21:13:48 +00002245 // If we ever capture reference-binding directly in the AST, we can
2246 // kill the second parameter.
2247
2248 if (IsForRef) {
2249 EvalResult Result;
2250 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2251 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002252
Anders Carlssone8a32b82008-11-24 05:23:59 +00002253 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002254 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002255 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002256 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002257 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002258 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002259 case CXXTemporaryObjectExprClass:
2260 case CXXConstructExprClass: {
2261 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002262
2263 // Only if it's
2264 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002265 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002266 if (!CE->getNumArgs()) return true;
2267
2268 // 2) an elidable trivial copy construction of an operand which is
2269 // itself a constant initializer. Note that we consider the
2270 // operand on its own, *not* as a reference binding.
2271 return CE->isElidable() &&
2272 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002273 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002274 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002275 // This handles gcc's extension that allows global initializers like
2276 // "struct x {int x;} x = (struct x) {};".
2277 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002278 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002279 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002280 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002281 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002282 // FIXME: This doesn't deal with fields with reference types correctly.
2283 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2284 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002285 const InitListExpr *Exp = cast<InitListExpr>(this);
2286 unsigned numInits = Exp->getNumInits();
2287 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002288 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002289 return false;
2290 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002291 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002292 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002293 case ImplicitValueInitExprClass:
2294 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002295 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002296 return cast<ParenExpr>(this)->getSubExpr()
2297 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002298 case GenericSelectionExprClass:
2299 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2300 return false;
2301 return cast<GenericSelectionExpr>(this)->getResultExpr()
2302 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002303 case ChooseExprClass:
2304 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2305 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002306 case UnaryOperatorClass: {
2307 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002308 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002309 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002310 break;
2311 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002312 case BinaryOperatorClass: {
2313 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2314 // but this handles the common case.
2315 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002316 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002317 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2318 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2319 return true;
2320 break;
2321 }
John McCall4204f072010-08-02 21:13:48 +00002322 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002323 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002324 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002325 case CStyleCastExprClass:
2326 // Handle casts with a destination that's a struct or union; this
2327 // deals with both the gcc no-op struct cast extension and the
2328 // cast-to-union extension.
2329 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002330 return cast<CastExpr>(this)->getSubExpr()
2331 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002332
Chris Lattner430656e2009-10-13 22:12:09 +00002333 // Integer->integer casts can be handled here, which is important for
2334 // things like (int)(&&x-&&y). Scary but true.
2335 if (getType()->isIntegerType() &&
2336 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002337 return cast<CastExpr>(this)->getSubExpr()
2338 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002339
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002340 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002341
2342 case MaterializeTemporaryExprClass:
2343 return llvm::cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
2344 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002345 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002346 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002347}
2348
Chandler Carruth82214a82011-02-18 23:54:50 +00002349/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2350/// pointer constant or not, as well as the specific kind of constant detected.
2351/// Null pointer constants can be integer constant expressions with the
2352/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2353/// (a GNU extension).
2354Expr::NullPointerConstantKind
2355Expr::isNullPointerConstant(ASTContext &Ctx,
2356 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002357 if (isValueDependent()) {
2358 switch (NPC) {
2359 case NPC_NeverValueDependent:
2360 assert(false && "Unexpected value dependent expression!");
2361 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002362
Douglas Gregorce940492009-09-25 04:25:58 +00002363 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002364 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2365 return NPCK_ZeroInteger;
2366 else
2367 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002368
Douglas Gregorce940492009-09-25 04:25:58 +00002369 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002370 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002371 }
2372 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002373
Sebastian Redl07779722008-10-31 14:43:28 +00002374 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002375 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002376 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002377 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002378 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002379 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002380 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002381 Pointee->isVoidType() && // to void*
2382 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002383 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002384 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002385 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002386 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2387 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002388 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002389 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2390 // Accept ((void*)0) as a null pointer constant, as many other
2391 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002392 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002393 } else if (const GenericSelectionExpr *GE =
2394 dyn_cast<GenericSelectionExpr>(this)) {
2395 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002396 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002397 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002398 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002399 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002400 } else if (isa<GNUNullExpr>(this)) {
2401 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002402 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002403 } else if (const MaterializeTemporaryExpr *M
2404 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2405 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002406 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002407
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002408 // C++0x nullptr_t is always a null pointer constant.
2409 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002410 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002411
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002412 if (const RecordType *UT = getType()->getAsUnionType())
2413 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2414 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2415 const Expr *InitExpr = CLE->getInitializer();
2416 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2417 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2418 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002419 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002420 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002421 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002422 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 // If we have an integer constant expression, we need to *evaluate* it and
2425 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002426 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002427 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2428
2429 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002430}
Steve Naroff31a45842007-07-28 23:10:27 +00002431
John McCallf6a16482010-12-04 03:47:34 +00002432/// \brief If this expression is an l-value for an Objective C
2433/// property, find the underlying property reference expression.
2434const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2435 const Expr *E = this;
2436 while (true) {
2437 assert((E->getValueKind() == VK_LValue &&
2438 E->getObjectKind() == OK_ObjCProperty) &&
2439 "expression is not a property reference");
2440 E = E->IgnoreParenCasts();
2441 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2442 if (BO->getOpcode() == BO_Comma) {
2443 E = BO->getRHS();
2444 continue;
2445 }
2446 }
2447
2448 break;
2449 }
2450
2451 return cast<ObjCPropertyRefExpr>(E);
2452}
2453
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002454FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002455 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002456
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002457 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002458 if (ICE->getCastKind() == CK_LValueToRValue ||
2459 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002460 E = ICE->getSubExpr()->IgnoreParens();
2461 else
2462 break;
2463 }
2464
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002465 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002466 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002467 if (Field->isBitField())
2468 return Field;
2469
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002470 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2471 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2472 if (Field->isBitField())
2473 return Field;
2474
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002475 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2476 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2477 return BinOp->getLHS()->getBitField();
2478
2479 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002480}
2481
Anders Carlsson09380262010-01-31 17:18:49 +00002482bool Expr::refersToVectorElement() const {
2483 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002484
Anders Carlsson09380262010-01-31 17:18:49 +00002485 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002486 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002487 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002488 E = ICE->getSubExpr()->IgnoreParens();
2489 else
2490 break;
2491 }
Sean Huntc3021132010-05-05 15:23:54 +00002492
Anders Carlsson09380262010-01-31 17:18:49 +00002493 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2494 return ASE->getBase()->getType()->isVectorType();
2495
2496 if (isa<ExtVectorElementExpr>(E))
2497 return true;
2498
2499 return false;
2500}
2501
Chris Lattner2140e902009-02-16 22:14:05 +00002502/// isArrow - Return true if the base expression is a pointer to vector,
2503/// return false if the base expression is a vector.
2504bool ExtVectorElementExpr::isArrow() const {
2505 return getBase()->getType()->isPointerType();
2506}
2507
Nate Begeman213541a2008-04-18 23:10:10 +00002508unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002509 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002510 return VT->getNumElements();
2511 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002512}
2513
Nate Begeman8a997642008-05-09 06:41:27 +00002514/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002515bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002516 // FIXME: Refactor this code to an accessor on the AST node which returns the
2517 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002518 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002519
2520 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002521 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002522 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002523
Nate Begeman190d6a22009-01-18 02:01:21 +00002524 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002525 if (Comp[0] == 's' || Comp[0] == 'S')
2526 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002527
Daniel Dunbar15027422009-10-17 23:53:04 +00002528 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2529 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002530 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002531
Steve Narofffec0b492007-07-30 03:29:09 +00002532 return false;
2533}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002534
Nate Begeman8a997642008-05-09 06:41:27 +00002535/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002536void ExtVectorElementExpr::getEncodedElementAccess(
2537 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002538 llvm::StringRef Comp = Accessor->getName();
2539 if (Comp[0] == 's' || Comp[0] == 'S')
2540 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002542 bool isHi = Comp == "hi";
2543 bool isLo = Comp == "lo";
2544 bool isEven = Comp == "even";
2545 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Nate Begeman8a997642008-05-09 06:41:27 +00002547 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2548 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Nate Begeman8a997642008-05-09 06:41:27 +00002550 if (isHi)
2551 Index = e + i;
2552 else if (isLo)
2553 Index = i;
2554 else if (isEven)
2555 Index = 2 * i;
2556 else if (isOdd)
2557 Index = 2 * i + 1;
2558 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002559 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002560
Nate Begeman3b8d1162008-05-13 21:03:02 +00002561 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002562 }
Nate Begeman8a997642008-05-09 06:41:27 +00002563}
2564
Douglas Gregor04badcf2010-04-21 00:45:42 +00002565ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002566 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002567 SourceLocation LBracLoc,
2568 SourceLocation SuperLoc,
2569 bool IsInstanceSuper,
2570 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002571 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002572 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002573 ObjCMethodDecl *Method,
2574 Expr **Args, unsigned NumArgs,
2575 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002576 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002577 /*TypeDependent=*/false, /*ValueDependent=*/false,
2578 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002579 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCallf85e1932011-06-15 23:02:42 +00002580 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002581 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2582 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002583 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002584{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002585 setReceiverPointer(SuperType.getAsOpaquePtr());
2586 if (NumArgs)
2587 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002588}
2589
Douglas Gregor04badcf2010-04-21 00:45:42 +00002590ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002591 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002592 SourceLocation LBracLoc,
2593 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002594 Selector Sel,
2595 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002596 ObjCMethodDecl *Method,
2597 Expr **Args, unsigned NumArgs,
2598 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002599 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002600 T->isDependentType(), T->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002601 NumArgs(NumArgs), Kind(Class),
2602 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002603 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2604 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002605 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002606{
2607 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002608 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002609 for (unsigned I = 0; I != NumArgs; ++I) {
2610 if (Args[I]->isTypeDependent())
2611 ExprBits.TypeDependent = true;
2612 if (Args[I]->isValueDependent())
2613 ExprBits.ValueDependent = true;
2614 if (Args[I]->containsUnexpandedParameterPack())
2615 ExprBits.ContainsUnexpandedParameterPack = true;
2616
2617 MyArgs[I] = Args[I];
2618 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002619}
2620
Douglas Gregor04badcf2010-04-21 00:45:42 +00002621ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002622 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002623 SourceLocation LBracLoc,
2624 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002625 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002626 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002627 ObjCMethodDecl *Method,
2628 Expr **Args, unsigned NumArgs,
2629 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002630 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002631 Receiver->isTypeDependent(),
2632 Receiver->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002633 NumArgs(NumArgs), Kind(Instance),
2634 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002635 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2636 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002637 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002638{
2639 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002640 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002641 for (unsigned I = 0; I != NumArgs; ++I) {
2642 if (Args[I]->isTypeDependent())
2643 ExprBits.TypeDependent = true;
2644 if (Args[I]->isValueDependent())
2645 ExprBits.ValueDependent = true;
2646 if (Args[I]->containsUnexpandedParameterPack())
2647 ExprBits.ContainsUnexpandedParameterPack = true;
2648
2649 MyArgs[I] = Args[I];
2650 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002651}
2652
Douglas Gregor04badcf2010-04-21 00:45:42 +00002653ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002654 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002655 SourceLocation LBracLoc,
2656 SourceLocation SuperLoc,
2657 bool IsInstanceSuper,
2658 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002659 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002660 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002661 ObjCMethodDecl *Method,
2662 Expr **Args, unsigned NumArgs,
2663 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002664 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002665 NumArgs * sizeof(Expr *);
2666 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002667 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002668 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002669 RBracLoc);
2670}
2671
2672ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002673 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002674 SourceLocation LBracLoc,
2675 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002676 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002677 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002678 ObjCMethodDecl *Method,
2679 Expr **Args, unsigned NumArgs,
2680 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002681 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002682 NumArgs * sizeof(Expr *);
2683 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002684 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2685 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002686}
2687
2688ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002689 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002690 SourceLocation LBracLoc,
2691 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002692 Selector Sel,
2693 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002694 ObjCMethodDecl *Method,
2695 Expr **Args, unsigned NumArgs,
2696 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002697 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002698 NumArgs * sizeof(Expr *);
2699 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002700 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2701 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002702}
2703
Sean Huntc3021132010-05-05 15:23:54 +00002704ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002705 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002706 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002707 NumArgs * sizeof(Expr *);
2708 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2709 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2710}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002711
2712SourceRange ObjCMessageExpr::getReceiverRange() const {
2713 switch (getReceiverKind()) {
2714 case Instance:
2715 return getInstanceReceiver()->getSourceRange();
2716
2717 case Class:
2718 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2719
2720 case SuperInstance:
2721 case SuperClass:
2722 return getSuperLoc();
2723 }
2724
2725 return SourceLocation();
2726}
2727
Douglas Gregor04badcf2010-04-21 00:45:42 +00002728Selector ObjCMessageExpr::getSelector() const {
2729 if (HasMethod)
2730 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2731 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002732 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002733}
2734
2735ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2736 switch (getReceiverKind()) {
2737 case Instance:
2738 if (const ObjCObjectPointerType *Ptr
2739 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2740 return Ptr->getInterfaceDecl();
2741 break;
2742
2743 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002744 if (const ObjCObjectType *Ty
2745 = getClassReceiver()->getAs<ObjCObjectType>())
2746 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002747 break;
2748
2749 case SuperInstance:
2750 if (const ObjCObjectPointerType *Ptr
2751 = getSuperType()->getAs<ObjCObjectPointerType>())
2752 return Ptr->getInterfaceDecl();
2753 break;
2754
2755 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002756 if (const ObjCObjectType *Iface
2757 = getSuperType()->getAs<ObjCObjectType>())
2758 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002759 break;
2760 }
2761
2762 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002763}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002764
John McCallf85e1932011-06-15 23:02:42 +00002765llvm::StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
2766 switch (getBridgeKind()) {
2767 case OBC_Bridge:
2768 return "__bridge";
2769 case OBC_BridgeTransfer:
2770 return "__bridge_transfer";
2771 case OBC_BridgeRetained:
2772 return "__bridge_retained";
2773 }
2774
2775 return "__bridge";
2776}
2777
Jay Foad4ba2a172011-01-12 09:06:06 +00002778bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002779 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002780}
2781
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002782ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2783 QualType Type, SourceLocation BLoc,
2784 SourceLocation RP)
2785 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2786 Type->isDependentType(), Type->isDependentType(),
2787 Type->containsUnexpandedParameterPack()),
2788 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2789{
2790 SubExprs = new (C) Stmt*[nexpr];
2791 for (unsigned i = 0; i < nexpr; i++) {
2792 if (args[i]->isTypeDependent())
2793 ExprBits.TypeDependent = true;
2794 if (args[i]->isValueDependent())
2795 ExprBits.ValueDependent = true;
2796 if (args[i]->containsUnexpandedParameterPack())
2797 ExprBits.ContainsUnexpandedParameterPack = true;
2798
2799 SubExprs[i] = args[i];
2800 }
2801}
2802
Nate Begeman888376a2009-08-12 02:28:50 +00002803void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2804 unsigned NumExprs) {
2805 if (SubExprs) C.Deallocate(SubExprs);
2806
2807 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002808 this->NumExprs = NumExprs;
2809 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002810}
Nate Begeman888376a2009-08-12 02:28:50 +00002811
Peter Collingbournef111d932011-04-15 00:35:48 +00002812GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2813 SourceLocation GenericLoc, Expr *ControllingExpr,
2814 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2815 unsigned NumAssocs, SourceLocation DefaultLoc,
2816 SourceLocation RParenLoc,
2817 bool ContainsUnexpandedParameterPack,
2818 unsigned ResultIndex)
2819 : Expr(GenericSelectionExprClass,
2820 AssocExprs[ResultIndex]->getType(),
2821 AssocExprs[ResultIndex]->getValueKind(),
2822 AssocExprs[ResultIndex]->getObjectKind(),
2823 AssocExprs[ResultIndex]->isTypeDependent(),
2824 AssocExprs[ResultIndex]->isValueDependent(),
2825 ContainsUnexpandedParameterPack),
2826 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2827 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2828 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2829 RParenLoc(RParenLoc) {
2830 SubExprs[CONTROLLING] = ControllingExpr;
2831 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2832 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2833}
2834
2835GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2836 SourceLocation GenericLoc, Expr *ControllingExpr,
2837 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2838 unsigned NumAssocs, SourceLocation DefaultLoc,
2839 SourceLocation RParenLoc,
2840 bool ContainsUnexpandedParameterPack)
2841 : Expr(GenericSelectionExprClass,
2842 Context.DependentTy,
2843 VK_RValue,
2844 OK_Ordinary,
2845 /*isTypeDependent=*/ true,
2846 /*isValueDependent=*/ true,
2847 ContainsUnexpandedParameterPack),
2848 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2849 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2850 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2851 RParenLoc(RParenLoc) {
2852 SubExprs[CONTROLLING] = ControllingExpr;
2853 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2854 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2855}
2856
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002857//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002858// DesignatedInitExpr
2859//===----------------------------------------------------------------------===//
2860
Chandler Carruthb1138242011-06-16 06:47:06 +00002861IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00002862 assert(Kind == FieldDesignator && "Only valid on a field designator");
2863 if (Field.NameOrField & 0x01)
2864 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2865 else
2866 return getField()->getIdentifier();
2867}
2868
Sean Huntc3021132010-05-05 15:23:54 +00002869DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002870 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002871 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002872 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002873 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002874 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002875 unsigned NumIndexExprs,
2876 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002877 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002878 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002879 Init->isTypeDependent(), Init->isValueDependent(),
2880 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002881 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2882 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002883 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002884
2885 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002886 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002887 *Child++ = Init;
2888
2889 // Copy the designators and their subexpressions, computing
2890 // value-dependence along the way.
2891 unsigned IndexIdx = 0;
2892 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002893 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002894
2895 if (this->Designators[I].isArrayDesignator()) {
2896 // Compute type- and value-dependence.
2897 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002898 if (Index->isTypeDependent() || Index->isValueDependent())
2899 ExprBits.ValueDependent = true;
2900
2901 // Propagate unexpanded parameter packs.
2902 if (Index->containsUnexpandedParameterPack())
2903 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002904
2905 // Copy the index expressions into permanent storage.
2906 *Child++ = IndexExprs[IndexIdx++];
2907 } else if (this->Designators[I].isArrayRangeDesignator()) {
2908 // Compute type- and value-dependence.
2909 Expr *Start = IndexExprs[IndexIdx];
2910 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002911 if (Start->isTypeDependent() || Start->isValueDependent() ||
2912 End->isTypeDependent() || End->isValueDependent())
2913 ExprBits.ValueDependent = true;
2914
2915 // Propagate unexpanded parameter packs.
2916 if (Start->containsUnexpandedParameterPack() ||
2917 End->containsUnexpandedParameterPack())
2918 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002919
2920 // Copy the start/end expressions into permanent storage.
2921 *Child++ = IndexExprs[IndexIdx++];
2922 *Child++ = IndexExprs[IndexIdx++];
2923 }
2924 }
2925
2926 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002927}
2928
Douglas Gregor05c13a32009-01-22 00:58:24 +00002929DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002930DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002931 unsigned NumDesignators,
2932 Expr **IndexExprs, unsigned NumIndexExprs,
2933 SourceLocation ColonOrEqualLoc,
2934 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002935 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002936 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002937 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002938 ColonOrEqualLoc, UsesColonSyntax,
2939 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002940}
2941
Mike Stump1eb44332009-09-09 15:08:12 +00002942DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002943 unsigned NumIndexExprs) {
2944 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2945 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2946 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2947}
2948
Douglas Gregor319d57f2010-01-06 23:17:19 +00002949void DesignatedInitExpr::setDesignators(ASTContext &C,
2950 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002951 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002952 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002953 NumDesignators = NumDesigs;
2954 for (unsigned I = 0; I != NumDesigs; ++I)
2955 Designators[I] = Desigs[I];
2956}
2957
Abramo Bagnara24f46742011-03-16 15:08:46 +00002958SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2959 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2960 if (size() == 1)
2961 return DIE->getDesignator(0)->getSourceRange();
2962 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2963 DIE->getDesignator(size()-1)->getEndLocation());
2964}
2965
Douglas Gregor05c13a32009-01-22 00:58:24 +00002966SourceRange DesignatedInitExpr::getSourceRange() const {
2967 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002968 Designator &First =
2969 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002970 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002971 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002972 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2973 else
2974 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2975 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002976 StartLoc =
2977 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002978 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2979}
2980
Douglas Gregor05c13a32009-01-22 00:58:24 +00002981Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2982 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2983 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2984 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002985 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2986 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2987}
2988
2989Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002990 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002991 "Requires array range designator");
2992 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2993 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002994 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2995 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2996}
2997
2998Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002999 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003000 "Requires array range designator");
3001 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3002 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003003 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3004 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3005}
3006
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003007/// \brief Replaces the designator at index @p Idx with the series
3008/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003009void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003010 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003011 const Designator *Last) {
3012 unsigned NumNewDesignators = Last - First;
3013 if (NumNewDesignators == 0) {
3014 std::copy_backward(Designators + Idx + 1,
3015 Designators + NumDesignators,
3016 Designators + Idx);
3017 --NumNewDesignators;
3018 return;
3019 } else if (NumNewDesignators == 1) {
3020 Designators[Idx] = *First;
3021 return;
3022 }
3023
Mike Stump1eb44332009-09-09 15:08:12 +00003024 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003025 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003026 std::copy(Designators, Designators + Idx, NewDesignators);
3027 std::copy(First, Last, NewDesignators + Idx);
3028 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3029 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003030 Designators = NewDesignators;
3031 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3032}
3033
Mike Stump1eb44332009-09-09 15:08:12 +00003034ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003035 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003036 SourceLocation rparenloc, QualType T)
3037 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003038 false, false, false),
3039 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003040 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003041 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003042 for (unsigned i = 0; i != nexprs; ++i) {
3043 if (exprs[i]->isTypeDependent())
3044 ExprBits.TypeDependent = true;
3045 if (exprs[i]->isValueDependent())
3046 ExprBits.ValueDependent = true;
3047 if (exprs[i]->containsUnexpandedParameterPack())
3048 ExprBits.ContainsUnexpandedParameterPack = true;
3049
Nate Begeman2ef13e52009-08-10 23:49:36 +00003050 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003051 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003052}
3053
John McCalle996ffd2011-02-16 08:02:54 +00003054const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3055 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3056 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003057 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3058 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003059 e = cast<CXXConstructExpr>(e)->getArg(0);
3060 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3061 e = ice->getSubExpr();
3062 return cast<OpaqueValueExpr>(e);
3063}
3064
Douglas Gregor05c13a32009-01-22 00:58:24 +00003065//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003066// ExprIterator.
3067//===----------------------------------------------------------------------===//
3068
3069Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3070Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3071Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3072const Expr* ConstExprIterator::operator[](size_t idx) const {
3073 return cast<Expr>(I[idx]);
3074}
3075const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3076const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3077
3078//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003079// Child Iterators for iterating over subexpressions/substatements
3080//===----------------------------------------------------------------------===//
3081
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003082// UnaryExprOrTypeTraitExpr
3083Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003084 // If this is of a type and the type is a VLA type (and not a typedef), the
3085 // size expression of the VLA needs to be treated as an executable expression.
3086 // Why isn't this weirdness documented better in StmtIterator?
3087 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003088 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003089 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003090 return child_range(child_iterator(T), child_iterator());
3091 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003092 }
John McCall63c00d72011-02-09 08:16:59 +00003093 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003094}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003095
Steve Naroff563477d2007-09-18 23:55:05 +00003096// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003097Stmt::child_range ObjCMessageExpr::children() {
3098 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003099 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003100 begin = reinterpret_cast<Stmt **>(this + 1);
3101 else
3102 begin = reinterpret_cast<Stmt **>(getArgs());
3103 return child_range(begin,
3104 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003105}
3106
Steve Naroff4eb206b2008-09-03 18:15:37 +00003107// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003108BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003109 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003110 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00003111 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003112 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003113 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003114{
Douglas Gregord967e312011-01-19 21:52:31 +00003115 bool TypeDependent = false;
3116 bool ValueDependent = false;
3117 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3118 ExprBits.TypeDependent = TypeDependent;
3119 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003120}