blob: 1fc4ccbd102b9b4ac02bdf5f480c161577855b59 [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 *);
James Y Knight53c76162015-07-17 18:21:37 +0000402 if (TemplateArgs) {
403 Size = llvm::RoundUpToAlignment(Size,
404 llvm::alignOf<ASTTemplateKWAndArgsInfo>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000405 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
James Y Knight53c76162015-07-17 18:21:37 +0000406 } else if (TemplateKWLoc.isValid()) {
407 Size = llvm::RoundUpToAlignment(Size,
408 llvm::alignOf<ASTTemplateKWAndArgsInfo>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000409 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
James Y Knight53c76162015-07-17 18:21:37 +0000410 }
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000411
Chris Lattner5c0b4052010-10-30 05:14:06 +0000412 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000413 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000414 RefersToEnclosingVariableOrCapture,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000415 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000416}
417
Craig Topperce7167c2013-08-22 04:58:56 +0000418DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000419 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000420 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000421 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000422 unsigned NumTemplateArgs) {
423 std::size_t Size = sizeof(DeclRefExpr);
424 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000425 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000426 if (HasFoundDecl)
427 Size += sizeof(NamedDecl *);
James Y Knight53c76162015-07-17 18:21:37 +0000428 if (HasTemplateKWAndArgsInfo) {
429 Size = llvm::RoundUpToAlignment(Size,
430 llvm::alignOf<ASTTemplateKWAndArgsInfo>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000431 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
James Y Knight53c76162015-07-17 18:21:37 +0000432 }
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000433
Chris Lattner5c0b4052010-10-30 05:14:06 +0000434 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000435 return new (Mem) DeclRefExpr(EmptyShell());
436}
437
Daniel Dunbarb507f272012-03-09 15:39:15 +0000438SourceLocation DeclRefExpr::getLocStart() const {
439 if (hasQualifier())
440 return getQualifierLoc().getBeginLoc();
441 return getNameInfo().getLocStart();
442}
443SourceLocation DeclRefExpr::getLocEnd() const {
444 if (hasExplicitTemplateArgs())
445 return getRAngleLoc();
446 return getNameInfo().getLocEnd();
447}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000448
Alexey Bataevec474782014-10-09 08:45:04 +0000449PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
450 StringLiteral *SL)
451 : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
452 FNTy->isDependentType(), FNTy->isDependentType(),
453 FNTy->isInstantiationDependentType(),
454 /*ContainsUnexpandedParameterPack=*/false),
455 Loc(L), Type(IT), FnName(SL) {}
456
457StringLiteral *PredefinedExpr::getFunctionName() {
Alexey Bataev769562a2014-10-10 18:58:13 +0000458 return cast_or_null<StringLiteral>(FnName);
Alexey Bataevec474782014-10-09 08:45:04 +0000459}
460
461StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
462 switch (IT) {
463 case Func:
464 return "__func__";
465 case Function:
466 return "__FUNCTION__";
467 case FuncDName:
468 return "__FUNCDNAME__";
469 case LFunction:
470 return "L__FUNCTION__";
471 case PrettyFunction:
472 return "__PRETTY_FUNCTION__";
473 case FuncSig:
474 return "__FUNCSIG__";
475 case PrettyFunctionNoVirtual:
476 break;
477 }
478 llvm_unreachable("Unknown ident type for PredefinedExpr");
479}
480
Anders Carlsson2fb08242009-09-08 18:24:21 +0000481// FIXME: Maybe this should use DeclPrinter with a special "print predefined
482// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000483std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
484 ASTContext &Context = CurrentDecl->getASTContext();
485
David Majnemerbed356a2013-11-06 23:31:56 +0000486 if (IT == PredefinedExpr::FuncDName) {
487 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000488 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000489 MC.reset(Context.createMangleContext());
490
491 if (MC->shouldMangleDeclName(ND)) {
492 SmallString<256> Buffer;
493 llvm::raw_svector_ostream Out(Buffer);
494 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
495 MC->mangleCXXCtor(CD, Ctor_Base, Out);
496 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
497 MC->mangleCXXDtor(DD, Dtor_Base, Out);
498 else
499 MC->mangleName(ND, Out);
500
David Majnemerbed356a2013-11-06 23:31:56 +0000501 if (!Buffer.empty() && Buffer.front() == '\01')
502 return Buffer.substr(1);
503 return Buffer.str();
504 } else
505 return ND->getIdentifier()->getName();
506 }
507 return "";
508 }
Alexey Bataevec474782014-10-09 08:45:04 +0000509 if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
510 std::unique_ptr<MangleContext> MC;
511 MC.reset(Context.createMangleContext());
512 SmallString<256> Buffer;
513 llvm::raw_svector_ostream Out(Buffer);
514 auto DC = CurrentDecl->getDeclContext();
515 if (DC->isFileContext())
516 MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
517 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
518 MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
519 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
520 MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
521 else
522 MC->mangleBlock(DC, BD, Out);
523 return Out.str();
524 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000525 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Reid Kleckner52eddda2014-04-08 18:13:24 +0000526 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000527 return FD->getNameAsString();
528
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000529 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000530 llvm::raw_svector_ostream Out(Name);
531
532 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000533 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000534 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000535 if (MD->isStatic())
536 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000537 }
538
David Blaikiebbafb8a2012-03-11 07:00:24 +0000539 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000540 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000541 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000542
Douglas Gregor11a434a2012-04-10 20:14:15 +0000543 const FunctionDecl *Decl = FD;
544 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
545 Decl = Pattern;
546 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Craig Topper36250ad2014-05-12 05:36:57 +0000547 const FunctionProtoType *FT = nullptr;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000548 if (FD->hasWrittenPrototype())
549 FT = dyn_cast<FunctionProtoType>(AFT);
550
Reid Kleckner52eddda2014-04-08 18:13:24 +0000551 if (IT == FuncSig) {
552 switch (FT->getCallConv()) {
553 case CC_C: POut << "__cdecl "; break;
554 case CC_X86StdCall: POut << "__stdcall "; break;
555 case CC_X86FastCall: POut << "__fastcall "; break;
556 case CC_X86ThisCall: POut << "__thiscall "; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +0000557 case CC_X86VectorCall: POut << "__vectorcall "; break;
Reid Kleckner52eddda2014-04-08 18:13:24 +0000558 // Only bother printing the conventions that MSVC knows about.
559 default: break;
560 }
561 }
562
563 FD->printQualifiedName(POut, Policy);
564
Douglas Gregor11a434a2012-04-10 20:14:15 +0000565 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000566 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000567 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000568 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000569 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000570 }
571
572 if (FT->isVariadic()) {
573 if (FD->getNumParams()) POut << ", ";
574 POut << "...";
575 }
576 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000577 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000578
Sam Weinig4e83bd22009-12-27 01:38:20 +0000579 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000580 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000581 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000582 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000583 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000584 POut << " volatile";
585 RefQualifierKind Ref = MD->getRefQualifier();
586 if (Ref == RQ_LValue)
587 POut << " &";
588 else if (Ref == RQ_RValue)
589 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000590 }
591
Douglas Gregor11a434a2012-04-10 20:14:15 +0000592 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
593 SpecsTy Specs;
594 const DeclContext *Ctx = FD->getDeclContext();
595 while (Ctx && isa<NamedDecl>(Ctx)) {
596 const ClassTemplateSpecializationDecl *Spec
597 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
598 if (Spec && !Spec->isExplicitSpecialization())
599 Specs.push_back(Spec);
600 Ctx = Ctx->getParent();
601 }
602
603 std::string TemplateParams;
604 llvm::raw_string_ostream TOut(TemplateParams);
605 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
606 I != E; ++I) {
607 const TemplateParameterList *Params
608 = (*I)->getSpecializedTemplate()->getTemplateParameters();
609 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
610 assert(Params->size() == Args.size());
611 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
612 StringRef Param = Params->getParam(i)->getName();
613 if (Param.empty()) continue;
614 TOut << Param << " = ";
615 Args.get(i).print(Policy, TOut);
616 TOut << ", ";
617 }
618 }
619
620 FunctionTemplateSpecializationInfo *FSI
621 = FD->getTemplateSpecializationInfo();
622 if (FSI && !FSI->isExplicitSpecialization()) {
623 const TemplateParameterList* Params
624 = FSI->getTemplate()->getTemplateParameters();
625 const TemplateArgumentList* Args = FSI->TemplateArguments;
626 assert(Params->size() == Args->size());
627 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
628 StringRef Param = Params->getParam(i)->getName();
629 if (Param.empty()) continue;
630 TOut << Param << " = ";
631 Args->get(i).print(Policy, TOut);
632 TOut << ", ";
633 }
634 }
635
636 TOut.flush();
637 if (!TemplateParams.empty()) {
638 // remove the trailing comma and space
639 TemplateParams.resize(TemplateParams.size() - 2);
640 POut << " [" << TemplateParams << "]";
641 }
642
643 POut.flush();
644
Benjamin Kramer90f54222013-08-21 11:45:27 +0000645 // Print "auto" for all deduced return types. This includes C++1y return
646 // type deduction and lambdas. For trailing return types resolve the
647 // decltype expression. Otherwise print the real type when this is
648 // not a constructor or destructor.
Alexey Bataevec474782014-10-09 08:45:04 +0000649 if (isa<CXXMethodDecl>(FD) &&
650 cast<CXXMethodDecl>(FD)->getParent()->isLambda())
Benjamin Kramer90f54222013-08-21 11:45:27 +0000651 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000652 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
653 FT->getReturnType()
654 ->getAs<DecltypeType>()
655 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000656 .getAsStringInternal(Proto, Policy);
657 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000658 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000659
660 Out << Proto;
661
Anders Carlsson2fb08242009-09-08 18:24:21 +0000662 return Name.str().str();
663 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000664 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
665 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
666 // Skip to its enclosing function or method, but not its enclosing
667 // CapturedDecl.
668 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
669 const Decl *D = Decl::castFromDeclContext(DC);
670 return ComputeName(IT, D);
671 }
672 llvm_unreachable("CapturedDecl not inside a function or method");
673 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000674 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000675 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000676 llvm::raw_svector_ostream Out(Name);
677 Out << (MD->isInstanceMethod() ? '-' : '+');
678 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000679
680 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
681 // a null check to avoid a crash.
682 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000683 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000684
Anders Carlsson2fb08242009-09-08 18:24:21 +0000685 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000686 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000687 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000688
Anders Carlsson2fb08242009-09-08 18:24:21 +0000689 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000690 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000691 Out << ']';
692
Anders Carlsson2fb08242009-09-08 18:24:21 +0000693 return Name.str().str();
694 }
695 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
696 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
697 return "top level";
698 }
699 return "";
700}
701
Craig Topper37932912013-08-18 10:09:15 +0000702void APNumericStorage::setIntValue(const ASTContext &C,
703 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000704 if (hasAllocation())
705 C.Deallocate(pVal);
706
707 BitWidth = Val.getBitWidth();
708 unsigned NumWords = Val.getNumWords();
709 const uint64_t* Words = Val.getRawData();
710 if (NumWords > 1) {
711 pVal = new (C) uint64_t[NumWords];
712 std::copy(Words, Words + NumWords, pVal);
713 } else if (NumWords == 1)
714 VAL = Words[0];
715 else
716 VAL = 0;
717}
718
Craig Topper37932912013-08-18 10:09:15 +0000719IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000720 QualType type, SourceLocation l)
721 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
722 false, false),
723 Loc(l) {
724 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
725 assert(V.getBitWidth() == C.getIntWidth(type) &&
726 "Integer type is not the correct size for constant.");
727 setValue(C, V);
728}
729
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000730IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000731IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000732 QualType type, SourceLocation l) {
733 return new (C) IntegerLiteral(C, V, type, l);
734}
735
736IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000737IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000738 return new (C) IntegerLiteral(Empty);
739}
740
Craig Topper37932912013-08-18 10:09:15 +0000741FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000742 bool isexact, QualType Type, SourceLocation L)
743 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
744 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000745 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000746 FloatingLiteralBits.IsExact = isexact;
747 setValue(C, V);
748}
749
Craig Topper37932912013-08-18 10:09:15 +0000750FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000751 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000752 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000753 FloatingLiteralBits.IsExact = false;
754}
755
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000756FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000757FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000758 bool isexact, QualType Type, SourceLocation L) {
759 return new (C) FloatingLiteral(C, V, isexact, Type, L);
760}
761
762FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000763FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000764 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000765}
766
Tim Northover178723a2013-01-22 09:46:51 +0000767const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
768 switch(FloatingLiteralBits.Semantics) {
769 case IEEEhalf:
770 return llvm::APFloat::IEEEhalf;
771 case IEEEsingle:
772 return llvm::APFloat::IEEEsingle;
773 case IEEEdouble:
774 return llvm::APFloat::IEEEdouble;
775 case x87DoubleExtended:
776 return llvm::APFloat::x87DoubleExtended;
777 case IEEEquad:
778 return llvm::APFloat::IEEEquad;
779 case PPCDoubleDouble:
780 return llvm::APFloat::PPCDoubleDouble;
781 }
782 llvm_unreachable("Unrecognised floating semantics");
783}
784
785void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
786 if (&Sem == &llvm::APFloat::IEEEhalf)
787 FloatingLiteralBits.Semantics = IEEEhalf;
788 else if (&Sem == &llvm::APFloat::IEEEsingle)
789 FloatingLiteralBits.Semantics = IEEEsingle;
790 else if (&Sem == &llvm::APFloat::IEEEdouble)
791 FloatingLiteralBits.Semantics = IEEEdouble;
792 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
793 FloatingLiteralBits.Semantics = x87DoubleExtended;
794 else if (&Sem == &llvm::APFloat::IEEEquad)
795 FloatingLiteralBits.Semantics = IEEEquad;
796 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
797 FloatingLiteralBits.Semantics = PPCDoubleDouble;
798 else
799 llvm_unreachable("Unknown floating semantics");
800}
801
Chris Lattnera0173132008-06-07 22:13:43 +0000802/// getValueAsApproximateDouble - This returns the value as an inaccurate
803/// double. Note that this may cause loss of precision, but is useful for
804/// debugging dumps, etc.
805double FloatingLiteral::getValueAsApproximateDouble() const {
806 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000807 bool ignored;
808 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
809 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000810 return V.convertToDouble();
811}
812
Nick Lewycky4ed84042012-02-24 09:07:53 +0000813int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000814 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000815 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000816 case Ascii:
817 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000818 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000819 break;
820 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000821 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000822 break;
823 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000824 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000825 break;
826 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000827 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000828 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000829 }
830 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
831 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000832 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000833 && "character byte widths supported are 1, 2, and 4 only");
834 return CharByteWidth;
835}
836
Craig Topper37932912013-08-18 10:09:15 +0000837StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000838 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000839 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000840 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000841 assert(C.getAsConstantArrayType(Ty) &&
842 "StringLiteral must be of constant array type!");
843
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000844 // Allocate enough space for the StringLiteral plus an array of locations for
845 // any concatenated string tokens.
846 void *Mem = C.Allocate(sizeof(StringLiteral)+
847 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000848 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000849 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000850
Steve Naroffdf7855b2007-02-21 23:46:25 +0000851 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000852 SL->setString(C,Str,Kind,Pascal);
853
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000854 SL->TokLocs[0] = Loc[0];
855 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000856
Chris Lattner630970d2009-02-18 05:49:11 +0000857 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000858 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
859 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000860}
861
Craig Topper37932912013-08-18 10:09:15 +0000862StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
863 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000864 void *Mem = C.Allocate(sizeof(StringLiteral)+
865 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000866 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000867 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000868 SL->CharByteWidth = 0;
869 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000870 SL->NumConcatenated = NumStrs;
871 return SL;
872}
873
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000874void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000875 switch (getKind()) {
876 case Ascii: break; // no prefix.
877 case Wide: OS << 'L'; break;
878 case UTF8: OS << "u8"; break;
879 case UTF16: OS << 'u'; break;
880 case UTF32: OS << 'U'; break;
881 }
882 OS << '"';
883 static const char Hex[] = "0123456789ABCDEF";
884
885 unsigned LastSlashX = getLength();
886 for (unsigned I = 0, N = getLength(); I != N; ++I) {
887 switch (uint32_t Char = getCodeUnit(I)) {
888 default:
889 // FIXME: Convert UTF-8 back to codepoints before rendering.
890
891 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
892 // Leave invalid surrogates alone; we'll use \x for those.
893 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
894 Char <= 0xdbff) {
895 uint32_t Trail = getCodeUnit(I + 1);
896 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
897 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
898 ++I;
899 }
900 }
901
902 if (Char > 0xff) {
903 // If this is a wide string, output characters over 0xff using \x
904 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
905 // codepoint: use \x escapes for invalid codepoints.
906 if (getKind() == Wide ||
907 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
908 // FIXME: Is this the best way to print wchar_t?
909 OS << "\\x";
910 int Shift = 28;
911 while ((Char >> Shift) == 0)
912 Shift -= 4;
913 for (/**/; Shift >= 0; Shift -= 4)
914 OS << Hex[(Char >> Shift) & 15];
915 LastSlashX = I;
916 break;
917 }
918
919 if (Char > 0xffff)
920 OS << "\\U00"
921 << Hex[(Char >> 20) & 15]
922 << Hex[(Char >> 16) & 15];
923 else
924 OS << "\\u";
925 OS << Hex[(Char >> 12) & 15]
926 << Hex[(Char >> 8) & 15]
927 << Hex[(Char >> 4) & 15]
928 << Hex[(Char >> 0) & 15];
929 break;
930 }
931
932 // If we used \x... for the previous character, and this character is a
933 // hexadecimal digit, prevent it being slurped as part of the \x.
934 if (LastSlashX + 1 == I) {
935 switch (Char) {
936 case '0': case '1': case '2': case '3': case '4':
937 case '5': case '6': case '7': case '8': case '9':
938 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
939 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
940 OS << "\"\"";
941 }
942 }
943
944 assert(Char <= 0xff &&
945 "Characters above 0xff should already have been handled.");
946
Jordan Rosea7d03842013-02-08 22:30:41 +0000947 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000948 OS << (char)Char;
949 else // Output anything hard as an octal escape.
950 OS << '\\'
951 << (char)('0' + ((Char >> 6) & 7))
952 << (char)('0' + ((Char >> 3) & 7))
953 << (char)('0' + ((Char >> 0) & 7));
954 break;
955 // Handle some common non-printable cases to make dumps prettier.
956 case '\\': OS << "\\\\"; break;
957 case '"': OS << "\\\""; break;
958 case '\n': OS << "\\n"; break;
959 case '\t': OS << "\\t"; break;
960 case '\a': OS << "\\a"; break;
961 case '\b': OS << "\\b"; break;
962 }
963 }
964 OS << '"';
965}
966
Craig Topper37932912013-08-18 10:09:15 +0000967void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000968 StringKind Kind, bool IsPascal) {
969 //FIXME: we assume that the string data comes from a target that uses the same
970 // code unit size and endianess for the type of string.
971 this->Kind = Kind;
972 this->IsPascal = IsPascal;
973
Nick Lewycky4ed84042012-02-24 09:07:53 +0000974 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000975 assert((Str.size()%CharByteWidth == 0)
976 && "size of data must be multiple of CharByteWidth");
977 Length = Str.size()/CharByteWidth;
978
979 switch(CharByteWidth) {
980 case 1: {
981 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000982 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000983 StrData.asChar = AStrData;
984 break;
985 }
986 case 2: {
987 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000988 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000989 StrData.asUInt16 = AStrData;
990 break;
991 }
992 case 4: {
993 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000994 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000995 StrData.asUInt32 = AStrData;
996 break;
997 }
998 default:
999 assert(false && "unsupported CharByteWidth");
1000 }
Douglas Gregor958dfc92009-04-15 16:35:07 +00001001}
1002
Chris Lattnere925d612010-11-17 07:37:15 +00001003/// getLocationOfByte - Return a source location that points to the specified
1004/// byte of this string literal.
1005///
1006/// Strings are amazingly complex. They can be formed from multiple tokens and
1007/// can have escape sequences in them in addition to the usual trigraph and
1008/// escaped newline business. This routine handles this complexity.
1009///
1010SourceLocation StringLiteral::
1011getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1012 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +00001013 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1014 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +00001015
Chris Lattnere925d612010-11-17 07:37:15 +00001016 // Loop over all of the tokens in this string until we find the one that
1017 // contains the byte we're looking for.
1018 unsigned TokNo = 0;
1019 while (1) {
1020 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1021 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1022
1023 // Get the spelling of the string so that we can get the data that makes up
1024 // the string literal, not the identifier for the macro it is potentially
1025 // expanded through.
1026 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1027
1028 // Re-lex the token to get its length and original spelling.
1029 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1030 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001031 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001032 if (Invalid)
1033 return StrTokSpellingLoc;
1034
1035 const char *StrData = Buffer.data()+LocInfo.second;
1036
Chris Lattnere925d612010-11-17 07:37:15 +00001037 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001038 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1039 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001040 Token TheTok;
1041 TheLexer.LexFromRawLexer(TheTok);
1042
1043 // Use the StringLiteralParser to compute the length of the string in bytes.
Craig Topper9d5583e2014-06-26 04:58:39 +00001044 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattnere925d612010-11-17 07:37:15 +00001045 unsigned TokNumBytes = SLP.GetStringLength();
1046
1047 // If the byte is in this token, return the location of the byte.
1048 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001049 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001050 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1051
1052 // Now that we know the offset of the token in the spelling, use the
1053 // preprocessor to get the offset in the original source.
1054 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1055 }
1056
1057 // Move to the next string token.
1058 ++TokNo;
1059 ByteNo -= TokNumBytes;
1060 }
1061}
1062
1063
1064
Chris Lattner1b926492006-08-23 06:42:10 +00001065/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1066/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001067StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001068 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001069 case UO_PostInc: return "++";
1070 case UO_PostDec: return "--";
1071 case UO_PreInc: return "++";
1072 case UO_PreDec: return "--";
1073 case UO_AddrOf: return "&";
1074 case UO_Deref: return "*";
1075 case UO_Plus: return "+";
1076 case UO_Minus: return "-";
1077 case UO_Not: return "~";
1078 case UO_LNot: return "!";
1079 case UO_Real: return "__real";
1080 case UO_Imag: return "__imag";
1081 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001082 }
David Blaikief47fa302012-01-17 02:30:50 +00001083 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001084}
1085
John McCalle3027922010-08-25 11:45:40 +00001086UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001087UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1088 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001089 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001090 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1091 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1092 case OO_Amp: return UO_AddrOf;
1093 case OO_Star: return UO_Deref;
1094 case OO_Plus: return UO_Plus;
1095 case OO_Minus: return UO_Minus;
1096 case OO_Tilde: return UO_Not;
1097 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001098 }
1099}
1100
1101OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1102 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001103 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1104 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1105 case UO_AddrOf: return OO_Amp;
1106 case UO_Deref: return OO_Star;
1107 case UO_Plus: return OO_Plus;
1108 case UO_Minus: return OO_Minus;
1109 case UO_Not: return OO_Tilde;
1110 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001111 default: return OO_None;
1112 }
1113}
1114
1115
Chris Lattner0eedafe2006-08-24 04:56:27 +00001116//===----------------------------------------------------------------------===//
1117// Postfix Operators.
1118//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001119
Craig Topper37932912013-08-18 10:09:15 +00001120CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1121 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1122 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001123 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001124 fn->isTypeDependent(),
1125 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001126 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001127 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001128 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001129
Benjamin Kramerc215e762012-08-24 11:54:20 +00001130 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001131 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001132 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001133 if (args[i]->isTypeDependent())
1134 ExprBits.TypeDependent = true;
1135 if (args[i]->isValueDependent())
1136 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001137 if (args[i]->isInstantiationDependent())
1138 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001139 if (args[i]->containsUnexpandedParameterPack())
1140 ExprBits.ContainsUnexpandedParameterPack = true;
1141
Peter Collingbourne3a347252011-02-08 21:18:02 +00001142 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001143 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001144
Peter Collingbourne3a347252011-02-08 21:18:02 +00001145 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001146 RParenLoc = rparenloc;
1147}
Nate Begeman1e36a852008-01-17 17:46:27 +00001148
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001149CallExpr::CallExpr(const ASTContext &C, Expr *fn, ArrayRef<Expr *> args,
John McCall7decc9e2010-11-18 06:31:45 +00001150 QualType t, ExprValueKind VK, SourceLocation rparenloc)
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001151 : CallExpr(C, CallExprClass, fn, /*NumPreArgs=*/0, args, t, VK, rparenloc) {
Chris Lattnere165d942006-08-24 04:40:38 +00001152}
1153
Craig Topper37932912013-08-18 10:09:15 +00001154CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Benjamin Kramerf04f98d2015-03-06 14:15:57 +00001155 : CallExpr(C, SC, /*NumPreArgs=*/0, Empty) {}
Peter Collingbourne3a347252011-02-08 21:18:02 +00001156
Craig Topper37932912013-08-18 10:09:15 +00001157CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001158 EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001159 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbourne3a347252011-02-08 21:18:02 +00001160 // FIXME: Why do we allocate this?
1161 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1162 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001163}
1164
Nuno Lopes518e3702009-12-20 23:11:08 +00001165Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001166 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001167
1168 while (SubstNonTypeTemplateParmExpr *NTTP
1169 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1170 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1171 }
1172
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001173 // If we're calling a dereference, look at the pointer instead.
1174 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1175 if (BO->isPtrMemOp())
1176 CEE = BO->getRHS()->IgnoreParenCasts();
1177 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1178 if (UO->getOpcode() == UO_Deref)
1179 CEE = UO->getSubExpr()->IgnoreParenCasts();
1180 }
Chris Lattner52301912009-07-17 15:46:27 +00001181 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001182 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001183 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1184 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001185
Craig Topper36250ad2014-05-12 05:36:57 +00001186 return nullptr;
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001187}
1188
Nuno Lopes518e3702009-12-20 23:11:08 +00001189FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001190 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001191}
1192
Chris Lattnere4407ed2007-12-28 05:25:02 +00001193/// setNumArgs - This changes the number of arguments present in this call.
1194/// Any orphaned expressions are deleted by this, and any new operands are set
1195/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001196void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001197 // No change, just return.
1198 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001199
Chris Lattnere4407ed2007-12-28 05:25:02 +00001200 // If shrinking # arguments, just delete the extras and forgot them.
1201 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001202 this->NumArgs = NumArgs;
1203 return;
1204 }
1205
1206 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001207 unsigned NumPreArgs = getNumPreArgs();
1208 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001209 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001210 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001211 NewSubExprs[i] = SubExprs[i];
1212 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001213 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1214 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001215 NewSubExprs[i] = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001216
Douglas Gregorba6e5572009-04-17 21:46:47 +00001217 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001218 SubExprs = NewSubExprs;
1219 this->NumArgs = NumArgs;
1220}
1221
Alp Tokera724cff2013-12-28 21:59:02 +00001222/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001223/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001224unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001225 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001226 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001227 // ImplicitCastExpr.
1228 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1229 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001230 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Steve Narofff6e3b3292008-01-31 01:07:12 +00001232 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1233 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001234 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001235
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001236 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1237 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001238 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001240 if (!FDecl->getIdentifier())
1241 return 0;
1242
Douglas Gregor15fc9562009-09-12 00:22:50 +00001243 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001244}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001245
Scott Douglass503fc392015-06-10 13:53:15 +00001246bool CallExpr::isUnevaluatedBuiltinCall(const ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001247 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001248 return Ctx.BuiltinInfo.isUnevaluated(BI);
1249 return false;
1250}
1251
David Majnemerced8bdf2015-02-25 17:36:15 +00001252QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const {
1253 const Expr *Callee = getCallee();
1254 QualType CalleeType = Callee->getType();
1255 if (const auto *FnTypePtr = CalleeType->getAs<PointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001256 CalleeType = FnTypePtr->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001257 } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001258 CalleeType = BPT->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001259 } else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
1260 if (isa<CXXPseudoDestructorExpr>(Callee->IgnoreParens()))
1261 return Ctx.VoidTy;
1262
John McCall0009fcc2011-04-26 20:42:42 +00001263 // This should never be overloaded and so should never return null.
David Majnemerced8bdf2015-02-25 17:36:15 +00001264 CalleeType = Expr::findBoundMemberType(Callee);
1265 }
1266
John McCall0009fcc2011-04-26 20:42:42 +00001267 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001268 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001269}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001270
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001271SourceLocation CallExpr::getLocStart() const {
1272 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001273 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001274
1275 SourceLocation begin = getCallee()->getLocStart();
Keno Fischer070db172014-08-15 01:39:12 +00001276 if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001277 begin = getArg(0)->getLocStart();
1278 return begin;
1279}
1280SourceLocation CallExpr::getLocEnd() const {
1281 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001282 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001283
1284 SourceLocation end = getRParenLoc();
Keno Fischer070db172014-08-15 01:39:12 +00001285 if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001286 end = getArg(getNumArgs() - 1)->getLocEnd();
1287 return end;
1288}
John McCall701417a2011-02-21 06:23:05 +00001289
Craig Topper37932912013-08-18 10:09:15 +00001290OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001291 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001292 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001293 ArrayRef<OffsetOfNode> comps,
1294 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001295 SourceLocation RParenLoc) {
1296 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001297 sizeof(OffsetOfNode) * comps.size() +
1298 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001299
Benjamin Kramerc215e762012-08-24 11:54:20 +00001300 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1301 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001302}
1303
Craig Topper37932912013-08-18 10:09:15 +00001304OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001305 unsigned numComps, unsigned numExprs) {
1306 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1307 sizeof(OffsetOfNode) * numComps +
1308 sizeof(Expr*) * numExprs);
1309 return new (Mem) OffsetOfExpr(numComps, numExprs);
1310}
1311
Craig Topper37932912013-08-18 10:09:15 +00001312OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001313 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001314 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001315 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001316 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1317 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001318 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001319 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001320 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001321 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001322 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001323{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001324 for (unsigned i = 0; i != comps.size(); ++i) {
1325 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001326 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001327
Benjamin Kramerc215e762012-08-24 11:54:20 +00001328 for (unsigned i = 0; i != exprs.size(); ++i) {
1329 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001330 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001331 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001332 ExprBits.ContainsUnexpandedParameterPack = true;
1333
Benjamin Kramerc215e762012-08-24 11:54:20 +00001334 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001335 }
1336}
1337
1338IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1339 assert(getKind() == Field || getKind() == Identifier);
1340 if (getKind() == Field)
1341 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001342
Douglas Gregor882211c2010-04-28 22:16:22 +00001343 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1344}
1345
David Majnemer10fd83d2015-01-15 10:04:14 +00001346UnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr(
1347 UnaryExprOrTypeTrait ExprKind, Expr *E, QualType resultType,
1348 SourceLocation op, SourceLocation rp)
1349 : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary,
1350 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1351 // Value-dependent if the argument is type-dependent.
1352 E->isTypeDependent(), E->isInstantiationDependent(),
1353 E->containsUnexpandedParameterPack()),
1354 OpLoc(op), RParenLoc(rp) {
1355 UnaryExprOrTypeTraitExprBits.Kind = ExprKind;
1356 UnaryExprOrTypeTraitExprBits.IsType = false;
1357 Argument.Ex = E;
1358
1359 // Check to see if we are in the situation where alignof(decl) should be
1360 // dependent because decl's alignment is dependent.
1361 if (ExprKind == UETT_AlignOf) {
1362 if (!isValueDependent() || !isInstantiationDependent()) {
1363 E = E->IgnoreParens();
1364
1365 const ValueDecl *D = nullptr;
1366 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
1367 D = DRE->getDecl();
1368 else if (const auto *ME = dyn_cast<MemberExpr>(E))
1369 D = ME->getMemberDecl();
1370
1371 if (D) {
1372 for (const auto *I : D->specific_attrs<AlignedAttr>()) {
1373 if (I->isAlignmentDependent()) {
1374 setValueDependent(true);
1375 setInstantiationDependent(true);
1376 break;
1377 }
1378 }
1379 }
1380 }
1381 }
1382}
1383
Aaron Ballmanf4cb2be2015-03-24 15:07:53 +00001384MemberExpr *MemberExpr::Create(
1385 const ASTContext &C, Expr *base, bool isarrow, SourceLocation OperatorLoc,
1386 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1387 ValueDecl *memberdecl, DeclAccessPair founddecl,
1388 DeclarationNameInfo nameinfo, const TemplateArgumentListInfo *targs,
1389 QualType ty, ExprValueKind vk, ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001390 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001391
Douglas Gregorea972d32011-02-28 21:54:11 +00001392 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001393 founddecl.getDecl() != memberdecl ||
1394 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001395 if (hasQualOrFound)
1396 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001397
John McCall6b51f282009-11-23 01:53:49 +00001398 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001399 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1400 else if (TemplateKWLoc.isValid())
1401 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001402
Chris Lattner5c0b4052010-10-30 05:14:06 +00001403 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
Aaron Ballmanf4cb2be2015-03-24 15:07:53 +00001404 MemberExpr *E = new (Mem)
1405 MemberExpr(base, isarrow, OperatorLoc, memberdecl, nameinfo, ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001406
1407 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001408 // FIXME: Wrong. We should be looking at the member declaration we found.
1409 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001410 E->setValueDependent(true);
1411 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001412 E->setInstantiationDependent(true);
1413 }
1414 else if (QualifierLoc &&
1415 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1416 E->setInstantiationDependent(true);
1417
John McCall16df1e52010-03-30 21:47:33 +00001418 E->HasQualifierOrFoundDecl = true;
1419
1420 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001421 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001422 NQ->FoundDecl = founddecl;
1423 }
1424
Abramo Bagnara7945c982012-01-27 09:46:47 +00001425 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1426
John McCall16df1e52010-03-30 21:47:33 +00001427 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001428 bool Dependent = false;
1429 bool InstantiationDependent = false;
1430 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001431 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1432 Dependent,
1433 InstantiationDependent,
1434 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001435 if (InstantiationDependent)
1436 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001437 } else if (TemplateKWLoc.isValid()) {
1438 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001439 }
1440
1441 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001442}
1443
Daniel Dunbarb507f272012-03-09 15:39:15 +00001444SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001445 if (isImplicitAccess()) {
1446 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001447 return getQualifierLoc().getBeginLoc();
1448 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001449 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001450
Daniel Dunbarb507f272012-03-09 15:39:15 +00001451 // FIXME: We don't want this to happen. Rather, we should be able to
1452 // detect all kinds of implicit accesses more cleanly.
1453 SourceLocation BaseStartLoc = getBase()->getLocStart();
1454 if (BaseStartLoc.isValid())
1455 return BaseStartLoc;
1456 return MemberLoc;
1457}
1458SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001459 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001460 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001461 EndLoc = getRAngleLoc();
1462 else if (EndLoc.isInvalid())
1463 EndLoc = getBase()->getLocEnd();
1464 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001465}
1466
Alp Tokerc1086762013-12-07 13:51:35 +00001467bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001468 switch (getCastKind()) {
1469 case CK_DerivedToBase:
1470 case CK_UncheckedDerivedToBase:
1471 case CK_DerivedToBaseMemberPointer:
1472 case CK_BaseToDerived:
1473 case CK_BaseToDerivedMemberPointer:
1474 assert(!path_empty() && "Cast kind should have a base path!");
1475 break;
1476
1477 case CK_CPointerToObjCPointerCast:
1478 assert(getType()->isObjCObjectPointerType());
1479 assert(getSubExpr()->getType()->isPointerType());
1480 goto CheckNoBasePath;
1481
1482 case CK_BlockPointerToObjCPointerCast:
1483 assert(getType()->isObjCObjectPointerType());
1484 assert(getSubExpr()->getType()->isBlockPointerType());
1485 goto CheckNoBasePath;
1486
John McCallc62bb392012-02-15 01:22:51 +00001487 case CK_ReinterpretMemberPointer:
1488 assert(getType()->isMemberPointerType());
1489 assert(getSubExpr()->getType()->isMemberPointerType());
1490 goto CheckNoBasePath;
1491
John McCall9320b872011-09-09 05:25:32 +00001492 case CK_BitCast:
1493 // Arbitrary casts to C pointer types count as bitcasts.
1494 // Otherwise, we should only have block and ObjC pointer casts
1495 // here if they stay within the type kind.
1496 if (!getType()->isPointerType()) {
1497 assert(getType()->isObjCObjectPointerType() ==
1498 getSubExpr()->getType()->isObjCObjectPointerType());
1499 assert(getType()->isBlockPointerType() ==
1500 getSubExpr()->getType()->isBlockPointerType());
1501 }
1502 goto CheckNoBasePath;
1503
1504 case CK_AnyPointerToBlockPointerCast:
1505 assert(getType()->isBlockPointerType());
1506 assert(getSubExpr()->getType()->isAnyPointerType() &&
1507 !getSubExpr()->getType()->isBlockPointerType());
1508 goto CheckNoBasePath;
1509
Douglas Gregored90df32012-02-22 05:02:47 +00001510 case CK_CopyAndAutoreleaseBlockObject:
1511 assert(getType()->isBlockPointerType());
1512 assert(getSubExpr()->getType()->isBlockPointerType());
1513 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001514
1515 case CK_FunctionToPointerDecay:
1516 assert(getType()->isPointerType());
1517 assert(getSubExpr()->getType()->isFunctionType());
1518 goto CheckNoBasePath;
1519
David Tweede1468322013-12-11 13:39:46 +00001520 case CK_AddressSpaceConversion:
1521 assert(getType()->isPointerType());
1522 assert(getSubExpr()->getType()->isPointerType());
1523 assert(getType()->getPointeeType().getAddressSpace() !=
1524 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001525 // These should not have an inheritance path.
1526 case CK_Dynamic:
1527 case CK_ToUnion:
1528 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001529 case CK_NullToMemberPointer:
1530 case CK_NullToPointer:
1531 case CK_ConstructorConversion:
1532 case CK_IntegralToPointer:
1533 case CK_PointerToIntegral:
1534 case CK_ToVoid:
1535 case CK_VectorSplat:
1536 case CK_IntegralCast:
1537 case CK_IntegralToFloating:
1538 case CK_FloatingToIntegral:
1539 case CK_FloatingCast:
1540 case CK_ObjCObjectLValueCast:
1541 case CK_FloatingRealToComplex:
1542 case CK_FloatingComplexToReal:
1543 case CK_FloatingComplexCast:
1544 case CK_FloatingComplexToIntegralComplex:
1545 case CK_IntegralRealToComplex:
1546 case CK_IntegralComplexToReal:
1547 case CK_IntegralComplexCast:
1548 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001549 case CK_ARCProduceObject:
1550 case CK_ARCConsumeObject:
1551 case CK_ARCReclaimReturnedObject:
1552 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001553 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001554 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1555 goto CheckNoBasePath;
1556
1557 case CK_Dependent:
1558 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001559 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001560 case CK_AtomicToNonAtomic:
1561 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001562 case CK_PointerToBoolean:
1563 case CK_IntegralToBoolean:
1564 case CK_FloatingToBoolean:
1565 case CK_MemberPointerToBoolean:
1566 case CK_FloatingComplexToBoolean:
1567 case CK_IntegralComplexToBoolean:
1568 case CK_LValueBitCast: // -> bool&
1569 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001570 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001571 CheckNoBasePath:
1572 assert(path_empty() && "Cast kind should not have a base path!");
1573 break;
1574 }
Alp Tokerc1086762013-12-07 13:51:35 +00001575 return true;
John McCall9320b872011-09-09 05:25:32 +00001576}
1577
Anders Carlsson496335e2009-09-03 00:59:21 +00001578const char *CastExpr::getCastKindName() const {
1579 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001580 case CK_Dependent:
1581 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001582 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001583 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001584 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001585 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001586 case CK_LValueToRValue:
1587 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001588 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001589 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001590 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001591 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001592 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001593 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001594 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001595 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001596 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001597 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001598 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001599 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001600 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001601 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001602 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001603 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001604 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001605 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001606 case CK_NullToPointer:
1607 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001608 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001609 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001610 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001611 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001612 case CK_ReinterpretMemberPointer:
1613 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001614 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001615 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001616 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001617 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001618 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001619 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001620 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001621 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001622 case CK_PointerToBoolean:
1623 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001624 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001625 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001626 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001627 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001628 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001629 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001630 case CK_IntegralToBoolean:
1631 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001632 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001633 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001634 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001635 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001636 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001637 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001638 case CK_FloatingToBoolean:
1639 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001640 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001641 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001642 case CK_CPointerToObjCPointerCast:
1643 return "CPointerToObjCPointerCast";
1644 case CK_BlockPointerToObjCPointerCast:
1645 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001646 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001647 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001648 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001649 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001650 case CK_FloatingRealToComplex:
1651 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001652 case CK_FloatingComplexToReal:
1653 return "FloatingComplexToReal";
1654 case CK_FloatingComplexToBoolean:
1655 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001656 case CK_FloatingComplexCast:
1657 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001658 case CK_FloatingComplexToIntegralComplex:
1659 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001660 case CK_IntegralRealToComplex:
1661 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001662 case CK_IntegralComplexToReal:
1663 return "IntegralComplexToReal";
1664 case CK_IntegralComplexToBoolean:
1665 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001666 case CK_IntegralComplexCast:
1667 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001668 case CK_IntegralComplexToFloatingComplex:
1669 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001670 case CK_ARCConsumeObject:
1671 return "ARCConsumeObject";
1672 case CK_ARCProduceObject:
1673 return "ARCProduceObject";
1674 case CK_ARCReclaimReturnedObject:
1675 return "ARCReclaimReturnedObject";
1676 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001677 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001678 case CK_AtomicToNonAtomic:
1679 return "AtomicToNonAtomic";
1680 case CK_NonAtomicToAtomic:
1681 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001682 case CK_CopyAndAutoreleaseBlockObject:
1683 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001684 case CK_BuiltinFnToFnPtr:
1685 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001686 case CK_ZeroToOCLEvent:
1687 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001688 case CK_AddressSpaceConversion:
1689 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001690 }
Mike Stump11289f42009-09-09 15:08:12 +00001691
John McCallc5e62b42010-11-13 09:02:35 +00001692 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001693}
1694
Douglas Gregord196a582009-12-14 19:27:10 +00001695Expr *CastExpr::getSubExprAsWritten() {
Craig Topper36250ad2014-05-12 05:36:57 +00001696 Expr *SubExpr = nullptr;
Douglas Gregord196a582009-12-14 19:27:10 +00001697 CastExpr *E = this;
1698 do {
1699 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001700
1701 // Skip through reference binding to temporary.
1702 if (MaterializeTemporaryExpr *Materialize
1703 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1704 SubExpr = Materialize->GetTemporaryExpr();
1705
Douglas Gregord196a582009-12-14 19:27:10 +00001706 // Skip any temporary bindings; they're implicit.
1707 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1708 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001709
Douglas Gregord196a582009-12-14 19:27:10 +00001710 // Conversions by constructor and conversion functions have a
1711 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001712 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001713 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001714 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001715 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001716
Douglas Gregord196a582009-12-14 19:27:10 +00001717 // If the subexpression we're left with is an implicit cast, look
1718 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001719 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1720
Douglas Gregord196a582009-12-14 19:27:10 +00001721 return SubExpr;
1722}
1723
John McCallcf142162010-08-07 06:22:56 +00001724CXXBaseSpecifier **CastExpr::path_buffer() {
1725 switch (getStmtClass()) {
1726#define ABSTRACT_STMT(x)
1727#define CASTEXPR(Type, Base) \
1728 case Stmt::Type##Class: \
1729 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1730#define STMT(Type, Base)
1731#include "clang/AST/StmtNodes.inc"
1732 default:
1733 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001734 }
1735}
1736
1737void CastExpr::setCastPath(const CXXCastPath &Path) {
1738 assert(Path.size() == path_size());
1739 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1740}
1741
Craig Topper37932912013-08-18 10:09:15 +00001742ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001743 CastKind Kind, Expr *Operand,
1744 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001745 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001746 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1747 void *Buffer =
1748 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1749 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001750 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001751 if (PathSize) E->setCastPath(*BasePath);
1752 return E;
1753}
1754
Craig Topper37932912013-08-18 10:09:15 +00001755ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001756 unsigned PathSize) {
1757 void *Buffer =
1758 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1759 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1760}
1761
1762
Craig Topper37932912013-08-18 10:09:15 +00001763CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001764 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001765 const CXXCastPath *BasePath,
1766 TypeSourceInfo *WrittenTy,
1767 SourceLocation L, SourceLocation R) {
1768 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1769 void *Buffer =
1770 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1771 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001772 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001773 if (PathSize) E->setCastPath(*BasePath);
1774 return E;
1775}
1776
Craig Topper37932912013-08-18 10:09:15 +00001777CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1778 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001779 void *Buffer =
1780 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1781 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1782}
1783
Chris Lattner1b926492006-08-23 06:42:10 +00001784/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1785/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001786StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001787 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001788 case BO_PtrMemD: return ".*";
1789 case BO_PtrMemI: return "->*";
1790 case BO_Mul: return "*";
1791 case BO_Div: return "/";
1792 case BO_Rem: return "%";
1793 case BO_Add: return "+";
1794 case BO_Sub: return "-";
1795 case BO_Shl: return "<<";
1796 case BO_Shr: return ">>";
1797 case BO_LT: return "<";
1798 case BO_GT: return ">";
1799 case BO_LE: return "<=";
1800 case BO_GE: return ">=";
1801 case BO_EQ: return "==";
1802 case BO_NE: return "!=";
1803 case BO_And: return "&";
1804 case BO_Xor: return "^";
1805 case BO_Or: return "|";
1806 case BO_LAnd: return "&&";
1807 case BO_LOr: return "||";
1808 case BO_Assign: return "=";
1809 case BO_MulAssign: return "*=";
1810 case BO_DivAssign: return "/=";
1811 case BO_RemAssign: return "%=";
1812 case BO_AddAssign: return "+=";
1813 case BO_SubAssign: return "-=";
1814 case BO_ShlAssign: return "<<=";
1815 case BO_ShrAssign: return ">>=";
1816 case BO_AndAssign: return "&=";
1817 case BO_XorAssign: return "^=";
1818 case BO_OrAssign: return "|=";
1819 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001820 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001821
David Blaikiee4d798f2012-01-20 21:50:17 +00001822 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001823}
Steve Naroff47500512007-04-19 23:00:49 +00001824
John McCalle3027922010-08-25 11:45:40 +00001825BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001826BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1827 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001828 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001829 case OO_Plus: return BO_Add;
1830 case OO_Minus: return BO_Sub;
1831 case OO_Star: return BO_Mul;
1832 case OO_Slash: return BO_Div;
1833 case OO_Percent: return BO_Rem;
1834 case OO_Caret: return BO_Xor;
1835 case OO_Amp: return BO_And;
1836 case OO_Pipe: return BO_Or;
1837 case OO_Equal: return BO_Assign;
1838 case OO_Less: return BO_LT;
1839 case OO_Greater: return BO_GT;
1840 case OO_PlusEqual: return BO_AddAssign;
1841 case OO_MinusEqual: return BO_SubAssign;
1842 case OO_StarEqual: return BO_MulAssign;
1843 case OO_SlashEqual: return BO_DivAssign;
1844 case OO_PercentEqual: return BO_RemAssign;
1845 case OO_CaretEqual: return BO_XorAssign;
1846 case OO_AmpEqual: return BO_AndAssign;
1847 case OO_PipeEqual: return BO_OrAssign;
1848 case OO_LessLess: return BO_Shl;
1849 case OO_GreaterGreater: return BO_Shr;
1850 case OO_LessLessEqual: return BO_ShlAssign;
1851 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1852 case OO_EqualEqual: return BO_EQ;
1853 case OO_ExclaimEqual: return BO_NE;
1854 case OO_LessEqual: return BO_LE;
1855 case OO_GreaterEqual: return BO_GE;
1856 case OO_AmpAmp: return BO_LAnd;
1857 case OO_PipePipe: return BO_LOr;
1858 case OO_Comma: return BO_Comma;
1859 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001860 }
1861}
1862
1863OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1864 static const OverloadedOperatorKind OverOps[] = {
1865 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1866 OO_Star, OO_Slash, OO_Percent,
1867 OO_Plus, OO_Minus,
1868 OO_LessLess, OO_GreaterGreater,
1869 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1870 OO_EqualEqual, OO_ExclaimEqual,
1871 OO_Amp,
1872 OO_Caret,
1873 OO_Pipe,
1874 OO_AmpAmp,
1875 OO_PipePipe,
1876 OO_Equal, OO_StarEqual,
1877 OO_SlashEqual, OO_PercentEqual,
1878 OO_PlusEqual, OO_MinusEqual,
1879 OO_LessLessEqual, OO_GreaterGreaterEqual,
1880 OO_AmpEqual, OO_CaretEqual,
1881 OO_PipeEqual,
1882 OO_Comma
1883 };
1884 return OverOps[Opc];
1885}
1886
Craig Topper37932912013-08-18 10:09:15 +00001887InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001888 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001889 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001890 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001891 InitExprs(C, initExprs.size()),
Craig Topper36250ad2014-05-12 05:36:57 +00001892 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001893{
1894 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001895 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001896 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001897 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001898 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001899 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001900 if (initExprs[I]->isInstantiationDependent())
1901 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001902 if (initExprs[I]->containsUnexpandedParameterPack())
1903 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001904 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001905
Benjamin Kramerc215e762012-08-24 11:54:20 +00001906 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001907}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001908
Craig Topper37932912013-08-18 10:09:15 +00001909void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001910 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001911 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001912}
1913
Craig Topper37932912013-08-18 10:09:15 +00001914void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Craig Topper36250ad2014-05-12 05:36:57 +00001915 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001916}
1917
Craig Topper37932912013-08-18 10:09:15 +00001918Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001919 if (Init >= InitExprs.size()) {
Craig Topper36250ad2014-05-12 05:36:57 +00001920 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Richard Smithc275da62013-12-06 01:27:24 +00001921 setInit(Init, expr);
Craig Topper36250ad2014-05-12 05:36:57 +00001922 return nullptr;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001925 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001926 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001927 return Result;
1928}
1929
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001930void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001931 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001932 ArrayFillerOrUnionFieldInit = filler;
1933 // Fill out any "holes" in the array due to designated initializers.
1934 Expr **inits = getInits();
1935 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001936 if (inits[i] == nullptr)
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001937 inits[i] = filler;
1938}
1939
Richard Smith9ec1e482012-04-15 02:50:59 +00001940bool InitListExpr::isStringLiteralInit() const {
1941 if (getNumInits() != 1)
1942 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001943 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1944 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001945 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001946 // It is possible for getInit() to return null.
1947 const Expr *Init = getInit(0);
1948 if (!Init)
1949 return false;
1950 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001951 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1952}
1953
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001954SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001955 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001956 return SyntacticForm->getLocStart();
1957 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001958 if (Beg.isInvalid()) {
1959 // Find the first non-null initializer.
1960 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1961 E = InitExprs.end();
1962 I != E; ++I) {
1963 if (Stmt *S = *I) {
1964 Beg = S->getLocStart();
1965 break;
1966 }
1967 }
1968 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001969 return Beg;
1970}
1971
1972SourceLocation InitListExpr::getLocEnd() const {
1973 if (InitListExpr *SyntacticForm = getSyntacticForm())
1974 return SyntacticForm->getLocEnd();
1975 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001976 if (End.isInvalid()) {
1977 // Find the first non-null initializer from the end.
1978 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001979 E = InitExprs.rend();
1980 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001981 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001982 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001983 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001984 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001985 }
1986 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001987 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001988}
1989
Steve Naroff991e99d2008-09-04 15:31:07 +00001990/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001991///
John McCallc833dea2012-02-17 03:32:35 +00001992const FunctionProtoType *BlockExpr::getFunctionType() const {
1993 // The block pointer is never sugared, but the function type might be.
1994 return cast<BlockPointerType>(getType())
1995 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001996}
1997
Mike Stump11289f42009-09-09 15:08:12 +00001998SourceLocation BlockExpr::getCaretLocation() const {
1999 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00002000}
Mike Stump11289f42009-09-09 15:08:12 +00002001const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00002002 return TheBlock->getBody();
2003}
Mike Stump11289f42009-09-09 15:08:12 +00002004Stmt *BlockExpr::getBody() {
2005 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00002006}
Steve Naroff415d3d52008-10-08 17:01:13 +00002007
2008
Chris Lattner1ec5f562007-06-27 05:38:08 +00002009//===----------------------------------------------------------------------===//
2010// Generic Expression Routines
2011//===----------------------------------------------------------------------===//
2012
Chris Lattner237f2752009-02-14 07:37:35 +00002013/// isUnusedResultAWarning - Return true if this immediate expression should
2014/// be warned about if the result is unused. If so, fill in Loc and Ranges
2015/// with location to warn on and the source range[s] to report with the
2016/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002017bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2018 SourceRange &R1, SourceRange &R2,
2019 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00002020 // Don't warn if the expr is type dependent. The type could end up
2021 // instantiating to void.
2022 if (isTypeDependent())
2023 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002024
Chris Lattner1ec5f562007-06-27 05:38:08 +00002025 switch (getStmtClass()) {
2026 default:
John McCallc493a732010-03-12 07:11:26 +00002027 if (getType()->isVoidType())
2028 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002029 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002030 Loc = getExprLoc();
2031 R1 = getSourceRange();
2032 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002033 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002034 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002035 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00002036 case GenericSelectionExprClass:
2037 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002038 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00002039 case ChooseExprClass:
2040 return cast<ChooseExpr>(this)->getChosenSubExpr()->
2041 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002042 case UnaryOperatorClass: {
2043 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00002044
Chris Lattner1ec5f562007-06-27 05:38:08 +00002045 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002046 case UO_Plus:
2047 case UO_Minus:
2048 case UO_AddrOf:
2049 case UO_Not:
2050 case UO_LNot:
2051 case UO_Deref:
2052 break;
John McCalle3027922010-08-25 11:45:40 +00002053 case UO_PostInc:
2054 case UO_PostDec:
2055 case UO_PreInc:
2056 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002057 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002058 case UO_Real:
2059 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002060 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002061 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2062 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002063 return false;
2064 break;
John McCalle3027922010-08-25 11:45:40 +00002065 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002066 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002067 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002068 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002069 Loc = UO->getOperatorLoc();
2070 R1 = UO->getSubExpr()->getSourceRange();
2071 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002072 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002073 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002074 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002075 switch (BO->getOpcode()) {
2076 default:
2077 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002078 // Consider the RHS of comma for side effects. LHS was checked by
2079 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002080 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002081 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2082 // lvalue-ness) of an assignment written in a macro.
2083 if (IntegerLiteral *IE =
2084 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2085 if (IE->getValue() == 0)
2086 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002087 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002088 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002089 case BO_LAnd:
2090 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002091 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2092 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002093 return false;
2094 break;
John McCall1e3715a2010-02-16 04:10:53 +00002095 }
Chris Lattner237f2752009-02-14 07:37:35 +00002096 if (BO->isAssignmentOp())
2097 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002098 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002099 Loc = BO->getOperatorLoc();
2100 R1 = BO->getLHS()->getSourceRange();
2101 R2 = BO->getRHS()->getSourceRange();
2102 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002103 }
Chris Lattner86928112007-08-25 02:00:02 +00002104 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002105 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002106 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002107 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002108
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002109 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002110 // If only one of the LHS or RHS is a warning, the operator might
2111 // be being used for control flow. Only warn if both the LHS and
2112 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002113 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002114 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002115 return false;
2116 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002117 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002118 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002119 }
2120
Chris Lattnera44d1162007-06-27 05:58:59 +00002121 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002122 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002123 Loc = cast<MemberExpr>(this)->getMemberLoc();
2124 R1 = SourceRange(Loc, Loc);
2125 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2126 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002127
Chris Lattner1ec5f562007-06-27 05:38:08 +00002128 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002129 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002130 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2131 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2132 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2133 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002134
Chandler Carruth46339472011-08-17 09:49:44 +00002135 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002136 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002137 // overloads as there is no reasonable way to define these such that they
2138 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002139 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002140 // provides additional value as well. If this list is updated,
2141 // DiagnoseUnusedComparison should be as well.
2142 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002143 switch (Op->getOperator()) {
2144 default:
2145 break;
2146 case OO_EqualEqual:
2147 case OO_ExclaimEqual:
2148 case OO_Less:
2149 case OO_Greater:
2150 case OO_GreaterEqual:
2151 case OO_LessEqual:
David Majnemerced8bdf2015-02-25 17:36:15 +00002152 if (Op->getCallReturnType(Ctx)->isReferenceType() ||
2153 Op->getCallReturnType(Ctx)->isVoidType())
Richard Trieu161132b2014-05-14 23:22:10 +00002154 break;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002155 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002156 Loc = Op->getOperatorLoc();
2157 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002158 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002159 }
Chandler Carruth46339472011-08-17 09:49:44 +00002160
2161 // Fallthrough for generic call handling.
2162 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002163 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002164 case CXXMemberCallExprClass:
2165 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002166 // If this is a direct call, get the callee.
2167 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002168 if (const Decl *FD = CE->getCalleeDecl()) {
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002169 const FunctionDecl *Func = dyn_cast<FunctionDecl>(FD);
2170 bool HasWarnUnusedResultAttr = Func ? Func->hasUnusedResultAttr()
2171 : FD->hasAttr<WarnUnusedResultAttr>();
2172
Chris Lattner237f2752009-02-14 07:37:35 +00002173 // If the callee has attribute pure, const, or warn_unused_result, warn
2174 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002175 //
2176 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2177 // updated to match for QoI.
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002178 if (HasWarnUnusedResultAttr ||
Aaron Ballman9ead1242013-12-19 02:39:40 +00002179 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002180 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002181 Loc = CE->getCallee()->getLocStart();
2182 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002183
Chris Lattner1a6babf2009-10-13 04:53:48 +00002184 if (unsigned NumArgs = CE->getNumArgs())
2185 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2186 CE->getArg(NumArgs-1)->getLocEnd());
2187 return true;
2188 }
Chris Lattner237f2752009-02-14 07:37:35 +00002189 }
2190 return false;
2191 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002192
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002193 // If we don't know precisely what we're looking at, let's not warn.
2194 case UnresolvedLookupExprClass:
2195 case CXXUnresolvedConstructExprClass:
2196 return false;
2197
Anders Carlsson6aa50392009-11-17 17:11:23 +00002198 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002199 case CXXConstructExprClass: {
2200 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2201 if (Type->hasAttr<WarnUnusedAttr>()) {
2202 WarnE = this;
2203 Loc = getLocStart();
2204 R1 = getSourceRange();
2205 return true;
2206 }
2207 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002208 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002209 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002210
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002211 case ObjCMessageExprClass: {
2212 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002213 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002214 ME->isInstanceMessage() &&
2215 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002216 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002217 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002218 Loc = getExprLoc();
2219 R1 = ME->getSourceRange();
2220 return true;
2221 }
2222
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002223 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
Fariborz Jahanianb0553e22015-02-16 23:49:44 +00002224 if (MD->hasAttr<WarnUnusedResultAttr>()) {
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002225 WarnE = this;
2226 Loc = getExprLoc();
2227 return true;
2228 }
2229
Chris Lattner237f2752009-02-14 07:37:35 +00002230 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002231 }
Mike Stump11289f42009-09-09 15:08:12 +00002232
John McCallb7bd14f2010-12-02 01:19:52 +00002233 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002234 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002235 Loc = getExprLoc();
2236 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002237 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002238
John McCallfe96e0b2011-11-06 09:01:30 +00002239 case PseudoObjectExprClass: {
2240 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2241
2242 // Only complain about things that have the form of a getter.
2243 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2244 isa<BinaryOperator>(PO->getSyntacticForm()))
2245 return false;
2246
Eli Friedmanc11535c2012-05-24 00:47:05 +00002247 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002248 Loc = getExprLoc();
2249 R1 = getSourceRange();
2250 return true;
2251 }
2252
Chris Lattner944d3062008-07-26 19:51:01 +00002253 case StmtExprClass: {
2254 // Statement exprs don't logically have side effects themselves, but are
2255 // sometimes used in macros in ways that give them a type that is unused.
2256 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2257 // however, if the result of the stmt expr is dead, we don't want to emit a
2258 // warning.
2259 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002260 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002261 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002262 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002263 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2264 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002265 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002266 }
Mike Stump11289f42009-09-09 15:08:12 +00002267
John McCallc493a732010-03-12 07:11:26 +00002268 if (getType()->isVoidType())
2269 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002270 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002271 Loc = cast<StmtExpr>(this)->getLParenLoc();
2272 R1 = getSourceRange();
2273 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002274 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002275 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002276 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002277 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002278 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002279 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002280 if (CE->getCastKind() == CK_ToVoid) {
2281 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002282 CE->getSubExpr()->getType().isVolatileQualified()) {
2283 const DeclRefExpr *DRE =
2284 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2285 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2286 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2287 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2288 R1, R2, Ctx);
2289 }
2290 }
Chris Lattner2706a552009-07-28 18:25:28 +00002291 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002292 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002293
Eli Friedmanc11535c2012-05-24 00:47:05 +00002294 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002295 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002296 if (CE->getCastKind() == CK_ConstructorConversion)
2297 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002298
Eli Friedmanc11535c2012-05-24 00:47:05 +00002299 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002300 if (const CXXFunctionalCastExpr *CXXCE =
2301 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002302 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002303 R1 = CXXCE->getSubExpr()->getSourceRange();
2304 } else {
2305 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2306 Loc = CStyleCE->getLParenLoc();
2307 R1 = CStyleCE->getSubExpr()->getSourceRange();
2308 }
Chris Lattner237f2752009-02-14 07:37:35 +00002309 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002310 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002311 case ImplicitCastExprClass: {
2312 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002313
Eli Friedmanc11535c2012-05-24 00:47:05 +00002314 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2315 if (ICE->getCastKind() == CK_LValueToRValue &&
2316 ICE->getSubExpr()->getType().isVolatileQualified())
2317 return false;
2318
2319 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2320 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002321 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002322 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002323 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002324 case CXXDefaultInitExprClass:
2325 return (cast<CXXDefaultInitExpr>(this)
2326 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002327
2328 case CXXNewExprClass:
2329 // FIXME: In theory, there might be new expressions that don't have side
2330 // effects (e.g. a placement new with an uninitialized POD).
2331 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002332 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002333 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002334 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002335 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002336 case ExprWithCleanupsClass:
2337 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002338 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002339 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002340}
2341
Fariborz Jahanian07735332009-02-22 18:40:18 +00002342/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002343/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002344bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002345 const Expr *E = IgnoreParens();
2346 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002347 default:
2348 return false;
2349 case ObjCIvarRefExprClass:
2350 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002351 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002352 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002353 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002354 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002355 case MaterializeTemporaryExprClass:
2356 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2357 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002358 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002359 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002360 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002361 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002362
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002363 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2364 if (VD->hasGlobalStorage())
2365 return true;
2366 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002367 // dereferencing to a pointer is always a gc'able candidate,
2368 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002369 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002370 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002371 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002372 return false;
2373 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002374 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002375 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002376 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002377 }
2378 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002379 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002380 }
2381}
Sebastian Redlce354af2010-09-10 20:55:33 +00002382
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002383bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2384 if (isTypeDependent())
2385 return false;
John McCall086a4642010-11-24 05:12:34 +00002386 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002387}
2388
John McCall0009fcc2011-04-26 20:42:42 +00002389QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002390 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002391
2392 // Bound member expressions are always one of these possibilities:
2393 // x->m x.m x->*y x.*y
2394 // (possibly parenthesized)
2395
2396 expr = expr->IgnoreParens();
2397 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2398 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2399 return mem->getMemberDecl()->getType();
2400 }
2401
2402 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2403 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2404 ->getPointeeType();
2405 assert(type->isFunctionType());
2406 return type;
2407 }
2408
David Majnemerced8bdf2015-02-25 17:36:15 +00002409 assert(isa<UnresolvedMemberExpr>(expr) || isa<CXXPseudoDestructorExpr>(expr));
John McCall0009fcc2011-04-26 20:42:42 +00002410 return QualType();
2411}
2412
Ted Kremenekfff70962008-01-17 16:57:34 +00002413Expr* Expr::IgnoreParens() {
2414 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002415 while (true) {
2416 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2417 E = P->getSubExpr();
2418 continue;
2419 }
2420 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2421 if (P->getOpcode() == UO_Extension) {
2422 E = P->getSubExpr();
2423 continue;
2424 }
2425 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002426 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2427 if (!P->isResultDependent()) {
2428 E = P->getResultExpr();
2429 continue;
2430 }
2431 }
Eli Friedman75807f22013-07-20 00:40:58 +00002432 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2433 if (!P->isConditionDependent()) {
2434 E = P->getChosenSubExpr();
2435 continue;
2436 }
2437 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002438 return E;
2439 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002440}
2441
Chris Lattnerf2660962008-02-13 01:02:39 +00002442/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2443/// or CastExprs or ImplicitCastExprs, returning their operand.
2444Expr *Expr::IgnoreParenCasts() {
2445 Expr *E = this;
2446 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002447 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002448 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002449 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002450 continue;
2451 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002452 if (MaterializeTemporaryExpr *Materialize
2453 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2454 E = Materialize->GetTemporaryExpr();
2455 continue;
2456 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002457 if (SubstNonTypeTemplateParmExpr *NTTP
2458 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2459 E = NTTP->getReplacement();
2460 continue;
2461 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002462 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002463 }
2464}
2465
Ted Kremenek6f375e52014-04-16 07:26:09 +00002466Expr *Expr::IgnoreCasts() {
2467 Expr *E = this;
2468 while (true) {
2469 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2470 E = P->getSubExpr();
2471 continue;
2472 }
2473 if (MaterializeTemporaryExpr *Materialize
2474 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2475 E = Materialize->GetTemporaryExpr();
2476 continue;
2477 }
2478 if (SubstNonTypeTemplateParmExpr *NTTP
2479 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2480 E = NTTP->getReplacement();
2481 continue;
2482 }
2483 return E;
2484 }
2485}
2486
John McCall5a4ce8b2010-12-04 08:24:19 +00002487/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2488/// casts. This is intended purely as a temporary workaround for code
2489/// that hasn't yet been rewritten to do the right thing about those
2490/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002491Expr *Expr::IgnoreParenLValueCasts() {
2492 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002493 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002494 E = E->IgnoreParens();
2495 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002496 if (P->getCastKind() == CK_LValueToRValue) {
2497 E = P->getSubExpr();
2498 continue;
2499 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002500 } else if (MaterializeTemporaryExpr *Materialize
2501 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2502 E = Materialize->GetTemporaryExpr();
2503 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002504 } else if (SubstNonTypeTemplateParmExpr *NTTP
2505 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2506 E = NTTP->getReplacement();
2507 continue;
John McCall34376a62010-12-04 03:47:34 +00002508 }
2509 break;
2510 }
2511 return E;
2512}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002513
2514Expr *Expr::ignoreParenBaseCasts() {
2515 Expr *E = this;
2516 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002517 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002518 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2519 if (CE->getCastKind() == CK_DerivedToBase ||
2520 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2521 CE->getCastKind() == CK_NoOp) {
2522 E = CE->getSubExpr();
2523 continue;
2524 }
2525 }
2526
2527 return E;
2528 }
2529}
2530
John McCalleebc8322010-05-05 22:59:52 +00002531Expr *Expr::IgnoreParenImpCasts() {
2532 Expr *E = this;
2533 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002534 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002535 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002536 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002537 continue;
2538 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002539 if (MaterializeTemporaryExpr *Materialize
2540 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2541 E = Materialize->GetTemporaryExpr();
2542 continue;
2543 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002544 if (SubstNonTypeTemplateParmExpr *NTTP
2545 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2546 E = NTTP->getReplacement();
2547 continue;
2548 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002549 return E;
John McCalleebc8322010-05-05 22:59:52 +00002550 }
2551}
2552
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002553Expr *Expr::IgnoreConversionOperator() {
2554 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002555 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002556 return MCE->getImplicitObjectArgument();
2557 }
2558 return this;
2559}
2560
Chris Lattneref26c772009-03-13 17:28:01 +00002561/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2562/// value (including ptr->int casts of the same size). Strip off any
2563/// ParenExpr or CastExprs, returning their operand.
2564Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2565 Expr *E = this;
2566 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002567 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattneref26c772009-03-13 17:28:01 +00002569 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2570 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002571 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002572 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002573
Chris Lattneref26c772009-03-13 17:28:01 +00002574 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2575 E = SE;
2576 continue;
2577 }
Mike Stump11289f42009-09-09 15:08:12 +00002578
Abramo Bagnara932e3932010-10-15 07:51:18 +00002579 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002580 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002581 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002582 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002583 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2584 E = SE;
2585 continue;
2586 }
2587 }
Mike Stump11289f42009-09-09 15:08:12 +00002588
Douglas Gregor6a40b082011-09-08 17:56:33 +00002589 if (SubstNonTypeTemplateParmExpr *NTTP
2590 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2591 E = NTTP->getReplacement();
2592 continue;
2593 }
2594
Chris Lattneref26c772009-03-13 17:28:01 +00002595 return E;
2596 }
2597}
2598
Douglas Gregord196a582009-12-14 19:27:10 +00002599bool Expr::isDefaultArgument() const {
2600 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002601 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2602 E = M->GetTemporaryExpr();
2603
Douglas Gregord196a582009-12-14 19:27:10 +00002604 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2605 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002606
Douglas Gregord196a582009-12-14 19:27:10 +00002607 return isa<CXXDefaultArgExpr>(E);
2608}
Chris Lattneref26c772009-03-13 17:28:01 +00002609
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002610/// \brief Skip over any no-op casts and any temporary-binding
2611/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002612static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002613 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2614 E = M->GetTemporaryExpr();
2615
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002616 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002617 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002618 E = ICE->getSubExpr();
2619 else
2620 break;
2621 }
2622
2623 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2624 E = BE->getSubExpr();
2625
2626 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002627 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002628 E = ICE->getSubExpr();
2629 else
2630 break;
2631 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002632
2633 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002634}
2635
John McCall7a626f62010-09-15 10:14:12 +00002636/// isTemporaryObject - Determines if this expression produces a
2637/// temporary of the given class type.
2638bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2639 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2640 return false;
2641
Anders Carlsson66bbf502010-11-28 16:40:49 +00002642 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002643
John McCall02dc8c72010-09-15 20:59:13 +00002644 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002645 if (!E->Classify(C).isPRValue()) {
2646 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002647 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002648 return false;
2649 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002650
John McCallf4ee1dd2010-09-16 06:57:56 +00002651 // Black-list a few cases which yield pr-values of class type that don't
2652 // refer to temporaries of that type:
2653
2654 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002655 if (isa<ImplicitCastExpr>(E)) {
2656 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2657 case CK_DerivedToBase:
2658 case CK_UncheckedDerivedToBase:
2659 return false;
2660 default:
2661 break;
2662 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002663 }
2664
John McCallf4ee1dd2010-09-16 06:57:56 +00002665 // - member expressions (all)
2666 if (isa<MemberExpr>(E))
2667 return false;
2668
Eli Friedman13ffdd82012-06-15 23:51:06 +00002669 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2670 if (BO->isPtrMemOp())
2671 return false;
2672
John McCallc07a0c72011-02-17 10:25:35 +00002673 // - opaque values (all)
2674 if (isa<OpaqueValueExpr>(E))
2675 return false;
2676
John McCall7a626f62010-09-15 10:14:12 +00002677 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002678}
2679
Douglas Gregor25b7e052011-03-02 21:06:53 +00002680bool Expr::isImplicitCXXThis() const {
2681 const Expr *E = this;
2682
2683 // Strip away parentheses and casts we don't care about.
2684 while (true) {
2685 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2686 E = Paren->getSubExpr();
2687 continue;
2688 }
2689
2690 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2691 if (ICE->getCastKind() == CK_NoOp ||
2692 ICE->getCastKind() == CK_LValueToRValue ||
2693 ICE->getCastKind() == CK_DerivedToBase ||
2694 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2695 E = ICE->getSubExpr();
2696 continue;
2697 }
2698 }
2699
2700 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2701 if (UnOp->getOpcode() == UO_Extension) {
2702 E = UnOp->getSubExpr();
2703 continue;
2704 }
2705 }
2706
Douglas Gregorfe314812011-06-21 17:03:29 +00002707 if (const MaterializeTemporaryExpr *M
2708 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2709 E = M->GetTemporaryExpr();
2710 continue;
2711 }
2712
Douglas Gregor25b7e052011-03-02 21:06:53 +00002713 break;
2714 }
2715
2716 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2717 return This->isImplicit();
2718
2719 return false;
2720}
2721
Douglas Gregor4619e432008-12-05 23:32:09 +00002722/// hasAnyTypeDependentArguments - Determines if any of the expressions
2723/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002724bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002725 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002726 if (Exprs[I]->isTypeDependent())
2727 return true;
2728
2729 return false;
2730}
2731
Abramo Bagnara847c6602014-05-22 19:20:46 +00002732bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2733 const Expr **Culprit) const {
Eli Friedman384da272009-01-25 03:12:18 +00002734 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002735 // which can be evaluated at compile-time. It very closely parallels
2736 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2737 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2738 // to isEvaluatable most of the time.
2739 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002740 // If we ever capture reference-binding directly in the AST, we can
2741 // kill the second parameter.
2742
2743 if (IsForRef) {
2744 EvalResult Result;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002745 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2746 return true;
2747 if (Culprit)
2748 *Culprit = this;
2749 return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002750 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002751
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002752 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002753 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002754 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002755 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002756 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002757 case CXXTemporaryObjectExprClass:
2758 case CXXConstructExprClass: {
2759 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002760
Eli Friedman4c27ac22013-07-16 22:40:53 +00002761 if (CE->getConstructor()->isTrivial() &&
2762 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2763 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002764 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002765
Eli Friedman4c27ac22013-07-16 22:40:53 +00002766 // Trivial copy constructor
2767 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Abramo Bagnara847c6602014-05-22 19:20:46 +00002768 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62306a2011-11-10 06:34:14 +00002769 }
2770
Richard Smithd62306a2011-11-10 06:34:14 +00002771 break;
John McCall81c9cea2010-08-01 21:51:45 +00002772 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002773 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002774 // This handles gcc's extension that allows global initializers like
2775 // "struct x {int x;} x = (struct x) {};".
2776 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002777 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002778 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002779 }
Yunzhong Gaocb779302015-06-10 00:27:52 +00002780 case DesignatedInitUpdateExprClass: {
2781 const DesignatedInitUpdateExpr *DIUE = cast<DesignatedInitUpdateExpr>(this);
2782 return DIUE->getBase()->isConstantInitializer(Ctx, false, Culprit) &&
2783 DIUE->getUpdater()->isConstantInitializer(Ctx, false, Culprit);
2784 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002785 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002786 const InitListExpr *ILE = cast<InitListExpr>(this);
2787 if (ILE->getType()->isArrayType()) {
2788 unsigned numInits = ILE->getNumInits();
2789 for (unsigned i = 0; i < numInits; i++) {
Abramo Bagnara847c6602014-05-22 19:20:46 +00002790 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002791 return false;
2792 }
2793 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002794 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002795
2796 if (ILE->getType()->isRecordType()) {
2797 unsigned ElementNo = 0;
2798 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
Hans Wennborga302cd92014-08-21 16:06:57 +00002799 for (const auto *Field : RD->fields()) {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002800 // If this is a union, skip all the fields that aren't being initialized.
Hans Wennborga302cd92014-08-21 16:06:57 +00002801 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
Eli Friedman4c27ac22013-07-16 22:40:53 +00002802 continue;
2803
2804 // Don't emit anonymous bitfields, they just affect layout.
2805 if (Field->isUnnamedBitfield())
2806 continue;
2807
2808 if (ElementNo < ILE->getNumInits()) {
2809 const Expr *Elt = ILE->getInit(ElementNo++);
2810 if (Field->isBitField()) {
2811 // Bitfields have to evaluate to an integer.
2812 llvm::APSInt ResultTmp;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002813 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2814 if (Culprit)
2815 *Culprit = Elt;
Eli Friedman4c27ac22013-07-16 22:40:53 +00002816 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002817 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002818 } else {
2819 bool RefType = Field->getType()->isReferenceType();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002820 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002821 return false;
2822 }
2823 }
2824 }
2825 return true;
2826 }
2827
2828 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002829 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002830 case ImplicitValueInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00002831 case NoInitExprClass:
Douglas Gregor0202cb42009-01-29 17:44:32 +00002832 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002833 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002834 return cast<ParenExpr>(this)->getSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002835 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbourne91147592011-04-15 00:35:48 +00002836 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002837 return cast<GenericSelectionExpr>(this)->getResultExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002838 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002839 case ChooseExprClass:
Abramo Bagnara847c6602014-05-22 19:20:46 +00002840 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2841 if (Culprit)
2842 *Culprit = this;
Eli Friedman75807f22013-07-20 00:40:58 +00002843 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002844 }
Eli Friedman75807f22013-07-20 00:40:58 +00002845 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002846 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002847 case UnaryOperatorClass: {
2848 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002849 if (Exp->getOpcode() == UO_Extension)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002850 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002851 break;
2852 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002853 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002854 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002855 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002856 case CStyleCastExprClass:
2857 case ObjCBridgedCastExprClass:
2858 case CXXDynamicCastExprClass:
2859 case CXXReinterpretCastExprClass:
2860 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002861 const CastExpr *CE = cast<CastExpr>(this);
2862
Eli Friedman13ec75b2011-12-21 00:43:02 +00002863 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002864 if (CE->getCastKind() == CK_NoOp ||
2865 CE->getCastKind() == CK_LValueToRValue ||
2866 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002867 CE->getCastKind() == CK_ConstructorConversion ||
2868 CE->getCastKind() == CK_NonAtomicToAtomic ||
2869 CE->getCastKind() == CK_AtomicToNonAtomic)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002870 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smith161f09a2011-12-06 22:44:34 +00002871
Eli Friedman384da272009-01-25 03:12:18 +00002872 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002873 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002874 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002875 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002876 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002877
2878 case SubstNonTypeTemplateParmExprClass:
2879 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002880 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002881 case CXXDefaultArgExprClass:
2882 return cast<CXXDefaultArgExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002883 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002884 case CXXDefaultInitExprClass:
2885 return cast<CXXDefaultInitExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002886 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002887 }
Abramo Bagnara847c6602014-05-22 19:20:46 +00002888 if (isEvaluatable(Ctx))
2889 return true;
2890 if (Culprit)
2891 *Culprit = this;
2892 return false;
Steve Naroffb03f5942007-09-02 20:30:18 +00002893}
2894
Scott Douglasscc013592015-06-10 15:18:23 +00002895namespace {
2896 /// \brief Look for any side effects within a Stmt.
2897 class SideEffectFinder : public ConstEvaluatedExprVisitor<SideEffectFinder> {
2898 typedef ConstEvaluatedExprVisitor<SideEffectFinder> Inherited;
2899 const bool IncludePossibleEffects;
2900 bool HasSideEffects;
2901
2902 public:
2903 explicit SideEffectFinder(const ASTContext &Context, bool IncludePossible)
2904 : Inherited(Context),
2905 IncludePossibleEffects(IncludePossible), HasSideEffects(false) { }
2906
2907 bool hasSideEffects() const { return HasSideEffects; }
2908
2909 void VisitExpr(const Expr *E) {
2910 if (!HasSideEffects &&
2911 E->HasSideEffects(Context, IncludePossibleEffects))
2912 HasSideEffects = true;
2913 }
2914 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002915}
Scott Douglasscc013592015-06-10 15:18:23 +00002916
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002917bool Expr::HasSideEffects(const ASTContext &Ctx,
2918 bool IncludePossibleEffects) const {
2919 // In circumstances where we care about definite side effects instead of
2920 // potential side effects, we want to ignore expressions that are part of a
2921 // macro expansion as a potential side effect.
2922 if (!IncludePossibleEffects && getExprLoc().isMacroID())
2923 return false;
2924
Richard Smith0421ce72012-08-07 04:16:51 +00002925 if (isInstantiationDependent())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002926 return IncludePossibleEffects;
Richard Smith0421ce72012-08-07 04:16:51 +00002927
2928 switch (getStmtClass()) {
2929 case NoStmtClass:
2930 #define ABSTRACT_STMT(Type)
2931 #define STMT(Type, Base) case Type##Class:
2932 #define EXPR(Type, Base)
2933 #include "clang/AST/StmtNodes.inc"
2934 llvm_unreachable("unexpected Expr kind");
2935
2936 case DependentScopeDeclRefExprClass:
2937 case CXXUnresolvedConstructExprClass:
2938 case CXXDependentScopeMemberExprClass:
2939 case UnresolvedLookupExprClass:
2940 case UnresolvedMemberExprClass:
2941 case PackExpansionExprClass:
2942 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002943 case FunctionParmPackExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002944 case TypoExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00002945 case CXXFoldExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002946 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2947
Richard Smitha33e4fe2012-08-07 05:18:29 +00002948 case DeclRefExprClass:
2949 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002950 case PredefinedExprClass:
2951 case IntegerLiteralClass:
2952 case FloatingLiteralClass:
2953 case ImaginaryLiteralClass:
2954 case StringLiteralClass:
2955 case CharacterLiteralClass:
2956 case OffsetOfExprClass:
2957 case ImplicitValueInitExprClass:
2958 case UnaryExprOrTypeTraitExprClass:
2959 case AddrLabelExprClass:
2960 case GNUNullExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00002961 case NoInitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002962 case CXXBoolLiteralExprClass:
2963 case CXXNullPtrLiteralExprClass:
2964 case CXXThisExprClass:
2965 case CXXScalarValueInitExprClass:
2966 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002967 case ArrayTypeTraitExprClass:
2968 case ExpressionTraitExprClass:
2969 case CXXNoexceptExprClass:
2970 case SizeOfPackExprClass:
2971 case ObjCStringLiteralClass:
2972 case ObjCEncodeExprClass:
2973 case ObjCBoolLiteralExprClass:
2974 case CXXUuidofExprClass:
2975 case OpaqueValueExprClass:
2976 // These never have a side-effect.
2977 return false;
2978
2979 case CallExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002980 case CXXOperatorCallExprClass:
2981 case CXXMemberCallExprClass:
2982 case CUDAKernelCallExprClass:
Michael Kupersteinaed5ccd2015-04-06 13:22:01 +00002983 case UserDefinedLiteralClass: {
2984 // We don't know a call definitely has side effects, except for calls
2985 // to pure/const functions that definitely don't.
2986 // If the call itself is considered side-effect free, check the operands.
2987 const Decl *FD = cast<CallExpr>(this)->getCalleeDecl();
2988 bool IsPure = FD && (FD->hasAttr<ConstAttr>() || FD->hasAttr<PureAttr>());
2989 if (IsPure || !IncludePossibleEffects)
2990 break;
2991 return true;
2992 }
2993
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002994 case BlockExprClass:
2995 case CXXBindTemporaryExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002996 if (!IncludePossibleEffects)
2997 break;
2998 return true;
2999
John McCall5e77d762013-04-16 07:28:30 +00003000 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003001 case CompoundAssignOperatorClass:
3002 case VAArgExprClass:
3003 case AtomicExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003004 case CXXThrowExprClass:
3005 case CXXNewExprClass:
3006 case CXXDeleteExprClass:
3007 case ExprWithCleanupsClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003008 // These always have a side-effect.
3009 return true;
3010
Scott Douglasscc013592015-06-10 15:18:23 +00003011 case StmtExprClass: {
3012 // StmtExprs have a side-effect if any substatement does.
3013 SideEffectFinder Finder(Ctx, IncludePossibleEffects);
3014 Finder.Visit(cast<StmtExpr>(this)->getSubStmt());
3015 return Finder.hasSideEffects();
3016 }
3017
Richard Smith0421ce72012-08-07 04:16:51 +00003018 case ParenExprClass:
3019 case ArraySubscriptExprClass:
Alexey Bataev1a3320e2015-08-25 14:24:04 +00003020 case OMPArraySectionExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003021 case MemberExprClass:
3022 case ConditionalOperatorClass:
3023 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003024 case CompoundLiteralExprClass:
3025 case ExtVectorElementExprClass:
3026 case DesignatedInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00003027 case DesignatedInitUpdateExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003028 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003029 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00003030 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003031 case SubstNonTypeTemplateParmExprClass:
3032 case MaterializeTemporaryExprClass:
3033 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00003034 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003035 case AsTypeExprClass:
3036 // These have a side-effect if any subexpression does.
3037 break;
3038
Richard Smitha33e4fe2012-08-07 05:18:29 +00003039 case UnaryOperatorClass:
3040 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00003041 return true;
3042 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003043
3044 case BinaryOperatorClass:
3045 if (cast<BinaryOperator>(this)->isAssignmentOp())
3046 return true;
3047 break;
3048
Richard Smith0421ce72012-08-07 04:16:51 +00003049 case InitListExprClass:
3050 // FIXME: The children for an InitListExpr doesn't include the array filler.
3051 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003052 if (E->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003053 return true;
3054 break;
3055
3056 case GenericSelectionExprClass:
3057 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003058 HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003059
3060 case ChooseExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003061 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
3062 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003063
3064 case CXXDefaultArgExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003065 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
3066 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003067
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003068 case CXXDefaultInitExprClass: {
3069 const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3070 if (const Expr *E = FD->getInClassInitializer())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003071 return E->HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith852c9db2013-04-20 22:23:05 +00003072 // If we've not yet parsed the initializer, assume it has side-effects.
3073 return true;
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003074 }
Richard Smith852c9db2013-04-20 22:23:05 +00003075
Richard Smith0421ce72012-08-07 04:16:51 +00003076 case CXXDynamicCastExprClass: {
3077 // A dynamic_cast expression has side-effects if it can throw.
3078 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3079 if (DCE->getTypeAsWritten()->isReferenceType() &&
3080 DCE->getCastKind() == CK_Dynamic)
3081 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003082 } // Fall through.
3083 case ImplicitCastExprClass:
3084 case CStyleCastExprClass:
3085 case CXXStaticCastExprClass:
3086 case CXXReinterpretCastExprClass:
3087 case CXXConstCastExprClass:
3088 case CXXFunctionalCastExprClass: {
Aaron Ballman409af502015-01-03 17:00:12 +00003089 // While volatile reads are side-effecting in both C and C++, we treat them
3090 // as having possible (not definite) side-effects. This allows idiomatic
3091 // code to behave without warning, such as sizeof(*v) for a volatile-
3092 // qualified pointer.
3093 if (!IncludePossibleEffects)
3094 break;
3095
Richard Smitha33e4fe2012-08-07 05:18:29 +00003096 const CastExpr *CE = cast<CastExpr>(this);
3097 if (CE->getCastKind() == CK_LValueToRValue &&
3098 CE->getSubExpr()->getType().isVolatileQualified())
3099 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003100 break;
3101 }
3102
Richard Smithef8bf432012-08-13 20:08:14 +00003103 case CXXTypeidExprClass:
3104 // typeid might throw if its subexpression is potentially-evaluated, so has
3105 // side-effects in that case whether or not its subexpression does.
3106 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00003107
3108 case CXXConstructExprClass:
3109 case CXXTemporaryObjectExprClass: {
3110 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003111 if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
Richard Smith0421ce72012-08-07 04:16:51 +00003112 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003113 // A trivial constructor does not add any side-effects of its own. Just look
3114 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00003115 break;
3116 }
3117
3118 case LambdaExprClass: {
3119 const LambdaExpr *LE = cast<LambdaExpr>(this);
3120 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3121 E = LE->capture_end(); I != E; ++I)
3122 if (I->getCaptureKind() == LCK_ByCopy)
3123 // FIXME: Only has a side-effect if the variable is volatile or if
3124 // the copy would invoke a non-trivial copy constructor.
3125 return true;
3126 return false;
3127 }
3128
3129 case PseudoObjectExprClass: {
3130 // Only look for side-effects in the semantic form, and look past
3131 // OpaqueValueExpr bindings in that form.
3132 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3133 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3134 E = PO->semantics_end();
3135 I != E; ++I) {
3136 const Expr *Subexpr = *I;
3137 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3138 Subexpr = OVE->getSourceExpr();
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003139 if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003140 return true;
3141 }
3142 return false;
3143 }
3144
3145 case ObjCBoxedExprClass:
3146 case ObjCArrayLiteralClass:
3147 case ObjCDictionaryLiteralClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003148 case ObjCSelectorExprClass:
3149 case ObjCProtocolExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003150 case ObjCIsaExprClass:
3151 case ObjCIndirectCopyRestoreExprClass:
3152 case ObjCSubscriptRefExprClass:
3153 case ObjCBridgedCastExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003154 case ObjCMessageExprClass:
3155 case ObjCPropertyRefExprClass:
3156 // FIXME: Classify these cases better.
3157 if (IncludePossibleEffects)
3158 return true;
3159 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003160 }
3161
3162 // Recurse to children.
Benjamin Kramer642f1732015-07-02 21:03:14 +00003163 for (const Stmt *SubStmt : children())
3164 if (SubStmt &&
3165 cast<Expr>(SubStmt)->HasSideEffects(Ctx, IncludePossibleEffects))
3166 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003167
3168 return false;
3169}
3170
Douglas Gregor1be329d2012-02-23 07:33:15 +00003171namespace {
3172 /// \brief Look for a call to a non-trivial function within an expression.
Scott Douglass503fc392015-06-10 13:53:15 +00003173 class NonTrivialCallFinder : public ConstEvaluatedExprVisitor<NonTrivialCallFinder>
Douglas Gregor1be329d2012-02-23 07:33:15 +00003174 {
Scott Douglass503fc392015-06-10 13:53:15 +00003175 typedef ConstEvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3176
Douglas Gregor1be329d2012-02-23 07:33:15 +00003177 bool NonTrivial;
3178
3179 public:
Scott Douglass503fc392015-06-10 13:53:15 +00003180 explicit NonTrivialCallFinder(const ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003181 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003182
3183 bool hasNonTrivialCall() const { return NonTrivial; }
Scott Douglass503fc392015-06-10 13:53:15 +00003184
3185 void VisitCallExpr(const CallExpr *E) {
3186 if (const CXXMethodDecl *Method
3187 = dyn_cast_or_null<const CXXMethodDecl>(E->getCalleeDecl())) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003188 if (Method->isTrivial()) {
3189 // Recurse to children of the call.
3190 Inherited::VisitStmt(E);
3191 return;
3192 }
3193 }
3194
3195 NonTrivial = true;
3196 }
Scott Douglass503fc392015-06-10 13:53:15 +00003197
3198 void VisitCXXConstructExpr(const CXXConstructExpr *E) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003199 if (E->getConstructor()->isTrivial()) {
3200 // Recurse to children of the call.
3201 Inherited::VisitStmt(E);
3202 return;
3203 }
3204
3205 NonTrivial = true;
3206 }
Scott Douglass503fc392015-06-10 13:53:15 +00003207
3208 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003209 if (E->getTemporary()->getDestructor()->isTrivial()) {
3210 Inherited::VisitStmt(E);
3211 return;
3212 }
3213
3214 NonTrivial = true;
3215 }
3216 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00003217}
Douglas Gregor1be329d2012-02-23 07:33:15 +00003218
Scott Douglass503fc392015-06-10 13:53:15 +00003219bool Expr::hasNonTrivialCall(const ASTContext &Ctx) const {
Douglas Gregor1be329d2012-02-23 07:33:15 +00003220 NonTrivialCallFinder Finder(Ctx);
3221 Finder.Visit(this);
3222 return Finder.hasNonTrivialCall();
3223}
3224
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003225/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3226/// pointer constant or not, as well as the specific kind of constant detected.
3227/// Null pointer constants can be integer constant expressions with the
3228/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3229/// (a GNU extension).
3230Expr::NullPointerConstantKind
3231Expr::isNullPointerConstant(ASTContext &Ctx,
3232 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003233 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003234 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003235 switch (NPC) {
3236 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003237 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003238 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003239 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003240 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003241 else
3242 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003243
Douglas Gregor56751b52009-09-25 04:25:58 +00003244 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003245 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003246 }
3247 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003248
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003249 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003250 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003251 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003252 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003253 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003254 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003255 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003256 Pointee->isVoidType() && // to void*
3257 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003258 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003259 }
Steve Naroffada7d422007-05-20 17:54:12 +00003260 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003261 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3262 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003263 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003264 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3265 // Accept ((void*)0) as a null pointer constant, as many other
3266 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003267 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003268 } else if (const GenericSelectionExpr *GE =
3269 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003270 if (GE->isResultDependent())
3271 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003272 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003273 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3274 if (CE->isConditionDependent())
3275 return NPCK_NotNull;
3276 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003277 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003278 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003279 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003280 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003281 } else if (const CXXDefaultInitExpr *DefaultInit
3282 = dyn_cast<CXXDefaultInitExpr>(this)) {
3283 // See through default initializer expressions.
3284 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003285 } else if (isa<GNUNullExpr>(this)) {
3286 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003287 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003288 } else if (const MaterializeTemporaryExpr *M
3289 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3290 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003291 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3292 if (const Expr *Source = OVE->getSourceExpr())
3293 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003294 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003295
Richard Smith89645bc2013-01-02 12:01:23 +00003296 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003297 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003298 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003299
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003300 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003301 if (!Ctx.getLangOpts().CPlusPlus11 &&
3302 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003303 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3304 const Expr *InitExpr = CLE->getInitializer();
3305 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3306 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3307 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003308 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003309 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003310 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003311 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003312
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003313 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003314 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3315 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003316 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003317 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003318 if (Lit && !Lit->getValue())
3319 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003320 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003321 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003322 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003323 // If we have an integer constant expression, we need to *evaluate* it and
3324 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003325 if (!isIntegerConstantExpr(Ctx))
3326 return NPCK_NotNull;
3327 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003328
David Blaikie1c7c8f72012-08-08 17:33:31 +00003329 if (EvaluateKnownConstInt(Ctx) != 0)
3330 return NPCK_NotNull;
3331
3332 if (isa<IntegerLiteral>(this))
3333 return NPCK_ZeroLiteral;
3334 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003335}
Steve Narofff7a5da12007-07-28 23:10:27 +00003336
John McCall34376a62010-12-04 03:47:34 +00003337/// \brief If this expression is an l-value for an Objective C
3338/// property, find the underlying property reference expression.
3339const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3340 const Expr *E = this;
3341 while (true) {
3342 assert((E->getValueKind() == VK_LValue &&
3343 E->getObjectKind() == OK_ObjCProperty) &&
3344 "expression is not a property reference");
3345 E = E->IgnoreParenCasts();
3346 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3347 if (BO->getOpcode() == BO_Comma) {
3348 E = BO->getRHS();
3349 continue;
3350 }
3351 }
3352
3353 break;
3354 }
3355
3356 return cast<ObjCPropertyRefExpr>(E);
3357}
3358
Anna Zaks97c7ce32012-10-01 20:34:04 +00003359bool Expr::isObjCSelfExpr() const {
3360 const Expr *E = IgnoreParenImpCasts();
3361
3362 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3363 if (!DRE)
3364 return false;
3365
3366 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3367 if (!Param)
3368 return false;
3369
3370 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3371 if (!M)
3372 return false;
3373
3374 return M->getSelfDecl() == Param;
3375}
3376
John McCalld25db7e2013-05-06 21:39:12 +00003377FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003378 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003379
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003380 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003381 if (ICE->getCastKind() == CK_LValueToRValue ||
3382 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003383 E = ICE->getSubExpr()->IgnoreParens();
3384 else
3385 break;
3386 }
3387
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003388 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003389 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003390 if (Field->isBitField())
3391 return Field;
3392
John McCalld25db7e2013-05-06 21:39:12 +00003393 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3394 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3395 if (Ivar->isBitField())
3396 return Ivar;
3397
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003398 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3399 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3400 if (Field->isBitField())
3401 return Field;
3402
Eli Friedman609ada22011-07-13 02:05:57 +00003403 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003404 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003405 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003406
Eli Friedman609ada22011-07-13 02:05:57 +00003407 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003408 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003409 }
3410
Richard Smith5b571672014-09-24 23:55:00 +00003411 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3412 if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3413 return UnOp->getSubExpr()->getSourceBitField();
3414
Craig Topper36250ad2014-05-12 05:36:57 +00003415 return nullptr;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003416}
3417
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003418bool Expr::refersToVectorElement() const {
3419 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003420
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003421 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003422 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003423 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003424 E = ICE->getSubExpr()->IgnoreParens();
3425 else
3426 break;
3427 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003428
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003429 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3430 return ASE->getBase()->getType()->isVectorType();
3431
3432 if (isa<ExtVectorElementExpr>(E))
3433 return true;
3434
3435 return false;
3436}
3437
Andrey Bokhankod9eab9c2015-08-03 10:38:10 +00003438bool Expr::refersToGlobalRegisterVar() const {
3439 const Expr *E = this->IgnoreParenImpCasts();
3440
3441 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
3442 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
3443 if (VD->getStorageClass() == SC_Register &&
3444 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
3445 return true;
3446
3447 return false;
3448}
3449
Chris Lattnerb8211f62009-02-16 22:14:05 +00003450/// isArrow - Return true if the base expression is a pointer to vector,
3451/// return false if the base expression is a vector.
3452bool ExtVectorElementExpr::isArrow() const {
3453 return getBase()->getType()->isPointerType();
3454}
3455
Nate Begemance4d7fc2008-04-18 23:10:10 +00003456unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003457 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003458 return VT->getNumElements();
3459 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003460}
3461
Nate Begemanf322eab2008-05-09 06:41:27 +00003462/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003463bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003464 // FIXME: Refactor this code to an accessor on the AST node which returns the
3465 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003466 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003467
3468 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003469 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003470 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003471
Nate Begeman7e5185b2009-01-18 02:01:21 +00003472 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003473 if (Comp[0] == 's' || Comp[0] == 'S')
3474 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003475
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003476 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003477 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003478 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003479
Steve Naroff0d595ca2007-07-30 03:29:09 +00003480 return false;
3481}
Chris Lattner885b4952007-08-02 23:36:59 +00003482
Nate Begemanf322eab2008-05-09 06:41:27 +00003483/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003484void ExtVectorElementExpr::getEncodedElementAccess(
Benjamin Kramer99383102015-07-28 16:25:32 +00003485 SmallVectorImpl<uint32_t> &Elts) const {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003486 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003487 if (Comp[0] == 's' || Comp[0] == 'S')
3488 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003489
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003490 bool isHi = Comp == "hi";
3491 bool isLo = Comp == "lo";
3492 bool isEven = Comp == "even";
3493 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003494
Nate Begemanf322eab2008-05-09 06:41:27 +00003495 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3496 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003497
Nate Begemanf322eab2008-05-09 06:41:27 +00003498 if (isHi)
3499 Index = e + i;
3500 else if (isLo)
3501 Index = i;
3502 else if (isEven)
3503 Index = 2 * i;
3504 else if (isOdd)
3505 Index = 2 * i + 1;
3506 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003507 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003508
Nate Begemand3862152008-05-13 21:03:02 +00003509 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003510 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003511}
3512
Douglas Gregor9a129192010-04-21 00:45:42 +00003513ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003514 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003515 SourceLocation LBracLoc,
3516 SourceLocation SuperLoc,
3517 bool IsInstanceSuper,
3518 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003519 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003520 ArrayRef<SourceLocation> SelLocs,
3521 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003522 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003523 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003524 SourceLocation RBracLoc,
3525 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003526 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003527 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003528 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003529 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003530 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3531 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003532 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Craig Topper36250ad2014-05-12 05:36:57 +00003533 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3534 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3535 RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003536{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003537 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003538 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003539}
3540
Douglas Gregor9a129192010-04-21 00:45:42 +00003541ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003542 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003543 SourceLocation LBracLoc,
3544 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003545 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003546 ArrayRef<SourceLocation> SelLocs,
3547 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003548 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003549 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003550 SourceLocation RBracLoc,
3551 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003552 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003553 T->isDependentType(), T->isInstantiationDependentType(),
3554 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003555 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3556 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003557 Kind(Class),
Craig Topper36250ad2014-05-12 05:36:57 +00003558 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3559 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003560{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003561 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003562 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003563}
3564
Douglas Gregor9a129192010-04-21 00:45:42 +00003565ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003566 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003567 SourceLocation LBracLoc,
3568 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003569 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003570 ArrayRef<SourceLocation> SelLocs,
3571 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003572 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003573 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003574 SourceLocation RBracLoc,
3575 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003576 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003577 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003578 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003579 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003580 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3581 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003582 Kind(Instance),
Craig Topper36250ad2014-05-12 05:36:57 +00003583 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3584 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003585{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003586 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003587 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003588}
3589
3590void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3591 ArrayRef<SourceLocation> SelLocs,
3592 SelectorLocationsKind SelLocsK) {
3593 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003594 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003595 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003596 if (Args[I]->isTypeDependent())
3597 ExprBits.TypeDependent = true;
3598 if (Args[I]->isValueDependent())
3599 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003600 if (Args[I]->isInstantiationDependent())
3601 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003602 if (Args[I]->containsUnexpandedParameterPack())
3603 ExprBits.ContainsUnexpandedParameterPack = true;
3604
3605 MyArgs[I] = Args[I];
3606 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003607
Benjamin Kramer2325b242012-02-20 00:20:48 +00003608 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003609 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003610 if (SelLocsK == SelLoc_NonStandard)
3611 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3612 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003613}
3614
Craig Topperce7167c2013-08-22 04:58:56 +00003615ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003616 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003617 SourceLocation LBracLoc,
3618 SourceLocation SuperLoc,
3619 bool IsInstanceSuper,
3620 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003621 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003622 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003623 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003624 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003625 SourceLocation RBracLoc,
3626 bool isImplicit) {
3627 assert((!SelLocs.empty() || isImplicit) &&
3628 "No selector locs for non-implicit message");
3629 ObjCMessageExpr *Mem;
3630 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3631 if (isImplicit)
3632 Mem = alloc(Context, Args.size(), 0);
3633 else
3634 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003635 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003636 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003637 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003638}
3639
Craig Topperce7167c2013-08-22 04:58:56 +00003640ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003641 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003642 SourceLocation LBracLoc,
3643 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003644 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003645 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003646 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003647 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003648 SourceLocation RBracLoc,
3649 bool isImplicit) {
3650 assert((!SelLocs.empty() || isImplicit) &&
3651 "No selector locs for non-implicit message");
3652 ObjCMessageExpr *Mem;
3653 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3654 if (isImplicit)
3655 Mem = alloc(Context, Args.size(), 0);
3656 else
3657 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003658 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003659 SelLocs, SelLocsK, Method, Args, RBracLoc,
3660 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003661}
3662
Craig Topperce7167c2013-08-22 04:58:56 +00003663ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003664 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003665 SourceLocation LBracLoc,
3666 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003667 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003668 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003669 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003670 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003671 SourceLocation RBracLoc,
3672 bool isImplicit) {
3673 assert((!SelLocs.empty() || isImplicit) &&
3674 "No selector locs for non-implicit message");
3675 ObjCMessageExpr *Mem;
3676 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3677 if (isImplicit)
3678 Mem = alloc(Context, Args.size(), 0);
3679 else
3680 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003681 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003682 SelLocs, SelLocsK, Method, Args, RBracLoc,
3683 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003684}
3685
Craig Topperce7167c2013-08-22 04:58:56 +00003686ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003687 unsigned NumArgs,
3688 unsigned NumStoredSelLocs) {
3689 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003690 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3691}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003692
Craig Topperce7167c2013-08-22 04:58:56 +00003693ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003694 ArrayRef<Expr *> Args,
3695 SourceLocation RBraceLoc,
3696 ArrayRef<SourceLocation> SelLocs,
3697 Selector Sel,
3698 SelectorLocationsKind &SelLocsK) {
3699 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3700 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3701 : 0;
3702 return alloc(C, Args.size(), NumStoredSelLocs);
3703}
3704
Craig Topperce7167c2013-08-22 04:58:56 +00003705ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003706 unsigned NumArgs,
3707 unsigned NumStoredSelLocs) {
3708 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3709 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3710 return (ObjCMessageExpr *)C.Allocate(Size,
3711 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3712}
3713
3714void ObjCMessageExpr::getSelectorLocs(
3715 SmallVectorImpl<SourceLocation> &SelLocs) const {
3716 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3717 SelLocs.push_back(getSelectorLoc(i));
3718}
3719
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003720SourceRange ObjCMessageExpr::getReceiverRange() const {
3721 switch (getReceiverKind()) {
3722 case Instance:
3723 return getInstanceReceiver()->getSourceRange();
3724
3725 case Class:
3726 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3727
3728 case SuperInstance:
3729 case SuperClass:
3730 return getSuperLoc();
3731 }
3732
David Blaikiee4d798f2012-01-20 21:50:17 +00003733 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003734}
3735
Douglas Gregor9a129192010-04-21 00:45:42 +00003736Selector ObjCMessageExpr::getSelector() const {
3737 if (HasMethod)
3738 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3739 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003740 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003741}
3742
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003743QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003744 switch (getReceiverKind()) {
3745 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003746 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003747 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003748 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003749 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003750 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003751 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003752 }
3753
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003754 llvm_unreachable("unexpected receiver kind");
3755}
3756
3757ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3758 QualType T = getReceiverType();
3759
3760 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3761 return Ptr->getInterfaceDecl();
3762
3763 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3764 return Ty->getInterface();
3765
Craig Topper36250ad2014-05-12 05:36:57 +00003766 return nullptr;
Ted Kremenek2c809302010-02-11 22:41:21 +00003767}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003768
Douglas Gregore83b9562015-07-07 03:57:53 +00003769QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const {
3770 if (isClassReceiver())
3771 return ctx.getObjCInterfaceType(getClassReceiver());
3772
3773 if (isSuperReceiver())
3774 return getSuperReceiverType();
3775
3776 return getBase()->getType();
3777}
3778
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003779StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003780 switch (getBridgeKind()) {
3781 case OBC_Bridge:
3782 return "__bridge";
3783 case OBC_BridgeTransfer:
3784 return "__bridge_transfer";
3785 case OBC_BridgeRetained:
3786 return "__bridge_retained";
3787 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003788
3789 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003790}
3791
Craig Topper37932912013-08-18 10:09:15 +00003792ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003793 QualType Type, SourceLocation BLoc,
3794 SourceLocation RP)
3795 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3796 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003797 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003798 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003799 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003800{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003801 SubExprs = new (C) Stmt*[args.size()];
3802 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003803 if (args[i]->isTypeDependent())
3804 ExprBits.TypeDependent = true;
3805 if (args[i]->isValueDependent())
3806 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003807 if (args[i]->isInstantiationDependent())
3808 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003809 if (args[i]->containsUnexpandedParameterPack())
3810 ExprBits.ContainsUnexpandedParameterPack = true;
3811
3812 SubExprs[i] = args[i];
3813 }
3814}
3815
Craig Topper37932912013-08-18 10:09:15 +00003816void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003817 if (SubExprs) C.Deallocate(SubExprs);
3818
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003819 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003820 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003821 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003822}
Nate Begeman48745922009-08-12 02:28:50 +00003823
Craig Topper37932912013-08-18 10:09:15 +00003824GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003825 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003826 ArrayRef<TypeSourceInfo*> AssocTypes,
3827 ArrayRef<Expr*> AssocExprs,
3828 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003829 SourceLocation RParenLoc,
3830 bool ContainsUnexpandedParameterPack,
3831 unsigned ResultIndex)
3832 : Expr(GenericSelectionExprClass,
3833 AssocExprs[ResultIndex]->getType(),
3834 AssocExprs[ResultIndex]->getValueKind(),
3835 AssocExprs[ResultIndex]->getObjectKind(),
3836 AssocExprs[ResultIndex]->isTypeDependent(),
3837 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003838 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003839 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003840 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3841 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3842 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3843 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003844 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003845 assert(AssocTypes.size() == AssocExprs.size());
3846 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3847 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003848}
3849
Craig Topper37932912013-08-18 10:09:15 +00003850GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003851 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003852 ArrayRef<TypeSourceInfo*> AssocTypes,
3853 ArrayRef<Expr*> AssocExprs,
3854 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003855 SourceLocation RParenLoc,
3856 bool ContainsUnexpandedParameterPack)
3857 : Expr(GenericSelectionExprClass,
3858 Context.DependentTy,
3859 VK_RValue,
3860 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003861 /*isTypeDependent=*/true,
3862 /*isValueDependent=*/true,
3863 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003864 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003865 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3866 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3867 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3868 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003869 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003870 assert(AssocTypes.size() == AssocExprs.size());
3871 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3872 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003873}
3874
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003875//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003876// DesignatedInitExpr
3877//===----------------------------------------------------------------------===//
3878
Chandler Carruth631abd92011-06-16 06:47:06 +00003879IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003880 assert(Kind == FieldDesignator && "Only valid on a field designator");
3881 if (Field.NameOrField & 0x01)
3882 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3883 else
3884 return getField()->getIdentifier();
3885}
3886
Craig Topper37932912013-08-18 10:09:15 +00003887DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003888 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003889 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003890 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003891 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003892 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003893 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003894 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003895 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003896 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003897 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003898 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003899 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003900 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003901 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003902
3903 // Record the initializer itself.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003904 child_iterator Child = child_begin();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003905 *Child++ = Init;
3906
3907 // Copy the designators and their subexpressions, computing
3908 // value-dependence along the way.
3909 unsigned IndexIdx = 0;
3910 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003911 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003912
3913 if (this->Designators[I].isArrayDesignator()) {
3914 // Compute type- and value-dependence.
3915 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003916 if (Index->isTypeDependent() || Index->isValueDependent())
David Majnemer4f217682015-01-09 01:39:09 +00003917 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003918 if (Index->isInstantiationDependent())
3919 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003920 // Propagate unexpanded parameter packs.
3921 if (Index->containsUnexpandedParameterPack())
3922 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003923
3924 // Copy the index expressions into permanent storage.
3925 *Child++ = IndexExprs[IndexIdx++];
3926 } else if (this->Designators[I].isArrayRangeDesignator()) {
3927 // Compute type- and value-dependence.
3928 Expr *Start = IndexExprs[IndexIdx];
3929 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003930 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003931 End->isTypeDependent() || End->isValueDependent()) {
David Majnemer4f217682015-01-09 01:39:09 +00003932 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003933 ExprBits.InstantiationDependent = true;
3934 } else if (Start->isInstantiationDependent() ||
3935 End->isInstantiationDependent()) {
3936 ExprBits.InstantiationDependent = true;
3937 }
3938
Douglas Gregora6e053e2010-12-15 01:34:56 +00003939 // Propagate unexpanded parameter packs.
3940 if (Start->containsUnexpandedParameterPack() ||
3941 End->containsUnexpandedParameterPack())
3942 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003943
3944 // Copy the start/end expressions into permanent storage.
3945 *Child++ = IndexExprs[IndexIdx++];
3946 *Child++ = IndexExprs[IndexIdx++];
3947 }
3948 }
3949
Benjamin Kramerc215e762012-08-24 11:54:20 +00003950 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003951}
3952
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003953DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003954DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003955 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003956 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003957 SourceLocation ColonOrEqualLoc,
3958 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003959 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
James Y Knight53c76162015-07-17 18:21:37 +00003960 sizeof(Stmt *) * (IndexExprs.size() + 1),
3961 llvm::alignOf<DesignatedInitExpr>());
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003962 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003963 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003964 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003965}
3966
Craig Topper37932912013-08-18 10:09:15 +00003967DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003968 unsigned NumIndexExprs) {
3969 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3970 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3971 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3972}
3973
Craig Topper37932912013-08-18 10:09:15 +00003974void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003975 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003976 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003977 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003978 NumDesignators = NumDesigs;
3979 for (unsigned I = 0; I != NumDesigs; ++I)
3980 Designators[I] = Desigs[I];
3981}
3982
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003983SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3984 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3985 if (size() == 1)
3986 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003987 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3988 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003989}
3990
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003991SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003992 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003993 Designator &First =
3994 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003995 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003996 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003997 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3998 else
3999 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
4000 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00004001 StartLoc =
4002 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00004003 return StartLoc;
4004}
4005
4006SourceLocation DesignatedInitExpr::getLocEnd() const {
4007 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004008}
4009
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00004010Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004011 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00004012 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004013 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
4014}
4015
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00004016Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00004017 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004018 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00004019 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004020 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
4021}
4022
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00004023Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00004024 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004025 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00004026 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004027 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
4028}
4029
Douglas Gregord5846a12009-04-15 06:41:24 +00004030/// \brief Replaces the designator at index @p Idx with the series
4031/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00004032void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00004033 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00004034 const Designator *Last) {
4035 unsigned NumNewDesignators = Last - First;
4036 if (NumNewDesignators == 0) {
4037 std::copy_backward(Designators + Idx + 1,
4038 Designators + NumDesignators,
4039 Designators + Idx);
4040 --NumNewDesignators;
4041 return;
4042 } else if (NumNewDesignators == 1) {
4043 Designators[Idx] = *First;
4044 return;
4045 }
4046
Mike Stump11289f42009-09-09 15:08:12 +00004047 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00004048 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00004049 std::copy(Designators, Designators + Idx, NewDesignators);
4050 std::copy(First, Last, NewDesignators + Idx);
4051 std::copy(Designators + Idx + 1, Designators + NumDesignators,
4052 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00004053 Designators = NewDesignators;
4054 NumDesignators = NumDesignators - 1 + NumNewDesignators;
4055}
4056
Yunzhong Gaocb779302015-06-10 00:27:52 +00004057DesignatedInitUpdateExpr::DesignatedInitUpdateExpr(const ASTContext &C,
4058 SourceLocation lBraceLoc, Expr *baseExpr, SourceLocation rBraceLoc)
4059 : Expr(DesignatedInitUpdateExprClass, baseExpr->getType(), VK_RValue,
4060 OK_Ordinary, false, false, false, false) {
4061 BaseAndUpdaterExprs[0] = baseExpr;
4062
4063 InitListExpr *ILE = new (C) InitListExpr(C, lBraceLoc, None, rBraceLoc);
4064 ILE->setType(baseExpr->getType());
4065 BaseAndUpdaterExprs[1] = ILE;
4066}
4067
4068SourceLocation DesignatedInitUpdateExpr::getLocStart() const {
4069 return getBase()->getLocStart();
4070}
4071
4072SourceLocation DesignatedInitUpdateExpr::getLocEnd() const {
4073 return getBase()->getLocEnd();
4074}
4075
Craig Topper37932912013-08-18 10:09:15 +00004076ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00004077 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00004078 SourceLocation rparenloc)
4079 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00004080 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004081 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
4082 Exprs = new (C) Stmt*[exprs.size()];
4083 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00004084 if (exprs[i]->isTypeDependent())
4085 ExprBits.TypeDependent = true;
4086 if (exprs[i]->isValueDependent())
4087 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00004088 if (exprs[i]->isInstantiationDependent())
4089 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00004090 if (exprs[i]->containsUnexpandedParameterPack())
4091 ExprBits.ContainsUnexpandedParameterPack = true;
4092
Nate Begeman5ec4b312009-08-10 23:49:36 +00004093 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00004094 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00004095}
4096
John McCall1bf58462011-02-16 08:02:54 +00004097const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
4098 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
4099 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00004100 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
4101 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00004102 e = cast<CXXConstructExpr>(e)->getArg(0);
4103 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
4104 e = ice->getSubExpr();
4105 return cast<OpaqueValueExpr>(e);
4106}
4107
Craig Topper37932912013-08-18 10:09:15 +00004108PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
4109 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00004110 unsigned numSemanticExprs) {
4111 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
4112 (1 + numSemanticExprs) * sizeof(Expr*),
4113 llvm::alignOf<PseudoObjectExpr>());
4114 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
4115}
4116
4117PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
4118 : Expr(PseudoObjectExprClass, shell) {
4119 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
4120}
4121
Craig Topper37932912013-08-18 10:09:15 +00004122PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00004123 ArrayRef<Expr*> semantics,
4124 unsigned resultIndex) {
4125 assert(syntax && "no syntactic expression!");
4126 assert(semantics.size() && "no semantic expressions!");
4127
4128 QualType type;
4129 ExprValueKind VK;
4130 if (resultIndex == NoResult) {
4131 type = C.VoidTy;
4132 VK = VK_RValue;
4133 } else {
4134 assert(resultIndex < semantics.size());
4135 type = semantics[resultIndex]->getType();
4136 VK = semantics[resultIndex]->getValueKind();
4137 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4138 }
4139
4140 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4141 (1 + semantics.size()) * sizeof(Expr*),
4142 llvm::alignOf<PseudoObjectExpr>());
4143 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4144 resultIndex);
4145}
4146
4147PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4148 Expr *syntax, ArrayRef<Expr*> semantics,
4149 unsigned resultIndex)
4150 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4151 /*filled in at end of ctor*/ false, false, false, false) {
4152 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4153 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4154
4155 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4156 Expr *E = (i == 0 ? syntax : semantics[i-1]);
4157 getSubExprsBuffer()[i] = E;
4158
4159 if (E->isTypeDependent())
4160 ExprBits.TypeDependent = true;
4161 if (E->isValueDependent())
4162 ExprBits.ValueDependent = true;
4163 if (E->isInstantiationDependent())
4164 ExprBits.InstantiationDependent = true;
4165 if (E->containsUnexpandedParameterPack())
4166 ExprBits.ContainsUnexpandedParameterPack = true;
4167
4168 if (isa<OpaqueValueExpr>(E))
Craig Topper36250ad2014-05-12 05:36:57 +00004169 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCallfe96e0b2011-11-06 09:01:30 +00004170 "opaque-value semantic expressions for pseudo-object "
4171 "operations must have sources");
4172 }
4173}
4174
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004175//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00004176// Child Iterators for iterating over subexpressions/substatements
4177//===----------------------------------------------------------------------===//
4178
Peter Collingbournee190dee2011-03-11 19:24:49 +00004179// UnaryExprOrTypeTraitExpr
4180Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00004181 // If this is of a type and the type is a VLA type (and not a typedef), the
4182 // size expression of the VLA needs to be treated as an executable expression.
4183 // Why isn't this weirdness documented better in StmtIterator?
4184 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00004185 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00004186 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00004187 return child_range(child_iterator(T), child_iterator());
Benjamin Kramer5733e352015-07-18 17:09:36 +00004188 return child_range(child_iterator(), child_iterator());
Sebastian Redl6f282892008-11-11 17:56:53 +00004189 }
John McCallbd066782011-02-09 08:16:59 +00004190 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00004191}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004192
Steve Naroffd54978b2007-09-18 23:55:05 +00004193// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004194Stmt::child_range ObjCMessageExpr::children() {
4195 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004196 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004197 begin = reinterpret_cast<Stmt **>(this + 1);
4198 else
4199 begin = reinterpret_cast<Stmt **>(getArgs());
4200 return child_range(begin,
4201 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004202}
4203
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004204ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004205 QualType T, ObjCMethodDecl *Method,
4206 SourceRange SR)
4207 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4208 false, false, false, false),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004209 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004210{
4211 Expr **SaveElements = getElements();
4212 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4213 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4214 ExprBits.ValueDependent = true;
4215 if (Elements[I]->isInstantiationDependent())
4216 ExprBits.InstantiationDependent = true;
4217 if (Elements[I]->containsUnexpandedParameterPack())
4218 ExprBits.ContainsUnexpandedParameterPack = true;
4219
4220 SaveElements[I] = Elements[I];
4221 }
4222}
4223
Craig Topperce7167c2013-08-22 04:58:56 +00004224ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004225 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004226 QualType T, ObjCMethodDecl * Method,
4227 SourceRange SR) {
4228 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4229 + Elements.size() * sizeof(Expr *));
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004230 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004231}
4232
Craig Topperce7167c2013-08-22 04:58:56 +00004233ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004234 unsigned NumElements) {
4235
4236 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4237 + NumElements * sizeof(Expr *));
4238 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4239}
4240
4241ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4242 ArrayRef<ObjCDictionaryElement> VK,
4243 bool HasPackExpansions,
4244 QualType T, ObjCMethodDecl *method,
4245 SourceRange SR)
4246 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4247 false, false),
4248 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004249 DictWithObjectsMethod(method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004250{
4251 KeyValuePair *KeyValues = getKeyValues();
4252 ExpansionData *Expansions = getExpansionData();
4253 for (unsigned I = 0; I < NumElements; I++) {
4254 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4255 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4256 ExprBits.ValueDependent = true;
4257 if (VK[I].Key->isInstantiationDependent() ||
4258 VK[I].Value->isInstantiationDependent())
4259 ExprBits.InstantiationDependent = true;
4260 if (VK[I].EllipsisLoc.isInvalid() &&
4261 (VK[I].Key->containsUnexpandedParameterPack() ||
4262 VK[I].Value->containsUnexpandedParameterPack()))
4263 ExprBits.ContainsUnexpandedParameterPack = true;
4264
4265 KeyValues[I].Key = VK[I].Key;
4266 KeyValues[I].Value = VK[I].Value;
4267 if (Expansions) {
4268 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4269 if (VK[I].NumExpansions)
4270 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4271 else
4272 Expansions[I].NumExpansionsPlusOne = 0;
4273 }
4274 }
4275}
4276
4277ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004278ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004279 ArrayRef<ObjCDictionaryElement> VK,
4280 bool HasPackExpansions,
4281 QualType T, ObjCMethodDecl *method,
4282 SourceRange SR) {
4283 unsigned ExpansionsSize = 0;
4284 if (HasPackExpansions)
4285 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4286
4287 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4288 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004289 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004290}
4291
4292ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004293ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004294 bool HasPackExpansions) {
4295 unsigned ExpansionsSize = 0;
4296 if (HasPackExpansions)
4297 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4298 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4299 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4300 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4301 HasPackExpansions);
4302}
4303
Craig Topperce7167c2013-08-22 04:58:56 +00004304ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004305 Expr *base,
4306 Expr *key, QualType T,
4307 ObjCMethodDecl *getMethod,
4308 ObjCMethodDecl *setMethod,
4309 SourceLocation RB) {
4310 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4311 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4312 OK_ObjCSubscript,
4313 getMethod, setMethod, RB);
4314}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004315
Benjamin Kramerc215e762012-08-24 11:54:20 +00004316AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004317 QualType t, AtomicOp op, SourceLocation RP)
4318 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4319 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004320 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004321{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004322 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4323 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004324 if (args[i]->isTypeDependent())
4325 ExprBits.TypeDependent = true;
4326 if (args[i]->isValueDependent())
4327 ExprBits.ValueDependent = true;
4328 if (args[i]->isInstantiationDependent())
4329 ExprBits.InstantiationDependent = true;
4330 if (args[i]->containsUnexpandedParameterPack())
4331 ExprBits.ContainsUnexpandedParameterPack = true;
4332
4333 SubExprs[i] = args[i];
4334 }
4335}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004336
4337unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4338 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004339 case AO__c11_atomic_init:
4340 case AO__c11_atomic_load:
4341 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004342 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004343
4344 case AO__c11_atomic_store:
4345 case AO__c11_atomic_exchange:
4346 case AO__atomic_load:
4347 case AO__atomic_store:
4348 case AO__atomic_store_n:
4349 case AO__atomic_exchange_n:
4350 case AO__c11_atomic_fetch_add:
4351 case AO__c11_atomic_fetch_sub:
4352 case AO__c11_atomic_fetch_and:
4353 case AO__c11_atomic_fetch_or:
4354 case AO__c11_atomic_fetch_xor:
4355 case AO__atomic_fetch_add:
4356 case AO__atomic_fetch_sub:
4357 case AO__atomic_fetch_and:
4358 case AO__atomic_fetch_or:
4359 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004360 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004361 case AO__atomic_add_fetch:
4362 case AO__atomic_sub_fetch:
4363 case AO__atomic_and_fetch:
4364 case AO__atomic_or_fetch:
4365 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004366 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004367 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004368
4369 case AO__atomic_exchange:
4370 return 4;
4371
4372 case AO__c11_atomic_compare_exchange_strong:
4373 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004374 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004375
4376 case AO__atomic_compare_exchange:
4377 case AO__atomic_compare_exchange_n:
4378 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004379 }
4380 llvm_unreachable("unknown atomic op");
4381}