blob: 8410bad88d9b4583144b8bbebc2f35d017891726 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner86ee2862008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor1be329d2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbed356a2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000036#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000037using namespace clang;
38
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola60a2bba2012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
Craig Topper36250ad2014-05-12 05:36:57 +000047 return nullptr;
Rafael Espindola60a2bba2012-07-17 20:24:05 +000048
Rafael Espindola49e860b2012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smithf3fabd22013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola9c006de2012-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 Smith6b6f8aa2013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smith2d187902013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola9c006de2012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-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 Smithf3fabd22013-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 Espindola9c006de2012-10-27 01:03:43 +000099 }
100 }
101
102 // Nothing changed.
103 break;
104 }
105 return E;
106}
107
Chris Lattner4ebae652010-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 Collingbourne91147592011-04-15 00:35:48 +0000113 const Expr *E = IgnoreParens();
114
Chris Lattner4ebae652010-04-16 23:34:13 +0000115 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000116 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000117 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000118 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000119
Peter Collingbourne91147592011-04-15 00:35:48 +0000120 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000121 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000122 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000123 return UO->getSubExpr()->isKnownToHaveBooleanValue();
Richard Trieu0f097742014-04-04 04:13:47 +0000124 case UO_LNot:
125 return true;
Chris Lattner4ebae652010-04-16 23:34:13 +0000126 default:
127 return false;
128 }
129 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000130
John McCall45d30c32010-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 Collingbourne91147592011-04-15 00:35:48 +0000133 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000134 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000135
Peter Collingbourne91147592011-04-15 00:35:48 +0000136 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000137 switch (BO->getOpcode()) {
138 default: return false;
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +0000147 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +0000152 // Handle things like (x==2)|(y==12).
153 return BO->getLHS()->isKnownToHaveBooleanValue() &&
154 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000155
John McCalle3027922010-08-25 11:45:40 +0000156 case BO_Comma:
157 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000158 return BO->getRHS()->isKnownToHaveBooleanValue();
159 }
160 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000161
Peter Collingbourne91147592011-04-15 00:35:48 +0000162 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000163 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
164 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000165
Chris Lattner4ebae652010-04-16 23:34:13 +0000166 return false;
167}
168
John McCallbd066782011-02-09 08:16:59 +0000169// Amusing macro metaprogramming hack: check whether a class provides
170// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000171//
172// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-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 Dunbarb0ab5e92012-03-09 15:39:19 +0000189 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-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) \
Richard Smitha0cbfc92014-07-26 00:47:13 +0000198 case Stmt::type##Class: break;
John McCallbd066782011-02-09 08:16:59 +0000199#define EXPR(type, base) \
200 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
201#include "clang/AST/StmtNodes.inc"
202 }
Richard Smitha0cbfc92014-07-26 00:47:13 +0000203 llvm_unreachable("unknown expression kind");
John McCallbd066782011-02-09 08:16:59 +0000204}
205
Chris Lattner0eedafe2006-08-24 04:56:27 +0000206//===----------------------------------------------------------------------===//
207// Primary Expressions.
208//===----------------------------------------------------------------------===//
209
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210/// \brief Compute the type-, value-, and instantiation-dependence of a
211/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000212/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000213static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
214 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000215 bool &ValueDependent,
216 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000217 TypeDependent = false;
218 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000219 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000224 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000228
Douglas Gregored6c7442009-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 Gregorf144f4f2011-01-19 21:52:31 +0000231 if (T->isDependentType()) {
232 TypeDependent = true;
233 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000234 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000235 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000236 } else if (T->isInstantiationDependentType()) {
237 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000238 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239
Douglas Gregored6c7442009-11-23 11:41:28 +0000240 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-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 Gregored6c7442009-11-23 11:41:28 +0000253 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000254
Douglas Gregored6c7442009-11-23 11:41:28 +0000255 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256 if (isa<NonTypeTemplateParmDecl>(D)) {
257 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000259 return;
260 }
261
Douglas Gregored6c7442009-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 Smithec8dcd22011-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 Gregorf144f4f2011-01-19 21:52:31 +0000269 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000270 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000271 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000272 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000273 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000274 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000275 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000276 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000277 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000278 InstantiationDependent = true;
279 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000280 }
281
Douglas Gregor0e4de762010-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 Smithec8dcd22011-11-08 01:31:09 +0000285 if (Var->isStaticDataMember() &&
286 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000287 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 InstantiationDependent = true;
Richard Smith00f5d892013-11-14 22:40:45 +0000289 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290 if (TInfo->getType()->isIncompleteArrayType())
291 TypeDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000292 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000293
294 return;
295 }
296
Douglas Gregor0e4de762010-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 Gregorf144f4f2011-01-19 21:52:31 +0000300 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000302 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000303 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000304}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000305
Craig Topperce7167c2013-08-22 04:58:56 +0000306void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000307 bool TypeDependent = false;
308 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000309 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000310 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
311 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-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 Gregor678d76c2011-07-01 01:22:09 +0000324 getNumTemplateArgs(),
325 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000326 TypeDependent = true;
327 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000328 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000329 }
330
331 ExprBits.TypeDependent = TypeDependent;
332 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000333 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000334
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000335 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000336 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000337 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000338}
339
Craig Topperce7167c2013-08-22 04:58:56 +0000340DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000341 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000342 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000343 ValueDecl *D, bool RefersToEnclosingLocal,
344 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000345 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000346 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000347 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000348 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000349 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
350 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000351 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000352 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000353 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
354 if (FoundD)
355 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000356 DeclRefExprBits.HasTemplateKWAndArgsInfo
357 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000358 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000359 if (TemplateArgs) {
360 bool Dependent = false;
361 bool InstantiationDependent = false;
362 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000363 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
364 Dependent,
365 InstantiationDependent,
366 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000367 if (InstantiationDependent)
368 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 } else if (TemplateKWLoc.isValid()) {
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000371 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000372 DeclRefExprBits.HadMultipleCandidates = 0;
373
Daniel Dunbar9d355812012-03-09 01:51:51 +0000374 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000375}
376
Craig Topperce7167c2013-08-22 04:58:56 +0000377DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000378 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000379 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000380 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000381 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000382 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000383 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000384 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000385 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000386 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000387 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000388 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000389 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000390 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000391}
392
Craig Topperce7167c2013-08-22 04:58:56 +0000393DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000394 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000395 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000396 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000397 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000398 const DeclarationNameInfo &NameInfo,
399 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000400 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000401 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000402 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000403 // Filter out cases where the found Decl is the same as the value refenenced.
404 if (D == FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +0000405 FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000406
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000407 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000408 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000409 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000410 if (FoundD)
411 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000412 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000413 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
414 else if (TemplateKWLoc.isValid())
415 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000416
Chris Lattner5c0b4052010-10-30 05:14:06 +0000417 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000418 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000419 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000420 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000421}
422
Craig Topperce7167c2013-08-22 04:58:56 +0000423DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000424 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000425 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000426 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000427 unsigned NumTemplateArgs) {
428 std::size_t Size = sizeof(DeclRefExpr);
429 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000430 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000431 if (HasFoundDecl)
432 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000433 if (HasTemplateKWAndArgsInfo)
434 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000435
Chris Lattner5c0b4052010-10-30 05:14:06 +0000436 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000437 return new (Mem) DeclRefExpr(EmptyShell());
438}
439
Daniel Dunbarb507f272012-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 Gregor4bd90e52009-10-23 18:54:35 +0000450
Alexey Bataevec474782014-10-09 08:45:04 +0000451PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
452 StringLiteral *SL)
453 : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
454 FNTy->isDependentType(), FNTy->isDependentType(),
455 FNTy->isInstantiationDependentType(),
456 /*ContainsUnexpandedParameterPack=*/false),
457 Loc(L), Type(IT), FnName(SL) {}
458
459StringLiteral *PredefinedExpr::getFunctionName() {
460 return cast<StringLiteral>(FnName);
461}
462
463StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
464 switch (IT) {
465 case Func:
466 return "__func__";
467 case Function:
468 return "__FUNCTION__";
469 case FuncDName:
470 return "__FUNCDNAME__";
471 case LFunction:
472 return "L__FUNCTION__";
473 case PrettyFunction:
474 return "__PRETTY_FUNCTION__";
475 case FuncSig:
476 return "__FUNCSIG__";
477 case PrettyFunctionNoVirtual:
478 break;
479 }
480 llvm_unreachable("Unknown ident type for PredefinedExpr");
481}
482
Anders Carlsson2fb08242009-09-08 18:24:21 +0000483// FIXME: Maybe this should use DeclPrinter with a special "print predefined
484// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000485std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
486 ASTContext &Context = CurrentDecl->getASTContext();
487
David Majnemerbed356a2013-11-06 23:31:56 +0000488 if (IT == PredefinedExpr::FuncDName) {
489 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000490 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000491 MC.reset(Context.createMangleContext());
492
493 if (MC->shouldMangleDeclName(ND)) {
494 SmallString<256> Buffer;
495 llvm::raw_svector_ostream Out(Buffer);
496 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
497 MC->mangleCXXCtor(CD, Ctor_Base, Out);
498 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
499 MC->mangleCXXDtor(DD, Dtor_Base, Out);
500 else
501 MC->mangleName(ND, Out);
502
503 Out.flush();
504 if (!Buffer.empty() && Buffer.front() == '\01')
505 return Buffer.substr(1);
506 return Buffer.str();
507 } else
508 return ND->getIdentifier()->getName();
509 }
510 return "";
511 }
Alexey Bataevec474782014-10-09 08:45:04 +0000512 if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
513 std::unique_ptr<MangleContext> MC;
514 MC.reset(Context.createMangleContext());
515 SmallString<256> Buffer;
516 llvm::raw_svector_ostream Out(Buffer);
517 auto DC = CurrentDecl->getDeclContext();
518 if (DC->isFileContext())
519 MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
520 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
521 MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
522 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
523 MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
524 else
525 MC->mangleBlock(DC, BD, Out);
526 return Out.str();
527 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000528 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Reid Kleckner52eddda2014-04-08 18:13:24 +0000529 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000530 return FD->getNameAsString();
531
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000532 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000533 llvm::raw_svector_ostream Out(Name);
534
535 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000536 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000537 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000538 if (MD->isStatic())
539 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000540 }
541
David Blaikiebbafb8a2012-03-11 07:00:24 +0000542 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000543 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000544 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000545
Douglas Gregor11a434a2012-04-10 20:14:15 +0000546 const FunctionDecl *Decl = FD;
547 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
548 Decl = Pattern;
549 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Craig Topper36250ad2014-05-12 05:36:57 +0000550 const FunctionProtoType *FT = nullptr;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000551 if (FD->hasWrittenPrototype())
552 FT = dyn_cast<FunctionProtoType>(AFT);
553
Reid Kleckner52eddda2014-04-08 18:13:24 +0000554 if (IT == FuncSig) {
555 switch (FT->getCallConv()) {
556 case CC_C: POut << "__cdecl "; break;
557 case CC_X86StdCall: POut << "__stdcall "; break;
558 case CC_X86FastCall: POut << "__fastcall "; break;
559 case CC_X86ThisCall: POut << "__thiscall "; break;
560 // Only bother printing the conventions that MSVC knows about.
561 default: break;
562 }
563 }
564
565 FD->printQualifiedName(POut, Policy);
566
Douglas Gregor11a434a2012-04-10 20:14:15 +0000567 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000568 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000569 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000570 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000571 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000572 }
573
574 if (FT->isVariadic()) {
575 if (FD->getNumParams()) POut << ", ";
576 POut << "...";
577 }
578 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000579 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000580
Sam Weinig4e83bd22009-12-27 01:38:20 +0000581 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000582 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000583 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000584 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000585 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000586 POut << " volatile";
587 RefQualifierKind Ref = MD->getRefQualifier();
588 if (Ref == RQ_LValue)
589 POut << " &";
590 else if (Ref == RQ_RValue)
591 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000592 }
593
Douglas Gregor11a434a2012-04-10 20:14:15 +0000594 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
595 SpecsTy Specs;
596 const DeclContext *Ctx = FD->getDeclContext();
597 while (Ctx && isa<NamedDecl>(Ctx)) {
598 const ClassTemplateSpecializationDecl *Spec
599 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
600 if (Spec && !Spec->isExplicitSpecialization())
601 Specs.push_back(Spec);
602 Ctx = Ctx->getParent();
603 }
604
605 std::string TemplateParams;
606 llvm::raw_string_ostream TOut(TemplateParams);
607 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
608 I != E; ++I) {
609 const TemplateParameterList *Params
610 = (*I)->getSpecializedTemplate()->getTemplateParameters();
611 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
612 assert(Params->size() == Args.size());
613 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
614 StringRef Param = Params->getParam(i)->getName();
615 if (Param.empty()) continue;
616 TOut << Param << " = ";
617 Args.get(i).print(Policy, TOut);
618 TOut << ", ";
619 }
620 }
621
622 FunctionTemplateSpecializationInfo *FSI
623 = FD->getTemplateSpecializationInfo();
624 if (FSI && !FSI->isExplicitSpecialization()) {
625 const TemplateParameterList* Params
626 = FSI->getTemplate()->getTemplateParameters();
627 const TemplateArgumentList* Args = FSI->TemplateArguments;
628 assert(Params->size() == Args->size());
629 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
630 StringRef Param = Params->getParam(i)->getName();
631 if (Param.empty()) continue;
632 TOut << Param << " = ";
633 Args->get(i).print(Policy, TOut);
634 TOut << ", ";
635 }
636 }
637
638 TOut.flush();
639 if (!TemplateParams.empty()) {
640 // remove the trailing comma and space
641 TemplateParams.resize(TemplateParams.size() - 2);
642 POut << " [" << TemplateParams << "]";
643 }
644
645 POut.flush();
646
Benjamin Kramer90f54222013-08-21 11:45:27 +0000647 // Print "auto" for all deduced return types. This includes C++1y return
648 // type deduction and lambdas. For trailing return types resolve the
649 // decltype expression. Otherwise print the real type when this is
650 // not a constructor or destructor.
Alexey Bataevec474782014-10-09 08:45:04 +0000651 if (isa<CXXMethodDecl>(FD) &&
652 cast<CXXMethodDecl>(FD)->getParent()->isLambda())
Benjamin Kramer90f54222013-08-21 11:45:27 +0000653 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000654 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
655 FT->getReturnType()
656 ->getAs<DecltypeType>()
657 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000658 .getAsStringInternal(Proto, Policy);
659 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000660 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000661
662 Out << Proto;
663
664 Out.flush();
665 return Name.str().str();
666 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000667 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
668 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
669 // Skip to its enclosing function or method, but not its enclosing
670 // CapturedDecl.
671 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
672 const Decl *D = Decl::castFromDeclContext(DC);
673 return ComputeName(IT, D);
674 }
675 llvm_unreachable("CapturedDecl not inside a function or method");
676 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000677 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000678 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000679 llvm::raw_svector_ostream Out(Name);
680 Out << (MD->isInstanceMethod() ? '-' : '+');
681 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000682
683 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
684 // a null check to avoid a crash.
685 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000686 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000687
Anders Carlsson2fb08242009-09-08 18:24:21 +0000688 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000689 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000690 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000691
Anders Carlsson2fb08242009-09-08 18:24:21 +0000692 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000693 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000694 Out << ']';
695
696 Out.flush();
697 return Name.str().str();
698 }
699 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
700 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
701 return "top level";
702 }
703 return "";
704}
705
Craig Topper37932912013-08-18 10:09:15 +0000706void APNumericStorage::setIntValue(const ASTContext &C,
707 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000708 if (hasAllocation())
709 C.Deallocate(pVal);
710
711 BitWidth = Val.getBitWidth();
712 unsigned NumWords = Val.getNumWords();
713 const uint64_t* Words = Val.getRawData();
714 if (NumWords > 1) {
715 pVal = new (C) uint64_t[NumWords];
716 std::copy(Words, Words + NumWords, pVal);
717 } else if (NumWords == 1)
718 VAL = Words[0];
719 else
720 VAL = 0;
721}
722
Craig Topper37932912013-08-18 10:09:15 +0000723IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000724 QualType type, SourceLocation l)
725 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
726 false, false),
727 Loc(l) {
728 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
729 assert(V.getBitWidth() == C.getIntWidth(type) &&
730 "Integer type is not the correct size for constant.");
731 setValue(C, V);
732}
733
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000734IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000735IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000736 QualType type, SourceLocation l) {
737 return new (C) IntegerLiteral(C, V, type, l);
738}
739
740IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000741IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000742 return new (C) IntegerLiteral(Empty);
743}
744
Craig Topper37932912013-08-18 10:09:15 +0000745FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000746 bool isexact, QualType Type, SourceLocation L)
747 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
748 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000749 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000750 FloatingLiteralBits.IsExact = isexact;
751 setValue(C, V);
752}
753
Craig Topper37932912013-08-18 10:09:15 +0000754FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000755 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000756 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000757 FloatingLiteralBits.IsExact = false;
758}
759
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000760FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000761FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000762 bool isexact, QualType Type, SourceLocation L) {
763 return new (C) FloatingLiteral(C, V, isexact, Type, L);
764}
765
766FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000767FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000768 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000769}
770
Tim Northover178723a2013-01-22 09:46:51 +0000771const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
772 switch(FloatingLiteralBits.Semantics) {
773 case IEEEhalf:
774 return llvm::APFloat::IEEEhalf;
775 case IEEEsingle:
776 return llvm::APFloat::IEEEsingle;
777 case IEEEdouble:
778 return llvm::APFloat::IEEEdouble;
779 case x87DoubleExtended:
780 return llvm::APFloat::x87DoubleExtended;
781 case IEEEquad:
782 return llvm::APFloat::IEEEquad;
783 case PPCDoubleDouble:
784 return llvm::APFloat::PPCDoubleDouble;
785 }
786 llvm_unreachable("Unrecognised floating semantics");
787}
788
789void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
790 if (&Sem == &llvm::APFloat::IEEEhalf)
791 FloatingLiteralBits.Semantics = IEEEhalf;
792 else if (&Sem == &llvm::APFloat::IEEEsingle)
793 FloatingLiteralBits.Semantics = IEEEsingle;
794 else if (&Sem == &llvm::APFloat::IEEEdouble)
795 FloatingLiteralBits.Semantics = IEEEdouble;
796 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
797 FloatingLiteralBits.Semantics = x87DoubleExtended;
798 else if (&Sem == &llvm::APFloat::IEEEquad)
799 FloatingLiteralBits.Semantics = IEEEquad;
800 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
801 FloatingLiteralBits.Semantics = PPCDoubleDouble;
802 else
803 llvm_unreachable("Unknown floating semantics");
804}
805
Chris Lattnera0173132008-06-07 22:13:43 +0000806/// getValueAsApproximateDouble - This returns the value as an inaccurate
807/// double. Note that this may cause loss of precision, but is useful for
808/// debugging dumps, etc.
809double FloatingLiteral::getValueAsApproximateDouble() const {
810 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000811 bool ignored;
812 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
813 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000814 return V.convertToDouble();
815}
816
Nick Lewycky4ed84042012-02-24 09:07:53 +0000817int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000818 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000819 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000820 case Ascii:
821 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000822 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000823 break;
824 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000825 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000826 break;
827 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000828 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000829 break;
830 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000831 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000832 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000833 }
834 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
835 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000836 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000837 && "character byte widths supported are 1, 2, and 4 only");
838 return CharByteWidth;
839}
840
Craig Topper37932912013-08-18 10:09:15 +0000841StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000842 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000843 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000844 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000845 assert(C.getAsConstantArrayType(Ty) &&
846 "StringLiteral must be of constant array type!");
847
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000848 // Allocate enough space for the StringLiteral plus an array of locations for
849 // any concatenated string tokens.
850 void *Mem = C.Allocate(sizeof(StringLiteral)+
851 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000852 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000853 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000854
Steve Naroffdf7855b2007-02-21 23:46:25 +0000855 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000856 SL->setString(C,Str,Kind,Pascal);
857
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000858 SL->TokLocs[0] = Loc[0];
859 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000860
Chris Lattner630970d2009-02-18 05:49:11 +0000861 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000862 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
863 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000864}
865
Craig Topper37932912013-08-18 10:09:15 +0000866StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
867 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000868 void *Mem = C.Allocate(sizeof(StringLiteral)+
869 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000870 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000871 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000872 SL->CharByteWidth = 0;
873 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000874 SL->NumConcatenated = NumStrs;
875 return SL;
876}
877
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000878void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000879 switch (getKind()) {
880 case Ascii: break; // no prefix.
881 case Wide: OS << 'L'; break;
882 case UTF8: OS << "u8"; break;
883 case UTF16: OS << 'u'; break;
884 case UTF32: OS << 'U'; break;
885 }
886 OS << '"';
887 static const char Hex[] = "0123456789ABCDEF";
888
889 unsigned LastSlashX = getLength();
890 for (unsigned I = 0, N = getLength(); I != N; ++I) {
891 switch (uint32_t Char = getCodeUnit(I)) {
892 default:
893 // FIXME: Convert UTF-8 back to codepoints before rendering.
894
895 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
896 // Leave invalid surrogates alone; we'll use \x for those.
897 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
898 Char <= 0xdbff) {
899 uint32_t Trail = getCodeUnit(I + 1);
900 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
901 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
902 ++I;
903 }
904 }
905
906 if (Char > 0xff) {
907 // If this is a wide string, output characters over 0xff using \x
908 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
909 // codepoint: use \x escapes for invalid codepoints.
910 if (getKind() == Wide ||
911 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
912 // FIXME: Is this the best way to print wchar_t?
913 OS << "\\x";
914 int Shift = 28;
915 while ((Char >> Shift) == 0)
916 Shift -= 4;
917 for (/**/; Shift >= 0; Shift -= 4)
918 OS << Hex[(Char >> Shift) & 15];
919 LastSlashX = I;
920 break;
921 }
922
923 if (Char > 0xffff)
924 OS << "\\U00"
925 << Hex[(Char >> 20) & 15]
926 << Hex[(Char >> 16) & 15];
927 else
928 OS << "\\u";
929 OS << Hex[(Char >> 12) & 15]
930 << Hex[(Char >> 8) & 15]
931 << Hex[(Char >> 4) & 15]
932 << Hex[(Char >> 0) & 15];
933 break;
934 }
935
936 // If we used \x... for the previous character, and this character is a
937 // hexadecimal digit, prevent it being slurped as part of the \x.
938 if (LastSlashX + 1 == I) {
939 switch (Char) {
940 case '0': case '1': case '2': case '3': case '4':
941 case '5': case '6': case '7': case '8': case '9':
942 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
943 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
944 OS << "\"\"";
945 }
946 }
947
948 assert(Char <= 0xff &&
949 "Characters above 0xff should already have been handled.");
950
Jordan Rosea7d03842013-02-08 22:30:41 +0000951 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000952 OS << (char)Char;
953 else // Output anything hard as an octal escape.
954 OS << '\\'
955 << (char)('0' + ((Char >> 6) & 7))
956 << (char)('0' + ((Char >> 3) & 7))
957 << (char)('0' + ((Char >> 0) & 7));
958 break;
959 // Handle some common non-printable cases to make dumps prettier.
960 case '\\': OS << "\\\\"; break;
961 case '"': OS << "\\\""; break;
962 case '\n': OS << "\\n"; break;
963 case '\t': OS << "\\t"; break;
964 case '\a': OS << "\\a"; break;
965 case '\b': OS << "\\b"; break;
966 }
967 }
968 OS << '"';
969}
970
Craig Topper37932912013-08-18 10:09:15 +0000971void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000972 StringKind Kind, bool IsPascal) {
973 //FIXME: we assume that the string data comes from a target that uses the same
974 // code unit size and endianess for the type of string.
975 this->Kind = Kind;
976 this->IsPascal = IsPascal;
977
Nick Lewycky4ed84042012-02-24 09:07:53 +0000978 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000979 assert((Str.size()%CharByteWidth == 0)
980 && "size of data must be multiple of CharByteWidth");
981 Length = Str.size()/CharByteWidth;
982
983 switch(CharByteWidth) {
984 case 1: {
985 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000986 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000987 StrData.asChar = AStrData;
988 break;
989 }
990 case 2: {
991 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000992 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000993 StrData.asUInt16 = AStrData;
994 break;
995 }
996 case 4: {
997 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000998 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000999 StrData.asUInt32 = AStrData;
1000 break;
1001 }
1002 default:
1003 assert(false && "unsupported CharByteWidth");
1004 }
Douglas Gregor958dfc92009-04-15 16:35:07 +00001005}
1006
Chris Lattnere925d612010-11-17 07:37:15 +00001007/// getLocationOfByte - Return a source location that points to the specified
1008/// byte of this string literal.
1009///
1010/// Strings are amazingly complex. They can be formed from multiple tokens and
1011/// can have escape sequences in them in addition to the usual trigraph and
1012/// escaped newline business. This routine handles this complexity.
1013///
1014SourceLocation StringLiteral::
1015getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1016 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +00001017 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1018 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +00001019
Chris Lattnere925d612010-11-17 07:37:15 +00001020 // Loop over all of the tokens in this string until we find the one that
1021 // contains the byte we're looking for.
1022 unsigned TokNo = 0;
1023 while (1) {
1024 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1025 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1026
1027 // Get the spelling of the string so that we can get the data that makes up
1028 // the string literal, not the identifier for the macro it is potentially
1029 // expanded through.
1030 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1031
1032 // Re-lex the token to get its length and original spelling.
1033 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1034 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001035 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001036 if (Invalid)
1037 return StrTokSpellingLoc;
1038
1039 const char *StrData = Buffer.data()+LocInfo.second;
1040
Chris Lattnere925d612010-11-17 07:37:15 +00001041 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001042 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1043 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001044 Token TheTok;
1045 TheLexer.LexFromRawLexer(TheTok);
1046
1047 // Use the StringLiteralParser to compute the length of the string in bytes.
Craig Topper9d5583e2014-06-26 04:58:39 +00001048 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattnere925d612010-11-17 07:37:15 +00001049 unsigned TokNumBytes = SLP.GetStringLength();
1050
1051 // If the byte is in this token, return the location of the byte.
1052 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001053 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001054 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1055
1056 // Now that we know the offset of the token in the spelling, use the
1057 // preprocessor to get the offset in the original source.
1058 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1059 }
1060
1061 // Move to the next string token.
1062 ++TokNo;
1063 ByteNo -= TokNumBytes;
1064 }
1065}
1066
1067
1068
Chris Lattner1b926492006-08-23 06:42:10 +00001069/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1070/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001071StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001072 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001073 case UO_PostInc: return "++";
1074 case UO_PostDec: return "--";
1075 case UO_PreInc: return "++";
1076 case UO_PreDec: return "--";
1077 case UO_AddrOf: return "&";
1078 case UO_Deref: return "*";
1079 case UO_Plus: return "+";
1080 case UO_Minus: return "-";
1081 case UO_Not: return "~";
1082 case UO_LNot: return "!";
1083 case UO_Real: return "__real";
1084 case UO_Imag: return "__imag";
1085 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001086 }
David Blaikief47fa302012-01-17 02:30:50 +00001087 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001088}
1089
John McCalle3027922010-08-25 11:45:40 +00001090UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001091UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1092 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001093 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001094 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1095 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1096 case OO_Amp: return UO_AddrOf;
1097 case OO_Star: return UO_Deref;
1098 case OO_Plus: return UO_Plus;
1099 case OO_Minus: return UO_Minus;
1100 case OO_Tilde: return UO_Not;
1101 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001102 }
1103}
1104
1105OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1106 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001107 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1108 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1109 case UO_AddrOf: return OO_Amp;
1110 case UO_Deref: return OO_Star;
1111 case UO_Plus: return OO_Plus;
1112 case UO_Minus: return OO_Minus;
1113 case UO_Not: return OO_Tilde;
1114 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001115 default: return OO_None;
1116 }
1117}
1118
1119
Chris Lattner0eedafe2006-08-24 04:56:27 +00001120//===----------------------------------------------------------------------===//
1121// Postfix Operators.
1122//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001123
Craig Topper37932912013-08-18 10:09:15 +00001124CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1125 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1126 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001127 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001128 fn->isTypeDependent(),
1129 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001130 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001131 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001132 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001133
Benjamin Kramerc215e762012-08-24 11:54:20 +00001134 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001135 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001136 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001137 if (args[i]->isTypeDependent())
1138 ExprBits.TypeDependent = true;
1139 if (args[i]->isValueDependent())
1140 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001141 if (args[i]->isInstantiationDependent())
1142 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001143 if (args[i]->containsUnexpandedParameterPack())
1144 ExprBits.ContainsUnexpandedParameterPack = true;
1145
Peter Collingbourne3a347252011-02-08 21:18:02 +00001146 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001147 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001148
Peter Collingbourne3a347252011-02-08 21:18:02 +00001149 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001150 RParenLoc = rparenloc;
1151}
Nate Begeman1e36a852008-01-17 17:46:27 +00001152
Craig Topper37932912013-08-18 10:09:15 +00001153CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001154 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1155 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 fn->isTypeDependent(),
1157 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001158 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001159 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001160 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001161
Benjamin Kramerc215e762012-08-24 11:54:20 +00001162 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001163 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001164 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001165 if (args[i]->isTypeDependent())
1166 ExprBits.TypeDependent = true;
1167 if (args[i]->isValueDependent())
1168 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001169 if (args[i]->isInstantiationDependent())
1170 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001171 if (args[i]->containsUnexpandedParameterPack())
1172 ExprBits.ContainsUnexpandedParameterPack = true;
1173
Peter Collingbourne3a347252011-02-08 21:18:02 +00001174 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001175 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001176
Peter Collingbourne3a347252011-02-08 21:18:02 +00001177 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001178 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001179}
1180
Craig Topper37932912013-08-18 10:09:15 +00001181CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001182 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001183 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001184 SubExprs = new (C) Stmt*[PREARGS_START];
1185 CallExprBits.NumPreArgs = 0;
1186}
1187
Craig Topper37932912013-08-18 10:09:15 +00001188CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001189 EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001190 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbourne3a347252011-02-08 21:18:02 +00001191 // FIXME: Why do we allocate this?
1192 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1193 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001194}
1195
Nuno Lopes518e3702009-12-20 23:11:08 +00001196Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001197 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001198
1199 while (SubstNonTypeTemplateParmExpr *NTTP
1200 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1201 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1202 }
1203
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001204 // If we're calling a dereference, look at the pointer instead.
1205 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1206 if (BO->isPtrMemOp())
1207 CEE = BO->getRHS()->IgnoreParenCasts();
1208 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1209 if (UO->getOpcode() == UO_Deref)
1210 CEE = UO->getSubExpr()->IgnoreParenCasts();
1211 }
Chris Lattner52301912009-07-17 15:46:27 +00001212 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001213 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001214 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1215 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001216
Craig Topper36250ad2014-05-12 05:36:57 +00001217 return nullptr;
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001218}
1219
Nuno Lopes518e3702009-12-20 23:11:08 +00001220FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001221 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001222}
1223
Chris Lattnere4407ed2007-12-28 05:25:02 +00001224/// setNumArgs - This changes the number of arguments present in this call.
1225/// Any orphaned expressions are deleted by this, and any new operands are set
1226/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001227void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001228 // No change, just return.
1229 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001230
Chris Lattnere4407ed2007-12-28 05:25:02 +00001231 // If shrinking # arguments, just delete the extras and forgot them.
1232 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001233 this->NumArgs = NumArgs;
1234 return;
1235 }
1236
1237 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001238 unsigned NumPreArgs = getNumPreArgs();
1239 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001240 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001241 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001242 NewSubExprs[i] = SubExprs[i];
1243 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001244 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1245 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001246 NewSubExprs[i] = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001247
Douglas Gregorba6e5572009-04-17 21:46:47 +00001248 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001249 SubExprs = NewSubExprs;
1250 this->NumArgs = NumArgs;
1251}
1252
Alp Tokera724cff2013-12-28 21:59:02 +00001253/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001254/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001255unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001256 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001257 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001258 // ImplicitCastExpr.
1259 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1260 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001261 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001262
Steve Narofff6e3b3292008-01-31 01:07:12 +00001263 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1264 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001265 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001266
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001267 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1268 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001269 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001270
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001271 if (!FDecl->getIdentifier())
1272 return 0;
1273
Douglas Gregor15fc9562009-09-12 00:22:50 +00001274 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001275}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001276
Richard Smith5011a002013-01-17 23:46:04 +00001277bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001278 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001279 return Ctx.BuiltinInfo.isUnevaluated(BI);
1280 return false;
1281}
1282
Anders Carlsson00a27592009-05-26 04:57:27 +00001283QualType CallExpr::getCallReturnType() const {
1284 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001285 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001286 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001287 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001288 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001289 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1290 // This should never be overloaded and so should never return null.
1291 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001292
John McCall0009fcc2011-04-26 20:42:42 +00001293 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001294 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001295}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001296
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001297SourceLocation CallExpr::getLocStart() const {
1298 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001299 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001300
1301 SourceLocation begin = getCallee()->getLocStart();
Keno Fischer070db172014-08-15 01:39:12 +00001302 if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001303 begin = getArg(0)->getLocStart();
1304 return begin;
1305}
1306SourceLocation CallExpr::getLocEnd() const {
1307 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001308 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001309
1310 SourceLocation end = getRParenLoc();
Keno Fischer070db172014-08-15 01:39:12 +00001311 if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001312 end = getArg(getNumArgs() - 1)->getLocEnd();
1313 return end;
1314}
John McCall701417a2011-02-21 06:23:05 +00001315
Craig Topper37932912013-08-18 10:09:15 +00001316OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001317 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001318 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001319 ArrayRef<OffsetOfNode> comps,
1320 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001321 SourceLocation RParenLoc) {
1322 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001323 sizeof(OffsetOfNode) * comps.size() +
1324 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001325
Benjamin Kramerc215e762012-08-24 11:54:20 +00001326 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1327 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001328}
1329
Craig Topper37932912013-08-18 10:09:15 +00001330OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001331 unsigned numComps, unsigned numExprs) {
1332 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1333 sizeof(OffsetOfNode) * numComps +
1334 sizeof(Expr*) * numExprs);
1335 return new (Mem) OffsetOfExpr(numComps, numExprs);
1336}
1337
Craig Topper37932912013-08-18 10:09:15 +00001338OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001339 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001340 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001341 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001342 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1343 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001344 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001345 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001346 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001347 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001348 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001349{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001350 for (unsigned i = 0; i != comps.size(); ++i) {
1351 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001352 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001353
Benjamin Kramerc215e762012-08-24 11:54:20 +00001354 for (unsigned i = 0; i != exprs.size(); ++i) {
1355 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001356 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001357 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001358 ExprBits.ContainsUnexpandedParameterPack = true;
1359
Benjamin Kramerc215e762012-08-24 11:54:20 +00001360 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001361 }
1362}
1363
1364IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1365 assert(getKind() == Field || getKind() == Identifier);
1366 if (getKind() == Field)
1367 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001368
Douglas Gregor882211c2010-04-28 22:16:22 +00001369 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1370}
1371
Craig Topper37932912013-08-18 10:09:15 +00001372MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001373 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001374 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001375 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001376 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001377 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001378 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001379 QualType ty,
1380 ExprValueKind vk,
1381 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001382 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001383
Douglas Gregorea972d32011-02-28 21:54:11 +00001384 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001385 founddecl.getDecl() != memberdecl ||
1386 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001387 if (hasQualOrFound)
1388 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001389
John McCall6b51f282009-11-23 01:53:49 +00001390 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001391 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1392 else if (TemplateKWLoc.isValid())
1393 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001394
Chris Lattner5c0b4052010-10-30 05:14:06 +00001395 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001396 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1397 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001398
1399 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001400 // FIXME: Wrong. We should be looking at the member declaration we found.
1401 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001402 E->setValueDependent(true);
1403 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001404 E->setInstantiationDependent(true);
1405 }
1406 else if (QualifierLoc &&
1407 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1408 E->setInstantiationDependent(true);
1409
John McCall16df1e52010-03-30 21:47:33 +00001410 E->HasQualifierOrFoundDecl = true;
1411
1412 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001413 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001414 NQ->FoundDecl = founddecl;
1415 }
1416
Abramo Bagnara7945c982012-01-27 09:46:47 +00001417 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1418
John McCall16df1e52010-03-30 21:47:33 +00001419 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001420 bool Dependent = false;
1421 bool InstantiationDependent = false;
1422 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001423 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1424 Dependent,
1425 InstantiationDependent,
1426 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001427 if (InstantiationDependent)
1428 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001429 } else if (TemplateKWLoc.isValid()) {
1430 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001431 }
1432
1433 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001434}
1435
Daniel Dunbarb507f272012-03-09 15:39:15 +00001436SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001437 if (isImplicitAccess()) {
1438 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001439 return getQualifierLoc().getBeginLoc();
1440 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001441 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001442
Daniel Dunbarb507f272012-03-09 15:39:15 +00001443 // FIXME: We don't want this to happen. Rather, we should be able to
1444 // detect all kinds of implicit accesses more cleanly.
1445 SourceLocation BaseStartLoc = getBase()->getLocStart();
1446 if (BaseStartLoc.isValid())
1447 return BaseStartLoc;
1448 return MemberLoc;
1449}
1450SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001451 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001452 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001453 EndLoc = getRAngleLoc();
1454 else if (EndLoc.isInvalid())
1455 EndLoc = getBase()->getLocEnd();
1456 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001457}
1458
Alp Tokerc1086762013-12-07 13:51:35 +00001459bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001460 switch (getCastKind()) {
1461 case CK_DerivedToBase:
1462 case CK_UncheckedDerivedToBase:
1463 case CK_DerivedToBaseMemberPointer:
1464 case CK_BaseToDerived:
1465 case CK_BaseToDerivedMemberPointer:
1466 assert(!path_empty() && "Cast kind should have a base path!");
1467 break;
1468
1469 case CK_CPointerToObjCPointerCast:
1470 assert(getType()->isObjCObjectPointerType());
1471 assert(getSubExpr()->getType()->isPointerType());
1472 goto CheckNoBasePath;
1473
1474 case CK_BlockPointerToObjCPointerCast:
1475 assert(getType()->isObjCObjectPointerType());
1476 assert(getSubExpr()->getType()->isBlockPointerType());
1477 goto CheckNoBasePath;
1478
John McCallc62bb392012-02-15 01:22:51 +00001479 case CK_ReinterpretMemberPointer:
1480 assert(getType()->isMemberPointerType());
1481 assert(getSubExpr()->getType()->isMemberPointerType());
1482 goto CheckNoBasePath;
1483
John McCall9320b872011-09-09 05:25:32 +00001484 case CK_BitCast:
1485 // Arbitrary casts to C pointer types count as bitcasts.
1486 // Otherwise, we should only have block and ObjC pointer casts
1487 // here if they stay within the type kind.
1488 if (!getType()->isPointerType()) {
1489 assert(getType()->isObjCObjectPointerType() ==
1490 getSubExpr()->getType()->isObjCObjectPointerType());
1491 assert(getType()->isBlockPointerType() ==
1492 getSubExpr()->getType()->isBlockPointerType());
1493 }
1494 goto CheckNoBasePath;
1495
1496 case CK_AnyPointerToBlockPointerCast:
1497 assert(getType()->isBlockPointerType());
1498 assert(getSubExpr()->getType()->isAnyPointerType() &&
1499 !getSubExpr()->getType()->isBlockPointerType());
1500 goto CheckNoBasePath;
1501
Douglas Gregored90df32012-02-22 05:02:47 +00001502 case CK_CopyAndAutoreleaseBlockObject:
1503 assert(getType()->isBlockPointerType());
1504 assert(getSubExpr()->getType()->isBlockPointerType());
1505 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001506
1507 case CK_FunctionToPointerDecay:
1508 assert(getType()->isPointerType());
1509 assert(getSubExpr()->getType()->isFunctionType());
1510 goto CheckNoBasePath;
1511
David Tweede1468322013-12-11 13:39:46 +00001512 case CK_AddressSpaceConversion:
1513 assert(getType()->isPointerType());
1514 assert(getSubExpr()->getType()->isPointerType());
1515 assert(getType()->getPointeeType().getAddressSpace() !=
1516 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001517 // These should not have an inheritance path.
1518 case CK_Dynamic:
1519 case CK_ToUnion:
1520 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001521 case CK_NullToMemberPointer:
1522 case CK_NullToPointer:
1523 case CK_ConstructorConversion:
1524 case CK_IntegralToPointer:
1525 case CK_PointerToIntegral:
1526 case CK_ToVoid:
1527 case CK_VectorSplat:
1528 case CK_IntegralCast:
1529 case CK_IntegralToFloating:
1530 case CK_FloatingToIntegral:
1531 case CK_FloatingCast:
1532 case CK_ObjCObjectLValueCast:
1533 case CK_FloatingRealToComplex:
1534 case CK_FloatingComplexToReal:
1535 case CK_FloatingComplexCast:
1536 case CK_FloatingComplexToIntegralComplex:
1537 case CK_IntegralRealToComplex:
1538 case CK_IntegralComplexToReal:
1539 case CK_IntegralComplexCast:
1540 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001541 case CK_ARCProduceObject:
1542 case CK_ARCConsumeObject:
1543 case CK_ARCReclaimReturnedObject:
1544 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001545 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001546 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1547 goto CheckNoBasePath;
1548
1549 case CK_Dependent:
1550 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001551 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001552 case CK_AtomicToNonAtomic:
1553 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001554 case CK_PointerToBoolean:
1555 case CK_IntegralToBoolean:
1556 case CK_FloatingToBoolean:
1557 case CK_MemberPointerToBoolean:
1558 case CK_FloatingComplexToBoolean:
1559 case CK_IntegralComplexToBoolean:
1560 case CK_LValueBitCast: // -> bool&
1561 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001562 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001563 CheckNoBasePath:
1564 assert(path_empty() && "Cast kind should not have a base path!");
1565 break;
1566 }
Alp Tokerc1086762013-12-07 13:51:35 +00001567 return true;
John McCall9320b872011-09-09 05:25:32 +00001568}
1569
Anders Carlsson496335e2009-09-03 00:59:21 +00001570const char *CastExpr::getCastKindName() const {
1571 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001572 case CK_Dependent:
1573 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001574 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001575 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001576 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001577 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001578 case CK_LValueToRValue:
1579 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001580 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001581 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001582 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001583 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001584 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001585 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001586 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001587 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001588 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001589 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001590 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001591 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001592 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001593 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001594 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001595 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001596 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001597 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001598 case CK_NullToPointer:
1599 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001600 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001601 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001602 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001603 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001604 case CK_ReinterpretMemberPointer:
1605 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001606 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001607 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001608 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001609 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001610 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001611 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001612 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001613 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001614 case CK_PointerToBoolean:
1615 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001616 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001617 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001618 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001619 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001620 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001621 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001622 case CK_IntegralToBoolean:
1623 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001624 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001625 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001626 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001627 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001628 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001629 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001630 case CK_FloatingToBoolean:
1631 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001632 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001633 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001634 case CK_CPointerToObjCPointerCast:
1635 return "CPointerToObjCPointerCast";
1636 case CK_BlockPointerToObjCPointerCast:
1637 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001638 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001639 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001640 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001641 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001642 case CK_FloatingRealToComplex:
1643 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001644 case CK_FloatingComplexToReal:
1645 return "FloatingComplexToReal";
1646 case CK_FloatingComplexToBoolean:
1647 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001648 case CK_FloatingComplexCast:
1649 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001650 case CK_FloatingComplexToIntegralComplex:
1651 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001652 case CK_IntegralRealToComplex:
1653 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001654 case CK_IntegralComplexToReal:
1655 return "IntegralComplexToReal";
1656 case CK_IntegralComplexToBoolean:
1657 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001658 case CK_IntegralComplexCast:
1659 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001660 case CK_IntegralComplexToFloatingComplex:
1661 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001662 case CK_ARCConsumeObject:
1663 return "ARCConsumeObject";
1664 case CK_ARCProduceObject:
1665 return "ARCProduceObject";
1666 case CK_ARCReclaimReturnedObject:
1667 return "ARCReclaimReturnedObject";
1668 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001669 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001670 case CK_AtomicToNonAtomic:
1671 return "AtomicToNonAtomic";
1672 case CK_NonAtomicToAtomic:
1673 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001674 case CK_CopyAndAutoreleaseBlockObject:
1675 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001676 case CK_BuiltinFnToFnPtr:
1677 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001678 case CK_ZeroToOCLEvent:
1679 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001680 case CK_AddressSpaceConversion:
1681 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
John McCallc5e62b42010-11-13 09:02:35 +00001684 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001685}
1686
Douglas Gregord196a582009-12-14 19:27:10 +00001687Expr *CastExpr::getSubExprAsWritten() {
Craig Topper36250ad2014-05-12 05:36:57 +00001688 Expr *SubExpr = nullptr;
Douglas Gregord196a582009-12-14 19:27:10 +00001689 CastExpr *E = this;
1690 do {
1691 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001692
1693 // Skip through reference binding to temporary.
1694 if (MaterializeTemporaryExpr *Materialize
1695 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1696 SubExpr = Materialize->GetTemporaryExpr();
1697
Douglas Gregord196a582009-12-14 19:27:10 +00001698 // Skip any temporary bindings; they're implicit.
1699 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1700 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001701
Douglas Gregord196a582009-12-14 19:27:10 +00001702 // Conversions by constructor and conversion functions have a
1703 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001704 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001705 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001706 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001707 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001708
Douglas Gregord196a582009-12-14 19:27:10 +00001709 // If the subexpression we're left with is an implicit cast, look
1710 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001711 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1712
Douglas Gregord196a582009-12-14 19:27:10 +00001713 return SubExpr;
1714}
1715
John McCallcf142162010-08-07 06:22:56 +00001716CXXBaseSpecifier **CastExpr::path_buffer() {
1717 switch (getStmtClass()) {
1718#define ABSTRACT_STMT(x)
1719#define CASTEXPR(Type, Base) \
1720 case Stmt::Type##Class: \
1721 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1722#define STMT(Type, Base)
1723#include "clang/AST/StmtNodes.inc"
1724 default:
1725 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001726 }
1727}
1728
1729void CastExpr::setCastPath(const CXXCastPath &Path) {
1730 assert(Path.size() == path_size());
1731 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1732}
1733
Craig Topper37932912013-08-18 10:09:15 +00001734ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001735 CastKind Kind, Expr *Operand,
1736 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001737 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001738 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1739 void *Buffer =
1740 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1741 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001742 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001743 if (PathSize) E->setCastPath(*BasePath);
1744 return E;
1745}
1746
Craig Topper37932912013-08-18 10:09:15 +00001747ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001748 unsigned PathSize) {
1749 void *Buffer =
1750 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1751 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1752}
1753
1754
Craig Topper37932912013-08-18 10:09:15 +00001755CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001756 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001757 const CXXCastPath *BasePath,
1758 TypeSourceInfo *WrittenTy,
1759 SourceLocation L, SourceLocation R) {
1760 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1761 void *Buffer =
1762 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1763 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001764 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001765 if (PathSize) E->setCastPath(*BasePath);
1766 return E;
1767}
1768
Craig Topper37932912013-08-18 10:09:15 +00001769CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1770 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001771 void *Buffer =
1772 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1773 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1774}
1775
Chris Lattner1b926492006-08-23 06:42:10 +00001776/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1777/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001778StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001779 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001780 case BO_PtrMemD: return ".*";
1781 case BO_PtrMemI: return "->*";
1782 case BO_Mul: return "*";
1783 case BO_Div: return "/";
1784 case BO_Rem: return "%";
1785 case BO_Add: return "+";
1786 case BO_Sub: return "-";
1787 case BO_Shl: return "<<";
1788 case BO_Shr: return ">>";
1789 case BO_LT: return "<";
1790 case BO_GT: return ">";
1791 case BO_LE: return "<=";
1792 case BO_GE: return ">=";
1793 case BO_EQ: return "==";
1794 case BO_NE: return "!=";
1795 case BO_And: return "&";
1796 case BO_Xor: return "^";
1797 case BO_Or: return "|";
1798 case BO_LAnd: return "&&";
1799 case BO_LOr: return "||";
1800 case BO_Assign: return "=";
1801 case BO_MulAssign: return "*=";
1802 case BO_DivAssign: return "/=";
1803 case BO_RemAssign: return "%=";
1804 case BO_AddAssign: return "+=";
1805 case BO_SubAssign: return "-=";
1806 case BO_ShlAssign: return "<<=";
1807 case BO_ShrAssign: return ">>=";
1808 case BO_AndAssign: return "&=";
1809 case BO_XorAssign: return "^=";
1810 case BO_OrAssign: return "|=";
1811 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001812 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001813
David Blaikiee4d798f2012-01-20 21:50:17 +00001814 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001815}
Steve Naroff47500512007-04-19 23:00:49 +00001816
John McCalle3027922010-08-25 11:45:40 +00001817BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001818BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1819 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001820 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001821 case OO_Plus: return BO_Add;
1822 case OO_Minus: return BO_Sub;
1823 case OO_Star: return BO_Mul;
1824 case OO_Slash: return BO_Div;
1825 case OO_Percent: return BO_Rem;
1826 case OO_Caret: return BO_Xor;
1827 case OO_Amp: return BO_And;
1828 case OO_Pipe: return BO_Or;
1829 case OO_Equal: return BO_Assign;
1830 case OO_Less: return BO_LT;
1831 case OO_Greater: return BO_GT;
1832 case OO_PlusEqual: return BO_AddAssign;
1833 case OO_MinusEqual: return BO_SubAssign;
1834 case OO_StarEqual: return BO_MulAssign;
1835 case OO_SlashEqual: return BO_DivAssign;
1836 case OO_PercentEqual: return BO_RemAssign;
1837 case OO_CaretEqual: return BO_XorAssign;
1838 case OO_AmpEqual: return BO_AndAssign;
1839 case OO_PipeEqual: return BO_OrAssign;
1840 case OO_LessLess: return BO_Shl;
1841 case OO_GreaterGreater: return BO_Shr;
1842 case OO_LessLessEqual: return BO_ShlAssign;
1843 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1844 case OO_EqualEqual: return BO_EQ;
1845 case OO_ExclaimEqual: return BO_NE;
1846 case OO_LessEqual: return BO_LE;
1847 case OO_GreaterEqual: return BO_GE;
1848 case OO_AmpAmp: return BO_LAnd;
1849 case OO_PipePipe: return BO_LOr;
1850 case OO_Comma: return BO_Comma;
1851 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001852 }
1853}
1854
1855OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1856 static const OverloadedOperatorKind OverOps[] = {
1857 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1858 OO_Star, OO_Slash, OO_Percent,
1859 OO_Plus, OO_Minus,
1860 OO_LessLess, OO_GreaterGreater,
1861 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1862 OO_EqualEqual, OO_ExclaimEqual,
1863 OO_Amp,
1864 OO_Caret,
1865 OO_Pipe,
1866 OO_AmpAmp,
1867 OO_PipePipe,
1868 OO_Equal, OO_StarEqual,
1869 OO_SlashEqual, OO_PercentEqual,
1870 OO_PlusEqual, OO_MinusEqual,
1871 OO_LessLessEqual, OO_GreaterGreaterEqual,
1872 OO_AmpEqual, OO_CaretEqual,
1873 OO_PipeEqual,
1874 OO_Comma
1875 };
1876 return OverOps[Opc];
1877}
1878
Craig Topper37932912013-08-18 10:09:15 +00001879InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001880 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001881 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001882 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001883 InitExprs(C, initExprs.size()),
Craig Topper36250ad2014-05-12 05:36:57 +00001884 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001885{
1886 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001887 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001888 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001889 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001890 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001891 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001892 if (initExprs[I]->isInstantiationDependent())
1893 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001894 if (initExprs[I]->containsUnexpandedParameterPack())
1895 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001896 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001897
Benjamin Kramerc215e762012-08-24 11:54:20 +00001898 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001899}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001900
Craig Topper37932912013-08-18 10:09:15 +00001901void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001902 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001903 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001904}
1905
Craig Topper37932912013-08-18 10:09:15 +00001906void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Craig Topper36250ad2014-05-12 05:36:57 +00001907 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001908}
1909
Craig Topper37932912013-08-18 10:09:15 +00001910Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001911 if (Init >= InitExprs.size()) {
Craig Topper36250ad2014-05-12 05:36:57 +00001912 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Richard Smithc275da62013-12-06 01:27:24 +00001913 setInit(Init, expr);
Craig Topper36250ad2014-05-12 05:36:57 +00001914 return nullptr;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001915 }
Mike Stump11289f42009-09-09 15:08:12 +00001916
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001917 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001918 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001919 return Result;
1920}
1921
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001922void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001923 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001924 ArrayFillerOrUnionFieldInit = filler;
1925 // Fill out any "holes" in the array due to designated initializers.
1926 Expr **inits = getInits();
1927 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001928 if (inits[i] == nullptr)
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001929 inits[i] = filler;
1930}
1931
Richard Smith9ec1e482012-04-15 02:50:59 +00001932bool InitListExpr::isStringLiteralInit() const {
1933 if (getNumInits() != 1)
1934 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001935 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1936 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001937 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001938 // It is possible for getInit() to return null.
1939 const Expr *Init = getInit(0);
1940 if (!Init)
1941 return false;
1942 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001943 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1944}
1945
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001946SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001947 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001948 return SyntacticForm->getLocStart();
1949 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001950 if (Beg.isInvalid()) {
1951 // Find the first non-null initializer.
1952 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1953 E = InitExprs.end();
1954 I != E; ++I) {
1955 if (Stmt *S = *I) {
1956 Beg = S->getLocStart();
1957 break;
1958 }
1959 }
1960 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001961 return Beg;
1962}
1963
1964SourceLocation InitListExpr::getLocEnd() const {
1965 if (InitListExpr *SyntacticForm = getSyntacticForm())
1966 return SyntacticForm->getLocEnd();
1967 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001968 if (End.isInvalid()) {
1969 // Find the first non-null initializer from the end.
1970 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001971 E = InitExprs.rend();
1972 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001973 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001974 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001975 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001976 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001977 }
1978 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001979 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001980}
1981
Steve Naroff991e99d2008-09-04 15:31:07 +00001982/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001983///
John McCallc833dea2012-02-17 03:32:35 +00001984const FunctionProtoType *BlockExpr::getFunctionType() const {
1985 // The block pointer is never sugared, but the function type might be.
1986 return cast<BlockPointerType>(getType())
1987 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001988}
1989
Mike Stump11289f42009-09-09 15:08:12 +00001990SourceLocation BlockExpr::getCaretLocation() const {
1991 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001992}
Mike Stump11289f42009-09-09 15:08:12 +00001993const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001994 return TheBlock->getBody();
1995}
Mike Stump11289f42009-09-09 15:08:12 +00001996Stmt *BlockExpr::getBody() {
1997 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001998}
Steve Naroff415d3d52008-10-08 17:01:13 +00001999
2000
Chris Lattner1ec5f562007-06-27 05:38:08 +00002001//===----------------------------------------------------------------------===//
2002// Generic Expression Routines
2003//===----------------------------------------------------------------------===//
2004
Chris Lattner237f2752009-02-14 07:37:35 +00002005/// isUnusedResultAWarning - Return true if this immediate expression should
2006/// be warned about if the result is unused. If so, fill in Loc and Ranges
2007/// with location to warn on and the source range[s] to report with the
2008/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002009bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2010 SourceRange &R1, SourceRange &R2,
2011 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00002012 // Don't warn if the expr is type dependent. The type could end up
2013 // instantiating to void.
2014 if (isTypeDependent())
2015 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002016
Chris Lattner1ec5f562007-06-27 05:38:08 +00002017 switch (getStmtClass()) {
2018 default:
John McCallc493a732010-03-12 07:11:26 +00002019 if (getType()->isVoidType())
2020 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002021 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002022 Loc = getExprLoc();
2023 R1 = getSourceRange();
2024 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002025 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002026 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002027 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00002028 case GenericSelectionExprClass:
2029 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002030 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00002031 case ChooseExprClass:
2032 return cast<ChooseExpr>(this)->getChosenSubExpr()->
2033 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002034 case UnaryOperatorClass: {
2035 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00002036
Chris Lattner1ec5f562007-06-27 05:38:08 +00002037 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002038 case UO_Plus:
2039 case UO_Minus:
2040 case UO_AddrOf:
2041 case UO_Not:
2042 case UO_LNot:
2043 case UO_Deref:
2044 break;
John McCalle3027922010-08-25 11:45:40 +00002045 case UO_PostInc:
2046 case UO_PostDec:
2047 case UO_PreInc:
2048 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002049 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002050 case UO_Real:
2051 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002052 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002053 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2054 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002055 return false;
2056 break;
John McCalle3027922010-08-25 11:45:40 +00002057 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002058 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002059 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002060 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002061 Loc = UO->getOperatorLoc();
2062 R1 = UO->getSubExpr()->getSourceRange();
2063 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002064 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002065 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002066 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002067 switch (BO->getOpcode()) {
2068 default:
2069 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002070 // Consider the RHS of comma for side effects. LHS was checked by
2071 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002072 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002073 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2074 // lvalue-ness) of an assignment written in a macro.
2075 if (IntegerLiteral *IE =
2076 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2077 if (IE->getValue() == 0)
2078 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002079 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002080 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002081 case BO_LAnd:
2082 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002083 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2084 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002085 return false;
2086 break;
John McCall1e3715a2010-02-16 04:10:53 +00002087 }
Chris Lattner237f2752009-02-14 07:37:35 +00002088 if (BO->isAssignmentOp())
2089 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002090 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002091 Loc = BO->getOperatorLoc();
2092 R1 = BO->getLHS()->getSourceRange();
2093 R2 = BO->getRHS()->getSourceRange();
2094 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002095 }
Chris Lattner86928112007-08-25 02:00:02 +00002096 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002097 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002098 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002099 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002100
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002101 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002102 // If only one of the LHS or RHS is a warning, the operator might
2103 // be being used for control flow. Only warn if both the LHS and
2104 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002105 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002106 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002107 return false;
2108 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002109 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002110 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002111 }
2112
Chris Lattnera44d1162007-06-27 05:58:59 +00002113 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002114 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002115 Loc = cast<MemberExpr>(this)->getMemberLoc();
2116 R1 = SourceRange(Loc, Loc);
2117 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2118 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002119
Chris Lattner1ec5f562007-06-27 05:38:08 +00002120 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002121 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002122 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2123 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2124 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2125 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002126
Chandler Carruth46339472011-08-17 09:49:44 +00002127 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002128 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002129 // overloads as there is no reasonable way to define these such that they
2130 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002131 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002132 // provides additional value as well. If this list is updated,
2133 // DiagnoseUnusedComparison should be as well.
2134 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002135 switch (Op->getOperator()) {
2136 default:
2137 break;
2138 case OO_EqualEqual:
2139 case OO_ExclaimEqual:
2140 case OO_Less:
2141 case OO_Greater:
2142 case OO_GreaterEqual:
2143 case OO_LessEqual:
Richard Trieuccedd522014-05-20 01:34:43 +00002144 if (Op->getCallReturnType()->isReferenceType() ||
2145 Op->getCallReturnType()->isVoidType())
Richard Trieu161132b2014-05-14 23:22:10 +00002146 break;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002147 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002148 Loc = Op->getOperatorLoc();
2149 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002150 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002151 }
Chandler Carruth46339472011-08-17 09:49:44 +00002152
2153 // Fallthrough for generic call handling.
2154 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002155 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002156 case CXXMemberCallExprClass:
2157 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002158 // If this is a direct call, get the callee.
2159 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002160 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002161 // If the callee has attribute pure, const, or warn_unused_result, warn
2162 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002163 //
2164 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2165 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002166 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2167 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002168 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002169 Loc = CE->getCallee()->getLocStart();
2170 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002171
Chris Lattner1a6babf2009-10-13 04:53:48 +00002172 if (unsigned NumArgs = CE->getNumArgs())
2173 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2174 CE->getArg(NumArgs-1)->getLocEnd());
2175 return true;
2176 }
Chris Lattner237f2752009-02-14 07:37:35 +00002177 }
2178 return false;
2179 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002180
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002181 // If we don't know precisely what we're looking at, let's not warn.
2182 case UnresolvedLookupExprClass:
2183 case CXXUnresolvedConstructExprClass:
2184 return false;
2185
Anders Carlsson6aa50392009-11-17 17:11:23 +00002186 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002187 case CXXConstructExprClass: {
2188 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2189 if (Type->hasAttr<WarnUnusedAttr>()) {
2190 WarnE = this;
2191 Loc = getLocStart();
2192 R1 = getSourceRange();
2193 return true;
2194 }
2195 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002196 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002197 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002198
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002199 case ObjCMessageExprClass: {
2200 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002201 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002202 ME->isInstanceMessage() &&
2203 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002204 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002205 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002206 Loc = getExprLoc();
2207 R1 = ME->getSourceRange();
2208 return true;
2209 }
2210
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002211 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
2212 if (MD->hasAttr<WarnUnusedResultAttr>() ||
2213 (MD->isPropertyAccessor() && !MD->getReturnType()->isVoidType() &&
2214 !ME->getReceiverType()->isObjCIdType())) {
2215 WarnE = this;
2216 Loc = getExprLoc();
2217 return true;
2218 }
2219
Chris Lattner237f2752009-02-14 07:37:35 +00002220 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002221 }
Mike Stump11289f42009-09-09 15:08:12 +00002222
John McCallb7bd14f2010-12-02 01:19:52 +00002223 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002224 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002225 Loc = getExprLoc();
2226 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002227 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002228
John McCallfe96e0b2011-11-06 09:01:30 +00002229 case PseudoObjectExprClass: {
2230 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2231
2232 // Only complain about things that have the form of a getter.
2233 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2234 isa<BinaryOperator>(PO->getSyntacticForm()))
2235 return false;
2236
Eli Friedmanc11535c2012-05-24 00:47:05 +00002237 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002238 Loc = getExprLoc();
2239 R1 = getSourceRange();
2240 return true;
2241 }
2242
Chris Lattner944d3062008-07-26 19:51:01 +00002243 case StmtExprClass: {
2244 // Statement exprs don't logically have side effects themselves, but are
2245 // sometimes used in macros in ways that give them a type that is unused.
2246 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2247 // however, if the result of the stmt expr is dead, we don't want to emit a
2248 // warning.
2249 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002250 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002251 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002252 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002253 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2254 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002255 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002256 }
Mike Stump11289f42009-09-09 15:08:12 +00002257
John McCallc493a732010-03-12 07:11:26 +00002258 if (getType()->isVoidType())
2259 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002260 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002261 Loc = cast<StmtExpr>(this)->getLParenLoc();
2262 R1 = getSourceRange();
2263 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002264 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002265 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002266 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002267 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002268 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002269 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002270 if (CE->getCastKind() == CK_ToVoid) {
2271 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002272 CE->getSubExpr()->getType().isVolatileQualified()) {
2273 const DeclRefExpr *DRE =
2274 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2275 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2276 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2277 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2278 R1, R2, Ctx);
2279 }
2280 }
Chris Lattner2706a552009-07-28 18:25:28 +00002281 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002282 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002283
Eli Friedmanc11535c2012-05-24 00:47:05 +00002284 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002285 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002286 if (CE->getCastKind() == CK_ConstructorConversion)
2287 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002288
Eli Friedmanc11535c2012-05-24 00:47:05 +00002289 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002290 if (const CXXFunctionalCastExpr *CXXCE =
2291 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002292 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002293 R1 = CXXCE->getSubExpr()->getSourceRange();
2294 } else {
2295 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2296 Loc = CStyleCE->getLParenLoc();
2297 R1 = CStyleCE->getSubExpr()->getSourceRange();
2298 }
Chris Lattner237f2752009-02-14 07:37:35 +00002299 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002300 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002301 case ImplicitCastExprClass: {
2302 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002303
Eli Friedmanc11535c2012-05-24 00:47:05 +00002304 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2305 if (ICE->getCastKind() == CK_LValueToRValue &&
2306 ICE->getSubExpr()->getType().isVolatileQualified())
2307 return false;
2308
2309 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2310 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002311 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002312 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002313 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002314 case CXXDefaultInitExprClass:
2315 return (cast<CXXDefaultInitExpr>(this)
2316 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002317
2318 case CXXNewExprClass:
2319 // FIXME: In theory, there might be new expressions that don't have side
2320 // effects (e.g. a placement new with an uninitialized POD).
2321 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002322 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002323 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002324 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002325 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002326 case ExprWithCleanupsClass:
2327 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002328 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002329 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002330}
2331
Fariborz Jahanian07735332009-02-22 18:40:18 +00002332/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002333/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002334bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002335 const Expr *E = IgnoreParens();
2336 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002337 default:
2338 return false;
2339 case ObjCIvarRefExprClass:
2340 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002341 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002342 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002343 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002344 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002345 case MaterializeTemporaryExprClass:
2346 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2347 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002348 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002349 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002350 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002351 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002352
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002353 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2354 if (VD->hasGlobalStorage())
2355 return true;
2356 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002357 // dereferencing to a pointer is always a gc'able candidate,
2358 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002359 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002360 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002361 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002362 return false;
2363 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002364 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002365 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002366 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002367 }
2368 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002369 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002370 }
2371}
Sebastian Redlce354af2010-09-10 20:55:33 +00002372
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002373bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2374 if (isTypeDependent())
2375 return false;
John McCall086a4642010-11-24 05:12:34 +00002376 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002377}
2378
John McCall0009fcc2011-04-26 20:42:42 +00002379QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002380 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002381
2382 // Bound member expressions are always one of these possibilities:
2383 // x->m x.m x->*y x.*y
2384 // (possibly parenthesized)
2385
2386 expr = expr->IgnoreParens();
2387 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2388 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2389 return mem->getMemberDecl()->getType();
2390 }
2391
2392 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2393 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2394 ->getPointeeType();
2395 assert(type->isFunctionType());
2396 return type;
2397 }
2398
2399 assert(isa<UnresolvedMemberExpr>(expr));
2400 return QualType();
2401}
2402
Ted Kremenekfff70962008-01-17 16:57:34 +00002403Expr* Expr::IgnoreParens() {
2404 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002405 while (true) {
2406 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2407 E = P->getSubExpr();
2408 continue;
2409 }
2410 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2411 if (P->getOpcode() == UO_Extension) {
2412 E = P->getSubExpr();
2413 continue;
2414 }
2415 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002416 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2417 if (!P->isResultDependent()) {
2418 E = P->getResultExpr();
2419 continue;
2420 }
2421 }
Eli Friedman75807f22013-07-20 00:40:58 +00002422 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2423 if (!P->isConditionDependent()) {
2424 E = P->getChosenSubExpr();
2425 continue;
2426 }
2427 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002428 return E;
2429 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002430}
2431
Chris Lattnerf2660962008-02-13 01:02:39 +00002432/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2433/// or CastExprs or ImplicitCastExprs, returning their operand.
2434Expr *Expr::IgnoreParenCasts() {
2435 Expr *E = this;
2436 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002437 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002438 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002439 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002440 continue;
2441 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002442 if (MaterializeTemporaryExpr *Materialize
2443 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2444 E = Materialize->GetTemporaryExpr();
2445 continue;
2446 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002447 if (SubstNonTypeTemplateParmExpr *NTTP
2448 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2449 E = NTTP->getReplacement();
2450 continue;
2451 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002452 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002453 }
2454}
2455
Ted Kremenek6f375e52014-04-16 07:26:09 +00002456Expr *Expr::IgnoreCasts() {
2457 Expr *E = this;
2458 while (true) {
2459 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2460 E = P->getSubExpr();
2461 continue;
2462 }
2463 if (MaterializeTemporaryExpr *Materialize
2464 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2465 E = Materialize->GetTemporaryExpr();
2466 continue;
2467 }
2468 if (SubstNonTypeTemplateParmExpr *NTTP
2469 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2470 E = NTTP->getReplacement();
2471 continue;
2472 }
2473 return E;
2474 }
2475}
2476
John McCall5a4ce8b2010-12-04 08:24:19 +00002477/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2478/// casts. This is intended purely as a temporary workaround for code
2479/// that hasn't yet been rewritten to do the right thing about those
2480/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002481Expr *Expr::IgnoreParenLValueCasts() {
2482 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002483 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002484 E = E->IgnoreParens();
2485 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002486 if (P->getCastKind() == CK_LValueToRValue) {
2487 E = P->getSubExpr();
2488 continue;
2489 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002490 } else if (MaterializeTemporaryExpr *Materialize
2491 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2492 E = Materialize->GetTemporaryExpr();
2493 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002494 } else if (SubstNonTypeTemplateParmExpr *NTTP
2495 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2496 E = NTTP->getReplacement();
2497 continue;
John McCall34376a62010-12-04 03:47:34 +00002498 }
2499 break;
2500 }
2501 return E;
2502}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002503
2504Expr *Expr::ignoreParenBaseCasts() {
2505 Expr *E = this;
2506 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002507 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002508 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2509 if (CE->getCastKind() == CK_DerivedToBase ||
2510 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2511 CE->getCastKind() == CK_NoOp) {
2512 E = CE->getSubExpr();
2513 continue;
2514 }
2515 }
2516
2517 return E;
2518 }
2519}
2520
John McCalleebc8322010-05-05 22:59:52 +00002521Expr *Expr::IgnoreParenImpCasts() {
2522 Expr *E = this;
2523 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002524 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002525 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002526 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002527 continue;
2528 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002529 if (MaterializeTemporaryExpr *Materialize
2530 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2531 E = Materialize->GetTemporaryExpr();
2532 continue;
2533 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002534 if (SubstNonTypeTemplateParmExpr *NTTP
2535 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2536 E = NTTP->getReplacement();
2537 continue;
2538 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002539 return E;
John McCalleebc8322010-05-05 22:59:52 +00002540 }
2541}
2542
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002543Expr *Expr::IgnoreConversionOperator() {
2544 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002545 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002546 return MCE->getImplicitObjectArgument();
2547 }
2548 return this;
2549}
2550
Chris Lattneref26c772009-03-13 17:28:01 +00002551/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2552/// value (including ptr->int casts of the same size). Strip off any
2553/// ParenExpr or CastExprs, returning their operand.
2554Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2555 Expr *E = this;
2556 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002557 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002558
Chris Lattneref26c772009-03-13 17:28:01 +00002559 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2560 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002561 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002562 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002563
Chris Lattneref26c772009-03-13 17:28:01 +00002564 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2565 E = SE;
2566 continue;
2567 }
Mike Stump11289f42009-09-09 15:08:12 +00002568
Abramo Bagnara932e3932010-10-15 07:51:18 +00002569 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002570 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002571 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002572 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002573 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2574 E = SE;
2575 continue;
2576 }
2577 }
Mike Stump11289f42009-09-09 15:08:12 +00002578
Douglas Gregor6a40b082011-09-08 17:56:33 +00002579 if (SubstNonTypeTemplateParmExpr *NTTP
2580 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2581 E = NTTP->getReplacement();
2582 continue;
2583 }
2584
Chris Lattneref26c772009-03-13 17:28:01 +00002585 return E;
2586 }
2587}
2588
Douglas Gregord196a582009-12-14 19:27:10 +00002589bool Expr::isDefaultArgument() const {
2590 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002591 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2592 E = M->GetTemporaryExpr();
2593
Douglas Gregord196a582009-12-14 19:27:10 +00002594 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2595 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002596
Douglas Gregord196a582009-12-14 19:27:10 +00002597 return isa<CXXDefaultArgExpr>(E);
2598}
Chris Lattneref26c772009-03-13 17:28:01 +00002599
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002600/// \brief Skip over any no-op casts and any temporary-binding
2601/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002602static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002603 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2604 E = M->GetTemporaryExpr();
2605
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002606 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002607 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002608 E = ICE->getSubExpr();
2609 else
2610 break;
2611 }
2612
2613 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2614 E = BE->getSubExpr();
2615
2616 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002617 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002618 E = ICE->getSubExpr();
2619 else
2620 break;
2621 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002622
2623 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002624}
2625
John McCall7a626f62010-09-15 10:14:12 +00002626/// isTemporaryObject - Determines if this expression produces a
2627/// temporary of the given class type.
2628bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2629 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2630 return false;
2631
Anders Carlsson66bbf502010-11-28 16:40:49 +00002632 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002633
John McCall02dc8c72010-09-15 20:59:13 +00002634 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002635 if (!E->Classify(C).isPRValue()) {
2636 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002637 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002638 return false;
2639 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002640
John McCallf4ee1dd2010-09-16 06:57:56 +00002641 // Black-list a few cases which yield pr-values of class type that don't
2642 // refer to temporaries of that type:
2643
2644 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002645 if (isa<ImplicitCastExpr>(E)) {
2646 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2647 case CK_DerivedToBase:
2648 case CK_UncheckedDerivedToBase:
2649 return false;
2650 default:
2651 break;
2652 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002653 }
2654
John McCallf4ee1dd2010-09-16 06:57:56 +00002655 // - member expressions (all)
2656 if (isa<MemberExpr>(E))
2657 return false;
2658
Eli Friedman13ffdd82012-06-15 23:51:06 +00002659 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2660 if (BO->isPtrMemOp())
2661 return false;
2662
John McCallc07a0c72011-02-17 10:25:35 +00002663 // - opaque values (all)
2664 if (isa<OpaqueValueExpr>(E))
2665 return false;
2666
John McCall7a626f62010-09-15 10:14:12 +00002667 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002668}
2669
Douglas Gregor25b7e052011-03-02 21:06:53 +00002670bool Expr::isImplicitCXXThis() const {
2671 const Expr *E = this;
2672
2673 // Strip away parentheses and casts we don't care about.
2674 while (true) {
2675 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2676 E = Paren->getSubExpr();
2677 continue;
2678 }
2679
2680 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2681 if (ICE->getCastKind() == CK_NoOp ||
2682 ICE->getCastKind() == CK_LValueToRValue ||
2683 ICE->getCastKind() == CK_DerivedToBase ||
2684 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2685 E = ICE->getSubExpr();
2686 continue;
2687 }
2688 }
2689
2690 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2691 if (UnOp->getOpcode() == UO_Extension) {
2692 E = UnOp->getSubExpr();
2693 continue;
2694 }
2695 }
2696
Douglas Gregorfe314812011-06-21 17:03:29 +00002697 if (const MaterializeTemporaryExpr *M
2698 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2699 E = M->GetTemporaryExpr();
2700 continue;
2701 }
2702
Douglas Gregor25b7e052011-03-02 21:06:53 +00002703 break;
2704 }
2705
2706 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2707 return This->isImplicit();
2708
2709 return false;
2710}
2711
Douglas Gregor4619e432008-12-05 23:32:09 +00002712/// hasAnyTypeDependentArguments - Determines if any of the expressions
2713/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002714bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002715 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002716 if (Exprs[I]->isTypeDependent())
2717 return true;
2718
2719 return false;
2720}
2721
Abramo Bagnara847c6602014-05-22 19:20:46 +00002722bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2723 const Expr **Culprit) const {
Eli Friedman384da272009-01-25 03:12:18 +00002724 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002725 // which can be evaluated at compile-time. It very closely parallels
2726 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2727 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2728 // to isEvaluatable most of the time.
2729 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002730 // If we ever capture reference-binding directly in the AST, we can
2731 // kill the second parameter.
2732
2733 if (IsForRef) {
2734 EvalResult Result;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002735 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2736 return true;
2737 if (Culprit)
2738 *Culprit = this;
2739 return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002740 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002741
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002742 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002743 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002744 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002745 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002746 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002747 case CXXTemporaryObjectExprClass:
2748 case CXXConstructExprClass: {
2749 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002750
Eli Friedman4c27ac22013-07-16 22:40:53 +00002751 if (CE->getConstructor()->isTrivial() &&
2752 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2753 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002754 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002755
Eli Friedman4c27ac22013-07-16 22:40:53 +00002756 // Trivial copy constructor
2757 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Abramo Bagnara847c6602014-05-22 19:20:46 +00002758 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62306a2011-11-10 06:34:14 +00002759 }
2760
Richard Smithd62306a2011-11-10 06:34:14 +00002761 break;
John McCall81c9cea2010-08-01 21:51:45 +00002762 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002763 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002764 // This handles gcc's extension that allows global initializers like
2765 // "struct x {int x;} x = (struct x) {};".
2766 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002767 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002768 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002769 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002770 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002771 const InitListExpr *ILE = cast<InitListExpr>(this);
2772 if (ILE->getType()->isArrayType()) {
2773 unsigned numInits = ILE->getNumInits();
2774 for (unsigned i = 0; i < numInits; i++) {
Abramo Bagnara847c6602014-05-22 19:20:46 +00002775 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002776 return false;
2777 }
2778 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002779 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002780
2781 if (ILE->getType()->isRecordType()) {
2782 unsigned ElementNo = 0;
2783 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
Hans Wennborga302cd92014-08-21 16:06:57 +00002784 for (const auto *Field : RD->fields()) {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002785 // If this is a union, skip all the fields that aren't being initialized.
Hans Wennborga302cd92014-08-21 16:06:57 +00002786 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
Eli Friedman4c27ac22013-07-16 22:40:53 +00002787 continue;
2788
2789 // Don't emit anonymous bitfields, they just affect layout.
2790 if (Field->isUnnamedBitfield())
2791 continue;
2792
2793 if (ElementNo < ILE->getNumInits()) {
2794 const Expr *Elt = ILE->getInit(ElementNo++);
2795 if (Field->isBitField()) {
2796 // Bitfields have to evaluate to an integer.
2797 llvm::APSInt ResultTmp;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002798 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2799 if (Culprit)
2800 *Culprit = Elt;
Eli Friedman4c27ac22013-07-16 22:40:53 +00002801 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002802 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002803 } else {
2804 bool RefType = Field->getType()->isReferenceType();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002805 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002806 return false;
2807 }
2808 }
2809 }
2810 return true;
2811 }
2812
2813 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002814 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002815 case ImplicitValueInitExprClass:
2816 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002817 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002818 return cast<ParenExpr>(this)->getSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002819 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbourne91147592011-04-15 00:35:48 +00002820 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002821 return cast<GenericSelectionExpr>(this)->getResultExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002822 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002823 case ChooseExprClass:
Abramo Bagnara847c6602014-05-22 19:20:46 +00002824 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2825 if (Culprit)
2826 *Culprit = this;
Eli Friedman75807f22013-07-20 00:40:58 +00002827 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002828 }
Eli Friedman75807f22013-07-20 00:40:58 +00002829 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002830 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002831 case UnaryOperatorClass: {
2832 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002833 if (Exp->getOpcode() == UO_Extension)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002834 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002835 break;
2836 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002837 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002838 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002839 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002840 case CStyleCastExprClass:
2841 case ObjCBridgedCastExprClass:
2842 case CXXDynamicCastExprClass:
2843 case CXXReinterpretCastExprClass:
2844 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002845 const CastExpr *CE = cast<CastExpr>(this);
2846
Eli Friedman13ec75b2011-12-21 00:43:02 +00002847 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002848 if (CE->getCastKind() == CK_NoOp ||
2849 CE->getCastKind() == CK_LValueToRValue ||
2850 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002851 CE->getCastKind() == CK_ConstructorConversion ||
2852 CE->getCastKind() == CK_NonAtomicToAtomic ||
2853 CE->getCastKind() == CK_AtomicToNonAtomic)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002854 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smith161f09a2011-12-06 22:44:34 +00002855
Eli Friedman384da272009-01-25 03:12:18 +00002856 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002857 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002858 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002859 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002860 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002861
2862 case SubstNonTypeTemplateParmExprClass:
2863 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002864 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002865 case CXXDefaultArgExprClass:
2866 return cast<CXXDefaultArgExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002867 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002868 case CXXDefaultInitExprClass:
2869 return cast<CXXDefaultInitExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002870 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002871 }
Abramo Bagnara847c6602014-05-22 19:20:46 +00002872 if (isEvaluatable(Ctx))
2873 return true;
2874 if (Culprit)
2875 *Culprit = this;
2876 return false;
Steve Naroffb03f5942007-09-02 20:30:18 +00002877}
2878
Richard Smith0421ce72012-08-07 04:16:51 +00002879bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2880 if (isInstantiationDependent())
2881 return true;
2882
2883 switch (getStmtClass()) {
2884 case NoStmtClass:
2885 #define ABSTRACT_STMT(Type)
2886 #define STMT(Type, Base) case Type##Class:
2887 #define EXPR(Type, Base)
2888 #include "clang/AST/StmtNodes.inc"
2889 llvm_unreachable("unexpected Expr kind");
2890
2891 case DependentScopeDeclRefExprClass:
2892 case CXXUnresolvedConstructExprClass:
2893 case CXXDependentScopeMemberExprClass:
2894 case UnresolvedLookupExprClass:
2895 case UnresolvedMemberExprClass:
2896 case PackExpansionExprClass:
2897 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002898 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002899 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2900
Richard Smitha33e4fe2012-08-07 05:18:29 +00002901 case DeclRefExprClass:
2902 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002903 case PredefinedExprClass:
2904 case IntegerLiteralClass:
2905 case FloatingLiteralClass:
2906 case ImaginaryLiteralClass:
2907 case StringLiteralClass:
2908 case CharacterLiteralClass:
2909 case OffsetOfExprClass:
2910 case ImplicitValueInitExprClass:
2911 case UnaryExprOrTypeTraitExprClass:
2912 case AddrLabelExprClass:
2913 case GNUNullExprClass:
2914 case CXXBoolLiteralExprClass:
2915 case CXXNullPtrLiteralExprClass:
2916 case CXXThisExprClass:
2917 case CXXScalarValueInitExprClass:
2918 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002919 case ArrayTypeTraitExprClass:
2920 case ExpressionTraitExprClass:
2921 case CXXNoexceptExprClass:
2922 case SizeOfPackExprClass:
2923 case ObjCStringLiteralClass:
2924 case ObjCEncodeExprClass:
2925 case ObjCBoolLiteralExprClass:
2926 case CXXUuidofExprClass:
2927 case OpaqueValueExprClass:
2928 // These never have a side-effect.
2929 return false;
2930
2931 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002932 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002933 case CompoundAssignOperatorClass:
2934 case VAArgExprClass:
2935 case AtomicExprClass:
2936 case StmtExprClass:
2937 case CXXOperatorCallExprClass:
2938 case CXXMemberCallExprClass:
2939 case UserDefinedLiteralClass:
2940 case CXXThrowExprClass:
2941 case CXXNewExprClass:
2942 case CXXDeleteExprClass:
2943 case ExprWithCleanupsClass:
2944 case CXXBindTemporaryExprClass:
2945 case BlockExprClass:
2946 case CUDAKernelCallExprClass:
2947 // These always have a side-effect.
2948 return true;
2949
2950 case ParenExprClass:
2951 case ArraySubscriptExprClass:
2952 case MemberExprClass:
2953 case ConditionalOperatorClass:
2954 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002955 case CompoundLiteralExprClass:
2956 case ExtVectorElementExprClass:
2957 case DesignatedInitExprClass:
2958 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002959 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002960 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002961 case SubstNonTypeTemplateParmExprClass:
2962 case MaterializeTemporaryExprClass:
2963 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002964 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002965 case AsTypeExprClass:
2966 // These have a side-effect if any subexpression does.
2967 break;
2968
Richard Smitha33e4fe2012-08-07 05:18:29 +00002969 case UnaryOperatorClass:
2970 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002971 return true;
2972 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002973
2974 case BinaryOperatorClass:
2975 if (cast<BinaryOperator>(this)->isAssignmentOp())
2976 return true;
2977 break;
2978
Richard Smith0421ce72012-08-07 04:16:51 +00002979 case InitListExprClass:
2980 // FIXME: The children for an InitListExpr doesn't include the array filler.
2981 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2982 if (E->HasSideEffects(Ctx))
2983 return true;
2984 break;
2985
2986 case GenericSelectionExprClass:
2987 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2988 HasSideEffects(Ctx);
2989
2990 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002991 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002992
2993 case CXXDefaultArgExprClass:
2994 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2995
Richard Smith852c9db2013-04-20 22:23:05 +00002996 case CXXDefaultInitExprClass:
2997 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2998 return E->HasSideEffects(Ctx);
2999 // If we've not yet parsed the initializer, assume it has side-effects.
3000 return true;
3001
Richard Smith0421ce72012-08-07 04:16:51 +00003002 case CXXDynamicCastExprClass: {
3003 // A dynamic_cast expression has side-effects if it can throw.
3004 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3005 if (DCE->getTypeAsWritten()->isReferenceType() &&
3006 DCE->getCastKind() == CK_Dynamic)
3007 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003008 } // Fall through.
3009 case ImplicitCastExprClass:
3010 case CStyleCastExprClass:
3011 case CXXStaticCastExprClass:
3012 case CXXReinterpretCastExprClass:
3013 case CXXConstCastExprClass:
3014 case CXXFunctionalCastExprClass: {
3015 const CastExpr *CE = cast<CastExpr>(this);
3016 if (CE->getCastKind() == CK_LValueToRValue &&
3017 CE->getSubExpr()->getType().isVolatileQualified())
3018 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003019 break;
3020 }
3021
Richard Smithef8bf432012-08-13 20:08:14 +00003022 case CXXTypeidExprClass:
3023 // typeid might throw if its subexpression is potentially-evaluated, so has
3024 // side-effects in that case whether or not its subexpression does.
3025 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00003026
3027 case CXXConstructExprClass:
3028 case CXXTemporaryObjectExprClass: {
3029 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00003030 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00003031 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003032 // A trivial constructor does not add any side-effects of its own. Just look
3033 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00003034 break;
3035 }
3036
3037 case LambdaExprClass: {
3038 const LambdaExpr *LE = cast<LambdaExpr>(this);
3039 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3040 E = LE->capture_end(); I != E; ++I)
3041 if (I->getCaptureKind() == LCK_ByCopy)
3042 // FIXME: Only has a side-effect if the variable is volatile or if
3043 // the copy would invoke a non-trivial copy constructor.
3044 return true;
3045 return false;
3046 }
3047
3048 case PseudoObjectExprClass: {
3049 // Only look for side-effects in the semantic form, and look past
3050 // OpaqueValueExpr bindings in that form.
3051 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3052 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3053 E = PO->semantics_end();
3054 I != E; ++I) {
3055 const Expr *Subexpr = *I;
3056 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3057 Subexpr = OVE->getSourceExpr();
3058 if (Subexpr->HasSideEffects(Ctx))
3059 return true;
3060 }
3061 return false;
3062 }
3063
3064 case ObjCBoxedExprClass:
3065 case ObjCArrayLiteralClass:
3066 case ObjCDictionaryLiteralClass:
3067 case ObjCMessageExprClass:
3068 case ObjCSelectorExprClass:
3069 case ObjCProtocolExprClass:
3070 case ObjCPropertyRefExprClass:
3071 case ObjCIsaExprClass:
3072 case ObjCIndirectCopyRestoreExprClass:
3073 case ObjCSubscriptRefExprClass:
3074 case ObjCBridgedCastExprClass:
3075 // FIXME: Classify these cases better.
3076 return true;
3077 }
3078
3079 // Recurse to children.
3080 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3081 if (const Stmt *S = *SubStmts)
3082 if (cast<Expr>(S)->HasSideEffects(Ctx))
3083 return true;
3084
3085 return false;
3086}
3087
Douglas Gregor1be329d2012-02-23 07:33:15 +00003088namespace {
3089 /// \brief Look for a call to a non-trivial function within an expression.
3090 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3091 {
3092 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3093
3094 bool NonTrivial;
3095
3096 public:
3097 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003098 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003099
3100 bool hasNonTrivialCall() const { return NonTrivial; }
3101
3102 void VisitCallExpr(CallExpr *E) {
3103 if (CXXMethodDecl *Method
3104 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3105 if (Method->isTrivial()) {
3106 // Recurse to children of the call.
3107 Inherited::VisitStmt(E);
3108 return;
3109 }
3110 }
3111
3112 NonTrivial = true;
3113 }
3114
3115 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3116 if (E->getConstructor()->isTrivial()) {
3117 // Recurse to children of the call.
3118 Inherited::VisitStmt(E);
3119 return;
3120 }
3121
3122 NonTrivial = true;
3123 }
3124
3125 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3126 if (E->getTemporary()->getDestructor()->isTrivial()) {
3127 Inherited::VisitStmt(E);
3128 return;
3129 }
3130
3131 NonTrivial = true;
3132 }
3133 };
3134}
3135
3136bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3137 NonTrivialCallFinder Finder(Ctx);
3138 Finder.Visit(this);
3139 return Finder.hasNonTrivialCall();
3140}
3141
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003142/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3143/// pointer constant or not, as well as the specific kind of constant detected.
3144/// Null pointer constants can be integer constant expressions with the
3145/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3146/// (a GNU extension).
3147Expr::NullPointerConstantKind
3148Expr::isNullPointerConstant(ASTContext &Ctx,
3149 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003150 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003151 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003152 switch (NPC) {
3153 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003154 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003155 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003156 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003157 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003158 else
3159 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003160
Douglas Gregor56751b52009-09-25 04:25:58 +00003161 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003162 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003163 }
3164 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003165
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003166 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003167 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003168 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003169 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003170 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003171 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003172 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003173 Pointee->isVoidType() && // to void*
3174 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003175 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003176 }
Steve Naroffada7d422007-05-20 17:54:12 +00003177 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003178 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3179 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003180 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003181 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3182 // Accept ((void*)0) as a null pointer constant, as many other
3183 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003184 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003185 } else if (const GenericSelectionExpr *GE =
3186 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003187 if (GE->isResultDependent())
3188 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003189 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003190 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3191 if (CE->isConditionDependent())
3192 return NPCK_NotNull;
3193 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003194 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003195 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003196 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003197 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003198 } else if (const CXXDefaultInitExpr *DefaultInit
3199 = dyn_cast<CXXDefaultInitExpr>(this)) {
3200 // See through default initializer expressions.
3201 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003202 } else if (isa<GNUNullExpr>(this)) {
3203 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003204 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003205 } else if (const MaterializeTemporaryExpr *M
3206 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3207 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003208 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3209 if (const Expr *Source = OVE->getSourceExpr())
3210 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003211 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003212
Richard Smith89645bc2013-01-02 12:01:23 +00003213 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003214 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003215 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003216
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003217 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003218 if (!Ctx.getLangOpts().CPlusPlus11 &&
3219 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003220 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3221 const Expr *InitExpr = CLE->getInitializer();
3222 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3223 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3224 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003225 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003226 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003227 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003228 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003229
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003230 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003231 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3232 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003233 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003234 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003235 if (Lit && !Lit->getValue())
3236 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003237 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003238 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003239 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003240 // If we have an integer constant expression, we need to *evaluate* it and
3241 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003242 if (!isIntegerConstantExpr(Ctx))
3243 return NPCK_NotNull;
3244 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003245
David Blaikie1c7c8f72012-08-08 17:33:31 +00003246 if (EvaluateKnownConstInt(Ctx) != 0)
3247 return NPCK_NotNull;
3248
3249 if (isa<IntegerLiteral>(this))
3250 return NPCK_ZeroLiteral;
3251 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003252}
Steve Narofff7a5da12007-07-28 23:10:27 +00003253
John McCall34376a62010-12-04 03:47:34 +00003254/// \brief If this expression is an l-value for an Objective C
3255/// property, find the underlying property reference expression.
3256const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3257 const Expr *E = this;
3258 while (true) {
3259 assert((E->getValueKind() == VK_LValue &&
3260 E->getObjectKind() == OK_ObjCProperty) &&
3261 "expression is not a property reference");
3262 E = E->IgnoreParenCasts();
3263 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3264 if (BO->getOpcode() == BO_Comma) {
3265 E = BO->getRHS();
3266 continue;
3267 }
3268 }
3269
3270 break;
3271 }
3272
3273 return cast<ObjCPropertyRefExpr>(E);
3274}
3275
Anna Zaks97c7ce32012-10-01 20:34:04 +00003276bool Expr::isObjCSelfExpr() const {
3277 const Expr *E = IgnoreParenImpCasts();
3278
3279 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3280 if (!DRE)
3281 return false;
3282
3283 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3284 if (!Param)
3285 return false;
3286
3287 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3288 if (!M)
3289 return false;
3290
3291 return M->getSelfDecl() == Param;
3292}
3293
John McCalld25db7e2013-05-06 21:39:12 +00003294FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003295 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003296
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003297 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003298 if (ICE->getCastKind() == CK_LValueToRValue ||
3299 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003300 E = ICE->getSubExpr()->IgnoreParens();
3301 else
3302 break;
3303 }
3304
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003305 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003306 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003307 if (Field->isBitField())
3308 return Field;
3309
John McCalld25db7e2013-05-06 21:39:12 +00003310 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3311 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3312 if (Ivar->isBitField())
3313 return Ivar;
3314
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003315 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3316 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3317 if (Field->isBitField())
3318 return Field;
3319
Eli Friedman609ada22011-07-13 02:05:57 +00003320 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003321 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003322 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003323
Eli Friedman609ada22011-07-13 02:05:57 +00003324 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003325 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003326 }
3327
Richard Smith5b571672014-09-24 23:55:00 +00003328 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3329 if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3330 return UnOp->getSubExpr()->getSourceBitField();
3331
Craig Topper36250ad2014-05-12 05:36:57 +00003332 return nullptr;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003333}
3334
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003335bool Expr::refersToVectorElement() const {
3336 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003337
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003338 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003339 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003340 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003341 E = ICE->getSubExpr()->IgnoreParens();
3342 else
3343 break;
3344 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003345
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003346 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3347 return ASE->getBase()->getType()->isVectorType();
3348
3349 if (isa<ExtVectorElementExpr>(E))
3350 return true;
3351
3352 return false;
3353}
3354
Chris Lattnerb8211f62009-02-16 22:14:05 +00003355/// isArrow - Return true if the base expression is a pointer to vector,
3356/// return false if the base expression is a vector.
3357bool ExtVectorElementExpr::isArrow() const {
3358 return getBase()->getType()->isPointerType();
3359}
3360
Nate Begemance4d7fc2008-04-18 23:10:10 +00003361unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003362 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003363 return VT->getNumElements();
3364 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003365}
3366
Nate Begemanf322eab2008-05-09 06:41:27 +00003367/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003368bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003369 // FIXME: Refactor this code to an accessor on the AST node which returns the
3370 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003371 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003372
3373 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003374 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003375 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003376
Nate Begeman7e5185b2009-01-18 02:01:21 +00003377 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003378 if (Comp[0] == 's' || Comp[0] == 'S')
3379 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003380
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003381 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003382 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003383 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003384
Steve Naroff0d595ca2007-07-30 03:29:09 +00003385 return false;
3386}
Chris Lattner885b4952007-08-02 23:36:59 +00003387
Nate Begemanf322eab2008-05-09 06:41:27 +00003388/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003389void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003390 SmallVectorImpl<unsigned> &Elts) const {
3391 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003392 if (Comp[0] == 's' || Comp[0] == 'S')
3393 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003394
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003395 bool isHi = Comp == "hi";
3396 bool isLo = Comp == "lo";
3397 bool isEven = Comp == "even";
3398 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003399
Nate Begemanf322eab2008-05-09 06:41:27 +00003400 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3401 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003402
Nate Begemanf322eab2008-05-09 06:41:27 +00003403 if (isHi)
3404 Index = e + i;
3405 else if (isLo)
3406 Index = i;
3407 else if (isEven)
3408 Index = 2 * i;
3409 else if (isOdd)
3410 Index = 2 * i + 1;
3411 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003412 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003413
Nate Begemand3862152008-05-13 21:03:02 +00003414 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003415 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003416}
3417
Douglas Gregor9a129192010-04-21 00:45:42 +00003418ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003419 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003420 SourceLocation LBracLoc,
3421 SourceLocation SuperLoc,
3422 bool IsInstanceSuper,
3423 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003424 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003425 ArrayRef<SourceLocation> SelLocs,
3426 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003427 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003428 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003429 SourceLocation RBracLoc,
3430 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003431 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003432 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003433 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003434 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003435 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3436 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003437 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Craig Topper36250ad2014-05-12 05:36:57 +00003438 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3439 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3440 RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003441{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003442 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003443 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003444}
3445
Douglas Gregor9a129192010-04-21 00:45:42 +00003446ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003447 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003448 SourceLocation LBracLoc,
3449 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003450 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003451 ArrayRef<SourceLocation> SelLocs,
3452 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003453 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003454 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003455 SourceLocation RBracLoc,
3456 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003457 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003458 T->isDependentType(), T->isInstantiationDependentType(),
3459 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003460 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3461 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003462 Kind(Class),
Craig Topper36250ad2014-05-12 05:36:57 +00003463 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3464 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003465{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003466 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003467 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003468}
3469
Douglas Gregor9a129192010-04-21 00:45:42 +00003470ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003471 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003472 SourceLocation LBracLoc,
3473 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003474 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003475 ArrayRef<SourceLocation> SelLocs,
3476 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003477 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003478 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003479 SourceLocation RBracLoc,
3480 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003481 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003482 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003483 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003484 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003485 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3486 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003487 Kind(Instance),
Craig Topper36250ad2014-05-12 05:36:57 +00003488 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3489 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003490{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003491 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003492 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003493}
3494
3495void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3496 ArrayRef<SourceLocation> SelLocs,
3497 SelectorLocationsKind SelLocsK) {
3498 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003499 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003500 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003501 if (Args[I]->isTypeDependent())
3502 ExprBits.TypeDependent = true;
3503 if (Args[I]->isValueDependent())
3504 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003505 if (Args[I]->isInstantiationDependent())
3506 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003507 if (Args[I]->containsUnexpandedParameterPack())
3508 ExprBits.ContainsUnexpandedParameterPack = true;
3509
3510 MyArgs[I] = Args[I];
3511 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003512
Benjamin Kramer2325b242012-02-20 00:20:48 +00003513 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003514 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003515 if (SelLocsK == SelLoc_NonStandard)
3516 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3517 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003518}
3519
Craig Topperce7167c2013-08-22 04:58:56 +00003520ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003521 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003522 SourceLocation LBracLoc,
3523 SourceLocation SuperLoc,
3524 bool IsInstanceSuper,
3525 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003526 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003527 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003528 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003529 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003530 SourceLocation RBracLoc,
3531 bool isImplicit) {
3532 assert((!SelLocs.empty() || isImplicit) &&
3533 "No selector locs for non-implicit message");
3534 ObjCMessageExpr *Mem;
3535 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3536 if (isImplicit)
3537 Mem = alloc(Context, Args.size(), 0);
3538 else
3539 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003540 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003541 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003542 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003543}
3544
Craig Topperce7167c2013-08-22 04:58:56 +00003545ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003546 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003547 SourceLocation LBracLoc,
3548 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003549 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003550 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003551 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003552 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003553 SourceLocation RBracLoc,
3554 bool isImplicit) {
3555 assert((!SelLocs.empty() || isImplicit) &&
3556 "No selector locs for non-implicit message");
3557 ObjCMessageExpr *Mem;
3558 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3559 if (isImplicit)
3560 Mem = alloc(Context, Args.size(), 0);
3561 else
3562 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003563 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003564 SelLocs, SelLocsK, Method, Args, RBracLoc,
3565 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003566}
3567
Craig Topperce7167c2013-08-22 04:58:56 +00003568ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003569 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003570 SourceLocation LBracLoc,
3571 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003572 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003573 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003574 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003575 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003576 SourceLocation RBracLoc,
3577 bool isImplicit) {
3578 assert((!SelLocs.empty() || isImplicit) &&
3579 "No selector locs for non-implicit message");
3580 ObjCMessageExpr *Mem;
3581 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3582 if (isImplicit)
3583 Mem = alloc(Context, Args.size(), 0);
3584 else
3585 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003586 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003587 SelLocs, SelLocsK, Method, Args, RBracLoc,
3588 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003589}
3590
Craig Topperce7167c2013-08-22 04:58:56 +00003591ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003592 unsigned NumArgs,
3593 unsigned NumStoredSelLocs) {
3594 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003595 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3596}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003597
Craig Topperce7167c2013-08-22 04:58:56 +00003598ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003599 ArrayRef<Expr *> Args,
3600 SourceLocation RBraceLoc,
3601 ArrayRef<SourceLocation> SelLocs,
3602 Selector Sel,
3603 SelectorLocationsKind &SelLocsK) {
3604 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3605 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3606 : 0;
3607 return alloc(C, Args.size(), NumStoredSelLocs);
3608}
3609
Craig Topperce7167c2013-08-22 04:58:56 +00003610ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003611 unsigned NumArgs,
3612 unsigned NumStoredSelLocs) {
3613 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3614 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3615 return (ObjCMessageExpr *)C.Allocate(Size,
3616 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3617}
3618
3619void ObjCMessageExpr::getSelectorLocs(
3620 SmallVectorImpl<SourceLocation> &SelLocs) const {
3621 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3622 SelLocs.push_back(getSelectorLoc(i));
3623}
3624
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003625SourceRange ObjCMessageExpr::getReceiverRange() const {
3626 switch (getReceiverKind()) {
3627 case Instance:
3628 return getInstanceReceiver()->getSourceRange();
3629
3630 case Class:
3631 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3632
3633 case SuperInstance:
3634 case SuperClass:
3635 return getSuperLoc();
3636 }
3637
David Blaikiee4d798f2012-01-20 21:50:17 +00003638 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003639}
3640
Douglas Gregor9a129192010-04-21 00:45:42 +00003641Selector ObjCMessageExpr::getSelector() const {
3642 if (HasMethod)
3643 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3644 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003645 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003646}
3647
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003648QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003649 switch (getReceiverKind()) {
3650 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003651 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003652 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003653 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003654 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003655 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003656 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003657 }
3658
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003659 llvm_unreachable("unexpected receiver kind");
3660}
3661
3662ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3663 QualType T = getReceiverType();
3664
3665 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3666 return Ptr->getInterfaceDecl();
3667
3668 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3669 return Ty->getInterface();
3670
Craig Topper36250ad2014-05-12 05:36:57 +00003671 return nullptr;
Ted Kremenek2c809302010-02-11 22:41:21 +00003672}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003673
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003674StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003675 switch (getBridgeKind()) {
3676 case OBC_Bridge:
3677 return "__bridge";
3678 case OBC_BridgeTransfer:
3679 return "__bridge_transfer";
3680 case OBC_BridgeRetained:
3681 return "__bridge_retained";
3682 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003683
3684 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003685}
3686
Craig Topper37932912013-08-18 10:09:15 +00003687ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003688 QualType Type, SourceLocation BLoc,
3689 SourceLocation RP)
3690 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3691 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003692 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003693 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003694 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003695{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003696 SubExprs = new (C) Stmt*[args.size()];
3697 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003698 if (args[i]->isTypeDependent())
3699 ExprBits.TypeDependent = true;
3700 if (args[i]->isValueDependent())
3701 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003702 if (args[i]->isInstantiationDependent())
3703 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003704 if (args[i]->containsUnexpandedParameterPack())
3705 ExprBits.ContainsUnexpandedParameterPack = true;
3706
3707 SubExprs[i] = args[i];
3708 }
3709}
3710
Craig Topper37932912013-08-18 10:09:15 +00003711void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003712 if (SubExprs) C.Deallocate(SubExprs);
3713
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003714 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003715 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003716 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003717}
Nate Begeman48745922009-08-12 02:28:50 +00003718
Craig Topper37932912013-08-18 10:09:15 +00003719GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003720 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003721 ArrayRef<TypeSourceInfo*> AssocTypes,
3722 ArrayRef<Expr*> AssocExprs,
3723 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003724 SourceLocation RParenLoc,
3725 bool ContainsUnexpandedParameterPack,
3726 unsigned ResultIndex)
3727 : Expr(GenericSelectionExprClass,
3728 AssocExprs[ResultIndex]->getType(),
3729 AssocExprs[ResultIndex]->getValueKind(),
3730 AssocExprs[ResultIndex]->getObjectKind(),
3731 AssocExprs[ResultIndex]->isTypeDependent(),
3732 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003733 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003734 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003735 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3736 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3737 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3738 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003739 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003740 assert(AssocTypes.size() == AssocExprs.size());
3741 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3742 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003743}
3744
Craig Topper37932912013-08-18 10:09:15 +00003745GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003746 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003747 ArrayRef<TypeSourceInfo*> AssocTypes,
3748 ArrayRef<Expr*> AssocExprs,
3749 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003750 SourceLocation RParenLoc,
3751 bool ContainsUnexpandedParameterPack)
3752 : Expr(GenericSelectionExprClass,
3753 Context.DependentTy,
3754 VK_RValue,
3755 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003756 /*isTypeDependent=*/true,
3757 /*isValueDependent=*/true,
3758 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003759 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003760 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3761 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3762 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3763 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003764 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003765 assert(AssocTypes.size() == AssocExprs.size());
3766 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3767 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003768}
3769
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003770//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003771// DesignatedInitExpr
3772//===----------------------------------------------------------------------===//
3773
Chandler Carruth631abd92011-06-16 06:47:06 +00003774IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003775 assert(Kind == FieldDesignator && "Only valid on a field designator");
3776 if (Field.NameOrField & 0x01)
3777 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3778 else
3779 return getField()->getIdentifier();
3780}
3781
Craig Topper37932912013-08-18 10:09:15 +00003782DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003783 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003784 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003785 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003786 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003787 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003788 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003789 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003790 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003791 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003792 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003793 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003794 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003795 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003796 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003797
3798 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003799 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003800 *Child++ = Init;
3801
3802 // Copy the designators and their subexpressions, computing
3803 // value-dependence along the way.
3804 unsigned IndexIdx = 0;
3805 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003806 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003807
3808 if (this->Designators[I].isArrayDesignator()) {
3809 // Compute type- and value-dependence.
3810 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003811 if (Index->isTypeDependent() || Index->isValueDependent())
3812 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003813 if (Index->isInstantiationDependent())
3814 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003815 // Propagate unexpanded parameter packs.
3816 if (Index->containsUnexpandedParameterPack())
3817 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003818
3819 // Copy the index expressions into permanent storage.
3820 *Child++ = IndexExprs[IndexIdx++];
3821 } else if (this->Designators[I].isArrayRangeDesignator()) {
3822 // Compute type- and value-dependence.
3823 Expr *Start = IndexExprs[IndexIdx];
3824 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003825 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003826 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003827 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003828 ExprBits.InstantiationDependent = true;
3829 } else if (Start->isInstantiationDependent() ||
3830 End->isInstantiationDependent()) {
3831 ExprBits.InstantiationDependent = true;
3832 }
3833
Douglas Gregora6e053e2010-12-15 01:34:56 +00003834 // Propagate unexpanded parameter packs.
3835 if (Start->containsUnexpandedParameterPack() ||
3836 End->containsUnexpandedParameterPack())
3837 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003838
3839 // Copy the start/end expressions into permanent storage.
3840 *Child++ = IndexExprs[IndexIdx++];
3841 *Child++ = IndexExprs[IndexIdx++];
3842 }
3843 }
3844
Benjamin Kramerc215e762012-08-24 11:54:20 +00003845 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003846}
3847
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003848DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003849DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003850 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003851 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003852 SourceLocation ColonOrEqualLoc,
3853 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003854 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003855 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003856 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003857 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003858 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003859}
3860
Craig Topper37932912013-08-18 10:09:15 +00003861DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003862 unsigned NumIndexExprs) {
3863 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3864 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3865 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3866}
3867
Craig Topper37932912013-08-18 10:09:15 +00003868void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003869 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003870 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003871 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003872 NumDesignators = NumDesigs;
3873 for (unsigned I = 0; I != NumDesigs; ++I)
3874 Designators[I] = Desigs[I];
3875}
3876
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003877SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3878 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3879 if (size() == 1)
3880 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003881 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3882 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003883}
3884
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003885SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003886 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003887 Designator &First =
3888 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003889 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003890 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003891 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3892 else
3893 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3894 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003895 StartLoc =
3896 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003897 return StartLoc;
3898}
3899
3900SourceLocation DesignatedInitExpr::getLocEnd() const {
3901 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003902}
3903
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003904Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003905 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003906 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003907 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3908}
3909
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003910Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003911 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003912 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003913 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003914 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3915}
3916
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003917Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003918 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003919 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003920 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003921 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3922}
3923
Douglas Gregord5846a12009-04-15 06:41:24 +00003924/// \brief Replaces the designator at index @p Idx with the series
3925/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003926void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003927 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003928 const Designator *Last) {
3929 unsigned NumNewDesignators = Last - First;
3930 if (NumNewDesignators == 0) {
3931 std::copy_backward(Designators + Idx + 1,
3932 Designators + NumDesignators,
3933 Designators + Idx);
3934 --NumNewDesignators;
3935 return;
3936 } else if (NumNewDesignators == 1) {
3937 Designators[Idx] = *First;
3938 return;
3939 }
3940
Mike Stump11289f42009-09-09 15:08:12 +00003941 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003942 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003943 std::copy(Designators, Designators + Idx, NewDesignators);
3944 std::copy(First, Last, NewDesignators + Idx);
3945 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3946 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003947 Designators = NewDesignators;
3948 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3949}
3950
Craig Topper37932912013-08-18 10:09:15 +00003951ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003952 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003953 SourceLocation rparenloc)
3954 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003955 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003956 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3957 Exprs = new (C) Stmt*[exprs.size()];
3958 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003959 if (exprs[i]->isTypeDependent())
3960 ExprBits.TypeDependent = true;
3961 if (exprs[i]->isValueDependent())
3962 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003963 if (exprs[i]->isInstantiationDependent())
3964 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003965 if (exprs[i]->containsUnexpandedParameterPack())
3966 ExprBits.ContainsUnexpandedParameterPack = true;
3967
Nate Begeman5ec4b312009-08-10 23:49:36 +00003968 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003969 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003970}
3971
John McCall1bf58462011-02-16 08:02:54 +00003972const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3973 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3974 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003975 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3976 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003977 e = cast<CXXConstructExpr>(e)->getArg(0);
3978 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3979 e = ice->getSubExpr();
3980 return cast<OpaqueValueExpr>(e);
3981}
3982
Craig Topper37932912013-08-18 10:09:15 +00003983PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3984 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003985 unsigned numSemanticExprs) {
3986 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3987 (1 + numSemanticExprs) * sizeof(Expr*),
3988 llvm::alignOf<PseudoObjectExpr>());
3989 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3990}
3991
3992PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3993 : Expr(PseudoObjectExprClass, shell) {
3994 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3995}
3996
Craig Topper37932912013-08-18 10:09:15 +00003997PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003998 ArrayRef<Expr*> semantics,
3999 unsigned resultIndex) {
4000 assert(syntax && "no syntactic expression!");
4001 assert(semantics.size() && "no semantic expressions!");
4002
4003 QualType type;
4004 ExprValueKind VK;
4005 if (resultIndex == NoResult) {
4006 type = C.VoidTy;
4007 VK = VK_RValue;
4008 } else {
4009 assert(resultIndex < semantics.size());
4010 type = semantics[resultIndex]->getType();
4011 VK = semantics[resultIndex]->getValueKind();
4012 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4013 }
4014
4015 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4016 (1 + semantics.size()) * sizeof(Expr*),
4017 llvm::alignOf<PseudoObjectExpr>());
4018 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4019 resultIndex);
4020}
4021
4022PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4023 Expr *syntax, ArrayRef<Expr*> semantics,
4024 unsigned resultIndex)
4025 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4026 /*filled in at end of ctor*/ false, false, false, false) {
4027 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4028 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4029
4030 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4031 Expr *E = (i == 0 ? syntax : semantics[i-1]);
4032 getSubExprsBuffer()[i] = E;
4033
4034 if (E->isTypeDependent())
4035 ExprBits.TypeDependent = true;
4036 if (E->isValueDependent())
4037 ExprBits.ValueDependent = true;
4038 if (E->isInstantiationDependent())
4039 ExprBits.InstantiationDependent = true;
4040 if (E->containsUnexpandedParameterPack())
4041 ExprBits.ContainsUnexpandedParameterPack = true;
4042
4043 if (isa<OpaqueValueExpr>(E))
Craig Topper36250ad2014-05-12 05:36:57 +00004044 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCallfe96e0b2011-11-06 09:01:30 +00004045 "opaque-value semantic expressions for pseudo-object "
4046 "operations must have sources");
4047 }
4048}
4049
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004050//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00004051// ExprIterator.
4052//===----------------------------------------------------------------------===//
4053
4054Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
4055Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
4056Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
4057const Expr* ConstExprIterator::operator[](size_t idx) const {
4058 return cast<Expr>(I[idx]);
4059}
4060const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
4061const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4062
4063//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00004064// Child Iterators for iterating over subexpressions/substatements
4065//===----------------------------------------------------------------------===//
4066
Peter Collingbournee190dee2011-03-11 19:24:49 +00004067// UnaryExprOrTypeTraitExpr
4068Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00004069 // If this is of a type and the type is a VLA type (and not a typedef), the
4070 // size expression of the VLA needs to be treated as an executable expression.
4071 // Why isn't this weirdness documented better in StmtIterator?
4072 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00004073 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00004074 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00004075 return child_range(child_iterator(T), child_iterator());
4076 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00004077 }
John McCallbd066782011-02-09 08:16:59 +00004078 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00004079}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004080
Steve Naroffd54978b2007-09-18 23:55:05 +00004081// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004082Stmt::child_range ObjCMessageExpr::children() {
4083 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004084 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004085 begin = reinterpret_cast<Stmt **>(this + 1);
4086 else
4087 begin = reinterpret_cast<Stmt **>(getArgs());
4088 return child_range(begin,
4089 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004090}
4091
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004092ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004093 QualType T, ObjCMethodDecl *Method,
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004094 ObjCMethodDecl *AllocMethod,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004095 SourceRange SR)
4096 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4097 false, false, false, false),
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004098 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method),
4099 ArrayAllocMethod(AllocMethod)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004100{
4101 Expr **SaveElements = getElements();
4102 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4103 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4104 ExprBits.ValueDependent = true;
4105 if (Elements[I]->isInstantiationDependent())
4106 ExprBits.InstantiationDependent = true;
4107 if (Elements[I]->containsUnexpandedParameterPack())
4108 ExprBits.ContainsUnexpandedParameterPack = true;
4109
4110 SaveElements[I] = Elements[I];
4111 }
4112}
4113
Craig Topperce7167c2013-08-22 04:58:56 +00004114ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004115 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004116 QualType T, ObjCMethodDecl * Method,
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004117 ObjCMethodDecl *allocMethod,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004118 SourceRange SR) {
4119 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4120 + Elements.size() * sizeof(Expr *));
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004121 return new (Mem) ObjCArrayLiteral(Elements, T, Method, allocMethod, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004122}
4123
Craig Topperce7167c2013-08-22 04:58:56 +00004124ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004125 unsigned NumElements) {
4126
4127 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4128 + NumElements * sizeof(Expr *));
4129 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4130}
4131
4132ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4133 ArrayRef<ObjCDictionaryElement> VK,
4134 bool HasPackExpansions,
4135 QualType T, ObjCMethodDecl *method,
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004136 ObjCMethodDecl *allocMethod,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004137 SourceRange SR)
4138 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4139 false, false),
4140 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004141 DictWithObjectsMethod(method),
4142 DictAllocMethod(allocMethod)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004143{
4144 KeyValuePair *KeyValues = getKeyValues();
4145 ExpansionData *Expansions = getExpansionData();
4146 for (unsigned I = 0; I < NumElements; I++) {
4147 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4148 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4149 ExprBits.ValueDependent = true;
4150 if (VK[I].Key->isInstantiationDependent() ||
4151 VK[I].Value->isInstantiationDependent())
4152 ExprBits.InstantiationDependent = true;
4153 if (VK[I].EllipsisLoc.isInvalid() &&
4154 (VK[I].Key->containsUnexpandedParameterPack() ||
4155 VK[I].Value->containsUnexpandedParameterPack()))
4156 ExprBits.ContainsUnexpandedParameterPack = true;
4157
4158 KeyValues[I].Key = VK[I].Key;
4159 KeyValues[I].Value = VK[I].Value;
4160 if (Expansions) {
4161 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4162 if (VK[I].NumExpansions)
4163 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4164 else
4165 Expansions[I].NumExpansionsPlusOne = 0;
4166 }
4167 }
4168}
4169
4170ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004171ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004172 ArrayRef<ObjCDictionaryElement> VK,
4173 bool HasPackExpansions,
4174 QualType T, ObjCMethodDecl *method,
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004175 ObjCMethodDecl *allocMethod,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004176 SourceRange SR) {
4177 unsigned ExpansionsSize = 0;
4178 if (HasPackExpansions)
4179 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4180
4181 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4182 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
Fariborz Jahanian413297c2014-08-06 18:13:46 +00004183 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T,
4184 method, allocMethod, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004185}
4186
4187ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004188ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004189 bool HasPackExpansions) {
4190 unsigned ExpansionsSize = 0;
4191 if (HasPackExpansions)
4192 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4193 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4194 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4195 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4196 HasPackExpansions);
4197}
4198
Craig Topperce7167c2013-08-22 04:58:56 +00004199ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004200 Expr *base,
4201 Expr *key, QualType T,
4202 ObjCMethodDecl *getMethod,
4203 ObjCMethodDecl *setMethod,
4204 SourceLocation RB) {
4205 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4206 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4207 OK_ObjCSubscript,
4208 getMethod, setMethod, RB);
4209}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004210
Benjamin Kramerc215e762012-08-24 11:54:20 +00004211AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004212 QualType t, AtomicOp op, SourceLocation RP)
4213 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4214 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004215 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004216{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004217 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4218 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004219 if (args[i]->isTypeDependent())
4220 ExprBits.TypeDependent = true;
4221 if (args[i]->isValueDependent())
4222 ExprBits.ValueDependent = true;
4223 if (args[i]->isInstantiationDependent())
4224 ExprBits.InstantiationDependent = true;
4225 if (args[i]->containsUnexpandedParameterPack())
4226 ExprBits.ContainsUnexpandedParameterPack = true;
4227
4228 SubExprs[i] = args[i];
4229 }
4230}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004231
4232unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4233 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004234 case AO__c11_atomic_init:
4235 case AO__c11_atomic_load:
4236 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004237 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004238
4239 case AO__c11_atomic_store:
4240 case AO__c11_atomic_exchange:
4241 case AO__atomic_load:
4242 case AO__atomic_store:
4243 case AO__atomic_store_n:
4244 case AO__atomic_exchange_n:
4245 case AO__c11_atomic_fetch_add:
4246 case AO__c11_atomic_fetch_sub:
4247 case AO__c11_atomic_fetch_and:
4248 case AO__c11_atomic_fetch_or:
4249 case AO__c11_atomic_fetch_xor:
4250 case AO__atomic_fetch_add:
4251 case AO__atomic_fetch_sub:
4252 case AO__atomic_fetch_and:
4253 case AO__atomic_fetch_or:
4254 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004255 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004256 case AO__atomic_add_fetch:
4257 case AO__atomic_sub_fetch:
4258 case AO__atomic_and_fetch:
4259 case AO__atomic_or_fetch:
4260 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004261 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004262 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004263
4264 case AO__atomic_exchange:
4265 return 4;
4266
4267 case AO__c11_atomic_compare_exchange_strong:
4268 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004269 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004270
4271 case AO__atomic_compare_exchange:
4272 case AO__atomic_compare_exchange_n:
4273 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004274 }
4275 llvm_unreachable("unknown atomic op");
4276}