blob: 58fb32d278b85b5e84db62f9418ec8653c88ae0b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000031#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
Chris Lattner2b334bb2010-04-16 23:34:13 +000034/// isKnownToHaveBooleanValue - Return true if this is an integer expression
35/// that is known to return 0 or 1. This happens for _Bool/bool expressions
36/// but also int expressions which are produced by things like comparisons in
37/// C.
38bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000039 const Expr *E = IgnoreParens();
40
Chris Lattner2b334bb2010-04-16 23:34:13 +000041 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000042 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000043 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000045
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000047 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000048 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Sean Huntc3021132010-05-05 15:23:54 +000054
John McCall6907fbe2010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000057 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000059
Peter Collingbournef111d932011-04-15 00:35:48 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 switch (BO->getOpcode()) {
62 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000071 return true;
Sean Huntc3021132010-05-05 15:23:54 +000072
John McCall2de56d12010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000079
John McCall2de56d12010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000082 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Sean Huntc3021132010-05-05 15:23:54 +000085
Peter Collingbournef111d932011-04-15 00:35:48 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000087 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000089
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 return false;
91}
92
John McCall63c00d72011-02-09 08:16:59 +000093// Amusing macro metaprogramming hack: check whether a class provides
94// a more specific implementation of getExprLoc().
95namespace {
96 /// This implementation is used when a class provides a custom
97 /// implementation of getExprLoc.
98 template <class E, class T>
99 SourceLocation getExprLocImpl(const Expr *expr,
100 SourceLocation (T::*v)() const) {
101 return static_cast<const E*>(expr)->getExprLoc();
102 }
103
104 /// This implementation is used when a class doesn't provide
105 /// a custom implementation of getExprLoc. Overload resolution
106 /// should pick it over the implementation above because it's
107 /// more specialized according to function template partial ordering.
108 template <class E>
109 SourceLocation getExprLocImpl(const Expr *expr,
110 SourceLocation (Expr::*v)() const) {
111 return static_cast<const E*>(expr)->getSourceRange().getBegin();
112 }
113}
114
115SourceLocation Expr::getExprLoc() const {
116 switch (getStmtClass()) {
117 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
118#define ABSTRACT_STMT(type)
119#define STMT(type, base) \
120 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
121#define EXPR(type, base) \
122 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
123#include "clang/AST/StmtNodes.inc"
124 }
125 llvm_unreachable("unknown statement kind");
126 return SourceLocation();
127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
John McCalld5532b62009-11-23 01:53:49 +0000133void ExplicitTemplateArgumentList::initializeFrom(
134 const TemplateArgumentListInfo &Info) {
135 LAngleLoc = Info.getLAngleLoc();
136 RAngleLoc = Info.getRAngleLoc();
137 NumTemplateArgs = Info.size();
138
139 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
140 for (unsigned i = 0; i != NumTemplateArgs; ++i)
141 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
142}
143
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000144void ExplicitTemplateArgumentList::initializeFrom(
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 const TemplateArgumentListInfo &Info,
146 bool &Dependent,
147 bool &InstantiationDependent,
148 bool &ContainsUnexpandedParameterPack) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000149 LAngleLoc = Info.getLAngleLoc();
150 RAngleLoc = Info.getRAngleLoc();
151 NumTemplateArgs = Info.size();
152
153 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
154 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
155 Dependent = Dependent || Info[i].getArgument().isDependent();
Douglas Gregor561f8122011-07-01 01:22:09 +0000156 InstantiationDependent = InstantiationDependent ||
157 Info[i].getArgument().isInstantiationDependent();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000158 ContainsUnexpandedParameterPack
159 = ContainsUnexpandedParameterPack ||
160 Info[i].getArgument().containsUnexpandedParameterPack();
161
162 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
163 }
164}
165
John McCalld5532b62009-11-23 01:53:49 +0000166void ExplicitTemplateArgumentList::copyInto(
167 TemplateArgumentListInfo &Info) const {
168 Info.setLAngleLoc(LAngleLoc);
169 Info.setRAngleLoc(RAngleLoc);
170 for (unsigned I = 0; I != NumTemplateArgs; ++I)
171 Info.addArgument(getTemplateArgs()[I]);
172}
173
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000174std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
175 return sizeof(ExplicitTemplateArgumentList) +
176 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
177}
178
John McCalld5532b62009-11-23 01:53:49 +0000179std::size_t ExplicitTemplateArgumentList::sizeFor(
180 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000181 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000182}
183
Douglas Gregor561f8122011-07-01 01:22:09 +0000184/// \brief Compute the type-, value-, and instantiation-dependence of a
185/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000186/// based on the declaration being referenced.
187static void computeDeclRefDependence(NamedDecl *D, QualType T,
188 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000189 bool &ValueDependent,
190 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000191 TypeDependent = false;
192 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000193 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000194
195 // (TD) C++ [temp.dep.expr]p3:
196 // An id-expression is type-dependent if it contains:
197 //
Sean Huntc3021132010-05-05 15:23:54 +0000198 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000199 //
200 // (VD) C++ [temp.dep.constexpr]p2:
201 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000202
Douglas Gregor0da76df2009-11-23 11:41:28 +0000203 // (TD) - an identifier that was declared with dependent type
204 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000205 if (T->isDependentType()) {
206 TypeDependent = true;
207 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000208 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000209 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000210 } else if (T->isInstantiationDependentType()) {
211 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000212 }
Douglas Gregord967e312011-01-19 21:52:31 +0000213
Douglas Gregor0da76df2009-11-23 11:41:28 +0000214 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000215 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000216 == DeclarationName::CXXConversionFunctionName) {
217 QualType T = D->getDeclName().getCXXNameType();
218 if (T->isDependentType()) {
219 TypeDependent = true;
220 ValueDependent = true;
221 InstantiationDependent = true;
222 return;
223 }
224
225 if (T->isInstantiationDependentType())
226 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000227 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000228
Douglas Gregor0da76df2009-11-23 11:41:28 +0000229 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000230 if (isa<NonTypeTemplateParmDecl>(D)) {
231 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000233 return;
234 }
235
Douglas Gregor0da76df2009-11-23 11:41:28 +0000236 // (VD) - a constant with integral or enumeration type and is
237 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000238 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000239 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000240 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000241 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000242 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000243 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 InstantiationDependent = true;
245 }
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000246 }
Douglas Gregord967e312011-01-19 21:52:31 +0000247
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000248 // (VD) - FIXME: Missing from the standard:
249 // - a member function or a static data member of the current
250 // instantiation
251 else if (Var->isStaticDataMember() &&
Douglas Gregor561f8122011-07-01 01:22:09 +0000252 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000253 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000254 InstantiationDependent = true;
255 }
Douglas Gregord967e312011-01-19 21:52:31 +0000256
257 return;
258 }
259
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000260 // (VD) - FIXME: Missing from the standard:
261 // - a member function or a static data member of the current
262 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000263 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
264 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000265 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000266 return;
267 }
268}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000269
Douglas Gregord967e312011-01-19 21:52:31 +0000270void DeclRefExpr::computeDependence() {
271 bool TypeDependent = false;
272 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000273 bool InstantiationDependent = false;
274 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
275 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000276
277 // (TD) C++ [temp.dep.expr]p3:
278 // An id-expression is type-dependent if it contains:
279 //
280 // and
281 //
282 // (VD) C++ [temp.dep.constexpr]p2:
283 // An identifier is value-dependent if it is:
284 if (!TypeDependent && !ValueDependent &&
285 hasExplicitTemplateArgs() &&
286 TemplateSpecializationType::anyDependentTemplateArguments(
287 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000288 getNumTemplateArgs(),
289 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000290 TypeDependent = true;
291 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000292 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000293 }
294
295 ExprBits.TypeDependent = TypeDependent;
296 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000297 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000298
Douglas Gregor10738d32010-12-23 23:51:58 +0000299 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000300 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000301 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000302}
303
Chandler Carruth3aa81402011-05-01 23:48:14 +0000304DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000305 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000306 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000308 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000309 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000310 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
311 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000312 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000313 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000314 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
315 if (FoundD)
316 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000317 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000318 if (TemplateArgs) {
319 bool Dependent = false;
320 bool InstantiationDependent = false;
321 bool ContainsUnexpandedParameterPack = false;
322 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
323 InstantiationDependent,
324 ContainsUnexpandedParameterPack);
325 if (InstantiationDependent)
326 setInstantiationDependent(true);
327 }
328
Abramo Bagnara25777432010-08-11 22:01:17 +0000329 computeDependence();
330}
331
Douglas Gregora2813ce2009-10-23 18:54:35 +0000332DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000333 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000334 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000335 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000336 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000337 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000339 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000340 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000341 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000342 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000343}
344
345DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000346 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000347 ValueDecl *D,
348 const DeclarationNameInfo &NameInfo,
349 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000350 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000351 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000352 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000353 // Filter out cases where the found Decl is the same as the value refenenced.
354 if (D == FoundD)
355 FoundD = 0;
356
Douglas Gregora2813ce2009-10-23 18:54:35 +0000357 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000358 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000359 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000360 if (FoundD)
361 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000362 if (TemplateArgs)
363 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000364
Chris Lattner32488542010-10-30 05:14:06 +0000365 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000366 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
367 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000368}
369
Chandler Carruth3aa81402011-05-01 23:48:14 +0000370DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000371 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000372 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000373 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000374 unsigned NumTemplateArgs) {
375 std::size_t Size = sizeof(DeclRefExpr);
376 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000377 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000378 if (HasFoundDecl)
379 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000380 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000381 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000382
Chris Lattner32488542010-10-30 05:14:06 +0000383 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000384 return new (Mem) DeclRefExpr(EmptyShell());
385}
386
Douglas Gregora2813ce2009-10-23 18:54:35 +0000387SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000389 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000390 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000391 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000392 R.setEnd(getRAngleLoc());
393 return R;
394}
395
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396// FIXME: Maybe this should use DeclPrinter with a special "print predefined
397// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000398std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
399 ASTContext &Context = CurrentDecl->getASTContext();
400
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000402 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000403 return FD->getNameAsString();
404
405 llvm::SmallString<256> Name;
406 llvm::raw_svector_ostream Out(Name);
407
408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000409 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000410 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000411 if (MD->isStatic())
412 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000413 }
414
415 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416
417 std::string Proto = FD->getQualifiedNameAsString(Policy);
418
John McCall183700f2009-09-21 23:43:11 +0000419 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000420 const FunctionProtoType *FT = 0;
421 if (FD->hasWrittenPrototype())
422 FT = dyn_cast<FunctionProtoType>(AFT);
423
424 Proto += "(";
425 if (FT) {
426 llvm::raw_string_ostream POut(Proto);
427 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
428 if (i) POut << ", ";
429 std::string Param;
430 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
431 POut << Param;
432 }
433
434 if (FT->isVariadic()) {
435 if (FD->getNumParams()) POut << ", ";
436 POut << "...";
437 }
438 }
439 Proto += ")";
440
Sam Weinig4eadcc52009-12-27 01:38:20 +0000441 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
442 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
443 if (ThisQuals.hasConst())
444 Proto += " const";
445 if (ThisQuals.hasVolatile())
446 Proto += " volatile";
447 }
448
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000449 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
450 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000451
452 Out << Proto;
453
454 Out.flush();
455 return Name.str().str();
456 }
457 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
458 llvm::SmallString<256> Name;
459 llvm::raw_svector_ostream Out(Name);
460 Out << (MD->isInstanceMethod() ? '-' : '+');
461 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000462
463 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
464 // a null check to avoid a crash.
465 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000466 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000467
Anders Carlsson3a082d82009-09-08 18:24:21 +0000468 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000469 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
470 Out << '(' << CID << ')';
471
Anders Carlsson3a082d82009-09-08 18:24:21 +0000472 Out << ' ';
473 Out << MD->getSelector().getAsString();
474 Out << ']';
475
476 Out.flush();
477 return Name.str().str();
478 }
479 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
480 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
481 return "top level";
482 }
483 return "";
484}
485
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000486void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
487 if (hasAllocation())
488 C.Deallocate(pVal);
489
490 BitWidth = Val.getBitWidth();
491 unsigned NumWords = Val.getNumWords();
492 const uint64_t* Words = Val.getRawData();
493 if (NumWords > 1) {
494 pVal = new (C) uint64_t[NumWords];
495 std::copy(Words, Words + NumWords, pVal);
496 } else if (NumWords == 1)
497 VAL = Words[0];
498 else
499 VAL = 0;
500}
501
502IntegerLiteral *
503IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
504 QualType type, SourceLocation l) {
505 return new (C) IntegerLiteral(C, V, type, l);
506}
507
508IntegerLiteral *
509IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
510 return new (C) IntegerLiteral(Empty);
511}
512
513FloatingLiteral *
514FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
515 bool isexact, QualType Type, SourceLocation L) {
516 return new (C) FloatingLiteral(C, V, isexact, Type, L);
517}
518
519FloatingLiteral *
520FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
521 return new (C) FloatingLiteral(Empty);
522}
523
Chris Lattnerda8249e2008-06-07 22:13:43 +0000524/// getValueAsApproximateDouble - This returns the value as an inaccurate
525/// double. Note that this may cause loss of precision, but is useful for
526/// debugging dumps, etc.
527double FloatingLiteral::getValueAsApproximateDouble() const {
528 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000529 bool ignored;
530 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
531 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000532 return V.convertToDouble();
533}
534
Chris Lattner5f9e2722011-07-23 10:55:15 +0000535StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Jay Foad65aa6882011-06-21 15:13:30 +0000536 bool Wide,
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000537 bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000538 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000539 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000540 // Allocate enough space for the StringLiteral plus an array of locations for
541 // any concatenated string tokens.
542 void *Mem = C.Allocate(sizeof(StringLiteral)+
543 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000544 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000545 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 // OPTIMIZE: could allocate this appended to the StringLiteral.
Jay Foad65aa6882011-06-21 15:13:30 +0000548 char *AStrData = new (C, 1) char[Str.size()];
549 memcpy(AStrData, Str.data(), Str.size());
Chris Lattner2085fd62009-02-18 06:40:38 +0000550 SL->StrData = AStrData;
Jay Foad65aa6882011-06-21 15:13:30 +0000551 SL->ByteLength = Str.size();
Chris Lattner2085fd62009-02-18 06:40:38 +0000552 SL->IsWide = Wide;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000553 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000554 SL->TokLocs[0] = Loc[0];
555 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
Chris Lattner726e1682009-02-18 05:49:11 +0000557 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000558 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
559 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000560}
561
Douglas Gregor673ecd62009-04-15 16:35:07 +0000562StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
563 void *Mem = C.Allocate(sizeof(StringLiteral)+
564 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000565 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000566 StringLiteral *SL = new (Mem) StringLiteral(QualType());
567 SL->StrData = 0;
568 SL->ByteLength = 0;
569 SL->NumConcatenated = NumStrs;
570 return SL;
571}
572
Chris Lattner5f9e2722011-07-23 10:55:15 +0000573void StringLiteral::setString(ASTContext &C, StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000574 char *AStrData = new (C, 1) char[Str.size()];
575 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000576 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000577 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000578}
579
Chris Lattner08f92e32010-11-17 07:37:15 +0000580/// getLocationOfByte - Return a source location that points to the specified
581/// byte of this string literal.
582///
583/// Strings are amazingly complex. They can be formed from multiple tokens and
584/// can have escape sequences in them in addition to the usual trigraph and
585/// escaped newline business. This routine handles this complexity.
586///
587SourceLocation StringLiteral::
588getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
589 const LangOptions &Features, const TargetInfo &Target) const {
590 assert(!isWide() && "This doesn't work for wide strings yet");
591
592 // Loop over all of the tokens in this string until we find the one that
593 // contains the byte we're looking for.
594 unsigned TokNo = 0;
595 while (1) {
596 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
597 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
598
599 // Get the spelling of the string so that we can get the data that makes up
600 // the string literal, not the identifier for the macro it is potentially
601 // expanded through.
602 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
603
604 // Re-lex the token to get its length and original spelling.
605 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
606 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000607 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000608 if (Invalid)
609 return StrTokSpellingLoc;
610
611 const char *StrData = Buffer.data()+LocInfo.second;
612
613 // Create a langops struct and enable trigraphs. This is sufficient for
614 // relexing tokens.
615 LangOptions LangOpts;
616 LangOpts.Trigraphs = true;
617
618 // Create a lexer starting at the beginning of this token.
619 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
620 Buffer.end());
621 Token TheTok;
622 TheLexer.LexFromRawLexer(TheTok);
623
624 // Use the StringLiteralParser to compute the length of the string in bytes.
625 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
626 unsigned TokNumBytes = SLP.GetStringLength();
627
628 // If the byte is in this token, return the location of the byte.
629 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000630 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000631 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
632
633 // Now that we know the offset of the token in the spelling, use the
634 // preprocessor to get the offset in the original source.
635 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
636 }
637
638 // Move to the next string token.
639 ++TokNo;
640 ByteNo -= TokNumBytes;
641 }
642}
643
644
645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
647/// corresponds to, e.g. "sizeof" or "[pre]++".
648const char *UnaryOperator::getOpcodeStr(Opcode Op) {
649 switch (Op) {
650 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000651 case UO_PostInc: return "++";
652 case UO_PostDec: return "--";
653 case UO_PreInc: return "++";
654 case UO_PreDec: return "--";
655 case UO_AddrOf: return "&";
656 case UO_Deref: return "*";
657 case UO_Plus: return "+";
658 case UO_Minus: return "-";
659 case UO_Not: return "~";
660 case UO_LNot: return "!";
661 case UO_Real: return "__real";
662 case UO_Imag: return "__imag";
663 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 }
665}
666
John McCall2de56d12010-08-25 11:45:40 +0000667UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000668UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
669 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000670 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000671 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
672 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
673 case OO_Amp: return UO_AddrOf;
674 case OO_Star: return UO_Deref;
675 case OO_Plus: return UO_Plus;
676 case OO_Minus: return UO_Minus;
677 case OO_Tilde: return UO_Not;
678 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000679 }
680}
681
682OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
683 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000684 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
685 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
686 case UO_AddrOf: return OO_Amp;
687 case UO_Deref: return OO_Star;
688 case UO_Plus: return OO_Plus;
689 case UO_Minus: return OO_Minus;
690 case UO_Not: return OO_Tilde;
691 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000692 default: return OO_None;
693 }
694}
695
696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697//===----------------------------------------------------------------------===//
698// Postfix Operators.
699//===----------------------------------------------------------------------===//
700
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000701CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
702 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000703 SourceLocation rparenloc)
704 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000705 fn->isTypeDependent(),
706 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000707 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000708 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000709 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000711 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000712 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713 for (unsigned i = 0; i != numargs; ++i) {
714 if (args[i]->isTypeDependent())
715 ExprBits.TypeDependent = true;
716 if (args[i]->isValueDependent())
717 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000718 if (args[i]->isInstantiationDependent())
719 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000720 if (args[i]->containsUnexpandedParameterPack())
721 ExprBits.ContainsUnexpandedParameterPack = true;
722
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000723 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000724 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000725
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000726 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000727 RParenLoc = rparenloc;
728}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000729
Ted Kremenek668bf912009-02-09 20:51:47 +0000730CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000731 QualType t, ExprValueKind VK, SourceLocation rparenloc)
732 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000733 fn->isTypeDependent(),
734 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000735 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000736 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000737 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000738
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000739 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000740 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000741 for (unsigned i = 0; i != numargs; ++i) {
742 if (args[i]->isTypeDependent())
743 ExprBits.TypeDependent = true;
744 if (args[i]->isValueDependent())
745 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000746 if (args[i]->isInstantiationDependent())
747 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000748 if (args[i]->containsUnexpandedParameterPack())
749 ExprBits.ContainsUnexpandedParameterPack = true;
750
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000751 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000752 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000753
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000754 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 RParenLoc = rparenloc;
756}
757
Mike Stump1eb44332009-09-09 15:08:12 +0000758CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
759 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000760 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000761 SubExprs = new (C) Stmt*[PREARGS_START];
762 CallExprBits.NumPreArgs = 0;
763}
764
765CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
766 EmptyShell Empty)
767 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
768 // FIXME: Why do we allocate this?
769 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
770 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000771}
772
Nuno Lopesd20254f2009-12-20 23:11:08 +0000773Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000774 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000775 // If we're calling a dereference, look at the pointer instead.
776 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
777 if (BO->isPtrMemOp())
778 CEE = BO->getRHS()->IgnoreParenCasts();
779 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
780 if (UO->getOpcode() == UO_Deref)
781 CEE = UO->getSubExpr()->IgnoreParenCasts();
782 }
Chris Lattner6346f962009-07-17 15:46:27 +0000783 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000784 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000785 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
786 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000787
788 return 0;
789}
790
Nuno Lopesd20254f2009-12-20 23:11:08 +0000791FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000792 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000793}
794
Chris Lattnerd18b3292007-12-28 05:25:02 +0000795/// setNumArgs - This changes the number of arguments present in this call.
796/// Any orphaned expressions are deleted by this, and any new operands are set
797/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000798void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000799 // No change, just return.
800 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattnerd18b3292007-12-28 05:25:02 +0000802 // If shrinking # arguments, just delete the extras and forgot them.
803 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000804 this->NumArgs = NumArgs;
805 return;
806 }
807
808 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000809 unsigned NumPreArgs = getNumPreArgs();
810 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000811 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000812 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000813 NewSubExprs[i] = SubExprs[i];
814 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000815 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
816 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000817 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor88c9a462009-04-17 21:46:47 +0000819 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000820 SubExprs = NewSubExprs;
821 this->NumArgs = NumArgs;
822}
823
Chris Lattnercb888962008-10-06 05:00:53 +0000824/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
825/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000826unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000827 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000828 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000829 // ImplicitCastExpr.
830 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
831 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000832 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000834 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
835 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000836 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Anders Carlssonbcba2012008-01-31 02:13:57 +0000838 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
839 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000840 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000842 if (!FDecl->getIdentifier())
843 return 0;
844
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000845 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000846}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000847
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000848QualType CallExpr::getCallReturnType() const {
849 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000850 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000851 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000852 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000853 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000854 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
855 // This should never be overloaded and so should never return null.
856 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000857
John McCall864c0412011-04-26 20:42:42 +0000858 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000859 return FnType->getResultType();
860}
Chris Lattnercb888962008-10-06 05:00:53 +0000861
John McCall2882eca2011-02-21 06:23:05 +0000862SourceRange CallExpr::getSourceRange() const {
863 if (isa<CXXOperatorCallExpr>(this))
864 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
865
866 SourceLocation begin = getCallee()->getLocStart();
867 if (begin.isInvalid() && getNumArgs() > 0)
868 begin = getArg(0)->getLocStart();
869 SourceLocation end = getRParenLoc();
870 if (end.isInvalid() && getNumArgs() > 0)
871 end = getArg(getNumArgs() - 1)->getLocEnd();
872 return SourceRange(begin, end);
873}
874
Sean Huntc3021132010-05-05 15:23:54 +0000875OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000876 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000877 TypeSourceInfo *tsi,
878 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000879 Expr** exprsPtr, unsigned numExprs,
880 SourceLocation RParenLoc) {
881 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000882 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000883 sizeof(Expr*) * numExprs);
884
885 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
886 exprsPtr, numExprs, RParenLoc);
887}
888
889OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
890 unsigned numComps, unsigned numExprs) {
891 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
892 sizeof(OffsetOfNode) * numComps +
893 sizeof(Expr*) * numExprs);
894 return new (Mem) OffsetOfExpr(numComps, numExprs);
895}
896
Sean Huntc3021132010-05-05 15:23:54 +0000897OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000898 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000899 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000900 Expr** exprsPtr, unsigned numExprs,
901 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000902 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
903 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000904 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000905 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000906 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000907 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
908 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000909{
910 for(unsigned i = 0; i < numComps; ++i) {
911 setComponent(i, compsPtr[i]);
912 }
Sean Huntc3021132010-05-05 15:23:54 +0000913
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000914 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000915 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
916 ExprBits.ValueDependent = true;
917 if (exprsPtr[i]->containsUnexpandedParameterPack())
918 ExprBits.ContainsUnexpandedParameterPack = true;
919
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000920 setIndexExpr(i, exprsPtr[i]);
921 }
922}
923
924IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
925 assert(getKind() == Field || getKind() == Identifier);
926 if (getKind() == Field)
927 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000928
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000929 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
930}
931
Mike Stump1eb44332009-09-09 15:08:12 +0000932MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000933 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000934 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000935 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000936 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000937 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000938 QualType ty,
939 ExprValueKind vk,
940 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000941 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000942
Douglas Gregor40d96a62011-02-28 21:54:11 +0000943 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000944 founddecl.getDecl() != memberdecl ||
945 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000946 if (hasQualOrFound)
947 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
John McCalld5532b62009-11-23 01:53:49 +0000949 if (targs)
950 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Chris Lattner32488542010-10-30 05:14:06 +0000952 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000953 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
954 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000955
956 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000957 // FIXME: Wrong. We should be looking at the member declaration we found.
958 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000959 E->setValueDependent(true);
960 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000961 E->setInstantiationDependent(true);
962 }
963 else if (QualifierLoc &&
964 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
965 E->setInstantiationDependent(true);
966
John McCall6bb80172010-03-30 21:47:33 +0000967 E->HasQualifierOrFoundDecl = true;
968
969 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000970 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000971 NQ->FoundDecl = founddecl;
972 }
973
974 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000975 bool Dependent = false;
976 bool InstantiationDependent = false;
977 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000978 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000979 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
980 InstantiationDependent,
981 ContainsUnexpandedParameterPack);
982 if (InstantiationDependent)
983 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000984 }
985
986 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000987}
988
Douglas Gregor75e85042011-03-02 21:06:53 +0000989SourceRange MemberExpr::getSourceRange() const {
990 SourceLocation StartLoc;
991 if (isImplicitAccess()) {
992 if (hasQualifier())
993 StartLoc = getQualifierLoc().getBeginLoc();
994 else
995 StartLoc = MemberLoc;
996 } else {
997 // FIXME: We don't want this to happen. Rather, we should be able to
998 // detect all kinds of implicit accesses more cleanly.
999 StartLoc = getBase()->getLocStart();
1000 if (StartLoc.isInvalid())
1001 StartLoc = MemberLoc;
1002 }
1003
1004 SourceLocation EndLoc =
1005 HasExplicitTemplateArgumentList? getRAngleLoc()
1006 : getMemberNameInfo().getEndLoc();
1007
1008 return SourceRange(StartLoc, EndLoc);
1009}
1010
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001011const char *CastExpr::getCastKindName() const {
1012 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001013 case CK_Dependent:
1014 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001015 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001016 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001017 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001018 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001019 case CK_LValueToRValue:
1020 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +00001021 case CK_GetObjCProperty:
1022 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +00001023 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001024 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001025 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001026 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001027 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001028 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001029 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001030 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001031 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001032 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001033 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001034 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001035 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001036 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001037 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001038 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001039 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001040 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001041 case CK_NullToPointer:
1042 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001043 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001044 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001045 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001046 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001047 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001048 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001049 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001050 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001051 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001052 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001053 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001054 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001055 case CK_PointerToBoolean:
1056 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001057 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001058 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001059 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001060 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001061 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001062 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001063 case CK_IntegralToBoolean:
1064 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001065 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001066 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001067 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001068 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001069 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001070 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001071 case CK_FloatingToBoolean:
1072 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001073 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001074 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001075 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001076 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001077 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001078 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001079 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001080 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001081 case CK_FloatingRealToComplex:
1082 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001083 case CK_FloatingComplexToReal:
1084 return "FloatingComplexToReal";
1085 case CK_FloatingComplexToBoolean:
1086 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001087 case CK_FloatingComplexCast:
1088 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001089 case CK_FloatingComplexToIntegralComplex:
1090 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001091 case CK_IntegralRealToComplex:
1092 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001093 case CK_IntegralComplexToReal:
1094 return "IntegralComplexToReal";
1095 case CK_IntegralComplexToBoolean:
1096 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001097 case CK_IntegralComplexCast:
1098 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001099 case CK_IntegralComplexToFloatingComplex:
1100 return "IntegralComplexToFloatingComplex";
John McCallf85e1932011-06-15 23:02:42 +00001101 case CK_ObjCConsumeObject:
1102 return "ObjCConsumeObject";
1103 case CK_ObjCProduceObject:
1104 return "ObjCProduceObject";
John McCall7e5e5f42011-07-07 06:58:02 +00001105 case CK_ObjCReclaimReturnedObject:
1106 return "ObjCReclaimReturnedObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001107 }
Mike Stump1eb44332009-09-09 15:08:12 +00001108
John McCall2bb5d002010-11-13 09:02:35 +00001109 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001110 return 0;
1111}
1112
Douglas Gregor6eef5192009-12-14 19:27:10 +00001113Expr *CastExpr::getSubExprAsWritten() {
1114 Expr *SubExpr = 0;
1115 CastExpr *E = this;
1116 do {
1117 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001118
1119 // Skip through reference binding to temporary.
1120 if (MaterializeTemporaryExpr *Materialize
1121 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1122 SubExpr = Materialize->GetTemporaryExpr();
1123
Douglas Gregor6eef5192009-12-14 19:27:10 +00001124 // Skip any temporary bindings; they're implicit.
1125 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1126 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001127
Douglas Gregor6eef5192009-12-14 19:27:10 +00001128 // Conversions by constructor and conversion functions have a
1129 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001130 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001131 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001132 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001133 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001134
Douglas Gregor6eef5192009-12-14 19:27:10 +00001135 // If the subexpression we're left with is an implicit cast, look
1136 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001137 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1138
Douglas Gregor6eef5192009-12-14 19:27:10 +00001139 return SubExpr;
1140}
1141
John McCallf871d0c2010-08-07 06:22:56 +00001142CXXBaseSpecifier **CastExpr::path_buffer() {
1143 switch (getStmtClass()) {
1144#define ABSTRACT_STMT(x)
1145#define CASTEXPR(Type, Base) \
1146 case Stmt::Type##Class: \
1147 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1148#define STMT(Type, Base)
1149#include "clang/AST/StmtNodes.inc"
1150 default:
1151 llvm_unreachable("non-cast expressions not possible here");
1152 return 0;
1153 }
1154}
1155
1156void CastExpr::setCastPath(const CXXCastPath &Path) {
1157 assert(Path.size() == path_size());
1158 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1159}
1160
1161ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1162 CastKind Kind, Expr *Operand,
1163 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001164 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001165 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1166 void *Buffer =
1167 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1168 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001169 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001170 if (PathSize) E->setCastPath(*BasePath);
1171 return E;
1172}
1173
1174ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1175 unsigned PathSize) {
1176 void *Buffer =
1177 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1178 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1179}
1180
1181
1182CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001183 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001184 const CXXCastPath *BasePath,
1185 TypeSourceInfo *WrittenTy,
1186 SourceLocation L, SourceLocation R) {
1187 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1188 void *Buffer =
1189 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1190 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001191 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001192 if (PathSize) E->setCastPath(*BasePath);
1193 return E;
1194}
1195
1196CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1197 void *Buffer =
1198 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1199 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1200}
1201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1203/// corresponds to, e.g. "<<=".
1204const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1205 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001206 case BO_PtrMemD: return ".*";
1207 case BO_PtrMemI: return "->*";
1208 case BO_Mul: return "*";
1209 case BO_Div: return "/";
1210 case BO_Rem: return "%";
1211 case BO_Add: return "+";
1212 case BO_Sub: return "-";
1213 case BO_Shl: return "<<";
1214 case BO_Shr: return ">>";
1215 case BO_LT: return "<";
1216 case BO_GT: return ">";
1217 case BO_LE: return "<=";
1218 case BO_GE: return ">=";
1219 case BO_EQ: return "==";
1220 case BO_NE: return "!=";
1221 case BO_And: return "&";
1222 case BO_Xor: return "^";
1223 case BO_Or: return "|";
1224 case BO_LAnd: return "&&";
1225 case BO_LOr: return "||";
1226 case BO_Assign: return "=";
1227 case BO_MulAssign: return "*=";
1228 case BO_DivAssign: return "/=";
1229 case BO_RemAssign: return "%=";
1230 case BO_AddAssign: return "+=";
1231 case BO_SubAssign: return "-=";
1232 case BO_ShlAssign: return "<<=";
1233 case BO_ShrAssign: return ">>=";
1234 case BO_AndAssign: return "&=";
1235 case BO_XorAssign: return "^=";
1236 case BO_OrAssign: return "|=";
1237 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001239
1240 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001241}
1242
John McCall2de56d12010-08-25 11:45:40 +00001243BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001244BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1245 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001246 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001247 case OO_Plus: return BO_Add;
1248 case OO_Minus: return BO_Sub;
1249 case OO_Star: return BO_Mul;
1250 case OO_Slash: return BO_Div;
1251 case OO_Percent: return BO_Rem;
1252 case OO_Caret: return BO_Xor;
1253 case OO_Amp: return BO_And;
1254 case OO_Pipe: return BO_Or;
1255 case OO_Equal: return BO_Assign;
1256 case OO_Less: return BO_LT;
1257 case OO_Greater: return BO_GT;
1258 case OO_PlusEqual: return BO_AddAssign;
1259 case OO_MinusEqual: return BO_SubAssign;
1260 case OO_StarEqual: return BO_MulAssign;
1261 case OO_SlashEqual: return BO_DivAssign;
1262 case OO_PercentEqual: return BO_RemAssign;
1263 case OO_CaretEqual: return BO_XorAssign;
1264 case OO_AmpEqual: return BO_AndAssign;
1265 case OO_PipeEqual: return BO_OrAssign;
1266 case OO_LessLess: return BO_Shl;
1267 case OO_GreaterGreater: return BO_Shr;
1268 case OO_LessLessEqual: return BO_ShlAssign;
1269 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1270 case OO_EqualEqual: return BO_EQ;
1271 case OO_ExclaimEqual: return BO_NE;
1272 case OO_LessEqual: return BO_LE;
1273 case OO_GreaterEqual: return BO_GE;
1274 case OO_AmpAmp: return BO_LAnd;
1275 case OO_PipePipe: return BO_LOr;
1276 case OO_Comma: return BO_Comma;
1277 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001278 }
1279}
1280
1281OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1282 static const OverloadedOperatorKind OverOps[] = {
1283 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1284 OO_Star, OO_Slash, OO_Percent,
1285 OO_Plus, OO_Minus,
1286 OO_LessLess, OO_GreaterGreater,
1287 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1288 OO_EqualEqual, OO_ExclaimEqual,
1289 OO_Amp,
1290 OO_Caret,
1291 OO_Pipe,
1292 OO_AmpAmp,
1293 OO_PipePipe,
1294 OO_Equal, OO_StarEqual,
1295 OO_SlashEqual, OO_PercentEqual,
1296 OO_PlusEqual, OO_MinusEqual,
1297 OO_LessLessEqual, OO_GreaterGreaterEqual,
1298 OO_AmpEqual, OO_CaretEqual,
1299 OO_PipeEqual,
1300 OO_Comma
1301 };
1302 return OverOps[Opc];
1303}
1304
Ted Kremenek709210f2010-04-13 23:39:13 +00001305InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001306 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001307 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001308 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001309 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001310 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001311 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001312 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001313{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001314 for (unsigned I = 0; I != numInits; ++I) {
1315 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001316 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001317 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001318 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001319 if (initExprs[I]->isInstantiationDependent())
1320 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001321 if (initExprs[I]->containsUnexpandedParameterPack())
1322 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001323 }
Sean Huntc3021132010-05-05 15:23:54 +00001324
Ted Kremenek709210f2010-04-13 23:39:13 +00001325 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001326}
Reid Spencer5f016e22007-07-11 17:01:13 +00001327
Ted Kremenek709210f2010-04-13 23:39:13 +00001328void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001329 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001330 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001331}
1332
Ted Kremenek709210f2010-04-13 23:39:13 +00001333void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001334 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001335}
1336
Ted Kremenek709210f2010-04-13 23:39:13 +00001337Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001338 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001339 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001340 InitExprs.back() = expr;
1341 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor4c678342009-01-28 21:54:33 +00001344 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1345 InitExprs[Init] = expr;
1346 return Result;
1347}
1348
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001349void InitListExpr::setArrayFiller(Expr *filler) {
1350 ArrayFillerOrUnionFieldInit = filler;
1351 // Fill out any "holes" in the array due to designated initializers.
1352 Expr **inits = getInits();
1353 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1354 if (inits[i] == 0)
1355 inits[i] = filler;
1356}
1357
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001358SourceRange InitListExpr::getSourceRange() const {
1359 if (SyntacticForm)
1360 return SyntacticForm->getSourceRange();
1361 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1362 if (Beg.isInvalid()) {
1363 // Find the first non-null initializer.
1364 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1365 E = InitExprs.end();
1366 I != E; ++I) {
1367 if (Stmt *S = *I) {
1368 Beg = S->getLocStart();
1369 break;
1370 }
1371 }
1372 }
1373 if (End.isInvalid()) {
1374 // Find the first non-null initializer from the end.
1375 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1376 E = InitExprs.rend();
1377 I != E; ++I) {
1378 if (Stmt *S = *I) {
1379 End = S->getSourceRange().getEnd();
1380 break;
1381 }
1382 }
1383 }
1384 return SourceRange(Beg, End);
1385}
1386
Steve Naroffbfdcae62008-09-04 15:31:07 +00001387/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001388///
1389const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001390 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001391 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001392}
1393
Mike Stump1eb44332009-09-09 15:08:12 +00001394SourceLocation BlockExpr::getCaretLocation() const {
1395 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001396}
Mike Stump1eb44332009-09-09 15:08:12 +00001397const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001398 return TheBlock->getBody();
1399}
Mike Stump1eb44332009-09-09 15:08:12 +00001400Stmt *BlockExpr::getBody() {
1401 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001402}
Steve Naroff56ee6892008-10-08 17:01:13 +00001403
1404
Reid Spencer5f016e22007-07-11 17:01:13 +00001405//===----------------------------------------------------------------------===//
1406// Generic Expression Routines
1407//===----------------------------------------------------------------------===//
1408
Chris Lattner026dc962009-02-14 07:37:35 +00001409/// isUnusedResultAWarning - Return true if this immediate expression should
1410/// be warned about if the result is unused. If so, fill in Loc and Ranges
1411/// with location to warn on and the source range[s] to report with the
1412/// warning.
1413bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001414 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001415 // Don't warn if the expr is type dependent. The type could end up
1416 // instantiating to void.
1417 if (isTypeDependent())
1418 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 switch (getStmtClass()) {
1421 default:
John McCall0faede62010-03-12 07:11:26 +00001422 if (getType()->isVoidType())
1423 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001424 Loc = getExprLoc();
1425 R1 = getSourceRange();
1426 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001428 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001429 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001430 case GenericSelectionExprClass:
1431 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1432 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 case UnaryOperatorClass: {
1434 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001437 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001438 case UO_PostInc:
1439 case UO_PostDec:
1440 case UO_PreInc:
1441 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001442 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001443 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001445 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001446 return false;
1447 break;
John McCall2de56d12010-08-25 11:45:40 +00001448 case UO_Real:
1449 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001451 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1452 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001453 return false;
1454 break;
John McCall2de56d12010-08-25 11:45:40 +00001455 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001456 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 }
Chris Lattner026dc962009-02-14 07:37:35 +00001458 Loc = UO->getOperatorLoc();
1459 R1 = UO->getSubExpr()->getSourceRange();
1460 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001462 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001463 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001464 switch (BO->getOpcode()) {
1465 default:
1466 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001467 // Consider the RHS of comma for side effects. LHS was checked by
1468 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001469 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001470 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1471 // lvalue-ness) of an assignment written in a macro.
1472 if (IntegerLiteral *IE =
1473 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1474 if (IE->getValue() == 0)
1475 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001476 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1477 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001478 case BO_LAnd:
1479 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001480 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1481 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1482 return false;
1483 break;
John McCallbf0ee352010-02-16 04:10:53 +00001484 }
Chris Lattner026dc962009-02-14 07:37:35 +00001485 if (BO->isAssignmentOp())
1486 return false;
1487 Loc = BO->getOperatorLoc();
1488 R1 = BO->getLHS()->getSourceRange();
1489 R2 = BO->getRHS()->getSourceRange();
1490 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001491 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001492 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001493 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001494 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001495
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001496 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001497 // If only one of the LHS or RHS is a warning, the operator might
1498 // be being used for control flow. Only warn if both the LHS and
1499 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001500 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001501 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1502 return false;
1503 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001504 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001505 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001506 }
1507
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001509 // If the base pointer or element is to a volatile pointer/field, accessing
1510 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001511 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001512 return false;
1513 Loc = cast<MemberExpr>(this)->getMemberLoc();
1514 R1 = SourceRange(Loc, Loc);
1515 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1516 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 case ArraySubscriptExprClass:
1519 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001520 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001521 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001522 return false;
1523 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1524 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1525 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1526 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001527
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001529 case CXXOperatorCallExprClass:
1530 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001531 // If this is a direct call, get the callee.
1532 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001533 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001534 // If the callee has attribute pure, const, or warn_unused_result, warn
1535 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001536 //
1537 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1538 // updated to match for QoI.
1539 if (FD->getAttr<WarnUnusedResultAttr>() ||
1540 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1541 Loc = CE->getCallee()->getLocStart();
1542 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001544 if (unsigned NumArgs = CE->getNumArgs())
1545 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1546 CE->getArg(NumArgs-1)->getLocEnd());
1547 return true;
1548 }
Chris Lattner026dc962009-02-14 07:37:35 +00001549 }
1550 return false;
1551 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001552
1553 case CXXTemporaryObjectExprClass:
1554 case CXXConstructExprClass:
1555 return false;
1556
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001557 case ObjCMessageExprClass: {
1558 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001559 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1560 ME->isInstanceMessage() &&
1561 !ME->getType()->isVoidType() &&
1562 ME->getSelector().getIdentifierInfoForSlot(0) &&
1563 ME->getSelector().getIdentifierInfoForSlot(0)
1564 ->getName().startswith("init")) {
1565 Loc = getExprLoc();
1566 R1 = ME->getSourceRange();
1567 return true;
1568 }
1569
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001570 const ObjCMethodDecl *MD = ME->getMethodDecl();
1571 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1572 Loc = getExprLoc();
1573 return true;
1574 }
Chris Lattner026dc962009-02-14 07:37:35 +00001575 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
John McCall12f78a62010-12-02 01:19:52 +00001578 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001579 Loc = getExprLoc();
1580 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001581 return true;
John McCall12f78a62010-12-02 01:19:52 +00001582
Chris Lattner611b2ec2008-07-26 19:51:01 +00001583 case StmtExprClass: {
1584 // Statement exprs don't logically have side effects themselves, but are
1585 // sometimes used in macros in ways that give them a type that is unused.
1586 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1587 // however, if the result of the stmt expr is dead, we don't want to emit a
1588 // warning.
1589 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001590 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001591 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001592 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001593 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1594 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1595 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
John McCall0faede62010-03-12 07:11:26 +00001598 if (getType()->isVoidType())
1599 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001600 Loc = cast<StmtExpr>(this)->getLParenLoc();
1601 R1 = getSourceRange();
1602 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001603 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001604 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001605 // If this is an explicit cast to void, allow it. People do this when they
1606 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001607 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001608 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001609 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1610 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1611 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001612 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001613 if (getType()->isVoidType())
1614 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001615 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001616
Anders Carlsson58beed92009-11-17 17:11:23 +00001617 // If this is a cast to void or a constructor conversion, check the operand.
1618 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001619 if (CE->getCastKind() == CK_ToVoid ||
1620 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001621 return (cast<CastExpr>(this)->getSubExpr()
1622 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001623 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1624 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1625 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Eli Friedman4be1f472008-05-19 21:24:43 +00001628 case ImplicitCastExprClass:
1629 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001630 return (cast<ImplicitCastExpr>(this)
1631 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001632
Chris Lattner04421082008-04-08 04:40:51 +00001633 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001634 return (cast<CXXDefaultArgExpr>(this)
1635 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001636
1637 case CXXNewExprClass:
1638 // FIXME: In theory, there might be new expressions that don't have side
1639 // effects (e.g. a placement new with an uninitialized POD).
1640 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001641 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001642 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001643 return (cast<CXXBindTemporaryExpr>(this)
1644 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001645 case ExprWithCleanupsClass:
1646 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001647 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001648 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001649}
1650
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001651/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001652/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001653bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001654 const Expr *E = IgnoreParens();
1655 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001656 default:
1657 return false;
1658 case ObjCIvarRefExprClass:
1659 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001660 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001661 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001662 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001663 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001664 case MaterializeTemporaryExprClass:
1665 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1666 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001667 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001668 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001669 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001670 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001671 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1672 if (VD->hasGlobalStorage())
1673 return true;
1674 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001675 // dereferencing to a pointer is always a gc'able candidate,
1676 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001677 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001678 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001679 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001680 return false;
1681 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001682 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001683 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001684 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001685 }
1686 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001687 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001688 }
1689}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001690
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001691bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1692 if (isTypeDependent())
1693 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001694 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001695}
1696
John McCall864c0412011-04-26 20:42:42 +00001697QualType Expr::findBoundMemberType(const Expr *expr) {
1698 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1699
1700 // Bound member expressions are always one of these possibilities:
1701 // x->m x.m x->*y x.*y
1702 // (possibly parenthesized)
1703
1704 expr = expr->IgnoreParens();
1705 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1706 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1707 return mem->getMemberDecl()->getType();
1708 }
1709
1710 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1711 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1712 ->getPointeeType();
1713 assert(type->isFunctionType());
1714 return type;
1715 }
1716
1717 assert(isa<UnresolvedMemberExpr>(expr));
1718 return QualType();
1719}
1720
Sebastian Redl369e51f2010-09-10 20:55:33 +00001721static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1722 Expr::CanThrowResult CT2) {
1723 // CanThrowResult constants are ordered so that the maximum is the correct
1724 // merge result.
1725 return CT1 > CT2 ? CT1 : CT2;
1726}
1727
1728static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1729 Expr *E = const_cast<Expr*>(CE);
1730 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001731 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001732 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1733 }
1734 return R;
1735}
1736
Richard Smith7a614d82011-06-11 17:19:42 +00001737static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1738 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001739 bool NullThrows = true) {
1740 if (!D)
1741 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1742
1743 // See if we can get a function type from the decl somehow.
1744 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1745 if (!VD) // If we have no clue what we're calling, assume the worst.
1746 return Expr::CT_Can;
1747
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001748 // As an extension, we assume that __attribute__((nothrow)) functions don't
1749 // throw.
1750 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1751 return Expr::CT_Cannot;
1752
Sebastian Redl369e51f2010-09-10 20:55:33 +00001753 QualType T = VD->getType();
1754 const FunctionProtoType *FT;
1755 if ((FT = T->getAs<FunctionProtoType>())) {
1756 } else if (const PointerType *PT = T->getAs<PointerType>())
1757 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1758 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1759 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1760 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1761 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1762 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1763 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1764
1765 if (!FT)
1766 return Expr::CT_Can;
1767
Richard Smith7a614d82011-06-11 17:19:42 +00001768 if (FT->getExceptionSpecType() == EST_Delayed) {
1769 assert(isa<CXXConstructorDecl>(D) &&
1770 "only constructor exception specs can be unknown");
1771 Ctx.getDiagnostics().Report(E->getLocStart(),
1772 diag::err_exception_spec_unknown)
1773 << E->getSourceRange();
1774 return Expr::CT_Can;
1775 }
1776
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001777 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001778}
1779
1780static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1781 if (DC->isTypeDependent())
1782 return Expr::CT_Dependent;
1783
Sebastian Redl295995c2010-09-10 20:55:47 +00001784 if (!DC->getTypeAsWritten()->isReferenceType())
1785 return Expr::CT_Cannot;
1786
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001787 if (DC->getSubExpr()->isTypeDependent())
1788 return Expr::CT_Dependent;
1789
Sebastian Redl369e51f2010-09-10 20:55:33 +00001790 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1791}
1792
1793static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1794 const CXXTypeidExpr *DC) {
1795 if (DC->isTypeOperand())
1796 return Expr::CT_Cannot;
1797
1798 Expr *Op = DC->getExprOperand();
1799 if (Op->isTypeDependent())
1800 return Expr::CT_Dependent;
1801
1802 const RecordType *RT = Op->getType()->getAs<RecordType>();
1803 if (!RT)
1804 return Expr::CT_Cannot;
1805
1806 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1807 return Expr::CT_Cannot;
1808
1809 if (Op->Classify(C).isPRValue())
1810 return Expr::CT_Cannot;
1811
1812 return Expr::CT_Can;
1813}
1814
1815Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1816 // C++ [expr.unary.noexcept]p3:
1817 // [Can throw] if in a potentially-evaluated context the expression would
1818 // contain:
1819 switch (getStmtClass()) {
1820 case CXXThrowExprClass:
1821 // - a potentially evaluated throw-expression
1822 return CT_Can;
1823
1824 case CXXDynamicCastExprClass: {
1825 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1826 // where T is a reference type, that requires a run-time check
1827 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1828 if (CT == CT_Can)
1829 return CT;
1830 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1831 }
1832
1833 case CXXTypeidExprClass:
1834 // - a potentially evaluated typeid expression applied to a glvalue
1835 // expression whose type is a polymorphic class type
1836 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1837
1838 // - a potentially evaluated call to a function, member function, function
1839 // pointer, or member function pointer that does not have a non-throwing
1840 // exception-specification
1841 case CallExprClass:
1842 case CXXOperatorCallExprClass:
1843 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001844 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001845 CanThrowResult CT;
1846 if (isTypeDependent())
1847 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001848 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1849 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001850 else
Richard Smith7a614d82011-06-11 17:19:42 +00001851 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001852 if (CT == CT_Can)
1853 return CT;
1854 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1855 }
1856
Sebastian Redl295995c2010-09-10 20:55:47 +00001857 case CXXConstructExprClass:
1858 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001859 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001860 cast<CXXConstructExpr>(this)->getConstructor());
1861 if (CT == CT_Can)
1862 return CT;
1863 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1864 }
1865
1866 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001867 CanThrowResult CT;
1868 if (isTypeDependent())
1869 CT = CT_Dependent;
1870 else
1871 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001872 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1873 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001874 /*NullThrows*/false));
1875 if (CT == CT_Can)
1876 return CT;
1877 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1878 }
1879
1880 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001881 CanThrowResult CT;
1882 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1883 if (DTy.isNull() || DTy->isDependentType()) {
1884 CT = CT_Dependent;
1885 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00001886 CT = CanCalleeThrow(C, this,
1887 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001888 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1889 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00001890 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001891 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001892 if (CT == CT_Can)
1893 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001894 }
1895 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1896 }
1897
1898 case CXXBindTemporaryExprClass: {
1899 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00001900 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001901 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1902 if (CT == CT_Can)
1903 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001904 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1905 }
1906
1907 // ObjC message sends are like function calls, but never have exception
1908 // specs.
1909 case ObjCMessageExprClass:
1910 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001911 return CT_Can;
1912
1913 // Many other things have subexpressions, so we have to test those.
1914 // Some are simple:
1915 case ParenExprClass:
1916 case MemberExprClass:
1917 case CXXReinterpretCastExprClass:
1918 case CXXConstCastExprClass:
1919 case ConditionalOperatorClass:
1920 case CompoundLiteralExprClass:
1921 case ExtVectorElementExprClass:
1922 case InitListExprClass:
1923 case DesignatedInitExprClass:
1924 case ParenListExprClass:
1925 case VAArgExprClass:
1926 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001927 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001928 case ObjCIvarRefExprClass:
1929 case ObjCIsaExprClass:
1930 case ShuffleVectorExprClass:
1931 return CanSubExprsThrow(C, this);
1932
1933 // Some might be dependent for other reasons.
1934 case UnaryOperatorClass:
1935 case ArraySubscriptExprClass:
1936 case ImplicitCastExprClass:
1937 case CStyleCastExprClass:
1938 case CXXStaticCastExprClass:
1939 case CXXFunctionalCastExprClass:
1940 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00001941 case CompoundAssignOperatorClass:
1942 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001943 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1944 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1945 }
1946
1947 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1948 case StmtExprClass:
1949 return CT_Can;
1950
1951 case ChooseExprClass:
1952 if (isTypeDependent() || isValueDependent())
1953 return CT_Dependent;
1954 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1955
Peter Collingbournef111d932011-04-15 00:35:48 +00001956 case GenericSelectionExprClass:
1957 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1958 return CT_Dependent;
1959 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1960
Sebastian Redl369e51f2010-09-10 20:55:33 +00001961 // Some expressions are always dependent.
1962 case DependentScopeDeclRefExprClass:
1963 case CXXUnresolvedConstructExprClass:
1964 case CXXDependentScopeMemberExprClass:
1965 return CT_Dependent;
1966
1967 default:
1968 // All other expressions don't have subexpressions, or else they are
1969 // unevaluated.
1970 return CT_Cannot;
1971 }
1972}
1973
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001974Expr* Expr::IgnoreParens() {
1975 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001976 while (true) {
1977 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1978 E = P->getSubExpr();
1979 continue;
1980 }
1981 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1982 if (P->getOpcode() == UO_Extension) {
1983 E = P->getSubExpr();
1984 continue;
1985 }
1986 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001987 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1988 if (!P->isResultDependent()) {
1989 E = P->getResultExpr();
1990 continue;
1991 }
1992 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001993 return E;
1994 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001995}
1996
Chris Lattner56f34942008-02-13 01:02:39 +00001997/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1998/// or CastExprs or ImplicitCastExprs, returning their operand.
1999Expr *Expr::IgnoreParenCasts() {
2000 Expr *E = this;
2001 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002002 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002003 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002004 continue;
2005 }
2006 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002007 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002008 continue;
2009 }
2010 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2011 if (P->getOpcode() == UO_Extension) {
2012 E = P->getSubExpr();
2013 continue;
2014 }
2015 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002016 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2017 if (!P->isResultDependent()) {
2018 E = P->getResultExpr();
2019 continue;
2020 }
2021 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002022 if (MaterializeTemporaryExpr *Materialize
2023 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2024 E = Materialize->GetTemporaryExpr();
2025 continue;
2026 }
2027
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002028 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002029 }
2030}
2031
John McCall9c5d70c2010-12-04 08:24:19 +00002032/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2033/// casts. This is intended purely as a temporary workaround for code
2034/// that hasn't yet been rewritten to do the right thing about those
2035/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002036Expr *Expr::IgnoreParenLValueCasts() {
2037 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002038 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002039 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2040 E = P->getSubExpr();
2041 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002042 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002043 if (P->getCastKind() == CK_LValueToRValue) {
2044 E = P->getSubExpr();
2045 continue;
2046 }
John McCall9c5d70c2010-12-04 08:24:19 +00002047 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2048 if (P->getOpcode() == UO_Extension) {
2049 E = P->getSubExpr();
2050 continue;
2051 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002052 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2053 if (!P->isResultDependent()) {
2054 E = P->getResultExpr();
2055 continue;
2056 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002057 } else if (MaterializeTemporaryExpr *Materialize
2058 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2059 E = Materialize->GetTemporaryExpr();
2060 continue;
John McCallf6a16482010-12-04 03:47:34 +00002061 }
2062 break;
2063 }
2064 return E;
2065}
2066
John McCall2fc46bf2010-05-05 22:59:52 +00002067Expr *Expr::IgnoreParenImpCasts() {
2068 Expr *E = this;
2069 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002070 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002071 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002072 continue;
2073 }
2074 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002075 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002076 continue;
2077 }
2078 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2079 if (P->getOpcode() == UO_Extension) {
2080 E = P->getSubExpr();
2081 continue;
2082 }
2083 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002084 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2085 if (!P->isResultDependent()) {
2086 E = P->getResultExpr();
2087 continue;
2088 }
2089 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002090 if (MaterializeTemporaryExpr *Materialize
2091 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2092 E = Materialize->GetTemporaryExpr();
2093 continue;
2094 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002095 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002096 }
2097}
2098
Hans Wennborg2f072b42011-06-09 17:06:51 +00002099Expr *Expr::IgnoreConversionOperator() {
2100 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002101 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002102 return MCE->getImplicitObjectArgument();
2103 }
2104 return this;
2105}
2106
Chris Lattnerecdd8412009-03-13 17:28:01 +00002107/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2108/// value (including ptr->int casts of the same size). Strip off any
2109/// ParenExpr or CastExprs, returning their operand.
2110Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2111 Expr *E = this;
2112 while (true) {
2113 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2114 E = P->getSubExpr();
2115 continue;
2116 }
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Chris Lattnerecdd8412009-03-13 17:28:01 +00002118 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2119 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002120 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002121 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Chris Lattnerecdd8412009-03-13 17:28:01 +00002123 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2124 E = SE;
2125 continue;
2126 }
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002128 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002129 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002130 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002131 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002132 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2133 E = SE;
2134 continue;
2135 }
2136 }
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002138 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2139 if (P->getOpcode() == UO_Extension) {
2140 E = P->getSubExpr();
2141 continue;
2142 }
2143 }
2144
Peter Collingbournef111d932011-04-15 00:35:48 +00002145 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2146 if (!P->isResultDependent()) {
2147 E = P->getResultExpr();
2148 continue;
2149 }
2150 }
2151
Chris Lattnerecdd8412009-03-13 17:28:01 +00002152 return E;
2153 }
2154}
2155
Douglas Gregor6eef5192009-12-14 19:27:10 +00002156bool Expr::isDefaultArgument() const {
2157 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002158 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2159 E = M->GetTemporaryExpr();
2160
Douglas Gregor6eef5192009-12-14 19:27:10 +00002161 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2162 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002163
Douglas Gregor6eef5192009-12-14 19:27:10 +00002164 return isa<CXXDefaultArgExpr>(E);
2165}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002166
Douglas Gregor2f599792010-04-02 18:24:57 +00002167/// \brief Skip over any no-op casts and any temporary-binding
2168/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002169static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002170 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2171 E = M->GetTemporaryExpr();
2172
Douglas Gregor2f599792010-04-02 18:24:57 +00002173 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002174 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002175 E = ICE->getSubExpr();
2176 else
2177 break;
2178 }
2179
2180 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2181 E = BE->getSubExpr();
2182
2183 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002184 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002185 E = ICE->getSubExpr();
2186 else
2187 break;
2188 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002189
2190 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002191}
2192
John McCall558d2ab2010-09-15 10:14:12 +00002193/// isTemporaryObject - Determines if this expression produces a
2194/// temporary of the given class type.
2195bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2196 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2197 return false;
2198
Anders Carlssonf8b30152010-11-28 16:40:49 +00002199 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002200
John McCall58277b52010-09-15 20:59:13 +00002201 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002202 if (!E->Classify(C).isPRValue()) {
2203 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002204 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002205 return false;
2206 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002207
John McCall19e60ad2010-09-16 06:57:56 +00002208 // Black-list a few cases which yield pr-values of class type that don't
2209 // refer to temporaries of that type:
2210
2211 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002212 if (isa<ImplicitCastExpr>(E)) {
2213 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2214 case CK_DerivedToBase:
2215 case CK_UncheckedDerivedToBase:
2216 return false;
2217 default:
2218 break;
2219 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002220 }
2221
John McCall19e60ad2010-09-16 06:57:56 +00002222 // - member expressions (all)
2223 if (isa<MemberExpr>(E))
2224 return false;
2225
John McCall56ca35d2011-02-17 10:25:35 +00002226 // - opaque values (all)
2227 if (isa<OpaqueValueExpr>(E))
2228 return false;
2229
John McCall558d2ab2010-09-15 10:14:12 +00002230 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002231}
2232
Douglas Gregor75e85042011-03-02 21:06:53 +00002233bool Expr::isImplicitCXXThis() const {
2234 const Expr *E = this;
2235
2236 // Strip away parentheses and casts we don't care about.
2237 while (true) {
2238 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2239 E = Paren->getSubExpr();
2240 continue;
2241 }
2242
2243 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2244 if (ICE->getCastKind() == CK_NoOp ||
2245 ICE->getCastKind() == CK_LValueToRValue ||
2246 ICE->getCastKind() == CK_DerivedToBase ||
2247 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2248 E = ICE->getSubExpr();
2249 continue;
2250 }
2251 }
2252
2253 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2254 if (UnOp->getOpcode() == UO_Extension) {
2255 E = UnOp->getSubExpr();
2256 continue;
2257 }
2258 }
2259
Douglas Gregor03e80032011-06-21 17:03:29 +00002260 if (const MaterializeTemporaryExpr *M
2261 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2262 E = M->GetTemporaryExpr();
2263 continue;
2264 }
2265
Douglas Gregor75e85042011-03-02 21:06:53 +00002266 break;
2267 }
2268
2269 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2270 return This->isImplicit();
2271
2272 return false;
2273}
2274
Douglas Gregor898574e2008-12-05 23:32:09 +00002275/// hasAnyTypeDependentArguments - Determines if any of the expressions
2276/// in Exprs is type-dependent.
2277bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2278 for (unsigned I = 0; I < NumExprs; ++I)
2279 if (Exprs[I]->isTypeDependent())
2280 return true;
2281
2282 return false;
2283}
2284
2285/// hasAnyValueDependentArguments - Determines if any of the expressions
2286/// in Exprs is value-dependent.
2287bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2288 for (unsigned I = 0; I < NumExprs; ++I)
2289 if (Exprs[I]->isValueDependent())
2290 return true;
2291
2292 return false;
2293}
2294
John McCall4204f072010-08-02 21:13:48 +00002295bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002296 // This function is attempting whether an expression is an initializer
2297 // which can be evaluated at compile-time. isEvaluatable handles most
2298 // of the cases, but it can't deal with some initializer-specific
2299 // expressions, and it can't deal with aggregates; we deal with those here,
2300 // and fall back to isEvaluatable for the other cases.
2301
John McCall4204f072010-08-02 21:13:48 +00002302 // If we ever capture reference-binding directly in the AST, we can
2303 // kill the second parameter.
2304
2305 if (IsForRef) {
2306 EvalResult Result;
2307 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2308 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002309
Anders Carlssone8a32b82008-11-24 05:23:59 +00002310 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002311 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002312 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002313 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002314 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002315 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002316 case CXXTemporaryObjectExprClass:
2317 case CXXConstructExprClass: {
2318 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002319
2320 // Only if it's
2321 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002322 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002323 if (!CE->getNumArgs()) return true;
2324
2325 // 2) an elidable trivial copy construction of an operand which is
2326 // itself a constant initializer. Note that we consider the
2327 // operand on its own, *not* as a reference binding.
2328 return CE->isElidable() &&
2329 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002330 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002331 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002332 // This handles gcc's extension that allows global initializers like
2333 // "struct x {int x;} x = (struct x) {};".
2334 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002335 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002336 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002337 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002338 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002339 // FIXME: This doesn't deal with fields with reference types correctly.
2340 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2341 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002342 const InitListExpr *Exp = cast<InitListExpr>(this);
2343 unsigned numInits = Exp->getNumInits();
2344 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002345 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002346 return false;
2347 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002348 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002349 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002350 case ImplicitValueInitExprClass:
2351 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002352 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002353 return cast<ParenExpr>(this)->getSubExpr()
2354 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002355 case GenericSelectionExprClass:
2356 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2357 return false;
2358 return cast<GenericSelectionExpr>(this)->getResultExpr()
2359 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002360 case ChooseExprClass:
2361 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2362 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002363 case UnaryOperatorClass: {
2364 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002365 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002366 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002367 break;
2368 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002369 case BinaryOperatorClass: {
2370 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2371 // but this handles the common case.
2372 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002373 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002374 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2375 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2376 return true;
2377 break;
2378 }
John McCall4204f072010-08-02 21:13:48 +00002379 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002380 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002381 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002382 case CStyleCastExprClass:
2383 // Handle casts with a destination that's a struct or union; this
2384 // deals with both the gcc no-op struct cast extension and the
2385 // cast-to-union extension.
2386 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002387 return cast<CastExpr>(this)->getSubExpr()
2388 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002389
Chris Lattner430656e2009-10-13 22:12:09 +00002390 // Integer->integer casts can be handled here, which is important for
2391 // things like (int)(&&x-&&y). Scary but true.
2392 if (getType()->isIntegerType() &&
2393 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002394 return cast<CastExpr>(this)->getSubExpr()
2395 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002396
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002397 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002398
2399 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002400 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002401 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002402 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002403 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002404}
2405
Chandler Carruth82214a82011-02-18 23:54:50 +00002406/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2407/// pointer constant or not, as well as the specific kind of constant detected.
2408/// Null pointer constants can be integer constant expressions with the
2409/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2410/// (a GNU extension).
2411Expr::NullPointerConstantKind
2412Expr::isNullPointerConstant(ASTContext &Ctx,
2413 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002414 if (isValueDependent()) {
2415 switch (NPC) {
2416 case NPC_NeverValueDependent:
2417 assert(false && "Unexpected value dependent expression!");
2418 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002419
Douglas Gregorce940492009-09-25 04:25:58 +00002420 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002421 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2422 return NPCK_ZeroInteger;
2423 else
2424 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002425
Douglas Gregorce940492009-09-25 04:25:58 +00002426 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002427 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002428 }
2429 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002430
Sebastian Redl07779722008-10-31 14:43:28 +00002431 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002432 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002433 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002434 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002435 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002436 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002437 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002438 Pointee->isVoidType() && // to void*
2439 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002440 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002441 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002442 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002443 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2444 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002445 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002446 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2447 // Accept ((void*)0) as a null pointer constant, as many other
2448 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002449 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002450 } else if (const GenericSelectionExpr *GE =
2451 dyn_cast<GenericSelectionExpr>(this)) {
2452 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002453 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002454 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002455 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002456 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002457 } else if (isa<GNUNullExpr>(this)) {
2458 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002459 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002460 } else if (const MaterializeTemporaryExpr *M
2461 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2462 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002463 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002464
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002465 // C++0x nullptr_t is always a null pointer constant.
2466 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002467 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002468
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002469 if (const RecordType *UT = getType()->getAsUnionType())
2470 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2471 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2472 const Expr *InitExpr = CLE->getInitializer();
2473 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2474 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2475 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002476 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002477 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002478 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002479 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Reid Spencer5f016e22007-07-11 17:01:13 +00002481 // If we have an integer constant expression, we need to *evaluate* it and
2482 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002483 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002484 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2485
2486 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002487}
Steve Naroff31a45842007-07-28 23:10:27 +00002488
John McCallf6a16482010-12-04 03:47:34 +00002489/// \brief If this expression is an l-value for an Objective C
2490/// property, find the underlying property reference expression.
2491const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2492 const Expr *E = this;
2493 while (true) {
2494 assert((E->getValueKind() == VK_LValue &&
2495 E->getObjectKind() == OK_ObjCProperty) &&
2496 "expression is not a property reference");
2497 E = E->IgnoreParenCasts();
2498 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2499 if (BO->getOpcode() == BO_Comma) {
2500 E = BO->getRHS();
2501 continue;
2502 }
2503 }
2504
2505 break;
2506 }
2507
2508 return cast<ObjCPropertyRefExpr>(E);
2509}
2510
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002511FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002512 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002513
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002514 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002515 if (ICE->getCastKind() == CK_LValueToRValue ||
2516 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002517 E = ICE->getSubExpr()->IgnoreParens();
2518 else
2519 break;
2520 }
2521
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002522 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002523 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002524 if (Field->isBitField())
2525 return Field;
2526
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002527 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2528 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2529 if (Field->isBitField())
2530 return Field;
2531
Eli Friedman42068e92011-07-13 02:05:57 +00002532 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002533 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2534 return BinOp->getLHS()->getBitField();
2535
Eli Friedman42068e92011-07-13 02:05:57 +00002536 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2537 return BinOp->getRHS()->getBitField();
2538 }
2539
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002540 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002541}
2542
Anders Carlsson09380262010-01-31 17:18:49 +00002543bool Expr::refersToVectorElement() const {
2544 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002545
Anders Carlsson09380262010-01-31 17:18:49 +00002546 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002547 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002548 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002549 E = ICE->getSubExpr()->IgnoreParens();
2550 else
2551 break;
2552 }
Sean Huntc3021132010-05-05 15:23:54 +00002553
Anders Carlsson09380262010-01-31 17:18:49 +00002554 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2555 return ASE->getBase()->getType()->isVectorType();
2556
2557 if (isa<ExtVectorElementExpr>(E))
2558 return true;
2559
2560 return false;
2561}
2562
Chris Lattner2140e902009-02-16 22:14:05 +00002563/// isArrow - Return true if the base expression is a pointer to vector,
2564/// return false if the base expression is a vector.
2565bool ExtVectorElementExpr::isArrow() const {
2566 return getBase()->getType()->isPointerType();
2567}
2568
Nate Begeman213541a2008-04-18 23:10:10 +00002569unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002570 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002571 return VT->getNumElements();
2572 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002573}
2574
Nate Begeman8a997642008-05-09 06:41:27 +00002575/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002576bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002577 // FIXME: Refactor this code to an accessor on the AST node which returns the
2578 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002579 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002580
2581 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002582 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002583 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Nate Begeman190d6a22009-01-18 02:01:21 +00002585 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002586 if (Comp[0] == 's' || Comp[0] == 'S')
2587 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Daniel Dunbar15027422009-10-17 23:53:04 +00002589 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002590 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002591 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002592
Steve Narofffec0b492007-07-30 03:29:09 +00002593 return false;
2594}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002595
Nate Begeman8a997642008-05-09 06:41:27 +00002596/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002597void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002598 SmallVectorImpl<unsigned> &Elts) const {
2599 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002600 if (Comp[0] == 's' || Comp[0] == 'S')
2601 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002603 bool isHi = Comp == "hi";
2604 bool isLo = Comp == "lo";
2605 bool isEven = Comp == "even";
2606 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002607
Nate Begeman8a997642008-05-09 06:41:27 +00002608 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2609 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Nate Begeman8a997642008-05-09 06:41:27 +00002611 if (isHi)
2612 Index = e + i;
2613 else if (isLo)
2614 Index = i;
2615 else if (isEven)
2616 Index = 2 * i;
2617 else if (isOdd)
2618 Index = 2 * i + 1;
2619 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002620 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002621
Nate Begeman3b8d1162008-05-13 21:03:02 +00002622 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002623 }
Nate Begeman8a997642008-05-09 06:41:27 +00002624}
2625
Douglas Gregor04badcf2010-04-21 00:45:42 +00002626ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002627 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002628 SourceLocation LBracLoc,
2629 SourceLocation SuperLoc,
2630 bool IsInstanceSuper,
2631 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002632 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002633 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002634 ObjCMethodDecl *Method,
2635 Expr **Args, unsigned NumArgs,
2636 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002637 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002638 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002639 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002640 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002641 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCallf85e1932011-06-15 23:02:42 +00002642 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002643 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2644 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002645 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002646{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002647 setReceiverPointer(SuperType.getAsOpaquePtr());
2648 if (NumArgs)
2649 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002650}
2651
Douglas Gregor04badcf2010-04-21 00:45:42 +00002652ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002653 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002654 SourceLocation LBracLoc,
2655 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002656 Selector Sel,
2657 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002658 ObjCMethodDecl *Method,
2659 Expr **Args, unsigned NumArgs,
2660 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002661 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002662 T->isDependentType(), T->isInstantiationDependentType(),
2663 T->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002664 NumArgs(NumArgs), Kind(Class),
2665 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002666 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2667 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002668 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002669{
2670 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002671 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002672 for (unsigned I = 0; I != NumArgs; ++I) {
2673 if (Args[I]->isTypeDependent())
2674 ExprBits.TypeDependent = true;
2675 if (Args[I]->isValueDependent())
2676 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002677 if (Args[I]->isInstantiationDependent())
2678 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002679 if (Args[I]->containsUnexpandedParameterPack())
2680 ExprBits.ContainsUnexpandedParameterPack = true;
2681
2682 MyArgs[I] = Args[I];
2683 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002684}
2685
Douglas Gregor04badcf2010-04-21 00:45:42 +00002686ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002687 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002688 SourceLocation LBracLoc,
2689 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002690 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002691 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002692 ObjCMethodDecl *Method,
2693 Expr **Args, unsigned NumArgs,
2694 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002695 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002696 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002697 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002698 Receiver->containsUnexpandedParameterPack()),
John McCallf85e1932011-06-15 23:02:42 +00002699 NumArgs(NumArgs), Kind(Instance),
2700 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002701 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2702 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002703 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002704{
2705 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002706 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002707 for (unsigned I = 0; I != NumArgs; ++I) {
2708 if (Args[I]->isTypeDependent())
2709 ExprBits.TypeDependent = true;
2710 if (Args[I]->isValueDependent())
2711 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002712 if (Args[I]->isInstantiationDependent())
2713 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002714 if (Args[I]->containsUnexpandedParameterPack())
2715 ExprBits.ContainsUnexpandedParameterPack = true;
2716
2717 MyArgs[I] = Args[I];
2718 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002719}
2720
Douglas Gregor04badcf2010-04-21 00:45:42 +00002721ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002722 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002723 SourceLocation LBracLoc,
2724 SourceLocation SuperLoc,
2725 bool IsInstanceSuper,
2726 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002727 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002728 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002729 ObjCMethodDecl *Method,
2730 Expr **Args, unsigned NumArgs,
2731 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002732 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002733 NumArgs * sizeof(Expr *);
2734 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002735 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002736 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002737 RBracLoc);
2738}
2739
2740ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002741 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002742 SourceLocation LBracLoc,
2743 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002744 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002745 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002746 ObjCMethodDecl *Method,
2747 Expr **Args, unsigned NumArgs,
2748 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002749 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002750 NumArgs * sizeof(Expr *);
2751 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002752 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2753 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002754}
2755
2756ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002757 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002758 SourceLocation LBracLoc,
2759 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002760 Selector Sel,
2761 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002762 ObjCMethodDecl *Method,
2763 Expr **Args, unsigned NumArgs,
2764 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002765 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002766 NumArgs * sizeof(Expr *);
2767 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002768 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2769 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002770}
2771
Sean Huntc3021132010-05-05 15:23:54 +00002772ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002773 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002774 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002775 NumArgs * sizeof(Expr *);
2776 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2777 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2778}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002779
2780SourceRange ObjCMessageExpr::getReceiverRange() const {
2781 switch (getReceiverKind()) {
2782 case Instance:
2783 return getInstanceReceiver()->getSourceRange();
2784
2785 case Class:
2786 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2787
2788 case SuperInstance:
2789 case SuperClass:
2790 return getSuperLoc();
2791 }
2792
2793 return SourceLocation();
2794}
2795
Douglas Gregor04badcf2010-04-21 00:45:42 +00002796Selector ObjCMessageExpr::getSelector() const {
2797 if (HasMethod)
2798 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2799 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002800 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002801}
2802
2803ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2804 switch (getReceiverKind()) {
2805 case Instance:
2806 if (const ObjCObjectPointerType *Ptr
2807 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2808 return Ptr->getInterfaceDecl();
2809 break;
2810
2811 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002812 if (const ObjCObjectType *Ty
2813 = getClassReceiver()->getAs<ObjCObjectType>())
2814 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002815 break;
2816
2817 case SuperInstance:
2818 if (const ObjCObjectPointerType *Ptr
2819 = getSuperType()->getAs<ObjCObjectPointerType>())
2820 return Ptr->getInterfaceDecl();
2821 break;
2822
2823 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002824 if (const ObjCObjectType *Iface
2825 = getSuperType()->getAs<ObjCObjectType>())
2826 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002827 break;
2828 }
2829
2830 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002831}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002832
Chris Lattner5f9e2722011-07-23 10:55:15 +00002833StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00002834 switch (getBridgeKind()) {
2835 case OBC_Bridge:
2836 return "__bridge";
2837 case OBC_BridgeTransfer:
2838 return "__bridge_transfer";
2839 case OBC_BridgeRetained:
2840 return "__bridge_retained";
2841 }
2842
2843 return "__bridge";
2844}
2845
Jay Foad4ba2a172011-01-12 09:06:06 +00002846bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002847 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002848}
2849
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002850ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2851 QualType Type, SourceLocation BLoc,
2852 SourceLocation RP)
2853 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2854 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002855 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002856 Type->containsUnexpandedParameterPack()),
2857 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2858{
2859 SubExprs = new (C) Stmt*[nexpr];
2860 for (unsigned i = 0; i < nexpr; i++) {
2861 if (args[i]->isTypeDependent())
2862 ExprBits.TypeDependent = true;
2863 if (args[i]->isValueDependent())
2864 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002865 if (args[i]->isInstantiationDependent())
2866 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002867 if (args[i]->containsUnexpandedParameterPack())
2868 ExprBits.ContainsUnexpandedParameterPack = true;
2869
2870 SubExprs[i] = args[i];
2871 }
2872}
2873
Nate Begeman888376a2009-08-12 02:28:50 +00002874void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2875 unsigned NumExprs) {
2876 if (SubExprs) C.Deallocate(SubExprs);
2877
2878 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002879 this->NumExprs = NumExprs;
2880 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002881}
Nate Begeman888376a2009-08-12 02:28:50 +00002882
Peter Collingbournef111d932011-04-15 00:35:48 +00002883GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2884 SourceLocation GenericLoc, Expr *ControllingExpr,
2885 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2886 unsigned NumAssocs, SourceLocation DefaultLoc,
2887 SourceLocation RParenLoc,
2888 bool ContainsUnexpandedParameterPack,
2889 unsigned ResultIndex)
2890 : Expr(GenericSelectionExprClass,
2891 AssocExprs[ResultIndex]->getType(),
2892 AssocExprs[ResultIndex]->getValueKind(),
2893 AssocExprs[ResultIndex]->getObjectKind(),
2894 AssocExprs[ResultIndex]->isTypeDependent(),
2895 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002896 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00002897 ContainsUnexpandedParameterPack),
2898 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2899 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2900 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2901 RParenLoc(RParenLoc) {
2902 SubExprs[CONTROLLING] = ControllingExpr;
2903 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2904 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2905}
2906
2907GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2908 SourceLocation GenericLoc, Expr *ControllingExpr,
2909 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2910 unsigned NumAssocs, SourceLocation DefaultLoc,
2911 SourceLocation RParenLoc,
2912 bool ContainsUnexpandedParameterPack)
2913 : Expr(GenericSelectionExprClass,
2914 Context.DependentTy,
2915 VK_RValue,
2916 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00002917 /*isTypeDependent=*/true,
2918 /*isValueDependent=*/true,
2919 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00002920 ContainsUnexpandedParameterPack),
2921 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2922 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2923 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2924 RParenLoc(RParenLoc) {
2925 SubExprs[CONTROLLING] = ControllingExpr;
2926 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2927 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2928}
2929
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002930//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002931// DesignatedInitExpr
2932//===----------------------------------------------------------------------===//
2933
Chandler Carruthb1138242011-06-16 06:47:06 +00002934IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00002935 assert(Kind == FieldDesignator && "Only valid on a field designator");
2936 if (Field.NameOrField & 0x01)
2937 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2938 else
2939 return getField()->getIdentifier();
2940}
2941
Sean Huntc3021132010-05-05 15:23:54 +00002942DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002943 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002944 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002945 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002946 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002947 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002948 unsigned NumIndexExprs,
2949 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002950 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002951 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002952 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002953 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002954 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002955 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2956 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002957 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002958
2959 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002960 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002961 *Child++ = Init;
2962
2963 // Copy the designators and their subexpressions, computing
2964 // value-dependence along the way.
2965 unsigned IndexIdx = 0;
2966 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002967 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002968
2969 if (this->Designators[I].isArrayDesignator()) {
2970 // Compute type- and value-dependence.
2971 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002972 if (Index->isTypeDependent() || Index->isValueDependent())
2973 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002974 if (Index->isInstantiationDependent())
2975 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002976 // Propagate unexpanded parameter packs.
2977 if (Index->containsUnexpandedParameterPack())
2978 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002979
2980 // Copy the index expressions into permanent storage.
2981 *Child++ = IndexExprs[IndexIdx++];
2982 } else if (this->Designators[I].isArrayRangeDesignator()) {
2983 // Compute type- and value-dependence.
2984 Expr *Start = IndexExprs[IndexIdx];
2985 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002986 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00002987 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002988 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002989 ExprBits.InstantiationDependent = true;
2990 } else if (Start->isInstantiationDependent() ||
2991 End->isInstantiationDependent()) {
2992 ExprBits.InstantiationDependent = true;
2993 }
2994
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002995 // Propagate unexpanded parameter packs.
2996 if (Start->containsUnexpandedParameterPack() ||
2997 End->containsUnexpandedParameterPack())
2998 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002999
3000 // Copy the start/end expressions into permanent storage.
3001 *Child++ = IndexExprs[IndexIdx++];
3002 *Child++ = IndexExprs[IndexIdx++];
3003 }
3004 }
3005
3006 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003007}
3008
Douglas Gregor05c13a32009-01-22 00:58:24 +00003009DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003010DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003011 unsigned NumDesignators,
3012 Expr **IndexExprs, unsigned NumIndexExprs,
3013 SourceLocation ColonOrEqualLoc,
3014 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003015 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003016 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003017 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003018 ColonOrEqualLoc, UsesColonSyntax,
3019 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003020}
3021
Mike Stump1eb44332009-09-09 15:08:12 +00003022DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003023 unsigned NumIndexExprs) {
3024 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3025 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3026 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3027}
3028
Douglas Gregor319d57f2010-01-06 23:17:19 +00003029void DesignatedInitExpr::setDesignators(ASTContext &C,
3030 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003031 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003032 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003033 NumDesignators = NumDesigs;
3034 for (unsigned I = 0; I != NumDesigs; ++I)
3035 Designators[I] = Desigs[I];
3036}
3037
Abramo Bagnara24f46742011-03-16 15:08:46 +00003038SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3039 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3040 if (size() == 1)
3041 return DIE->getDesignator(0)->getSourceRange();
3042 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3043 DIE->getDesignator(size()-1)->getEndLocation());
3044}
3045
Douglas Gregor05c13a32009-01-22 00:58:24 +00003046SourceRange DesignatedInitExpr::getSourceRange() const {
3047 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003048 Designator &First =
3049 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003050 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003051 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003052 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3053 else
3054 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3055 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003056 StartLoc =
3057 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003058 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3059}
3060
Douglas Gregor05c13a32009-01-22 00:58:24 +00003061Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3062 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3063 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3064 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003065 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3066 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3067}
3068
3069Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003070 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003071 "Requires array range designator");
3072 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3073 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003074 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3075 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3076}
3077
3078Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003079 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003080 "Requires array range designator");
3081 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3082 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003083 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3084 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3085}
3086
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003087/// \brief Replaces the designator at index @p Idx with the series
3088/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003089void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003090 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003091 const Designator *Last) {
3092 unsigned NumNewDesignators = Last - First;
3093 if (NumNewDesignators == 0) {
3094 std::copy_backward(Designators + Idx + 1,
3095 Designators + NumDesignators,
3096 Designators + Idx);
3097 --NumNewDesignators;
3098 return;
3099 } else if (NumNewDesignators == 1) {
3100 Designators[Idx] = *First;
3101 return;
3102 }
3103
Mike Stump1eb44332009-09-09 15:08:12 +00003104 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003105 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003106 std::copy(Designators, Designators + Idx, NewDesignators);
3107 std::copy(First, Last, NewDesignators + Idx);
3108 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3109 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003110 Designators = NewDesignators;
3111 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3112}
3113
Mike Stump1eb44332009-09-09 15:08:12 +00003114ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003115 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003116 SourceLocation rparenloc, QualType T)
3117 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003118 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003119 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003120 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003121 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003122 for (unsigned i = 0; i != nexprs; ++i) {
3123 if (exprs[i]->isTypeDependent())
3124 ExprBits.TypeDependent = true;
3125 if (exprs[i]->isValueDependent())
3126 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003127 if (exprs[i]->isInstantiationDependent())
3128 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003129 if (exprs[i]->containsUnexpandedParameterPack())
3130 ExprBits.ContainsUnexpandedParameterPack = true;
3131
Nate Begeman2ef13e52009-08-10 23:49:36 +00003132 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003133 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003134}
3135
John McCalle996ffd2011-02-16 08:02:54 +00003136const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3137 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3138 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003139 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3140 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003141 e = cast<CXXConstructExpr>(e)->getArg(0);
3142 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3143 e = ice->getSubExpr();
3144 return cast<OpaqueValueExpr>(e);
3145}
3146
Douglas Gregor05c13a32009-01-22 00:58:24 +00003147//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003148// ExprIterator.
3149//===----------------------------------------------------------------------===//
3150
3151Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3152Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3153Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3154const Expr* ConstExprIterator::operator[](size_t idx) const {
3155 return cast<Expr>(I[idx]);
3156}
3157const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3158const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3159
3160//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003161// Child Iterators for iterating over subexpressions/substatements
3162//===----------------------------------------------------------------------===//
3163
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003164// UnaryExprOrTypeTraitExpr
3165Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003166 // If this is of a type and the type is a VLA type (and not a typedef), the
3167 // size expression of the VLA needs to be treated as an executable expression.
3168 // Why isn't this weirdness documented better in StmtIterator?
3169 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003170 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003171 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003172 return child_range(child_iterator(T), child_iterator());
3173 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003174 }
John McCall63c00d72011-02-09 08:16:59 +00003175 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003176}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003177
Steve Naroff563477d2007-09-18 23:55:05 +00003178// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003179Stmt::child_range ObjCMessageExpr::children() {
3180 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003181 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003182 begin = reinterpret_cast<Stmt **>(this + 1);
3183 else
3184 begin = reinterpret_cast<Stmt **>(getArgs());
3185 return child_range(begin,
3186 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003187}
3188
Steve Naroff4eb206b2008-09-03 18:15:37 +00003189// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003190BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003191 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003192 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003193 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003194 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003195 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003196{
Douglas Gregord967e312011-01-19 21:52:31 +00003197 bool TypeDependent = false;
3198 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003199 bool InstantiationDependent = false;
3200 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3201 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003202 ExprBits.TypeDependent = TypeDependent;
3203 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003204 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003205}