blob: ae8079031271502ca174bc1d0c03b5216404a157 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner86ee2862008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor1be329d2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbed356a2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000036#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000037using namespace clang;
38
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola60a2bba2012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
Craig Topper36250ad2014-05-12 05:36:57 +000047 return nullptr;
Rafael Espindola60a2bba2012-07-17 20:24:05 +000048
Rafael Espindola49e860b2012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smithf3fabd22013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola9c006de2012-10-27 01:03:43 +000057 const Expr *E = this;
58 while (true) {
59 E = E->IgnoreParens();
60
61 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
62 if ((CE->getCastKind() == CK_DerivedToBase ||
63 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
64 E->getType()->isRecordType()) {
65 E = CE->getSubExpr();
66 CXXRecordDecl *Derived
67 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
68 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
69 continue;
70 }
71
72 if (CE->getCastKind() == CK_NoOp) {
73 E = CE->getSubExpr();
74 continue;
75 }
76 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smith2d187902013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola9c006de2012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-10-27 01:03:43 +000090 E = BO->getLHS();
91 const MemberPointerType *MPT =
92 BO->getRHS()->getType()->getAs<MemberPointerType>();
93 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smithf3fabd22013-06-03 00:17:11 +000094 continue;
95 } else if (BO->getOpcode() == BO_Comma) {
96 CommaLHSs.push_back(BO->getLHS());
97 E = BO->getRHS();
98 continue;
Rafael Espindola9c006de2012-10-27 01:03:43 +000099 }
100 }
101
102 // Nothing changed.
103 break;
104 }
105 return E;
106}
107
Chris Lattner4ebae652010-04-16 23:34:13 +0000108/// isKnownToHaveBooleanValue - Return true if this is an integer expression
109/// that is known to return 0 or 1. This happens for _Bool/bool expressions
110/// but also int expressions which are produced by things like comparisons in
111/// C.
112bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000113 const Expr *E = IgnoreParens();
114
Chris Lattner4ebae652010-04-16 23:34:13 +0000115 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000116 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000117 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000118 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000119
Peter Collingbourne91147592011-04-15 00:35:48 +0000120 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000121 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000122 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000123 return UO->getSubExpr()->isKnownToHaveBooleanValue();
Richard Trieu0f097742014-04-04 04:13:47 +0000124 case UO_LNot:
125 return true;
Chris Lattner4ebae652010-04-16 23:34:13 +0000126 default:
127 return false;
128 }
129 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000130
John McCall45d30c32010-06-12 01:56:02 +0000131 // Only look through implicit casts. If the user writes
132 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000133 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000134 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000135
Peter Collingbourne91147592011-04-15 00:35:48 +0000136 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000137 switch (BO->getOpcode()) {
138 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000139 case BO_LT: // Relational operators.
140 case BO_GT:
141 case BO_LE:
142 case BO_GE:
143 case BO_EQ: // Equality operators.
144 case BO_NE:
145 case BO_LAnd: // AND operator.
146 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000147 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148
John McCalle3027922010-08-25 11:45:40 +0000149 case BO_And: // Bitwise AND operator.
150 case BO_Xor: // Bitwise XOR operator.
151 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000152 // Handle things like (x==2)|(y==12).
153 return BO->getLHS()->isKnownToHaveBooleanValue() &&
154 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000155
John McCalle3027922010-08-25 11:45:40 +0000156 case BO_Comma:
157 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000158 return BO->getRHS()->isKnownToHaveBooleanValue();
159 }
160 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000161
Peter Collingbourne91147592011-04-15 00:35:48 +0000162 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000163 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
164 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000165
Chris Lattner4ebae652010-04-16 23:34:13 +0000166 return false;
167}
168
John McCallbd066782011-02-09 08:16:59 +0000169// Amusing macro metaprogramming hack: check whether a class provides
170// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000171//
172// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000173namespace {
174 /// This implementation is used when a class provides a custom
175 /// implementation of getExprLoc.
176 template <class E, class T>
177 SourceLocation getExprLocImpl(const Expr *expr,
178 SourceLocation (T::*v)() const) {
179 return static_cast<const E*>(expr)->getExprLoc();
180 }
181
182 /// This implementation is used when a class doesn't provide
183 /// a custom implementation of getExprLoc. Overload resolution
184 /// should pick it over the implementation above because it's
185 /// more specialized according to function template partial ordering.
186 template <class E>
187 SourceLocation getExprLocImpl(const Expr *expr,
188 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000189 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000190 }
191}
192
193SourceLocation Expr::getExprLoc() const {
194 switch (getStmtClass()) {
195 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
196#define ABSTRACT_STMT(type)
197#define STMT(type, base) \
Richard Smitha0cbfc92014-07-26 00:47:13 +0000198 case Stmt::type##Class: break;
John McCallbd066782011-02-09 08:16:59 +0000199#define EXPR(type, base) \
200 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
201#include "clang/AST/StmtNodes.inc"
202 }
Richard Smitha0cbfc92014-07-26 00:47:13 +0000203 llvm_unreachable("unknown expression kind");
John McCallbd066782011-02-09 08:16:59 +0000204}
205
Chris Lattner0eedafe2006-08-24 04:56:27 +0000206//===----------------------------------------------------------------------===//
207// Primary Expressions.
208//===----------------------------------------------------------------------===//
209
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210/// \brief Compute the type-, value-, and instantiation-dependence of a
211/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000212/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000213static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
214 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000215 bool &ValueDependent,
216 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000217 TypeDependent = false;
218 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000219 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
Richard Smithcfaa5a32014-10-17 02:46:42 +0000224 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
Richard Smithcfaa5a32014-10-17 02:46:42 +0000228
Douglas Gregored6c7442009-11-23 11:41:28 +0000229 // (TD) - an identifier that was declared with dependent type
230 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000231 if (T->isDependentType()) {
232 TypeDependent = true;
233 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000234 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000235 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000236 } else if (T->isInstantiationDependentType()) {
237 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000238 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239
Douglas Gregored6c7442009-11-23 11:41:28 +0000240 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000242 == DeclarationName::CXXConversionFunctionName) {
243 QualType T = D->getDeclName().getCXXNameType();
244 if (T->isDependentType()) {
245 TypeDependent = true;
246 ValueDependent = true;
247 InstantiationDependent = true;
248 return;
249 }
250
251 if (T->isInstantiationDependentType())
252 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000253 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000254
Douglas Gregored6c7442009-11-23 11:41:28 +0000255 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256 if (isa<NonTypeTemplateParmDecl>(D)) {
257 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000259 return;
260 }
261
Douglas Gregored6c7442009-11-23 11:41:28 +0000262 // (VD) - a constant with integral or enumeration type and is
263 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000264 // (VD) - a constant with literal type and is initialized with an
265 // expression that is value-dependent [C++11].
266 // (VD) - FIXME: Missing from the standard:
267 // - an entity with reference type and is initialized with an
268 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000269 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000270 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000271 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000272 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000273 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000274 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000275 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000276 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000277 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000278 InstantiationDependent = true;
279 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000280 }
281
Douglas Gregor0e4de762010-05-11 08:41:30 +0000282 // (VD) - FIXME: Missing from the standard:
283 // - a member function or a static data member of the current
284 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000285 if (Var->isStaticDataMember() &&
286 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000287 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 InstantiationDependent = true;
Richard Smith00f5d892013-11-14 22:40:45 +0000289 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290 if (TInfo->getType()->isIncompleteArrayType())
291 TypeDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000292 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000293
294 return;
295 }
296
Douglas Gregor0e4de762010-05-11 08:41:30 +0000297 // (VD) - FIXME: Missing from the standard:
298 // - a member function or a static data member of the current
299 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000300 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000302 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000303 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000304}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000305
Craig Topperce7167c2013-08-22 04:58:56 +0000306void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000307 bool TypeDependent = false;
308 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000309 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000310 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
311 ValueDependent, InstantiationDependent);
Richard Smithcfaa5a32014-10-17 02:46:42 +0000312
313 ExprBits.TypeDependent |= TypeDependent;
314 ExprBits.ValueDependent |= ValueDependent;
315 ExprBits.InstantiationDependent |= InstantiationDependent;
316
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000317 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000318 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000319 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000320}
321
Craig Topperce7167c2013-08-22 04:58:56 +0000322DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000323 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000324 SourceLocation TemplateKWLoc,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000325 ValueDecl *D, bool RefersToEnclosingVariableOrCapture,
John McCall113bee02012-03-10 09:33:50 +0000326 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000327 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000328 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000329 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000330 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000331 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
332 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Richard Smithcfaa5a32014-10-17 02:46:42 +0000333 if (QualifierLoc) {
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000334 getInternalQualifierLoc() = QualifierLoc;
Richard Smithcfaa5a32014-10-17 02:46:42 +0000335 auto *NNS = QualifierLoc.getNestedNameSpecifier();
336 if (NNS->isInstantiationDependent())
337 ExprBits.InstantiationDependent = true;
338 if (NNS->containsUnexpandedParameterPack())
339 ExprBits.ContainsUnexpandedParameterPack = true;
340 }
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000341 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
342 if (FoundD)
343 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000344 DeclRefExprBits.HasTemplateKWAndArgsInfo
345 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000346 DeclRefExprBits.RefersToEnclosingVariableOrCapture =
347 RefersToEnclosingVariableOrCapture;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000348 if (TemplateArgs) {
349 bool Dependent = false;
350 bool InstantiationDependent = false;
351 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000352 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
353 Dependent,
354 InstantiationDependent,
355 ContainsUnexpandedParameterPack);
Richard Smithcfaa5a32014-10-17 02:46:42 +0000356 assert(!Dependent && "built a DeclRefExpr with dependent template args");
357 ExprBits.InstantiationDependent |= InstantiationDependent;
358 ExprBits.ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000361 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000362 DeclRefExprBits.HadMultipleCandidates = 0;
363
Daniel Dunbar9d355812012-03-09 01:51:51 +0000364 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000365}
366
Craig Topperce7167c2013-08-22 04:58:56 +0000367DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000368 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000370 ValueDecl *D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000371 bool RefersToEnclosingVariableOrCapture,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000372 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000373 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000374 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000375 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000376 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000378 RefersToEnclosingVariableOrCapture,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000379 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000380 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000381}
382
Craig Topperce7167c2013-08-22 04:58:56 +0000383DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000384 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000386 ValueDecl *D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000387 bool RefersToEnclosingVariableOrCapture,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000388 const DeclarationNameInfo &NameInfo,
389 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000390 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000391 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000392 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000393 // Filter out cases where the found Decl is the same as the value refenenced.
394 if (D == FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +0000395 FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000396
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000397 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000398 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000399 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000400 if (FoundD)
401 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000402 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000403 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
404 else if (TemplateKWLoc.isValid())
405 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000406
Chris Lattner5c0b4052010-10-30 05:14:06 +0000407 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000408 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev19acc3d2015-01-12 10:17:46 +0000409 RefersToEnclosingVariableOrCapture,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000410 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000411}
412
Craig Topperce7167c2013-08-22 04:58:56 +0000413DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000414 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000415 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000416 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000417 unsigned NumTemplateArgs) {
418 std::size_t Size = sizeof(DeclRefExpr);
419 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000420 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000421 if (HasFoundDecl)
422 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000423 if (HasTemplateKWAndArgsInfo)
424 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000425
Chris Lattner5c0b4052010-10-30 05:14:06 +0000426 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000427 return new (Mem) DeclRefExpr(EmptyShell());
428}
429
Daniel Dunbarb507f272012-03-09 15:39:15 +0000430SourceLocation DeclRefExpr::getLocStart() const {
431 if (hasQualifier())
432 return getQualifierLoc().getBeginLoc();
433 return getNameInfo().getLocStart();
434}
435SourceLocation DeclRefExpr::getLocEnd() const {
436 if (hasExplicitTemplateArgs())
437 return getRAngleLoc();
438 return getNameInfo().getLocEnd();
439}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000440
Alexey Bataevec474782014-10-09 08:45:04 +0000441PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
442 StringLiteral *SL)
443 : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
444 FNTy->isDependentType(), FNTy->isDependentType(),
445 FNTy->isInstantiationDependentType(),
446 /*ContainsUnexpandedParameterPack=*/false),
447 Loc(L), Type(IT), FnName(SL) {}
448
449StringLiteral *PredefinedExpr::getFunctionName() {
Alexey Bataev769562a2014-10-10 18:58:13 +0000450 return cast_or_null<StringLiteral>(FnName);
Alexey Bataevec474782014-10-09 08:45:04 +0000451}
452
453StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
454 switch (IT) {
455 case Func:
456 return "__func__";
457 case Function:
458 return "__FUNCTION__";
459 case FuncDName:
460 return "__FUNCDNAME__";
461 case LFunction:
462 return "L__FUNCTION__";
463 case PrettyFunction:
464 return "__PRETTY_FUNCTION__";
465 case FuncSig:
466 return "__FUNCSIG__";
467 case PrettyFunctionNoVirtual:
468 break;
469 }
470 llvm_unreachable("Unknown ident type for PredefinedExpr");
471}
472
Anders Carlsson2fb08242009-09-08 18:24:21 +0000473// FIXME: Maybe this should use DeclPrinter with a special "print predefined
474// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000475std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
476 ASTContext &Context = CurrentDecl->getASTContext();
477
David Majnemerbed356a2013-11-06 23:31:56 +0000478 if (IT == PredefinedExpr::FuncDName) {
479 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000480 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000481 MC.reset(Context.createMangleContext());
482
483 if (MC->shouldMangleDeclName(ND)) {
484 SmallString<256> Buffer;
485 llvm::raw_svector_ostream Out(Buffer);
486 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
487 MC->mangleCXXCtor(CD, Ctor_Base, Out);
488 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
489 MC->mangleCXXDtor(DD, Dtor_Base, Out);
490 else
491 MC->mangleName(ND, Out);
492
493 Out.flush();
494 if (!Buffer.empty() && Buffer.front() == '\01')
495 return Buffer.substr(1);
496 return Buffer.str();
497 } else
498 return ND->getIdentifier()->getName();
499 }
500 return "";
501 }
Alexey Bataevec474782014-10-09 08:45:04 +0000502 if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
503 std::unique_ptr<MangleContext> MC;
504 MC.reset(Context.createMangleContext());
505 SmallString<256> Buffer;
506 llvm::raw_svector_ostream Out(Buffer);
507 auto DC = CurrentDecl->getDeclContext();
508 if (DC->isFileContext())
509 MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
510 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
511 MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
512 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
513 MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
514 else
515 MC->mangleBlock(DC, BD, Out);
516 return Out.str();
517 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000518 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Reid Kleckner52eddda2014-04-08 18:13:24 +0000519 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000520 return FD->getNameAsString();
521
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000522 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000523 llvm::raw_svector_ostream Out(Name);
524
525 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000526 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000527 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000528 if (MD->isStatic())
529 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000530 }
531
David Blaikiebbafb8a2012-03-11 07:00:24 +0000532 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000533 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000534 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000535
Douglas Gregor11a434a2012-04-10 20:14:15 +0000536 const FunctionDecl *Decl = FD;
537 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
538 Decl = Pattern;
539 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Craig Topper36250ad2014-05-12 05:36:57 +0000540 const FunctionProtoType *FT = nullptr;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000541 if (FD->hasWrittenPrototype())
542 FT = dyn_cast<FunctionProtoType>(AFT);
543
Reid Kleckner52eddda2014-04-08 18:13:24 +0000544 if (IT == FuncSig) {
545 switch (FT->getCallConv()) {
546 case CC_C: POut << "__cdecl "; break;
547 case CC_X86StdCall: POut << "__stdcall "; break;
548 case CC_X86FastCall: POut << "__fastcall "; break;
549 case CC_X86ThisCall: POut << "__thiscall "; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +0000550 case CC_X86VectorCall: POut << "__vectorcall "; break;
Reid Kleckner52eddda2014-04-08 18:13:24 +0000551 // Only bother printing the conventions that MSVC knows about.
552 default: break;
553 }
554 }
555
556 FD->printQualifiedName(POut, Policy);
557
Douglas Gregor11a434a2012-04-10 20:14:15 +0000558 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000559 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000560 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000561 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000562 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000563 }
564
565 if (FT->isVariadic()) {
566 if (FD->getNumParams()) POut << ", ";
567 POut << "...";
568 }
569 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000570 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000571
Sam Weinig4e83bd22009-12-27 01:38:20 +0000572 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000573 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000574 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000575 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000576 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000577 POut << " volatile";
578 RefQualifierKind Ref = MD->getRefQualifier();
579 if (Ref == RQ_LValue)
580 POut << " &";
581 else if (Ref == RQ_RValue)
582 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000583 }
584
Douglas Gregor11a434a2012-04-10 20:14:15 +0000585 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
586 SpecsTy Specs;
587 const DeclContext *Ctx = FD->getDeclContext();
588 while (Ctx && isa<NamedDecl>(Ctx)) {
589 const ClassTemplateSpecializationDecl *Spec
590 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
591 if (Spec && !Spec->isExplicitSpecialization())
592 Specs.push_back(Spec);
593 Ctx = Ctx->getParent();
594 }
595
596 std::string TemplateParams;
597 llvm::raw_string_ostream TOut(TemplateParams);
598 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
599 I != E; ++I) {
600 const TemplateParameterList *Params
601 = (*I)->getSpecializedTemplate()->getTemplateParameters();
602 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
603 assert(Params->size() == Args.size());
604 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
605 StringRef Param = Params->getParam(i)->getName();
606 if (Param.empty()) continue;
607 TOut << Param << " = ";
608 Args.get(i).print(Policy, TOut);
609 TOut << ", ";
610 }
611 }
612
613 FunctionTemplateSpecializationInfo *FSI
614 = FD->getTemplateSpecializationInfo();
615 if (FSI && !FSI->isExplicitSpecialization()) {
616 const TemplateParameterList* Params
617 = FSI->getTemplate()->getTemplateParameters();
618 const TemplateArgumentList* Args = FSI->TemplateArguments;
619 assert(Params->size() == Args->size());
620 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
621 StringRef Param = Params->getParam(i)->getName();
622 if (Param.empty()) continue;
623 TOut << Param << " = ";
624 Args->get(i).print(Policy, TOut);
625 TOut << ", ";
626 }
627 }
628
629 TOut.flush();
630 if (!TemplateParams.empty()) {
631 // remove the trailing comma and space
632 TemplateParams.resize(TemplateParams.size() - 2);
633 POut << " [" << TemplateParams << "]";
634 }
635
636 POut.flush();
637
Benjamin Kramer90f54222013-08-21 11:45:27 +0000638 // Print "auto" for all deduced return types. This includes C++1y return
639 // type deduction and lambdas. For trailing return types resolve the
640 // decltype expression. Otherwise print the real type when this is
641 // not a constructor or destructor.
Alexey Bataevec474782014-10-09 08:45:04 +0000642 if (isa<CXXMethodDecl>(FD) &&
643 cast<CXXMethodDecl>(FD)->getParent()->isLambda())
Benjamin Kramer90f54222013-08-21 11:45:27 +0000644 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000645 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
646 FT->getReturnType()
647 ->getAs<DecltypeType>()
648 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000649 .getAsStringInternal(Proto, Policy);
650 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000651 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000652
653 Out << Proto;
654
655 Out.flush();
656 return Name.str().str();
657 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000658 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
659 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
660 // Skip to its enclosing function or method, but not its enclosing
661 // CapturedDecl.
662 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
663 const Decl *D = Decl::castFromDeclContext(DC);
664 return ComputeName(IT, D);
665 }
666 llvm_unreachable("CapturedDecl not inside a function or method");
667 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000668 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000669 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000670 llvm::raw_svector_ostream Out(Name);
671 Out << (MD->isInstanceMethod() ? '-' : '+');
672 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000673
674 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
675 // a null check to avoid a crash.
676 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000677 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000678
Anders Carlsson2fb08242009-09-08 18:24:21 +0000679 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000680 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000681 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000682
Anders Carlsson2fb08242009-09-08 18:24:21 +0000683 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000684 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000685 Out << ']';
686
687 Out.flush();
688 return Name.str().str();
689 }
690 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
691 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
692 return "top level";
693 }
694 return "";
695}
696
Craig Topper37932912013-08-18 10:09:15 +0000697void APNumericStorage::setIntValue(const ASTContext &C,
698 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000699 if (hasAllocation())
700 C.Deallocate(pVal);
701
702 BitWidth = Val.getBitWidth();
703 unsigned NumWords = Val.getNumWords();
704 const uint64_t* Words = Val.getRawData();
705 if (NumWords > 1) {
706 pVal = new (C) uint64_t[NumWords];
707 std::copy(Words, Words + NumWords, pVal);
708 } else if (NumWords == 1)
709 VAL = Words[0];
710 else
711 VAL = 0;
712}
713
Craig Topper37932912013-08-18 10:09:15 +0000714IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000715 QualType type, SourceLocation l)
716 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
717 false, false),
718 Loc(l) {
719 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
720 assert(V.getBitWidth() == C.getIntWidth(type) &&
721 "Integer type is not the correct size for constant.");
722 setValue(C, V);
723}
724
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000725IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000726IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000727 QualType type, SourceLocation l) {
728 return new (C) IntegerLiteral(C, V, type, l);
729}
730
731IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000732IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000733 return new (C) IntegerLiteral(Empty);
734}
735
Craig Topper37932912013-08-18 10:09:15 +0000736FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000737 bool isexact, QualType Type, SourceLocation L)
738 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
739 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000740 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000741 FloatingLiteralBits.IsExact = isexact;
742 setValue(C, V);
743}
744
Craig Topper37932912013-08-18 10:09:15 +0000745FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000746 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000747 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000748 FloatingLiteralBits.IsExact = false;
749}
750
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000751FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000752FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000753 bool isexact, QualType Type, SourceLocation L) {
754 return new (C) FloatingLiteral(C, V, isexact, Type, L);
755}
756
757FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000758FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000759 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000760}
761
Tim Northover178723a2013-01-22 09:46:51 +0000762const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
763 switch(FloatingLiteralBits.Semantics) {
764 case IEEEhalf:
765 return llvm::APFloat::IEEEhalf;
766 case IEEEsingle:
767 return llvm::APFloat::IEEEsingle;
768 case IEEEdouble:
769 return llvm::APFloat::IEEEdouble;
770 case x87DoubleExtended:
771 return llvm::APFloat::x87DoubleExtended;
772 case IEEEquad:
773 return llvm::APFloat::IEEEquad;
774 case PPCDoubleDouble:
775 return llvm::APFloat::PPCDoubleDouble;
776 }
777 llvm_unreachable("Unrecognised floating semantics");
778}
779
780void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
781 if (&Sem == &llvm::APFloat::IEEEhalf)
782 FloatingLiteralBits.Semantics = IEEEhalf;
783 else if (&Sem == &llvm::APFloat::IEEEsingle)
784 FloatingLiteralBits.Semantics = IEEEsingle;
785 else if (&Sem == &llvm::APFloat::IEEEdouble)
786 FloatingLiteralBits.Semantics = IEEEdouble;
787 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
788 FloatingLiteralBits.Semantics = x87DoubleExtended;
789 else if (&Sem == &llvm::APFloat::IEEEquad)
790 FloatingLiteralBits.Semantics = IEEEquad;
791 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
792 FloatingLiteralBits.Semantics = PPCDoubleDouble;
793 else
794 llvm_unreachable("Unknown floating semantics");
795}
796
Chris Lattnera0173132008-06-07 22:13:43 +0000797/// getValueAsApproximateDouble - This returns the value as an inaccurate
798/// double. Note that this may cause loss of precision, but is useful for
799/// debugging dumps, etc.
800double FloatingLiteral::getValueAsApproximateDouble() const {
801 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000802 bool ignored;
803 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
804 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000805 return V.convertToDouble();
806}
807
Nick Lewycky4ed84042012-02-24 09:07:53 +0000808int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000809 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000810 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000811 case Ascii:
812 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000813 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000814 break;
815 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000816 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000817 break;
818 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000819 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000820 break;
821 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000822 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000823 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000824 }
825 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
826 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000827 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000828 && "character byte widths supported are 1, 2, and 4 only");
829 return CharByteWidth;
830}
831
Craig Topper37932912013-08-18 10:09:15 +0000832StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000833 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000834 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000835 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000836 assert(C.getAsConstantArrayType(Ty) &&
837 "StringLiteral must be of constant array type!");
838
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000839 // Allocate enough space for the StringLiteral plus an array of locations for
840 // any concatenated string tokens.
841 void *Mem = C.Allocate(sizeof(StringLiteral)+
842 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000843 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000844 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000845
Steve Naroffdf7855b2007-02-21 23:46:25 +0000846 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000847 SL->setString(C,Str,Kind,Pascal);
848
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000849 SL->TokLocs[0] = Loc[0];
850 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000851
Chris Lattner630970d2009-02-18 05:49:11 +0000852 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000853 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
854 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000855}
856
Craig Topper37932912013-08-18 10:09:15 +0000857StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
858 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000859 void *Mem = C.Allocate(sizeof(StringLiteral)+
860 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000861 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000862 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000863 SL->CharByteWidth = 0;
864 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000865 SL->NumConcatenated = NumStrs;
866 return SL;
867}
868
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000869void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000870 switch (getKind()) {
871 case Ascii: break; // no prefix.
872 case Wide: OS << 'L'; break;
873 case UTF8: OS << "u8"; break;
874 case UTF16: OS << 'u'; break;
875 case UTF32: OS << 'U'; break;
876 }
877 OS << '"';
878 static const char Hex[] = "0123456789ABCDEF";
879
880 unsigned LastSlashX = getLength();
881 for (unsigned I = 0, N = getLength(); I != N; ++I) {
882 switch (uint32_t Char = getCodeUnit(I)) {
883 default:
884 // FIXME: Convert UTF-8 back to codepoints before rendering.
885
886 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
887 // Leave invalid surrogates alone; we'll use \x for those.
888 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
889 Char <= 0xdbff) {
890 uint32_t Trail = getCodeUnit(I + 1);
891 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
892 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
893 ++I;
894 }
895 }
896
897 if (Char > 0xff) {
898 // If this is a wide string, output characters over 0xff using \x
899 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
900 // codepoint: use \x escapes for invalid codepoints.
901 if (getKind() == Wide ||
902 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
903 // FIXME: Is this the best way to print wchar_t?
904 OS << "\\x";
905 int Shift = 28;
906 while ((Char >> Shift) == 0)
907 Shift -= 4;
908 for (/**/; Shift >= 0; Shift -= 4)
909 OS << Hex[(Char >> Shift) & 15];
910 LastSlashX = I;
911 break;
912 }
913
914 if (Char > 0xffff)
915 OS << "\\U00"
916 << Hex[(Char >> 20) & 15]
917 << Hex[(Char >> 16) & 15];
918 else
919 OS << "\\u";
920 OS << Hex[(Char >> 12) & 15]
921 << Hex[(Char >> 8) & 15]
922 << Hex[(Char >> 4) & 15]
923 << Hex[(Char >> 0) & 15];
924 break;
925 }
926
927 // If we used \x... for the previous character, and this character is a
928 // hexadecimal digit, prevent it being slurped as part of the \x.
929 if (LastSlashX + 1 == I) {
930 switch (Char) {
931 case '0': case '1': case '2': case '3': case '4':
932 case '5': case '6': case '7': case '8': case '9':
933 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
934 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
935 OS << "\"\"";
936 }
937 }
938
939 assert(Char <= 0xff &&
940 "Characters above 0xff should already have been handled.");
941
Jordan Rosea7d03842013-02-08 22:30:41 +0000942 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000943 OS << (char)Char;
944 else // Output anything hard as an octal escape.
945 OS << '\\'
946 << (char)('0' + ((Char >> 6) & 7))
947 << (char)('0' + ((Char >> 3) & 7))
948 << (char)('0' + ((Char >> 0) & 7));
949 break;
950 // Handle some common non-printable cases to make dumps prettier.
951 case '\\': OS << "\\\\"; break;
952 case '"': OS << "\\\""; break;
953 case '\n': OS << "\\n"; break;
954 case '\t': OS << "\\t"; break;
955 case '\a': OS << "\\a"; break;
956 case '\b': OS << "\\b"; break;
957 }
958 }
959 OS << '"';
960}
961
Craig Topper37932912013-08-18 10:09:15 +0000962void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000963 StringKind Kind, bool IsPascal) {
964 //FIXME: we assume that the string data comes from a target that uses the same
965 // code unit size and endianess for the type of string.
966 this->Kind = Kind;
967 this->IsPascal = IsPascal;
968
Nick Lewycky4ed84042012-02-24 09:07:53 +0000969 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000970 assert((Str.size()%CharByteWidth == 0)
971 && "size of data must be multiple of CharByteWidth");
972 Length = Str.size()/CharByteWidth;
973
974 switch(CharByteWidth) {
975 case 1: {
976 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000977 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000978 StrData.asChar = AStrData;
979 break;
980 }
981 case 2: {
982 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000983 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000984 StrData.asUInt16 = AStrData;
985 break;
986 }
987 case 4: {
988 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000989 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000990 StrData.asUInt32 = AStrData;
991 break;
992 }
993 default:
994 assert(false && "unsupported CharByteWidth");
995 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000996}
997
Chris Lattnere925d612010-11-17 07:37:15 +0000998/// getLocationOfByte - Return a source location that points to the specified
999/// byte of this string literal.
1000///
1001/// Strings are amazingly complex. They can be formed from multiple tokens and
1002/// can have escape sequences in them in addition to the usual trigraph and
1003/// escaped newline business. This routine handles this complexity.
1004///
1005SourceLocation StringLiteral::
1006getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1007 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +00001008 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1009 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +00001010
Chris Lattnere925d612010-11-17 07:37:15 +00001011 // Loop over all of the tokens in this string until we find the one that
1012 // contains the byte we're looking for.
1013 unsigned TokNo = 0;
1014 while (1) {
1015 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1016 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1017
1018 // Get the spelling of the string so that we can get the data that makes up
1019 // the string literal, not the identifier for the macro it is potentially
1020 // expanded through.
1021 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1022
1023 // Re-lex the token to get its length and original spelling.
1024 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1025 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001026 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001027 if (Invalid)
1028 return StrTokSpellingLoc;
1029
1030 const char *StrData = Buffer.data()+LocInfo.second;
1031
Chris Lattnere925d612010-11-17 07:37:15 +00001032 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001033 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1034 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001035 Token TheTok;
1036 TheLexer.LexFromRawLexer(TheTok);
1037
1038 // Use the StringLiteralParser to compute the length of the string in bytes.
Craig Topper9d5583e2014-06-26 04:58:39 +00001039 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattnere925d612010-11-17 07:37:15 +00001040 unsigned TokNumBytes = SLP.GetStringLength();
1041
1042 // If the byte is in this token, return the location of the byte.
1043 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001044 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001045 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1046
1047 // Now that we know the offset of the token in the spelling, use the
1048 // preprocessor to get the offset in the original source.
1049 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1050 }
1051
1052 // Move to the next string token.
1053 ++TokNo;
1054 ByteNo -= TokNumBytes;
1055 }
1056}
1057
1058
1059
Chris Lattner1b926492006-08-23 06:42:10 +00001060/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1061/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001062StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001063 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001064 case UO_PostInc: return "++";
1065 case UO_PostDec: return "--";
1066 case UO_PreInc: return "++";
1067 case UO_PreDec: return "--";
1068 case UO_AddrOf: return "&";
1069 case UO_Deref: return "*";
1070 case UO_Plus: return "+";
1071 case UO_Minus: return "-";
1072 case UO_Not: return "~";
1073 case UO_LNot: return "!";
1074 case UO_Real: return "__real";
1075 case UO_Imag: return "__imag";
1076 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001077 }
David Blaikief47fa302012-01-17 02:30:50 +00001078 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001079}
1080
John McCalle3027922010-08-25 11:45:40 +00001081UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001082UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1083 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001084 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001085 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1086 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1087 case OO_Amp: return UO_AddrOf;
1088 case OO_Star: return UO_Deref;
1089 case OO_Plus: return UO_Plus;
1090 case OO_Minus: return UO_Minus;
1091 case OO_Tilde: return UO_Not;
1092 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001093 }
1094}
1095
1096OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1097 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001098 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1099 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1100 case UO_AddrOf: return OO_Amp;
1101 case UO_Deref: return OO_Star;
1102 case UO_Plus: return OO_Plus;
1103 case UO_Minus: return OO_Minus;
1104 case UO_Not: return OO_Tilde;
1105 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001106 default: return OO_None;
1107 }
1108}
1109
1110
Chris Lattner0eedafe2006-08-24 04:56:27 +00001111//===----------------------------------------------------------------------===//
1112// Postfix Operators.
1113//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001114
Craig Topper37932912013-08-18 10:09:15 +00001115CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1116 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1117 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001118 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001119 fn->isTypeDependent(),
1120 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001121 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001122 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001123 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001124
Benjamin Kramerc215e762012-08-24 11:54:20 +00001125 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001126 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001127 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001128 if (args[i]->isTypeDependent())
1129 ExprBits.TypeDependent = true;
1130 if (args[i]->isValueDependent())
1131 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001132 if (args[i]->isInstantiationDependent())
1133 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001134 if (args[i]->containsUnexpandedParameterPack())
1135 ExprBits.ContainsUnexpandedParameterPack = true;
1136
Peter Collingbourne3a347252011-02-08 21:18:02 +00001137 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001138 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001139
Peter Collingbourne3a347252011-02-08 21:18:02 +00001140 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001141 RParenLoc = rparenloc;
1142}
Nate Begeman1e36a852008-01-17 17:46:27 +00001143
Craig Topper37932912013-08-18 10:09:15 +00001144CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001145 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1146 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001147 fn->isTypeDependent(),
1148 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001149 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001150 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001151 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001152
Benjamin Kramerc215e762012-08-24 11:54:20 +00001153 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001154 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001155 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 if (args[i]->isTypeDependent())
1157 ExprBits.TypeDependent = true;
1158 if (args[i]->isValueDependent())
1159 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001160 if (args[i]->isInstantiationDependent())
1161 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001162 if (args[i]->containsUnexpandedParameterPack())
1163 ExprBits.ContainsUnexpandedParameterPack = true;
1164
Peter Collingbourne3a347252011-02-08 21:18:02 +00001165 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001166 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001167
Peter Collingbourne3a347252011-02-08 21:18:02 +00001168 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001169 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001170}
1171
Craig Topper37932912013-08-18 10:09:15 +00001172CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001173 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001174 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001175 SubExprs = new (C) Stmt*[PREARGS_START];
1176 CallExprBits.NumPreArgs = 0;
1177}
1178
Craig Topper37932912013-08-18 10:09:15 +00001179CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001180 EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001181 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbourne3a347252011-02-08 21:18:02 +00001182 // FIXME: Why do we allocate this?
1183 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1184 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001185}
1186
Nuno Lopes518e3702009-12-20 23:11:08 +00001187Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001188 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001189
1190 while (SubstNonTypeTemplateParmExpr *NTTP
1191 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1192 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1193 }
1194
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001195 // If we're calling a dereference, look at the pointer instead.
1196 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1197 if (BO->isPtrMemOp())
1198 CEE = BO->getRHS()->IgnoreParenCasts();
1199 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1200 if (UO->getOpcode() == UO_Deref)
1201 CEE = UO->getSubExpr()->IgnoreParenCasts();
1202 }
Chris Lattner52301912009-07-17 15:46:27 +00001203 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001204 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001205 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1206 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001207
Craig Topper36250ad2014-05-12 05:36:57 +00001208 return nullptr;
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001209}
1210
Nuno Lopes518e3702009-12-20 23:11:08 +00001211FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001212 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001213}
1214
Chris Lattnere4407ed2007-12-28 05:25:02 +00001215/// setNumArgs - This changes the number of arguments present in this call.
1216/// Any orphaned expressions are deleted by this, and any new operands are set
1217/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001218void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001219 // No change, just return.
1220 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001221
Chris Lattnere4407ed2007-12-28 05:25:02 +00001222 // If shrinking # arguments, just delete the extras and forgot them.
1223 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001224 this->NumArgs = NumArgs;
1225 return;
1226 }
1227
1228 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001229 unsigned NumPreArgs = getNumPreArgs();
1230 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001231 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001232 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001233 NewSubExprs[i] = SubExprs[i];
1234 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001235 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1236 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001237 NewSubExprs[i] = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregorba6e5572009-04-17 21:46:47 +00001239 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001240 SubExprs = NewSubExprs;
1241 this->NumArgs = NumArgs;
1242}
1243
Alp Tokera724cff2013-12-28 21:59:02 +00001244/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001245/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001246unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001247 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001248 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001249 // ImplicitCastExpr.
1250 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1251 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001252 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001253
Steve Narofff6e3b3292008-01-31 01:07:12 +00001254 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1255 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001256 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001258 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1259 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001260 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001261
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001262 if (!FDecl->getIdentifier())
1263 return 0;
1264
Douglas Gregor15fc9562009-09-12 00:22:50 +00001265 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001266}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001267
Richard Smith5011a002013-01-17 23:46:04 +00001268bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001269 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001270 return Ctx.BuiltinInfo.isUnevaluated(BI);
1271 return false;
1272}
1273
David Majnemerced8bdf2015-02-25 17:36:15 +00001274QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const {
1275 const Expr *Callee = getCallee();
1276 QualType CalleeType = Callee->getType();
1277 if (const auto *FnTypePtr = CalleeType->getAs<PointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001278 CalleeType = FnTypePtr->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001279 } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) {
Anders Carlsson00a27592009-05-26 04:57:27 +00001280 CalleeType = BPT->getPointeeType();
David Majnemerced8bdf2015-02-25 17:36:15 +00001281 } else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
1282 if (isa<CXXPseudoDestructorExpr>(Callee->IgnoreParens()))
1283 return Ctx.VoidTy;
1284
John McCall0009fcc2011-04-26 20:42:42 +00001285 // This should never be overloaded and so should never return null.
David Majnemerced8bdf2015-02-25 17:36:15 +00001286 CalleeType = Expr::findBoundMemberType(Callee);
1287 }
1288
John McCall0009fcc2011-04-26 20:42:42 +00001289 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001290 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001291}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001292
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001293SourceLocation CallExpr::getLocStart() const {
1294 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001295 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001296
1297 SourceLocation begin = getCallee()->getLocStart();
Keno Fischer070db172014-08-15 01:39:12 +00001298 if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001299 begin = getArg(0)->getLocStart();
1300 return begin;
1301}
1302SourceLocation CallExpr::getLocEnd() const {
1303 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001304 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001305
1306 SourceLocation end = getRParenLoc();
Keno Fischer070db172014-08-15 01:39:12 +00001307 if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001308 end = getArg(getNumArgs() - 1)->getLocEnd();
1309 return end;
1310}
John McCall701417a2011-02-21 06:23:05 +00001311
Craig Topper37932912013-08-18 10:09:15 +00001312OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001313 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001314 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001315 ArrayRef<OffsetOfNode> comps,
1316 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001317 SourceLocation RParenLoc) {
1318 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001319 sizeof(OffsetOfNode) * comps.size() +
1320 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001321
Benjamin Kramerc215e762012-08-24 11:54:20 +00001322 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1323 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001324}
1325
Craig Topper37932912013-08-18 10:09:15 +00001326OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001327 unsigned numComps, unsigned numExprs) {
1328 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1329 sizeof(OffsetOfNode) * numComps +
1330 sizeof(Expr*) * numExprs);
1331 return new (Mem) OffsetOfExpr(numComps, numExprs);
1332}
1333
Craig Topper37932912013-08-18 10:09:15 +00001334OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001335 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001336 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001337 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001338 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1339 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001340 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001341 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001342 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001343 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001344 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001345{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001346 for (unsigned i = 0; i != comps.size(); ++i) {
1347 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001348 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001349
Benjamin Kramerc215e762012-08-24 11:54:20 +00001350 for (unsigned i = 0; i != exprs.size(); ++i) {
1351 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001352 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001353 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001354 ExprBits.ContainsUnexpandedParameterPack = true;
1355
Benjamin Kramerc215e762012-08-24 11:54:20 +00001356 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001357 }
1358}
1359
1360IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1361 assert(getKind() == Field || getKind() == Identifier);
1362 if (getKind() == Field)
1363 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001364
Douglas Gregor882211c2010-04-28 22:16:22 +00001365 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1366}
1367
David Majnemer10fd83d2015-01-15 10:04:14 +00001368UnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr(
1369 UnaryExprOrTypeTrait ExprKind, Expr *E, QualType resultType,
1370 SourceLocation op, SourceLocation rp)
1371 : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary,
1372 false, // Never type-dependent (C++ [temp.dep.expr]p3).
1373 // Value-dependent if the argument is type-dependent.
1374 E->isTypeDependent(), E->isInstantiationDependent(),
1375 E->containsUnexpandedParameterPack()),
1376 OpLoc(op), RParenLoc(rp) {
1377 UnaryExprOrTypeTraitExprBits.Kind = ExprKind;
1378 UnaryExprOrTypeTraitExprBits.IsType = false;
1379 Argument.Ex = E;
1380
1381 // Check to see if we are in the situation where alignof(decl) should be
1382 // dependent because decl's alignment is dependent.
1383 if (ExprKind == UETT_AlignOf) {
1384 if (!isValueDependent() || !isInstantiationDependent()) {
1385 E = E->IgnoreParens();
1386
1387 const ValueDecl *D = nullptr;
1388 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
1389 D = DRE->getDecl();
1390 else if (const auto *ME = dyn_cast<MemberExpr>(E))
1391 D = ME->getMemberDecl();
1392
1393 if (D) {
1394 for (const auto *I : D->specific_attrs<AlignedAttr>()) {
1395 if (I->isAlignmentDependent()) {
1396 setValueDependent(true);
1397 setInstantiationDependent(true);
1398 break;
1399 }
1400 }
1401 }
1402 }
1403 }
1404}
1405
Craig Topper37932912013-08-18 10:09:15 +00001406MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001407 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001408 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001409 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001410 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001411 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001412 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001413 QualType ty,
1414 ExprValueKind vk,
1415 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001416 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001417
Douglas Gregorea972d32011-02-28 21:54:11 +00001418 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001419 founddecl.getDecl() != memberdecl ||
1420 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001421 if (hasQualOrFound)
1422 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001423
John McCall6b51f282009-11-23 01:53:49 +00001424 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001425 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1426 else if (TemplateKWLoc.isValid())
1427 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001428
Chris Lattner5c0b4052010-10-30 05:14:06 +00001429 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001430 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1431 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001432
1433 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001434 // FIXME: Wrong. We should be looking at the member declaration we found.
1435 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001436 E->setValueDependent(true);
1437 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001438 E->setInstantiationDependent(true);
1439 }
1440 else if (QualifierLoc &&
1441 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1442 E->setInstantiationDependent(true);
1443
John McCall16df1e52010-03-30 21:47:33 +00001444 E->HasQualifierOrFoundDecl = true;
1445
1446 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001447 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001448 NQ->FoundDecl = founddecl;
1449 }
1450
Abramo Bagnara7945c982012-01-27 09:46:47 +00001451 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1452
John McCall16df1e52010-03-30 21:47:33 +00001453 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001454 bool Dependent = false;
1455 bool InstantiationDependent = false;
1456 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001457 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1458 Dependent,
1459 InstantiationDependent,
1460 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001461 if (InstantiationDependent)
1462 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001463 } else if (TemplateKWLoc.isValid()) {
1464 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001465 }
1466
1467 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001468}
1469
Daniel Dunbarb507f272012-03-09 15:39:15 +00001470SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001471 if (isImplicitAccess()) {
1472 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001473 return getQualifierLoc().getBeginLoc();
1474 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001475 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001476
Daniel Dunbarb507f272012-03-09 15:39:15 +00001477 // FIXME: We don't want this to happen. Rather, we should be able to
1478 // detect all kinds of implicit accesses more cleanly.
1479 SourceLocation BaseStartLoc = getBase()->getLocStart();
1480 if (BaseStartLoc.isValid())
1481 return BaseStartLoc;
1482 return MemberLoc;
1483}
1484SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001485 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001486 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001487 EndLoc = getRAngleLoc();
1488 else if (EndLoc.isInvalid())
1489 EndLoc = getBase()->getLocEnd();
1490 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001491}
1492
Alp Tokerc1086762013-12-07 13:51:35 +00001493bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001494 switch (getCastKind()) {
1495 case CK_DerivedToBase:
1496 case CK_UncheckedDerivedToBase:
1497 case CK_DerivedToBaseMemberPointer:
1498 case CK_BaseToDerived:
1499 case CK_BaseToDerivedMemberPointer:
1500 assert(!path_empty() && "Cast kind should have a base path!");
1501 break;
1502
1503 case CK_CPointerToObjCPointerCast:
1504 assert(getType()->isObjCObjectPointerType());
1505 assert(getSubExpr()->getType()->isPointerType());
1506 goto CheckNoBasePath;
1507
1508 case CK_BlockPointerToObjCPointerCast:
1509 assert(getType()->isObjCObjectPointerType());
1510 assert(getSubExpr()->getType()->isBlockPointerType());
1511 goto CheckNoBasePath;
1512
John McCallc62bb392012-02-15 01:22:51 +00001513 case CK_ReinterpretMemberPointer:
1514 assert(getType()->isMemberPointerType());
1515 assert(getSubExpr()->getType()->isMemberPointerType());
1516 goto CheckNoBasePath;
1517
John McCall9320b872011-09-09 05:25:32 +00001518 case CK_BitCast:
1519 // Arbitrary casts to C pointer types count as bitcasts.
1520 // Otherwise, we should only have block and ObjC pointer casts
1521 // here if they stay within the type kind.
1522 if (!getType()->isPointerType()) {
1523 assert(getType()->isObjCObjectPointerType() ==
1524 getSubExpr()->getType()->isObjCObjectPointerType());
1525 assert(getType()->isBlockPointerType() ==
1526 getSubExpr()->getType()->isBlockPointerType());
1527 }
1528 goto CheckNoBasePath;
1529
1530 case CK_AnyPointerToBlockPointerCast:
1531 assert(getType()->isBlockPointerType());
1532 assert(getSubExpr()->getType()->isAnyPointerType() &&
1533 !getSubExpr()->getType()->isBlockPointerType());
1534 goto CheckNoBasePath;
1535
Douglas Gregored90df32012-02-22 05:02:47 +00001536 case CK_CopyAndAutoreleaseBlockObject:
1537 assert(getType()->isBlockPointerType());
1538 assert(getSubExpr()->getType()->isBlockPointerType());
1539 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001540
1541 case CK_FunctionToPointerDecay:
1542 assert(getType()->isPointerType());
1543 assert(getSubExpr()->getType()->isFunctionType());
1544 goto CheckNoBasePath;
1545
David Tweede1468322013-12-11 13:39:46 +00001546 case CK_AddressSpaceConversion:
1547 assert(getType()->isPointerType());
1548 assert(getSubExpr()->getType()->isPointerType());
1549 assert(getType()->getPointeeType().getAddressSpace() !=
1550 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001551 // These should not have an inheritance path.
1552 case CK_Dynamic:
1553 case CK_ToUnion:
1554 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001555 case CK_NullToMemberPointer:
1556 case CK_NullToPointer:
1557 case CK_ConstructorConversion:
1558 case CK_IntegralToPointer:
1559 case CK_PointerToIntegral:
1560 case CK_ToVoid:
1561 case CK_VectorSplat:
1562 case CK_IntegralCast:
1563 case CK_IntegralToFloating:
1564 case CK_FloatingToIntegral:
1565 case CK_FloatingCast:
1566 case CK_ObjCObjectLValueCast:
1567 case CK_FloatingRealToComplex:
1568 case CK_FloatingComplexToReal:
1569 case CK_FloatingComplexCast:
1570 case CK_FloatingComplexToIntegralComplex:
1571 case CK_IntegralRealToComplex:
1572 case CK_IntegralComplexToReal:
1573 case CK_IntegralComplexCast:
1574 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001575 case CK_ARCProduceObject:
1576 case CK_ARCConsumeObject:
1577 case CK_ARCReclaimReturnedObject:
1578 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001579 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001580 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1581 goto CheckNoBasePath;
1582
1583 case CK_Dependent:
1584 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001585 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001586 case CK_AtomicToNonAtomic:
1587 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001588 case CK_PointerToBoolean:
1589 case CK_IntegralToBoolean:
1590 case CK_FloatingToBoolean:
1591 case CK_MemberPointerToBoolean:
1592 case CK_FloatingComplexToBoolean:
1593 case CK_IntegralComplexToBoolean:
1594 case CK_LValueBitCast: // -> bool&
1595 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001596 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001597 CheckNoBasePath:
1598 assert(path_empty() && "Cast kind should not have a base path!");
1599 break;
1600 }
Alp Tokerc1086762013-12-07 13:51:35 +00001601 return true;
John McCall9320b872011-09-09 05:25:32 +00001602}
1603
Anders Carlsson496335e2009-09-03 00:59:21 +00001604const char *CastExpr::getCastKindName() const {
1605 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001606 case CK_Dependent:
1607 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001608 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001609 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001610 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001611 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001612 case CK_LValueToRValue:
1613 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001614 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001615 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001616 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001617 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001618 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001619 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001620 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001621 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001622 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001623 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001624 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001625 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001626 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001627 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001628 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001629 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001630 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001631 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001632 case CK_NullToPointer:
1633 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001634 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001635 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001636 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001637 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001638 case CK_ReinterpretMemberPointer:
1639 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001640 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001641 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001642 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001643 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001644 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001645 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001646 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001647 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001648 case CK_PointerToBoolean:
1649 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001650 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001651 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001652 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001653 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001654 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001655 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001656 case CK_IntegralToBoolean:
1657 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001658 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001659 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001660 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001661 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001662 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001663 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001664 case CK_FloatingToBoolean:
1665 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001666 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001667 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001668 case CK_CPointerToObjCPointerCast:
1669 return "CPointerToObjCPointerCast";
1670 case CK_BlockPointerToObjCPointerCast:
1671 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001672 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001673 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001674 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001675 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001676 case CK_FloatingRealToComplex:
1677 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001678 case CK_FloatingComplexToReal:
1679 return "FloatingComplexToReal";
1680 case CK_FloatingComplexToBoolean:
1681 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001682 case CK_FloatingComplexCast:
1683 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001684 case CK_FloatingComplexToIntegralComplex:
1685 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001686 case CK_IntegralRealToComplex:
1687 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001688 case CK_IntegralComplexToReal:
1689 return "IntegralComplexToReal";
1690 case CK_IntegralComplexToBoolean:
1691 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001692 case CK_IntegralComplexCast:
1693 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001694 case CK_IntegralComplexToFloatingComplex:
1695 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001696 case CK_ARCConsumeObject:
1697 return "ARCConsumeObject";
1698 case CK_ARCProduceObject:
1699 return "ARCProduceObject";
1700 case CK_ARCReclaimReturnedObject:
1701 return "ARCReclaimReturnedObject";
1702 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001703 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001704 case CK_AtomicToNonAtomic:
1705 return "AtomicToNonAtomic";
1706 case CK_NonAtomicToAtomic:
1707 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001708 case CK_CopyAndAutoreleaseBlockObject:
1709 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001710 case CK_BuiltinFnToFnPtr:
1711 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001712 case CK_ZeroToOCLEvent:
1713 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001714 case CK_AddressSpaceConversion:
1715 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001716 }
Mike Stump11289f42009-09-09 15:08:12 +00001717
John McCallc5e62b42010-11-13 09:02:35 +00001718 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001719}
1720
Douglas Gregord196a582009-12-14 19:27:10 +00001721Expr *CastExpr::getSubExprAsWritten() {
Craig Topper36250ad2014-05-12 05:36:57 +00001722 Expr *SubExpr = nullptr;
Douglas Gregord196a582009-12-14 19:27:10 +00001723 CastExpr *E = this;
1724 do {
1725 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001726
1727 // Skip through reference binding to temporary.
1728 if (MaterializeTemporaryExpr *Materialize
1729 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1730 SubExpr = Materialize->GetTemporaryExpr();
1731
Douglas Gregord196a582009-12-14 19:27:10 +00001732 // Skip any temporary bindings; they're implicit.
1733 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1734 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001735
Douglas Gregord196a582009-12-14 19:27:10 +00001736 // Conversions by constructor and conversion functions have a
1737 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001738 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001739 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001740 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001741 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001742
Douglas Gregord196a582009-12-14 19:27:10 +00001743 // If the subexpression we're left with is an implicit cast, look
1744 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001745 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1746
Douglas Gregord196a582009-12-14 19:27:10 +00001747 return SubExpr;
1748}
1749
John McCallcf142162010-08-07 06:22:56 +00001750CXXBaseSpecifier **CastExpr::path_buffer() {
1751 switch (getStmtClass()) {
1752#define ABSTRACT_STMT(x)
1753#define CASTEXPR(Type, Base) \
1754 case Stmt::Type##Class: \
1755 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1756#define STMT(Type, Base)
1757#include "clang/AST/StmtNodes.inc"
1758 default:
1759 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001760 }
1761}
1762
1763void CastExpr::setCastPath(const CXXCastPath &Path) {
1764 assert(Path.size() == path_size());
1765 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1766}
1767
Craig Topper37932912013-08-18 10:09:15 +00001768ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001769 CastKind Kind, Expr *Operand,
1770 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001771 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001772 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1773 void *Buffer =
1774 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1775 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001776 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001777 if (PathSize) E->setCastPath(*BasePath);
1778 return E;
1779}
1780
Craig Topper37932912013-08-18 10:09:15 +00001781ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001782 unsigned PathSize) {
1783 void *Buffer =
1784 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1785 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1786}
1787
1788
Craig Topper37932912013-08-18 10:09:15 +00001789CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001790 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001791 const CXXCastPath *BasePath,
1792 TypeSourceInfo *WrittenTy,
1793 SourceLocation L, SourceLocation R) {
1794 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1795 void *Buffer =
1796 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1797 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001798 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001799 if (PathSize) E->setCastPath(*BasePath);
1800 return E;
1801}
1802
Craig Topper37932912013-08-18 10:09:15 +00001803CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1804 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001805 void *Buffer =
1806 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1807 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1808}
1809
Chris Lattner1b926492006-08-23 06:42:10 +00001810/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1811/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001812StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001813 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001814 case BO_PtrMemD: return ".*";
1815 case BO_PtrMemI: return "->*";
1816 case BO_Mul: return "*";
1817 case BO_Div: return "/";
1818 case BO_Rem: return "%";
1819 case BO_Add: return "+";
1820 case BO_Sub: return "-";
1821 case BO_Shl: return "<<";
1822 case BO_Shr: return ">>";
1823 case BO_LT: return "<";
1824 case BO_GT: return ">";
1825 case BO_LE: return "<=";
1826 case BO_GE: return ">=";
1827 case BO_EQ: return "==";
1828 case BO_NE: return "!=";
1829 case BO_And: return "&";
1830 case BO_Xor: return "^";
1831 case BO_Or: return "|";
1832 case BO_LAnd: return "&&";
1833 case BO_LOr: return "||";
1834 case BO_Assign: return "=";
1835 case BO_MulAssign: return "*=";
1836 case BO_DivAssign: return "/=";
1837 case BO_RemAssign: return "%=";
1838 case BO_AddAssign: return "+=";
1839 case BO_SubAssign: return "-=";
1840 case BO_ShlAssign: return "<<=";
1841 case BO_ShrAssign: return ">>=";
1842 case BO_AndAssign: return "&=";
1843 case BO_XorAssign: return "^=";
1844 case BO_OrAssign: return "|=";
1845 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001846 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001847
David Blaikiee4d798f2012-01-20 21:50:17 +00001848 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001849}
Steve Naroff47500512007-04-19 23:00:49 +00001850
John McCalle3027922010-08-25 11:45:40 +00001851BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001852BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1853 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001854 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001855 case OO_Plus: return BO_Add;
1856 case OO_Minus: return BO_Sub;
1857 case OO_Star: return BO_Mul;
1858 case OO_Slash: return BO_Div;
1859 case OO_Percent: return BO_Rem;
1860 case OO_Caret: return BO_Xor;
1861 case OO_Amp: return BO_And;
1862 case OO_Pipe: return BO_Or;
1863 case OO_Equal: return BO_Assign;
1864 case OO_Less: return BO_LT;
1865 case OO_Greater: return BO_GT;
1866 case OO_PlusEqual: return BO_AddAssign;
1867 case OO_MinusEqual: return BO_SubAssign;
1868 case OO_StarEqual: return BO_MulAssign;
1869 case OO_SlashEqual: return BO_DivAssign;
1870 case OO_PercentEqual: return BO_RemAssign;
1871 case OO_CaretEqual: return BO_XorAssign;
1872 case OO_AmpEqual: return BO_AndAssign;
1873 case OO_PipeEqual: return BO_OrAssign;
1874 case OO_LessLess: return BO_Shl;
1875 case OO_GreaterGreater: return BO_Shr;
1876 case OO_LessLessEqual: return BO_ShlAssign;
1877 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1878 case OO_EqualEqual: return BO_EQ;
1879 case OO_ExclaimEqual: return BO_NE;
1880 case OO_LessEqual: return BO_LE;
1881 case OO_GreaterEqual: return BO_GE;
1882 case OO_AmpAmp: return BO_LAnd;
1883 case OO_PipePipe: return BO_LOr;
1884 case OO_Comma: return BO_Comma;
1885 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001886 }
1887}
1888
1889OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1890 static const OverloadedOperatorKind OverOps[] = {
1891 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1892 OO_Star, OO_Slash, OO_Percent,
1893 OO_Plus, OO_Minus,
1894 OO_LessLess, OO_GreaterGreater,
1895 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1896 OO_EqualEqual, OO_ExclaimEqual,
1897 OO_Amp,
1898 OO_Caret,
1899 OO_Pipe,
1900 OO_AmpAmp,
1901 OO_PipePipe,
1902 OO_Equal, OO_StarEqual,
1903 OO_SlashEqual, OO_PercentEqual,
1904 OO_PlusEqual, OO_MinusEqual,
1905 OO_LessLessEqual, OO_GreaterGreaterEqual,
1906 OO_AmpEqual, OO_CaretEqual,
1907 OO_PipeEqual,
1908 OO_Comma
1909 };
1910 return OverOps[Opc];
1911}
1912
Craig Topper37932912013-08-18 10:09:15 +00001913InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001914 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001915 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001916 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001917 InitExprs(C, initExprs.size()),
Craig Topper36250ad2014-05-12 05:36:57 +00001918 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001919{
1920 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001921 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001922 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001923 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001924 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001925 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001926 if (initExprs[I]->isInstantiationDependent())
1927 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001928 if (initExprs[I]->containsUnexpandedParameterPack())
1929 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001930 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001931
Benjamin Kramerc215e762012-08-24 11:54:20 +00001932 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001933}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001934
Craig Topper37932912013-08-18 10:09:15 +00001935void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001936 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001937 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001938}
1939
Craig Topper37932912013-08-18 10:09:15 +00001940void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Craig Topper36250ad2014-05-12 05:36:57 +00001941 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001942}
1943
Craig Topper37932912013-08-18 10:09:15 +00001944Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001945 if (Init >= InitExprs.size()) {
Craig Topper36250ad2014-05-12 05:36:57 +00001946 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Richard Smithc275da62013-12-06 01:27:24 +00001947 setInit(Init, expr);
Craig Topper36250ad2014-05-12 05:36:57 +00001948 return nullptr;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001949 }
Mike Stump11289f42009-09-09 15:08:12 +00001950
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001951 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001952 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001953 return Result;
1954}
1955
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001956void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001957 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001958 ArrayFillerOrUnionFieldInit = filler;
1959 // Fill out any "holes" in the array due to designated initializers.
1960 Expr **inits = getInits();
1961 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001962 if (inits[i] == nullptr)
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001963 inits[i] = filler;
1964}
1965
Richard Smith9ec1e482012-04-15 02:50:59 +00001966bool InitListExpr::isStringLiteralInit() const {
1967 if (getNumInits() != 1)
1968 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001969 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1970 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001971 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001972 // It is possible for getInit() to return null.
1973 const Expr *Init = getInit(0);
1974 if (!Init)
1975 return false;
1976 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001977 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1978}
1979
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001980SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001981 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001982 return SyntacticForm->getLocStart();
1983 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001984 if (Beg.isInvalid()) {
1985 // Find the first non-null initializer.
1986 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1987 E = InitExprs.end();
1988 I != E; ++I) {
1989 if (Stmt *S = *I) {
1990 Beg = S->getLocStart();
1991 break;
1992 }
1993 }
1994 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001995 return Beg;
1996}
1997
1998SourceLocation InitListExpr::getLocEnd() const {
1999 if (InitListExpr *SyntacticForm = getSyntacticForm())
2000 return SyntacticForm->getLocEnd();
2001 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00002002 if (End.isInvalid()) {
2003 // Find the first non-null initializer from the end.
2004 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00002005 E = InitExprs.rend();
2006 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00002007 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00002008 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00002009 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00002010 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00002011 }
2012 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00002013 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00002014}
2015
Steve Naroff991e99d2008-09-04 15:31:07 +00002016/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00002017///
John McCallc833dea2012-02-17 03:32:35 +00002018const FunctionProtoType *BlockExpr::getFunctionType() const {
2019 // The block pointer is never sugared, but the function type might be.
2020 return cast<BlockPointerType>(getType())
2021 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00002022}
2023
Mike Stump11289f42009-09-09 15:08:12 +00002024SourceLocation BlockExpr::getCaretLocation() const {
2025 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00002026}
Mike Stump11289f42009-09-09 15:08:12 +00002027const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00002028 return TheBlock->getBody();
2029}
Mike Stump11289f42009-09-09 15:08:12 +00002030Stmt *BlockExpr::getBody() {
2031 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00002032}
Steve Naroff415d3d52008-10-08 17:01:13 +00002033
2034
Chris Lattner1ec5f562007-06-27 05:38:08 +00002035//===----------------------------------------------------------------------===//
2036// Generic Expression Routines
2037//===----------------------------------------------------------------------===//
2038
Chris Lattner237f2752009-02-14 07:37:35 +00002039/// isUnusedResultAWarning - Return true if this immediate expression should
2040/// be warned about if the result is unused. If so, fill in Loc and Ranges
2041/// with location to warn on and the source range[s] to report with the
2042/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002043bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2044 SourceRange &R1, SourceRange &R2,
2045 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00002046 // Don't warn if the expr is type dependent. The type could end up
2047 // instantiating to void.
2048 if (isTypeDependent())
2049 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002050
Chris Lattner1ec5f562007-06-27 05:38:08 +00002051 switch (getStmtClass()) {
2052 default:
John McCallc493a732010-03-12 07:11:26 +00002053 if (getType()->isVoidType())
2054 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002055 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002056 Loc = getExprLoc();
2057 R1 = getSourceRange();
2058 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002059 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002060 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002061 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00002062 case GenericSelectionExprClass:
2063 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002064 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00002065 case ChooseExprClass:
2066 return cast<ChooseExpr>(this)->getChosenSubExpr()->
2067 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002068 case UnaryOperatorClass: {
2069 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00002070
Chris Lattner1ec5f562007-06-27 05:38:08 +00002071 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002072 case UO_Plus:
2073 case UO_Minus:
2074 case UO_AddrOf:
2075 case UO_Not:
2076 case UO_LNot:
2077 case UO_Deref:
2078 break;
John McCalle3027922010-08-25 11:45:40 +00002079 case UO_PostInc:
2080 case UO_PostDec:
2081 case UO_PreInc:
2082 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002083 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002084 case UO_Real:
2085 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002086 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002087 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2088 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002089 return false;
2090 break;
John McCalle3027922010-08-25 11:45:40 +00002091 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002092 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002093 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002094 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002095 Loc = UO->getOperatorLoc();
2096 R1 = UO->getSubExpr()->getSourceRange();
2097 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002098 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002099 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002100 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002101 switch (BO->getOpcode()) {
2102 default:
2103 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002104 // Consider the RHS of comma for side effects. LHS was checked by
2105 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002106 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002107 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2108 // lvalue-ness) of an assignment written in a macro.
2109 if (IntegerLiteral *IE =
2110 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2111 if (IE->getValue() == 0)
2112 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002113 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002114 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002115 case BO_LAnd:
2116 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002117 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2118 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002119 return false;
2120 break;
John McCall1e3715a2010-02-16 04:10:53 +00002121 }
Chris Lattner237f2752009-02-14 07:37:35 +00002122 if (BO->isAssignmentOp())
2123 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002124 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002125 Loc = BO->getOperatorLoc();
2126 R1 = BO->getLHS()->getSourceRange();
2127 R2 = BO->getRHS()->getSourceRange();
2128 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002129 }
Chris Lattner86928112007-08-25 02:00:02 +00002130 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002131 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002132 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002133 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002134
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002135 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002136 // If only one of the LHS or RHS is a warning, the operator might
2137 // be being used for control flow. Only warn if both the LHS and
2138 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002139 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002140 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002141 return false;
2142 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002143 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002144 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002145 }
2146
Chris Lattnera44d1162007-06-27 05:58:59 +00002147 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002148 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002149 Loc = cast<MemberExpr>(this)->getMemberLoc();
2150 R1 = SourceRange(Loc, Loc);
2151 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2152 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002153
Chris Lattner1ec5f562007-06-27 05:38:08 +00002154 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002155 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002156 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2157 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2158 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2159 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002160
Chandler Carruth46339472011-08-17 09:49:44 +00002161 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002162 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002163 // overloads as there is no reasonable way to define these such that they
2164 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002165 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002166 // provides additional value as well. If this list is updated,
2167 // DiagnoseUnusedComparison should be as well.
2168 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002169 switch (Op->getOperator()) {
2170 default:
2171 break;
2172 case OO_EqualEqual:
2173 case OO_ExclaimEqual:
2174 case OO_Less:
2175 case OO_Greater:
2176 case OO_GreaterEqual:
2177 case OO_LessEqual:
David Majnemerced8bdf2015-02-25 17:36:15 +00002178 if (Op->getCallReturnType(Ctx)->isReferenceType() ||
2179 Op->getCallReturnType(Ctx)->isVoidType())
Richard Trieu161132b2014-05-14 23:22:10 +00002180 break;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002181 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002182 Loc = Op->getOperatorLoc();
2183 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002184 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002185 }
Chandler Carruth46339472011-08-17 09:49:44 +00002186
2187 // Fallthrough for generic call handling.
2188 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002189 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002190 case CXXMemberCallExprClass:
2191 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002192 // If this is a direct call, get the callee.
2193 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002194 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002195 // If the callee has attribute pure, const, or warn_unused_result, warn
2196 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002197 //
2198 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2199 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002200 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2201 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002202 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002203 Loc = CE->getCallee()->getLocStart();
2204 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002205
Chris Lattner1a6babf2009-10-13 04:53:48 +00002206 if (unsigned NumArgs = CE->getNumArgs())
2207 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2208 CE->getArg(NumArgs-1)->getLocEnd());
2209 return true;
2210 }
Chris Lattner237f2752009-02-14 07:37:35 +00002211 }
2212 return false;
2213 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002214
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002215 // If we don't know precisely what we're looking at, let's not warn.
2216 case UnresolvedLookupExprClass:
2217 case CXXUnresolvedConstructExprClass:
2218 return false;
2219
Anders Carlsson6aa50392009-11-17 17:11:23 +00002220 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002221 case CXXConstructExprClass: {
2222 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2223 if (Type->hasAttr<WarnUnusedAttr>()) {
2224 WarnE = this;
2225 Loc = getLocStart();
2226 R1 = getSourceRange();
2227 return true;
2228 }
2229 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002230 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002231 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002232
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002233 case ObjCMessageExprClass: {
2234 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002235 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002236 ME->isInstanceMessage() &&
2237 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002238 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002239 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002240 Loc = getExprLoc();
2241 R1 = ME->getSourceRange();
2242 return true;
2243 }
2244
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002245 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
Fariborz Jahanianb0553e22015-02-16 23:49:44 +00002246 if (MD->hasAttr<WarnUnusedResultAttr>()) {
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002247 WarnE = this;
2248 Loc = getExprLoc();
2249 return true;
2250 }
2251
Chris Lattner237f2752009-02-14 07:37:35 +00002252 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002253 }
Mike Stump11289f42009-09-09 15:08:12 +00002254
John McCallb7bd14f2010-12-02 01:19:52 +00002255 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002256 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002257 Loc = getExprLoc();
2258 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002259 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002260
John McCallfe96e0b2011-11-06 09:01:30 +00002261 case PseudoObjectExprClass: {
2262 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2263
2264 // Only complain about things that have the form of a getter.
2265 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2266 isa<BinaryOperator>(PO->getSyntacticForm()))
2267 return false;
2268
Eli Friedmanc11535c2012-05-24 00:47:05 +00002269 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002270 Loc = getExprLoc();
2271 R1 = getSourceRange();
2272 return true;
2273 }
2274
Chris Lattner944d3062008-07-26 19:51:01 +00002275 case StmtExprClass: {
2276 // Statement exprs don't logically have side effects themselves, but are
2277 // sometimes used in macros in ways that give them a type that is unused.
2278 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2279 // however, if the result of the stmt expr is dead, we don't want to emit a
2280 // warning.
2281 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002282 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002283 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002284 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002285 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2286 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002287 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002288 }
Mike Stump11289f42009-09-09 15:08:12 +00002289
John McCallc493a732010-03-12 07:11:26 +00002290 if (getType()->isVoidType())
2291 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002292 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002293 Loc = cast<StmtExpr>(this)->getLParenLoc();
2294 R1 = getSourceRange();
2295 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002296 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002297 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002298 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002299 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002300 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002301 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002302 if (CE->getCastKind() == CK_ToVoid) {
2303 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002304 CE->getSubExpr()->getType().isVolatileQualified()) {
2305 const DeclRefExpr *DRE =
2306 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2307 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2308 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2309 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2310 R1, R2, Ctx);
2311 }
2312 }
Chris Lattner2706a552009-07-28 18:25:28 +00002313 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002314 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002315
Eli Friedmanc11535c2012-05-24 00:47:05 +00002316 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002317 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002318 if (CE->getCastKind() == CK_ConstructorConversion)
2319 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002320
Eli Friedmanc11535c2012-05-24 00:47:05 +00002321 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002322 if (const CXXFunctionalCastExpr *CXXCE =
2323 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002324 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002325 R1 = CXXCE->getSubExpr()->getSourceRange();
2326 } else {
2327 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2328 Loc = CStyleCE->getLParenLoc();
2329 R1 = CStyleCE->getSubExpr()->getSourceRange();
2330 }
Chris Lattner237f2752009-02-14 07:37:35 +00002331 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002332 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002333 case ImplicitCastExprClass: {
2334 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002335
Eli Friedmanc11535c2012-05-24 00:47:05 +00002336 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2337 if (ICE->getCastKind() == CK_LValueToRValue &&
2338 ICE->getSubExpr()->getType().isVolatileQualified())
2339 return false;
2340
2341 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2342 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002343 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002344 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002345 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002346 case CXXDefaultInitExprClass:
2347 return (cast<CXXDefaultInitExpr>(this)
2348 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002349
2350 case CXXNewExprClass:
2351 // FIXME: In theory, there might be new expressions that don't have side
2352 // effects (e.g. a placement new with an uninitialized POD).
2353 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002354 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002355 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002356 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002357 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002358 case ExprWithCleanupsClass:
2359 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002360 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002361 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002362}
2363
Fariborz Jahanian07735332009-02-22 18:40:18 +00002364/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002365/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002366bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002367 const Expr *E = IgnoreParens();
2368 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002369 default:
2370 return false;
2371 case ObjCIvarRefExprClass:
2372 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002373 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002374 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002375 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002376 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002377 case MaterializeTemporaryExprClass:
2378 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2379 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002380 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002381 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002382 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002383 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002384
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002385 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2386 if (VD->hasGlobalStorage())
2387 return true;
2388 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002389 // dereferencing to a pointer is always a gc'able candidate,
2390 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002391 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002392 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002393 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002394 return false;
2395 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002396 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002397 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002398 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002399 }
2400 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002401 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002402 }
2403}
Sebastian Redlce354af2010-09-10 20:55:33 +00002404
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002405bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2406 if (isTypeDependent())
2407 return false;
John McCall086a4642010-11-24 05:12:34 +00002408 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002409}
2410
John McCall0009fcc2011-04-26 20:42:42 +00002411QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002412 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002413
2414 // Bound member expressions are always one of these possibilities:
2415 // x->m x.m x->*y x.*y
2416 // (possibly parenthesized)
2417
2418 expr = expr->IgnoreParens();
2419 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2420 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2421 return mem->getMemberDecl()->getType();
2422 }
2423
2424 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2425 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2426 ->getPointeeType();
2427 assert(type->isFunctionType());
2428 return type;
2429 }
2430
David Majnemerced8bdf2015-02-25 17:36:15 +00002431 assert(isa<UnresolvedMemberExpr>(expr) || isa<CXXPseudoDestructorExpr>(expr));
John McCall0009fcc2011-04-26 20:42:42 +00002432 return QualType();
2433}
2434
Ted Kremenekfff70962008-01-17 16:57:34 +00002435Expr* Expr::IgnoreParens() {
2436 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002437 while (true) {
2438 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2439 E = P->getSubExpr();
2440 continue;
2441 }
2442 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2443 if (P->getOpcode() == UO_Extension) {
2444 E = P->getSubExpr();
2445 continue;
2446 }
2447 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002448 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2449 if (!P->isResultDependent()) {
2450 E = P->getResultExpr();
2451 continue;
2452 }
2453 }
Eli Friedman75807f22013-07-20 00:40:58 +00002454 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2455 if (!P->isConditionDependent()) {
2456 E = P->getChosenSubExpr();
2457 continue;
2458 }
2459 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002460 return E;
2461 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002462}
2463
Chris Lattnerf2660962008-02-13 01:02:39 +00002464/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2465/// or CastExprs or ImplicitCastExprs, returning their operand.
2466Expr *Expr::IgnoreParenCasts() {
2467 Expr *E = this;
2468 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002469 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002470 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002471 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002472 continue;
2473 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002474 if (MaterializeTemporaryExpr *Materialize
2475 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2476 E = Materialize->GetTemporaryExpr();
2477 continue;
2478 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002479 if (SubstNonTypeTemplateParmExpr *NTTP
2480 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2481 E = NTTP->getReplacement();
2482 continue;
2483 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002484 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002485 }
2486}
2487
Ted Kremenek6f375e52014-04-16 07:26:09 +00002488Expr *Expr::IgnoreCasts() {
2489 Expr *E = this;
2490 while (true) {
2491 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2492 E = P->getSubExpr();
2493 continue;
2494 }
2495 if (MaterializeTemporaryExpr *Materialize
2496 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2497 E = Materialize->GetTemporaryExpr();
2498 continue;
2499 }
2500 if (SubstNonTypeTemplateParmExpr *NTTP
2501 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2502 E = NTTP->getReplacement();
2503 continue;
2504 }
2505 return E;
2506 }
2507}
2508
John McCall5a4ce8b2010-12-04 08:24:19 +00002509/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2510/// casts. This is intended purely as a temporary workaround for code
2511/// that hasn't yet been rewritten to do the right thing about those
2512/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002513Expr *Expr::IgnoreParenLValueCasts() {
2514 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002515 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002516 E = E->IgnoreParens();
2517 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002518 if (P->getCastKind() == CK_LValueToRValue) {
2519 E = P->getSubExpr();
2520 continue;
2521 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002522 } else if (MaterializeTemporaryExpr *Materialize
2523 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2524 E = Materialize->GetTemporaryExpr();
2525 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002526 } else if (SubstNonTypeTemplateParmExpr *NTTP
2527 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2528 E = NTTP->getReplacement();
2529 continue;
John McCall34376a62010-12-04 03:47:34 +00002530 }
2531 break;
2532 }
2533 return E;
2534}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002535
2536Expr *Expr::ignoreParenBaseCasts() {
2537 Expr *E = this;
2538 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002539 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002540 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2541 if (CE->getCastKind() == CK_DerivedToBase ||
2542 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2543 CE->getCastKind() == CK_NoOp) {
2544 E = CE->getSubExpr();
2545 continue;
2546 }
2547 }
2548
2549 return E;
2550 }
2551}
2552
John McCalleebc8322010-05-05 22:59:52 +00002553Expr *Expr::IgnoreParenImpCasts() {
2554 Expr *E = this;
2555 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002556 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002557 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002558 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002559 continue;
2560 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002561 if (MaterializeTemporaryExpr *Materialize
2562 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2563 E = Materialize->GetTemporaryExpr();
2564 continue;
2565 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002566 if (SubstNonTypeTemplateParmExpr *NTTP
2567 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2568 E = NTTP->getReplacement();
2569 continue;
2570 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002571 return E;
John McCalleebc8322010-05-05 22:59:52 +00002572 }
2573}
2574
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002575Expr *Expr::IgnoreConversionOperator() {
2576 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002577 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002578 return MCE->getImplicitObjectArgument();
2579 }
2580 return this;
2581}
2582
Chris Lattneref26c772009-03-13 17:28:01 +00002583/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2584/// value (including ptr->int casts of the same size). Strip off any
2585/// ParenExpr or CastExprs, returning their operand.
2586Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2587 Expr *E = this;
2588 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002589 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002590
Chris Lattneref26c772009-03-13 17:28:01 +00002591 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2592 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002593 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002594 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002595
Chris Lattneref26c772009-03-13 17:28:01 +00002596 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2597 E = SE;
2598 continue;
2599 }
Mike Stump11289f42009-09-09 15:08:12 +00002600
Abramo Bagnara932e3932010-10-15 07:51:18 +00002601 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002602 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002603 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002604 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002605 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2606 E = SE;
2607 continue;
2608 }
2609 }
Mike Stump11289f42009-09-09 15:08:12 +00002610
Douglas Gregor6a40b082011-09-08 17:56:33 +00002611 if (SubstNonTypeTemplateParmExpr *NTTP
2612 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2613 E = NTTP->getReplacement();
2614 continue;
2615 }
2616
Chris Lattneref26c772009-03-13 17:28:01 +00002617 return E;
2618 }
2619}
2620
Douglas Gregord196a582009-12-14 19:27:10 +00002621bool Expr::isDefaultArgument() const {
2622 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002623 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2624 E = M->GetTemporaryExpr();
2625
Douglas Gregord196a582009-12-14 19:27:10 +00002626 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2627 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002628
Douglas Gregord196a582009-12-14 19:27:10 +00002629 return isa<CXXDefaultArgExpr>(E);
2630}
Chris Lattneref26c772009-03-13 17:28:01 +00002631
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002632/// \brief Skip over any no-op casts and any temporary-binding
2633/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002634static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002635 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2636 E = M->GetTemporaryExpr();
2637
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002638 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002639 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002640 E = ICE->getSubExpr();
2641 else
2642 break;
2643 }
2644
2645 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2646 E = BE->getSubExpr();
2647
2648 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002649 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002650 E = ICE->getSubExpr();
2651 else
2652 break;
2653 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002654
2655 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002656}
2657
John McCall7a626f62010-09-15 10:14:12 +00002658/// isTemporaryObject - Determines if this expression produces a
2659/// temporary of the given class type.
2660bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2661 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2662 return false;
2663
Anders Carlsson66bbf502010-11-28 16:40:49 +00002664 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002665
John McCall02dc8c72010-09-15 20:59:13 +00002666 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002667 if (!E->Classify(C).isPRValue()) {
2668 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002669 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002670 return false;
2671 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002672
John McCallf4ee1dd2010-09-16 06:57:56 +00002673 // Black-list a few cases which yield pr-values of class type that don't
2674 // refer to temporaries of that type:
2675
2676 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002677 if (isa<ImplicitCastExpr>(E)) {
2678 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2679 case CK_DerivedToBase:
2680 case CK_UncheckedDerivedToBase:
2681 return false;
2682 default:
2683 break;
2684 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002685 }
2686
John McCallf4ee1dd2010-09-16 06:57:56 +00002687 // - member expressions (all)
2688 if (isa<MemberExpr>(E))
2689 return false;
2690
Eli Friedman13ffdd82012-06-15 23:51:06 +00002691 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2692 if (BO->isPtrMemOp())
2693 return false;
2694
John McCallc07a0c72011-02-17 10:25:35 +00002695 // - opaque values (all)
2696 if (isa<OpaqueValueExpr>(E))
2697 return false;
2698
John McCall7a626f62010-09-15 10:14:12 +00002699 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002700}
2701
Douglas Gregor25b7e052011-03-02 21:06:53 +00002702bool Expr::isImplicitCXXThis() const {
2703 const Expr *E = this;
2704
2705 // Strip away parentheses and casts we don't care about.
2706 while (true) {
2707 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2708 E = Paren->getSubExpr();
2709 continue;
2710 }
2711
2712 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2713 if (ICE->getCastKind() == CK_NoOp ||
2714 ICE->getCastKind() == CK_LValueToRValue ||
2715 ICE->getCastKind() == CK_DerivedToBase ||
2716 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2717 E = ICE->getSubExpr();
2718 continue;
2719 }
2720 }
2721
2722 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2723 if (UnOp->getOpcode() == UO_Extension) {
2724 E = UnOp->getSubExpr();
2725 continue;
2726 }
2727 }
2728
Douglas Gregorfe314812011-06-21 17:03:29 +00002729 if (const MaterializeTemporaryExpr *M
2730 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2731 E = M->GetTemporaryExpr();
2732 continue;
2733 }
2734
Douglas Gregor25b7e052011-03-02 21:06:53 +00002735 break;
2736 }
2737
2738 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2739 return This->isImplicit();
2740
2741 return false;
2742}
2743
Douglas Gregor4619e432008-12-05 23:32:09 +00002744/// hasAnyTypeDependentArguments - Determines if any of the expressions
2745/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002746bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002747 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002748 if (Exprs[I]->isTypeDependent())
2749 return true;
2750
2751 return false;
2752}
2753
Abramo Bagnara847c6602014-05-22 19:20:46 +00002754bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2755 const Expr **Culprit) const {
Eli Friedman384da272009-01-25 03:12:18 +00002756 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002757 // which can be evaluated at compile-time. It very closely parallels
2758 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2759 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2760 // to isEvaluatable most of the time.
2761 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002762 // If we ever capture reference-binding directly in the AST, we can
2763 // kill the second parameter.
2764
2765 if (IsForRef) {
2766 EvalResult Result;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002767 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2768 return true;
2769 if (Culprit)
2770 *Culprit = this;
2771 return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002772 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002773
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002774 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002775 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002776 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002777 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002778 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002779 case CXXTemporaryObjectExprClass:
2780 case CXXConstructExprClass: {
2781 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002782
Eli Friedman4c27ac22013-07-16 22:40:53 +00002783 if (CE->getConstructor()->isTrivial() &&
2784 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2785 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002786 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002787
Eli Friedman4c27ac22013-07-16 22:40:53 +00002788 // Trivial copy constructor
2789 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Abramo Bagnara847c6602014-05-22 19:20:46 +00002790 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62306a2011-11-10 06:34:14 +00002791 }
2792
Richard Smithd62306a2011-11-10 06:34:14 +00002793 break;
John McCall81c9cea2010-08-01 21:51:45 +00002794 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002795 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002796 // This handles gcc's extension that allows global initializers like
2797 // "struct x {int x;} x = (struct x) {};".
2798 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002799 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002800 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002801 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002802 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002803 const InitListExpr *ILE = cast<InitListExpr>(this);
2804 if (ILE->getType()->isArrayType()) {
2805 unsigned numInits = ILE->getNumInits();
2806 for (unsigned i = 0; i < numInits; i++) {
Abramo Bagnara847c6602014-05-22 19:20:46 +00002807 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002808 return false;
2809 }
2810 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002811 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002812
2813 if (ILE->getType()->isRecordType()) {
2814 unsigned ElementNo = 0;
2815 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
Hans Wennborga302cd92014-08-21 16:06:57 +00002816 for (const auto *Field : RD->fields()) {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002817 // If this is a union, skip all the fields that aren't being initialized.
Hans Wennborga302cd92014-08-21 16:06:57 +00002818 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
Eli Friedman4c27ac22013-07-16 22:40:53 +00002819 continue;
2820
2821 // Don't emit anonymous bitfields, they just affect layout.
2822 if (Field->isUnnamedBitfield())
2823 continue;
2824
2825 if (ElementNo < ILE->getNumInits()) {
2826 const Expr *Elt = ILE->getInit(ElementNo++);
2827 if (Field->isBitField()) {
2828 // Bitfields have to evaluate to an integer.
2829 llvm::APSInt ResultTmp;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002830 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2831 if (Culprit)
2832 *Culprit = Elt;
Eli Friedman4c27ac22013-07-16 22:40:53 +00002833 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002834 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002835 } else {
2836 bool RefType = Field->getType()->isReferenceType();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002837 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002838 return false;
2839 }
2840 }
2841 }
2842 return true;
2843 }
2844
2845 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002846 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002847 case ImplicitValueInitExprClass:
2848 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002849 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002850 return cast<ParenExpr>(this)->getSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002851 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbourne91147592011-04-15 00:35:48 +00002852 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002853 return cast<GenericSelectionExpr>(this)->getResultExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002854 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002855 case ChooseExprClass:
Abramo Bagnara847c6602014-05-22 19:20:46 +00002856 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2857 if (Culprit)
2858 *Culprit = this;
Eli Friedman75807f22013-07-20 00:40:58 +00002859 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002860 }
Eli Friedman75807f22013-07-20 00:40:58 +00002861 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002862 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002863 case UnaryOperatorClass: {
2864 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002865 if (Exp->getOpcode() == UO_Extension)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002866 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002867 break;
2868 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002869 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002870 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002871 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002872 case CStyleCastExprClass:
2873 case ObjCBridgedCastExprClass:
2874 case CXXDynamicCastExprClass:
2875 case CXXReinterpretCastExprClass:
2876 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002877 const CastExpr *CE = cast<CastExpr>(this);
2878
Eli Friedman13ec75b2011-12-21 00:43:02 +00002879 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002880 if (CE->getCastKind() == CK_NoOp ||
2881 CE->getCastKind() == CK_LValueToRValue ||
2882 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002883 CE->getCastKind() == CK_ConstructorConversion ||
2884 CE->getCastKind() == CK_NonAtomicToAtomic ||
2885 CE->getCastKind() == CK_AtomicToNonAtomic)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002886 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smith161f09a2011-12-06 22:44:34 +00002887
Eli Friedman384da272009-01-25 03:12:18 +00002888 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002889 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002890 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002891 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002892 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002893
2894 case SubstNonTypeTemplateParmExprClass:
2895 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002896 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002897 case CXXDefaultArgExprClass:
2898 return cast<CXXDefaultArgExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002899 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002900 case CXXDefaultInitExprClass:
2901 return cast<CXXDefaultInitExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002902 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002903 }
Abramo Bagnara847c6602014-05-22 19:20:46 +00002904 if (isEvaluatable(Ctx))
2905 return true;
2906 if (Culprit)
2907 *Culprit = this;
2908 return false;
Steve Naroffb03f5942007-09-02 20:30:18 +00002909}
2910
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002911bool Expr::HasSideEffects(const ASTContext &Ctx,
2912 bool IncludePossibleEffects) const {
2913 // In circumstances where we care about definite side effects instead of
2914 // potential side effects, we want to ignore expressions that are part of a
2915 // macro expansion as a potential side effect.
2916 if (!IncludePossibleEffects && getExprLoc().isMacroID())
2917 return false;
2918
Richard Smith0421ce72012-08-07 04:16:51 +00002919 if (isInstantiationDependent())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002920 return IncludePossibleEffects;
Richard Smith0421ce72012-08-07 04:16:51 +00002921
2922 switch (getStmtClass()) {
2923 case NoStmtClass:
2924 #define ABSTRACT_STMT(Type)
2925 #define STMT(Type, Base) case Type##Class:
2926 #define EXPR(Type, Base)
2927 #include "clang/AST/StmtNodes.inc"
2928 llvm_unreachable("unexpected Expr kind");
2929
2930 case DependentScopeDeclRefExprClass:
2931 case CXXUnresolvedConstructExprClass:
2932 case CXXDependentScopeMemberExprClass:
2933 case UnresolvedLookupExprClass:
2934 case UnresolvedMemberExprClass:
2935 case PackExpansionExprClass:
2936 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002937 case FunctionParmPackExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002938 case TypoExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00002939 case CXXFoldExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002940 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2941
Richard Smitha33e4fe2012-08-07 05:18:29 +00002942 case DeclRefExprClass:
2943 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002944 case PredefinedExprClass:
2945 case IntegerLiteralClass:
2946 case FloatingLiteralClass:
2947 case ImaginaryLiteralClass:
2948 case StringLiteralClass:
2949 case CharacterLiteralClass:
2950 case OffsetOfExprClass:
2951 case ImplicitValueInitExprClass:
2952 case UnaryExprOrTypeTraitExprClass:
2953 case AddrLabelExprClass:
2954 case GNUNullExprClass:
2955 case CXXBoolLiteralExprClass:
2956 case CXXNullPtrLiteralExprClass:
2957 case CXXThisExprClass:
2958 case CXXScalarValueInitExprClass:
2959 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002960 case ArrayTypeTraitExprClass:
2961 case ExpressionTraitExprClass:
2962 case CXXNoexceptExprClass:
2963 case SizeOfPackExprClass:
2964 case ObjCStringLiteralClass:
2965 case ObjCEncodeExprClass:
2966 case ObjCBoolLiteralExprClass:
2967 case CXXUuidofExprClass:
2968 case OpaqueValueExprClass:
2969 // These never have a side-effect.
2970 return false;
2971
2972 case CallExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002973 case CXXOperatorCallExprClass:
2974 case CXXMemberCallExprClass:
2975 case CUDAKernelCallExprClass:
2976 case BlockExprClass:
2977 case CXXBindTemporaryExprClass:
2978 case UserDefinedLiteralClass:
2979 // We don't know a call definitely has side effects, but we can check the
2980 // call's operands.
2981 if (!IncludePossibleEffects)
2982 break;
2983 return true;
2984
John McCall5e77d762013-04-16 07:28:30 +00002985 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002986 case CompoundAssignOperatorClass:
2987 case VAArgExprClass:
2988 case AtomicExprClass:
2989 case StmtExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002990 case CXXThrowExprClass:
2991 case CXXNewExprClass:
2992 case CXXDeleteExprClass:
2993 case ExprWithCleanupsClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002994 // These always have a side-effect.
2995 return true;
2996
2997 case ParenExprClass:
2998 case ArraySubscriptExprClass:
2999 case MemberExprClass:
3000 case ConditionalOperatorClass:
3001 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003002 case CompoundLiteralExprClass:
3003 case ExtVectorElementExprClass:
3004 case DesignatedInitExprClass:
3005 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003006 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00003007 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003008 case SubstNonTypeTemplateParmExprClass:
3009 case MaterializeTemporaryExprClass:
3010 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00003011 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003012 case AsTypeExprClass:
3013 // These have a side-effect if any subexpression does.
3014 break;
3015
Richard Smitha33e4fe2012-08-07 05:18:29 +00003016 case UnaryOperatorClass:
3017 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00003018 return true;
3019 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003020
3021 case BinaryOperatorClass:
3022 if (cast<BinaryOperator>(this)->isAssignmentOp())
3023 return true;
3024 break;
3025
Richard Smith0421ce72012-08-07 04:16:51 +00003026 case InitListExprClass:
3027 // FIXME: The children for an InitListExpr doesn't include the array filler.
3028 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003029 if (E->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003030 return true;
3031 break;
3032
3033 case GenericSelectionExprClass:
3034 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003035 HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003036
3037 case ChooseExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003038 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
3039 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003040
3041 case CXXDefaultArgExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003042 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
3043 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003044
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003045 case CXXDefaultInitExprClass: {
3046 const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3047 if (const Expr *E = FD->getInClassInitializer())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003048 return E->HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith852c9db2013-04-20 22:23:05 +00003049 // If we've not yet parsed the initializer, assume it has side-effects.
3050 return true;
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003051 }
Richard Smith852c9db2013-04-20 22:23:05 +00003052
Richard Smith0421ce72012-08-07 04:16:51 +00003053 case CXXDynamicCastExprClass: {
3054 // A dynamic_cast expression has side-effects if it can throw.
3055 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3056 if (DCE->getTypeAsWritten()->isReferenceType() &&
3057 DCE->getCastKind() == CK_Dynamic)
3058 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003059 } // Fall through.
3060 case ImplicitCastExprClass:
3061 case CStyleCastExprClass:
3062 case CXXStaticCastExprClass:
3063 case CXXReinterpretCastExprClass:
3064 case CXXConstCastExprClass:
3065 case CXXFunctionalCastExprClass: {
Aaron Ballman409af502015-01-03 17:00:12 +00003066 // While volatile reads are side-effecting in both C and C++, we treat them
3067 // as having possible (not definite) side-effects. This allows idiomatic
3068 // code to behave without warning, such as sizeof(*v) for a volatile-
3069 // qualified pointer.
3070 if (!IncludePossibleEffects)
3071 break;
3072
Richard Smitha33e4fe2012-08-07 05:18:29 +00003073 const CastExpr *CE = cast<CastExpr>(this);
3074 if (CE->getCastKind() == CK_LValueToRValue &&
3075 CE->getSubExpr()->getType().isVolatileQualified())
3076 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003077 break;
3078 }
3079
Richard Smithef8bf432012-08-13 20:08:14 +00003080 case CXXTypeidExprClass:
3081 // typeid might throw if its subexpression is potentially-evaluated, so has
3082 // side-effects in that case whether or not its subexpression does.
3083 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00003084
3085 case CXXConstructExprClass:
3086 case CXXTemporaryObjectExprClass: {
3087 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003088 if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
Richard Smith0421ce72012-08-07 04:16:51 +00003089 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003090 // A trivial constructor does not add any side-effects of its own. Just look
3091 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00003092 break;
3093 }
3094
3095 case LambdaExprClass: {
3096 const LambdaExpr *LE = cast<LambdaExpr>(this);
3097 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3098 E = LE->capture_end(); I != E; ++I)
3099 if (I->getCaptureKind() == LCK_ByCopy)
3100 // FIXME: Only has a side-effect if the variable is volatile or if
3101 // the copy would invoke a non-trivial copy constructor.
3102 return true;
3103 return false;
3104 }
3105
3106 case PseudoObjectExprClass: {
3107 // Only look for side-effects in the semantic form, and look past
3108 // OpaqueValueExpr bindings in that form.
3109 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3110 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3111 E = PO->semantics_end();
3112 I != E; ++I) {
3113 const Expr *Subexpr = *I;
3114 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3115 Subexpr = OVE->getSourceExpr();
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003116 if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003117 return true;
3118 }
3119 return false;
3120 }
3121
3122 case ObjCBoxedExprClass:
3123 case ObjCArrayLiteralClass:
3124 case ObjCDictionaryLiteralClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003125 case ObjCSelectorExprClass:
3126 case ObjCProtocolExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003127 case ObjCIsaExprClass:
3128 case ObjCIndirectCopyRestoreExprClass:
3129 case ObjCSubscriptRefExprClass:
3130 case ObjCBridgedCastExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003131 case ObjCMessageExprClass:
3132 case ObjCPropertyRefExprClass:
3133 // FIXME: Classify these cases better.
3134 if (IncludePossibleEffects)
3135 return true;
3136 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003137 }
3138
3139 // Recurse to children.
3140 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3141 if (const Stmt *S = *SubStmts)
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003142 if (cast<Expr>(S)->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003143 return true;
3144
3145 return false;
3146}
3147
Douglas Gregor1be329d2012-02-23 07:33:15 +00003148namespace {
3149 /// \brief Look for a call to a non-trivial function within an expression.
3150 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3151 {
3152 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3153
3154 bool NonTrivial;
3155
3156 public:
3157 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003158 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003159
3160 bool hasNonTrivialCall() const { return NonTrivial; }
3161
3162 void VisitCallExpr(CallExpr *E) {
3163 if (CXXMethodDecl *Method
3164 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3165 if (Method->isTrivial()) {
3166 // Recurse to children of the call.
3167 Inherited::VisitStmt(E);
3168 return;
3169 }
3170 }
3171
3172 NonTrivial = true;
3173 }
3174
3175 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3176 if (E->getConstructor()->isTrivial()) {
3177 // Recurse to children of the call.
3178 Inherited::VisitStmt(E);
3179 return;
3180 }
3181
3182 NonTrivial = true;
3183 }
3184
3185 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3186 if (E->getTemporary()->getDestructor()->isTrivial()) {
3187 Inherited::VisitStmt(E);
3188 return;
3189 }
3190
3191 NonTrivial = true;
3192 }
3193 };
3194}
3195
3196bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3197 NonTrivialCallFinder Finder(Ctx);
3198 Finder.Visit(this);
3199 return Finder.hasNonTrivialCall();
3200}
3201
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003202/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3203/// pointer constant or not, as well as the specific kind of constant detected.
3204/// Null pointer constants can be integer constant expressions with the
3205/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3206/// (a GNU extension).
3207Expr::NullPointerConstantKind
3208Expr::isNullPointerConstant(ASTContext &Ctx,
3209 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003210 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003211 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003212 switch (NPC) {
3213 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003214 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003215 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003216 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003217 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003218 else
3219 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003220
Douglas Gregor56751b52009-09-25 04:25:58 +00003221 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003222 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003223 }
3224 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003225
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003226 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003227 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003228 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003229 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003230 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003231 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003232 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003233 Pointee->isVoidType() && // to void*
3234 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003235 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003236 }
Steve Naroffada7d422007-05-20 17:54:12 +00003237 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003238 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3239 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003240 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003241 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3242 // Accept ((void*)0) as a null pointer constant, as many other
3243 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003244 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003245 } else if (const GenericSelectionExpr *GE =
3246 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003247 if (GE->isResultDependent())
3248 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003249 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003250 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3251 if (CE->isConditionDependent())
3252 return NPCK_NotNull;
3253 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003254 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003255 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003256 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003257 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003258 } else if (const CXXDefaultInitExpr *DefaultInit
3259 = dyn_cast<CXXDefaultInitExpr>(this)) {
3260 // See through default initializer expressions.
3261 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003262 } else if (isa<GNUNullExpr>(this)) {
3263 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003264 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003265 } else if (const MaterializeTemporaryExpr *M
3266 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3267 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003268 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3269 if (const Expr *Source = OVE->getSourceExpr())
3270 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003271 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003272
Richard Smith89645bc2013-01-02 12:01:23 +00003273 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003274 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003275 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003276
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003277 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003278 if (!Ctx.getLangOpts().CPlusPlus11 &&
3279 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003280 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3281 const Expr *InitExpr = CLE->getInitializer();
3282 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3283 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3284 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003285 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003286 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003287 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003288 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003289
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003290 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003291 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3292 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003293 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003294 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003295 if (Lit && !Lit->getValue())
3296 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003297 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003298 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003299 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003300 // If we have an integer constant expression, we need to *evaluate* it and
3301 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003302 if (!isIntegerConstantExpr(Ctx))
3303 return NPCK_NotNull;
3304 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003305
David Blaikie1c7c8f72012-08-08 17:33:31 +00003306 if (EvaluateKnownConstInt(Ctx) != 0)
3307 return NPCK_NotNull;
3308
3309 if (isa<IntegerLiteral>(this))
3310 return NPCK_ZeroLiteral;
3311 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003312}
Steve Narofff7a5da12007-07-28 23:10:27 +00003313
John McCall34376a62010-12-04 03:47:34 +00003314/// \brief If this expression is an l-value for an Objective C
3315/// property, find the underlying property reference expression.
3316const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3317 const Expr *E = this;
3318 while (true) {
3319 assert((E->getValueKind() == VK_LValue &&
3320 E->getObjectKind() == OK_ObjCProperty) &&
3321 "expression is not a property reference");
3322 E = E->IgnoreParenCasts();
3323 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3324 if (BO->getOpcode() == BO_Comma) {
3325 E = BO->getRHS();
3326 continue;
3327 }
3328 }
3329
3330 break;
3331 }
3332
3333 return cast<ObjCPropertyRefExpr>(E);
3334}
3335
Anna Zaks97c7ce32012-10-01 20:34:04 +00003336bool Expr::isObjCSelfExpr() const {
3337 const Expr *E = IgnoreParenImpCasts();
3338
3339 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3340 if (!DRE)
3341 return false;
3342
3343 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3344 if (!Param)
3345 return false;
3346
3347 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3348 if (!M)
3349 return false;
3350
3351 return M->getSelfDecl() == Param;
3352}
3353
John McCalld25db7e2013-05-06 21:39:12 +00003354FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003355 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003356
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003357 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003358 if (ICE->getCastKind() == CK_LValueToRValue ||
3359 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003360 E = ICE->getSubExpr()->IgnoreParens();
3361 else
3362 break;
3363 }
3364
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003365 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003366 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003367 if (Field->isBitField())
3368 return Field;
3369
John McCalld25db7e2013-05-06 21:39:12 +00003370 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3371 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3372 if (Ivar->isBitField())
3373 return Ivar;
3374
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003375 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3376 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3377 if (Field->isBitField())
3378 return Field;
3379
Eli Friedman609ada22011-07-13 02:05:57 +00003380 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003381 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003382 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003383
Eli Friedman609ada22011-07-13 02:05:57 +00003384 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003385 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003386 }
3387
Richard Smith5b571672014-09-24 23:55:00 +00003388 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3389 if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3390 return UnOp->getSubExpr()->getSourceBitField();
3391
Craig Topper36250ad2014-05-12 05:36:57 +00003392 return nullptr;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003393}
3394
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003395bool Expr::refersToVectorElement() const {
3396 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003397
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003398 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003399 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003400 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003401 E = ICE->getSubExpr()->IgnoreParens();
3402 else
3403 break;
3404 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003405
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003406 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3407 return ASE->getBase()->getType()->isVectorType();
3408
3409 if (isa<ExtVectorElementExpr>(E))
3410 return true;
3411
3412 return false;
3413}
3414
Chris Lattnerb8211f62009-02-16 22:14:05 +00003415/// isArrow - Return true if the base expression is a pointer to vector,
3416/// return false if the base expression is a vector.
3417bool ExtVectorElementExpr::isArrow() const {
3418 return getBase()->getType()->isPointerType();
3419}
3420
Nate Begemance4d7fc2008-04-18 23:10:10 +00003421unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003422 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003423 return VT->getNumElements();
3424 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003425}
3426
Nate Begemanf322eab2008-05-09 06:41:27 +00003427/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003428bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003429 // FIXME: Refactor this code to an accessor on the AST node which returns the
3430 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003431 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003432
3433 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003434 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003435 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003436
Nate Begeman7e5185b2009-01-18 02:01:21 +00003437 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003438 if (Comp[0] == 's' || Comp[0] == 'S')
3439 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003440
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003441 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003442 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003443 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003444
Steve Naroff0d595ca2007-07-30 03:29:09 +00003445 return false;
3446}
Chris Lattner885b4952007-08-02 23:36:59 +00003447
Nate Begemanf322eab2008-05-09 06:41:27 +00003448/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003449void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003450 SmallVectorImpl<unsigned> &Elts) const {
3451 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003452 if (Comp[0] == 's' || Comp[0] == 'S')
3453 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003454
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003455 bool isHi = Comp == "hi";
3456 bool isLo = Comp == "lo";
3457 bool isEven = Comp == "even";
3458 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003459
Nate Begemanf322eab2008-05-09 06:41:27 +00003460 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3461 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003462
Nate Begemanf322eab2008-05-09 06:41:27 +00003463 if (isHi)
3464 Index = e + i;
3465 else if (isLo)
3466 Index = i;
3467 else if (isEven)
3468 Index = 2 * i;
3469 else if (isOdd)
3470 Index = 2 * i + 1;
3471 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003472 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003473
Nate Begemand3862152008-05-13 21:03:02 +00003474 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003475 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003476}
3477
Douglas Gregor9a129192010-04-21 00:45:42 +00003478ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003479 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003480 SourceLocation LBracLoc,
3481 SourceLocation SuperLoc,
3482 bool IsInstanceSuper,
3483 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003484 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003485 ArrayRef<SourceLocation> SelLocs,
3486 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003487 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003488 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003489 SourceLocation RBracLoc,
3490 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003491 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003492 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003493 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003494 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003495 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3496 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003497 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Craig Topper36250ad2014-05-12 05:36:57 +00003498 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3499 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3500 RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003501{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003502 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003503 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003504}
3505
Douglas Gregor9a129192010-04-21 00:45:42 +00003506ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003507 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003508 SourceLocation LBracLoc,
3509 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003510 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003511 ArrayRef<SourceLocation> SelLocs,
3512 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003513 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003514 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003515 SourceLocation RBracLoc,
3516 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003517 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003518 T->isDependentType(), T->isInstantiationDependentType(),
3519 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003520 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3521 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003522 Kind(Class),
Craig Topper36250ad2014-05-12 05:36:57 +00003523 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3524 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003525{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003526 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003527 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003528}
3529
Douglas Gregor9a129192010-04-21 00:45:42 +00003530ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003531 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003532 SourceLocation LBracLoc,
3533 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003534 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003535 ArrayRef<SourceLocation> SelLocs,
3536 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003537 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003538 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003539 SourceLocation RBracLoc,
3540 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003541 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003542 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003543 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003544 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003545 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3546 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003547 Kind(Instance),
Craig Topper36250ad2014-05-12 05:36:57 +00003548 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3549 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003550{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003551 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003552 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003553}
3554
3555void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3556 ArrayRef<SourceLocation> SelLocs,
3557 SelectorLocationsKind SelLocsK) {
3558 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003559 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003560 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003561 if (Args[I]->isTypeDependent())
3562 ExprBits.TypeDependent = true;
3563 if (Args[I]->isValueDependent())
3564 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003565 if (Args[I]->isInstantiationDependent())
3566 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003567 if (Args[I]->containsUnexpandedParameterPack())
3568 ExprBits.ContainsUnexpandedParameterPack = true;
3569
3570 MyArgs[I] = Args[I];
3571 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003572
Benjamin Kramer2325b242012-02-20 00:20:48 +00003573 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003574 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003575 if (SelLocsK == SelLoc_NonStandard)
3576 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3577 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003578}
3579
Craig Topperce7167c2013-08-22 04:58:56 +00003580ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003581 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003582 SourceLocation LBracLoc,
3583 SourceLocation SuperLoc,
3584 bool IsInstanceSuper,
3585 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003586 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003587 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003588 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003589 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003590 SourceLocation RBracLoc,
3591 bool isImplicit) {
3592 assert((!SelLocs.empty() || isImplicit) &&
3593 "No selector locs for non-implicit message");
3594 ObjCMessageExpr *Mem;
3595 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3596 if (isImplicit)
3597 Mem = alloc(Context, Args.size(), 0);
3598 else
3599 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003600 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003601 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003602 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003603}
3604
Craig Topperce7167c2013-08-22 04:58:56 +00003605ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003606 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003607 SourceLocation LBracLoc,
3608 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003609 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003610 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003611 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003612 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003613 SourceLocation RBracLoc,
3614 bool isImplicit) {
3615 assert((!SelLocs.empty() || isImplicit) &&
3616 "No selector locs for non-implicit message");
3617 ObjCMessageExpr *Mem;
3618 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3619 if (isImplicit)
3620 Mem = alloc(Context, Args.size(), 0);
3621 else
3622 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003623 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003624 SelLocs, SelLocsK, Method, Args, RBracLoc,
3625 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003626}
3627
Craig Topperce7167c2013-08-22 04:58:56 +00003628ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003629 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003630 SourceLocation LBracLoc,
3631 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003632 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003633 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003634 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003635 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003636 SourceLocation RBracLoc,
3637 bool isImplicit) {
3638 assert((!SelLocs.empty() || isImplicit) &&
3639 "No selector locs for non-implicit message");
3640 ObjCMessageExpr *Mem;
3641 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3642 if (isImplicit)
3643 Mem = alloc(Context, Args.size(), 0);
3644 else
3645 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003646 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003647 SelLocs, SelLocsK, Method, Args, RBracLoc,
3648 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003649}
3650
Craig Topperce7167c2013-08-22 04:58:56 +00003651ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003652 unsigned NumArgs,
3653 unsigned NumStoredSelLocs) {
3654 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003655 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3656}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003657
Craig Topperce7167c2013-08-22 04:58:56 +00003658ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003659 ArrayRef<Expr *> Args,
3660 SourceLocation RBraceLoc,
3661 ArrayRef<SourceLocation> SelLocs,
3662 Selector Sel,
3663 SelectorLocationsKind &SelLocsK) {
3664 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3665 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3666 : 0;
3667 return alloc(C, Args.size(), NumStoredSelLocs);
3668}
3669
Craig Topperce7167c2013-08-22 04:58:56 +00003670ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003671 unsigned NumArgs,
3672 unsigned NumStoredSelLocs) {
3673 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3674 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3675 return (ObjCMessageExpr *)C.Allocate(Size,
3676 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3677}
3678
3679void ObjCMessageExpr::getSelectorLocs(
3680 SmallVectorImpl<SourceLocation> &SelLocs) const {
3681 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3682 SelLocs.push_back(getSelectorLoc(i));
3683}
3684
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003685SourceRange ObjCMessageExpr::getReceiverRange() const {
3686 switch (getReceiverKind()) {
3687 case Instance:
3688 return getInstanceReceiver()->getSourceRange();
3689
3690 case Class:
3691 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3692
3693 case SuperInstance:
3694 case SuperClass:
3695 return getSuperLoc();
3696 }
3697
David Blaikiee4d798f2012-01-20 21:50:17 +00003698 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003699}
3700
Douglas Gregor9a129192010-04-21 00:45:42 +00003701Selector ObjCMessageExpr::getSelector() const {
3702 if (HasMethod)
3703 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3704 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003705 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003706}
3707
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003708QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003709 switch (getReceiverKind()) {
3710 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003711 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003712 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003713 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003714 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003715 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003716 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003717 }
3718
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003719 llvm_unreachable("unexpected receiver kind");
3720}
3721
3722ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3723 QualType T = getReceiverType();
3724
3725 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3726 return Ptr->getInterfaceDecl();
3727
3728 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3729 return Ty->getInterface();
3730
Craig Topper36250ad2014-05-12 05:36:57 +00003731 return nullptr;
Ted Kremenek2c809302010-02-11 22:41:21 +00003732}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003733
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003734StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003735 switch (getBridgeKind()) {
3736 case OBC_Bridge:
3737 return "__bridge";
3738 case OBC_BridgeTransfer:
3739 return "__bridge_transfer";
3740 case OBC_BridgeRetained:
3741 return "__bridge_retained";
3742 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003743
3744 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003745}
3746
Craig Topper37932912013-08-18 10:09:15 +00003747ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003748 QualType Type, SourceLocation BLoc,
3749 SourceLocation RP)
3750 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3751 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003752 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003753 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003754 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003755{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003756 SubExprs = new (C) Stmt*[args.size()];
3757 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003758 if (args[i]->isTypeDependent())
3759 ExprBits.TypeDependent = true;
3760 if (args[i]->isValueDependent())
3761 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003762 if (args[i]->isInstantiationDependent())
3763 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003764 if (args[i]->containsUnexpandedParameterPack())
3765 ExprBits.ContainsUnexpandedParameterPack = true;
3766
3767 SubExprs[i] = args[i];
3768 }
3769}
3770
Craig Topper37932912013-08-18 10:09:15 +00003771void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003772 if (SubExprs) C.Deallocate(SubExprs);
3773
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003774 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003775 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003776 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003777}
Nate Begeman48745922009-08-12 02:28:50 +00003778
Craig Topper37932912013-08-18 10:09:15 +00003779GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003780 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003781 ArrayRef<TypeSourceInfo*> AssocTypes,
3782 ArrayRef<Expr*> AssocExprs,
3783 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003784 SourceLocation RParenLoc,
3785 bool ContainsUnexpandedParameterPack,
3786 unsigned ResultIndex)
3787 : Expr(GenericSelectionExprClass,
3788 AssocExprs[ResultIndex]->getType(),
3789 AssocExprs[ResultIndex]->getValueKind(),
3790 AssocExprs[ResultIndex]->getObjectKind(),
3791 AssocExprs[ResultIndex]->isTypeDependent(),
3792 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003793 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003794 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003795 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3796 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3797 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3798 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003799 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003800 assert(AssocTypes.size() == AssocExprs.size());
3801 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3802 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003803}
3804
Craig Topper37932912013-08-18 10:09:15 +00003805GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003806 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003807 ArrayRef<TypeSourceInfo*> AssocTypes,
3808 ArrayRef<Expr*> AssocExprs,
3809 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003810 SourceLocation RParenLoc,
3811 bool ContainsUnexpandedParameterPack)
3812 : Expr(GenericSelectionExprClass,
3813 Context.DependentTy,
3814 VK_RValue,
3815 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003816 /*isTypeDependent=*/true,
3817 /*isValueDependent=*/true,
3818 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003819 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003820 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3821 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3822 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3823 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003824 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003825 assert(AssocTypes.size() == AssocExprs.size());
3826 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3827 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003828}
3829
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003830//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003831// DesignatedInitExpr
3832//===----------------------------------------------------------------------===//
3833
Chandler Carruth631abd92011-06-16 06:47:06 +00003834IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003835 assert(Kind == FieldDesignator && "Only valid on a field designator");
3836 if (Field.NameOrField & 0x01)
3837 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3838 else
3839 return getField()->getIdentifier();
3840}
3841
Craig Topper37932912013-08-18 10:09:15 +00003842DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003843 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003844 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003845 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003846 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003847 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003848 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003849 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003850 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003851 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003852 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003853 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003854 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003855 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003856 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003857
3858 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003859 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003860 *Child++ = Init;
3861
3862 // Copy the designators and their subexpressions, computing
3863 // value-dependence along the way.
3864 unsigned IndexIdx = 0;
3865 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003866 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003867
3868 if (this->Designators[I].isArrayDesignator()) {
3869 // Compute type- and value-dependence.
3870 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003871 if (Index->isTypeDependent() || Index->isValueDependent())
David Majnemer4f217682015-01-09 01:39:09 +00003872 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003873 if (Index->isInstantiationDependent())
3874 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003875 // Propagate unexpanded parameter packs.
3876 if (Index->containsUnexpandedParameterPack())
3877 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003878
3879 // Copy the index expressions into permanent storage.
3880 *Child++ = IndexExprs[IndexIdx++];
3881 } else if (this->Designators[I].isArrayRangeDesignator()) {
3882 // Compute type- and value-dependence.
3883 Expr *Start = IndexExprs[IndexIdx];
3884 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003885 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003886 End->isTypeDependent() || End->isValueDependent()) {
David Majnemer4f217682015-01-09 01:39:09 +00003887 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003888 ExprBits.InstantiationDependent = true;
3889 } else if (Start->isInstantiationDependent() ||
3890 End->isInstantiationDependent()) {
3891 ExprBits.InstantiationDependent = true;
3892 }
3893
Douglas Gregora6e053e2010-12-15 01:34:56 +00003894 // Propagate unexpanded parameter packs.
3895 if (Start->containsUnexpandedParameterPack() ||
3896 End->containsUnexpandedParameterPack())
3897 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003898
3899 // Copy the start/end expressions into permanent storage.
3900 *Child++ = IndexExprs[IndexIdx++];
3901 *Child++ = IndexExprs[IndexIdx++];
3902 }
3903 }
3904
Benjamin Kramerc215e762012-08-24 11:54:20 +00003905 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003906}
3907
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003908DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003909DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003910 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003911 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003912 SourceLocation ColonOrEqualLoc,
3913 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003914 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003915 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003916 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003917 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003918 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003919}
3920
Craig Topper37932912013-08-18 10:09:15 +00003921DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003922 unsigned NumIndexExprs) {
3923 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3924 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3925 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3926}
3927
Craig Topper37932912013-08-18 10:09:15 +00003928void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003929 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003930 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003931 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003932 NumDesignators = NumDesigs;
3933 for (unsigned I = 0; I != NumDesigs; ++I)
3934 Designators[I] = Desigs[I];
3935}
3936
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003937SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3938 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3939 if (size() == 1)
3940 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003941 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3942 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003943}
3944
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003945SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003946 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003947 Designator &First =
3948 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003949 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003950 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003951 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3952 else
3953 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3954 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003955 StartLoc =
3956 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003957 return StartLoc;
3958}
3959
3960SourceLocation DesignatedInitExpr::getLocEnd() const {
3961 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003962}
3963
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003964Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003965 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003966 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003967 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3968}
3969
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003970Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003971 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003972 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003973 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003974 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3975}
3976
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003977Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003978 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003979 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003980 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003981 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3982}
3983
Douglas Gregord5846a12009-04-15 06:41:24 +00003984/// \brief Replaces the designator at index @p Idx with the series
3985/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003986void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003987 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003988 const Designator *Last) {
3989 unsigned NumNewDesignators = Last - First;
3990 if (NumNewDesignators == 0) {
3991 std::copy_backward(Designators + Idx + 1,
3992 Designators + NumDesignators,
3993 Designators + Idx);
3994 --NumNewDesignators;
3995 return;
3996 } else if (NumNewDesignators == 1) {
3997 Designators[Idx] = *First;
3998 return;
3999 }
4000
Mike Stump11289f42009-09-09 15:08:12 +00004001 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00004002 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00004003 std::copy(Designators, Designators + Idx, NewDesignators);
4004 std::copy(First, Last, NewDesignators + Idx);
4005 std::copy(Designators + Idx + 1, Designators + NumDesignators,
4006 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00004007 Designators = NewDesignators;
4008 NumDesignators = NumDesignators - 1 + NumNewDesignators;
4009}
4010
Craig Topper37932912013-08-18 10:09:15 +00004011ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00004012 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00004013 SourceLocation rparenloc)
4014 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00004015 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004016 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
4017 Exprs = new (C) Stmt*[exprs.size()];
4018 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00004019 if (exprs[i]->isTypeDependent())
4020 ExprBits.TypeDependent = true;
4021 if (exprs[i]->isValueDependent())
4022 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00004023 if (exprs[i]->isInstantiationDependent())
4024 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00004025 if (exprs[i]->containsUnexpandedParameterPack())
4026 ExprBits.ContainsUnexpandedParameterPack = true;
4027
Nate Begeman5ec4b312009-08-10 23:49:36 +00004028 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00004029 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00004030}
4031
John McCall1bf58462011-02-16 08:02:54 +00004032const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
4033 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
4034 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00004035 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
4036 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00004037 e = cast<CXXConstructExpr>(e)->getArg(0);
4038 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
4039 e = ice->getSubExpr();
4040 return cast<OpaqueValueExpr>(e);
4041}
4042
Craig Topper37932912013-08-18 10:09:15 +00004043PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
4044 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00004045 unsigned numSemanticExprs) {
4046 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
4047 (1 + numSemanticExprs) * sizeof(Expr*),
4048 llvm::alignOf<PseudoObjectExpr>());
4049 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
4050}
4051
4052PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
4053 : Expr(PseudoObjectExprClass, shell) {
4054 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
4055}
4056
Craig Topper37932912013-08-18 10:09:15 +00004057PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00004058 ArrayRef<Expr*> semantics,
4059 unsigned resultIndex) {
4060 assert(syntax && "no syntactic expression!");
4061 assert(semantics.size() && "no semantic expressions!");
4062
4063 QualType type;
4064 ExprValueKind VK;
4065 if (resultIndex == NoResult) {
4066 type = C.VoidTy;
4067 VK = VK_RValue;
4068 } else {
4069 assert(resultIndex < semantics.size());
4070 type = semantics[resultIndex]->getType();
4071 VK = semantics[resultIndex]->getValueKind();
4072 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4073 }
4074
4075 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4076 (1 + semantics.size()) * sizeof(Expr*),
4077 llvm::alignOf<PseudoObjectExpr>());
4078 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4079 resultIndex);
4080}
4081
4082PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4083 Expr *syntax, ArrayRef<Expr*> semantics,
4084 unsigned resultIndex)
4085 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4086 /*filled in at end of ctor*/ false, false, false, false) {
4087 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4088 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4089
4090 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4091 Expr *E = (i == 0 ? syntax : semantics[i-1]);
4092 getSubExprsBuffer()[i] = E;
4093
4094 if (E->isTypeDependent())
4095 ExprBits.TypeDependent = true;
4096 if (E->isValueDependent())
4097 ExprBits.ValueDependent = true;
4098 if (E->isInstantiationDependent())
4099 ExprBits.InstantiationDependent = true;
4100 if (E->containsUnexpandedParameterPack())
4101 ExprBits.ContainsUnexpandedParameterPack = true;
4102
4103 if (isa<OpaqueValueExpr>(E))
Craig Topper36250ad2014-05-12 05:36:57 +00004104 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCallfe96e0b2011-11-06 09:01:30 +00004105 "opaque-value semantic expressions for pseudo-object "
4106 "operations must have sources");
4107 }
4108}
4109
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004110//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00004111// ExprIterator.
4112//===----------------------------------------------------------------------===//
4113
4114Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
4115Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
4116Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
4117const Expr* ConstExprIterator::operator[](size_t idx) const {
4118 return cast<Expr>(I[idx]);
4119}
4120const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
4121const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4122
4123//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00004124// Child Iterators for iterating over subexpressions/substatements
4125//===----------------------------------------------------------------------===//
4126
Peter Collingbournee190dee2011-03-11 19:24:49 +00004127// UnaryExprOrTypeTraitExpr
4128Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00004129 // If this is of a type and the type is a VLA type (and not a typedef), the
4130 // size expression of the VLA needs to be treated as an executable expression.
4131 // Why isn't this weirdness documented better in StmtIterator?
4132 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00004133 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00004134 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00004135 return child_range(child_iterator(T), child_iterator());
4136 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00004137 }
John McCallbd066782011-02-09 08:16:59 +00004138 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00004139}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004140
Steve Naroffd54978b2007-09-18 23:55:05 +00004141// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004142Stmt::child_range ObjCMessageExpr::children() {
4143 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004144 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004145 begin = reinterpret_cast<Stmt **>(this + 1);
4146 else
4147 begin = reinterpret_cast<Stmt **>(getArgs());
4148 return child_range(begin,
4149 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004150}
4151
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004152ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004153 QualType T, ObjCMethodDecl *Method,
4154 SourceRange SR)
4155 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4156 false, false, false, false),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004157 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004158{
4159 Expr **SaveElements = getElements();
4160 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4161 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4162 ExprBits.ValueDependent = true;
4163 if (Elements[I]->isInstantiationDependent())
4164 ExprBits.InstantiationDependent = true;
4165 if (Elements[I]->containsUnexpandedParameterPack())
4166 ExprBits.ContainsUnexpandedParameterPack = true;
4167
4168 SaveElements[I] = Elements[I];
4169 }
4170}
4171
Craig Topperce7167c2013-08-22 04:58:56 +00004172ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004173 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004174 QualType T, ObjCMethodDecl * Method,
4175 SourceRange SR) {
4176 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4177 + Elements.size() * sizeof(Expr *));
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004178 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004179}
4180
Craig Topperce7167c2013-08-22 04:58:56 +00004181ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004182 unsigned NumElements) {
4183
4184 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4185 + NumElements * sizeof(Expr *));
4186 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4187}
4188
4189ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4190 ArrayRef<ObjCDictionaryElement> VK,
4191 bool HasPackExpansions,
4192 QualType T, ObjCMethodDecl *method,
4193 SourceRange SR)
4194 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4195 false, false),
4196 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004197 DictWithObjectsMethod(method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004198{
4199 KeyValuePair *KeyValues = getKeyValues();
4200 ExpansionData *Expansions = getExpansionData();
4201 for (unsigned I = 0; I < NumElements; I++) {
4202 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4203 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4204 ExprBits.ValueDependent = true;
4205 if (VK[I].Key->isInstantiationDependent() ||
4206 VK[I].Value->isInstantiationDependent())
4207 ExprBits.InstantiationDependent = true;
4208 if (VK[I].EllipsisLoc.isInvalid() &&
4209 (VK[I].Key->containsUnexpandedParameterPack() ||
4210 VK[I].Value->containsUnexpandedParameterPack()))
4211 ExprBits.ContainsUnexpandedParameterPack = true;
4212
4213 KeyValues[I].Key = VK[I].Key;
4214 KeyValues[I].Value = VK[I].Value;
4215 if (Expansions) {
4216 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4217 if (VK[I].NumExpansions)
4218 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4219 else
4220 Expansions[I].NumExpansionsPlusOne = 0;
4221 }
4222 }
4223}
4224
4225ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004226ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004227 ArrayRef<ObjCDictionaryElement> VK,
4228 bool HasPackExpansions,
4229 QualType T, ObjCMethodDecl *method,
4230 SourceRange SR) {
4231 unsigned ExpansionsSize = 0;
4232 if (HasPackExpansions)
4233 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4234
4235 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4236 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004237 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004238}
4239
4240ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004241ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004242 bool HasPackExpansions) {
4243 unsigned ExpansionsSize = 0;
4244 if (HasPackExpansions)
4245 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4246 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4247 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4248 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4249 HasPackExpansions);
4250}
4251
Craig Topperce7167c2013-08-22 04:58:56 +00004252ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004253 Expr *base,
4254 Expr *key, QualType T,
4255 ObjCMethodDecl *getMethod,
4256 ObjCMethodDecl *setMethod,
4257 SourceLocation RB) {
4258 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4259 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4260 OK_ObjCSubscript,
4261 getMethod, setMethod, RB);
4262}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004263
Benjamin Kramerc215e762012-08-24 11:54:20 +00004264AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004265 QualType t, AtomicOp op, SourceLocation RP)
4266 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4267 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004268 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004269{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004270 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4271 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004272 if (args[i]->isTypeDependent())
4273 ExprBits.TypeDependent = true;
4274 if (args[i]->isValueDependent())
4275 ExprBits.ValueDependent = true;
4276 if (args[i]->isInstantiationDependent())
4277 ExprBits.InstantiationDependent = true;
4278 if (args[i]->containsUnexpandedParameterPack())
4279 ExprBits.ContainsUnexpandedParameterPack = true;
4280
4281 SubExprs[i] = args[i];
4282 }
4283}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004284
4285unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4286 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004287 case AO__c11_atomic_init:
4288 case AO__c11_atomic_load:
4289 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004290 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004291
4292 case AO__c11_atomic_store:
4293 case AO__c11_atomic_exchange:
4294 case AO__atomic_load:
4295 case AO__atomic_store:
4296 case AO__atomic_store_n:
4297 case AO__atomic_exchange_n:
4298 case AO__c11_atomic_fetch_add:
4299 case AO__c11_atomic_fetch_sub:
4300 case AO__c11_atomic_fetch_and:
4301 case AO__c11_atomic_fetch_or:
4302 case AO__c11_atomic_fetch_xor:
4303 case AO__atomic_fetch_add:
4304 case AO__atomic_fetch_sub:
4305 case AO__atomic_fetch_and:
4306 case AO__atomic_fetch_or:
4307 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004308 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004309 case AO__atomic_add_fetch:
4310 case AO__atomic_sub_fetch:
4311 case AO__atomic_and_fetch:
4312 case AO__atomic_or_fetch:
4313 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004314 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004315 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004316
4317 case AO__atomic_exchange:
4318 return 4;
4319
4320 case AO__c11_atomic_compare_exchange_strong:
4321 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004322 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004323
4324 case AO__atomic_compare_exchange:
4325 case AO__atomic_compare_exchange_n:
4326 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004327 }
4328 llvm_unreachable("unknown atomic op");
4329}