blob: 36f4139f835216e7e827604370a3d7751917f688 [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 }
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000191}
John McCallbd066782011-02-09 08:16:59 +0000192
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 //
Richard Smithcfaa5a32014-10-17 02:46:42 +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:
Richard Smithcfaa5a32014-10-17 02:46:42 +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);
Richard Smithcfaa5a32014-10-17 02:46:42 +0000312
313 ExprBits.TypeDependent |= TypeDependent;
314 ExprBits.ValueDependent |= ValueDependent;
315 ExprBits.InstantiationDependent |= InstantiationDependent;
316
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000317 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000318 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000319 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000320}
321
Craig Topperce7167c2013-08-22 04:58:56 +0000322DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000323 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000324 SourceLocation TemplateKWLoc,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000325 ValueDecl *D, bool RefersToEnclosingVariableOrCapture,
John McCall113bee02012-03-10 09:33:50 +0000326 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000327 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000328 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000329 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000330 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000331 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
332 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Richard Smithcfaa5a32014-10-17 02:46:42 +0000333 if (QualifierLoc) {
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000334 getInternalQualifierLoc() = QualifierLoc;
Richard Smithcfaa5a32014-10-17 02:46:42 +0000335 auto *NNS = QualifierLoc.getNestedNameSpecifier();
336 if (NNS->isInstantiationDependent())
337 ExprBits.InstantiationDependent = true;
338 if (NNS->containsUnexpandedParameterPack())
339 ExprBits.ContainsUnexpandedParameterPack = true;
340 }
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000341 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
342 if (FoundD)
343 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000344 DeclRefExprBits.HasTemplateKWAndArgsInfo
345 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000346 DeclRefExprBits.RefersToEnclosingVariableOrCapture =
347 RefersToEnclosingVariableOrCapture;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000348 if (TemplateArgs) {
349 bool Dependent = false;
350 bool InstantiationDependent = false;
351 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000352 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
353 Dependent,
354 InstantiationDependent,
355 ContainsUnexpandedParameterPack);
Richard Smithcfaa5a32014-10-17 02:46:42 +0000356 assert(!Dependent && "built a DeclRefExpr with dependent template args");
357 ExprBits.InstantiationDependent |= InstantiationDependent;
358 ExprBits.ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000361 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000362 DeclRefExprBits.HadMultipleCandidates = 0;
363
Daniel Dunbar9d355812012-03-09 01:51:51 +0000364 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000365}
366
Craig Topperce7167c2013-08-22 04:58:56 +0000367DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000368 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000370 ValueDecl *D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000371 bool RefersToEnclosingVariableOrCapture,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000372 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000373 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000374 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000375 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000376 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000378 RefersToEnclosingVariableOrCapture,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000379 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000380 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000381}
382
Craig Topperce7167c2013-08-22 04:58:56 +0000383DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000384 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000386 ValueDecl *D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000387 bool RefersToEnclosingVariableOrCapture,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000388 const DeclarationNameInfo &NameInfo,
389 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000390 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000391 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000392 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000393 // Filter out cases where the found Decl is the same as the value refenenced.
394 if (D == FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +0000395 FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000396
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000397 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000398 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000399 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000400 if (FoundD)
401 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000402 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000403 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
404 else if (TemplateKWLoc.isValid())
405 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000406
Chris Lattner5c0b4052010-10-30 05:14:06 +0000407 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000408 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000409 RefersToEnclosingVariableOrCapture,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000410 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000411}
412
Craig Topperce7167c2013-08-22 04:58:56 +0000413DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000414 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000415 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000416 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000417 unsigned NumTemplateArgs) {
418 std::size_t Size = sizeof(DeclRefExpr);
419 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000420 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000421 if (HasFoundDecl)
422 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000423 if (HasTemplateKWAndArgsInfo)
424 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000425
Chris Lattner5c0b4052010-10-30 05:14:06 +0000426 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000427 return new (Mem) DeclRefExpr(EmptyShell());
428}
429
Daniel Dunbarb507f272012-03-09 15:39:15 +0000430SourceLocation DeclRefExpr::getLocStart() const {
431 if (hasQualifier())
432 return getQualifierLoc().getBeginLoc();
433 return getNameInfo().getLocStart();
434}
435SourceLocation DeclRefExpr::getLocEnd() const {
436 if (hasExplicitTemplateArgs())
437 return getRAngleLoc();
438 return getNameInfo().getLocEnd();
439}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000440
Alexey Bataevec474782014-10-09 08:45:04 +0000441PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
442 StringLiteral *SL)
443 : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
444 FNTy->isDependentType(), FNTy->isDependentType(),
445 FNTy->isInstantiationDependentType(),
446 /*ContainsUnexpandedParameterPack=*/false),
447 Loc(L), Type(IT), FnName(SL) {}
448
449StringLiteral *PredefinedExpr::getFunctionName() {
Alexey Bataev769562a2014-10-10 18:58:13 +0000450 return cast_or_null<StringLiteral>(FnName);
Alexey Bataevec474782014-10-09 08:45:04 +0000451}
452
453StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
454 switch (IT) {
455 case Func:
456 return "__func__";
457 case Function:
458 return "__FUNCTION__";
459 case FuncDName:
460 return "__FUNCDNAME__";
461 case LFunction:
462 return "L__FUNCTION__";
463 case PrettyFunction:
464 return "__PRETTY_FUNCTION__";
465 case FuncSig:
466 return "__FUNCSIG__";
467 case PrettyFunctionNoVirtual:
468 break;
469 }
470 llvm_unreachable("Unknown ident type for PredefinedExpr");
471}
472
Anders Carlsson2fb08242009-09-08 18:24:21 +0000473// FIXME: Maybe this should use DeclPrinter with a special "print predefined
474// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000475std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
476 ASTContext &Context = CurrentDecl->getASTContext();
477
David Majnemerbed356a2013-11-06 23:31:56 +0000478 if (IT == PredefinedExpr::FuncDName) {
479 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000480 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000481 MC.reset(Context.createMangleContext());
482
483 if (MC->shouldMangleDeclName(ND)) {
484 SmallString<256> Buffer;
485 llvm::raw_svector_ostream Out(Buffer);
486 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
487 MC->mangleCXXCtor(CD, Ctor_Base, Out);
488 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
489 MC->mangleCXXDtor(DD, Dtor_Base, Out);
490 else
491 MC->mangleName(ND, Out);
492
493 Out.flush();
494 if (!Buffer.empty() && Buffer.front() == '\01')
495 return Buffer.substr(1);
496 return Buffer.str();
497 } else
498 return ND->getIdentifier()->getName();
499 }
500 return "";
501 }
Alexey Bataevec474782014-10-09 08:45:04 +0000502 if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
503 std::unique_ptr<MangleContext> MC;
504 MC.reset(Context.createMangleContext());
505 SmallString<256> Buffer;
506 llvm::raw_svector_ostream Out(Buffer);
507 auto DC = CurrentDecl->getDeclContext();
508 if (DC->isFileContext())
509 MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
510 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
511 MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
512 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
513 MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
514 else
515 MC->mangleBlock(DC, BD, Out);
516 return Out.str();
517 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000518 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Reid Kleckner52eddda2014-04-08 18:13:24 +0000519 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000520 return FD->getNameAsString();
521
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000522 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000523 llvm::raw_svector_ostream Out(Name);
524
525 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000526 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000527 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000528 if (MD->isStatic())
529 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000530 }
531
David Blaikiebbafb8a2012-03-11 07:00:24 +0000532 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000533 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000534 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000535
Douglas Gregor11a434a2012-04-10 20:14:15 +0000536 const FunctionDecl *Decl = FD;
537 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
538 Decl = Pattern;
539 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Craig Topper36250ad2014-05-12 05:36:57 +0000540 const FunctionProtoType *FT = nullptr;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000541 if (FD->hasWrittenPrototype())
542 FT = dyn_cast<FunctionProtoType>(AFT);
543
Reid Kleckner52eddda2014-04-08 18:13:24 +0000544 if (IT == FuncSig) {
545 switch (FT->getCallConv()) {
546 case CC_C: POut << "__cdecl "; break;
547 case CC_X86StdCall: POut << "__stdcall "; break;
548 case CC_X86FastCall: POut << "__fastcall "; break;
549 case CC_X86ThisCall: POut << "__thiscall "; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +0000550 case CC_X86VectorCall: POut << "__vectorcall "; break;
Reid Kleckner52eddda2014-04-08 18:13:24 +0000551 // Only bother printing the conventions that MSVC knows about.
552 default: break;
553 }
554 }
555
556 FD->printQualifiedName(POut, Policy);
557
Douglas Gregor11a434a2012-04-10 20:14:15 +0000558 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000559 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000560 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000561 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000562 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000563 }
564
565 if (FT->isVariadic()) {
566 if (FD->getNumParams()) POut << ", ";
567 POut << "...";
568 }
569 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000570 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000571
Sam Weinig4e83bd22009-12-27 01:38:20 +0000572 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000573 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000574 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000575 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000576 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000577 POut << " volatile";
578 RefQualifierKind Ref = MD->getRefQualifier();
579 if (Ref == RQ_LValue)
580 POut << " &";
581 else if (Ref == RQ_RValue)
582 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000583 }
584
Douglas Gregor11a434a2012-04-10 20:14:15 +0000585 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
586 SpecsTy Specs;
587 const DeclContext *Ctx = FD->getDeclContext();
588 while (Ctx && isa<NamedDecl>(Ctx)) {
589 const ClassTemplateSpecializationDecl *Spec
590 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
591 if (Spec && !Spec->isExplicitSpecialization())
592 Specs.push_back(Spec);
593 Ctx = Ctx->getParent();
594 }
595
596 std::string TemplateParams;
597 llvm::raw_string_ostream TOut(TemplateParams);
598 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
599 I != E; ++I) {
600 const TemplateParameterList *Params
601 = (*I)->getSpecializedTemplate()->getTemplateParameters();
602 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
603 assert(Params->size() == Args.size());
604 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
605 StringRef Param = Params->getParam(i)->getName();
606 if (Param.empty()) continue;
607 TOut << Param << " = ";
608 Args.get(i).print(Policy, TOut);
609 TOut << ", ";
610 }
611 }
612
613 FunctionTemplateSpecializationInfo *FSI
614 = FD->getTemplateSpecializationInfo();
615 if (FSI && !FSI->isExplicitSpecialization()) {
616 const TemplateParameterList* Params
617 = FSI->getTemplate()->getTemplateParameters();
618 const TemplateArgumentList* Args = FSI->TemplateArguments;
619 assert(Params->size() == Args->size());
620 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
621 StringRef Param = Params->getParam(i)->getName();
622 if (Param.empty()) continue;
623 TOut << Param << " = ";
624 Args->get(i).print(Policy, TOut);
625 TOut << ", ";
626 }
627 }
628
629 TOut.flush();
630 if (!TemplateParams.empty()) {
631 // remove the trailing comma and space
632 TemplateParams.resize(TemplateParams.size() - 2);
633 POut << " [" << TemplateParams << "]";
634 }
635
636 POut.flush();
637
Benjamin Kramer90f54222013-08-21 11:45:27 +0000638 // Print "auto" for all deduced return types. This includes C++1y return
639 // type deduction and lambdas. For trailing return types resolve the
640 // decltype expression. Otherwise print the real type when this is
641 // not a constructor or destructor.
Alexey Bataevec474782014-10-09 08:45:04 +0000642 if (isa<CXXMethodDecl>(FD) &&
643 cast<CXXMethodDecl>(FD)->getParent()->isLambda())
Benjamin Kramer90f54222013-08-21 11:45:27 +0000644 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000645 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
646 FT->getReturnType()
647 ->getAs<DecltypeType>()
648 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000649 .getAsStringInternal(Proto, Policy);
650 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000651 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000652
653 Out << Proto;
654
655 Out.flush();
656 return Name.str().str();
657 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000658 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
659 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
660 // Skip to its enclosing function or method, but not its enclosing
661 // CapturedDecl.
662 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
663 const Decl *D = Decl::castFromDeclContext(DC);
664 return ComputeName(IT, D);
665 }
666 llvm_unreachable("CapturedDecl not inside a function or method");
667 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000668 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000669 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000670 llvm::raw_svector_ostream Out(Name);
671 Out << (MD->isInstanceMethod() ? '-' : '+');
672 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000673
674 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
675 // a null check to avoid a crash.
676 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000677 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000678
Anders Carlsson2fb08242009-09-08 18:24:21 +0000679 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000680 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000681 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000682
Anders Carlsson2fb08242009-09-08 18:24:21 +0000683 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000684 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000685 Out << ']';
686
687 Out.flush();
688 return Name.str().str();
689 }
690 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
691 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
692 return "top level";
693 }
694 return "";
695}
696
Craig Topper37932912013-08-18 10:09:15 +0000697void APNumericStorage::setIntValue(const ASTContext &C,
698 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000699 if (hasAllocation())
700 C.Deallocate(pVal);
701
702 BitWidth = Val.getBitWidth();
703 unsigned NumWords = Val.getNumWords();
704 const uint64_t* Words = Val.getRawData();
705 if (NumWords > 1) {
706 pVal = new (C) uint64_t[NumWords];
707 std::copy(Words, Words + NumWords, pVal);
708 } else if (NumWords == 1)
709 VAL = Words[0];
710 else
711 VAL = 0;
712}
713
Craig Topper37932912013-08-18 10:09:15 +0000714IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000715 QualType type, SourceLocation l)
716 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
717 false, false),
718 Loc(l) {
719 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
720 assert(V.getBitWidth() == C.getIntWidth(type) &&
721 "Integer type is not the correct size for constant.");
722 setValue(C, V);
723}
724
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000725IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000726IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000727 QualType type, SourceLocation l) {
728 return new (C) IntegerLiteral(C, V, type, l);
729}
730
731IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000732IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000733 return new (C) IntegerLiteral(Empty);
734}
735
Craig Topper37932912013-08-18 10:09:15 +0000736FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000737 bool isexact, QualType Type, SourceLocation L)
738 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
739 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000740 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000741 FloatingLiteralBits.IsExact = isexact;
742 setValue(C, V);
743}
744
Craig Topper37932912013-08-18 10:09:15 +0000745FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000746 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000747 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000748 FloatingLiteralBits.IsExact = false;
749}
750
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000751FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000752FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000753 bool isexact, QualType Type, SourceLocation L) {
754 return new (C) FloatingLiteral(C, V, isexact, Type, L);
755}
756
757FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000758FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000759 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000760}
761
Tim Northover178723a2013-01-22 09:46:51 +0000762const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
763 switch(FloatingLiteralBits.Semantics) {
764 case IEEEhalf:
765 return llvm::APFloat::IEEEhalf;
766 case IEEEsingle:
767 return llvm::APFloat::IEEEsingle;
768 case IEEEdouble:
769 return llvm::APFloat::IEEEdouble;
770 case x87DoubleExtended:
771 return llvm::APFloat::x87DoubleExtended;
772 case IEEEquad:
773 return llvm::APFloat::IEEEquad;
774 case PPCDoubleDouble:
775 return llvm::APFloat::PPCDoubleDouble;
776 }
777 llvm_unreachable("Unrecognised floating semantics");
778}
779
780void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
781 if (&Sem == &llvm::APFloat::IEEEhalf)
782 FloatingLiteralBits.Semantics = IEEEhalf;
783 else if (&Sem == &llvm::APFloat::IEEEsingle)
784 FloatingLiteralBits.Semantics = IEEEsingle;
785 else if (&Sem == &llvm::APFloat::IEEEdouble)
786 FloatingLiteralBits.Semantics = IEEEdouble;
787 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
788 FloatingLiteralBits.Semantics = x87DoubleExtended;
789 else if (&Sem == &llvm::APFloat::IEEEquad)
790 FloatingLiteralBits.Semantics = IEEEquad;
791 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
792 FloatingLiteralBits.Semantics = PPCDoubleDouble;
793 else
794 llvm_unreachable("Unknown floating semantics");
795}
796
Chris Lattnera0173132008-06-07 22:13:43 +0000797/// getValueAsApproximateDouble - This returns the value as an inaccurate
798/// double. Note that this may cause loss of precision, but is useful for
799/// debugging dumps, etc.
800double FloatingLiteral::getValueAsApproximateDouble() const {
801 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000802 bool ignored;
803 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
804 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000805 return V.convertToDouble();
806}
807
Nick Lewycky4ed84042012-02-24 09:07:53 +0000808int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000809 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000810 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000811 case Ascii:
812 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000813 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000814 break;
815 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000816 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000817 break;
818 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000819 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000820 break;
821 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000822 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000823 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000824 }
825 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
826 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000827 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000828 && "character byte widths supported are 1, 2, and 4 only");
829 return CharByteWidth;
830}
831
Craig Topper37932912013-08-18 10:09:15 +0000832StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000833 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000834 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000835 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000836 assert(C.getAsConstantArrayType(Ty) &&
837 "StringLiteral must be of constant array type!");
838
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000839 // Allocate enough space for the StringLiteral plus an array of locations for
840 // any concatenated string tokens.
841 void *Mem = C.Allocate(sizeof(StringLiteral)+
842 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000843 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000844 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000845
Steve Naroffdf7855b2007-02-21 23:46:25 +0000846 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000847 SL->setString(C,Str,Kind,Pascal);
848
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000849 SL->TokLocs[0] = Loc[0];
850 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000851
Chris Lattner630970d2009-02-18 05:49:11 +0000852 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000853 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
854 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000855}
856
Craig Topper37932912013-08-18 10:09:15 +0000857StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
858 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000859 void *Mem = C.Allocate(sizeof(StringLiteral)+
860 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000861 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000862 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000863 SL->CharByteWidth = 0;
864 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000865 SL->NumConcatenated = NumStrs;
866 return SL;
867}
868
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000869void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000870 switch (getKind()) {
871 case Ascii: break; // no prefix.
872 case Wide: OS << 'L'; break;
873 case UTF8: OS << "u8"; break;
874 case UTF16: OS << 'u'; break;
875 case UTF32: OS << 'U'; break;
876 }
877 OS << '"';
878 static const char Hex[] = "0123456789ABCDEF";
879
880 unsigned LastSlashX = getLength();
881 for (unsigned I = 0, N = getLength(); I != N; ++I) {
882 switch (uint32_t Char = getCodeUnit(I)) {
883 default:
884 // FIXME: Convert UTF-8 back to codepoints before rendering.
885
886 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
887 // Leave invalid surrogates alone; we'll use \x for those.
888 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
889 Char <= 0xdbff) {
890 uint32_t Trail = getCodeUnit(I + 1);
891 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
892 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
893 ++I;
894 }
895 }
896
897 if (Char > 0xff) {
898 // If this is a wide string, output characters over 0xff using \x
899 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
900 // codepoint: use \x escapes for invalid codepoints.
901 if (getKind() == Wide ||
902 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
903 // FIXME: Is this the best way to print wchar_t?
904 OS << "\\x";
905 int Shift = 28;
906 while ((Char >> Shift) == 0)
907 Shift -= 4;
908 for (/**/; Shift >= 0; Shift -= 4)
909 OS << Hex[(Char >> Shift) & 15];
910 LastSlashX = I;
911 break;
912 }
913
914 if (Char > 0xffff)
915 OS << "\\U00"
916 << Hex[(Char >> 20) & 15]
917 << Hex[(Char >> 16) & 15];
918 else
919 OS << "\\u";
920 OS << Hex[(Char >> 12) & 15]
921 << Hex[(Char >> 8) & 15]
922 << Hex[(Char >> 4) & 15]
923 << Hex[(Char >> 0) & 15];
924 break;
925 }
926
927 // If we used \x... for the previous character, and this character is a
928 // hexadecimal digit, prevent it being slurped as part of the \x.
929 if (LastSlashX + 1 == I) {
930 switch (Char) {
931 case '0': case '1': case '2': case '3': case '4':
932 case '5': case '6': case '7': case '8': case '9':
933 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
934 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
935 OS << "\"\"";
936 }
937 }
938
939 assert(Char <= 0xff &&
940 "Characters above 0xff should already have been handled.");
941
Jordan Rosea7d03842013-02-08 22:30:41 +0000942 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000943 OS << (char)Char;
944 else // Output anything hard as an octal escape.
945 OS << '\\'
946 << (char)('0' + ((Char >> 6) & 7))
947 << (char)('0' + ((Char >> 3) & 7))
948 << (char)('0' + ((Char >> 0) & 7));
949 break;
950 // Handle some common non-printable cases to make dumps prettier.
951 case '\\': OS << "\\\\"; break;
952 case '"': OS << "\\\""; break;
953 case '\n': OS << "\\n"; break;
954 case '\t': OS << "\\t"; break;
955 case '\a': OS << "\\a"; break;
956 case '\b': OS << "\\b"; break;
957 }
958 }
959 OS << '"';
960}
961
Craig Topper37932912013-08-18 10:09:15 +0000962void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000963 StringKind Kind, bool IsPascal) {
964 //FIXME: we assume that the string data comes from a target that uses the same
965 // code unit size and endianess for the type of string.
966 this->Kind = Kind;
967 this->IsPascal = IsPascal;
968
Nick Lewycky4ed84042012-02-24 09:07:53 +0000969 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000970 assert((Str.size()%CharByteWidth == 0)
971 && "size of data must be multiple of CharByteWidth");
972 Length = Str.size()/CharByteWidth;
973
974 switch(CharByteWidth) {
975 case 1: {
976 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000977 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000978 StrData.asChar = AStrData;
979 break;
980 }
981 case 2: {
982 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000983 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000984 StrData.asUInt16 = AStrData;
985 break;
986 }
987 case 4: {
988 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000989 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000990 StrData.asUInt32 = AStrData;
991 break;
992 }
993 default:
994 assert(false && "unsupported CharByteWidth");
995 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000996}
997
Chris Lattnere925d612010-11-17 07:37:15 +0000998/// getLocationOfByte - Return a source location that points to the specified
999/// byte of this string literal.
1000///
1001/// Strings are amazingly complex. They can be formed from multiple tokens and
1002/// can have escape sequences in them in addition to the usual trigraph and
1003/// escaped newline business. This routine handles this complexity.
1004///
1005SourceLocation StringLiteral::
1006getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1007 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +00001008 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1009 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +00001010
Chris Lattnere925d612010-11-17 07:37:15 +00001011 // Loop over all of the tokens in this string until we find the one that
1012 // contains the byte we're looking for.
1013 unsigned TokNo = 0;
1014 while (1) {
1015 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1016 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1017
1018 // Get the spelling of the string so that we can get the data that makes up
1019 // the string literal, not the identifier for the macro it is potentially
1020 // expanded through.
1021 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1022
1023 // Re-lex the token to get its length and original spelling.
1024 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1025 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001026 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001027 if (Invalid)
1028 return StrTokSpellingLoc;
1029
1030 const char *StrData = Buffer.data()+LocInfo.second;
1031
Chris Lattnere925d612010-11-17 07:37:15 +00001032 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001033 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1034 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001035 Token TheTok;
1036 TheLexer.LexFromRawLexer(TheTok);
1037
1038 // Use the StringLiteralParser to compute the length of the string in bytes.
Craig Topper9d5583e2014-06-26 04:58:39 +00001039 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattnere925d612010-11-17 07:37:15 +00001040 unsigned TokNumBytes = SLP.GetStringLength();
1041
1042 // If the byte is in this token, return the location of the byte.
1043 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001044 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001045 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1046
1047 // Now that we know the offset of the token in the spelling, use the
1048 // preprocessor to get the offset in the original source.
1049 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1050 }
1051
1052 // Move to the next string token.
1053 ++TokNo;
1054 ByteNo -= TokNumBytes;
1055 }
1056}
1057
1058
1059
Chris Lattner1b926492006-08-23 06:42:10 +00001060/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1061/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001062StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001063 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001064 case UO_PostInc: return "++";
1065 case UO_PostDec: return "--";
1066 case UO_PreInc: return "++";
1067 case UO_PreDec: return "--";
1068 case UO_AddrOf: return "&";
1069 case UO_Deref: return "*";
1070 case UO_Plus: return "+";
1071 case UO_Minus: return "-";
1072 case UO_Not: return "~";
1073 case UO_LNot: return "!";
1074 case UO_Real: return "__real";
1075 case UO_Imag: return "__imag";
1076 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001077 }
David Blaikief47fa302012-01-17 02:30:50 +00001078 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001079}
1080
John McCalle3027922010-08-25 11:45:40 +00001081UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001082UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1083 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001084 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001085 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1086 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1087 case OO_Amp: return UO_AddrOf;
1088 case OO_Star: return UO_Deref;
1089 case OO_Plus: return UO_Plus;
1090 case OO_Minus: return UO_Minus;
1091 case OO_Tilde: return UO_Not;
1092 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001093 }
1094}
1095
1096OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1097 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001098 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1099 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1100 case UO_AddrOf: return OO_Amp;
1101 case UO_Deref: return OO_Star;
1102 case UO_Plus: return OO_Plus;
1103 case UO_Minus: return OO_Minus;
1104 case UO_Not: return OO_Tilde;
1105 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001106 default: return OO_None;
1107 }
1108}
1109
1110
Chris Lattner0eedafe2006-08-24 04:56:27 +00001111//===----------------------------------------------------------------------===//
1112// Postfix Operators.
1113//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001114
Craig Topper37932912013-08-18 10:09:15 +00001115CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1116 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1117 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001118 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001119 fn->isTypeDependent(),
1120 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001121 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001122 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001123 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001124
Benjamin Kramerc215e762012-08-24 11:54:20 +00001125 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001126 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001127 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001128 if (args[i]->isTypeDependent())
1129 ExprBits.TypeDependent = true;
1130 if (args[i]->isValueDependent())
1131 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001132 if (args[i]->isInstantiationDependent())
1133 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001134 if (args[i]->containsUnexpandedParameterPack())
1135 ExprBits.ContainsUnexpandedParameterPack = true;
1136
Peter Collingbourne3a347252011-02-08 21:18:02 +00001137 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001138 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001139
Peter Collingbourne3a347252011-02-08 21:18:02 +00001140 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001141 RParenLoc = rparenloc;
1142}
Nate Begeman1e36a852008-01-17 17:46:27 +00001143
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001144CallExpr::CallExpr(const ASTContext &C, Expr *fn, ArrayRef<Expr *> args,
John McCall7decc9e2010-11-18 06:31:45 +00001145 QualType t, ExprValueKind VK, SourceLocation rparenloc)
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001146 : CallExpr(C, CallExprClass, fn, /*NumPreArgs=*/0, args, t, VK, rparenloc) {
Chris Lattnere165d942006-08-24 04:40:38 +00001147}
1148
Craig Topper37932912013-08-18 10:09:15 +00001149CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001150 : CallExpr(C, SC, /*NumPreArgs=*/0, Empty) {}
Peter Collingbourne3a347252011-02-08 21:18:02 +00001151
Craig Topper37932912013-08-18 10:09:15 +00001152CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001153 EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001154 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbourne3a347252011-02-08 21:18:02 +00001155 // FIXME: Why do we allocate this?
1156 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1157 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001158}
1159
Nuno Lopes518e3702009-12-20 23:11:08 +00001160Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001161 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001162
1163 while (SubstNonTypeTemplateParmExpr *NTTP
1164 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1165 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1166 }
1167
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001168 // If we're calling a dereference, look at the pointer instead.
1169 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1170 if (BO->isPtrMemOp())
1171 CEE = BO->getRHS()->IgnoreParenCasts();
1172 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1173 if (UO->getOpcode() == UO_Deref)
1174 CEE = UO->getSubExpr()->IgnoreParenCasts();
1175 }
Chris Lattner52301912009-07-17 15:46:27 +00001176 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001177 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001178 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1179 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001180
Craig Topper36250ad2014-05-12 05:36:57 +00001181 return nullptr;
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001182}
1183
Nuno Lopes518e3702009-12-20 23:11:08 +00001184FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001185 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001186}
1187
Chris Lattnere4407ed2007-12-28 05:25:02 +00001188/// setNumArgs - This changes the number of arguments present in this call.
1189/// Any orphaned expressions are deleted by this, and any new operands are set
1190/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001191void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001192 // No change, just return.
1193 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattnere4407ed2007-12-28 05:25:02 +00001195 // If shrinking # arguments, just delete the extras and forgot them.
1196 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001197 this->NumArgs = NumArgs;
1198 return;
1199 }
1200
1201 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001202 unsigned NumPreArgs = getNumPreArgs();
1203 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001204 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001205 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001206 NewSubExprs[i] = SubExprs[i];
1207 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001208 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1209 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001210 NewSubExprs[i] = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregorba6e5572009-04-17 21:46:47 +00001212 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001213 SubExprs = NewSubExprs;
1214 this->NumArgs = NumArgs;
1215}
1216
Alp Tokera724cff2013-12-28 21:59:02 +00001217/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001218/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001219unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001220 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001221 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001222 // ImplicitCastExpr.
1223 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1224 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001225 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001226
Steve Narofff6e3b3292008-01-31 01:07:12 +00001227 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1228 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001229 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001230
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001231 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1232 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001233 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001235 if (!FDecl->getIdentifier())
1236 return 0;
1237
Douglas Gregor15fc9562009-09-12 00:22:50 +00001238 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001239}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001240
Scott Douglass503fc392015-06-10 13:53:15 +00001241bool CallExpr::isUnevaluatedBuiltinCall(const ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001242 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001243 return Ctx.BuiltinInfo.isUnevaluated(BI);
1244 return false;
1245}
1246
David Majnemerced8bdf2015-02-25 17:36:15 +00001247QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const {
1248 const Expr *Callee = getCallee();
1249 QualType CalleeType = Callee->getType();
1250 if (const auto *FnTypePtr = CalleeType->getAs<PointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001251 CalleeType = FnTypePtr->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001252 } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001253 CalleeType = BPT->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001254 } else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
1255 if (isa<CXXPseudoDestructorExpr>(Callee->IgnoreParens()))
1256 return Ctx.VoidTy;
1257
John McCall0009fcc2011-04-26 20:42:42 +00001258 // This should never be overloaded and so should never return null.
David Majnemerced8bdf2015-02-25 17:36:15 +00001259 CalleeType = Expr::findBoundMemberType(Callee);
1260 }
1261
John McCall0009fcc2011-04-26 20:42:42 +00001262 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001263 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001264}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001265
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001266SourceLocation CallExpr::getLocStart() const {
1267 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001268 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001269
1270 SourceLocation begin = getCallee()->getLocStart();
Keno Fischer070db172014-08-15 01:39:12 +00001271 if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001272 begin = getArg(0)->getLocStart();
1273 return begin;
1274}
1275SourceLocation CallExpr::getLocEnd() const {
1276 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001277 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001278
1279 SourceLocation end = getRParenLoc();
Keno Fischer070db172014-08-15 01:39:12 +00001280 if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001281 end = getArg(getNumArgs() - 1)->getLocEnd();
1282 return end;
1283}
John McCall701417a2011-02-21 06:23:05 +00001284
Craig Topper37932912013-08-18 10:09:15 +00001285OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001286 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001287 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001288 ArrayRef<OffsetOfNode> comps,
1289 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001290 SourceLocation RParenLoc) {
1291 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001292 sizeof(OffsetOfNode) * comps.size() +
1293 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001294
Benjamin Kramerc215e762012-08-24 11:54:20 +00001295 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1296 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001297}
1298
Craig Topper37932912013-08-18 10:09:15 +00001299OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001300 unsigned numComps, unsigned numExprs) {
1301 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1302 sizeof(OffsetOfNode) * numComps +
1303 sizeof(Expr*) * numExprs);
1304 return new (Mem) OffsetOfExpr(numComps, numExprs);
1305}
1306
Craig Topper37932912013-08-18 10:09:15 +00001307OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001308 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001309 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001310 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001311 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1312 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001313 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001314 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001315 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001316 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001317 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001318{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001319 for (unsigned i = 0; i != comps.size(); ++i) {
1320 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001321 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001322
Benjamin Kramerc215e762012-08-24 11:54:20 +00001323 for (unsigned i = 0; i != exprs.size(); ++i) {
1324 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001325 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001326 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001327 ExprBits.ContainsUnexpandedParameterPack = true;
1328
Benjamin Kramerc215e762012-08-24 11:54:20 +00001329 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001330 }
1331}
1332
1333IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1334 assert(getKind() == Field || getKind() == Identifier);
1335 if (getKind() == Field)
1336 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001337
Douglas Gregor882211c2010-04-28 22:16:22 +00001338 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1339}
1340
David Majnemer10fd83d2015-01-15 10:04:14 +00001341UnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr(
1342 UnaryExprOrTypeTrait ExprKind, Expr *E, QualType resultType,
1343 SourceLocation op, SourceLocation rp)
1344 : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary,
1345 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1346 // Value-dependent if the argument is type-dependent.
1347 E->isTypeDependent(), E->isInstantiationDependent(),
1348 E->containsUnexpandedParameterPack()),
1349 OpLoc(op), RParenLoc(rp) {
1350 UnaryExprOrTypeTraitExprBits.Kind = ExprKind;
1351 UnaryExprOrTypeTraitExprBits.IsType = false;
1352 Argument.Ex = E;
1353
1354 // Check to see if we are in the situation where alignof(decl) should be
1355 // dependent because decl's alignment is dependent.
1356 if (ExprKind == UETT_AlignOf) {
1357 if (!isValueDependent() || !isInstantiationDependent()) {
1358 E = E->IgnoreParens();
1359
1360 const ValueDecl *D = nullptr;
1361 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
1362 D = DRE->getDecl();
1363 else if (const auto *ME = dyn_cast<MemberExpr>(E))
1364 D = ME->getMemberDecl();
1365
1366 if (D) {
1367 for (const auto *I : D->specific_attrs<AlignedAttr>()) {
1368 if (I->isAlignmentDependent()) {
1369 setValueDependent(true);
1370 setInstantiationDependent(true);
1371 break;
1372 }
1373 }
1374 }
1375 }
1376 }
1377}
1378
Aaron Ballmanf4cb2be2015-03-24 15:07:53 +00001379MemberExpr *MemberExpr::Create(
1380 const ASTContext &C, Expr *base, bool isarrow, SourceLocation OperatorLoc,
1381 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1382 ValueDecl *memberdecl, DeclAccessPair founddecl,
1383 DeclarationNameInfo nameinfo, const TemplateArgumentListInfo *targs,
1384 QualType ty, ExprValueKind vk, ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001385 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001386
Douglas Gregorea972d32011-02-28 21:54:11 +00001387 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001388 founddecl.getDecl() != memberdecl ||
1389 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001390 if (hasQualOrFound)
1391 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001392
John McCall6b51f282009-11-23 01:53:49 +00001393 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001394 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1395 else if (TemplateKWLoc.isValid())
1396 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001397
Chris Lattner5c0b4052010-10-30 05:14:06 +00001398 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
Aaron Ballmanf4cb2be2015-03-24 15:07:53 +00001399 MemberExpr *E = new (Mem)
1400 MemberExpr(base, isarrow, OperatorLoc, memberdecl, nameinfo, ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001401
1402 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001403 // FIXME: Wrong. We should be looking at the member declaration we found.
1404 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001405 E->setValueDependent(true);
1406 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001407 E->setInstantiationDependent(true);
1408 }
1409 else if (QualifierLoc &&
1410 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1411 E->setInstantiationDependent(true);
1412
John McCall16df1e52010-03-30 21:47:33 +00001413 E->HasQualifierOrFoundDecl = true;
1414
1415 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001416 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001417 NQ->FoundDecl = founddecl;
1418 }
1419
Abramo Bagnara7945c982012-01-27 09:46:47 +00001420 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1421
John McCall16df1e52010-03-30 21:47:33 +00001422 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001423 bool Dependent = false;
1424 bool InstantiationDependent = false;
1425 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001426 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1427 Dependent,
1428 InstantiationDependent,
1429 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001430 if (InstantiationDependent)
1431 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001432 } else if (TemplateKWLoc.isValid()) {
1433 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001434 }
1435
1436 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001437}
1438
Daniel Dunbarb507f272012-03-09 15:39:15 +00001439SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001440 if (isImplicitAccess()) {
1441 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001442 return getQualifierLoc().getBeginLoc();
1443 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001444 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001445
Daniel Dunbarb507f272012-03-09 15:39:15 +00001446 // FIXME: We don't want this to happen. Rather, we should be able to
1447 // detect all kinds of implicit accesses more cleanly.
1448 SourceLocation BaseStartLoc = getBase()->getLocStart();
1449 if (BaseStartLoc.isValid())
1450 return BaseStartLoc;
1451 return MemberLoc;
1452}
1453SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001454 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001455 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001456 EndLoc = getRAngleLoc();
1457 else if (EndLoc.isInvalid())
1458 EndLoc = getBase()->getLocEnd();
1459 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001460}
1461
Alp Tokerc1086762013-12-07 13:51:35 +00001462bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001463 switch (getCastKind()) {
1464 case CK_DerivedToBase:
1465 case CK_UncheckedDerivedToBase:
1466 case CK_DerivedToBaseMemberPointer:
1467 case CK_BaseToDerived:
1468 case CK_BaseToDerivedMemberPointer:
1469 assert(!path_empty() && "Cast kind should have a base path!");
1470 break;
1471
1472 case CK_CPointerToObjCPointerCast:
1473 assert(getType()->isObjCObjectPointerType());
1474 assert(getSubExpr()->getType()->isPointerType());
1475 goto CheckNoBasePath;
1476
1477 case CK_BlockPointerToObjCPointerCast:
1478 assert(getType()->isObjCObjectPointerType());
1479 assert(getSubExpr()->getType()->isBlockPointerType());
1480 goto CheckNoBasePath;
1481
John McCallc62bb392012-02-15 01:22:51 +00001482 case CK_ReinterpretMemberPointer:
1483 assert(getType()->isMemberPointerType());
1484 assert(getSubExpr()->getType()->isMemberPointerType());
1485 goto CheckNoBasePath;
1486
John McCall9320b872011-09-09 05:25:32 +00001487 case CK_BitCast:
1488 // Arbitrary casts to C pointer types count as bitcasts.
1489 // Otherwise, we should only have block and ObjC pointer casts
1490 // here if they stay within the type kind.
1491 if (!getType()->isPointerType()) {
1492 assert(getType()->isObjCObjectPointerType() ==
1493 getSubExpr()->getType()->isObjCObjectPointerType());
1494 assert(getType()->isBlockPointerType() ==
1495 getSubExpr()->getType()->isBlockPointerType());
1496 }
1497 goto CheckNoBasePath;
1498
1499 case CK_AnyPointerToBlockPointerCast:
1500 assert(getType()->isBlockPointerType());
1501 assert(getSubExpr()->getType()->isAnyPointerType() &&
1502 !getSubExpr()->getType()->isBlockPointerType());
1503 goto CheckNoBasePath;
1504
Douglas Gregored90df32012-02-22 05:02:47 +00001505 case CK_CopyAndAutoreleaseBlockObject:
1506 assert(getType()->isBlockPointerType());
1507 assert(getSubExpr()->getType()->isBlockPointerType());
1508 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001509
1510 case CK_FunctionToPointerDecay:
1511 assert(getType()->isPointerType());
1512 assert(getSubExpr()->getType()->isFunctionType());
1513 goto CheckNoBasePath;
1514
David Tweede1468322013-12-11 13:39:46 +00001515 case CK_AddressSpaceConversion:
1516 assert(getType()->isPointerType());
1517 assert(getSubExpr()->getType()->isPointerType());
1518 assert(getType()->getPointeeType().getAddressSpace() !=
1519 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001520 // These should not have an inheritance path.
1521 case CK_Dynamic:
1522 case CK_ToUnion:
1523 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001524 case CK_NullToMemberPointer:
1525 case CK_NullToPointer:
1526 case CK_ConstructorConversion:
1527 case CK_IntegralToPointer:
1528 case CK_PointerToIntegral:
1529 case CK_ToVoid:
1530 case CK_VectorSplat:
1531 case CK_IntegralCast:
1532 case CK_IntegralToFloating:
1533 case CK_FloatingToIntegral:
1534 case CK_FloatingCast:
1535 case CK_ObjCObjectLValueCast:
1536 case CK_FloatingRealToComplex:
1537 case CK_FloatingComplexToReal:
1538 case CK_FloatingComplexCast:
1539 case CK_FloatingComplexToIntegralComplex:
1540 case CK_IntegralRealToComplex:
1541 case CK_IntegralComplexToReal:
1542 case CK_IntegralComplexCast:
1543 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001544 case CK_ARCProduceObject:
1545 case CK_ARCConsumeObject:
1546 case CK_ARCReclaimReturnedObject:
1547 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001548 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001549 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1550 goto CheckNoBasePath;
1551
1552 case CK_Dependent:
1553 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001554 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001555 case CK_AtomicToNonAtomic:
1556 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001557 case CK_PointerToBoolean:
1558 case CK_IntegralToBoolean:
1559 case CK_FloatingToBoolean:
1560 case CK_MemberPointerToBoolean:
1561 case CK_FloatingComplexToBoolean:
1562 case CK_IntegralComplexToBoolean:
1563 case CK_LValueBitCast: // -> bool&
1564 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001565 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001566 CheckNoBasePath:
1567 assert(path_empty() && "Cast kind should not have a base path!");
1568 break;
1569 }
Alp Tokerc1086762013-12-07 13:51:35 +00001570 return true;
John McCall9320b872011-09-09 05:25:32 +00001571}
1572
Anders Carlsson496335e2009-09-03 00:59:21 +00001573const char *CastExpr::getCastKindName() const {
1574 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001575 case CK_Dependent:
1576 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001577 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001578 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001579 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001580 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001581 case CK_LValueToRValue:
1582 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001583 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001584 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001585 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001586 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001587 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001588 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001589 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001590 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001591 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001592 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001593 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001594 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001595 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001596 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001597 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001598 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001599 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001600 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001601 case CK_NullToPointer:
1602 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001603 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001604 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001605 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001606 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001607 case CK_ReinterpretMemberPointer:
1608 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001609 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001610 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001611 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001612 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001613 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001614 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001615 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001616 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001617 case CK_PointerToBoolean:
1618 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001619 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001620 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001621 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001622 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001623 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001624 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001625 case CK_IntegralToBoolean:
1626 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001627 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001628 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001629 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001630 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001631 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001632 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001633 case CK_FloatingToBoolean:
1634 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001635 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001636 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001637 case CK_CPointerToObjCPointerCast:
1638 return "CPointerToObjCPointerCast";
1639 case CK_BlockPointerToObjCPointerCast:
1640 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001641 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001642 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001643 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001644 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001645 case CK_FloatingRealToComplex:
1646 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001647 case CK_FloatingComplexToReal:
1648 return "FloatingComplexToReal";
1649 case CK_FloatingComplexToBoolean:
1650 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001651 case CK_FloatingComplexCast:
1652 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001653 case CK_FloatingComplexToIntegralComplex:
1654 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001655 case CK_IntegralRealToComplex:
1656 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001657 case CK_IntegralComplexToReal:
1658 return "IntegralComplexToReal";
1659 case CK_IntegralComplexToBoolean:
1660 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001661 case CK_IntegralComplexCast:
1662 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001663 case CK_IntegralComplexToFloatingComplex:
1664 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001665 case CK_ARCConsumeObject:
1666 return "ARCConsumeObject";
1667 case CK_ARCProduceObject:
1668 return "ARCProduceObject";
1669 case CK_ARCReclaimReturnedObject:
1670 return "ARCReclaimReturnedObject";
1671 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001672 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001673 case CK_AtomicToNonAtomic:
1674 return "AtomicToNonAtomic";
1675 case CK_NonAtomicToAtomic:
1676 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001677 case CK_CopyAndAutoreleaseBlockObject:
1678 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001679 case CK_BuiltinFnToFnPtr:
1680 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001681 case CK_ZeroToOCLEvent:
1682 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001683 case CK_AddressSpaceConversion:
1684 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001685 }
Mike Stump11289f42009-09-09 15:08:12 +00001686
John McCallc5e62b42010-11-13 09:02:35 +00001687 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001688}
1689
Douglas Gregord196a582009-12-14 19:27:10 +00001690Expr *CastExpr::getSubExprAsWritten() {
Craig Topper36250ad2014-05-12 05:36:57 +00001691 Expr *SubExpr = nullptr;
Douglas Gregord196a582009-12-14 19:27:10 +00001692 CastExpr *E = this;
1693 do {
1694 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001695
1696 // Skip through reference binding to temporary.
1697 if (MaterializeTemporaryExpr *Materialize
1698 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1699 SubExpr = Materialize->GetTemporaryExpr();
1700
Douglas Gregord196a582009-12-14 19:27:10 +00001701 // Skip any temporary bindings; they're implicit.
1702 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1703 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001704
Douglas Gregord196a582009-12-14 19:27:10 +00001705 // Conversions by constructor and conversion functions have a
1706 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001707 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001708 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001709 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001710 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001711
Douglas Gregord196a582009-12-14 19:27:10 +00001712 // If the subexpression we're left with is an implicit cast, look
1713 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001714 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1715
Douglas Gregord196a582009-12-14 19:27:10 +00001716 return SubExpr;
1717}
1718
John McCallcf142162010-08-07 06:22:56 +00001719CXXBaseSpecifier **CastExpr::path_buffer() {
1720 switch (getStmtClass()) {
1721#define ABSTRACT_STMT(x)
1722#define CASTEXPR(Type, Base) \
1723 case Stmt::Type##Class: \
1724 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1725#define STMT(Type, Base)
1726#include "clang/AST/StmtNodes.inc"
1727 default:
1728 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001729 }
1730}
1731
1732void CastExpr::setCastPath(const CXXCastPath &Path) {
1733 assert(Path.size() == path_size());
1734 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1735}
1736
Craig Topper37932912013-08-18 10:09:15 +00001737ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001738 CastKind Kind, Expr *Operand,
1739 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001740 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001741 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1742 void *Buffer =
1743 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1744 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001745 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001746 if (PathSize) E->setCastPath(*BasePath);
1747 return E;
1748}
1749
Craig Topper37932912013-08-18 10:09:15 +00001750ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001751 unsigned PathSize) {
1752 void *Buffer =
1753 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1754 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1755}
1756
1757
Craig Topper37932912013-08-18 10:09:15 +00001758CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001759 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001760 const CXXCastPath *BasePath,
1761 TypeSourceInfo *WrittenTy,
1762 SourceLocation L, SourceLocation R) {
1763 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1764 void *Buffer =
1765 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1766 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001767 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001768 if (PathSize) E->setCastPath(*BasePath);
1769 return E;
1770}
1771
Craig Topper37932912013-08-18 10:09:15 +00001772CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1773 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001774 void *Buffer =
1775 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1776 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1777}
1778
Chris Lattner1b926492006-08-23 06:42:10 +00001779/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1780/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001781StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001782 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001783 case BO_PtrMemD: return ".*";
1784 case BO_PtrMemI: return "->*";
1785 case BO_Mul: return "*";
1786 case BO_Div: return "/";
1787 case BO_Rem: return "%";
1788 case BO_Add: return "+";
1789 case BO_Sub: return "-";
1790 case BO_Shl: return "<<";
1791 case BO_Shr: return ">>";
1792 case BO_LT: return "<";
1793 case BO_GT: return ">";
1794 case BO_LE: return "<=";
1795 case BO_GE: return ">=";
1796 case BO_EQ: return "==";
1797 case BO_NE: return "!=";
1798 case BO_And: return "&";
1799 case BO_Xor: return "^";
1800 case BO_Or: return "|";
1801 case BO_LAnd: return "&&";
1802 case BO_LOr: return "||";
1803 case BO_Assign: return "=";
1804 case BO_MulAssign: return "*=";
1805 case BO_DivAssign: return "/=";
1806 case BO_RemAssign: return "%=";
1807 case BO_AddAssign: return "+=";
1808 case BO_SubAssign: return "-=";
1809 case BO_ShlAssign: return "<<=";
1810 case BO_ShrAssign: return ">>=";
1811 case BO_AndAssign: return "&=";
1812 case BO_XorAssign: return "^=";
1813 case BO_OrAssign: return "|=";
1814 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001815 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001816
David Blaikiee4d798f2012-01-20 21:50:17 +00001817 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001818}
Steve Naroff47500512007-04-19 23:00:49 +00001819
John McCalle3027922010-08-25 11:45:40 +00001820BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001821BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1822 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001823 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001824 case OO_Plus: return BO_Add;
1825 case OO_Minus: return BO_Sub;
1826 case OO_Star: return BO_Mul;
1827 case OO_Slash: return BO_Div;
1828 case OO_Percent: return BO_Rem;
1829 case OO_Caret: return BO_Xor;
1830 case OO_Amp: return BO_And;
1831 case OO_Pipe: return BO_Or;
1832 case OO_Equal: return BO_Assign;
1833 case OO_Less: return BO_LT;
1834 case OO_Greater: return BO_GT;
1835 case OO_PlusEqual: return BO_AddAssign;
1836 case OO_MinusEqual: return BO_SubAssign;
1837 case OO_StarEqual: return BO_MulAssign;
1838 case OO_SlashEqual: return BO_DivAssign;
1839 case OO_PercentEqual: return BO_RemAssign;
1840 case OO_CaretEqual: return BO_XorAssign;
1841 case OO_AmpEqual: return BO_AndAssign;
1842 case OO_PipeEqual: return BO_OrAssign;
1843 case OO_LessLess: return BO_Shl;
1844 case OO_GreaterGreater: return BO_Shr;
1845 case OO_LessLessEqual: return BO_ShlAssign;
1846 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1847 case OO_EqualEqual: return BO_EQ;
1848 case OO_ExclaimEqual: return BO_NE;
1849 case OO_LessEqual: return BO_LE;
1850 case OO_GreaterEqual: return BO_GE;
1851 case OO_AmpAmp: return BO_LAnd;
1852 case OO_PipePipe: return BO_LOr;
1853 case OO_Comma: return BO_Comma;
1854 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001855 }
1856}
1857
1858OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1859 static const OverloadedOperatorKind OverOps[] = {
1860 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1861 OO_Star, OO_Slash, OO_Percent,
1862 OO_Plus, OO_Minus,
1863 OO_LessLess, OO_GreaterGreater,
1864 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1865 OO_EqualEqual, OO_ExclaimEqual,
1866 OO_Amp,
1867 OO_Caret,
1868 OO_Pipe,
1869 OO_AmpAmp,
1870 OO_PipePipe,
1871 OO_Equal, OO_StarEqual,
1872 OO_SlashEqual, OO_PercentEqual,
1873 OO_PlusEqual, OO_MinusEqual,
1874 OO_LessLessEqual, OO_GreaterGreaterEqual,
1875 OO_AmpEqual, OO_CaretEqual,
1876 OO_PipeEqual,
1877 OO_Comma
1878 };
1879 return OverOps[Opc];
1880}
1881
Craig Topper37932912013-08-18 10:09:15 +00001882InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001883 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001884 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001885 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001886 InitExprs(C, initExprs.size()),
Craig Topper36250ad2014-05-12 05:36:57 +00001887 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001888{
1889 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001890 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001891 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001892 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001893 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001894 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001895 if (initExprs[I]->isInstantiationDependent())
1896 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001897 if (initExprs[I]->containsUnexpandedParameterPack())
1898 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001899 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001900
Benjamin Kramerc215e762012-08-24 11:54:20 +00001901 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001902}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001903
Craig Topper37932912013-08-18 10:09:15 +00001904void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001905 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001906 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001907}
1908
Craig Topper37932912013-08-18 10:09:15 +00001909void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Craig Topper36250ad2014-05-12 05:36:57 +00001910 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001911}
1912
Craig Topper37932912013-08-18 10:09:15 +00001913Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001914 if (Init >= InitExprs.size()) {
Craig Topper36250ad2014-05-12 05:36:57 +00001915 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Richard Smithc275da62013-12-06 01:27:24 +00001916 setInit(Init, expr);
Craig Topper36250ad2014-05-12 05:36:57 +00001917 return nullptr;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001918 }
Mike Stump11289f42009-09-09 15:08:12 +00001919
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001920 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001921 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001922 return Result;
1923}
1924
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001925void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001926 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001927 ArrayFillerOrUnionFieldInit = filler;
1928 // Fill out any "holes" in the array due to designated initializers.
1929 Expr **inits = getInits();
1930 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001931 if (inits[i] == nullptr)
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001932 inits[i] = filler;
1933}
1934
Richard Smith9ec1e482012-04-15 02:50:59 +00001935bool InitListExpr::isStringLiteralInit() const {
1936 if (getNumInits() != 1)
1937 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001938 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1939 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001940 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001941 // It is possible for getInit() to return null.
1942 const Expr *Init = getInit(0);
1943 if (!Init)
1944 return false;
1945 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001946 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1947}
1948
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001949SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001950 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001951 return SyntacticForm->getLocStart();
1952 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001953 if (Beg.isInvalid()) {
1954 // Find the first non-null initializer.
1955 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1956 E = InitExprs.end();
1957 I != E; ++I) {
1958 if (Stmt *S = *I) {
1959 Beg = S->getLocStart();
1960 break;
1961 }
1962 }
1963 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001964 return Beg;
1965}
1966
1967SourceLocation InitListExpr::getLocEnd() const {
1968 if (InitListExpr *SyntacticForm = getSyntacticForm())
1969 return SyntacticForm->getLocEnd();
1970 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001971 if (End.isInvalid()) {
1972 // Find the first non-null initializer from the end.
1973 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001974 E = InitExprs.rend();
1975 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001976 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001977 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001978 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001979 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001980 }
1981 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001982 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001983}
1984
Steve Naroff991e99d2008-09-04 15:31:07 +00001985/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001986///
John McCallc833dea2012-02-17 03:32:35 +00001987const FunctionProtoType *BlockExpr::getFunctionType() const {
1988 // The block pointer is never sugared, but the function type might be.
1989 return cast<BlockPointerType>(getType())
1990 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001991}
1992
Mike Stump11289f42009-09-09 15:08:12 +00001993SourceLocation BlockExpr::getCaretLocation() const {
1994 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001995}
Mike Stump11289f42009-09-09 15:08:12 +00001996const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001997 return TheBlock->getBody();
1998}
Mike Stump11289f42009-09-09 15:08:12 +00001999Stmt *BlockExpr::getBody() {
2000 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00002001}
Steve Naroff415d3d52008-10-08 17:01:13 +00002002
2003
Chris Lattner1ec5f562007-06-27 05:38:08 +00002004//===----------------------------------------------------------------------===//
2005// Generic Expression Routines
2006//===----------------------------------------------------------------------===//
2007
Chris Lattner237f2752009-02-14 07:37:35 +00002008/// isUnusedResultAWarning - Return true if this immediate expression should
2009/// be warned about if the result is unused. If so, fill in Loc and Ranges
2010/// with location to warn on and the source range[s] to report with the
2011/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002012bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2013 SourceRange &R1, SourceRange &R2,
2014 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00002015 // Don't warn if the expr is type dependent. The type could end up
2016 // instantiating to void.
2017 if (isTypeDependent())
2018 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002019
Chris Lattner1ec5f562007-06-27 05:38:08 +00002020 switch (getStmtClass()) {
2021 default:
John McCallc493a732010-03-12 07:11:26 +00002022 if (getType()->isVoidType())
2023 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002024 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002025 Loc = getExprLoc();
2026 R1 = getSourceRange();
2027 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002028 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002029 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002030 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00002031 case GenericSelectionExprClass:
2032 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002033 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00002034 case ChooseExprClass:
2035 return cast<ChooseExpr>(this)->getChosenSubExpr()->
2036 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002037 case UnaryOperatorClass: {
2038 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00002039
Chris Lattner1ec5f562007-06-27 05:38:08 +00002040 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002041 case UO_Plus:
2042 case UO_Minus:
2043 case UO_AddrOf:
2044 case UO_Not:
2045 case UO_LNot:
2046 case UO_Deref:
2047 break;
John McCalle3027922010-08-25 11:45:40 +00002048 case UO_PostInc:
2049 case UO_PostDec:
2050 case UO_PreInc:
2051 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002052 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002053 case UO_Real:
2054 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002055 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002056 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2057 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002058 return false;
2059 break;
John McCalle3027922010-08-25 11:45:40 +00002060 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002061 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002062 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002063 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002064 Loc = UO->getOperatorLoc();
2065 R1 = UO->getSubExpr()->getSourceRange();
2066 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002067 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002068 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002069 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002070 switch (BO->getOpcode()) {
2071 default:
2072 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002073 // Consider the RHS of comma for side effects. LHS was checked by
2074 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002075 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002076 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2077 // lvalue-ness) of an assignment written in a macro.
2078 if (IntegerLiteral *IE =
2079 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2080 if (IE->getValue() == 0)
2081 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002082 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002083 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002084 case BO_LAnd:
2085 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002086 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2087 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002088 return false;
2089 break;
John McCall1e3715a2010-02-16 04:10:53 +00002090 }
Chris Lattner237f2752009-02-14 07:37:35 +00002091 if (BO->isAssignmentOp())
2092 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002093 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002094 Loc = BO->getOperatorLoc();
2095 R1 = BO->getLHS()->getSourceRange();
2096 R2 = BO->getRHS()->getSourceRange();
2097 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002098 }
Chris Lattner86928112007-08-25 02:00:02 +00002099 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002100 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002101 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002102 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002103
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002104 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002105 // If only one of the LHS or RHS is a warning, the operator might
2106 // be being used for control flow. Only warn if both the LHS and
2107 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002108 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002109 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002110 return false;
2111 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002112 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002113 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002114 }
2115
Chris Lattnera44d1162007-06-27 05:58:59 +00002116 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002117 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002118 Loc = cast<MemberExpr>(this)->getMemberLoc();
2119 R1 = SourceRange(Loc, Loc);
2120 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2121 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002122
Chris Lattner1ec5f562007-06-27 05:38:08 +00002123 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002124 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002125 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2126 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2127 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2128 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002129
Chandler Carruth46339472011-08-17 09:49:44 +00002130 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002131 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002132 // overloads as there is no reasonable way to define these such that they
2133 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002134 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002135 // provides additional value as well. If this list is updated,
2136 // DiagnoseUnusedComparison should be as well.
2137 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002138 switch (Op->getOperator()) {
2139 default:
2140 break;
2141 case OO_EqualEqual:
2142 case OO_ExclaimEqual:
2143 case OO_Less:
2144 case OO_Greater:
2145 case OO_GreaterEqual:
2146 case OO_LessEqual:
David Majnemerced8bdf2015-02-25 17:36:15 +00002147 if (Op->getCallReturnType(Ctx)->isReferenceType() ||
2148 Op->getCallReturnType(Ctx)->isVoidType())
Richard Trieu161132b2014-05-14 23:22:10 +00002149 break;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002150 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002151 Loc = Op->getOperatorLoc();
2152 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002153 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002154 }
Chandler Carruth46339472011-08-17 09:49:44 +00002155
2156 // Fallthrough for generic call handling.
2157 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002158 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002159 case CXXMemberCallExprClass:
2160 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002161 // If this is a direct call, get the callee.
2162 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002163 if (const Decl *FD = CE->getCalleeDecl()) {
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002164 const FunctionDecl *Func = dyn_cast<FunctionDecl>(FD);
2165 bool HasWarnUnusedResultAttr = Func ? Func->hasUnusedResultAttr()
2166 : FD->hasAttr<WarnUnusedResultAttr>();
2167
Chris Lattner237f2752009-02-14 07:37:35 +00002168 // If the callee has attribute pure, const, or warn_unused_result, warn
2169 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002170 //
2171 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2172 // updated to match for QoI.
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002173 if (HasWarnUnusedResultAttr ||
Aaron Ballman9ead1242013-12-19 02:39:40 +00002174 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002175 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002176 Loc = CE->getCallee()->getLocStart();
2177 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002178
Chris Lattner1a6babf2009-10-13 04:53:48 +00002179 if (unsigned NumArgs = CE->getNumArgs())
2180 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2181 CE->getArg(NumArgs-1)->getLocEnd());
2182 return true;
2183 }
Chris Lattner237f2752009-02-14 07:37:35 +00002184 }
2185 return false;
2186 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002187
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002188 // If we don't know precisely what we're looking at, let's not warn.
2189 case UnresolvedLookupExprClass:
2190 case CXXUnresolvedConstructExprClass:
2191 return false;
2192
Anders Carlsson6aa50392009-11-17 17:11:23 +00002193 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002194 case CXXConstructExprClass: {
2195 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2196 if (Type->hasAttr<WarnUnusedAttr>()) {
2197 WarnE = this;
2198 Loc = getLocStart();
2199 R1 = getSourceRange();
2200 return true;
2201 }
2202 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002203 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002204 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002205
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002206 case ObjCMessageExprClass: {
2207 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002208 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002209 ME->isInstanceMessage() &&
2210 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002211 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002212 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002213 Loc = getExprLoc();
2214 R1 = ME->getSourceRange();
2215 return true;
2216 }
2217
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002218 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
Fariborz Jahanianb0553e22015-02-16 23:49:44 +00002219 if (MD->hasAttr<WarnUnusedResultAttr>()) {
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002220 WarnE = this;
2221 Loc = getExprLoc();
2222 return true;
2223 }
2224
Chris Lattner237f2752009-02-14 07:37:35 +00002225 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002226 }
Mike Stump11289f42009-09-09 15:08:12 +00002227
John McCallb7bd14f2010-12-02 01:19:52 +00002228 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002229 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002230 Loc = getExprLoc();
2231 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002232 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002233
John McCallfe96e0b2011-11-06 09:01:30 +00002234 case PseudoObjectExprClass: {
2235 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2236
2237 // Only complain about things that have the form of a getter.
2238 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2239 isa<BinaryOperator>(PO->getSyntacticForm()))
2240 return false;
2241
Eli Friedmanc11535c2012-05-24 00:47:05 +00002242 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002243 Loc = getExprLoc();
2244 R1 = getSourceRange();
2245 return true;
2246 }
2247
Chris Lattner944d3062008-07-26 19:51:01 +00002248 case StmtExprClass: {
2249 // Statement exprs don't logically have side effects themselves, but are
2250 // sometimes used in macros in ways that give them a type that is unused.
2251 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2252 // however, if the result of the stmt expr is dead, we don't want to emit a
2253 // warning.
2254 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002255 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002256 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002257 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002258 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2259 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002260 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002261 }
Mike Stump11289f42009-09-09 15:08:12 +00002262
John McCallc493a732010-03-12 07:11:26 +00002263 if (getType()->isVoidType())
2264 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002265 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002266 Loc = cast<StmtExpr>(this)->getLParenLoc();
2267 R1 = getSourceRange();
2268 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002269 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002270 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002271 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002272 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002273 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002274 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002275 if (CE->getCastKind() == CK_ToVoid) {
2276 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002277 CE->getSubExpr()->getType().isVolatileQualified()) {
2278 const DeclRefExpr *DRE =
2279 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2280 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2281 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2282 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2283 R1, R2, Ctx);
2284 }
2285 }
Chris Lattner2706a552009-07-28 18:25:28 +00002286 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002287 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002288
Eli Friedmanc11535c2012-05-24 00:47:05 +00002289 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002290 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002291 if (CE->getCastKind() == CK_ConstructorConversion)
2292 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002293
Eli Friedmanc11535c2012-05-24 00:47:05 +00002294 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002295 if (const CXXFunctionalCastExpr *CXXCE =
2296 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002297 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002298 R1 = CXXCE->getSubExpr()->getSourceRange();
2299 } else {
2300 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2301 Loc = CStyleCE->getLParenLoc();
2302 R1 = CStyleCE->getSubExpr()->getSourceRange();
2303 }
Chris Lattner237f2752009-02-14 07:37:35 +00002304 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002305 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002306 case ImplicitCastExprClass: {
2307 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002308
Eli Friedmanc11535c2012-05-24 00:47:05 +00002309 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2310 if (ICE->getCastKind() == CK_LValueToRValue &&
2311 ICE->getSubExpr()->getType().isVolatileQualified())
2312 return false;
2313
2314 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2315 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002316 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002317 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002318 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002319 case CXXDefaultInitExprClass:
2320 return (cast<CXXDefaultInitExpr>(this)
2321 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002322
2323 case CXXNewExprClass:
2324 // FIXME: In theory, there might be new expressions that don't have side
2325 // effects (e.g. a placement new with an uninitialized POD).
2326 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002327 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002328 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002329 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002330 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002331 case ExprWithCleanupsClass:
2332 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002333 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002334 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002335}
2336
Fariborz Jahanian07735332009-02-22 18:40:18 +00002337/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002338/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002339bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002340 const Expr *E = IgnoreParens();
2341 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002342 default:
2343 return false;
2344 case ObjCIvarRefExprClass:
2345 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002346 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002347 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002348 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002349 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002350 case MaterializeTemporaryExprClass:
2351 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2352 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002353 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002354 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002355 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002356 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002357
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002358 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2359 if (VD->hasGlobalStorage())
2360 return true;
2361 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002362 // dereferencing to a pointer is always a gc'able candidate,
2363 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002364 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002365 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002366 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002367 return false;
2368 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002369 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002370 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002371 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002372 }
2373 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002374 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002375 }
2376}
Sebastian Redlce354af2010-09-10 20:55:33 +00002377
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002378bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2379 if (isTypeDependent())
2380 return false;
John McCall086a4642010-11-24 05:12:34 +00002381 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002382}
2383
John McCall0009fcc2011-04-26 20:42:42 +00002384QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002385 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002386
2387 // Bound member expressions are always one of these possibilities:
2388 // x->m x.m x->*y x.*y
2389 // (possibly parenthesized)
2390
2391 expr = expr->IgnoreParens();
2392 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2393 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2394 return mem->getMemberDecl()->getType();
2395 }
2396
2397 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2398 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2399 ->getPointeeType();
2400 assert(type->isFunctionType());
2401 return type;
2402 }
2403
David Majnemerced8bdf2015-02-25 17:36:15 +00002404 assert(isa<UnresolvedMemberExpr>(expr) || isa<CXXPseudoDestructorExpr>(expr));
John McCall0009fcc2011-04-26 20:42:42 +00002405 return QualType();
2406}
2407
Ted Kremenekfff70962008-01-17 16:57:34 +00002408Expr* Expr::IgnoreParens() {
2409 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002410 while (true) {
2411 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2412 E = P->getSubExpr();
2413 continue;
2414 }
2415 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2416 if (P->getOpcode() == UO_Extension) {
2417 E = P->getSubExpr();
2418 continue;
2419 }
2420 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002421 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2422 if (!P->isResultDependent()) {
2423 E = P->getResultExpr();
2424 continue;
2425 }
2426 }
Eli Friedman75807f22013-07-20 00:40:58 +00002427 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2428 if (!P->isConditionDependent()) {
2429 E = P->getChosenSubExpr();
2430 continue;
2431 }
2432 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002433 return E;
2434 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002435}
2436
Chris Lattnerf2660962008-02-13 01:02:39 +00002437/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2438/// or CastExprs or ImplicitCastExprs, returning their operand.
2439Expr *Expr::IgnoreParenCasts() {
2440 Expr *E = this;
2441 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002442 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002443 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002444 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002445 continue;
2446 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002447 if (MaterializeTemporaryExpr *Materialize
2448 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2449 E = Materialize->GetTemporaryExpr();
2450 continue;
2451 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002452 if (SubstNonTypeTemplateParmExpr *NTTP
2453 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2454 E = NTTP->getReplacement();
2455 continue;
2456 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002457 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002458 }
2459}
2460
Ted Kremenek6f375e52014-04-16 07:26:09 +00002461Expr *Expr::IgnoreCasts() {
2462 Expr *E = this;
2463 while (true) {
2464 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2465 E = P->getSubExpr();
2466 continue;
2467 }
2468 if (MaterializeTemporaryExpr *Materialize
2469 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2470 E = Materialize->GetTemporaryExpr();
2471 continue;
2472 }
2473 if (SubstNonTypeTemplateParmExpr *NTTP
2474 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2475 E = NTTP->getReplacement();
2476 continue;
2477 }
2478 return E;
2479 }
2480}
2481
John McCall5a4ce8b2010-12-04 08:24:19 +00002482/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2483/// casts. This is intended purely as a temporary workaround for code
2484/// that hasn't yet been rewritten to do the right thing about those
2485/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002486Expr *Expr::IgnoreParenLValueCasts() {
2487 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002488 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002489 E = E->IgnoreParens();
2490 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002491 if (P->getCastKind() == CK_LValueToRValue) {
2492 E = P->getSubExpr();
2493 continue;
2494 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002495 } else if (MaterializeTemporaryExpr *Materialize
2496 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2497 E = Materialize->GetTemporaryExpr();
2498 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002499 } else if (SubstNonTypeTemplateParmExpr *NTTP
2500 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2501 E = NTTP->getReplacement();
2502 continue;
John McCall34376a62010-12-04 03:47:34 +00002503 }
2504 break;
2505 }
2506 return E;
2507}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002508
2509Expr *Expr::ignoreParenBaseCasts() {
2510 Expr *E = this;
2511 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002512 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002513 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2514 if (CE->getCastKind() == CK_DerivedToBase ||
2515 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2516 CE->getCastKind() == CK_NoOp) {
2517 E = CE->getSubExpr();
2518 continue;
2519 }
2520 }
2521
2522 return E;
2523 }
2524}
2525
John McCalleebc8322010-05-05 22:59:52 +00002526Expr *Expr::IgnoreParenImpCasts() {
2527 Expr *E = this;
2528 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002529 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002530 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002531 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002532 continue;
2533 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002534 if (MaterializeTemporaryExpr *Materialize
2535 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2536 E = Materialize->GetTemporaryExpr();
2537 continue;
2538 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002539 if (SubstNonTypeTemplateParmExpr *NTTP
2540 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2541 E = NTTP->getReplacement();
2542 continue;
2543 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002544 return E;
John McCalleebc8322010-05-05 22:59:52 +00002545 }
2546}
2547
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002548Expr *Expr::IgnoreConversionOperator() {
2549 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002550 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002551 return MCE->getImplicitObjectArgument();
2552 }
2553 return this;
2554}
2555
Chris Lattneref26c772009-03-13 17:28:01 +00002556/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2557/// value (including ptr->int casts of the same size). Strip off any
2558/// ParenExpr or CastExprs, returning their operand.
2559Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2560 Expr *E = this;
2561 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002562 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002563
Chris Lattneref26c772009-03-13 17:28:01 +00002564 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2565 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002566 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002567 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattneref26c772009-03-13 17:28:01 +00002569 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2570 E = SE;
2571 continue;
2572 }
Mike Stump11289f42009-09-09 15:08:12 +00002573
Abramo Bagnara932e3932010-10-15 07:51:18 +00002574 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002575 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002576 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002577 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002578 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2579 E = SE;
2580 continue;
2581 }
2582 }
Mike Stump11289f42009-09-09 15:08:12 +00002583
Douglas Gregor6a40b082011-09-08 17:56:33 +00002584 if (SubstNonTypeTemplateParmExpr *NTTP
2585 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2586 E = NTTP->getReplacement();
2587 continue;
2588 }
2589
Chris Lattneref26c772009-03-13 17:28:01 +00002590 return E;
2591 }
2592}
2593
Douglas Gregord196a582009-12-14 19:27:10 +00002594bool Expr::isDefaultArgument() const {
2595 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002596 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2597 E = M->GetTemporaryExpr();
2598
Douglas Gregord196a582009-12-14 19:27:10 +00002599 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2600 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002601
Douglas Gregord196a582009-12-14 19:27:10 +00002602 return isa<CXXDefaultArgExpr>(E);
2603}
Chris Lattneref26c772009-03-13 17:28:01 +00002604
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002605/// \brief Skip over any no-op casts and any temporary-binding
2606/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002607static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002608 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2609 E = M->GetTemporaryExpr();
2610
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002611 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002612 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002613 E = ICE->getSubExpr();
2614 else
2615 break;
2616 }
2617
2618 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2619 E = BE->getSubExpr();
2620
2621 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002622 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002623 E = ICE->getSubExpr();
2624 else
2625 break;
2626 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002627
2628 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002629}
2630
John McCall7a626f62010-09-15 10:14:12 +00002631/// isTemporaryObject - Determines if this expression produces a
2632/// temporary of the given class type.
2633bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2634 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2635 return false;
2636
Anders Carlsson66bbf502010-11-28 16:40:49 +00002637 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002638
John McCall02dc8c72010-09-15 20:59:13 +00002639 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002640 if (!E->Classify(C).isPRValue()) {
2641 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002642 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002643 return false;
2644 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002645
John McCallf4ee1dd2010-09-16 06:57:56 +00002646 // Black-list a few cases which yield pr-values of class type that don't
2647 // refer to temporaries of that type:
2648
2649 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002650 if (isa<ImplicitCastExpr>(E)) {
2651 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2652 case CK_DerivedToBase:
2653 case CK_UncheckedDerivedToBase:
2654 return false;
2655 default:
2656 break;
2657 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002658 }
2659
John McCallf4ee1dd2010-09-16 06:57:56 +00002660 // - member expressions (all)
2661 if (isa<MemberExpr>(E))
2662 return false;
2663
Eli Friedman13ffdd82012-06-15 23:51:06 +00002664 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2665 if (BO->isPtrMemOp())
2666 return false;
2667
John McCallc07a0c72011-02-17 10:25:35 +00002668 // - opaque values (all)
2669 if (isa<OpaqueValueExpr>(E))
2670 return false;
2671
John McCall7a626f62010-09-15 10:14:12 +00002672 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002673}
2674
Douglas Gregor25b7e052011-03-02 21:06:53 +00002675bool Expr::isImplicitCXXThis() const {
2676 const Expr *E = this;
2677
2678 // Strip away parentheses and casts we don't care about.
2679 while (true) {
2680 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2681 E = Paren->getSubExpr();
2682 continue;
2683 }
2684
2685 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2686 if (ICE->getCastKind() == CK_NoOp ||
2687 ICE->getCastKind() == CK_LValueToRValue ||
2688 ICE->getCastKind() == CK_DerivedToBase ||
2689 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2690 E = ICE->getSubExpr();
2691 continue;
2692 }
2693 }
2694
2695 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2696 if (UnOp->getOpcode() == UO_Extension) {
2697 E = UnOp->getSubExpr();
2698 continue;
2699 }
2700 }
2701
Douglas Gregorfe314812011-06-21 17:03:29 +00002702 if (const MaterializeTemporaryExpr *M
2703 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2704 E = M->GetTemporaryExpr();
2705 continue;
2706 }
2707
Douglas Gregor25b7e052011-03-02 21:06:53 +00002708 break;
2709 }
2710
2711 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2712 return This->isImplicit();
2713
2714 return false;
2715}
2716
Douglas Gregor4619e432008-12-05 23:32:09 +00002717/// hasAnyTypeDependentArguments - Determines if any of the expressions
2718/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002719bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002720 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002721 if (Exprs[I]->isTypeDependent())
2722 return true;
2723
2724 return false;
2725}
2726
Abramo Bagnara847c6602014-05-22 19:20:46 +00002727bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2728 const Expr **Culprit) const {
Eli Friedman384da272009-01-25 03:12:18 +00002729 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002730 // which can be evaluated at compile-time. It very closely parallels
2731 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2732 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2733 // to isEvaluatable most of the time.
2734 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002735 // If we ever capture reference-binding directly in the AST, we can
2736 // kill the second parameter.
2737
2738 if (IsForRef) {
2739 EvalResult Result;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002740 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2741 return true;
2742 if (Culprit)
2743 *Culprit = this;
2744 return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002745 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002746
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002747 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002748 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002749 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002750 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002751 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002752 case CXXTemporaryObjectExprClass:
2753 case CXXConstructExprClass: {
2754 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002755
Eli Friedman4c27ac22013-07-16 22:40:53 +00002756 if (CE->getConstructor()->isTrivial() &&
2757 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2758 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002759 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002760
Eli Friedman4c27ac22013-07-16 22:40:53 +00002761 // Trivial copy constructor
2762 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Abramo Bagnara847c6602014-05-22 19:20:46 +00002763 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62306a2011-11-10 06:34:14 +00002764 }
2765
Richard Smithd62306a2011-11-10 06:34:14 +00002766 break;
John McCall81c9cea2010-08-01 21:51:45 +00002767 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002768 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002769 // This handles gcc's extension that allows global initializers like
2770 // "struct x {int x;} x = (struct x) {};".
2771 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002772 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002773 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002774 }
Yunzhong Gaocb779302015-06-10 00:27:52 +00002775 case DesignatedInitUpdateExprClass: {
2776 const DesignatedInitUpdateExpr *DIUE = cast<DesignatedInitUpdateExpr>(this);
2777 return DIUE->getBase()->isConstantInitializer(Ctx, false, Culprit) &&
2778 DIUE->getUpdater()->isConstantInitializer(Ctx, false, Culprit);
2779 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002780 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002781 const InitListExpr *ILE = cast<InitListExpr>(this);
2782 if (ILE->getType()->isArrayType()) {
2783 unsigned numInits = ILE->getNumInits();
2784 for (unsigned i = 0; i < numInits; i++) {
Abramo Bagnara847c6602014-05-22 19:20:46 +00002785 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002786 return false;
2787 }
2788 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002789 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002790
2791 if (ILE->getType()->isRecordType()) {
2792 unsigned ElementNo = 0;
2793 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
Hans Wennborga302cd92014-08-21 16:06:57 +00002794 for (const auto *Field : RD->fields()) {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002795 // If this is a union, skip all the fields that aren't being initialized.
Hans Wennborga302cd92014-08-21 16:06:57 +00002796 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
Eli Friedman4c27ac22013-07-16 22:40:53 +00002797 continue;
2798
2799 // Don't emit anonymous bitfields, they just affect layout.
2800 if (Field->isUnnamedBitfield())
2801 continue;
2802
2803 if (ElementNo < ILE->getNumInits()) {
2804 const Expr *Elt = ILE->getInit(ElementNo++);
2805 if (Field->isBitField()) {
2806 // Bitfields have to evaluate to an integer.
2807 llvm::APSInt ResultTmp;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002808 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2809 if (Culprit)
2810 *Culprit = Elt;
Eli Friedman4c27ac22013-07-16 22:40:53 +00002811 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002812 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002813 } else {
2814 bool RefType = Field->getType()->isReferenceType();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002815 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002816 return false;
2817 }
2818 }
2819 }
2820 return true;
2821 }
2822
2823 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002824 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002825 case ImplicitValueInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00002826 case NoInitExprClass:
Douglas Gregor0202cb42009-01-29 17:44:32 +00002827 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002828 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002829 return cast<ParenExpr>(this)->getSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002830 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbourne91147592011-04-15 00:35:48 +00002831 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002832 return cast<GenericSelectionExpr>(this)->getResultExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002833 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002834 case ChooseExprClass:
Abramo Bagnara847c6602014-05-22 19:20:46 +00002835 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2836 if (Culprit)
2837 *Culprit = this;
Eli Friedman75807f22013-07-20 00:40:58 +00002838 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002839 }
Eli Friedman75807f22013-07-20 00:40:58 +00002840 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002841 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002842 case UnaryOperatorClass: {
2843 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002844 if (Exp->getOpcode() == UO_Extension)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002845 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002846 break;
2847 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002848 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002849 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002850 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002851 case CStyleCastExprClass:
2852 case ObjCBridgedCastExprClass:
2853 case CXXDynamicCastExprClass:
2854 case CXXReinterpretCastExprClass:
2855 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002856 const CastExpr *CE = cast<CastExpr>(this);
2857
Eli Friedman13ec75b2011-12-21 00:43:02 +00002858 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002859 if (CE->getCastKind() == CK_NoOp ||
2860 CE->getCastKind() == CK_LValueToRValue ||
2861 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002862 CE->getCastKind() == CK_ConstructorConversion ||
2863 CE->getCastKind() == CK_NonAtomicToAtomic ||
2864 CE->getCastKind() == CK_AtomicToNonAtomic)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002865 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smith161f09a2011-12-06 22:44:34 +00002866
Eli Friedman384da272009-01-25 03:12:18 +00002867 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002868 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002869 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002870 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002871 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002872
2873 case SubstNonTypeTemplateParmExprClass:
2874 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002875 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002876 case CXXDefaultArgExprClass:
2877 return cast<CXXDefaultArgExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002878 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002879 case CXXDefaultInitExprClass:
2880 return cast<CXXDefaultInitExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002881 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002882 }
Abramo Bagnara847c6602014-05-22 19:20:46 +00002883 if (isEvaluatable(Ctx))
2884 return true;
2885 if (Culprit)
2886 *Culprit = this;
2887 return false;
Steve Naroffb03f5942007-09-02 20:30:18 +00002888}
2889
Scott Douglasscc013592015-06-10 15:18:23 +00002890namespace {
2891 /// \brief Look for any side effects within a Stmt.
2892 class SideEffectFinder : public ConstEvaluatedExprVisitor<SideEffectFinder> {
2893 typedef ConstEvaluatedExprVisitor<SideEffectFinder> Inherited;
2894 const bool IncludePossibleEffects;
2895 bool HasSideEffects;
2896
2897 public:
2898 explicit SideEffectFinder(const ASTContext &Context, bool IncludePossible)
2899 : Inherited(Context),
2900 IncludePossibleEffects(IncludePossible), HasSideEffects(false) { }
2901
2902 bool hasSideEffects() const { return HasSideEffects; }
2903
2904 void VisitExpr(const Expr *E) {
2905 if (!HasSideEffects &&
2906 E->HasSideEffects(Context, IncludePossibleEffects))
2907 HasSideEffects = true;
2908 }
2909 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002910}
Scott Douglasscc013592015-06-10 15:18:23 +00002911
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002912bool Expr::HasSideEffects(const ASTContext &Ctx,
2913 bool IncludePossibleEffects) const {
2914 // In circumstances where we care about definite side effects instead of
2915 // potential side effects, we want to ignore expressions that are part of a
2916 // macro expansion as a potential side effect.
2917 if (!IncludePossibleEffects && getExprLoc().isMacroID())
2918 return false;
2919
Richard Smith0421ce72012-08-07 04:16:51 +00002920 if (isInstantiationDependent())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002921 return IncludePossibleEffects;
Richard Smith0421ce72012-08-07 04:16:51 +00002922
2923 switch (getStmtClass()) {
2924 case NoStmtClass:
2925 #define ABSTRACT_STMT(Type)
2926 #define STMT(Type, Base) case Type##Class:
2927 #define EXPR(Type, Base)
2928 #include "clang/AST/StmtNodes.inc"
2929 llvm_unreachable("unexpected Expr kind");
2930
2931 case DependentScopeDeclRefExprClass:
2932 case CXXUnresolvedConstructExprClass:
2933 case CXXDependentScopeMemberExprClass:
2934 case UnresolvedLookupExprClass:
2935 case UnresolvedMemberExprClass:
2936 case PackExpansionExprClass:
2937 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002938 case FunctionParmPackExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002939 case TypoExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00002940 case CXXFoldExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002941 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2942
Richard Smitha33e4fe2012-08-07 05:18:29 +00002943 case DeclRefExprClass:
2944 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002945 case PredefinedExprClass:
2946 case IntegerLiteralClass:
2947 case FloatingLiteralClass:
2948 case ImaginaryLiteralClass:
2949 case StringLiteralClass:
2950 case CharacterLiteralClass:
2951 case OffsetOfExprClass:
2952 case ImplicitValueInitExprClass:
2953 case UnaryExprOrTypeTraitExprClass:
2954 case AddrLabelExprClass:
2955 case GNUNullExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00002956 case NoInitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002957 case CXXBoolLiteralExprClass:
2958 case CXXNullPtrLiteralExprClass:
2959 case CXXThisExprClass:
2960 case CXXScalarValueInitExprClass:
2961 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002962 case ArrayTypeTraitExprClass:
2963 case ExpressionTraitExprClass:
2964 case CXXNoexceptExprClass:
2965 case SizeOfPackExprClass:
2966 case ObjCStringLiteralClass:
2967 case ObjCEncodeExprClass:
2968 case ObjCBoolLiteralExprClass:
2969 case CXXUuidofExprClass:
2970 case OpaqueValueExprClass:
2971 // These never have a side-effect.
2972 return false;
2973
2974 case CallExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002975 case CXXOperatorCallExprClass:
2976 case CXXMemberCallExprClass:
2977 case CUDAKernelCallExprClass:
Michael Kupersteinaed5ccd2015-04-06 13:22:01 +00002978 case UserDefinedLiteralClass: {
2979 // We don't know a call definitely has side effects, except for calls
2980 // to pure/const functions that definitely don't.
2981 // If the call itself is considered side-effect free, check the operands.
2982 const Decl *FD = cast<CallExpr>(this)->getCalleeDecl();
2983 bool IsPure = FD && (FD->hasAttr<ConstAttr>() || FD->hasAttr<PureAttr>());
2984 if (IsPure || !IncludePossibleEffects)
2985 break;
2986 return true;
2987 }
2988
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002989 case BlockExprClass:
2990 case CXXBindTemporaryExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002991 if (!IncludePossibleEffects)
2992 break;
2993 return true;
2994
John McCall5e77d762013-04-16 07:28:30 +00002995 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002996 case CompoundAssignOperatorClass:
2997 case VAArgExprClass:
2998 case AtomicExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002999 case CXXThrowExprClass:
3000 case CXXNewExprClass:
3001 case CXXDeleteExprClass:
3002 case ExprWithCleanupsClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003003 // These always have a side-effect.
3004 return true;
3005
Scott Douglasscc013592015-06-10 15:18:23 +00003006 case StmtExprClass: {
3007 // StmtExprs have a side-effect if any substatement does.
3008 SideEffectFinder Finder(Ctx, IncludePossibleEffects);
3009 Finder.Visit(cast<StmtExpr>(this)->getSubStmt());
3010 return Finder.hasSideEffects();
3011 }
3012
Richard Smith0421ce72012-08-07 04:16:51 +00003013 case ParenExprClass:
3014 case ArraySubscriptExprClass:
3015 case MemberExprClass:
3016 case ConditionalOperatorClass:
3017 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003018 case CompoundLiteralExprClass:
3019 case ExtVectorElementExprClass:
3020 case DesignatedInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00003021 case DesignatedInitUpdateExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003022 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003023 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00003024 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003025 case SubstNonTypeTemplateParmExprClass:
3026 case MaterializeTemporaryExprClass:
3027 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00003028 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003029 case AsTypeExprClass:
3030 // These have a side-effect if any subexpression does.
3031 break;
3032
Richard Smitha33e4fe2012-08-07 05:18:29 +00003033 case UnaryOperatorClass:
3034 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00003035 return true;
3036 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003037
3038 case BinaryOperatorClass:
3039 if (cast<BinaryOperator>(this)->isAssignmentOp())
3040 return true;
3041 break;
3042
Richard Smith0421ce72012-08-07 04:16:51 +00003043 case InitListExprClass:
3044 // FIXME: The children for an InitListExpr doesn't include the array filler.
3045 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003046 if (E->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003047 return true;
3048 break;
3049
3050 case GenericSelectionExprClass:
3051 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003052 HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003053
3054 case ChooseExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003055 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
3056 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003057
3058 case CXXDefaultArgExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003059 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
3060 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003061
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003062 case CXXDefaultInitExprClass: {
3063 const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3064 if (const Expr *E = FD->getInClassInitializer())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003065 return E->HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith852c9db2013-04-20 22:23:05 +00003066 // If we've not yet parsed the initializer, assume it has side-effects.
3067 return true;
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003068 }
Richard Smith852c9db2013-04-20 22:23:05 +00003069
Richard Smith0421ce72012-08-07 04:16:51 +00003070 case CXXDynamicCastExprClass: {
3071 // A dynamic_cast expression has side-effects if it can throw.
3072 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3073 if (DCE->getTypeAsWritten()->isReferenceType() &&
3074 DCE->getCastKind() == CK_Dynamic)
3075 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003076 } // Fall through.
3077 case ImplicitCastExprClass:
3078 case CStyleCastExprClass:
3079 case CXXStaticCastExprClass:
3080 case CXXReinterpretCastExprClass:
3081 case CXXConstCastExprClass:
3082 case CXXFunctionalCastExprClass: {
Aaron Ballman409af502015-01-03 17:00:12 +00003083 // While volatile reads are side-effecting in both C and C++, we treat them
3084 // as having possible (not definite) side-effects. This allows idiomatic
3085 // code to behave without warning, such as sizeof(*v) for a volatile-
3086 // qualified pointer.
3087 if (!IncludePossibleEffects)
3088 break;
3089
Richard Smitha33e4fe2012-08-07 05:18:29 +00003090 const CastExpr *CE = cast<CastExpr>(this);
3091 if (CE->getCastKind() == CK_LValueToRValue &&
3092 CE->getSubExpr()->getType().isVolatileQualified())
3093 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003094 break;
3095 }
3096
Richard Smithef8bf432012-08-13 20:08:14 +00003097 case CXXTypeidExprClass:
3098 // typeid might throw if its subexpression is potentially-evaluated, so has
3099 // side-effects in that case whether or not its subexpression does.
3100 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00003101
3102 case CXXConstructExprClass:
3103 case CXXTemporaryObjectExprClass: {
3104 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003105 if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
Richard Smith0421ce72012-08-07 04:16:51 +00003106 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003107 // A trivial constructor does not add any side-effects of its own. Just look
3108 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00003109 break;
3110 }
3111
3112 case LambdaExprClass: {
3113 const LambdaExpr *LE = cast<LambdaExpr>(this);
3114 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3115 E = LE->capture_end(); I != E; ++I)
3116 if (I->getCaptureKind() == LCK_ByCopy)
3117 // FIXME: Only has a side-effect if the variable is volatile or if
3118 // the copy would invoke a non-trivial copy constructor.
3119 return true;
3120 return false;
3121 }
3122
3123 case PseudoObjectExprClass: {
3124 // Only look for side-effects in the semantic form, and look past
3125 // OpaqueValueExpr bindings in that form.
3126 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3127 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3128 E = PO->semantics_end();
3129 I != E; ++I) {
3130 const Expr *Subexpr = *I;
3131 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3132 Subexpr = OVE->getSourceExpr();
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003133 if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003134 return true;
3135 }
3136 return false;
3137 }
3138
3139 case ObjCBoxedExprClass:
3140 case ObjCArrayLiteralClass:
3141 case ObjCDictionaryLiteralClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003142 case ObjCSelectorExprClass:
3143 case ObjCProtocolExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003144 case ObjCIsaExprClass:
3145 case ObjCIndirectCopyRestoreExprClass:
3146 case ObjCSubscriptRefExprClass:
3147 case ObjCBridgedCastExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003148 case ObjCMessageExprClass:
3149 case ObjCPropertyRefExprClass:
3150 // FIXME: Classify these cases better.
3151 if (IncludePossibleEffects)
3152 return true;
3153 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003154 }
3155
3156 // Recurse to children.
3157 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3158 if (const Stmt *S = *SubStmts)
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003159 if (cast<Expr>(S)->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003160 return true;
3161
3162 return false;
3163}
3164
Douglas Gregor1be329d2012-02-23 07:33:15 +00003165namespace {
3166 /// \brief Look for a call to a non-trivial function within an expression.
Scott Douglass503fc392015-06-10 13:53:15 +00003167 class NonTrivialCallFinder : public ConstEvaluatedExprVisitor<NonTrivialCallFinder>
Douglas Gregor1be329d2012-02-23 07:33:15 +00003168 {
Scott Douglass503fc392015-06-10 13:53:15 +00003169 typedef ConstEvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3170
Douglas Gregor1be329d2012-02-23 07:33:15 +00003171 bool NonTrivial;
3172
3173 public:
Scott Douglass503fc392015-06-10 13:53:15 +00003174 explicit NonTrivialCallFinder(const ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003175 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003176
3177 bool hasNonTrivialCall() const { return NonTrivial; }
Scott Douglass503fc392015-06-10 13:53:15 +00003178
3179 void VisitCallExpr(const CallExpr *E) {
3180 if (const CXXMethodDecl *Method
3181 = dyn_cast_or_null<const CXXMethodDecl>(E->getCalleeDecl())) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003182 if (Method->isTrivial()) {
3183 // Recurse to children of the call.
3184 Inherited::VisitStmt(E);
3185 return;
3186 }
3187 }
3188
3189 NonTrivial = true;
3190 }
Scott Douglass503fc392015-06-10 13:53:15 +00003191
3192 void VisitCXXConstructExpr(const CXXConstructExpr *E) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003193 if (E->getConstructor()->isTrivial()) {
3194 // Recurse to children of the call.
3195 Inherited::VisitStmt(E);
3196 return;
3197 }
3198
3199 NonTrivial = true;
3200 }
Scott Douglass503fc392015-06-10 13:53:15 +00003201
3202 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003203 if (E->getTemporary()->getDestructor()->isTrivial()) {
3204 Inherited::VisitStmt(E);
3205 return;
3206 }
3207
3208 NonTrivial = true;
3209 }
3210 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003211}
Douglas Gregor1be329d2012-02-23 07:33:15 +00003212
Scott Douglass503fc392015-06-10 13:53:15 +00003213bool Expr::hasNonTrivialCall(const ASTContext &Ctx) const {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003214 NonTrivialCallFinder Finder(Ctx);
3215 Finder.Visit(this);
3216 return Finder.hasNonTrivialCall();
3217}
3218
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003219/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3220/// pointer constant or not, as well as the specific kind of constant detected.
3221/// Null pointer constants can be integer constant expressions with the
3222/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3223/// (a GNU extension).
3224Expr::NullPointerConstantKind
3225Expr::isNullPointerConstant(ASTContext &Ctx,
3226 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003227 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003228 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003229 switch (NPC) {
3230 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003231 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003232 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003233 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003234 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003235 else
3236 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003237
Douglas Gregor56751b52009-09-25 04:25:58 +00003238 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003239 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003240 }
3241 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003242
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003243 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003244 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003245 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003246 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003247 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003248 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003249 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003250 Pointee->isVoidType() && // to void*
3251 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003252 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003253 }
Steve Naroffada7d422007-05-20 17:54:12 +00003254 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003255 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3256 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003257 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003258 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3259 // Accept ((void*)0) as a null pointer constant, as many other
3260 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003261 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003262 } else if (const GenericSelectionExpr *GE =
3263 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003264 if (GE->isResultDependent())
3265 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003266 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003267 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3268 if (CE->isConditionDependent())
3269 return NPCK_NotNull;
3270 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003271 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003272 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003273 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003274 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003275 } else if (const CXXDefaultInitExpr *DefaultInit
3276 = dyn_cast<CXXDefaultInitExpr>(this)) {
3277 // See through default initializer expressions.
3278 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003279 } else if (isa<GNUNullExpr>(this)) {
3280 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003281 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003282 } else if (const MaterializeTemporaryExpr *M
3283 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3284 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003285 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3286 if (const Expr *Source = OVE->getSourceExpr())
3287 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003288 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003289
Richard Smith89645bc2013-01-02 12:01:23 +00003290 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003291 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003292 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003293
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003294 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003295 if (!Ctx.getLangOpts().CPlusPlus11 &&
3296 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003297 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3298 const Expr *InitExpr = CLE->getInitializer();
3299 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3300 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3301 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003302 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003303 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003304 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003305 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003306
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003307 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003308 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3309 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003310 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003311 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003312 if (Lit && !Lit->getValue())
3313 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003314 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003315 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003316 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003317 // If we have an integer constant expression, we need to *evaluate* it and
3318 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003319 if (!isIntegerConstantExpr(Ctx))
3320 return NPCK_NotNull;
3321 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003322
David Blaikie1c7c8f72012-08-08 17:33:31 +00003323 if (EvaluateKnownConstInt(Ctx) != 0)
3324 return NPCK_NotNull;
3325
3326 if (isa<IntegerLiteral>(this))
3327 return NPCK_ZeroLiteral;
3328 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003329}
Steve Narofff7a5da12007-07-28 23:10:27 +00003330
John McCall34376a62010-12-04 03:47:34 +00003331/// \brief If this expression is an l-value for an Objective C
3332/// property, find the underlying property reference expression.
3333const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3334 const Expr *E = this;
3335 while (true) {
3336 assert((E->getValueKind() == VK_LValue &&
3337 E->getObjectKind() == OK_ObjCProperty) &&
3338 "expression is not a property reference");
3339 E = E->IgnoreParenCasts();
3340 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3341 if (BO->getOpcode() == BO_Comma) {
3342 E = BO->getRHS();
3343 continue;
3344 }
3345 }
3346
3347 break;
3348 }
3349
3350 return cast<ObjCPropertyRefExpr>(E);
3351}
3352
Anna Zaks97c7ce32012-10-01 20:34:04 +00003353bool Expr::isObjCSelfExpr() const {
3354 const Expr *E = IgnoreParenImpCasts();
3355
3356 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3357 if (!DRE)
3358 return false;
3359
3360 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3361 if (!Param)
3362 return false;
3363
3364 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3365 if (!M)
3366 return false;
3367
3368 return M->getSelfDecl() == Param;
3369}
3370
John McCalld25db7e2013-05-06 21:39:12 +00003371FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003372 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003373
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003374 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003375 if (ICE->getCastKind() == CK_LValueToRValue ||
3376 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003377 E = ICE->getSubExpr()->IgnoreParens();
3378 else
3379 break;
3380 }
3381
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003382 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003383 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003384 if (Field->isBitField())
3385 return Field;
3386
John McCalld25db7e2013-05-06 21:39:12 +00003387 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3388 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3389 if (Ivar->isBitField())
3390 return Ivar;
3391
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003392 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3393 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3394 if (Field->isBitField())
3395 return Field;
3396
Eli Friedman609ada22011-07-13 02:05:57 +00003397 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003398 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003399 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003400
Eli Friedman609ada22011-07-13 02:05:57 +00003401 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003402 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003403 }
3404
Richard Smith5b571672014-09-24 23:55:00 +00003405 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3406 if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3407 return UnOp->getSubExpr()->getSourceBitField();
3408
Craig Topper36250ad2014-05-12 05:36:57 +00003409 return nullptr;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003410}
3411
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003412bool Expr::refersToVectorElement() const {
3413 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003414
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003415 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003416 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003417 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003418 E = ICE->getSubExpr()->IgnoreParens();
3419 else
3420 break;
3421 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003422
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003423 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3424 return ASE->getBase()->getType()->isVectorType();
3425
3426 if (isa<ExtVectorElementExpr>(E))
3427 return true;
3428
3429 return false;
3430}
3431
Chris Lattnerb8211f62009-02-16 22:14:05 +00003432/// isArrow - Return true if the base expression is a pointer to vector,
3433/// return false if the base expression is a vector.
3434bool ExtVectorElementExpr::isArrow() const {
3435 return getBase()->getType()->isPointerType();
3436}
3437
Nate Begemance4d7fc2008-04-18 23:10:10 +00003438unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003439 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003440 return VT->getNumElements();
3441 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003442}
3443
Nate Begemanf322eab2008-05-09 06:41:27 +00003444/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003445bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003446 // FIXME: Refactor this code to an accessor on the AST node which returns the
3447 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003448 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003449
3450 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003451 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003452 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003453
Nate Begeman7e5185b2009-01-18 02:01:21 +00003454 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003455 if (Comp[0] == 's' || Comp[0] == 'S')
3456 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003457
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003458 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003459 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003460 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003461
Steve Naroff0d595ca2007-07-30 03:29:09 +00003462 return false;
3463}
Chris Lattner885b4952007-08-02 23:36:59 +00003464
Nate Begemanf322eab2008-05-09 06:41:27 +00003465/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003466void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003467 SmallVectorImpl<unsigned> &Elts) const {
3468 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003469 if (Comp[0] == 's' || Comp[0] == 'S')
3470 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003471
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003472 bool isHi = Comp == "hi";
3473 bool isLo = Comp == "lo";
3474 bool isEven = Comp == "even";
3475 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003476
Nate Begemanf322eab2008-05-09 06:41:27 +00003477 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3478 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003479
Nate Begemanf322eab2008-05-09 06:41:27 +00003480 if (isHi)
3481 Index = e + i;
3482 else if (isLo)
3483 Index = i;
3484 else if (isEven)
3485 Index = 2 * i;
3486 else if (isOdd)
3487 Index = 2 * i + 1;
3488 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003489 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003490
Nate Begemand3862152008-05-13 21:03:02 +00003491 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003492 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003493}
3494
Douglas Gregor9a129192010-04-21 00:45:42 +00003495ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003496 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003497 SourceLocation LBracLoc,
3498 SourceLocation SuperLoc,
3499 bool IsInstanceSuper,
3500 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003501 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003502 ArrayRef<SourceLocation> SelLocs,
3503 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003504 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003505 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003506 SourceLocation RBracLoc,
3507 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003508 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003509 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003510 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003511 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003512 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3513 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003514 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Craig Topper36250ad2014-05-12 05:36:57 +00003515 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3516 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3517 RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003518{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003519 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003520 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003521}
3522
Douglas Gregor9a129192010-04-21 00:45:42 +00003523ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003524 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003525 SourceLocation LBracLoc,
3526 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003527 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003528 ArrayRef<SourceLocation> SelLocs,
3529 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003530 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003531 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003532 SourceLocation RBracLoc,
3533 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003534 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003535 T->isDependentType(), T->isInstantiationDependentType(),
3536 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003537 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3538 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003539 Kind(Class),
Craig Topper36250ad2014-05-12 05:36:57 +00003540 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3541 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003542{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003543 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003544 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003545}
3546
Douglas Gregor9a129192010-04-21 00:45:42 +00003547ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003548 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003549 SourceLocation LBracLoc,
3550 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003551 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003552 ArrayRef<SourceLocation> SelLocs,
3553 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003554 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003555 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003556 SourceLocation RBracLoc,
3557 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003558 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003559 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003560 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003561 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003562 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3563 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003564 Kind(Instance),
Craig Topper36250ad2014-05-12 05:36:57 +00003565 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3566 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003567{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003568 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003569 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003570}
3571
3572void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3573 ArrayRef<SourceLocation> SelLocs,
3574 SelectorLocationsKind SelLocsK) {
3575 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003576 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003577 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003578 if (Args[I]->isTypeDependent())
3579 ExprBits.TypeDependent = true;
3580 if (Args[I]->isValueDependent())
3581 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003582 if (Args[I]->isInstantiationDependent())
3583 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003584 if (Args[I]->containsUnexpandedParameterPack())
3585 ExprBits.ContainsUnexpandedParameterPack = true;
3586
3587 MyArgs[I] = Args[I];
3588 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003589
Benjamin Kramer2325b242012-02-20 00:20:48 +00003590 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003591 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003592 if (SelLocsK == SelLoc_NonStandard)
3593 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3594 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003595}
3596
Craig Topperce7167c2013-08-22 04:58:56 +00003597ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003598 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003599 SourceLocation LBracLoc,
3600 SourceLocation SuperLoc,
3601 bool IsInstanceSuper,
3602 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003603 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003604 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003605 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003606 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003607 SourceLocation RBracLoc,
3608 bool isImplicit) {
3609 assert((!SelLocs.empty() || isImplicit) &&
3610 "No selector locs for non-implicit message");
3611 ObjCMessageExpr *Mem;
3612 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3613 if (isImplicit)
3614 Mem = alloc(Context, Args.size(), 0);
3615 else
3616 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003617 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003618 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003619 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003620}
3621
Craig Topperce7167c2013-08-22 04:58:56 +00003622ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003623 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003624 SourceLocation LBracLoc,
3625 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003626 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003627 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003628 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003629 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003630 SourceLocation RBracLoc,
3631 bool isImplicit) {
3632 assert((!SelLocs.empty() || isImplicit) &&
3633 "No selector locs for non-implicit message");
3634 ObjCMessageExpr *Mem;
3635 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3636 if (isImplicit)
3637 Mem = alloc(Context, Args.size(), 0);
3638 else
3639 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003640 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003641 SelLocs, SelLocsK, Method, Args, RBracLoc,
3642 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003643}
3644
Craig Topperce7167c2013-08-22 04:58:56 +00003645ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003646 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003647 SourceLocation LBracLoc,
3648 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003649 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003650 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003651 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003652 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003653 SourceLocation RBracLoc,
3654 bool isImplicit) {
3655 assert((!SelLocs.empty() || isImplicit) &&
3656 "No selector locs for non-implicit message");
3657 ObjCMessageExpr *Mem;
3658 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3659 if (isImplicit)
3660 Mem = alloc(Context, Args.size(), 0);
3661 else
3662 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003663 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003664 SelLocs, SelLocsK, Method, Args, RBracLoc,
3665 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003666}
3667
Craig Topperce7167c2013-08-22 04:58:56 +00003668ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003669 unsigned NumArgs,
3670 unsigned NumStoredSelLocs) {
3671 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003672 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3673}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003674
Craig Topperce7167c2013-08-22 04:58:56 +00003675ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003676 ArrayRef<Expr *> Args,
3677 SourceLocation RBraceLoc,
3678 ArrayRef<SourceLocation> SelLocs,
3679 Selector Sel,
3680 SelectorLocationsKind &SelLocsK) {
3681 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3682 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3683 : 0;
3684 return alloc(C, Args.size(), NumStoredSelLocs);
3685}
3686
Craig Topperce7167c2013-08-22 04:58:56 +00003687ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003688 unsigned NumArgs,
3689 unsigned NumStoredSelLocs) {
3690 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3691 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3692 return (ObjCMessageExpr *)C.Allocate(Size,
3693 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3694}
3695
3696void ObjCMessageExpr::getSelectorLocs(
3697 SmallVectorImpl<SourceLocation> &SelLocs) const {
3698 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3699 SelLocs.push_back(getSelectorLoc(i));
3700}
3701
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003702SourceRange ObjCMessageExpr::getReceiverRange() const {
3703 switch (getReceiverKind()) {
3704 case Instance:
3705 return getInstanceReceiver()->getSourceRange();
3706
3707 case Class:
3708 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3709
3710 case SuperInstance:
3711 case SuperClass:
3712 return getSuperLoc();
3713 }
3714
David Blaikiee4d798f2012-01-20 21:50:17 +00003715 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003716}
3717
Douglas Gregor9a129192010-04-21 00:45:42 +00003718Selector ObjCMessageExpr::getSelector() const {
3719 if (HasMethod)
3720 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3721 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003722 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003723}
3724
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003725QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003726 switch (getReceiverKind()) {
3727 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003728 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003729 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003730 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003731 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003732 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003733 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003734 }
3735
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003736 llvm_unreachable("unexpected receiver kind");
3737}
3738
3739ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3740 QualType T = getReceiverType();
3741
3742 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3743 return Ptr->getInterfaceDecl();
3744
3745 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3746 return Ty->getInterface();
3747
Craig Topper36250ad2014-05-12 05:36:57 +00003748 return nullptr;
Ted Kremenek2c809302010-02-11 22:41:21 +00003749}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003750
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003751StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003752 switch (getBridgeKind()) {
3753 case OBC_Bridge:
3754 return "__bridge";
3755 case OBC_BridgeTransfer:
3756 return "__bridge_transfer";
3757 case OBC_BridgeRetained:
3758 return "__bridge_retained";
3759 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003760
3761 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003762}
3763
Craig Topper37932912013-08-18 10:09:15 +00003764ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003765 QualType Type, SourceLocation BLoc,
3766 SourceLocation RP)
3767 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3768 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003769 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003770 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003771 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003772{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003773 SubExprs = new (C) Stmt*[args.size()];
3774 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003775 if (args[i]->isTypeDependent())
3776 ExprBits.TypeDependent = true;
3777 if (args[i]->isValueDependent())
3778 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003779 if (args[i]->isInstantiationDependent())
3780 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003781 if (args[i]->containsUnexpandedParameterPack())
3782 ExprBits.ContainsUnexpandedParameterPack = true;
3783
3784 SubExprs[i] = args[i];
3785 }
3786}
3787
Craig Topper37932912013-08-18 10:09:15 +00003788void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003789 if (SubExprs) C.Deallocate(SubExprs);
3790
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003791 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003792 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003793 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003794}
Nate Begeman48745922009-08-12 02:28:50 +00003795
Craig Topper37932912013-08-18 10:09:15 +00003796GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003797 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003798 ArrayRef<TypeSourceInfo*> AssocTypes,
3799 ArrayRef<Expr*> AssocExprs,
3800 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003801 SourceLocation RParenLoc,
3802 bool ContainsUnexpandedParameterPack,
3803 unsigned ResultIndex)
3804 : Expr(GenericSelectionExprClass,
3805 AssocExprs[ResultIndex]->getType(),
3806 AssocExprs[ResultIndex]->getValueKind(),
3807 AssocExprs[ResultIndex]->getObjectKind(),
3808 AssocExprs[ResultIndex]->isTypeDependent(),
3809 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003810 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003811 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003812 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3813 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3814 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3815 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003816 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003817 assert(AssocTypes.size() == AssocExprs.size());
3818 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3819 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003820}
3821
Craig Topper37932912013-08-18 10:09:15 +00003822GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003823 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003824 ArrayRef<TypeSourceInfo*> AssocTypes,
3825 ArrayRef<Expr*> AssocExprs,
3826 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003827 SourceLocation RParenLoc,
3828 bool ContainsUnexpandedParameterPack)
3829 : Expr(GenericSelectionExprClass,
3830 Context.DependentTy,
3831 VK_RValue,
3832 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003833 /*isTypeDependent=*/true,
3834 /*isValueDependent=*/true,
3835 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003836 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003837 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3838 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3839 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3840 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003841 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003842 assert(AssocTypes.size() == AssocExprs.size());
3843 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3844 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003845}
3846
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003847//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003848// DesignatedInitExpr
3849//===----------------------------------------------------------------------===//
3850
Chandler Carruth631abd92011-06-16 06:47:06 +00003851IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003852 assert(Kind == FieldDesignator && "Only valid on a field designator");
3853 if (Field.NameOrField & 0x01)
3854 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3855 else
3856 return getField()->getIdentifier();
3857}
3858
Craig Topper37932912013-08-18 10:09:15 +00003859DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003860 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003861 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003862 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003863 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003864 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003865 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003866 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003867 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003868 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003869 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003870 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003871 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003872 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003873 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003874
3875 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003876 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003877 *Child++ = Init;
3878
3879 // Copy the designators and their subexpressions, computing
3880 // value-dependence along the way.
3881 unsigned IndexIdx = 0;
3882 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003883 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003884
3885 if (this->Designators[I].isArrayDesignator()) {
3886 // Compute type- and value-dependence.
3887 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003888 if (Index->isTypeDependent() || Index->isValueDependent())
David Majnemer4f217682015-01-09 01:39:09 +00003889 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003890 if (Index->isInstantiationDependent())
3891 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003892 // Propagate unexpanded parameter packs.
3893 if (Index->containsUnexpandedParameterPack())
3894 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003895
3896 // Copy the index expressions into permanent storage.
3897 *Child++ = IndexExprs[IndexIdx++];
3898 } else if (this->Designators[I].isArrayRangeDesignator()) {
3899 // Compute type- and value-dependence.
3900 Expr *Start = IndexExprs[IndexIdx];
3901 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003902 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003903 End->isTypeDependent() || End->isValueDependent()) {
David Majnemer4f217682015-01-09 01:39:09 +00003904 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003905 ExprBits.InstantiationDependent = true;
3906 } else if (Start->isInstantiationDependent() ||
3907 End->isInstantiationDependent()) {
3908 ExprBits.InstantiationDependent = true;
3909 }
3910
Douglas Gregora6e053e2010-12-15 01:34:56 +00003911 // Propagate unexpanded parameter packs.
3912 if (Start->containsUnexpandedParameterPack() ||
3913 End->containsUnexpandedParameterPack())
3914 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003915
3916 // Copy the start/end expressions into permanent storage.
3917 *Child++ = IndexExprs[IndexIdx++];
3918 *Child++ = IndexExprs[IndexIdx++];
3919 }
3920 }
3921
Benjamin Kramerc215e762012-08-24 11:54:20 +00003922 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003923}
3924
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003925DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003926DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003927 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003928 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003929 SourceLocation ColonOrEqualLoc,
3930 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003931 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003932 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003933 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003934 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003935 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003936}
3937
Craig Topper37932912013-08-18 10:09:15 +00003938DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003939 unsigned NumIndexExprs) {
3940 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3941 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3942 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3943}
3944
Craig Topper37932912013-08-18 10:09:15 +00003945void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003946 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003947 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003948 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003949 NumDesignators = NumDesigs;
3950 for (unsigned I = 0; I != NumDesigs; ++I)
3951 Designators[I] = Desigs[I];
3952}
3953
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003954SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3955 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3956 if (size() == 1)
3957 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003958 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3959 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003960}
3961
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003962SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003963 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003964 Designator &First =
3965 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003966 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003967 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003968 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3969 else
3970 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3971 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003972 StartLoc =
3973 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003974 return StartLoc;
3975}
3976
3977SourceLocation DesignatedInitExpr::getLocEnd() const {
3978 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003979}
3980
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003981Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003982 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003983 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003984 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3985}
3986
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003987Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003988 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003989 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003990 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003991 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3992}
3993
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003994Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003995 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003996 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003997 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003998 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3999}
4000
Douglas Gregord5846a12009-04-15 06:41:24 +00004001/// \brief Replaces the designator at index @p Idx with the series
4002/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00004003void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00004004 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00004005 const Designator *Last) {
4006 unsigned NumNewDesignators = Last - First;
4007 if (NumNewDesignators == 0) {
4008 std::copy_backward(Designators + Idx + 1,
4009 Designators + NumDesignators,
4010 Designators + Idx);
4011 --NumNewDesignators;
4012 return;
4013 } else if (NumNewDesignators == 1) {
4014 Designators[Idx] = *First;
4015 return;
4016 }
4017
Mike Stump11289f42009-09-09 15:08:12 +00004018 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00004019 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00004020 std::copy(Designators, Designators + Idx, NewDesignators);
4021 std::copy(First, Last, NewDesignators + Idx);
4022 std::copy(Designators + Idx + 1, Designators + NumDesignators,
4023 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00004024 Designators = NewDesignators;
4025 NumDesignators = NumDesignators - 1 + NumNewDesignators;
4026}
4027
Yunzhong Gaocb779302015-06-10 00:27:52 +00004028DesignatedInitUpdateExpr::DesignatedInitUpdateExpr(const ASTContext &C,
4029 SourceLocation lBraceLoc, Expr *baseExpr, SourceLocation rBraceLoc)
4030 : Expr(DesignatedInitUpdateExprClass, baseExpr->getType(), VK_RValue,
4031 OK_Ordinary, false, false, false, false) {
4032 BaseAndUpdaterExprs[0] = baseExpr;
4033
4034 InitListExpr *ILE = new (C) InitListExpr(C, lBraceLoc, None, rBraceLoc);
4035 ILE->setType(baseExpr->getType());
4036 BaseAndUpdaterExprs[1] = ILE;
4037}
4038
4039SourceLocation DesignatedInitUpdateExpr::getLocStart() const {
4040 return getBase()->getLocStart();
4041}
4042
4043SourceLocation DesignatedInitUpdateExpr::getLocEnd() const {
4044 return getBase()->getLocEnd();
4045}
4046
Craig Topper37932912013-08-18 10:09:15 +00004047ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00004048 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00004049 SourceLocation rparenloc)
4050 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00004051 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004052 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
4053 Exprs = new (C) Stmt*[exprs.size()];
4054 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00004055 if (exprs[i]->isTypeDependent())
4056 ExprBits.TypeDependent = true;
4057 if (exprs[i]->isValueDependent())
4058 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00004059 if (exprs[i]->isInstantiationDependent())
4060 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00004061 if (exprs[i]->containsUnexpandedParameterPack())
4062 ExprBits.ContainsUnexpandedParameterPack = true;
4063
Nate Begeman5ec4b312009-08-10 23:49:36 +00004064 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00004065 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00004066}
4067
John McCall1bf58462011-02-16 08:02:54 +00004068const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
4069 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
4070 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00004071 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
4072 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00004073 e = cast<CXXConstructExpr>(e)->getArg(0);
4074 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
4075 e = ice->getSubExpr();
4076 return cast<OpaqueValueExpr>(e);
4077}
4078
Craig Topper37932912013-08-18 10:09:15 +00004079PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
4080 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00004081 unsigned numSemanticExprs) {
4082 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
4083 (1 + numSemanticExprs) * sizeof(Expr*),
4084 llvm::alignOf<PseudoObjectExpr>());
4085 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
4086}
4087
4088PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
4089 : Expr(PseudoObjectExprClass, shell) {
4090 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
4091}
4092
Craig Topper37932912013-08-18 10:09:15 +00004093PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00004094 ArrayRef<Expr*> semantics,
4095 unsigned resultIndex) {
4096 assert(syntax && "no syntactic expression!");
4097 assert(semantics.size() && "no semantic expressions!");
4098
4099 QualType type;
4100 ExprValueKind VK;
4101 if (resultIndex == NoResult) {
4102 type = C.VoidTy;
4103 VK = VK_RValue;
4104 } else {
4105 assert(resultIndex < semantics.size());
4106 type = semantics[resultIndex]->getType();
4107 VK = semantics[resultIndex]->getValueKind();
4108 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4109 }
4110
4111 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4112 (1 + semantics.size()) * sizeof(Expr*),
4113 llvm::alignOf<PseudoObjectExpr>());
4114 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4115 resultIndex);
4116}
4117
4118PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4119 Expr *syntax, ArrayRef<Expr*> semantics,
4120 unsigned resultIndex)
4121 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4122 /*filled in at end of ctor*/ false, false, false, false) {
4123 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4124 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4125
4126 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4127 Expr *E = (i == 0 ? syntax : semantics[i-1]);
4128 getSubExprsBuffer()[i] = E;
4129
4130 if (E->isTypeDependent())
4131 ExprBits.TypeDependent = true;
4132 if (E->isValueDependent())
4133 ExprBits.ValueDependent = true;
4134 if (E->isInstantiationDependent())
4135 ExprBits.InstantiationDependent = true;
4136 if (E->containsUnexpandedParameterPack())
4137 ExprBits.ContainsUnexpandedParameterPack = true;
4138
4139 if (isa<OpaqueValueExpr>(E))
Craig Topper36250ad2014-05-12 05:36:57 +00004140 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCallfe96e0b2011-11-06 09:01:30 +00004141 "opaque-value semantic expressions for pseudo-object "
4142 "operations must have sources");
4143 }
4144}
4145
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004146//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00004147// ExprIterator.
4148//===----------------------------------------------------------------------===//
4149
4150Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
4151Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
4152Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
4153const Expr* ConstExprIterator::operator[](size_t idx) const {
4154 return cast<Expr>(I[idx]);
4155}
4156const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
4157const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4158
4159//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00004160// Child Iterators for iterating over subexpressions/substatements
4161//===----------------------------------------------------------------------===//
4162
Peter Collingbournee190dee2011-03-11 19:24:49 +00004163// UnaryExprOrTypeTraitExpr
4164Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00004165 // If this is of a type and the type is a VLA type (and not a typedef), the
4166 // size expression of the VLA needs to be treated as an executable expression.
4167 // Why isn't this weirdness documented better in StmtIterator?
4168 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00004169 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00004170 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00004171 return child_range(child_iterator(T), child_iterator());
4172 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00004173 }
John McCallbd066782011-02-09 08:16:59 +00004174 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00004175}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004176
Steve Naroffd54978b2007-09-18 23:55:05 +00004177// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004178Stmt::child_range ObjCMessageExpr::children() {
4179 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004180 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004181 begin = reinterpret_cast<Stmt **>(this + 1);
4182 else
4183 begin = reinterpret_cast<Stmt **>(getArgs());
4184 return child_range(begin,
4185 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004186}
4187
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004188ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004189 QualType T, ObjCMethodDecl *Method,
4190 SourceRange SR)
4191 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4192 false, false, false, false),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004193 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004194{
4195 Expr **SaveElements = getElements();
4196 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4197 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4198 ExprBits.ValueDependent = true;
4199 if (Elements[I]->isInstantiationDependent())
4200 ExprBits.InstantiationDependent = true;
4201 if (Elements[I]->containsUnexpandedParameterPack())
4202 ExprBits.ContainsUnexpandedParameterPack = true;
4203
4204 SaveElements[I] = Elements[I];
4205 }
4206}
4207
Craig Topperce7167c2013-08-22 04:58:56 +00004208ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004209 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004210 QualType T, ObjCMethodDecl * Method,
4211 SourceRange SR) {
4212 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4213 + Elements.size() * sizeof(Expr *));
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004214 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004215}
4216
Craig Topperce7167c2013-08-22 04:58:56 +00004217ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004218 unsigned NumElements) {
4219
4220 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4221 + NumElements * sizeof(Expr *));
4222 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4223}
4224
4225ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4226 ArrayRef<ObjCDictionaryElement> VK,
4227 bool HasPackExpansions,
4228 QualType T, ObjCMethodDecl *method,
4229 SourceRange SR)
4230 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4231 false, false),
4232 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004233 DictWithObjectsMethod(method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004234{
4235 KeyValuePair *KeyValues = getKeyValues();
4236 ExpansionData *Expansions = getExpansionData();
4237 for (unsigned I = 0; I < NumElements; I++) {
4238 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4239 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4240 ExprBits.ValueDependent = true;
4241 if (VK[I].Key->isInstantiationDependent() ||
4242 VK[I].Value->isInstantiationDependent())
4243 ExprBits.InstantiationDependent = true;
4244 if (VK[I].EllipsisLoc.isInvalid() &&
4245 (VK[I].Key->containsUnexpandedParameterPack() ||
4246 VK[I].Value->containsUnexpandedParameterPack()))
4247 ExprBits.ContainsUnexpandedParameterPack = true;
4248
4249 KeyValues[I].Key = VK[I].Key;
4250 KeyValues[I].Value = VK[I].Value;
4251 if (Expansions) {
4252 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4253 if (VK[I].NumExpansions)
4254 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4255 else
4256 Expansions[I].NumExpansionsPlusOne = 0;
4257 }
4258 }
4259}
4260
4261ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004262ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004263 ArrayRef<ObjCDictionaryElement> VK,
4264 bool HasPackExpansions,
4265 QualType T, ObjCMethodDecl *method,
4266 SourceRange SR) {
4267 unsigned ExpansionsSize = 0;
4268 if (HasPackExpansions)
4269 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4270
4271 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4272 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004273 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004274}
4275
4276ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004277ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004278 bool HasPackExpansions) {
4279 unsigned ExpansionsSize = 0;
4280 if (HasPackExpansions)
4281 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4282 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4283 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4284 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4285 HasPackExpansions);
4286}
4287
Craig Topperce7167c2013-08-22 04:58:56 +00004288ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004289 Expr *base,
4290 Expr *key, QualType T,
4291 ObjCMethodDecl *getMethod,
4292 ObjCMethodDecl *setMethod,
4293 SourceLocation RB) {
4294 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4295 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4296 OK_ObjCSubscript,
4297 getMethod, setMethod, RB);
4298}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004299
Benjamin Kramerc215e762012-08-24 11:54:20 +00004300AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004301 QualType t, AtomicOp op, SourceLocation RP)
4302 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4303 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004304 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004305{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004306 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4307 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004308 if (args[i]->isTypeDependent())
4309 ExprBits.TypeDependent = true;
4310 if (args[i]->isValueDependent())
4311 ExprBits.ValueDependent = true;
4312 if (args[i]->isInstantiationDependent())
4313 ExprBits.InstantiationDependent = true;
4314 if (args[i]->containsUnexpandedParameterPack())
4315 ExprBits.ContainsUnexpandedParameterPack = true;
4316
4317 SubExprs[i] = args[i];
4318 }
4319}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004320
4321unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4322 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004323 case AO__c11_atomic_init:
4324 case AO__c11_atomic_load:
4325 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004326 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004327
4328 case AO__c11_atomic_store:
4329 case AO__c11_atomic_exchange:
4330 case AO__atomic_load:
4331 case AO__atomic_store:
4332 case AO__atomic_store_n:
4333 case AO__atomic_exchange_n:
4334 case AO__c11_atomic_fetch_add:
4335 case AO__c11_atomic_fetch_sub:
4336 case AO__c11_atomic_fetch_and:
4337 case AO__c11_atomic_fetch_or:
4338 case AO__c11_atomic_fetch_xor:
4339 case AO__atomic_fetch_add:
4340 case AO__atomic_fetch_sub:
4341 case AO__atomic_fetch_and:
4342 case AO__atomic_fetch_or:
4343 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004344 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004345 case AO__atomic_add_fetch:
4346 case AO__atomic_sub_fetch:
4347 case AO__atomic_and_fetch:
4348 case AO__atomic_or_fetch:
4349 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004350 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004351 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004352
4353 case AO__atomic_exchange:
4354 return 4;
4355
4356 case AO__c11_atomic_compare_exchange_strong:
4357 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004358 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004359
4360 case AO__atomic_compare_exchange:
4361 case AO__atomic_compare_exchange_n:
4362 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004363 }
4364 llvm_unreachable("unknown atomic op");
4365}