blob: 0cc046c6d24fe469c42cd43817e489a273221b2a [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
Chris Lattnera4d55d82008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbafa74f2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000036#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000037using namespace clang;
38
Rafael Espindola8d852e32012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola251c4492012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
Stephen Hines6bcf27b2014-05-29 04:14:42 -070047 return nullptr;
Rafael Espindola251c4492012-07-17 20:24:05 +000048
Rafael Espindola0b4fe502012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smith4e43dec2013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola0a7dd832012-10-27 01:03:43 +000057 const Expr *E = this;
58 while (true) {
59 E = E->IgnoreParens();
60
61 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
62 if ((CE->getCastKind() == CK_DerivedToBase ||
63 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
64 E->getType()->isRecordType()) {
65 E = CE->getSubExpr();
66 CXXRecordDecl *Derived
67 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
68 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
69 continue;
70 }
71
72 if (CE->getCastKind() == CK_NoOp) {
73 E = CE->getSubExpr();
74 continue;
75 }
76 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Richard Smithd6b69872013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola0a7dd832012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smithd6b69872013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smithd1b55dc2013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola0a7dd832012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindolaef4b6662012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola0a7dd832012-10-27 01:03:43 +000090 E = BO->getLHS();
91 const MemberPointerType *MPT =
92 BO->getRHS()->getType()->getAs<MemberPointerType>();
93 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smith4e43dec2013-06-03 00:17:11 +000094 continue;
95 } else if (BO->getOpcode() == BO_Comma) {
96 CommaLHSs.push_back(BO->getLHS());
97 E = BO->getRHS();
98 continue;
Rafael Espindola0a7dd832012-10-27 01:03:43 +000099 }
100 }
101
102 // Nothing changed.
103 break;
104 }
105 return E;
106}
107
Chris Lattner2b334bb2010-04-16 23:34:13 +0000108/// isKnownToHaveBooleanValue - Return true if this is an integer expression
109/// that is known to return 0 or 1. This happens for _Bool/bool expressions
110/// but also int expressions which are produced by things like comparisons in
111/// C.
112bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +0000113 const Expr *E = IgnoreParens();
114
Chris Lattner2b334bb2010-04-16 23:34:13 +0000115 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +0000116 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +0000117 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +0000118 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +0000119
Peter Collingbournef111d932011-04-15 00:35:48 +0000120 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000121 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000122 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000123 return UO->getSubExpr()->isKnownToHaveBooleanValue();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700124 case UO_LNot:
125 return true;
Chris Lattner2b334bb2010-04-16 23:34:13 +0000126 default:
127 return false;
128 }
129 }
Sean Huntc3021132010-05-05 15:23:54 +0000130
John McCall6907fbe2010-06-12 01:56:02 +0000131 // Only look through implicit casts. If the user writes
132 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +0000133 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000134 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000135
Peter Collingbournef111d932011-04-15 00:35:48 +0000136 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000137 switch (BO->getOpcode()) {
138 default: return false;
John McCall2de56d12010-08-25 11:45:40 +0000139 case BO_LT: // Relational operators.
140 case BO_GT:
141 case BO_LE:
142 case BO_GE:
143 case BO_EQ: // Equality operators.
144 case BO_NE:
145 case BO_LAnd: // AND operator.
146 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000147 return true;
Sean Huntc3021132010-05-05 15:23:54 +0000148
John McCall2de56d12010-08-25 11:45:40 +0000149 case BO_And: // Bitwise AND operator.
150 case BO_Xor: // Bitwise XOR operator.
151 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000152 // Handle things like (x==2)|(y==12).
153 return BO->getLHS()->isKnownToHaveBooleanValue() &&
154 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000155
John McCall2de56d12010-08-25 11:45:40 +0000156 case BO_Comma:
157 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000158 return BO->getRHS()->isKnownToHaveBooleanValue();
159 }
160 }
Sean Huntc3021132010-05-05 15:23:54 +0000161
Peter Collingbournef111d932011-04-15 00:35:48 +0000162 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000163 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
164 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000165
Chris Lattner2b334bb2010-04-16 23:34:13 +0000166 return false;
167}
168
John McCall63c00d72011-02-09 08:16:59 +0000169// Amusing macro metaprogramming hack: check whether a class provides
170// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000171//
172// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +0000173namespace {
174 /// This implementation is used when a class provides a custom
175 /// implementation of getExprLoc.
176 template <class E, class T>
177 SourceLocation getExprLocImpl(const Expr *expr,
178 SourceLocation (T::*v)() const) {
179 return static_cast<const E*>(expr)->getExprLoc();
180 }
181
182 /// This implementation is used when a class doesn't provide
183 /// a custom implementation of getExprLoc. Overload resolution
184 /// should pick it over the implementation above because it's
185 /// more specialized according to function template partial ordering.
186 template <class E>
187 SourceLocation getExprLocImpl(const Expr *expr,
188 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000189 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000190 }
191}
192
193SourceLocation Expr::getExprLoc() const {
194 switch (getStmtClass()) {
195 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
196#define ABSTRACT_STMT(type)
197#define STMT(type, base) \
198 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
199#define EXPR(type, base) \
200 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
201#include "clang/AST/StmtNodes.inc"
202 }
203 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000204}
205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206//===----------------------------------------------------------------------===//
207// Primary Expressions.
208//===----------------------------------------------------------------------===//
209
Douglas Gregor561f8122011-07-01 01:22:09 +0000210/// \brief Compute the type-, value-, and instantiation-dependence of a
211/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000212/// based on the declaration being referenced.
Craig Topper9db7a7e2013-08-22 04:58:56 +0000213static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
214 QualType T, bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000215 bool &ValueDependent,
216 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000217 TypeDependent = false;
218 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000219 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
Sean Huntc3021132010-05-05 15:23:54 +0000224 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000228
Douglas Gregor0da76df2009-11-23 11:41:28 +0000229 // (TD) - an identifier that was declared with dependent type
230 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000231 if (T->isDependentType()) {
232 TypeDependent = true;
233 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000234 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000235 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000236 } else if (T->isInstantiationDependentType()) {
237 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000238 }
Douglas Gregord967e312011-01-19 21:52:31 +0000239
Douglas Gregor0da76df2009-11-23 11:41:28 +0000240 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000241 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000242 == DeclarationName::CXXConversionFunctionName) {
243 QualType T = D->getDeclName().getCXXNameType();
244 if (T->isDependentType()) {
245 TypeDependent = true;
246 ValueDependent = true;
247 InstantiationDependent = true;
248 return;
249 }
250
251 if (T->isInstantiationDependentType())
252 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000253 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000254
Douglas Gregor0da76df2009-11-23 11:41:28 +0000255 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000256 if (isa<NonTypeTemplateParmDecl>(D)) {
257 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000258 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000259 return;
260 }
261
Douglas Gregor0da76df2009-11-23 11:41:28 +0000262 // (VD) - a constant with integral or enumeration type and is
263 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000264 // (VD) - a constant with literal type and is initialized with an
265 // expression that is value-dependent [C++11].
266 // (VD) - FIXME: Missing from the standard:
267 // - an entity with reference type and is initialized with an
268 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000269 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000270 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smitha10b9782013-04-22 15:31:51 +0000271 Var->getType()->isLiteralType(Ctx) :
Richard Smithdb1822c2011-11-08 01:31:09 +0000272 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikie4ef832f2012-08-10 00:55:35 +0000273 (Var->getType().isConstQualified() ||
Richard Smithdb1822c2011-11-08 01:31:09 +0000274 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000275 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000276 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000277 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000278 InstantiationDependent = true;
279 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000280 }
281
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000282 // (VD) - FIXME: Missing from the standard:
283 // - a member function or a static data member of the current
284 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000285 if (Var->isStaticDataMember() &&
286 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000287 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000288 InstantiationDependent = true;
Richard Smith418220b2013-11-14 22:40:45 +0000289 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290 if (TInfo->getType()->isIncompleteArrayType())
291 TypeDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000292 }
Douglas Gregord967e312011-01-19 21:52:31 +0000293
294 return;
295 }
296
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000297 // (VD) - FIXME: Missing from the standard:
298 // - a member function or a static data member of the current
299 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000300 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000302 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000303 }
Douglas Gregord967e312011-01-19 21:52:31 +0000304}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000305
Craig Topper9db7a7e2013-08-22 04:58:56 +0000306void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000307 bool TypeDependent = false;
308 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000309 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000310 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
311 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000312
313 // (TD) C++ [temp.dep.expr]p3:
314 // An id-expression is type-dependent if it contains:
315 //
316 // and
317 //
318 // (VD) C++ [temp.dep.constexpr]p2:
319 // An identifier is value-dependent if it is:
320 if (!TypeDependent && !ValueDependent &&
321 hasExplicitTemplateArgs() &&
322 TemplateSpecializationType::anyDependentTemplateArguments(
323 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000324 getNumTemplateArgs(),
325 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000326 TypeDependent = true;
327 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000328 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000329 }
330
331 ExprBits.TypeDependent = TypeDependent;
332 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000333 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000334
Douglas Gregor10738d32010-12-23 23:51:58 +0000335 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000336 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000337 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000338}
339
Craig Topper9db7a7e2013-08-22 04:58:56 +0000340DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000341 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000342 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000343 ValueDecl *D, bool RefersToEnclosingLocal,
344 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000345 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000346 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000347 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000348 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000349 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
350 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000351 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000352 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000353 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
354 if (FoundD)
355 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000356 DeclRefExprBits.HasTemplateKWAndArgsInfo
357 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000358 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000359 if (TemplateArgs) {
360 bool Dependent = false;
361 bool InstantiationDependent = false;
362 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000363 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
364 Dependent,
365 InstantiationDependent,
366 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000367 if (InstantiationDependent)
368 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000369 } else if (TemplateKWLoc.isValid()) {
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000371 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000372 DeclRefExprBits.HadMultipleCandidates = 0;
373
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000374 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000375}
376
Craig Topper9db7a7e2013-08-22 04:58:56 +0000377DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000378 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000379 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000380 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000381 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000382 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000383 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000384 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000385 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000386 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000387 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000388 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000389 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000390 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000391}
392
Craig Topper9db7a7e2013-08-22 04:58:56 +0000393DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000394 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000395 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000396 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000397 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000398 const DeclarationNameInfo &NameInfo,
399 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000400 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000401 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000402 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000403 // Filter out cases where the found Decl is the same as the value refenenced.
404 if (D == FoundD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700405 FoundD = nullptr;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000406
Douglas Gregora2813ce2009-10-23 18:54:35 +0000407 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7247c882013-05-15 07:37:26 +0000408 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000409 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000410 if (FoundD)
411 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000412 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000413 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
414 else if (TemplateKWLoc.isValid())
415 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000416
Chris Lattner32488542010-10-30 05:14:06 +0000417 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000418 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000419 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000420 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000421}
422
Craig Topper9db7a7e2013-08-22 04:58:56 +0000423DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000424 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000425 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000427 unsigned NumTemplateArgs) {
428 std::size_t Size = sizeof(DeclRefExpr);
429 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000430 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000431 if (HasFoundDecl)
432 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000433 if (HasTemplateKWAndArgsInfo)
434 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000435
Chris Lattner32488542010-10-30 05:14:06 +0000436 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000437 return new (Mem) DeclRefExpr(EmptyShell());
438}
439
Daniel Dunbar396ec672012-03-09 15:39:15 +0000440SourceLocation DeclRefExpr::getLocStart() const {
441 if (hasQualifier())
442 return getQualifierLoc().getBeginLoc();
443 return getNameInfo().getLocStart();
444}
445SourceLocation DeclRefExpr::getLocEnd() const {
446 if (hasExplicitTemplateArgs())
447 return getRAngleLoc();
448 return getNameInfo().getLocEnd();
449}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000450
Anders Carlsson3a082d82009-09-08 18:24:21 +0000451// FIXME: Maybe this should use DeclPrinter with a special "print predefined
452// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000453std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
454 ASTContext &Context = CurrentDecl->getASTContext();
455
David Majnemerbafa74f2013-11-06 23:31:56 +0000456 if (IT == PredefinedExpr::FuncDName) {
457 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700458 std::unique_ptr<MangleContext> MC;
David Majnemerbafa74f2013-11-06 23:31:56 +0000459 MC.reset(Context.createMangleContext());
460
461 if (MC->shouldMangleDeclName(ND)) {
462 SmallString<256> Buffer;
463 llvm::raw_svector_ostream Out(Buffer);
464 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
465 MC->mangleCXXCtor(CD, Ctor_Base, Out);
466 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
467 MC->mangleCXXDtor(DD, Dtor_Base, Out);
468 else
469 MC->mangleName(ND, Out);
470
471 Out.flush();
472 if (!Buffer.empty() && Buffer.front() == '\01')
473 return Buffer.substr(1);
474 return Buffer.str();
475 } else
476 return ND->getIdentifier()->getName();
477 }
478 return "";
479 }
Anders Carlsson3a082d82009-09-08 18:24:21 +0000480 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700481 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000482 return FD->getNameAsString();
483
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000484 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000485 llvm::raw_svector_ostream Out(Name);
486
487 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000488 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000489 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000490 if (MD->isStatic())
491 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000492 }
493
David Blaikie4e4d0842012-03-11 07:00:24 +0000494 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000495 std::string Proto;
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000496 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000497
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000498 const FunctionDecl *Decl = FD;
499 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
500 Decl = Pattern;
501 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700502 const FunctionProtoType *FT = nullptr;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503 if (FD->hasWrittenPrototype())
504 FT = dyn_cast<FunctionProtoType>(AFT);
505
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700506 if (IT == FuncSig) {
507 switch (FT->getCallConv()) {
508 case CC_C: POut << "__cdecl "; break;
509 case CC_X86StdCall: POut << "__stdcall "; break;
510 case CC_X86FastCall: POut << "__fastcall "; break;
511 case CC_X86ThisCall: POut << "__thiscall "; break;
512 // Only bother printing the conventions that MSVC knows about.
513 default: break;
514 }
515 }
516
517 FD->printQualifiedName(POut, Policy);
518
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000519 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000520 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000521 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000522 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000523 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000524 }
525
526 if (FT->isVariadic()) {
527 if (FD->getNumParams()) POut << ", ";
528 POut << "...";
529 }
530 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000531 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000532
Sam Weinig4eadcc52009-12-27 01:38:20 +0000533 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000534 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000535 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000536 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000537 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000538 POut << " volatile";
539 RefQualifierKind Ref = MD->getRefQualifier();
540 if (Ref == RQ_LValue)
541 POut << " &";
542 else if (Ref == RQ_RValue)
543 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000544 }
545
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000546 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
547 SpecsTy Specs;
548 const DeclContext *Ctx = FD->getDeclContext();
549 while (Ctx && isa<NamedDecl>(Ctx)) {
550 const ClassTemplateSpecializationDecl *Spec
551 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
552 if (Spec && !Spec->isExplicitSpecialization())
553 Specs.push_back(Spec);
554 Ctx = Ctx->getParent();
555 }
556
557 std::string TemplateParams;
558 llvm::raw_string_ostream TOut(TemplateParams);
559 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
560 I != E; ++I) {
561 const TemplateParameterList *Params
562 = (*I)->getSpecializedTemplate()->getTemplateParameters();
563 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
564 assert(Params->size() == Args.size());
565 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
566 StringRef Param = Params->getParam(i)->getName();
567 if (Param.empty()) continue;
568 TOut << Param << " = ";
569 Args.get(i).print(Policy, TOut);
570 TOut << ", ";
571 }
572 }
573
574 FunctionTemplateSpecializationInfo *FSI
575 = FD->getTemplateSpecializationInfo();
576 if (FSI && !FSI->isExplicitSpecialization()) {
577 const TemplateParameterList* Params
578 = FSI->getTemplate()->getTemplateParameters();
579 const TemplateArgumentList* Args = FSI->TemplateArguments;
580 assert(Params->size() == Args->size());
581 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
582 StringRef Param = Params->getParam(i)->getName();
583 if (Param.empty()) continue;
584 TOut << Param << " = ";
585 Args->get(i).print(Policy, TOut);
586 TOut << ", ";
587 }
588 }
589
590 TOut.flush();
591 if (!TemplateParams.empty()) {
592 // remove the trailing comma and space
593 TemplateParams.resize(TemplateParams.size() - 2);
594 POut << " [" << TemplateParams << "]";
595 }
596
597 POut.flush();
598
Benjamin Kramer28bdbf02013-08-21 11:45:27 +0000599 // Print "auto" for all deduced return types. This includes C++1y return
600 // type deduction and lambdas. For trailing return types resolve the
601 // decltype expression. Otherwise print the real type when this is
602 // not a constructor or destructor.
603 if ((isa<CXXMethodDecl>(FD) &&
604 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700605 (FT && FT->getReturnType()->getAs<AutoType>()))
Benjamin Kramer28bdbf02013-08-21 11:45:27 +0000606 Proto = "auto " + Proto;
Stephen Hines651f13c2014-04-23 16:59:28 -0700607 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
608 FT->getReturnType()
609 ->getAs<DecltypeType>()
610 ->getUnderlyingType()
Benjamin Kramer28bdbf02013-08-21 11:45:27 +0000611 .getAsStringInternal(Proto, Policy);
612 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Stephen Hines651f13c2014-04-23 16:59:28 -0700613 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000614
615 Out << Proto;
616
617 Out.flush();
618 return Name.str().str();
619 }
Wei Pan15b26742013-08-26 14:27:34 +0000620 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
621 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
622 // Skip to its enclosing function or method, but not its enclosing
623 // CapturedDecl.
624 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
625 const Decl *D = Decl::castFromDeclContext(DC);
626 return ComputeName(IT, D);
627 }
628 llvm_unreachable("CapturedDecl not inside a function or method");
629 }
Anders Carlsson3a082d82009-09-08 18:24:21 +0000630 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000631 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000632 llvm::raw_svector_ostream Out(Name);
633 Out << (MD->isInstanceMethod() ? '-' : '+');
634 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000635
636 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
637 // a null check to avoid a crash.
638 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000639 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000640
Anders Carlsson3a082d82009-09-08 18:24:21 +0000641 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000642 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000643 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000644
Anders Carlsson3a082d82009-09-08 18:24:21 +0000645 Out << ' ';
Stephen Hines651f13c2014-04-23 16:59:28 -0700646 MD->getSelector().print(Out);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000647 Out << ']';
648
649 Out.flush();
650 return Name.str().str();
651 }
652 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
653 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
654 return "top level";
655 }
656 return "";
657}
658
Craig Topper05ed1a02013-08-18 10:09:15 +0000659void APNumericStorage::setIntValue(const ASTContext &C,
660 const llvm::APInt &Val) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000661 if (hasAllocation())
662 C.Deallocate(pVal);
663
664 BitWidth = Val.getBitWidth();
665 unsigned NumWords = Val.getNumWords();
666 const uint64_t* Words = Val.getRawData();
667 if (NumWords > 1) {
668 pVal = new (C) uint64_t[NumWords];
669 std::copy(Words, Words + NumWords, pVal);
670 } else if (NumWords == 1)
671 VAL = Words[0];
672 else
673 VAL = 0;
674}
675
Craig Topper05ed1a02013-08-18 10:09:15 +0000676IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000677 QualType type, SourceLocation l)
678 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
679 false, false),
680 Loc(l) {
681 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
682 assert(V.getBitWidth() == C.getIntWidth(type) &&
683 "Integer type is not the correct size for constant.");
684 setValue(C, V);
685}
686
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000687IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000688IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000689 QualType type, SourceLocation l) {
690 return new (C) IntegerLiteral(C, V, type, l);
691}
692
693IntegerLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000694IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000695 return new (C) IntegerLiteral(Empty);
696}
697
Craig Topper05ed1a02013-08-18 10:09:15 +0000698FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer478851c2012-07-04 17:04:04 +0000699 bool isexact, QualType Type, SourceLocation L)
700 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
701 false, false), Loc(L) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000702 setSemantics(V.getSemantics());
Benjamin Kramer478851c2012-07-04 17:04:04 +0000703 FloatingLiteralBits.IsExact = isexact;
704 setValue(C, V);
705}
706
Craig Topper05ed1a02013-08-18 10:09:15 +0000707FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer478851c2012-07-04 17:04:04 +0000708 : Expr(FloatingLiteralClass, Empty) {
Tim Northover9ec55f22013-01-22 09:46:51 +0000709 setRawSemantics(IEEEhalf);
Benjamin Kramer478851c2012-07-04 17:04:04 +0000710 FloatingLiteralBits.IsExact = false;
711}
712
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000713FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000714FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000715 bool isexact, QualType Type, SourceLocation L) {
716 return new (C) FloatingLiteral(C, V, isexact, Type, L);
717}
718
719FloatingLiteral *
Craig Topper05ed1a02013-08-18 10:09:15 +0000720FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000721 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000722}
723
Tim Northover9ec55f22013-01-22 09:46:51 +0000724const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
725 switch(FloatingLiteralBits.Semantics) {
726 case IEEEhalf:
727 return llvm::APFloat::IEEEhalf;
728 case IEEEsingle:
729 return llvm::APFloat::IEEEsingle;
730 case IEEEdouble:
731 return llvm::APFloat::IEEEdouble;
732 case x87DoubleExtended:
733 return llvm::APFloat::x87DoubleExtended;
734 case IEEEquad:
735 return llvm::APFloat::IEEEquad;
736 case PPCDoubleDouble:
737 return llvm::APFloat::PPCDoubleDouble;
738 }
739 llvm_unreachable("Unrecognised floating semantics");
740}
741
742void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
743 if (&Sem == &llvm::APFloat::IEEEhalf)
744 FloatingLiteralBits.Semantics = IEEEhalf;
745 else if (&Sem == &llvm::APFloat::IEEEsingle)
746 FloatingLiteralBits.Semantics = IEEEsingle;
747 else if (&Sem == &llvm::APFloat::IEEEdouble)
748 FloatingLiteralBits.Semantics = IEEEdouble;
749 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
750 FloatingLiteralBits.Semantics = x87DoubleExtended;
751 else if (&Sem == &llvm::APFloat::IEEEquad)
752 FloatingLiteralBits.Semantics = IEEEquad;
753 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
754 FloatingLiteralBits.Semantics = PPCDoubleDouble;
755 else
756 llvm_unreachable("Unknown floating semantics");
757}
758
Chris Lattnerda8249e2008-06-07 22:13:43 +0000759/// getValueAsApproximateDouble - This returns the value as an inaccurate
760/// double. Note that this may cause loss of precision, but is useful for
761/// debugging dumps, etc.
762double FloatingLiteral::getValueAsApproximateDouble() const {
763 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000764 bool ignored;
765 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
766 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000767 return V.convertToDouble();
768}
769
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000770int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000771 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000772 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000773 case Ascii:
774 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000775 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000776 break;
777 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000778 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000779 break;
780 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000781 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000782 break;
783 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000784 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000785 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000786 }
787 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
788 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000789 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000790 && "character byte widths supported are 1, 2, and 4 only");
791 return CharByteWidth;
792}
793
Craig Topper05ed1a02013-08-18 10:09:15 +0000794StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000795 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000796 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000797 unsigned NumStrs) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700798 assert(C.getAsConstantArrayType(Ty) &&
799 "StringLiteral must be of constant array type!");
800
Chris Lattner2085fd62009-02-18 06:40:38 +0000801 // Allocate enough space for the StringLiteral plus an array of locations for
802 // any concatenated string tokens.
803 void *Mem = C.Allocate(sizeof(StringLiteral)+
804 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000805 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000806 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000809 SL->setString(C,Str,Kind,Pascal);
810
Chris Lattner2085fd62009-02-18 06:40:38 +0000811 SL->TokLocs[0] = Loc[0];
812 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
Chris Lattner726e1682009-02-18 05:49:11 +0000814 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000815 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
816 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000817}
818
Craig Topper05ed1a02013-08-18 10:09:15 +0000819StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
820 unsigned NumStrs) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000821 void *Mem = C.Allocate(sizeof(StringLiteral)+
822 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000823 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000824 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000825 SL->CharByteWidth = 0;
826 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000827 SL->NumConcatenated = NumStrs;
828 return SL;
829}
830
Alexander Kornienkoae541212013-02-01 12:35:51 +0000831void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieu8ab09da2012-06-13 20:25:24 +0000832 switch (getKind()) {
833 case Ascii: break; // no prefix.
834 case Wide: OS << 'L'; break;
835 case UTF8: OS << "u8"; break;
836 case UTF16: OS << 'u'; break;
837 case UTF32: OS << 'U'; break;
838 }
839 OS << '"';
840 static const char Hex[] = "0123456789ABCDEF";
841
842 unsigned LastSlashX = getLength();
843 for (unsigned I = 0, N = getLength(); I != N; ++I) {
844 switch (uint32_t Char = getCodeUnit(I)) {
845 default:
846 // FIXME: Convert UTF-8 back to codepoints before rendering.
847
848 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
849 // Leave invalid surrogates alone; we'll use \x for those.
850 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
851 Char <= 0xdbff) {
852 uint32_t Trail = getCodeUnit(I + 1);
853 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
854 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
855 ++I;
856 }
857 }
858
859 if (Char > 0xff) {
860 // If this is a wide string, output characters over 0xff using \x
861 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
862 // codepoint: use \x escapes for invalid codepoints.
863 if (getKind() == Wide ||
864 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
865 // FIXME: Is this the best way to print wchar_t?
866 OS << "\\x";
867 int Shift = 28;
868 while ((Char >> Shift) == 0)
869 Shift -= 4;
870 for (/**/; Shift >= 0; Shift -= 4)
871 OS << Hex[(Char >> Shift) & 15];
872 LastSlashX = I;
873 break;
874 }
875
876 if (Char > 0xffff)
877 OS << "\\U00"
878 << Hex[(Char >> 20) & 15]
879 << Hex[(Char >> 16) & 15];
880 else
881 OS << "\\u";
882 OS << Hex[(Char >> 12) & 15]
883 << Hex[(Char >> 8) & 15]
884 << Hex[(Char >> 4) & 15]
885 << Hex[(Char >> 0) & 15];
886 break;
887 }
888
889 // If we used \x... for the previous character, and this character is a
890 // hexadecimal digit, prevent it being slurped as part of the \x.
891 if (LastSlashX + 1 == I) {
892 switch (Char) {
893 case '0': case '1': case '2': case '3': case '4':
894 case '5': case '6': case '7': case '8': case '9':
895 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
896 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
897 OS << "\"\"";
898 }
899 }
900
901 assert(Char <= 0xff &&
902 "Characters above 0xff should already have been handled.");
903
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000904 if (isPrintable(Char))
Richard Trieu8ab09da2012-06-13 20:25:24 +0000905 OS << (char)Char;
906 else // Output anything hard as an octal escape.
907 OS << '\\'
908 << (char)('0' + ((Char >> 6) & 7))
909 << (char)('0' + ((Char >> 3) & 7))
910 << (char)('0' + ((Char >> 0) & 7));
911 break;
912 // Handle some common non-printable cases to make dumps prettier.
913 case '\\': OS << "\\\\"; break;
914 case '"': OS << "\\\""; break;
915 case '\n': OS << "\\n"; break;
916 case '\t': OS << "\\t"; break;
917 case '\a': OS << "\\a"; break;
918 case '\b': OS << "\\b"; break;
919 }
920 }
921 OS << '"';
922}
923
Craig Topper05ed1a02013-08-18 10:09:15 +0000924void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedman64f45a22011-11-01 02:23:42 +0000925 StringKind Kind, bool IsPascal) {
926 //FIXME: we assume that the string data comes from a target that uses the same
927 // code unit size and endianess for the type of string.
928 this->Kind = Kind;
929 this->IsPascal = IsPascal;
930
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000931 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000932 assert((Str.size()%CharByteWidth == 0)
933 && "size of data must be multiple of CharByteWidth");
934 Length = Str.size()/CharByteWidth;
935
936 switch(CharByteWidth) {
937 case 1: {
938 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000939 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000940 StrData.asChar = AStrData;
941 break;
942 }
943 case 2: {
944 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000945 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000946 StrData.asUInt16 = AStrData;
947 break;
948 }
949 case 4: {
950 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000951 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000952 StrData.asUInt32 = AStrData;
953 break;
954 }
955 default:
956 assert(false && "unsupported CharByteWidth");
957 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000958}
959
Chris Lattner08f92e32010-11-17 07:37:15 +0000960/// getLocationOfByte - Return a source location that points to the specified
961/// byte of this string literal.
962///
963/// Strings are amazingly complex. They can be formed from multiple tokens and
964/// can have escape sequences in them in addition to the usual trigraph and
965/// escaped newline business. This routine handles this complexity.
966///
967SourceLocation StringLiteral::
968getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
969 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000970 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
971 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000972
Chris Lattner08f92e32010-11-17 07:37:15 +0000973 // Loop over all of the tokens in this string until we find the one that
974 // contains the byte we're looking for.
975 unsigned TokNo = 0;
976 while (1) {
977 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
978 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
979
980 // Get the spelling of the string so that we can get the data that makes up
981 // the string literal, not the identifier for the macro it is potentially
982 // expanded through.
983 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
984
985 // Re-lex the token to get its length and original spelling.
986 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
987 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000988 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000989 if (Invalid)
990 return StrTokSpellingLoc;
991
992 const char *StrData = Buffer.data()+LocInfo.second;
993
Chris Lattner08f92e32010-11-17 07:37:15 +0000994 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000995 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
996 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000997 Token TheTok;
998 TheLexer.LexFromRawLexer(TheTok);
999
1000 // Use the StringLiteralParser to compute the length of the string in bytes.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001001 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattner08f92e32010-11-17 07:37:15 +00001002 unsigned TokNumBytes = SLP.GetStringLength();
1003
1004 // If the byte is in this token, return the location of the byte.
1005 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +00001006 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +00001007 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1008
1009 // Now that we know the offset of the token in the spelling, use the
1010 // preprocessor to get the offset in the original source.
1011 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1012 }
1013
1014 // Move to the next string token.
1015 ++TokNo;
1016 ByteNo -= TokNumBytes;
1017 }
1018}
1019
1020
1021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1023/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +00001024StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001026 case UO_PostInc: return "++";
1027 case UO_PostDec: return "--";
1028 case UO_PreInc: return "++";
1029 case UO_PreDec: return "--";
1030 case UO_AddrOf: return "&";
1031 case UO_Deref: return "*";
1032 case UO_Plus: return "+";
1033 case UO_Minus: return "-";
1034 case UO_Not: return "~";
1035 case UO_LNot: return "!";
1036 case UO_Real: return "__real";
1037 case UO_Imag: return "__imag";
1038 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 }
David Blaikie561d3ab2012-01-17 02:30:50 +00001040 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +00001041}
1042
John McCall2de56d12010-08-25 11:45:40 +00001043UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001044UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1045 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001046 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +00001047 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1048 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1049 case OO_Amp: return UO_AddrOf;
1050 case OO_Star: return UO_Deref;
1051 case OO_Plus: return UO_Plus;
1052 case OO_Minus: return UO_Minus;
1053 case OO_Tilde: return UO_Not;
1054 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001055 }
1056}
1057
1058OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1059 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +00001060 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1061 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1062 case UO_AddrOf: return OO_Amp;
1063 case UO_Deref: return OO_Star;
1064 case UO_Plus: return OO_Plus;
1065 case UO_Minus: return OO_Minus;
1066 case UO_Not: return OO_Tilde;
1067 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +00001068 default: return OO_None;
1069 }
1070}
1071
1072
Reid Spencer5f016e22007-07-11 17:01:13 +00001073//===----------------------------------------------------------------------===//
1074// Postfix Operators.
1075//===----------------------------------------------------------------------===//
1076
Craig Topper05ed1a02013-08-18 10:09:15 +00001077CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1078 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1079 ExprValueKind VK, SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00001080 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001081 fn->isTypeDependent(),
1082 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001083 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001084 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001085 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001087 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001088 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001089 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001090 if (args[i]->isTypeDependent())
1091 ExprBits.TypeDependent = true;
1092 if (args[i]->isValueDependent())
1093 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001094 if (args[i]->isInstantiationDependent())
1095 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001096 if (args[i]->containsUnexpandedParameterPack())
1097 ExprBits.ContainsUnexpandedParameterPack = true;
1098
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001099 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001100 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001101
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001102 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001103 RParenLoc = rparenloc;
1104}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001105
Craig Topper05ed1a02013-08-18 10:09:15 +00001106CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001107 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1108 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001109 fn->isTypeDependent(),
1110 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001111 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001112 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001113 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001114
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001115 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001116 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001117 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001118 if (args[i]->isTypeDependent())
1119 ExprBits.TypeDependent = true;
1120 if (args[i]->isValueDependent())
1121 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001122 if (args[i]->isInstantiationDependent())
1123 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001124 if (args[i]->containsUnexpandedParameterPack())
1125 ExprBits.ContainsUnexpandedParameterPack = true;
1126
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001127 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001128 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001129
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001130 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 RParenLoc = rparenloc;
1132}
1133
Craig Topper05ed1a02013-08-18 10:09:15 +00001134CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001135 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001136 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001137 SubExprs = new (C) Stmt*[PREARGS_START];
1138 CallExprBits.NumPreArgs = 0;
1139}
1140
Craig Topper05ed1a02013-08-18 10:09:15 +00001141CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001142 EmptyShell Empty)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001143 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001144 // FIXME: Why do we allocate this?
1145 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1146 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001147}
1148
Nuno Lopesd20254f2009-12-20 23:11:08 +00001149Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001150 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001151
1152 while (SubstNonTypeTemplateParmExpr *NTTP
1153 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1154 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1155 }
1156
Sebastian Redl20012152010-09-10 20:55:30 +00001157 // If we're calling a dereference, look at the pointer instead.
1158 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1159 if (BO->isPtrMemOp())
1160 CEE = BO->getRHS()->IgnoreParenCasts();
1161 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1162 if (UO->getOpcode() == UO_Deref)
1163 CEE = UO->getSubExpr()->IgnoreParenCasts();
1164 }
Chris Lattner6346f962009-07-17 15:46:27 +00001165 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001166 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001167 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1168 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001169
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001170 return nullptr;
Zhongxing Xua0042542009-07-17 07:29:51 +00001171}
1172
Nuno Lopesd20254f2009-12-20 23:11:08 +00001173FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001174 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001175}
1176
Chris Lattnerd18b3292007-12-28 05:25:02 +00001177/// setNumArgs - This changes the number of arguments present in this call.
1178/// Any orphaned expressions are deleted by this, and any new operands are set
1179/// to null.
Craig Topper05ed1a02013-08-18 10:09:15 +00001180void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001181 // No change, just return.
1182 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Chris Lattnerd18b3292007-12-28 05:25:02 +00001184 // If shrinking # arguments, just delete the extras and forgot them.
1185 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001186 this->NumArgs = NumArgs;
1187 return;
1188 }
1189
1190 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001191 unsigned NumPreArgs = getNumPreArgs();
1192 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001193 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001194 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001195 NewSubExprs[i] = SubExprs[i];
1196 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001197 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1198 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001199 NewSubExprs[i] = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor88c9a462009-04-17 21:46:47 +00001201 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001202 SubExprs = NewSubExprs;
1203 this->NumArgs = NumArgs;
1204}
1205
Stephen Hines651f13c2014-04-23 16:59:28 -07001206/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattnercb888962008-10-06 05:00:53 +00001207/// not, return 0.
Stephen Hines651f13c2014-04-23 16:59:28 -07001208unsigned CallExpr::getBuiltinCallee() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001209 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001210 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001211 // ImplicitCastExpr.
1212 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1213 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001214 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001216 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1217 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001218 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Anders Carlssonbcba2012008-01-31 02:13:57 +00001220 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1221 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001222 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001224 if (!FDecl->getIdentifier())
1225 return 0;
1226
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001227 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001228}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001229
Richard Smithba571832013-01-17 23:46:04 +00001230bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001231 if (unsigned BI = getBuiltinCallee())
Richard Smithba571832013-01-17 23:46:04 +00001232 return Ctx.BuiltinInfo.isUnevaluated(BI);
1233 return false;
1234}
1235
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001236QualType CallExpr::getCallReturnType() const {
1237 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001238 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001239 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001240 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001241 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001242 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1243 // This should never be overloaded and so should never return null.
1244 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001245
John McCall864c0412011-04-26 20:42:42 +00001246 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Stephen Hines651f13c2014-04-23 16:59:28 -07001247 return FnType->getReturnType();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001248}
Chris Lattnercb888962008-10-06 05:00:53 +00001249
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001250SourceLocation CallExpr::getLocStart() const {
1251 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001252 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001253
1254 SourceLocation begin = getCallee()->getLocStart();
1255 if (begin.isInvalid() && getNumArgs() > 0)
1256 begin = getArg(0)->getLocStart();
1257 return begin;
1258}
1259SourceLocation CallExpr::getLocEnd() const {
1260 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001261 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001262
1263 SourceLocation end = getRParenLoc();
1264 if (end.isInvalid() && getNumArgs() > 0)
1265 end = getArg(getNumArgs() - 1)->getLocEnd();
1266 return end;
1267}
John McCall2882eca2011-02-21 06:23:05 +00001268
Craig Topper05ed1a02013-08-18 10:09:15 +00001269OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001270 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001271 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001272 ArrayRef<OffsetOfNode> comps,
1273 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001274 SourceLocation RParenLoc) {
1275 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001276 sizeof(OffsetOfNode) * comps.size() +
1277 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001278
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001279 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1280 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001281}
1282
Craig Topper05ed1a02013-08-18 10:09:15 +00001283OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001284 unsigned numComps, unsigned numExprs) {
1285 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1286 sizeof(OffsetOfNode) * numComps +
1287 sizeof(Expr*) * numExprs);
1288 return new (Mem) OffsetOfExpr(numComps, numExprs);
1289}
1290
Craig Topper05ed1a02013-08-18 10:09:15 +00001291OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001292 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001293 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001294 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001295 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1296 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001297 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001298 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001299 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001300 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001301 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001302{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001303 for (unsigned i = 0; i != comps.size(); ++i) {
1304 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001305 }
Sean Huntc3021132010-05-05 15:23:54 +00001306
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001307 for (unsigned i = 0; i != exprs.size(); ++i) {
1308 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001309 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001310 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001311 ExprBits.ContainsUnexpandedParameterPack = true;
1312
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001313 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001314 }
1315}
1316
1317IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1318 assert(getKind() == Field || getKind() == Identifier);
1319 if (getKind() == Field)
1320 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001321
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001322 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1323}
1324
Craig Topper05ed1a02013-08-18 10:09:15 +00001325MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001326 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001327 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001328 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001329 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001330 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001331 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001332 QualType ty,
1333 ExprValueKind vk,
1334 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001335 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001336
Douglas Gregor40d96a62011-02-28 21:54:11 +00001337 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001338 founddecl.getDecl() != memberdecl ||
1339 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001340 if (hasQualOrFound)
1341 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
John McCalld5532b62009-11-23 01:53:49 +00001343 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001344 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1345 else if (TemplateKWLoc.isValid())
1346 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Chris Lattner32488542010-10-30 05:14:06 +00001348 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001349 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1350 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001351
1352 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001353 // FIXME: Wrong. We should be looking at the member declaration we found.
1354 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001355 E->setValueDependent(true);
1356 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001357 E->setInstantiationDependent(true);
1358 }
1359 else if (QualifierLoc &&
1360 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1361 E->setInstantiationDependent(true);
1362
John McCall6bb80172010-03-30 21:47:33 +00001363 E->HasQualifierOrFoundDecl = true;
1364
1365 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001366 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001367 NQ->FoundDecl = founddecl;
1368 }
1369
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001370 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1371
John McCall6bb80172010-03-30 21:47:33 +00001372 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001373 bool Dependent = false;
1374 bool InstantiationDependent = false;
1375 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001376 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1377 Dependent,
1378 InstantiationDependent,
1379 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001380 if (InstantiationDependent)
1381 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001382 } else if (TemplateKWLoc.isValid()) {
1383 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001384 }
1385
1386 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001387}
1388
Daniel Dunbar396ec672012-03-09 15:39:15 +00001389SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001390 if (isImplicitAccess()) {
1391 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001392 return getQualifierLoc().getBeginLoc();
1393 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001394 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001395
Daniel Dunbar396ec672012-03-09 15:39:15 +00001396 // FIXME: We don't want this to happen. Rather, we should be able to
1397 // detect all kinds of implicit accesses more cleanly.
1398 SourceLocation BaseStartLoc = getBase()->getLocStart();
1399 if (BaseStartLoc.isValid())
1400 return BaseStartLoc;
1401 return MemberLoc;
1402}
1403SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001404 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001405 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001406 EndLoc = getRAngleLoc();
1407 else if (EndLoc.isInvalid())
1408 EndLoc = getBase()->getLocEnd();
1409 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001410}
1411
Stephen Hines651f13c2014-04-23 16:59:28 -07001412bool CastExpr::CastConsistency() const {
John McCall1d9b3b22011-09-09 05:25:32 +00001413 switch (getCastKind()) {
1414 case CK_DerivedToBase:
1415 case CK_UncheckedDerivedToBase:
1416 case CK_DerivedToBaseMemberPointer:
1417 case CK_BaseToDerived:
1418 case CK_BaseToDerivedMemberPointer:
1419 assert(!path_empty() && "Cast kind should have a base path!");
1420 break;
1421
1422 case CK_CPointerToObjCPointerCast:
1423 assert(getType()->isObjCObjectPointerType());
1424 assert(getSubExpr()->getType()->isPointerType());
1425 goto CheckNoBasePath;
1426
1427 case CK_BlockPointerToObjCPointerCast:
1428 assert(getType()->isObjCObjectPointerType());
1429 assert(getSubExpr()->getType()->isBlockPointerType());
1430 goto CheckNoBasePath;
1431
John McCall4d4e5c12012-02-15 01:22:51 +00001432 case CK_ReinterpretMemberPointer:
1433 assert(getType()->isMemberPointerType());
1434 assert(getSubExpr()->getType()->isMemberPointerType());
1435 goto CheckNoBasePath;
1436
John McCall1d9b3b22011-09-09 05:25:32 +00001437 case CK_BitCast:
1438 // Arbitrary casts to C pointer types count as bitcasts.
1439 // Otherwise, we should only have block and ObjC pointer casts
1440 // here if they stay within the type kind.
1441 if (!getType()->isPointerType()) {
1442 assert(getType()->isObjCObjectPointerType() ==
1443 getSubExpr()->getType()->isObjCObjectPointerType());
1444 assert(getType()->isBlockPointerType() ==
1445 getSubExpr()->getType()->isBlockPointerType());
1446 }
1447 goto CheckNoBasePath;
1448
1449 case CK_AnyPointerToBlockPointerCast:
1450 assert(getType()->isBlockPointerType());
1451 assert(getSubExpr()->getType()->isAnyPointerType() &&
1452 !getSubExpr()->getType()->isBlockPointerType());
1453 goto CheckNoBasePath;
1454
Douglas Gregorac1303e2012-02-22 05:02:47 +00001455 case CK_CopyAndAutoreleaseBlockObject:
1456 assert(getType()->isBlockPointerType());
1457 assert(getSubExpr()->getType()->isBlockPointerType());
1458 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001459
1460 case CK_FunctionToPointerDecay:
1461 assert(getType()->isPointerType());
1462 assert(getSubExpr()->getType()->isFunctionType());
1463 goto CheckNoBasePath;
1464
Stephen Hines651f13c2014-04-23 16:59:28 -07001465 case CK_AddressSpaceConversion:
1466 assert(getType()->isPointerType());
1467 assert(getSubExpr()->getType()->isPointerType());
1468 assert(getType()->getPointeeType().getAddressSpace() !=
1469 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall1d9b3b22011-09-09 05:25:32 +00001470 // These should not have an inheritance path.
1471 case CK_Dynamic:
1472 case CK_ToUnion:
1473 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001474 case CK_NullToMemberPointer:
1475 case CK_NullToPointer:
1476 case CK_ConstructorConversion:
1477 case CK_IntegralToPointer:
1478 case CK_PointerToIntegral:
1479 case CK_ToVoid:
1480 case CK_VectorSplat:
1481 case CK_IntegralCast:
1482 case CK_IntegralToFloating:
1483 case CK_FloatingToIntegral:
1484 case CK_FloatingCast:
1485 case CK_ObjCObjectLValueCast:
1486 case CK_FloatingRealToComplex:
1487 case CK_FloatingComplexToReal:
1488 case CK_FloatingComplexCast:
1489 case CK_FloatingComplexToIntegralComplex:
1490 case CK_IntegralRealToComplex:
1491 case CK_IntegralComplexToReal:
1492 case CK_IntegralComplexCast:
1493 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001494 case CK_ARCProduceObject:
1495 case CK_ARCConsumeObject:
1496 case CK_ARCReclaimReturnedObject:
1497 case CK_ARCExtendBlockObject:
Guy Benyeie6b9d802013-01-20 12:31:11 +00001498 case CK_ZeroToOCLEvent:
John McCall1d9b3b22011-09-09 05:25:32 +00001499 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1500 goto CheckNoBasePath;
1501
1502 case CK_Dependent:
1503 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001504 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001505 case CK_AtomicToNonAtomic:
1506 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001507 case CK_PointerToBoolean:
1508 case CK_IntegralToBoolean:
1509 case CK_FloatingToBoolean:
1510 case CK_MemberPointerToBoolean:
1511 case CK_FloatingComplexToBoolean:
1512 case CK_IntegralComplexToBoolean:
1513 case CK_LValueBitCast: // -> bool&
1514 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001515 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001516 CheckNoBasePath:
1517 assert(path_empty() && "Cast kind should not have a base path!");
1518 break;
1519 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001520 return true;
John McCall1d9b3b22011-09-09 05:25:32 +00001521}
1522
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001523const char *CastExpr::getCastKindName() const {
1524 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001525 case CK_Dependent:
1526 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001527 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001528 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001529 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001530 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001531 case CK_LValueToRValue:
1532 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001533 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001534 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001535 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001536 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001537 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001538 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001539 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001540 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001541 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001542 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001543 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001544 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001545 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001546 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001547 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001548 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001549 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001550 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001551 case CK_NullToPointer:
1552 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001553 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001554 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001555 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001556 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001557 case CK_ReinterpretMemberPointer:
1558 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001559 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001560 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001561 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001562 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001563 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001564 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001565 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001566 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001567 case CK_PointerToBoolean:
1568 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001569 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001570 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001571 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001572 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001573 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001574 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001575 case CK_IntegralToBoolean:
1576 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001577 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001578 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001579 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001580 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001581 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001582 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001583 case CK_FloatingToBoolean:
1584 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001585 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001586 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001587 case CK_CPointerToObjCPointerCast:
1588 return "CPointerToObjCPointerCast";
1589 case CK_BlockPointerToObjCPointerCast:
1590 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001591 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001592 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001593 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001594 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001595 case CK_FloatingRealToComplex:
1596 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001597 case CK_FloatingComplexToReal:
1598 return "FloatingComplexToReal";
1599 case CK_FloatingComplexToBoolean:
1600 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001601 case CK_FloatingComplexCast:
1602 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001603 case CK_FloatingComplexToIntegralComplex:
1604 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001605 case CK_IntegralRealToComplex:
1606 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001607 case CK_IntegralComplexToReal:
1608 return "IntegralComplexToReal";
1609 case CK_IntegralComplexToBoolean:
1610 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001611 case CK_IntegralComplexCast:
1612 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001613 case CK_IntegralComplexToFloatingComplex:
1614 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001615 case CK_ARCConsumeObject:
1616 return "ARCConsumeObject";
1617 case CK_ARCProduceObject:
1618 return "ARCProduceObject";
1619 case CK_ARCReclaimReturnedObject:
1620 return "ARCReclaimReturnedObject";
1621 case CK_ARCExtendBlockObject:
Stephen Hines651f13c2014-04-23 16:59:28 -07001622 return "ARCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001623 case CK_AtomicToNonAtomic:
1624 return "AtomicToNonAtomic";
1625 case CK_NonAtomicToAtomic:
1626 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001627 case CK_CopyAndAutoreleaseBlockObject:
1628 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001629 case CK_BuiltinFnToFnPtr:
1630 return "BuiltinFnToFnPtr";
Guy Benyeie6b9d802013-01-20 12:31:11 +00001631 case CK_ZeroToOCLEvent:
1632 return "ZeroToOCLEvent";
Stephen Hines651f13c2014-04-23 16:59:28 -07001633 case CK_AddressSpaceConversion:
1634 return "AddressSpaceConversion";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
John McCall2bb5d002010-11-13 09:02:35 +00001637 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001638}
1639
Douglas Gregor6eef5192009-12-14 19:27:10 +00001640Expr *CastExpr::getSubExprAsWritten() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001641 Expr *SubExpr = nullptr;
Douglas Gregor6eef5192009-12-14 19:27:10 +00001642 CastExpr *E = this;
1643 do {
1644 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001645
1646 // Skip through reference binding to temporary.
1647 if (MaterializeTemporaryExpr *Materialize
1648 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1649 SubExpr = Materialize->GetTemporaryExpr();
1650
Douglas Gregor6eef5192009-12-14 19:27:10 +00001651 // Skip any temporary bindings; they're implicit.
1652 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1653 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001654
Douglas Gregor6eef5192009-12-14 19:27:10 +00001655 // Conversions by constructor and conversion functions have a
1656 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001657 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001658 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001659 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001660 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001661
Douglas Gregor6eef5192009-12-14 19:27:10 +00001662 // If the subexpression we're left with is an implicit cast, look
1663 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001664 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1665
Douglas Gregor6eef5192009-12-14 19:27:10 +00001666 return SubExpr;
1667}
1668
John McCallf871d0c2010-08-07 06:22:56 +00001669CXXBaseSpecifier **CastExpr::path_buffer() {
1670 switch (getStmtClass()) {
1671#define ABSTRACT_STMT(x)
1672#define CASTEXPR(Type, Base) \
1673 case Stmt::Type##Class: \
1674 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1675#define STMT(Type, Base)
1676#include "clang/AST/StmtNodes.inc"
1677 default:
1678 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001679 }
1680}
1681
1682void CastExpr::setCastPath(const CXXCastPath &Path) {
1683 assert(Path.size() == path_size());
1684 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1685}
1686
Craig Topper05ed1a02013-08-18 10:09:15 +00001687ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallf871d0c2010-08-07 06:22:56 +00001688 CastKind Kind, Expr *Operand,
1689 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001690 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001691 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1692 void *Buffer =
1693 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1694 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001695 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001696 if (PathSize) E->setCastPath(*BasePath);
1697 return E;
1698}
1699
Craig Topper05ed1a02013-08-18 10:09:15 +00001700ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-08-07 06:22:56 +00001701 unsigned PathSize) {
1702 void *Buffer =
1703 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1704 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1705}
1706
1707
Craig Topper05ed1a02013-08-18 10:09:15 +00001708CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001709 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001710 const CXXCastPath *BasePath,
1711 TypeSourceInfo *WrittenTy,
1712 SourceLocation L, SourceLocation R) {
1713 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1714 void *Buffer =
1715 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1716 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001717 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001718 if (PathSize) E->setCastPath(*BasePath);
1719 return E;
1720}
1721
Craig Topper05ed1a02013-08-18 10:09:15 +00001722CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1723 unsigned PathSize) {
John McCallf871d0c2010-08-07 06:22:56 +00001724 void *Buffer =
1725 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1726 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1727}
1728
Reid Spencer5f016e22007-07-11 17:01:13 +00001729/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1730/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001731StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001733 case BO_PtrMemD: return ".*";
1734 case BO_PtrMemI: return "->*";
1735 case BO_Mul: return "*";
1736 case BO_Div: return "/";
1737 case BO_Rem: return "%";
1738 case BO_Add: return "+";
1739 case BO_Sub: return "-";
1740 case BO_Shl: return "<<";
1741 case BO_Shr: return ">>";
1742 case BO_LT: return "<";
1743 case BO_GT: return ">";
1744 case BO_LE: return "<=";
1745 case BO_GE: return ">=";
1746 case BO_EQ: return "==";
1747 case BO_NE: return "!=";
1748 case BO_And: return "&";
1749 case BO_Xor: return "^";
1750 case BO_Or: return "|";
1751 case BO_LAnd: return "&&";
1752 case BO_LOr: return "||";
1753 case BO_Assign: return "=";
1754 case BO_MulAssign: return "*=";
1755 case BO_DivAssign: return "/=";
1756 case BO_RemAssign: return "%=";
1757 case BO_AddAssign: return "+=";
1758 case BO_SubAssign: return "-=";
1759 case BO_ShlAssign: return "<<=";
1760 case BO_ShrAssign: return ">>=";
1761 case BO_AndAssign: return "&=";
1762 case BO_XorAssign: return "^=";
1763 case BO_OrAssign: return "|=";
1764 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001766
David Blaikie30263482012-01-20 21:50:17 +00001767 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001768}
1769
John McCall2de56d12010-08-25 11:45:40 +00001770BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001771BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1772 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001773 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001774 case OO_Plus: return BO_Add;
1775 case OO_Minus: return BO_Sub;
1776 case OO_Star: return BO_Mul;
1777 case OO_Slash: return BO_Div;
1778 case OO_Percent: return BO_Rem;
1779 case OO_Caret: return BO_Xor;
1780 case OO_Amp: return BO_And;
1781 case OO_Pipe: return BO_Or;
1782 case OO_Equal: return BO_Assign;
1783 case OO_Less: return BO_LT;
1784 case OO_Greater: return BO_GT;
1785 case OO_PlusEqual: return BO_AddAssign;
1786 case OO_MinusEqual: return BO_SubAssign;
1787 case OO_StarEqual: return BO_MulAssign;
1788 case OO_SlashEqual: return BO_DivAssign;
1789 case OO_PercentEqual: return BO_RemAssign;
1790 case OO_CaretEqual: return BO_XorAssign;
1791 case OO_AmpEqual: return BO_AndAssign;
1792 case OO_PipeEqual: return BO_OrAssign;
1793 case OO_LessLess: return BO_Shl;
1794 case OO_GreaterGreater: return BO_Shr;
1795 case OO_LessLessEqual: return BO_ShlAssign;
1796 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1797 case OO_EqualEqual: return BO_EQ;
1798 case OO_ExclaimEqual: return BO_NE;
1799 case OO_LessEqual: return BO_LE;
1800 case OO_GreaterEqual: return BO_GE;
1801 case OO_AmpAmp: return BO_LAnd;
1802 case OO_PipePipe: return BO_LOr;
1803 case OO_Comma: return BO_Comma;
1804 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001805 }
1806}
1807
1808OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1809 static const OverloadedOperatorKind OverOps[] = {
1810 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1811 OO_Star, OO_Slash, OO_Percent,
1812 OO_Plus, OO_Minus,
1813 OO_LessLess, OO_GreaterGreater,
1814 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1815 OO_EqualEqual, OO_ExclaimEqual,
1816 OO_Amp,
1817 OO_Caret,
1818 OO_Pipe,
1819 OO_AmpAmp,
1820 OO_PipePipe,
1821 OO_Equal, OO_StarEqual,
1822 OO_SlashEqual, OO_PercentEqual,
1823 OO_PlusEqual, OO_MinusEqual,
1824 OO_LessLessEqual, OO_GreaterGreaterEqual,
1825 OO_AmpEqual, OO_CaretEqual,
1826 OO_PipeEqual,
1827 OO_Comma
1828 };
1829 return OverOps[Opc];
1830}
1831
Craig Topper05ed1a02013-08-18 10:09:15 +00001832InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001833 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001834 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001835 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001836 InitExprs(C, initExprs.size()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001837 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001838{
1839 sawArrayRangeDesignator(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001840 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001841 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001842 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001843 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001844 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001845 if (initExprs[I]->isInstantiationDependent())
1846 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001847 if (initExprs[I]->containsUnexpandedParameterPack())
1848 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001849 }
Sean Huntc3021132010-05-05 15:23:54 +00001850
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001851 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001852}
Reid Spencer5f016e22007-07-11 17:01:13 +00001853
Craig Topper05ed1a02013-08-18 10:09:15 +00001854void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001855 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001856 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001857}
1858
Craig Topper05ed1a02013-08-18 10:09:15 +00001859void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001860 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor4c678342009-01-28 21:54:33 +00001861}
1862
Craig Topper05ed1a02013-08-18 10:09:15 +00001863Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001864 if (Init >= InitExprs.size()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001865 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001866 setInit(Init, expr);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001867 return nullptr;
Douglas Gregor4c678342009-01-28 21:54:33 +00001868 }
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor4c678342009-01-28 21:54:33 +00001870 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Stephen Hines651f13c2014-04-23 16:59:28 -07001871 setInit(Init, expr);
Douglas Gregor4c678342009-01-28 21:54:33 +00001872 return Result;
1873}
1874
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001875void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001876 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001877 ArrayFillerOrUnionFieldInit = filler;
1878 // Fill out any "holes" in the array due to designated initializers.
1879 Expr **inits = getInits();
1880 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001881 if (inits[i] == nullptr)
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001882 inits[i] = filler;
1883}
1884
Richard Smithfe587202012-04-15 02:50:59 +00001885bool InitListExpr::isStringLiteralInit() const {
1886 if (getNumInits() != 1)
1887 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001888 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1889 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001890 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001891 // It is possible for getInit() to return null.
1892 const Expr *Init = getInit(0);
1893 if (!Init)
1894 return false;
1895 Init = Init->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001896 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1897}
1898
Erik Verbruggen65d78312012-12-25 14:51:39 +00001899SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001900 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001901 return SyntacticForm->getLocStart();
1902 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001903 if (Beg.isInvalid()) {
1904 // Find the first non-null initializer.
1905 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1906 E = InitExprs.end();
1907 I != E; ++I) {
1908 if (Stmt *S = *I) {
1909 Beg = S->getLocStart();
1910 break;
1911 }
1912 }
1913 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001914 return Beg;
1915}
1916
1917SourceLocation InitListExpr::getLocEnd() const {
1918 if (InitListExpr *SyntacticForm = getSyntacticForm())
1919 return SyntacticForm->getLocEnd();
1920 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001921 if (End.isInvalid()) {
1922 // Find the first non-null initializer from the end.
1923 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001924 E = InitExprs.rend();
1925 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001926 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001927 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001928 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001929 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001930 }
1931 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001932 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001933}
1934
Steve Naroffbfdcae62008-09-04 15:31:07 +00001935/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001936///
John McCalla345edb2012-02-17 03:32:35 +00001937const FunctionProtoType *BlockExpr::getFunctionType() const {
1938 // The block pointer is never sugared, but the function type might be.
1939 return cast<BlockPointerType>(getType())
1940 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001941}
1942
Mike Stump1eb44332009-09-09 15:08:12 +00001943SourceLocation BlockExpr::getCaretLocation() const {
1944 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001945}
Mike Stump1eb44332009-09-09 15:08:12 +00001946const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001947 return TheBlock->getBody();
1948}
Mike Stump1eb44332009-09-09 15:08:12 +00001949Stmt *BlockExpr::getBody() {
1950 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001951}
Steve Naroff56ee6892008-10-08 17:01:13 +00001952
1953
Reid Spencer5f016e22007-07-11 17:01:13 +00001954//===----------------------------------------------------------------------===//
1955// Generic Expression Routines
1956//===----------------------------------------------------------------------===//
1957
Chris Lattner026dc962009-02-14 07:37:35 +00001958/// isUnusedResultAWarning - Return true if this immediate expression should
1959/// be warned about if the result is unused. If so, fill in Loc and Ranges
1960/// with location to warn on and the source range[s] to report with the
1961/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001962bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1963 SourceRange &R1, SourceRange &R2,
1964 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001965 // Don't warn if the expr is type dependent. The type could end up
1966 // instantiating to void.
1967 if (isTypeDependent())
1968 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 switch (getStmtClass()) {
1971 default:
John McCall0faede62010-03-12 07:11:26 +00001972 if (getType()->isVoidType())
1973 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001974 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001975 Loc = getExprLoc();
1976 R1 = getSourceRange();
1977 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001979 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001980 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001981 case GenericSelectionExprClass:
1982 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001983 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmana5e66012013-07-20 00:40:58 +00001984 case ChooseExprClass:
1985 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1986 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 case UnaryOperatorClass: {
1988 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001989
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001991 case UO_Plus:
1992 case UO_Minus:
1993 case UO_AddrOf:
1994 case UO_Not:
1995 case UO_LNot:
1996 case UO_Deref:
1997 break;
John McCall2de56d12010-08-25 11:45:40 +00001998 case UO_PostInc:
1999 case UO_PostDec:
2000 case UO_PreInc:
2001 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00002002 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00002003 case UO_Real:
2004 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00002006 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2007 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00002008 return false;
2009 break;
John McCall2de56d12010-08-25 11:45:40 +00002010 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00002011 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 }
Eli Friedmana6115062012-05-24 00:47:05 +00002013 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002014 Loc = UO->getOperatorLoc();
2015 R1 = UO->getSubExpr()->getSourceRange();
2016 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 }
Chris Lattnere7716e62007-12-01 06:07:34 +00002018 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002019 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00002020 switch (BO->getOpcode()) {
2021 default:
2022 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002023 // Consider the RHS of comma for side effects. LHS was checked by
2024 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00002025 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00002026 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2027 // lvalue-ness) of an assignment written in a macro.
2028 if (IntegerLiteral *IE =
2029 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2030 if (IE->getValue() == 0)
2031 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002032 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002033 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00002034 case BO_LAnd:
2035 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00002036 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2037 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00002038 return false;
2039 break;
John McCallbf0ee352010-02-16 04:10:53 +00002040 }
Chris Lattner026dc962009-02-14 07:37:35 +00002041 if (BO->isAssignmentOp())
2042 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002043 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002044 Loc = BO->getOperatorLoc();
2045 R1 = BO->getLHS()->getSourceRange();
2046 R2 = BO->getRHS()->getSourceRange();
2047 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00002048 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00002049 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00002050 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00002051 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002052 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002053
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002054 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002055 // If only one of the LHS or RHS is a warning, the operator might
2056 // be being used for control flow. Only warn if both the LHS and
2057 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002058 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002059 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00002060 return false;
2061 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00002062 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00002063 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00002064 }
2065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002067 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002068 Loc = cast<MemberExpr>(this)->getMemberLoc();
2069 R1 = SourceRange(Loc, Loc);
2070 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2071 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002074 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002075 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2076 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2077 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2078 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00002079
Chandler Carruth9b106832011-08-17 09:49:44 +00002080 case CXXOperatorCallExprClass: {
Stephen Hines651f13c2014-04-23 16:59:28 -07002081 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth9b106832011-08-17 09:49:44 +00002082 // overloads as there is no reasonable way to define these such that they
2083 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Stephen Hines651f13c2014-04-23 16:59:28 -07002084 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth9b106832011-08-17 09:49:44 +00002085 // provides additional value as well. If this list is updated,
2086 // DiagnoseUnusedComparison should be as well.
2087 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Stephen Hines651f13c2014-04-23 16:59:28 -07002088 switch (Op->getOperator()) {
2089 default:
2090 break;
2091 case OO_EqualEqual:
2092 case OO_ExclaimEqual:
2093 case OO_Less:
2094 case OO_Greater:
2095 case OO_GreaterEqual:
2096 case OO_LessEqual:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002097 if (Op->getCallReturnType()->isReferenceType() ||
2098 Op->getCallReturnType()->isVoidType())
2099 break;
Eli Friedmana6115062012-05-24 00:47:05 +00002100 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002101 Loc = Op->getOperatorLoc();
2102 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00002103 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00002104 }
Chandler Carruth9b106832011-08-17 09:49:44 +00002105
2106 // Fallthrough for generic call handling.
2107 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002109 case CXXMemberCallExprClass:
2110 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002111 // If this is a direct call, get the callee.
2112 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002113 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002114 // If the callee has attribute pure, const, or warn_unused_result, warn
2115 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002116 //
2117 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2118 // updated to match for QoI.
Stephen Hines651f13c2014-04-23 16:59:28 -07002119 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2120 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002121 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002122 Loc = CE->getCallee()->getLocStart();
2123 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002125 if (unsigned NumArgs = CE->getNumArgs())
2126 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2127 CE->getArg(NumArgs-1)->getLocEnd());
2128 return true;
2129 }
Chris Lattner026dc962009-02-14 07:37:35 +00002130 }
2131 return false;
2132 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002133
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002134 // If we don't know precisely what we're looking at, let's not warn.
2135 case UnresolvedLookupExprClass:
2136 case CXXUnresolvedConstructExprClass:
2137 return false;
2138
Anders Carlsson58beed92009-11-17 17:11:23 +00002139 case CXXTemporaryObjectExprClass:
Lubos Lunak81e45492013-07-21 13:15:58 +00002140 case CXXConstructExprClass: {
2141 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2142 if (Type->hasAttr<WarnUnusedAttr>()) {
2143 WarnE = this;
2144 Loc = getLocStart();
2145 R1 = getSourceRange();
2146 return true;
2147 }
2148 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002149 return false;
Lubos Lunak81e45492013-07-21 13:15:58 +00002150 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002151
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002152 case ObjCMessageExprClass: {
2153 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002155 ME->isInstanceMessage() &&
2156 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas4bdb6022013-07-19 20:25:56 +00002157 ME->getMethodFamily() == OMF_init) {
Eli Friedmana6115062012-05-24 00:47:05 +00002158 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002159 Loc = getExprLoc();
2160 R1 = ME->getSourceRange();
2161 return true;
2162 }
2163
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002164 const ObjCMethodDecl *MD = ME->getMethodDecl();
Stephen Hines651f13c2014-04-23 16:59:28 -07002165 if (MD && MD->hasAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002166 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002167 Loc = getExprLoc();
2168 return true;
2169 }
Chris Lattner026dc962009-02-14 07:37:35 +00002170 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002171 }
Mike Stump1eb44332009-09-09 15:08:12 +00002172
John McCall12f78a62010-12-02 01:19:52 +00002173 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002174 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002175 Loc = getExprLoc();
2176 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002177 return true;
John McCall12f78a62010-12-02 01:19:52 +00002178
John McCall4b9c2d22011-11-06 09:01:30 +00002179 case PseudoObjectExprClass: {
2180 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2181
2182 // Only complain about things that have the form of a getter.
2183 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2184 isa<BinaryOperator>(PO->getSyntacticForm()))
2185 return false;
2186
Eli Friedmana6115062012-05-24 00:47:05 +00002187 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002188 Loc = getExprLoc();
2189 R1 = getSourceRange();
2190 return true;
2191 }
2192
Chris Lattner611b2ec2008-07-26 19:51:01 +00002193 case StmtExprClass: {
2194 // Statement exprs don't logically have side effects themselves, but are
2195 // sometimes used in macros in ways that give them a type that is unused.
2196 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2197 // however, if the result of the stmt expr is dead, we don't want to emit a
2198 // warning.
2199 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002200 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002201 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002202 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002203 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2204 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002205 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002206 }
Mike Stump1eb44332009-09-09 15:08:12 +00002207
John McCall0faede62010-03-12 07:11:26 +00002208 if (getType()->isVoidType())
2209 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002210 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002211 Loc = cast<StmtExpr>(this)->getLParenLoc();
2212 R1 = getSourceRange();
2213 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002214 }
Eli Friedman63199172012-09-24 23:02:26 +00002215 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002216 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002217 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002218 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002219 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002220 if (CE->getCastKind() == CK_ToVoid) {
2221 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002222 CE->getSubExpr()->getType().isVolatileQualified()) {
2223 const DeclRefExpr *DRE =
2224 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2225 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2226 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2227 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2228 R1, R2, Ctx);
2229 }
2230 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002231 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002232 }
Eli Friedman4059da82012-05-24 21:05:41 +00002233
Eli Friedmana6115062012-05-24 00:47:05 +00002234 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002235 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002236 if (CE->getCastKind() == CK_ConstructorConversion)
2237 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002238
Eli Friedmana6115062012-05-24 00:47:05 +00002239 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002240 if (const CXXFunctionalCastExpr *CXXCE =
2241 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedmancdd4b782013-08-15 22:02:56 +00002242 Loc = CXXCE->getLocStart();
Eli Friedman4059da82012-05-24 21:05:41 +00002243 R1 = CXXCE->getSubExpr()->getSourceRange();
2244 } else {
2245 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2246 Loc = CStyleCE->getLParenLoc();
2247 R1 = CStyleCE->getSubExpr()->getSourceRange();
2248 }
Chris Lattner026dc962009-02-14 07:37:35 +00002249 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002250 }
Eli Friedmana6115062012-05-24 00:47:05 +00002251 case ImplicitCastExprClass: {
2252 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002253
Eli Friedmana6115062012-05-24 00:47:05 +00002254 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2255 if (ICE->getCastKind() == CK_LValueToRValue &&
2256 ICE->getSubExpr()->getType().isVolatileQualified())
2257 return false;
2258
2259 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2260 }
Chris Lattner04421082008-04-08 04:40:51 +00002261 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002262 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002263 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smithc3bf52c2013-04-20 22:23:05 +00002264 case CXXDefaultInitExprClass:
2265 return (cast<CXXDefaultInitExpr>(this)
2266 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002267
2268 case CXXNewExprClass:
2269 // FIXME: In theory, there might be new expressions that don't have side
2270 // effects (e.g. a placement new with an uninitialized POD).
2271 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002272 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002273 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002274 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002275 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002276 case ExprWithCleanupsClass:
2277 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002278 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002279 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002280}
2281
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002282/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002283/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002284bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002285 const Expr *E = IgnoreParens();
2286 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002287 default:
2288 return false;
2289 case ObjCIvarRefExprClass:
2290 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002291 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002292 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002293 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002294 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002295 case MaterializeTemporaryExprClass:
2296 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2297 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002298 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002299 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002300 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002301 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002302
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002303 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2304 if (VD->hasGlobalStorage())
2305 return true;
2306 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002307 // dereferencing to a pointer is always a gc'able candidate,
2308 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002309 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002310 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002311 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002312 return false;
2313 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002314 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002315 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002316 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002317 }
2318 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002319 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002320 }
2321}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002322
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002323bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2324 if (isTypeDependent())
2325 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002326 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002327}
2328
John McCall864c0412011-04-26 20:42:42 +00002329QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002330 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002331
2332 // Bound member expressions are always one of these possibilities:
2333 // x->m x.m x->*y x.*y
2334 // (possibly parenthesized)
2335
2336 expr = expr->IgnoreParens();
2337 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2338 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2339 return mem->getMemberDecl()->getType();
2340 }
2341
2342 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2343 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2344 ->getPointeeType();
2345 assert(type->isFunctionType());
2346 return type;
2347 }
2348
2349 assert(isa<UnresolvedMemberExpr>(expr));
2350 return QualType();
2351}
2352
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002353Expr* Expr::IgnoreParens() {
2354 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002355 while (true) {
2356 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2357 E = P->getSubExpr();
2358 continue;
2359 }
2360 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2361 if (P->getOpcode() == UO_Extension) {
2362 E = P->getSubExpr();
2363 continue;
2364 }
2365 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002366 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2367 if (!P->isResultDependent()) {
2368 E = P->getResultExpr();
2369 continue;
2370 }
2371 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002372 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2373 if (!P->isConditionDependent()) {
2374 E = P->getChosenSubExpr();
2375 continue;
2376 }
2377 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002378 return E;
2379 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002380}
2381
Chris Lattner56f34942008-02-13 01:02:39 +00002382/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2383/// or CastExprs or ImplicitCastExprs, returning their operand.
2384Expr *Expr::IgnoreParenCasts() {
2385 Expr *E = this;
2386 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002387 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002388 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002389 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002390 continue;
2391 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002392 if (MaterializeTemporaryExpr *Materialize
2393 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2394 E = Materialize->GetTemporaryExpr();
2395 continue;
2396 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002397 if (SubstNonTypeTemplateParmExpr *NTTP
2398 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2399 E = NTTP->getReplacement();
2400 continue;
2401 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002402 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002403 }
2404}
2405
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002406Expr *Expr::IgnoreCasts() {
2407 Expr *E = this;
2408 while (true) {
2409 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2410 E = P->getSubExpr();
2411 continue;
2412 }
2413 if (MaterializeTemporaryExpr *Materialize
2414 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2415 E = Materialize->GetTemporaryExpr();
2416 continue;
2417 }
2418 if (SubstNonTypeTemplateParmExpr *NTTP
2419 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2420 E = NTTP->getReplacement();
2421 continue;
2422 }
2423 return E;
2424 }
2425}
2426
John McCall9c5d70c2010-12-04 08:24:19 +00002427/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2428/// casts. This is intended purely as a temporary workaround for code
2429/// that hasn't yet been rewritten to do the right thing about those
2430/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002431Expr *Expr::IgnoreParenLValueCasts() {
2432 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002433 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002434 E = E->IgnoreParens();
2435 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002436 if (P->getCastKind() == CK_LValueToRValue) {
2437 E = P->getSubExpr();
2438 continue;
2439 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002440 } else if (MaterializeTemporaryExpr *Materialize
2441 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2442 E = Materialize->GetTemporaryExpr();
2443 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002444 } else if (SubstNonTypeTemplateParmExpr *NTTP
2445 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2446 E = NTTP->getReplacement();
2447 continue;
John McCallf6a16482010-12-04 03:47:34 +00002448 }
2449 break;
2450 }
2451 return E;
2452}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002453
2454Expr *Expr::ignoreParenBaseCasts() {
2455 Expr *E = this;
2456 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002457 E = E->IgnoreParens();
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002458 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2459 if (CE->getCastKind() == CK_DerivedToBase ||
2460 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2461 CE->getCastKind() == CK_NoOp) {
2462 E = CE->getSubExpr();
2463 continue;
2464 }
2465 }
2466
2467 return E;
2468 }
2469}
2470
John McCall2fc46bf2010-05-05 22:59:52 +00002471Expr *Expr::IgnoreParenImpCasts() {
2472 Expr *E = this;
2473 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002474 E = E->IgnoreParens();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002475 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002476 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002477 continue;
2478 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002479 if (MaterializeTemporaryExpr *Materialize
2480 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2481 E = Materialize->GetTemporaryExpr();
2482 continue;
2483 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002484 if (SubstNonTypeTemplateParmExpr *NTTP
2485 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2486 E = NTTP->getReplacement();
2487 continue;
2488 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002489 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002490 }
2491}
2492
Hans Wennborg2f072b42011-06-09 17:06:51 +00002493Expr *Expr::IgnoreConversionOperator() {
2494 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002495 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002496 return MCE->getImplicitObjectArgument();
2497 }
2498 return this;
2499}
2500
Chris Lattnerecdd8412009-03-13 17:28:01 +00002501/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2502/// value (including ptr->int casts of the same size). Strip off any
2503/// ParenExpr or CastExprs, returning their operand.
2504Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2505 Expr *E = this;
2506 while (true) {
Eli Friedmana5e66012013-07-20 00:40:58 +00002507 E = E->IgnoreParens();
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Chris Lattnerecdd8412009-03-13 17:28:01 +00002509 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2510 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002511 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002512 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattnerecdd8412009-03-13 17:28:01 +00002514 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2515 E = SE;
2516 continue;
2517 }
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002519 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002520 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002521 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002522 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002523 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2524 E = SE;
2525 continue;
2526 }
2527 }
Mike Stump1eb44332009-09-09 15:08:12 +00002528
Douglas Gregorc0244c52011-09-08 17:56:33 +00002529 if (SubstNonTypeTemplateParmExpr *NTTP
2530 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2531 E = NTTP->getReplacement();
2532 continue;
2533 }
2534
Chris Lattnerecdd8412009-03-13 17:28:01 +00002535 return E;
2536 }
2537}
2538
Douglas Gregor6eef5192009-12-14 19:27:10 +00002539bool Expr::isDefaultArgument() const {
2540 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002541 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2542 E = M->GetTemporaryExpr();
2543
Douglas Gregor6eef5192009-12-14 19:27:10 +00002544 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2545 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002546
Douglas Gregor6eef5192009-12-14 19:27:10 +00002547 return isa<CXXDefaultArgExpr>(E);
2548}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002549
Douglas Gregor2f599792010-04-02 18:24:57 +00002550/// \brief Skip over any no-op casts and any temporary-binding
2551/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002552static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002553 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2554 E = M->GetTemporaryExpr();
2555
Douglas Gregor2f599792010-04-02 18:24:57 +00002556 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002557 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002558 E = ICE->getSubExpr();
2559 else
2560 break;
2561 }
2562
2563 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2564 E = BE->getSubExpr();
2565
2566 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002567 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002568 E = ICE->getSubExpr();
2569 else
2570 break;
2571 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002572
2573 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002574}
2575
John McCall558d2ab2010-09-15 10:14:12 +00002576/// isTemporaryObject - Determines if this expression produces a
2577/// temporary of the given class type.
2578bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2579 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2580 return false;
2581
Anders Carlssonf8b30152010-11-28 16:40:49 +00002582 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002583
John McCall58277b52010-09-15 20:59:13 +00002584 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002585 if (!E->Classify(C).isPRValue()) {
2586 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002587 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002588 return false;
2589 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002590
John McCall19e60ad2010-09-16 06:57:56 +00002591 // Black-list a few cases which yield pr-values of class type that don't
2592 // refer to temporaries of that type:
2593
2594 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002595 if (isa<ImplicitCastExpr>(E)) {
2596 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2597 case CK_DerivedToBase:
2598 case CK_UncheckedDerivedToBase:
2599 return false;
2600 default:
2601 break;
2602 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002603 }
2604
John McCall19e60ad2010-09-16 06:57:56 +00002605 // - member expressions (all)
2606 if (isa<MemberExpr>(E))
2607 return false;
2608
Eli Friedman32f498a2012-06-15 23:51:06 +00002609 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2610 if (BO->isPtrMemOp())
2611 return false;
2612
John McCall56ca35d2011-02-17 10:25:35 +00002613 // - opaque values (all)
2614 if (isa<OpaqueValueExpr>(E))
2615 return false;
2616
John McCall558d2ab2010-09-15 10:14:12 +00002617 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002618}
2619
Douglas Gregor75e85042011-03-02 21:06:53 +00002620bool Expr::isImplicitCXXThis() const {
2621 const Expr *E = this;
2622
2623 // Strip away parentheses and casts we don't care about.
2624 while (true) {
2625 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2626 E = Paren->getSubExpr();
2627 continue;
2628 }
2629
2630 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2631 if (ICE->getCastKind() == CK_NoOp ||
2632 ICE->getCastKind() == CK_LValueToRValue ||
2633 ICE->getCastKind() == CK_DerivedToBase ||
2634 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2635 E = ICE->getSubExpr();
2636 continue;
2637 }
2638 }
2639
2640 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2641 if (UnOp->getOpcode() == UO_Extension) {
2642 E = UnOp->getSubExpr();
2643 continue;
2644 }
2645 }
2646
Douglas Gregor03e80032011-06-21 17:03:29 +00002647 if (const MaterializeTemporaryExpr *M
2648 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2649 E = M->GetTemporaryExpr();
2650 continue;
2651 }
2652
Douglas Gregor75e85042011-03-02 21:06:53 +00002653 break;
2654 }
2655
2656 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2657 return This->isImplicit();
2658
2659 return false;
2660}
2661
Douglas Gregor898574e2008-12-05 23:32:09 +00002662/// hasAnyTypeDependentArguments - Determines if any of the expressions
2663/// in Exprs is type-dependent.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002664bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00002665 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002666 if (Exprs[I]->isTypeDependent())
2667 return true;
2668
2669 return false;
2670}
2671
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002672bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2673 const Expr **Culprit) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002674 // This function is attempting whether an expression is an initializer
Eli Friedman21cde052013-07-16 22:40:53 +00002675 // which can be evaluated at compile-time. It very closely parallels
2676 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2677 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2678 // to isEvaluatable most of the time.
2679 //
John McCall4204f072010-08-02 21:13:48 +00002680 // If we ever capture reference-binding directly in the AST, we can
2681 // kill the second parameter.
2682
2683 if (IsForRef) {
2684 EvalResult Result;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002685 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2686 return true;
2687 if (Culprit)
2688 *Culprit = this;
2689 return false;
John McCall4204f072010-08-02 21:13:48 +00002690 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002691
Anders Carlssone8a32b82008-11-24 05:23:59 +00002692 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002693 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002694 case StringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002695 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002696 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002697 case CXXTemporaryObjectExprClass:
2698 case CXXConstructExprClass: {
2699 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002700
Eli Friedman21cde052013-07-16 22:40:53 +00002701 if (CE->getConstructor()->isTrivial() &&
2702 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2703 // Trivial default constructor
Richard Smith180f4792011-11-10 06:34:14 +00002704 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002705
Eli Friedman21cde052013-07-16 22:40:53 +00002706 // Trivial copy constructor
2707 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002708 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smith180f4792011-11-10 06:34:14 +00002709 }
2710
Richard Smith180f4792011-11-10 06:34:14 +00002711 break;
John McCallb4b9b152010-08-01 21:51:45 +00002712 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002713 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002714 // This handles gcc's extension that allows global initializers like
2715 // "struct x {int x;} x = (struct x) {};".
2716 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002717 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002718 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman59b5da62009-01-18 03:20:47 +00002719 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002720 case InitListExprClass: {
Eli Friedman21cde052013-07-16 22:40:53 +00002721 const InitListExpr *ILE = cast<InitListExpr>(this);
2722 if (ILE->getType()->isArrayType()) {
2723 unsigned numInits = ILE->getNumInits();
2724 for (unsigned i = 0; i < numInits; i++) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002725 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman21cde052013-07-16 22:40:53 +00002726 return false;
2727 }
2728 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002729 }
Eli Friedman21cde052013-07-16 22:40:53 +00002730
2731 if (ILE->getType()->isRecordType()) {
2732 unsigned ElementNo = 0;
2733 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2734 for (RecordDecl::field_iterator Field = RD->field_begin(),
2735 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2736 // If this is a union, skip all the fields that aren't being initialized.
2737 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2738 continue;
2739
2740 // Don't emit anonymous bitfields, they just affect layout.
2741 if (Field->isUnnamedBitfield())
2742 continue;
2743
2744 if (ElementNo < ILE->getNumInits()) {
2745 const Expr *Elt = ILE->getInit(ElementNo++);
2746 if (Field->isBitField()) {
2747 // Bitfields have to evaluate to an integer.
2748 llvm::APSInt ResultTmp;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002749 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2750 if (Culprit)
2751 *Culprit = Elt;
Eli Friedman21cde052013-07-16 22:40:53 +00002752 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002753 }
Eli Friedman21cde052013-07-16 22:40:53 +00002754 } else {
2755 bool RefType = Field->getType()->isReferenceType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002756 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman21cde052013-07-16 22:40:53 +00002757 return false;
2758 }
2759 }
2760 }
2761 return true;
2762 }
2763
2764 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002765 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002766 case ImplicitValueInitExprClass:
2767 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002768 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002769 return cast<ParenExpr>(this)->getSubExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002770 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbournef111d932011-04-15 00:35:48 +00002771 case GenericSelectionExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002772 return cast<GenericSelectionExpr>(this)->getResultExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002773 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002774 case ChooseExprClass:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002775 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2776 if (Culprit)
2777 *Culprit = this;
Eli Friedmana5e66012013-07-20 00:40:58 +00002778 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002779 }
Eli Friedmana5e66012013-07-20 00:40:58 +00002780 return cast<ChooseExpr>(this)->getChosenSubExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002781 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002782 case UnaryOperatorClass: {
2783 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002784 if (Exp->getOpcode() == UO_Extension)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002785 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002786 break;
2787 }
John McCall4204f072010-08-02 21:13:48 +00002788 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002789 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002790 case ImplicitCastExprClass:
Eli Friedman21cde052013-07-16 22:40:53 +00002791 case CStyleCastExprClass:
2792 case ObjCBridgedCastExprClass:
2793 case CXXDynamicCastExprClass:
2794 case CXXReinterpretCastExprClass:
2795 case CXXConstCastExprClass: {
Richard Smithd62ca372011-12-06 22:44:34 +00002796 const CastExpr *CE = cast<CastExpr>(this);
2797
Eli Friedman6bd97192011-12-21 00:43:02 +00002798 // Handle misc casts we want to ignore.
Eli Friedman6bd97192011-12-21 00:43:02 +00002799 if (CE->getCastKind() == CK_NoOp ||
2800 CE->getCastKind() == CK_LValueToRValue ||
2801 CE->getCastKind() == CK_ToUnion ||
Eli Friedman21cde052013-07-16 22:40:53 +00002802 CE->getCastKind() == CK_ConstructorConversion ||
2803 CE->getCastKind() == CK_NonAtomicToAtomic ||
2804 CE->getCastKind() == CK_AtomicToNonAtomic)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002805 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62ca372011-12-06 22:44:34 +00002806
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002807 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002808 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002809 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002810 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002811 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman21cde052013-07-16 22:40:53 +00002812
2813 case SubstNonTypeTemplateParmExprClass:
2814 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002815 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman21cde052013-07-16 22:40:53 +00002816 case CXXDefaultArgExprClass:
2817 return cast<CXXDefaultArgExpr>(this)->getExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002818 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman21cde052013-07-16 22:40:53 +00002819 case CXXDefaultInitExprClass:
2820 return cast<CXXDefaultInitExpr>(this)->getExpr()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002821 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002822 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002823 if (isEvaluatable(Ctx))
2824 return true;
2825 if (Culprit)
2826 *Culprit = this;
2827 return false;
Steve Naroff38374b02007-09-02 20:30:18 +00002828}
2829
Richard Smith8ae4ec22012-08-07 04:16:51 +00002830bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2831 if (isInstantiationDependent())
2832 return true;
2833
2834 switch (getStmtClass()) {
2835 case NoStmtClass:
2836 #define ABSTRACT_STMT(Type)
2837 #define STMT(Type, Base) case Type##Class:
2838 #define EXPR(Type, Base)
2839 #include "clang/AST/StmtNodes.inc"
2840 llvm_unreachable("unexpected Expr kind");
2841
2842 case DependentScopeDeclRefExprClass:
2843 case CXXUnresolvedConstructExprClass:
2844 case CXXDependentScopeMemberExprClass:
2845 case UnresolvedLookupExprClass:
2846 case UnresolvedMemberExprClass:
2847 case PackExpansionExprClass:
2848 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002849 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002850 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2851
Richard Smith60b70382012-08-07 05:18:29 +00002852 case DeclRefExprClass:
2853 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002854 case PredefinedExprClass:
2855 case IntegerLiteralClass:
2856 case FloatingLiteralClass:
2857 case ImaginaryLiteralClass:
2858 case StringLiteralClass:
2859 case CharacterLiteralClass:
2860 case OffsetOfExprClass:
2861 case ImplicitValueInitExprClass:
2862 case UnaryExprOrTypeTraitExprClass:
2863 case AddrLabelExprClass:
2864 case GNUNullExprClass:
2865 case CXXBoolLiteralExprClass:
2866 case CXXNullPtrLiteralExprClass:
2867 case CXXThisExprClass:
2868 case CXXScalarValueInitExprClass:
2869 case TypeTraitExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002870 case ArrayTypeTraitExprClass:
2871 case ExpressionTraitExprClass:
2872 case CXXNoexceptExprClass:
2873 case SizeOfPackExprClass:
2874 case ObjCStringLiteralClass:
2875 case ObjCEncodeExprClass:
2876 case ObjCBoolLiteralExprClass:
2877 case CXXUuidofExprClass:
2878 case OpaqueValueExprClass:
2879 // These never have a side-effect.
2880 return false;
2881
2882 case CallExprClass:
John McCall76da55d2013-04-16 07:28:30 +00002883 case MSPropertyRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002884 case CompoundAssignOperatorClass:
2885 case VAArgExprClass:
2886 case AtomicExprClass:
2887 case StmtExprClass:
2888 case CXXOperatorCallExprClass:
2889 case CXXMemberCallExprClass:
2890 case UserDefinedLiteralClass:
2891 case CXXThrowExprClass:
2892 case CXXNewExprClass:
2893 case CXXDeleteExprClass:
2894 case ExprWithCleanupsClass:
2895 case CXXBindTemporaryExprClass:
2896 case BlockExprClass:
2897 case CUDAKernelCallExprClass:
2898 // These always have a side-effect.
2899 return true;
2900
2901 case ParenExprClass:
2902 case ArraySubscriptExprClass:
2903 case MemberExprClass:
2904 case ConditionalOperatorClass:
2905 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002906 case CompoundLiteralExprClass:
2907 case ExtVectorElementExprClass:
2908 case DesignatedInitExprClass:
2909 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002910 case CXXPseudoDestructorExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +00002911 case CXXStdInitializerListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002912 case SubstNonTypeTemplateParmExprClass:
2913 case MaterializeTemporaryExprClass:
2914 case ShuffleVectorExprClass:
Hal Finkel414a1bd2013-09-18 03:29:45 +00002915 case ConvertVectorExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002916 case AsTypeExprClass:
2917 // These have a side-effect if any subexpression does.
2918 break;
2919
Richard Smith60b70382012-08-07 05:18:29 +00002920 case UnaryOperatorClass:
2921 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002922 return true;
2923 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002924
2925 case BinaryOperatorClass:
2926 if (cast<BinaryOperator>(this)->isAssignmentOp())
2927 return true;
2928 break;
2929
Richard Smith8ae4ec22012-08-07 04:16:51 +00002930 case InitListExprClass:
2931 // FIXME: The children for an InitListExpr doesn't include the array filler.
2932 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2933 if (E->HasSideEffects(Ctx))
2934 return true;
2935 break;
2936
2937 case GenericSelectionExprClass:
2938 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2939 HasSideEffects(Ctx);
2940
2941 case ChooseExprClass:
Eli Friedmana5e66012013-07-20 00:40:58 +00002942 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith8ae4ec22012-08-07 04:16:51 +00002943
2944 case CXXDefaultArgExprClass:
2945 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2946
Richard Smithc3bf52c2013-04-20 22:23:05 +00002947 case CXXDefaultInitExprClass:
2948 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2949 return E->HasSideEffects(Ctx);
2950 // If we've not yet parsed the initializer, assume it has side-effects.
2951 return true;
2952
Richard Smith8ae4ec22012-08-07 04:16:51 +00002953 case CXXDynamicCastExprClass: {
2954 // A dynamic_cast expression has side-effects if it can throw.
2955 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2956 if (DCE->getTypeAsWritten()->isReferenceType() &&
2957 DCE->getCastKind() == CK_Dynamic)
2958 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002959 } // Fall through.
2960 case ImplicitCastExprClass:
2961 case CStyleCastExprClass:
2962 case CXXStaticCastExprClass:
2963 case CXXReinterpretCastExprClass:
2964 case CXXConstCastExprClass:
2965 case CXXFunctionalCastExprClass: {
2966 const CastExpr *CE = cast<CastExpr>(this);
2967 if (CE->getCastKind() == CK_LValueToRValue &&
2968 CE->getSubExpr()->getType().isVolatileQualified())
2969 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002970 break;
2971 }
2972
Richard Smith0d729102012-08-13 20:08:14 +00002973 case CXXTypeidExprClass:
2974 // typeid might throw if its subexpression is potentially-evaluated, so has
2975 // side-effects in that case whether or not its subexpression does.
2976 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002977
2978 case CXXConstructExprClass:
2979 case CXXTemporaryObjectExprClass: {
2980 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002981 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002982 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002983 // A trivial constructor does not add any side-effects of its own. Just look
2984 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002985 break;
2986 }
2987
2988 case LambdaExprClass: {
2989 const LambdaExpr *LE = cast<LambdaExpr>(this);
2990 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2991 E = LE->capture_end(); I != E; ++I)
2992 if (I->getCaptureKind() == LCK_ByCopy)
2993 // FIXME: Only has a side-effect if the variable is volatile or if
2994 // the copy would invoke a non-trivial copy constructor.
2995 return true;
2996 return false;
2997 }
2998
2999 case PseudoObjectExprClass: {
3000 // Only look for side-effects in the semantic form, and look past
3001 // OpaqueValueExpr bindings in that form.
3002 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3003 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3004 E = PO->semantics_end();
3005 I != E; ++I) {
3006 const Expr *Subexpr = *I;
3007 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3008 Subexpr = OVE->getSourceExpr();
3009 if (Subexpr->HasSideEffects(Ctx))
3010 return true;
3011 }
3012 return false;
3013 }
3014
3015 case ObjCBoxedExprClass:
3016 case ObjCArrayLiteralClass:
3017 case ObjCDictionaryLiteralClass:
3018 case ObjCMessageExprClass:
3019 case ObjCSelectorExprClass:
3020 case ObjCProtocolExprClass:
3021 case ObjCPropertyRefExprClass:
3022 case ObjCIsaExprClass:
3023 case ObjCIndirectCopyRestoreExprClass:
3024 case ObjCSubscriptRefExprClass:
3025 case ObjCBridgedCastExprClass:
3026 // FIXME: Classify these cases better.
3027 return true;
3028 }
3029
3030 // Recurse to children.
3031 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3032 if (const Stmt *S = *SubStmts)
3033 if (cast<Expr>(S)->HasSideEffects(Ctx))
3034 return true;
3035
3036 return false;
3037}
3038
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00003039namespace {
3040 /// \brief Look for a call to a non-trivial function within an expression.
3041 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3042 {
3043 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3044
3045 bool NonTrivial;
3046
3047 public:
3048 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00003049 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00003050
3051 bool hasNonTrivialCall() const { return NonTrivial; }
3052
3053 void VisitCallExpr(CallExpr *E) {
3054 if (CXXMethodDecl *Method
3055 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3056 if (Method->isTrivial()) {
3057 // Recurse to children of the call.
3058 Inherited::VisitStmt(E);
3059 return;
3060 }
3061 }
3062
3063 NonTrivial = true;
3064 }
3065
3066 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3067 if (E->getConstructor()->isTrivial()) {
3068 // Recurse to children of the call.
3069 Inherited::VisitStmt(E);
3070 return;
3071 }
3072
3073 NonTrivial = true;
3074 }
3075
3076 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3077 if (E->getTemporary()->getDestructor()->isTrivial()) {
3078 Inherited::VisitStmt(E);
3079 return;
3080 }
3081
3082 NonTrivial = true;
3083 }
3084 };
3085}
3086
3087bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3088 NonTrivialCallFinder Finder(Ctx);
3089 Finder.Visit(this);
3090 return Finder.hasNonTrivialCall();
3091}
3092
Chandler Carruth82214a82011-02-18 23:54:50 +00003093/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3094/// pointer constant or not, as well as the specific kind of constant detected.
3095/// Null pointer constants can be integer constant expressions with the
3096/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3097/// (a GNU extension).
3098Expr::NullPointerConstantKind
3099Expr::isNullPointerConstant(ASTContext &Ctx,
3100 NullPointerConstantValueDependence NPC) const {
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003101 if (isValueDependent() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07003102 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregorce940492009-09-25 04:25:58 +00003103 switch (NPC) {
3104 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00003105 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00003106 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003107 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00003108 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00003109 else
3110 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00003111
Douglas Gregorce940492009-09-25 04:25:58 +00003112 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00003113 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00003114 }
3115 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00003116
Sebastian Redl07779722008-10-31 14:43:28 +00003117 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00003118 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003119 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00003120 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00003121 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00003122 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00003123 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00003124 Pointee->isVoidType() && // to void*
3125 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003126 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003127 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003129 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3130 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003131 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003132 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3133 // Accept ((void*)0) as a null pointer constant, as many other
3134 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003135 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003136 } else if (const GenericSelectionExpr *GE =
3137 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedmana5e66012013-07-20 00:40:58 +00003138 if (GE->isResultDependent())
3139 return NPCK_NotNull;
Peter Collingbournef111d932011-04-15 00:35:48 +00003140 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedmana5e66012013-07-20 00:40:58 +00003141 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3142 if (CE->isConditionDependent())
3143 return NPCK_NotNull;
3144 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003145 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003146 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smithc3bf52c2013-04-20 22:23:05 +00003147 // See through default argument expressions.
Douglas Gregorce940492009-09-25 04:25:58 +00003148 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smithc3bf52c2013-04-20 22:23:05 +00003149 } else if (const CXXDefaultInitExpr *DefaultInit
3150 = dyn_cast<CXXDefaultInitExpr>(this)) {
3151 // See through default initializer expressions.
3152 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003153 } else if (isa<GNUNullExpr>(this)) {
3154 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003155 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003156 } else if (const MaterializeTemporaryExpr *M
3157 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3158 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003159 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3160 if (const Expr *Source = OVE->getSourceExpr())
3161 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003162 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003163
Richard Smith4e24f0f2013-01-02 12:01:23 +00003164 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003165 if (getType()->isNullPtrType())
Richard Smith4e24f0f2013-01-02 12:01:23 +00003166 return NPCK_CXX11_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003167
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003168 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smithf050d242013-06-13 02:46:14 +00003169 if (!Ctx.getLangOpts().CPlusPlus11 &&
3170 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003171 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3172 const Expr *InitExpr = CLE->getInitializer();
3173 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3174 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3175 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003176 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003177 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003178 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003179 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Richard Smith80ad52f2013-01-02 11:42:31 +00003181 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smithf050d242013-06-13 02:46:14 +00003182 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3183 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003184 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smithf050d242013-06-13 02:46:14 +00003185 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003186 if (Lit && !Lit->getValue())
3187 return NPCK_ZeroLiteral;
Stephen Hines651f13c2014-04-23 16:59:28 -07003188 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknere0ac9bf2013-11-12 02:22:34 +00003189 return NPCK_NotNull;
Richard Smith70488e22012-02-14 21:38:30 +00003190 } else {
Richard Smithf050d242013-06-13 02:46:14 +00003191 // If we have an integer constant expression, we need to *evaluate* it and
3192 // test for the value 0.
Richard Smith70488e22012-02-14 21:38:30 +00003193 if (!isIntegerConstantExpr(Ctx))
3194 return NPCK_NotNull;
3195 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003196
David Blaikie50800fc2012-08-08 17:33:31 +00003197 if (EvaluateKnownConstInt(Ctx) != 0)
3198 return NPCK_NotNull;
3199
3200 if (isa<IntegerLiteral>(this))
3201 return NPCK_ZeroLiteral;
3202 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003203}
Steve Naroff31a45842007-07-28 23:10:27 +00003204
John McCallf6a16482010-12-04 03:47:34 +00003205/// \brief If this expression is an l-value for an Objective C
3206/// property, find the underlying property reference expression.
3207const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3208 const Expr *E = this;
3209 while (true) {
3210 assert((E->getValueKind() == VK_LValue &&
3211 E->getObjectKind() == OK_ObjCProperty) &&
3212 "expression is not a property reference");
3213 E = E->IgnoreParenCasts();
3214 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3215 if (BO->getOpcode() == BO_Comma) {
3216 E = BO->getRHS();
3217 continue;
3218 }
3219 }
3220
3221 break;
3222 }
3223
3224 return cast<ObjCPropertyRefExpr>(E);
3225}
3226
Anna Zaksbbff82f2012-10-01 20:34:04 +00003227bool Expr::isObjCSelfExpr() const {
3228 const Expr *E = IgnoreParenImpCasts();
3229
3230 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3231 if (!DRE)
3232 return false;
3233
3234 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3235 if (!Param)
3236 return false;
3237
3238 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3239 if (!M)
3240 return false;
3241
3242 return M->getSelfDecl() == Param;
3243}
3244
John McCall993f43f2013-05-06 21:39:12 +00003245FieldDecl *Expr::getSourceBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003246 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003247
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003248 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003249 if (ICE->getCastKind() == CK_LValueToRValue ||
3250 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003251 E = ICE->getSubExpr()->IgnoreParens();
3252 else
3253 break;
3254 }
3255
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003256 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003257 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003258 if (Field->isBitField())
3259 return Field;
3260
John McCall993f43f2013-05-06 21:39:12 +00003261 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3262 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3263 if (Ivar->isBitField())
3264 return Ivar;
3265
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003266 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3267 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3268 if (Field->isBitField())
3269 return Field;
3270
Eli Friedman42068e92011-07-13 02:05:57 +00003271 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003272 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCall993f43f2013-05-06 21:39:12 +00003273 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003274
Eli Friedman42068e92011-07-13 02:05:57 +00003275 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCall993f43f2013-05-06 21:39:12 +00003276 return BinOp->getRHS()->getSourceBitField();
Eli Friedman42068e92011-07-13 02:05:57 +00003277 }
3278
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003279 return nullptr;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003280}
3281
Anders Carlsson09380262010-01-31 17:18:49 +00003282bool Expr::refersToVectorElement() const {
3283 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003284
Anders Carlsson09380262010-01-31 17:18:49 +00003285 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003286 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003287 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003288 E = ICE->getSubExpr()->IgnoreParens();
3289 else
3290 break;
3291 }
Sean Huntc3021132010-05-05 15:23:54 +00003292
Anders Carlsson09380262010-01-31 17:18:49 +00003293 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3294 return ASE->getBase()->getType()->isVectorType();
3295
3296 if (isa<ExtVectorElementExpr>(E))
3297 return true;
3298
3299 return false;
3300}
3301
Chris Lattner2140e902009-02-16 22:14:05 +00003302/// isArrow - Return true if the base expression is a pointer to vector,
3303/// return false if the base expression is a vector.
3304bool ExtVectorElementExpr::isArrow() const {
3305 return getBase()->getType()->isPointerType();
3306}
3307
Nate Begeman213541a2008-04-18 23:10:10 +00003308unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003309 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003310 return VT->getNumElements();
3311 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003312}
3313
Nate Begeman8a997642008-05-09 06:41:27 +00003314/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003315bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003316 // FIXME: Refactor this code to an accessor on the AST node which returns the
3317 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003318 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003319
3320 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003321 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003322 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003323
Nate Begeman190d6a22009-01-18 02:01:21 +00003324 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003325 if (Comp[0] == 's' || Comp[0] == 'S')
3326 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003327
Daniel Dunbar15027422009-10-17 23:53:04 +00003328 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003329 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003330 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003331
Steve Narofffec0b492007-07-30 03:29:09 +00003332 return false;
3333}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003334
Nate Begeman8a997642008-05-09 06:41:27 +00003335/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003336void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003337 SmallVectorImpl<unsigned> &Elts) const {
3338 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003339 if (Comp[0] == 's' || Comp[0] == 'S')
3340 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003341
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003342 bool isHi = Comp == "hi";
3343 bool isLo = Comp == "lo";
3344 bool isEven = Comp == "even";
3345 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003346
Nate Begeman8a997642008-05-09 06:41:27 +00003347 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3348 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Nate Begeman8a997642008-05-09 06:41:27 +00003350 if (isHi)
3351 Index = e + i;
3352 else if (isLo)
3353 Index = i;
3354 else if (isEven)
3355 Index = 2 * i;
3356 else if (isOdd)
3357 Index = 2 * i + 1;
3358 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003359 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003360
Nate Begeman3b8d1162008-05-13 21:03:02 +00003361 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003362 }
Nate Begeman8a997642008-05-09 06:41:27 +00003363}
3364
Douglas Gregor04badcf2010-04-21 00:45:42 +00003365ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003366 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003367 SourceLocation LBracLoc,
3368 SourceLocation SuperLoc,
3369 bool IsInstanceSuper,
3370 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003371 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003372 ArrayRef<SourceLocation> SelLocs,
3373 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003374 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003375 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003376 SourceLocation RBracLoc,
3377 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003378 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003379 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003380 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003381 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003382 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3383 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003384 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003385 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3386 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3387 RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003388{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003389 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003390 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003391}
3392
Douglas Gregor04badcf2010-04-21 00:45:42 +00003393ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003394 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003395 SourceLocation LBracLoc,
3396 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003397 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003398 ArrayRef<SourceLocation> SelLocs,
3399 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003400 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003401 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003402 SourceLocation RBracLoc,
3403 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003404 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003405 T->isDependentType(), T->isInstantiationDependentType(),
3406 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003407 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3408 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003409 Kind(Class),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003410 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3411 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003412{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003413 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003414 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003415}
3416
Douglas Gregor04badcf2010-04-21 00:45:42 +00003417ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003418 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003419 SourceLocation LBracLoc,
3420 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003421 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003422 ArrayRef<SourceLocation> SelLocs,
3423 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003424 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003425 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003426 SourceLocation RBracLoc,
3427 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003428 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003429 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003430 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003431 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003432 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3433 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003434 Kind(Instance),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003435 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3436 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003437{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003438 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003439 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003440}
3441
3442void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3443 ArrayRef<SourceLocation> SelLocs,
3444 SelectorLocationsKind SelLocsK) {
3445 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003446 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003447 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003448 if (Args[I]->isTypeDependent())
3449 ExprBits.TypeDependent = true;
3450 if (Args[I]->isValueDependent())
3451 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003452 if (Args[I]->isInstantiationDependent())
3453 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003454 if (Args[I]->containsUnexpandedParameterPack())
3455 ExprBits.ContainsUnexpandedParameterPack = true;
3456
3457 MyArgs[I] = Args[I];
3458 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003459
Benjamin Kramer19562c92012-02-20 00:20:48 +00003460 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003461 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003462 if (SelLocsK == SelLoc_NonStandard)
3463 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3464 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003465}
3466
Craig Topper9db7a7e2013-08-22 04:58:56 +00003467ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003468 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003469 SourceLocation LBracLoc,
3470 SourceLocation SuperLoc,
3471 bool IsInstanceSuper,
3472 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003473 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003474 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003475 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003476 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003477 SourceLocation RBracLoc,
3478 bool isImplicit) {
3479 assert((!SelLocs.empty() || isImplicit) &&
3480 "No selector locs for non-implicit message");
3481 ObjCMessageExpr *Mem;
3482 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3483 if (isImplicit)
3484 Mem = alloc(Context, Args.size(), 0);
3485 else
3486 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003487 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003488 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003489 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003490}
3491
Craig Topper9db7a7e2013-08-22 04:58:56 +00003492ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003493 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003494 SourceLocation LBracLoc,
3495 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003496 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003497 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003498 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003499 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003500 SourceLocation RBracLoc,
3501 bool isImplicit) {
3502 assert((!SelLocs.empty() || isImplicit) &&
3503 "No selector locs for non-implicit message");
3504 ObjCMessageExpr *Mem;
3505 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3506 if (isImplicit)
3507 Mem = alloc(Context, Args.size(), 0);
3508 else
3509 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003510 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003511 SelLocs, SelLocsK, Method, Args, RBracLoc,
3512 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003513}
3514
Craig Topper9db7a7e2013-08-22 04:58:56 +00003515ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003516 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003517 SourceLocation LBracLoc,
3518 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003519 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003520 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003521 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003522 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003523 SourceLocation RBracLoc,
3524 bool isImplicit) {
3525 assert((!SelLocs.empty() || isImplicit) &&
3526 "No selector locs for non-implicit message");
3527 ObjCMessageExpr *Mem;
3528 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3529 if (isImplicit)
3530 Mem = alloc(Context, Args.size(), 0);
3531 else
3532 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003533 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003534 SelLocs, SelLocsK, Method, Args, RBracLoc,
3535 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003536}
3537
Craig Topper9db7a7e2013-08-22 04:58:56 +00003538ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003539 unsigned NumArgs,
3540 unsigned NumStoredSelLocs) {
3541 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003542 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3543}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003544
Craig Topper9db7a7e2013-08-22 04:58:56 +00003545ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003546 ArrayRef<Expr *> Args,
3547 SourceLocation RBraceLoc,
3548 ArrayRef<SourceLocation> SelLocs,
3549 Selector Sel,
3550 SelectorLocationsKind &SelLocsK) {
3551 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3552 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3553 : 0;
3554 return alloc(C, Args.size(), NumStoredSelLocs);
3555}
3556
Craig Topper9db7a7e2013-08-22 04:58:56 +00003557ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003558 unsigned NumArgs,
3559 unsigned NumStoredSelLocs) {
3560 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3561 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3562 return (ObjCMessageExpr *)C.Allocate(Size,
3563 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3564}
3565
3566void ObjCMessageExpr::getSelectorLocs(
3567 SmallVectorImpl<SourceLocation> &SelLocs) const {
3568 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3569 SelLocs.push_back(getSelectorLoc(i));
3570}
3571
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003572SourceRange ObjCMessageExpr::getReceiverRange() const {
3573 switch (getReceiverKind()) {
3574 case Instance:
3575 return getInstanceReceiver()->getSourceRange();
3576
3577 case Class:
3578 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3579
3580 case SuperInstance:
3581 case SuperClass:
3582 return getSuperLoc();
3583 }
3584
David Blaikie30263482012-01-20 21:50:17 +00003585 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003586}
3587
Douglas Gregor04badcf2010-04-21 00:45:42 +00003588Selector ObjCMessageExpr::getSelector() const {
3589 if (HasMethod)
3590 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3591 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003592 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003593}
3594
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003595QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003596 switch (getReceiverKind()) {
3597 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003598 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003599 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003600 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003601 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003602 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003603 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003604 }
3605
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003606 llvm_unreachable("unexpected receiver kind");
3607}
3608
3609ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3610 QualType T = getReceiverType();
3611
3612 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3613 return Ptr->getInterfaceDecl();
3614
3615 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3616 return Ty->getInterface();
3617
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003618 return nullptr;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003619}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003620
Chris Lattner5f9e2722011-07-23 10:55:15 +00003621StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003622 switch (getBridgeKind()) {
3623 case OBC_Bridge:
3624 return "__bridge";
3625 case OBC_BridgeTransfer:
3626 return "__bridge_transfer";
3627 case OBC_BridgeRetained:
3628 return "__bridge_retained";
3629 }
David Blaikie30263482012-01-20 21:50:17 +00003630
3631 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003632}
3633
Craig Topper05ed1a02013-08-18 10:09:15 +00003634ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003635 QualType Type, SourceLocation BLoc,
3636 SourceLocation RP)
3637 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3638 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003639 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003640 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003641 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003642{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003643 SubExprs = new (C) Stmt*[args.size()];
3644 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003645 if (args[i]->isTypeDependent())
3646 ExprBits.TypeDependent = true;
3647 if (args[i]->isValueDependent())
3648 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003649 if (args[i]->isInstantiationDependent())
3650 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003651 if (args[i]->containsUnexpandedParameterPack())
3652 ExprBits.ContainsUnexpandedParameterPack = true;
3653
3654 SubExprs[i] = args[i];
3655 }
3656}
3657
Craig Topper05ed1a02013-08-18 10:09:15 +00003658void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman888376a2009-08-12 02:28:50 +00003659 if (SubExprs) C.Deallocate(SubExprs);
3660
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003661 this->NumExprs = Exprs.size();
Dmitri Gribenko2ad77cd2013-05-10 17:30:13 +00003662 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko27365ee2013-05-10 00:43:44 +00003663 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00003664}
Nate Begeman888376a2009-08-12 02:28:50 +00003665
Craig Topper05ed1a02013-08-18 10:09:15 +00003666GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003667 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003668 ArrayRef<TypeSourceInfo*> AssocTypes,
3669 ArrayRef<Expr*> AssocExprs,
3670 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003671 SourceLocation RParenLoc,
3672 bool ContainsUnexpandedParameterPack,
3673 unsigned ResultIndex)
3674 : Expr(GenericSelectionExprClass,
3675 AssocExprs[ResultIndex]->getType(),
3676 AssocExprs[ResultIndex]->getValueKind(),
3677 AssocExprs[ResultIndex]->getObjectKind(),
3678 AssocExprs[ResultIndex]->isTypeDependent(),
3679 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003680 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003681 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003682 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3683 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3684 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3685 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003686 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003687 assert(AssocTypes.size() == AssocExprs.size());
3688 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3689 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003690}
3691
Craig Topper05ed1a02013-08-18 10:09:15 +00003692GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbournef111d932011-04-15 00:35:48 +00003693 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003694 ArrayRef<TypeSourceInfo*> AssocTypes,
3695 ArrayRef<Expr*> AssocExprs,
3696 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003697 SourceLocation RParenLoc,
3698 bool ContainsUnexpandedParameterPack)
3699 : Expr(GenericSelectionExprClass,
3700 Context.DependentTy,
3701 VK_RValue,
3702 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003703 /*isTypeDependent=*/true,
3704 /*isValueDependent=*/true,
3705 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003706 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003707 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3708 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3709 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3710 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003711 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003712 assert(AssocTypes.size() == AssocExprs.size());
3713 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3714 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003715}
3716
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003717//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003718// DesignatedInitExpr
3719//===----------------------------------------------------------------------===//
3720
Chandler Carruthb1138242011-06-16 06:47:06 +00003721IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003722 assert(Kind == FieldDesignator && "Only valid on a field designator");
3723 if (Field.NameOrField & 0x01)
3724 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3725 else
3726 return getField()->getIdentifier();
3727}
3728
Craig Topper05ed1a02013-08-18 10:09:15 +00003729DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003730 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003731 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003732 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003733 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003734 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003735 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003736 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003737 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003738 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003739 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003740 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003741 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003742 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003743 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003744
3745 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003746 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003747 *Child++ = Init;
3748
3749 // Copy the designators and their subexpressions, computing
3750 // value-dependence along the way.
3751 unsigned IndexIdx = 0;
3752 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003753 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003754
3755 if (this->Designators[I].isArrayDesignator()) {
3756 // Compute type- and value-dependence.
3757 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003758 if (Index->isTypeDependent() || Index->isValueDependent())
3759 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003760 if (Index->isInstantiationDependent())
3761 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003762 // Propagate unexpanded parameter packs.
3763 if (Index->containsUnexpandedParameterPack())
3764 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003765
3766 // Copy the index expressions into permanent storage.
3767 *Child++ = IndexExprs[IndexIdx++];
3768 } else if (this->Designators[I].isArrayRangeDesignator()) {
3769 // Compute type- and value-dependence.
3770 Expr *Start = IndexExprs[IndexIdx];
3771 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003772 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003773 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003774 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003775 ExprBits.InstantiationDependent = true;
3776 } else if (Start->isInstantiationDependent() ||
3777 End->isInstantiationDependent()) {
3778 ExprBits.InstantiationDependent = true;
3779 }
3780
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003781 // Propagate unexpanded parameter packs.
3782 if (Start->containsUnexpandedParameterPack() ||
3783 End->containsUnexpandedParameterPack())
3784 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003785
3786 // Copy the start/end expressions into permanent storage.
3787 *Child++ = IndexExprs[IndexIdx++];
3788 *Child++ = IndexExprs[IndexIdx++];
3789 }
3790 }
3791
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003792 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003793}
3794
Douglas Gregor05c13a32009-01-22 00:58:24 +00003795DesignatedInitExpr *
Craig Topper05ed1a02013-08-18 10:09:15 +00003796DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003797 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003798 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003799 SourceLocation ColonOrEqualLoc,
3800 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003801 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003802 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003803 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003804 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003805 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003806}
3807
Craig Topper05ed1a02013-08-18 10:09:15 +00003808DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003809 unsigned NumIndexExprs) {
3810 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3811 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3812 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3813}
3814
Craig Topper05ed1a02013-08-18 10:09:15 +00003815void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003816 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003817 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003818 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003819 NumDesignators = NumDesigs;
3820 for (unsigned I = 0; I != NumDesigs; ++I)
3821 Designators[I] = Desigs[I];
3822}
3823
Abramo Bagnara24f46742011-03-16 15:08:46 +00003824SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3825 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3826 if (size() == 1)
3827 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003828 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3829 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003830}
3831
Erik Verbruggen65d78312012-12-25 14:51:39 +00003832SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003833 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003834 Designator &First =
3835 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003836 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003837 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003838 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3839 else
3840 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3841 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003842 StartLoc =
3843 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003844 return StartLoc;
3845}
3846
3847SourceLocation DesignatedInitExpr::getLocEnd() const {
3848 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003849}
3850
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003851Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003852 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Stephen Hines651f13c2014-04-23 16:59:28 -07003853 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003854 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3855}
3856
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003857Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003858 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003859 "Requires array range designator");
Stephen Hines651f13c2014-04-23 16:59:28 -07003860 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003861 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3862}
3863
Dmitri Gribenkod615f882013-01-26 15:15:52 +00003864Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003865 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003866 "Requires array range designator");
Stephen Hines651f13c2014-04-23 16:59:28 -07003867 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003868 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3869}
3870
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003871/// \brief Replaces the designator at index @p Idx with the series
3872/// of designators in [First, Last).
Craig Topper05ed1a02013-08-18 10:09:15 +00003873void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003874 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003875 const Designator *Last) {
3876 unsigned NumNewDesignators = Last - First;
3877 if (NumNewDesignators == 0) {
3878 std::copy_backward(Designators + Idx + 1,
3879 Designators + NumDesignators,
3880 Designators + Idx);
3881 --NumNewDesignators;
3882 return;
3883 } else if (NumNewDesignators == 1) {
3884 Designators[Idx] = *First;
3885 return;
3886 }
3887
Mike Stump1eb44332009-09-09 15:08:12 +00003888 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003889 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003890 std::copy(Designators, Designators + Idx, NewDesignators);
3891 std::copy(First, Last, NewDesignators + Idx);
3892 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3893 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003894 Designators = NewDesignators;
3895 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3896}
3897
Craig Topper05ed1a02013-08-18 10:09:15 +00003898ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003899 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003900 SourceLocation rparenloc)
3901 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003902 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003903 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3904 Exprs = new (C) Stmt*[exprs.size()];
3905 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003906 if (exprs[i]->isTypeDependent())
3907 ExprBits.TypeDependent = true;
3908 if (exprs[i]->isValueDependent())
3909 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003910 if (exprs[i]->isInstantiationDependent())
3911 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003912 if (exprs[i]->containsUnexpandedParameterPack())
3913 ExprBits.ContainsUnexpandedParameterPack = true;
3914
Nate Begeman2ef13e52009-08-10 23:49:36 +00003915 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003916 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003917}
3918
John McCalle996ffd2011-02-16 08:02:54 +00003919const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3920 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3921 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003922 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3923 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003924 e = cast<CXXConstructExpr>(e)->getArg(0);
3925 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3926 e = ice->getSubExpr();
3927 return cast<OpaqueValueExpr>(e);
3928}
3929
Craig Topper05ed1a02013-08-18 10:09:15 +00003930PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3931 EmptyShell sh,
John McCall4b9c2d22011-11-06 09:01:30 +00003932 unsigned numSemanticExprs) {
3933 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3934 (1 + numSemanticExprs) * sizeof(Expr*),
3935 llvm::alignOf<PseudoObjectExpr>());
3936 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3937}
3938
3939PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3940 : Expr(PseudoObjectExprClass, shell) {
3941 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3942}
3943
Craig Topper05ed1a02013-08-18 10:09:15 +00003944PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCall4b9c2d22011-11-06 09:01:30 +00003945 ArrayRef<Expr*> semantics,
3946 unsigned resultIndex) {
3947 assert(syntax && "no syntactic expression!");
3948 assert(semantics.size() && "no semantic expressions!");
3949
3950 QualType type;
3951 ExprValueKind VK;
3952 if (resultIndex == NoResult) {
3953 type = C.VoidTy;
3954 VK = VK_RValue;
3955 } else {
3956 assert(resultIndex < semantics.size());
3957 type = semantics[resultIndex]->getType();
3958 VK = semantics[resultIndex]->getValueKind();
3959 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3960 }
3961
3962 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3963 (1 + semantics.size()) * sizeof(Expr*),
3964 llvm::alignOf<PseudoObjectExpr>());
3965 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3966 resultIndex);
3967}
3968
3969PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3970 Expr *syntax, ArrayRef<Expr*> semantics,
3971 unsigned resultIndex)
3972 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3973 /*filled in at end of ctor*/ false, false, false, false) {
3974 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3975 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3976
3977 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3978 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3979 getSubExprsBuffer()[i] = E;
3980
3981 if (E->isTypeDependent())
3982 ExprBits.TypeDependent = true;
3983 if (E->isValueDependent())
3984 ExprBits.ValueDependent = true;
3985 if (E->isInstantiationDependent())
3986 ExprBits.InstantiationDependent = true;
3987 if (E->containsUnexpandedParameterPack())
3988 ExprBits.ContainsUnexpandedParameterPack = true;
3989
3990 if (isa<OpaqueValueExpr>(E))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003991 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCall4b9c2d22011-11-06 09:01:30 +00003992 "opaque-value semantic expressions for pseudo-object "
3993 "operations must have sources");
3994 }
3995}
3996
Douglas Gregor05c13a32009-01-22 00:58:24 +00003997//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003998// ExprIterator.
3999//===----------------------------------------------------------------------===//
4000
4001Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
4002Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
4003Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
4004const Expr* ConstExprIterator::operator[](size_t idx) const {
4005 return cast<Expr>(I[idx]);
4006}
4007const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
4008const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4009
4010//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00004011// Child Iterators for iterating over subexpressions/substatements
4012//===----------------------------------------------------------------------===//
4013
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004014// UnaryExprOrTypeTraitExpr
4015Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00004016 // If this is of a type and the type is a VLA type (and not a typedef), the
4017 // size expression of the VLA needs to be treated as an executable expression.
4018 // Why isn't this weirdness documented better in StmtIterator?
4019 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00004020 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00004021 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00004022 return child_range(child_iterator(T), child_iterator());
4023 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00004024 }
John McCall63c00d72011-02-09 08:16:59 +00004025 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00004026}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004027
Steve Naroff563477d2007-09-18 23:55:05 +00004028// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00004029Stmt::child_range ObjCMessageExpr::children() {
4030 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00004031 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00004032 begin = reinterpret_cast<Stmt **>(this + 1);
4033 else
4034 begin = reinterpret_cast<Stmt **>(getArgs());
4035 return child_range(begin,
4036 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00004037}
4038
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004039ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004040 QualType T, ObjCMethodDecl *Method,
4041 SourceRange SR)
4042 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4043 false, false, false, false),
4044 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
4045{
4046 Expr **SaveElements = getElements();
4047 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4048 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4049 ExprBits.ValueDependent = true;
4050 if (Elements[I]->isInstantiationDependent())
4051 ExprBits.InstantiationDependent = true;
4052 if (Elements[I]->containsUnexpandedParameterPack())
4053 ExprBits.ContainsUnexpandedParameterPack = true;
4054
4055 SaveElements[I] = Elements[I];
4056 }
4057}
4058
Craig Topper9db7a7e2013-08-22 04:58:56 +00004059ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004060 ArrayRef<Expr *> Elements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004061 QualType T, ObjCMethodDecl * Method,
4062 SourceRange SR) {
4063 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4064 + Elements.size() * sizeof(Expr *));
4065 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4066}
4067
Craig Topper9db7a7e2013-08-22 04:58:56 +00004068ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004069 unsigned NumElements) {
4070
4071 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4072 + NumElements * sizeof(Expr *));
4073 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4074}
4075
4076ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4077 ArrayRef<ObjCDictionaryElement> VK,
4078 bool HasPackExpansions,
4079 QualType T, ObjCMethodDecl *method,
4080 SourceRange SR)
4081 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4082 false, false),
4083 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4084 DictWithObjectsMethod(method)
4085{
4086 KeyValuePair *KeyValues = getKeyValues();
4087 ExpansionData *Expansions = getExpansionData();
4088 for (unsigned I = 0; I < NumElements; I++) {
4089 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4090 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4091 ExprBits.ValueDependent = true;
4092 if (VK[I].Key->isInstantiationDependent() ||
4093 VK[I].Value->isInstantiationDependent())
4094 ExprBits.InstantiationDependent = true;
4095 if (VK[I].EllipsisLoc.isInvalid() &&
4096 (VK[I].Key->containsUnexpandedParameterPack() ||
4097 VK[I].Value->containsUnexpandedParameterPack()))
4098 ExprBits.ContainsUnexpandedParameterPack = true;
4099
4100 KeyValues[I].Key = VK[I].Key;
4101 KeyValues[I].Value = VK[I].Value;
4102 if (Expansions) {
4103 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4104 if (VK[I].NumExpansions)
4105 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4106 else
4107 Expansions[I].NumExpansionsPlusOne = 0;
4108 }
4109 }
4110}
4111
4112ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004113ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004114 ArrayRef<ObjCDictionaryElement> VK,
4115 bool HasPackExpansions,
4116 QualType T, ObjCMethodDecl *method,
4117 SourceRange SR) {
4118 unsigned ExpansionsSize = 0;
4119 if (HasPackExpansions)
4120 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4121
4122 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4123 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4124 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4125}
4126
4127ObjCDictionaryLiteral *
Craig Topper9db7a7e2013-08-22 04:58:56 +00004128ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004129 bool HasPackExpansions) {
4130 unsigned ExpansionsSize = 0;
4131 if (HasPackExpansions)
4132 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4133 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4134 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4135 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4136 HasPackExpansions);
4137}
4138
Craig Topper9db7a7e2013-08-22 04:58:56 +00004139ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004140 Expr *base,
4141 Expr *key, QualType T,
4142 ObjCMethodDecl *getMethod,
4143 ObjCMethodDecl *setMethod,
4144 SourceLocation RB) {
4145 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4146 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4147 OK_ObjCSubscript,
4148 getMethod, setMethod, RB);
4149}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004150
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004151AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004152 QualType t, AtomicOp op, SourceLocation RP)
4153 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4154 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004155 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004156{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004157 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4158 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004159 if (args[i]->isTypeDependent())
4160 ExprBits.TypeDependent = true;
4161 if (args[i]->isValueDependent())
4162 ExprBits.ValueDependent = true;
4163 if (args[i]->isInstantiationDependent())
4164 ExprBits.InstantiationDependent = true;
4165 if (args[i]->containsUnexpandedParameterPack())
4166 ExprBits.ContainsUnexpandedParameterPack = true;
4167
4168 SubExprs[i] = args[i];
4169 }
4170}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004171
4172unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4173 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004174 case AO__c11_atomic_init:
4175 case AO__c11_atomic_load:
4176 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004177 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004178
4179 case AO__c11_atomic_store:
4180 case AO__c11_atomic_exchange:
4181 case AO__atomic_load:
4182 case AO__atomic_store:
4183 case AO__atomic_store_n:
4184 case AO__atomic_exchange_n:
4185 case AO__c11_atomic_fetch_add:
4186 case AO__c11_atomic_fetch_sub:
4187 case AO__c11_atomic_fetch_and:
4188 case AO__c11_atomic_fetch_or:
4189 case AO__c11_atomic_fetch_xor:
4190 case AO__atomic_fetch_add:
4191 case AO__atomic_fetch_sub:
4192 case AO__atomic_fetch_and:
4193 case AO__atomic_fetch_or:
4194 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004195 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004196 case AO__atomic_add_fetch:
4197 case AO__atomic_sub_fetch:
4198 case AO__atomic_and_fetch:
4199 case AO__atomic_or_fetch:
4200 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004201 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004202 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004203
4204 case AO__atomic_exchange:
4205 return 4;
4206
4207 case AO__c11_atomic_compare_exchange_strong:
4208 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004209 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004210
4211 case AO__atomic_compare_exchange:
4212 case AO__atomic_compare_exchange_n:
4213 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004214 }
4215 llvm_unreachable("unknown atomic op");
4216}