blob: db912d1d369a367cdea864ce84080956ad773249 [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 Bataev07649fb2014-12-16 08:01:48 +0000325 ValueDecl *D, bool RefersToCapturedVariable,
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 Bataev07649fb2014-12-16 08:01:48 +0000346 DeclRefExprBits.RefersToCapturedVariable = RefersToCapturedVariable;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000347 if (TemplateArgs) {
348 bool Dependent = false;
349 bool InstantiationDependent = false;
350 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000351 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
352 Dependent,
353 InstantiationDependent,
354 ContainsUnexpandedParameterPack);
Richard Smithcfaa5a32014-10-17 02:46:42 +0000355 assert(!Dependent && "built a DeclRefExpr with dependent template args");
356 ExprBits.InstantiationDependent |= InstantiationDependent;
357 ExprBits.ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000358 } else if (TemplateKWLoc.isValid()) {
359 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000360 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000361 DeclRefExprBits.HadMultipleCandidates = 0;
362
Daniel Dunbar9d355812012-03-09 01:51:51 +0000363 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000364}
365
Craig Topperce7167c2013-08-22 04:58:56 +0000366DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000368 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000369 ValueDecl *D,
Alexey Bataev07649fb2014-12-16 08:01:48 +0000370 bool RefersToCapturedVariable,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000371 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000372 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000373 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000374 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000375 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000376 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev07649fb2014-12-16 08:01:48 +0000377 RefersToCapturedVariable,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000378 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000379 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000380}
381
Craig Topperce7167c2013-08-22 04:58:56 +0000382DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000383 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000384 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000385 ValueDecl *D,
Alexey Bataev07649fb2014-12-16 08:01:48 +0000386 bool RefersToCapturedVariable,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000387 const DeclarationNameInfo &NameInfo,
388 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000389 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000390 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000391 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000392 // Filter out cases where the found Decl is the same as the value refenenced.
393 if (D == FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +0000394 FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000395
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000396 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000397 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000398 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000399 if (FoundD)
400 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000401 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000402 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
403 else if (TemplateKWLoc.isValid())
404 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000405
Chris Lattner5c0b4052010-10-30 05:14:06 +0000406 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000407 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
Alexey Bataev07649fb2014-12-16 08:01:48 +0000408 RefersToCapturedVariable,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000409 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000410}
411
Craig Topperce7167c2013-08-22 04:58:56 +0000412DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000413 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000414 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000415 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000416 unsigned NumTemplateArgs) {
417 std::size_t Size = sizeof(DeclRefExpr);
418 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000419 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000420 if (HasFoundDecl)
421 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000422 if (HasTemplateKWAndArgsInfo)
423 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000424
Chris Lattner5c0b4052010-10-30 05:14:06 +0000425 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000426 return new (Mem) DeclRefExpr(EmptyShell());
427}
428
Daniel Dunbarb507f272012-03-09 15:39:15 +0000429SourceLocation DeclRefExpr::getLocStart() const {
430 if (hasQualifier())
431 return getQualifierLoc().getBeginLoc();
432 return getNameInfo().getLocStart();
433}
434SourceLocation DeclRefExpr::getLocEnd() const {
435 if (hasExplicitTemplateArgs())
436 return getRAngleLoc();
437 return getNameInfo().getLocEnd();
438}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000439
Alexey Bataevec474782014-10-09 08:45:04 +0000440PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy, IdentType IT,
441 StringLiteral *SL)
442 : Expr(PredefinedExprClass, FNTy, VK_LValue, OK_Ordinary,
443 FNTy->isDependentType(), FNTy->isDependentType(),
444 FNTy->isInstantiationDependentType(),
445 /*ContainsUnexpandedParameterPack=*/false),
446 Loc(L), Type(IT), FnName(SL) {}
447
448StringLiteral *PredefinedExpr::getFunctionName() {
Alexey Bataev769562a2014-10-10 18:58:13 +0000449 return cast_or_null<StringLiteral>(FnName);
Alexey Bataevec474782014-10-09 08:45:04 +0000450}
451
452StringRef PredefinedExpr::getIdentTypeName(PredefinedExpr::IdentType IT) {
453 switch (IT) {
454 case Func:
455 return "__func__";
456 case Function:
457 return "__FUNCTION__";
458 case FuncDName:
459 return "__FUNCDNAME__";
460 case LFunction:
461 return "L__FUNCTION__";
462 case PrettyFunction:
463 return "__PRETTY_FUNCTION__";
464 case FuncSig:
465 return "__FUNCSIG__";
466 case PrettyFunctionNoVirtual:
467 break;
468 }
469 llvm_unreachable("Unknown ident type for PredefinedExpr");
470}
471
Anders Carlsson2fb08242009-09-08 18:24:21 +0000472// FIXME: Maybe this should use DeclPrinter with a special "print predefined
473// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000474std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
475 ASTContext &Context = CurrentDecl->getASTContext();
476
David Majnemerbed356a2013-11-06 23:31:56 +0000477 if (IT == PredefinedExpr::FuncDName) {
478 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000479 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000480 MC.reset(Context.createMangleContext());
481
482 if (MC->shouldMangleDeclName(ND)) {
483 SmallString<256> Buffer;
484 llvm::raw_svector_ostream Out(Buffer);
485 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
486 MC->mangleCXXCtor(CD, Ctor_Base, Out);
487 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
488 MC->mangleCXXDtor(DD, Dtor_Base, Out);
489 else
490 MC->mangleName(ND, Out);
491
492 Out.flush();
493 if (!Buffer.empty() && Buffer.front() == '\01')
494 return Buffer.substr(1);
495 return Buffer.str();
496 } else
497 return ND->getIdentifier()->getName();
498 }
499 return "";
500 }
Alexey Bataevec474782014-10-09 08:45:04 +0000501 if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
502 std::unique_ptr<MangleContext> MC;
503 MC.reset(Context.createMangleContext());
504 SmallString<256> Buffer;
505 llvm::raw_svector_ostream Out(Buffer);
506 auto DC = CurrentDecl->getDeclContext();
507 if (DC->isFileContext())
508 MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
509 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
510 MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
511 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
512 MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
513 else
514 MC->mangleBlock(DC, BD, Out);
515 return Out.str();
516 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000517 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Reid Kleckner52eddda2014-04-08 18:13:24 +0000518 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual && IT != FuncSig)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000519 return FD->getNameAsString();
520
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000521 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000522 llvm::raw_svector_ostream Out(Name);
523
524 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000525 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000526 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000527 if (MD->isStatic())
528 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000529 }
530
David Blaikiebbafb8a2012-03-11 07:00:24 +0000531 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000532 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000533 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000534
Douglas Gregor11a434a2012-04-10 20:14:15 +0000535 const FunctionDecl *Decl = FD;
536 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
537 Decl = Pattern;
538 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Craig Topper36250ad2014-05-12 05:36:57 +0000539 const FunctionProtoType *FT = nullptr;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000540 if (FD->hasWrittenPrototype())
541 FT = dyn_cast<FunctionProtoType>(AFT);
542
Reid Kleckner52eddda2014-04-08 18:13:24 +0000543 if (IT == FuncSig) {
544 switch (FT->getCallConv()) {
545 case CC_C: POut << "__cdecl "; break;
546 case CC_X86StdCall: POut << "__stdcall "; break;
547 case CC_X86FastCall: POut << "__fastcall "; break;
548 case CC_X86ThisCall: POut << "__thiscall "; break;
Reid Klecknerd7857f02014-10-24 17:42:17 +0000549 case CC_X86VectorCall: POut << "__vectorcall "; break;
Reid Kleckner52eddda2014-04-08 18:13:24 +0000550 // Only bother printing the conventions that MSVC knows about.
551 default: break;
552 }
553 }
554
555 FD->printQualifiedName(POut, Policy);
556
Douglas Gregor11a434a2012-04-10 20:14:15 +0000557 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000558 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000559 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000560 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000561 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000562 }
563
564 if (FT->isVariadic()) {
565 if (FD->getNumParams()) POut << ", ";
566 POut << "...";
567 }
568 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000569 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000570
Sam Weinig4e83bd22009-12-27 01:38:20 +0000571 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000572 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000573 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000574 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000575 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000576 POut << " volatile";
577 RefQualifierKind Ref = MD->getRefQualifier();
578 if (Ref == RQ_LValue)
579 POut << " &";
580 else if (Ref == RQ_RValue)
581 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000582 }
583
Douglas Gregor11a434a2012-04-10 20:14:15 +0000584 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
585 SpecsTy Specs;
586 const DeclContext *Ctx = FD->getDeclContext();
587 while (Ctx && isa<NamedDecl>(Ctx)) {
588 const ClassTemplateSpecializationDecl *Spec
589 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
590 if (Spec && !Spec->isExplicitSpecialization())
591 Specs.push_back(Spec);
592 Ctx = Ctx->getParent();
593 }
594
595 std::string TemplateParams;
596 llvm::raw_string_ostream TOut(TemplateParams);
597 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
598 I != E; ++I) {
599 const TemplateParameterList *Params
600 = (*I)->getSpecializedTemplate()->getTemplateParameters();
601 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
602 assert(Params->size() == Args.size());
603 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
604 StringRef Param = Params->getParam(i)->getName();
605 if (Param.empty()) continue;
606 TOut << Param << " = ";
607 Args.get(i).print(Policy, TOut);
608 TOut << ", ";
609 }
610 }
611
612 FunctionTemplateSpecializationInfo *FSI
613 = FD->getTemplateSpecializationInfo();
614 if (FSI && !FSI->isExplicitSpecialization()) {
615 const TemplateParameterList* Params
616 = FSI->getTemplate()->getTemplateParameters();
617 const TemplateArgumentList* Args = FSI->TemplateArguments;
618 assert(Params->size() == Args->size());
619 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
620 StringRef Param = Params->getParam(i)->getName();
621 if (Param.empty()) continue;
622 TOut << Param << " = ";
623 Args->get(i).print(Policy, TOut);
624 TOut << ", ";
625 }
626 }
627
628 TOut.flush();
629 if (!TemplateParams.empty()) {
630 // remove the trailing comma and space
631 TemplateParams.resize(TemplateParams.size() - 2);
632 POut << " [" << TemplateParams << "]";
633 }
634
635 POut.flush();
636
Benjamin Kramer90f54222013-08-21 11:45:27 +0000637 // Print "auto" for all deduced return types. This includes C++1y return
638 // type deduction and lambdas. For trailing return types resolve the
639 // decltype expression. Otherwise print the real type when this is
640 // not a constructor or destructor.
Alexey Bataevec474782014-10-09 08:45:04 +0000641 if (isa<CXXMethodDecl>(FD) &&
642 cast<CXXMethodDecl>(FD)->getParent()->isLambda())
Benjamin Kramer90f54222013-08-21 11:45:27 +0000643 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000644 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
645 FT->getReturnType()
646 ->getAs<DecltypeType>()
647 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000648 .getAsStringInternal(Proto, Policy);
649 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000650 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000651
652 Out << Proto;
653
654 Out.flush();
655 return Name.str().str();
656 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000657 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
658 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
659 // Skip to its enclosing function or method, but not its enclosing
660 // CapturedDecl.
661 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
662 const Decl *D = Decl::castFromDeclContext(DC);
663 return ComputeName(IT, D);
664 }
665 llvm_unreachable("CapturedDecl not inside a function or method");
666 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000667 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000668 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000669 llvm::raw_svector_ostream Out(Name);
670 Out << (MD->isInstanceMethod() ? '-' : '+');
671 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000672
673 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
674 // a null check to avoid a crash.
675 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000676 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000677
Anders Carlsson2fb08242009-09-08 18:24:21 +0000678 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000679 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000680 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000681
Anders Carlsson2fb08242009-09-08 18:24:21 +0000682 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000683 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000684 Out << ']';
685
686 Out.flush();
687 return Name.str().str();
688 }
689 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
690 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
691 return "top level";
692 }
693 return "";
694}
695
Craig Topper37932912013-08-18 10:09:15 +0000696void APNumericStorage::setIntValue(const ASTContext &C,
697 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000698 if (hasAllocation())
699 C.Deallocate(pVal);
700
701 BitWidth = Val.getBitWidth();
702 unsigned NumWords = Val.getNumWords();
703 const uint64_t* Words = Val.getRawData();
704 if (NumWords > 1) {
705 pVal = new (C) uint64_t[NumWords];
706 std::copy(Words, Words + NumWords, pVal);
707 } else if (NumWords == 1)
708 VAL = Words[0];
709 else
710 VAL = 0;
711}
712
Craig Topper37932912013-08-18 10:09:15 +0000713IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000714 QualType type, SourceLocation l)
715 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
716 false, false),
717 Loc(l) {
718 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
719 assert(V.getBitWidth() == C.getIntWidth(type) &&
720 "Integer type is not the correct size for constant.");
721 setValue(C, V);
722}
723
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000724IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000725IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000726 QualType type, SourceLocation l) {
727 return new (C) IntegerLiteral(C, V, type, l);
728}
729
730IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000731IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000732 return new (C) IntegerLiteral(Empty);
733}
734
Craig Topper37932912013-08-18 10:09:15 +0000735FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000736 bool isexact, QualType Type, SourceLocation L)
737 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
738 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000739 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000740 FloatingLiteralBits.IsExact = isexact;
741 setValue(C, V);
742}
743
Craig Topper37932912013-08-18 10:09:15 +0000744FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000745 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000746 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000747 FloatingLiteralBits.IsExact = false;
748}
749
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000750FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000751FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000752 bool isexact, QualType Type, SourceLocation L) {
753 return new (C) FloatingLiteral(C, V, isexact, Type, L);
754}
755
756FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000757FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000758 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000759}
760
Tim Northover178723a2013-01-22 09:46:51 +0000761const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
762 switch(FloatingLiteralBits.Semantics) {
763 case IEEEhalf:
764 return llvm::APFloat::IEEEhalf;
765 case IEEEsingle:
766 return llvm::APFloat::IEEEsingle;
767 case IEEEdouble:
768 return llvm::APFloat::IEEEdouble;
769 case x87DoubleExtended:
770 return llvm::APFloat::x87DoubleExtended;
771 case IEEEquad:
772 return llvm::APFloat::IEEEquad;
773 case PPCDoubleDouble:
774 return llvm::APFloat::PPCDoubleDouble;
775 }
776 llvm_unreachable("Unrecognised floating semantics");
777}
778
779void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
780 if (&Sem == &llvm::APFloat::IEEEhalf)
781 FloatingLiteralBits.Semantics = IEEEhalf;
782 else if (&Sem == &llvm::APFloat::IEEEsingle)
783 FloatingLiteralBits.Semantics = IEEEsingle;
784 else if (&Sem == &llvm::APFloat::IEEEdouble)
785 FloatingLiteralBits.Semantics = IEEEdouble;
786 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
787 FloatingLiteralBits.Semantics = x87DoubleExtended;
788 else if (&Sem == &llvm::APFloat::IEEEquad)
789 FloatingLiteralBits.Semantics = IEEEquad;
790 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
791 FloatingLiteralBits.Semantics = PPCDoubleDouble;
792 else
793 llvm_unreachable("Unknown floating semantics");
794}
795
Chris Lattnera0173132008-06-07 22:13:43 +0000796/// getValueAsApproximateDouble - This returns the value as an inaccurate
797/// double. Note that this may cause loss of precision, but is useful for
798/// debugging dumps, etc.
799double FloatingLiteral::getValueAsApproximateDouble() const {
800 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000801 bool ignored;
802 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
803 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000804 return V.convertToDouble();
805}
806
Nick Lewycky4ed84042012-02-24 09:07:53 +0000807int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000808 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000809 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000810 case Ascii:
811 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000812 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000813 break;
814 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000815 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000816 break;
817 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000818 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000819 break;
820 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000821 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000822 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000823 }
824 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
825 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000826 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000827 && "character byte widths supported are 1, 2, and 4 only");
828 return CharByteWidth;
829}
830
Craig Topper37932912013-08-18 10:09:15 +0000831StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000832 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000833 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000834 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000835 assert(C.getAsConstantArrayType(Ty) &&
836 "StringLiteral must be of constant array type!");
837
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000838 // Allocate enough space for the StringLiteral plus an array of locations for
839 // any concatenated string tokens.
840 void *Mem = C.Allocate(sizeof(StringLiteral)+
841 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000842 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000843 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Steve Naroffdf7855b2007-02-21 23:46:25 +0000845 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000846 SL->setString(C,Str,Kind,Pascal);
847
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000848 SL->TokLocs[0] = Loc[0];
849 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000850
Chris Lattner630970d2009-02-18 05:49:11 +0000851 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000852 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
853 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000854}
855
Craig Topper37932912013-08-18 10:09:15 +0000856StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
857 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000858 void *Mem = C.Allocate(sizeof(StringLiteral)+
859 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000860 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000861 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000862 SL->CharByteWidth = 0;
863 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000864 SL->NumConcatenated = NumStrs;
865 return SL;
866}
867
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000868void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000869 switch (getKind()) {
870 case Ascii: break; // no prefix.
871 case Wide: OS << 'L'; break;
872 case UTF8: OS << "u8"; break;
873 case UTF16: OS << 'u'; break;
874 case UTF32: OS << 'U'; break;
875 }
876 OS << '"';
877 static const char Hex[] = "0123456789ABCDEF";
878
879 unsigned LastSlashX = getLength();
880 for (unsigned I = 0, N = getLength(); I != N; ++I) {
881 switch (uint32_t Char = getCodeUnit(I)) {
882 default:
883 // FIXME: Convert UTF-8 back to codepoints before rendering.
884
885 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
886 // Leave invalid surrogates alone; we'll use \x for those.
887 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
888 Char <= 0xdbff) {
889 uint32_t Trail = getCodeUnit(I + 1);
890 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
891 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
892 ++I;
893 }
894 }
895
896 if (Char > 0xff) {
897 // If this is a wide string, output characters over 0xff using \x
898 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
899 // codepoint: use \x escapes for invalid codepoints.
900 if (getKind() == Wide ||
901 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
902 // FIXME: Is this the best way to print wchar_t?
903 OS << "\\x";
904 int Shift = 28;
905 while ((Char >> Shift) == 0)
906 Shift -= 4;
907 for (/**/; Shift >= 0; Shift -= 4)
908 OS << Hex[(Char >> Shift) & 15];
909 LastSlashX = I;
910 break;
911 }
912
913 if (Char > 0xffff)
914 OS << "\\U00"
915 << Hex[(Char >> 20) & 15]
916 << Hex[(Char >> 16) & 15];
917 else
918 OS << "\\u";
919 OS << Hex[(Char >> 12) & 15]
920 << Hex[(Char >> 8) & 15]
921 << Hex[(Char >> 4) & 15]
922 << Hex[(Char >> 0) & 15];
923 break;
924 }
925
926 // If we used \x... for the previous character, and this character is a
927 // hexadecimal digit, prevent it being slurped as part of the \x.
928 if (LastSlashX + 1 == I) {
929 switch (Char) {
930 case '0': case '1': case '2': case '3': case '4':
931 case '5': case '6': case '7': case '8': case '9':
932 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
933 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
934 OS << "\"\"";
935 }
936 }
937
938 assert(Char <= 0xff &&
939 "Characters above 0xff should already have been handled.");
940
Jordan Rosea7d03842013-02-08 22:30:41 +0000941 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000942 OS << (char)Char;
943 else // Output anything hard as an octal escape.
944 OS << '\\'
945 << (char)('0' + ((Char >> 6) & 7))
946 << (char)('0' + ((Char >> 3) & 7))
947 << (char)('0' + ((Char >> 0) & 7));
948 break;
949 // Handle some common non-printable cases to make dumps prettier.
950 case '\\': OS << "\\\\"; break;
951 case '"': OS << "\\\""; break;
952 case '\n': OS << "\\n"; break;
953 case '\t': OS << "\\t"; break;
954 case '\a': OS << "\\a"; break;
955 case '\b': OS << "\\b"; break;
956 }
957 }
958 OS << '"';
959}
960
Craig Topper37932912013-08-18 10:09:15 +0000961void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000962 StringKind Kind, bool IsPascal) {
963 //FIXME: we assume that the string data comes from a target that uses the same
964 // code unit size and endianess for the type of string.
965 this->Kind = Kind;
966 this->IsPascal = IsPascal;
967
Nick Lewycky4ed84042012-02-24 09:07:53 +0000968 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000969 assert((Str.size()%CharByteWidth == 0)
970 && "size of data must be multiple of CharByteWidth");
971 Length = Str.size()/CharByteWidth;
972
973 switch(CharByteWidth) {
974 case 1: {
975 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000976 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000977 StrData.asChar = AStrData;
978 break;
979 }
980 case 2: {
981 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000982 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000983 StrData.asUInt16 = AStrData;
984 break;
985 }
986 case 4: {
987 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000988 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000989 StrData.asUInt32 = AStrData;
990 break;
991 }
992 default:
993 assert(false && "unsupported CharByteWidth");
994 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000995}
996
Chris Lattnere925d612010-11-17 07:37:15 +0000997/// getLocationOfByte - Return a source location that points to the specified
998/// byte of this string literal.
999///
1000/// Strings are amazingly complex. They can be formed from multiple tokens and
1001/// can have escape sequences in them in addition to the usual trigraph and
1002/// escaped newline business. This routine handles this complexity.
1003///
1004SourceLocation StringLiteral::
1005getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
1006 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +00001007 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
1008 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +00001009
Chris Lattnere925d612010-11-17 07:37:15 +00001010 // Loop over all of the tokens in this string until we find the one that
1011 // contains the byte we're looking for.
1012 unsigned TokNo = 0;
1013 while (1) {
1014 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
1015 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
1016
1017 // Get the spelling of the string so that we can get the data that makes up
1018 // the string literal, not the identifier for the macro it is potentially
1019 // expanded through.
1020 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
1021
1022 // Re-lex the token to get its length and original spelling.
1023 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
1024 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001025 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001026 if (Invalid)
1027 return StrTokSpellingLoc;
1028
1029 const char *StrData = Buffer.data()+LocInfo.second;
1030
Chris Lattnere925d612010-11-17 07:37:15 +00001031 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001032 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1033 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001034 Token TheTok;
1035 TheLexer.LexFromRawLexer(TheTok);
1036
1037 // Use the StringLiteralParser to compute the length of the string in bytes.
Craig Topper9d5583e2014-06-26 04:58:39 +00001038 StringLiteralParser SLP(TheTok, SM, Features, Target);
Chris Lattnere925d612010-11-17 07:37:15 +00001039 unsigned TokNumBytes = SLP.GetStringLength();
1040
1041 // If the byte is in this token, return the location of the byte.
1042 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001043 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001044 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1045
1046 // Now that we know the offset of the token in the spelling, use the
1047 // preprocessor to get the offset in the original source.
1048 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1049 }
1050
1051 // Move to the next string token.
1052 ++TokNo;
1053 ByteNo -= TokNumBytes;
1054 }
1055}
1056
1057
1058
Chris Lattner1b926492006-08-23 06:42:10 +00001059/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1060/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001061StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001062 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001063 case UO_PostInc: return "++";
1064 case UO_PostDec: return "--";
1065 case UO_PreInc: return "++";
1066 case UO_PreDec: return "--";
1067 case UO_AddrOf: return "&";
1068 case UO_Deref: return "*";
1069 case UO_Plus: return "+";
1070 case UO_Minus: return "-";
1071 case UO_Not: return "~";
1072 case UO_LNot: return "!";
1073 case UO_Real: return "__real";
1074 case UO_Imag: return "__imag";
1075 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001076 }
David Blaikief47fa302012-01-17 02:30:50 +00001077 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001078}
1079
John McCalle3027922010-08-25 11:45:40 +00001080UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001081UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1082 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001083 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001084 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1085 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1086 case OO_Amp: return UO_AddrOf;
1087 case OO_Star: return UO_Deref;
1088 case OO_Plus: return UO_Plus;
1089 case OO_Minus: return UO_Minus;
1090 case OO_Tilde: return UO_Not;
1091 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001092 }
1093}
1094
1095OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1096 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001097 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1098 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1099 case UO_AddrOf: return OO_Amp;
1100 case UO_Deref: return OO_Star;
1101 case UO_Plus: return OO_Plus;
1102 case UO_Minus: return OO_Minus;
1103 case UO_Not: return OO_Tilde;
1104 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001105 default: return OO_None;
1106 }
1107}
1108
1109
Chris Lattner0eedafe2006-08-24 04:56:27 +00001110//===----------------------------------------------------------------------===//
1111// Postfix Operators.
1112//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001113
Craig Topper37932912013-08-18 10:09:15 +00001114CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1115 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1116 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001117 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001118 fn->isTypeDependent(),
1119 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001120 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001121 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001122 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001123
Benjamin Kramerc215e762012-08-24 11:54:20 +00001124 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001125 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001126 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001127 if (args[i]->isTypeDependent())
1128 ExprBits.TypeDependent = true;
1129 if (args[i]->isValueDependent())
1130 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001131 if (args[i]->isInstantiationDependent())
1132 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001133 if (args[i]->containsUnexpandedParameterPack())
1134 ExprBits.ContainsUnexpandedParameterPack = true;
1135
Peter Collingbourne3a347252011-02-08 21:18:02 +00001136 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001137 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001138
Peter Collingbourne3a347252011-02-08 21:18:02 +00001139 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001140 RParenLoc = rparenloc;
1141}
Nate Begeman1e36a852008-01-17 17:46:27 +00001142
Craig Topper37932912013-08-18 10:09:15 +00001143CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001144 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1145 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 fn->isTypeDependent(),
1147 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001148 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001149 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001150 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001151
Benjamin Kramerc215e762012-08-24 11:54:20 +00001152 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001153 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001154 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001155 if (args[i]->isTypeDependent())
1156 ExprBits.TypeDependent = true;
1157 if (args[i]->isValueDependent())
1158 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001159 if (args[i]->isInstantiationDependent())
1160 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001161 if (args[i]->containsUnexpandedParameterPack())
1162 ExprBits.ContainsUnexpandedParameterPack = true;
1163
Peter Collingbourne3a347252011-02-08 21:18:02 +00001164 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001165 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001166
Peter Collingbourne3a347252011-02-08 21:18:02 +00001167 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001168 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001169}
1170
Craig Topper37932912013-08-18 10:09:15 +00001171CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001172 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001173 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001174 SubExprs = new (C) Stmt*[PREARGS_START];
1175 CallExprBits.NumPreArgs = 0;
1176}
1177
Craig Topper37932912013-08-18 10:09:15 +00001178CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001179 EmptyShell Empty)
Craig Topper36250ad2014-05-12 05:36:57 +00001180 : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) {
Peter Collingbourne3a347252011-02-08 21:18:02 +00001181 // FIXME: Why do we allocate this?
1182 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1183 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001184}
1185
Nuno Lopes518e3702009-12-20 23:11:08 +00001186Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001187 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001188
1189 while (SubstNonTypeTemplateParmExpr *NTTP
1190 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1191 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1192 }
1193
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001194 // If we're calling a dereference, look at the pointer instead.
1195 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1196 if (BO->isPtrMemOp())
1197 CEE = BO->getRHS()->IgnoreParenCasts();
1198 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1199 if (UO->getOpcode() == UO_Deref)
1200 CEE = UO->getSubExpr()->IgnoreParenCasts();
1201 }
Chris Lattner52301912009-07-17 15:46:27 +00001202 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001203 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001204 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1205 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001206
Craig Topper36250ad2014-05-12 05:36:57 +00001207 return nullptr;
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001208}
1209
Nuno Lopes518e3702009-12-20 23:11:08 +00001210FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001211 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001212}
1213
Chris Lattnere4407ed2007-12-28 05:25:02 +00001214/// setNumArgs - This changes the number of arguments present in this call.
1215/// Any orphaned expressions are deleted by this, and any new operands are set
1216/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001217void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001218 // No change, just return.
1219 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001220
Chris Lattnere4407ed2007-12-28 05:25:02 +00001221 // If shrinking # arguments, just delete the extras and forgot them.
1222 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001223 this->NumArgs = NumArgs;
1224 return;
1225 }
1226
1227 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001228 unsigned NumPreArgs = getNumPreArgs();
1229 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001230 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001231 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001232 NewSubExprs[i] = SubExprs[i];
1233 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001234 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1235 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001236 NewSubExprs[i] = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001237
Douglas Gregorba6e5572009-04-17 21:46:47 +00001238 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001239 SubExprs = NewSubExprs;
1240 this->NumArgs = NumArgs;
1241}
1242
Alp Tokera724cff2013-12-28 21:59:02 +00001243/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001244/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001245unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001246 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001247 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001248 // ImplicitCastExpr.
1249 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1250 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001251 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001252
Steve Narofff6e3b3292008-01-31 01:07:12 +00001253 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1254 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001255 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001256
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001257 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1258 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001259 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001261 if (!FDecl->getIdentifier())
1262 return 0;
1263
Douglas Gregor15fc9562009-09-12 00:22:50 +00001264 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001265}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001266
Richard Smith5011a002013-01-17 23:46:04 +00001267bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001268 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001269 return Ctx.BuiltinInfo.isUnevaluated(BI);
1270 return false;
1271}
1272
Anders Carlsson00a27592009-05-26 04:57:27 +00001273QualType CallExpr::getCallReturnType() const {
1274 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001275 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001276 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001277 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001278 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001279 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1280 // This should never be overloaded and so should never return null.
1281 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001282
John McCall0009fcc2011-04-26 20:42:42 +00001283 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001284 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001285}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001286
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001287SourceLocation CallExpr::getLocStart() const {
1288 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001289 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001290
1291 SourceLocation begin = getCallee()->getLocStart();
Keno Fischer070db172014-08-15 01:39:12 +00001292 if (begin.isInvalid() && getNumArgs() > 0 && getArg(0))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001293 begin = getArg(0)->getLocStart();
1294 return begin;
1295}
1296SourceLocation CallExpr::getLocEnd() const {
1297 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001298 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001299
1300 SourceLocation end = getRParenLoc();
Keno Fischer070db172014-08-15 01:39:12 +00001301 if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1))
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001302 end = getArg(getNumArgs() - 1)->getLocEnd();
1303 return end;
1304}
John McCall701417a2011-02-21 06:23:05 +00001305
Craig Topper37932912013-08-18 10:09:15 +00001306OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001307 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001308 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001309 ArrayRef<OffsetOfNode> comps,
1310 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001311 SourceLocation RParenLoc) {
1312 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001313 sizeof(OffsetOfNode) * comps.size() +
1314 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001315
Benjamin Kramerc215e762012-08-24 11:54:20 +00001316 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1317 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001318}
1319
Craig Topper37932912013-08-18 10:09:15 +00001320OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001321 unsigned numComps, unsigned numExprs) {
1322 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1323 sizeof(OffsetOfNode) * numComps +
1324 sizeof(Expr*) * numExprs);
1325 return new (Mem) OffsetOfExpr(numComps, numExprs);
1326}
1327
Craig Topper37932912013-08-18 10:09:15 +00001328OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001329 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001330 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001331 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001332 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1333 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001334 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001335 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001336 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001337 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001338 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001339{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001340 for (unsigned i = 0; i != comps.size(); ++i) {
1341 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001342 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001343
Benjamin Kramerc215e762012-08-24 11:54:20 +00001344 for (unsigned i = 0; i != exprs.size(); ++i) {
1345 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001346 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001347 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001348 ExprBits.ContainsUnexpandedParameterPack = true;
1349
Benjamin Kramerc215e762012-08-24 11:54:20 +00001350 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001351 }
1352}
1353
1354IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1355 assert(getKind() == Field || getKind() == Identifier);
1356 if (getKind() == Field)
1357 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001358
Douglas Gregor882211c2010-04-28 22:16:22 +00001359 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1360}
1361
Craig Topper37932912013-08-18 10:09:15 +00001362MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001363 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001364 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001365 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001366 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001367 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001368 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001369 QualType ty,
1370 ExprValueKind vk,
1371 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001372 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001373
Douglas Gregorea972d32011-02-28 21:54:11 +00001374 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001375 founddecl.getDecl() != memberdecl ||
1376 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001377 if (hasQualOrFound)
1378 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001379
John McCall6b51f282009-11-23 01:53:49 +00001380 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001381 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1382 else if (TemplateKWLoc.isValid())
1383 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001384
Chris Lattner5c0b4052010-10-30 05:14:06 +00001385 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001386 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1387 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001388
1389 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001390 // FIXME: Wrong. We should be looking at the member declaration we found.
1391 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001392 E->setValueDependent(true);
1393 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001394 E->setInstantiationDependent(true);
1395 }
1396 else if (QualifierLoc &&
1397 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1398 E->setInstantiationDependent(true);
1399
John McCall16df1e52010-03-30 21:47:33 +00001400 E->HasQualifierOrFoundDecl = true;
1401
1402 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001403 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001404 NQ->FoundDecl = founddecl;
1405 }
1406
Abramo Bagnara7945c982012-01-27 09:46:47 +00001407 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1408
John McCall16df1e52010-03-30 21:47:33 +00001409 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001410 bool Dependent = false;
1411 bool InstantiationDependent = false;
1412 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001413 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1414 Dependent,
1415 InstantiationDependent,
1416 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001417 if (InstantiationDependent)
1418 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001419 } else if (TemplateKWLoc.isValid()) {
1420 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001421 }
1422
1423 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001424}
1425
Daniel Dunbarb507f272012-03-09 15:39:15 +00001426SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001427 if (isImplicitAccess()) {
1428 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001429 return getQualifierLoc().getBeginLoc();
1430 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001431 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001432
Daniel Dunbarb507f272012-03-09 15:39:15 +00001433 // FIXME: We don't want this to happen. Rather, we should be able to
1434 // detect all kinds of implicit accesses more cleanly.
1435 SourceLocation BaseStartLoc = getBase()->getLocStart();
1436 if (BaseStartLoc.isValid())
1437 return BaseStartLoc;
1438 return MemberLoc;
1439}
1440SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001441 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001442 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001443 EndLoc = getRAngleLoc();
1444 else if (EndLoc.isInvalid())
1445 EndLoc = getBase()->getLocEnd();
1446 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001447}
1448
Alp Tokerc1086762013-12-07 13:51:35 +00001449bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001450 switch (getCastKind()) {
1451 case CK_DerivedToBase:
1452 case CK_UncheckedDerivedToBase:
1453 case CK_DerivedToBaseMemberPointer:
1454 case CK_BaseToDerived:
1455 case CK_BaseToDerivedMemberPointer:
1456 assert(!path_empty() && "Cast kind should have a base path!");
1457 break;
1458
1459 case CK_CPointerToObjCPointerCast:
1460 assert(getType()->isObjCObjectPointerType());
1461 assert(getSubExpr()->getType()->isPointerType());
1462 goto CheckNoBasePath;
1463
1464 case CK_BlockPointerToObjCPointerCast:
1465 assert(getType()->isObjCObjectPointerType());
1466 assert(getSubExpr()->getType()->isBlockPointerType());
1467 goto CheckNoBasePath;
1468
John McCallc62bb392012-02-15 01:22:51 +00001469 case CK_ReinterpretMemberPointer:
1470 assert(getType()->isMemberPointerType());
1471 assert(getSubExpr()->getType()->isMemberPointerType());
1472 goto CheckNoBasePath;
1473
John McCall9320b872011-09-09 05:25:32 +00001474 case CK_BitCast:
1475 // Arbitrary casts to C pointer types count as bitcasts.
1476 // Otherwise, we should only have block and ObjC pointer casts
1477 // here if they stay within the type kind.
1478 if (!getType()->isPointerType()) {
1479 assert(getType()->isObjCObjectPointerType() ==
1480 getSubExpr()->getType()->isObjCObjectPointerType());
1481 assert(getType()->isBlockPointerType() ==
1482 getSubExpr()->getType()->isBlockPointerType());
1483 }
1484 goto CheckNoBasePath;
1485
1486 case CK_AnyPointerToBlockPointerCast:
1487 assert(getType()->isBlockPointerType());
1488 assert(getSubExpr()->getType()->isAnyPointerType() &&
1489 !getSubExpr()->getType()->isBlockPointerType());
1490 goto CheckNoBasePath;
1491
Douglas Gregored90df32012-02-22 05:02:47 +00001492 case CK_CopyAndAutoreleaseBlockObject:
1493 assert(getType()->isBlockPointerType());
1494 assert(getSubExpr()->getType()->isBlockPointerType());
1495 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001496
1497 case CK_FunctionToPointerDecay:
1498 assert(getType()->isPointerType());
1499 assert(getSubExpr()->getType()->isFunctionType());
1500 goto CheckNoBasePath;
1501
David Tweede1468322013-12-11 13:39:46 +00001502 case CK_AddressSpaceConversion:
1503 assert(getType()->isPointerType());
1504 assert(getSubExpr()->getType()->isPointerType());
1505 assert(getType()->getPointeeType().getAddressSpace() !=
1506 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001507 // These should not have an inheritance path.
1508 case CK_Dynamic:
1509 case CK_ToUnion:
1510 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001511 case CK_NullToMemberPointer:
1512 case CK_NullToPointer:
1513 case CK_ConstructorConversion:
1514 case CK_IntegralToPointer:
1515 case CK_PointerToIntegral:
1516 case CK_ToVoid:
1517 case CK_VectorSplat:
1518 case CK_IntegralCast:
1519 case CK_IntegralToFloating:
1520 case CK_FloatingToIntegral:
1521 case CK_FloatingCast:
1522 case CK_ObjCObjectLValueCast:
1523 case CK_FloatingRealToComplex:
1524 case CK_FloatingComplexToReal:
1525 case CK_FloatingComplexCast:
1526 case CK_FloatingComplexToIntegralComplex:
1527 case CK_IntegralRealToComplex:
1528 case CK_IntegralComplexToReal:
1529 case CK_IntegralComplexCast:
1530 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001531 case CK_ARCProduceObject:
1532 case CK_ARCConsumeObject:
1533 case CK_ARCReclaimReturnedObject:
1534 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001535 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001536 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1537 goto CheckNoBasePath;
1538
1539 case CK_Dependent:
1540 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001541 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001542 case CK_AtomicToNonAtomic:
1543 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001544 case CK_PointerToBoolean:
1545 case CK_IntegralToBoolean:
1546 case CK_FloatingToBoolean:
1547 case CK_MemberPointerToBoolean:
1548 case CK_FloatingComplexToBoolean:
1549 case CK_IntegralComplexToBoolean:
1550 case CK_LValueBitCast: // -> bool&
1551 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001552 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001553 CheckNoBasePath:
1554 assert(path_empty() && "Cast kind should not have a base path!");
1555 break;
1556 }
Alp Tokerc1086762013-12-07 13:51:35 +00001557 return true;
John McCall9320b872011-09-09 05:25:32 +00001558}
1559
Anders Carlsson496335e2009-09-03 00:59:21 +00001560const char *CastExpr::getCastKindName() const {
1561 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001562 case CK_Dependent:
1563 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001564 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001565 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001566 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001567 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001568 case CK_LValueToRValue:
1569 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001570 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001571 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001572 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001573 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001574 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001575 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001576 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001577 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001578 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001579 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001580 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001581 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001582 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001583 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001584 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001585 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001586 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001587 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001588 case CK_NullToPointer:
1589 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001590 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001591 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001592 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001593 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001594 case CK_ReinterpretMemberPointer:
1595 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001596 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001597 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001598 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001599 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001600 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001601 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001602 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001603 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001604 case CK_PointerToBoolean:
1605 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001606 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001607 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001608 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001609 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001610 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001611 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001612 case CK_IntegralToBoolean:
1613 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001614 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001615 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001616 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001617 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001618 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001619 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001620 case CK_FloatingToBoolean:
1621 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001622 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001623 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001624 case CK_CPointerToObjCPointerCast:
1625 return "CPointerToObjCPointerCast";
1626 case CK_BlockPointerToObjCPointerCast:
1627 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001628 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001629 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001630 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001631 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001632 case CK_FloatingRealToComplex:
1633 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001634 case CK_FloatingComplexToReal:
1635 return "FloatingComplexToReal";
1636 case CK_FloatingComplexToBoolean:
1637 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001638 case CK_FloatingComplexCast:
1639 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001640 case CK_FloatingComplexToIntegralComplex:
1641 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001642 case CK_IntegralRealToComplex:
1643 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001644 case CK_IntegralComplexToReal:
1645 return "IntegralComplexToReal";
1646 case CK_IntegralComplexToBoolean:
1647 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001648 case CK_IntegralComplexCast:
1649 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001650 case CK_IntegralComplexToFloatingComplex:
1651 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001652 case CK_ARCConsumeObject:
1653 return "ARCConsumeObject";
1654 case CK_ARCProduceObject:
1655 return "ARCProduceObject";
1656 case CK_ARCReclaimReturnedObject:
1657 return "ARCReclaimReturnedObject";
1658 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001659 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001660 case CK_AtomicToNonAtomic:
1661 return "AtomicToNonAtomic";
1662 case CK_NonAtomicToAtomic:
1663 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001664 case CK_CopyAndAutoreleaseBlockObject:
1665 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001666 case CK_BuiltinFnToFnPtr:
1667 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001668 case CK_ZeroToOCLEvent:
1669 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001670 case CK_AddressSpaceConversion:
1671 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001672 }
Mike Stump11289f42009-09-09 15:08:12 +00001673
John McCallc5e62b42010-11-13 09:02:35 +00001674 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001675}
1676
Douglas Gregord196a582009-12-14 19:27:10 +00001677Expr *CastExpr::getSubExprAsWritten() {
Craig Topper36250ad2014-05-12 05:36:57 +00001678 Expr *SubExpr = nullptr;
Douglas Gregord196a582009-12-14 19:27:10 +00001679 CastExpr *E = this;
1680 do {
1681 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001682
1683 // Skip through reference binding to temporary.
1684 if (MaterializeTemporaryExpr *Materialize
1685 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1686 SubExpr = Materialize->GetTemporaryExpr();
1687
Douglas Gregord196a582009-12-14 19:27:10 +00001688 // Skip any temporary bindings; they're implicit.
1689 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1690 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001691
Douglas Gregord196a582009-12-14 19:27:10 +00001692 // Conversions by constructor and conversion functions have a
1693 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001694 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001695 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001696 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001697 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001698
Douglas Gregord196a582009-12-14 19:27:10 +00001699 // If the subexpression we're left with is an implicit cast, look
1700 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001701 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1702
Douglas Gregord196a582009-12-14 19:27:10 +00001703 return SubExpr;
1704}
1705
John McCallcf142162010-08-07 06:22:56 +00001706CXXBaseSpecifier **CastExpr::path_buffer() {
1707 switch (getStmtClass()) {
1708#define ABSTRACT_STMT(x)
1709#define CASTEXPR(Type, Base) \
1710 case Stmt::Type##Class: \
1711 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1712#define STMT(Type, Base)
1713#include "clang/AST/StmtNodes.inc"
1714 default:
1715 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001716 }
1717}
1718
1719void CastExpr::setCastPath(const CXXCastPath &Path) {
1720 assert(Path.size() == path_size());
1721 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1722}
1723
Craig Topper37932912013-08-18 10:09:15 +00001724ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001725 CastKind Kind, Expr *Operand,
1726 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001727 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001728 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1729 void *Buffer =
1730 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1731 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001732 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001733 if (PathSize) E->setCastPath(*BasePath);
1734 return E;
1735}
1736
Craig Topper37932912013-08-18 10:09:15 +00001737ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001738 unsigned PathSize) {
1739 void *Buffer =
1740 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1741 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1742}
1743
1744
Craig Topper37932912013-08-18 10:09:15 +00001745CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001746 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001747 const CXXCastPath *BasePath,
1748 TypeSourceInfo *WrittenTy,
1749 SourceLocation L, SourceLocation R) {
1750 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1751 void *Buffer =
1752 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1753 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001754 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001755 if (PathSize) E->setCastPath(*BasePath);
1756 return E;
1757}
1758
Craig Topper37932912013-08-18 10:09:15 +00001759CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1760 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001761 void *Buffer =
1762 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1763 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1764}
1765
Chris Lattner1b926492006-08-23 06:42:10 +00001766/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1767/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001768StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001769 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001770 case BO_PtrMemD: return ".*";
1771 case BO_PtrMemI: return "->*";
1772 case BO_Mul: return "*";
1773 case BO_Div: return "/";
1774 case BO_Rem: return "%";
1775 case BO_Add: return "+";
1776 case BO_Sub: return "-";
1777 case BO_Shl: return "<<";
1778 case BO_Shr: return ">>";
1779 case BO_LT: return "<";
1780 case BO_GT: return ">";
1781 case BO_LE: return "<=";
1782 case BO_GE: return ">=";
1783 case BO_EQ: return "==";
1784 case BO_NE: return "!=";
1785 case BO_And: return "&";
1786 case BO_Xor: return "^";
1787 case BO_Or: return "|";
1788 case BO_LAnd: return "&&";
1789 case BO_LOr: return "||";
1790 case BO_Assign: return "=";
1791 case BO_MulAssign: return "*=";
1792 case BO_DivAssign: return "/=";
1793 case BO_RemAssign: return "%=";
1794 case BO_AddAssign: return "+=";
1795 case BO_SubAssign: return "-=";
1796 case BO_ShlAssign: return "<<=";
1797 case BO_ShrAssign: return ">>=";
1798 case BO_AndAssign: return "&=";
1799 case BO_XorAssign: return "^=";
1800 case BO_OrAssign: return "|=";
1801 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001802 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001803
David Blaikiee4d798f2012-01-20 21:50:17 +00001804 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001805}
Steve Naroff47500512007-04-19 23:00:49 +00001806
John McCalle3027922010-08-25 11:45:40 +00001807BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001808BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1809 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001810 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001811 case OO_Plus: return BO_Add;
1812 case OO_Minus: return BO_Sub;
1813 case OO_Star: return BO_Mul;
1814 case OO_Slash: return BO_Div;
1815 case OO_Percent: return BO_Rem;
1816 case OO_Caret: return BO_Xor;
1817 case OO_Amp: return BO_And;
1818 case OO_Pipe: return BO_Or;
1819 case OO_Equal: return BO_Assign;
1820 case OO_Less: return BO_LT;
1821 case OO_Greater: return BO_GT;
1822 case OO_PlusEqual: return BO_AddAssign;
1823 case OO_MinusEqual: return BO_SubAssign;
1824 case OO_StarEqual: return BO_MulAssign;
1825 case OO_SlashEqual: return BO_DivAssign;
1826 case OO_PercentEqual: return BO_RemAssign;
1827 case OO_CaretEqual: return BO_XorAssign;
1828 case OO_AmpEqual: return BO_AndAssign;
1829 case OO_PipeEqual: return BO_OrAssign;
1830 case OO_LessLess: return BO_Shl;
1831 case OO_GreaterGreater: return BO_Shr;
1832 case OO_LessLessEqual: return BO_ShlAssign;
1833 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1834 case OO_EqualEqual: return BO_EQ;
1835 case OO_ExclaimEqual: return BO_NE;
1836 case OO_LessEqual: return BO_LE;
1837 case OO_GreaterEqual: return BO_GE;
1838 case OO_AmpAmp: return BO_LAnd;
1839 case OO_PipePipe: return BO_LOr;
1840 case OO_Comma: return BO_Comma;
1841 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001842 }
1843}
1844
1845OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1846 static const OverloadedOperatorKind OverOps[] = {
1847 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1848 OO_Star, OO_Slash, OO_Percent,
1849 OO_Plus, OO_Minus,
1850 OO_LessLess, OO_GreaterGreater,
1851 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1852 OO_EqualEqual, OO_ExclaimEqual,
1853 OO_Amp,
1854 OO_Caret,
1855 OO_Pipe,
1856 OO_AmpAmp,
1857 OO_PipePipe,
1858 OO_Equal, OO_StarEqual,
1859 OO_SlashEqual, OO_PercentEqual,
1860 OO_PlusEqual, OO_MinusEqual,
1861 OO_LessLessEqual, OO_GreaterGreaterEqual,
1862 OO_AmpEqual, OO_CaretEqual,
1863 OO_PipeEqual,
1864 OO_Comma
1865 };
1866 return OverOps[Opc];
1867}
1868
Craig Topper37932912013-08-18 10:09:15 +00001869InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001870 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001871 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001872 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001873 InitExprs(C, initExprs.size()),
Craig Topper36250ad2014-05-12 05:36:57 +00001874 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001875{
1876 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001877 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001878 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001879 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001880 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001881 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001882 if (initExprs[I]->isInstantiationDependent())
1883 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001884 if (initExprs[I]->containsUnexpandedParameterPack())
1885 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001886 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001887
Benjamin Kramerc215e762012-08-24 11:54:20 +00001888 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001889}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001890
Craig Topper37932912013-08-18 10:09:15 +00001891void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001892 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001893 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001894}
1895
Craig Topper37932912013-08-18 10:09:15 +00001896void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Craig Topper36250ad2014-05-12 05:36:57 +00001897 InitExprs.resize(C, NumInits, nullptr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001898}
1899
Craig Topper37932912013-08-18 10:09:15 +00001900Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001901 if (Init >= InitExprs.size()) {
Craig Topper36250ad2014-05-12 05:36:57 +00001902 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr);
Richard Smithc275da62013-12-06 01:27:24 +00001903 setInit(Init, expr);
Craig Topper36250ad2014-05-12 05:36:57 +00001904 return nullptr;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001905 }
Mike Stump11289f42009-09-09 15:08:12 +00001906
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001907 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001908 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001909 return Result;
1910}
1911
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001912void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001913 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001914 ArrayFillerOrUnionFieldInit = filler;
1915 // Fill out any "holes" in the array due to designated initializers.
1916 Expr **inits = getInits();
1917 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
Craig Topper36250ad2014-05-12 05:36:57 +00001918 if (inits[i] == nullptr)
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001919 inits[i] = filler;
1920}
1921
Richard Smith9ec1e482012-04-15 02:50:59 +00001922bool InitListExpr::isStringLiteralInit() const {
1923 if (getNumInits() != 1)
1924 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001925 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1926 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001927 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001928 // It is possible for getInit() to return null.
1929 const Expr *Init = getInit(0);
1930 if (!Init)
1931 return false;
1932 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001933 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1934}
1935
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001936SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001937 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001938 return SyntacticForm->getLocStart();
1939 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001940 if (Beg.isInvalid()) {
1941 // Find the first non-null initializer.
1942 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1943 E = InitExprs.end();
1944 I != E; ++I) {
1945 if (Stmt *S = *I) {
1946 Beg = S->getLocStart();
1947 break;
1948 }
1949 }
1950 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001951 return Beg;
1952}
1953
1954SourceLocation InitListExpr::getLocEnd() const {
1955 if (InitListExpr *SyntacticForm = getSyntacticForm())
1956 return SyntacticForm->getLocEnd();
1957 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001958 if (End.isInvalid()) {
1959 // Find the first non-null initializer from the end.
1960 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001961 E = InitExprs.rend();
1962 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001963 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001964 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001965 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001966 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001967 }
1968 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001969 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001970}
1971
Steve Naroff991e99d2008-09-04 15:31:07 +00001972/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001973///
John McCallc833dea2012-02-17 03:32:35 +00001974const FunctionProtoType *BlockExpr::getFunctionType() const {
1975 // The block pointer is never sugared, but the function type might be.
1976 return cast<BlockPointerType>(getType())
1977 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001978}
1979
Mike Stump11289f42009-09-09 15:08:12 +00001980SourceLocation BlockExpr::getCaretLocation() const {
1981 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001982}
Mike Stump11289f42009-09-09 15:08:12 +00001983const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001984 return TheBlock->getBody();
1985}
Mike Stump11289f42009-09-09 15:08:12 +00001986Stmt *BlockExpr::getBody() {
1987 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001988}
Steve Naroff415d3d52008-10-08 17:01:13 +00001989
1990
Chris Lattner1ec5f562007-06-27 05:38:08 +00001991//===----------------------------------------------------------------------===//
1992// Generic Expression Routines
1993//===----------------------------------------------------------------------===//
1994
Chris Lattner237f2752009-02-14 07:37:35 +00001995/// isUnusedResultAWarning - Return true if this immediate expression should
1996/// be warned about if the result is unused. If so, fill in Loc and Ranges
1997/// with location to warn on and the source range[s] to report with the
1998/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001999bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
2000 SourceRange &R1, SourceRange &R2,
2001 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00002002 // Don't warn if the expr is type dependent. The type could end up
2003 // instantiating to void.
2004 if (isTypeDependent())
2005 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002006
Chris Lattner1ec5f562007-06-27 05:38:08 +00002007 switch (getStmtClass()) {
2008 default:
John McCallc493a732010-03-12 07:11:26 +00002009 if (getType()->isVoidType())
2010 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002011 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002012 Loc = getExprLoc();
2013 R1 = getSourceRange();
2014 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002015 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002016 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002017 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00002018 case GenericSelectionExprClass:
2019 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00002020 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00002021 case ChooseExprClass:
2022 return cast<ChooseExpr>(this)->getChosenSubExpr()->
2023 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002024 case UnaryOperatorClass: {
2025 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattner1ec5f562007-06-27 05:38:08 +00002027 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002028 case UO_Plus:
2029 case UO_Minus:
2030 case UO_AddrOf:
2031 case UO_Not:
2032 case UO_LNot:
2033 case UO_Deref:
2034 break;
John McCalle3027922010-08-25 11:45:40 +00002035 case UO_PostInc:
2036 case UO_PostDec:
2037 case UO_PreInc:
2038 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002039 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002040 case UO_Real:
2041 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002042 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002043 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2044 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002045 return false;
2046 break;
John McCalle3027922010-08-25 11:45:40 +00002047 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002048 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002049 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002050 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002051 Loc = UO->getOperatorLoc();
2052 R1 = UO->getSubExpr()->getSourceRange();
2053 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002054 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002055 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002056 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002057 switch (BO->getOpcode()) {
2058 default:
2059 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002060 // Consider the RHS of comma for side effects. LHS was checked by
2061 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002062 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002063 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2064 // lvalue-ness) of an assignment written in a macro.
2065 if (IntegerLiteral *IE =
2066 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2067 if (IE->getValue() == 0)
2068 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002069 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002070 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002071 case BO_LAnd:
2072 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002073 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2074 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002075 return false;
2076 break;
John McCall1e3715a2010-02-16 04:10:53 +00002077 }
Chris Lattner237f2752009-02-14 07:37:35 +00002078 if (BO->isAssignmentOp())
2079 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002080 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002081 Loc = BO->getOperatorLoc();
2082 R1 = BO->getLHS()->getSourceRange();
2083 R2 = BO->getRHS()->getSourceRange();
2084 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002085 }
Chris Lattner86928112007-08-25 02:00:02 +00002086 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002087 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002088 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002089 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002090
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002091 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002092 // If only one of the LHS or RHS is a warning, the operator might
2093 // be being used for control flow. Only warn if both the LHS and
2094 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002095 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002096 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002097 return false;
2098 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002099 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002100 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002101 }
2102
Chris Lattnera44d1162007-06-27 05:58:59 +00002103 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002104 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002105 Loc = cast<MemberExpr>(this)->getMemberLoc();
2106 R1 = SourceRange(Loc, Loc);
2107 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2108 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002109
Chris Lattner1ec5f562007-06-27 05:38:08 +00002110 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002111 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002112 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2113 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2114 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2115 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002116
Chandler Carruth46339472011-08-17 09:49:44 +00002117 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002118 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002119 // overloads as there is no reasonable way to define these such that they
2120 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002121 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002122 // provides additional value as well. If this list is updated,
2123 // DiagnoseUnusedComparison should be as well.
2124 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002125 switch (Op->getOperator()) {
2126 default:
2127 break;
2128 case OO_EqualEqual:
2129 case OO_ExclaimEqual:
2130 case OO_Less:
2131 case OO_Greater:
2132 case OO_GreaterEqual:
2133 case OO_LessEqual:
Richard Trieuccedd522014-05-20 01:34:43 +00002134 if (Op->getCallReturnType()->isReferenceType() ||
2135 Op->getCallReturnType()->isVoidType())
Richard Trieu161132b2014-05-14 23:22:10 +00002136 break;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002137 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002138 Loc = Op->getOperatorLoc();
2139 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002140 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002141 }
Chandler Carruth46339472011-08-17 09:49:44 +00002142
2143 // Fallthrough for generic call handling.
2144 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002145 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002146 case CXXMemberCallExprClass:
2147 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002148 // If this is a direct call, get the callee.
2149 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002150 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002151 // If the callee has attribute pure, const, or warn_unused_result, warn
2152 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002153 //
2154 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2155 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002156 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2157 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002158 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002159 Loc = CE->getCallee()->getLocStart();
2160 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002161
Chris Lattner1a6babf2009-10-13 04:53:48 +00002162 if (unsigned NumArgs = CE->getNumArgs())
2163 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2164 CE->getArg(NumArgs-1)->getLocEnd());
2165 return true;
2166 }
Chris Lattner237f2752009-02-14 07:37:35 +00002167 }
2168 return false;
2169 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002170
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002171 // If we don't know precisely what we're looking at, let's not warn.
2172 case UnresolvedLookupExprClass:
2173 case CXXUnresolvedConstructExprClass:
2174 return false;
2175
Anders Carlsson6aa50392009-11-17 17:11:23 +00002176 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002177 case CXXConstructExprClass: {
2178 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2179 if (Type->hasAttr<WarnUnusedAttr>()) {
2180 WarnE = this;
2181 Loc = getLocStart();
2182 R1 = getSourceRange();
2183 return true;
2184 }
2185 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002186 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002187 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002188
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002189 case ObjCMessageExprClass: {
2190 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002191 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002192 ME->isInstanceMessage() &&
2193 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002194 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002195 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002196 Loc = getExprLoc();
2197 R1 = ME->getSourceRange();
2198 return true;
2199 }
2200
Fariborz Jahanianac6b4ef2014-07-18 22:59:10 +00002201 if (const ObjCMethodDecl *MD = ME->getMethodDecl())
2202 if (MD->hasAttr<WarnUnusedResultAttr>() ||
2203 (MD->isPropertyAccessor() && !MD->getReturnType()->isVoidType() &&
2204 !ME->getReceiverType()->isObjCIdType())) {
2205 WarnE = this;
2206 Loc = getExprLoc();
2207 return true;
2208 }
2209
Chris Lattner237f2752009-02-14 07:37:35 +00002210 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002211 }
Mike Stump11289f42009-09-09 15:08:12 +00002212
John McCallb7bd14f2010-12-02 01:19:52 +00002213 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002214 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002215 Loc = getExprLoc();
2216 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002217 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002218
John McCallfe96e0b2011-11-06 09:01:30 +00002219 case PseudoObjectExprClass: {
2220 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2221
2222 // Only complain about things that have the form of a getter.
2223 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2224 isa<BinaryOperator>(PO->getSyntacticForm()))
2225 return false;
2226
Eli Friedmanc11535c2012-05-24 00:47:05 +00002227 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002228 Loc = getExprLoc();
2229 R1 = getSourceRange();
2230 return true;
2231 }
2232
Chris Lattner944d3062008-07-26 19:51:01 +00002233 case StmtExprClass: {
2234 // Statement exprs don't logically have side effects themselves, but are
2235 // sometimes used in macros in ways that give them a type that is unused.
2236 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2237 // however, if the result of the stmt expr is dead, we don't want to emit a
2238 // warning.
2239 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002240 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002241 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002242 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002243 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2244 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002245 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002246 }
Mike Stump11289f42009-09-09 15:08:12 +00002247
John McCallc493a732010-03-12 07:11:26 +00002248 if (getType()->isVoidType())
2249 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002250 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002251 Loc = cast<StmtExpr>(this)->getLParenLoc();
2252 R1 = getSourceRange();
2253 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002254 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002255 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002256 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002257 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002258 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002259 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002260 if (CE->getCastKind() == CK_ToVoid) {
2261 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002262 CE->getSubExpr()->getType().isVolatileQualified()) {
2263 const DeclRefExpr *DRE =
2264 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2265 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2266 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2267 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2268 R1, R2, Ctx);
2269 }
2270 }
Chris Lattner2706a552009-07-28 18:25:28 +00002271 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002272 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002273
Eli Friedmanc11535c2012-05-24 00:47:05 +00002274 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002275 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002276 if (CE->getCastKind() == CK_ConstructorConversion)
2277 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002278
Eli Friedmanc11535c2012-05-24 00:47:05 +00002279 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002280 if (const CXXFunctionalCastExpr *CXXCE =
2281 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002282 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002283 R1 = CXXCE->getSubExpr()->getSourceRange();
2284 } else {
2285 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2286 Loc = CStyleCE->getLParenLoc();
2287 R1 = CStyleCE->getSubExpr()->getSourceRange();
2288 }
Chris Lattner237f2752009-02-14 07:37:35 +00002289 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002290 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002291 case ImplicitCastExprClass: {
2292 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002293
Eli Friedmanc11535c2012-05-24 00:47:05 +00002294 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2295 if (ICE->getCastKind() == CK_LValueToRValue &&
2296 ICE->getSubExpr()->getType().isVolatileQualified())
2297 return false;
2298
2299 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2300 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002301 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002302 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002303 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002304 case CXXDefaultInitExprClass:
2305 return (cast<CXXDefaultInitExpr>(this)
2306 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002307
2308 case CXXNewExprClass:
2309 // FIXME: In theory, there might be new expressions that don't have side
2310 // effects (e.g. a placement new with an uninitialized POD).
2311 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002312 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002313 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002314 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002315 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002316 case ExprWithCleanupsClass:
2317 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002318 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002319 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002320}
2321
Fariborz Jahanian07735332009-02-22 18:40:18 +00002322/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002323/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002324bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002325 const Expr *E = IgnoreParens();
2326 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002327 default:
2328 return false;
2329 case ObjCIvarRefExprClass:
2330 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002331 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002332 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002333 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002334 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002335 case MaterializeTemporaryExprClass:
2336 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2337 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002338 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002339 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002340 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002341 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002342
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002343 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2344 if (VD->hasGlobalStorage())
2345 return true;
2346 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002347 // dereferencing to a pointer is always a gc'able candidate,
2348 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002349 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002350 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002351 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002352 return false;
2353 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002354 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002355 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002356 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002357 }
2358 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002359 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002360 }
2361}
Sebastian Redlce354af2010-09-10 20:55:33 +00002362
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002363bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2364 if (isTypeDependent())
2365 return false;
John McCall086a4642010-11-24 05:12:34 +00002366 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002367}
2368
John McCall0009fcc2011-04-26 20:42:42 +00002369QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002370 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002371
2372 // Bound member expressions are always one of these possibilities:
2373 // x->m x.m x->*y x.*y
2374 // (possibly parenthesized)
2375
2376 expr = expr->IgnoreParens();
2377 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2378 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2379 return mem->getMemberDecl()->getType();
2380 }
2381
2382 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2383 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2384 ->getPointeeType();
2385 assert(type->isFunctionType());
2386 return type;
2387 }
2388
2389 assert(isa<UnresolvedMemberExpr>(expr));
2390 return QualType();
2391}
2392
Ted Kremenekfff70962008-01-17 16:57:34 +00002393Expr* Expr::IgnoreParens() {
2394 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002395 while (true) {
2396 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2397 E = P->getSubExpr();
2398 continue;
2399 }
2400 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2401 if (P->getOpcode() == UO_Extension) {
2402 E = P->getSubExpr();
2403 continue;
2404 }
2405 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002406 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2407 if (!P->isResultDependent()) {
2408 E = P->getResultExpr();
2409 continue;
2410 }
2411 }
Eli Friedman75807f22013-07-20 00:40:58 +00002412 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2413 if (!P->isConditionDependent()) {
2414 E = P->getChosenSubExpr();
2415 continue;
2416 }
2417 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002418 return E;
2419 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002420}
2421
Chris Lattnerf2660962008-02-13 01:02:39 +00002422/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2423/// or CastExprs or ImplicitCastExprs, returning their operand.
2424Expr *Expr::IgnoreParenCasts() {
2425 Expr *E = this;
2426 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002427 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002428 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002429 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002430 continue;
2431 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002432 if (MaterializeTemporaryExpr *Materialize
2433 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2434 E = Materialize->GetTemporaryExpr();
2435 continue;
2436 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002437 if (SubstNonTypeTemplateParmExpr *NTTP
2438 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2439 E = NTTP->getReplacement();
2440 continue;
2441 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002442 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002443 }
2444}
2445
Ted Kremenek6f375e52014-04-16 07:26:09 +00002446Expr *Expr::IgnoreCasts() {
2447 Expr *E = this;
2448 while (true) {
2449 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2450 E = P->getSubExpr();
2451 continue;
2452 }
2453 if (MaterializeTemporaryExpr *Materialize
2454 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2455 E = Materialize->GetTemporaryExpr();
2456 continue;
2457 }
2458 if (SubstNonTypeTemplateParmExpr *NTTP
2459 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2460 E = NTTP->getReplacement();
2461 continue;
2462 }
2463 return E;
2464 }
2465}
2466
John McCall5a4ce8b2010-12-04 08:24:19 +00002467/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2468/// casts. This is intended purely as a temporary workaround for code
2469/// that hasn't yet been rewritten to do the right thing about those
2470/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002471Expr *Expr::IgnoreParenLValueCasts() {
2472 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002473 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002474 E = E->IgnoreParens();
2475 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002476 if (P->getCastKind() == CK_LValueToRValue) {
2477 E = P->getSubExpr();
2478 continue;
2479 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002480 } else if (MaterializeTemporaryExpr *Materialize
2481 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2482 E = Materialize->GetTemporaryExpr();
2483 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002484 } else if (SubstNonTypeTemplateParmExpr *NTTP
2485 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2486 E = NTTP->getReplacement();
2487 continue;
John McCall34376a62010-12-04 03:47:34 +00002488 }
2489 break;
2490 }
2491 return E;
2492}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002493
2494Expr *Expr::ignoreParenBaseCasts() {
2495 Expr *E = this;
2496 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002497 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002498 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2499 if (CE->getCastKind() == CK_DerivedToBase ||
2500 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2501 CE->getCastKind() == CK_NoOp) {
2502 E = CE->getSubExpr();
2503 continue;
2504 }
2505 }
2506
2507 return E;
2508 }
2509}
2510
John McCalleebc8322010-05-05 22:59:52 +00002511Expr *Expr::IgnoreParenImpCasts() {
2512 Expr *E = this;
2513 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002514 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002515 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002516 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002517 continue;
2518 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002519 if (MaterializeTemporaryExpr *Materialize
2520 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2521 E = Materialize->GetTemporaryExpr();
2522 continue;
2523 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002524 if (SubstNonTypeTemplateParmExpr *NTTP
2525 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2526 E = NTTP->getReplacement();
2527 continue;
2528 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002529 return E;
John McCalleebc8322010-05-05 22:59:52 +00002530 }
2531}
2532
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002533Expr *Expr::IgnoreConversionOperator() {
2534 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002535 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002536 return MCE->getImplicitObjectArgument();
2537 }
2538 return this;
2539}
2540
Chris Lattneref26c772009-03-13 17:28:01 +00002541/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2542/// value (including ptr->int casts of the same size). Strip off any
2543/// ParenExpr or CastExprs, returning their operand.
2544Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2545 Expr *E = this;
2546 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002547 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002548
Chris Lattneref26c772009-03-13 17:28:01 +00002549 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2550 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002551 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002552 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002553
Chris Lattneref26c772009-03-13 17:28:01 +00002554 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2555 E = SE;
2556 continue;
2557 }
Mike Stump11289f42009-09-09 15:08:12 +00002558
Abramo Bagnara932e3932010-10-15 07:51:18 +00002559 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002560 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002561 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002562 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002563 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2564 E = SE;
2565 continue;
2566 }
2567 }
Mike Stump11289f42009-09-09 15:08:12 +00002568
Douglas Gregor6a40b082011-09-08 17:56:33 +00002569 if (SubstNonTypeTemplateParmExpr *NTTP
2570 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2571 E = NTTP->getReplacement();
2572 continue;
2573 }
2574
Chris Lattneref26c772009-03-13 17:28:01 +00002575 return E;
2576 }
2577}
2578
Douglas Gregord196a582009-12-14 19:27:10 +00002579bool Expr::isDefaultArgument() const {
2580 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002581 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2582 E = M->GetTemporaryExpr();
2583
Douglas Gregord196a582009-12-14 19:27:10 +00002584 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2585 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002586
Douglas Gregord196a582009-12-14 19:27:10 +00002587 return isa<CXXDefaultArgExpr>(E);
2588}
Chris Lattneref26c772009-03-13 17:28:01 +00002589
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002590/// \brief Skip over any no-op casts and any temporary-binding
2591/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002592static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002593 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2594 E = M->GetTemporaryExpr();
2595
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002596 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002597 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002598 E = ICE->getSubExpr();
2599 else
2600 break;
2601 }
2602
2603 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2604 E = BE->getSubExpr();
2605
2606 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002607 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002608 E = ICE->getSubExpr();
2609 else
2610 break;
2611 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002612
2613 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002614}
2615
John McCall7a626f62010-09-15 10:14:12 +00002616/// isTemporaryObject - Determines if this expression produces a
2617/// temporary of the given class type.
2618bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2619 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2620 return false;
2621
Anders Carlsson66bbf502010-11-28 16:40:49 +00002622 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002623
John McCall02dc8c72010-09-15 20:59:13 +00002624 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002625 if (!E->Classify(C).isPRValue()) {
2626 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002627 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002628 return false;
2629 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002630
John McCallf4ee1dd2010-09-16 06:57:56 +00002631 // Black-list a few cases which yield pr-values of class type that don't
2632 // refer to temporaries of that type:
2633
2634 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002635 if (isa<ImplicitCastExpr>(E)) {
2636 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2637 case CK_DerivedToBase:
2638 case CK_UncheckedDerivedToBase:
2639 return false;
2640 default:
2641 break;
2642 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002643 }
2644
John McCallf4ee1dd2010-09-16 06:57:56 +00002645 // - member expressions (all)
2646 if (isa<MemberExpr>(E))
2647 return false;
2648
Eli Friedman13ffdd82012-06-15 23:51:06 +00002649 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2650 if (BO->isPtrMemOp())
2651 return false;
2652
John McCallc07a0c72011-02-17 10:25:35 +00002653 // - opaque values (all)
2654 if (isa<OpaqueValueExpr>(E))
2655 return false;
2656
John McCall7a626f62010-09-15 10:14:12 +00002657 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002658}
2659
Douglas Gregor25b7e052011-03-02 21:06:53 +00002660bool Expr::isImplicitCXXThis() const {
2661 const Expr *E = this;
2662
2663 // Strip away parentheses and casts we don't care about.
2664 while (true) {
2665 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2666 E = Paren->getSubExpr();
2667 continue;
2668 }
2669
2670 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2671 if (ICE->getCastKind() == CK_NoOp ||
2672 ICE->getCastKind() == CK_LValueToRValue ||
2673 ICE->getCastKind() == CK_DerivedToBase ||
2674 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2675 E = ICE->getSubExpr();
2676 continue;
2677 }
2678 }
2679
2680 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2681 if (UnOp->getOpcode() == UO_Extension) {
2682 E = UnOp->getSubExpr();
2683 continue;
2684 }
2685 }
2686
Douglas Gregorfe314812011-06-21 17:03:29 +00002687 if (const MaterializeTemporaryExpr *M
2688 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2689 E = M->GetTemporaryExpr();
2690 continue;
2691 }
2692
Douglas Gregor25b7e052011-03-02 21:06:53 +00002693 break;
2694 }
2695
2696 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2697 return This->isImplicit();
2698
2699 return false;
2700}
2701
Douglas Gregor4619e432008-12-05 23:32:09 +00002702/// hasAnyTypeDependentArguments - Determines if any of the expressions
2703/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002704bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002705 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002706 if (Exprs[I]->isTypeDependent())
2707 return true;
2708
2709 return false;
2710}
2711
Abramo Bagnara847c6602014-05-22 19:20:46 +00002712bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
2713 const Expr **Culprit) const {
Eli Friedman384da272009-01-25 03:12:18 +00002714 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002715 // which can be evaluated at compile-time. It very closely parallels
2716 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2717 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2718 // to isEvaluatable most of the time.
2719 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002720 // If we ever capture reference-binding directly in the AST, we can
2721 // kill the second parameter.
2722
2723 if (IsForRef) {
2724 EvalResult Result;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002725 if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects)
2726 return true;
2727 if (Culprit)
2728 *Culprit = this;
2729 return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002730 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002731
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002732 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002733 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002734 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002735 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002736 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002737 case CXXTemporaryObjectExprClass:
2738 case CXXConstructExprClass: {
2739 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002740
Eli Friedman4c27ac22013-07-16 22:40:53 +00002741 if (CE->getConstructor()->isTrivial() &&
2742 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2743 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002744 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002745
Eli Friedman4c27ac22013-07-16 22:40:53 +00002746 // Trivial copy constructor
2747 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
Abramo Bagnara847c6602014-05-22 19:20:46 +00002748 return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit);
Richard Smithd62306a2011-11-10 06:34:14 +00002749 }
2750
Richard Smithd62306a2011-11-10 06:34:14 +00002751 break;
John McCall81c9cea2010-08-01 21:51:45 +00002752 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002753 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002754 // This handles gcc's extension that allows global initializers like
2755 // "struct x {int x;} x = (struct x) {};".
2756 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002757 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002758 return Exp->isConstantInitializer(Ctx, false, Culprit);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002759 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002760 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002761 const InitListExpr *ILE = cast<InitListExpr>(this);
2762 if (ILE->getType()->isArrayType()) {
2763 unsigned numInits = ILE->getNumInits();
2764 for (unsigned i = 0; i < numInits; i++) {
Abramo Bagnara847c6602014-05-22 19:20:46 +00002765 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002766 return false;
2767 }
2768 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002769 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002770
2771 if (ILE->getType()->isRecordType()) {
2772 unsigned ElementNo = 0;
2773 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
Hans Wennborga302cd92014-08-21 16:06:57 +00002774 for (const auto *Field : RD->fields()) {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002775 // If this is a union, skip all the fields that aren't being initialized.
Hans Wennborga302cd92014-08-21 16:06:57 +00002776 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field)
Eli Friedman4c27ac22013-07-16 22:40:53 +00002777 continue;
2778
2779 // Don't emit anonymous bitfields, they just affect layout.
2780 if (Field->isUnnamedBitfield())
2781 continue;
2782
2783 if (ElementNo < ILE->getNumInits()) {
2784 const Expr *Elt = ILE->getInit(ElementNo++);
2785 if (Field->isBitField()) {
2786 // Bitfields have to evaluate to an integer.
2787 llvm::APSInt ResultTmp;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002788 if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) {
2789 if (Culprit)
2790 *Culprit = Elt;
Eli Friedman4c27ac22013-07-16 22:40:53 +00002791 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002792 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002793 } else {
2794 bool RefType = Field->getType()->isReferenceType();
Abramo Bagnara847c6602014-05-22 19:20:46 +00002795 if (!Elt->isConstantInitializer(Ctx, RefType, Culprit))
Eli Friedman4c27ac22013-07-16 22:40:53 +00002796 return false;
2797 }
2798 }
2799 }
2800 return true;
2801 }
2802
2803 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002804 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002805 case ImplicitValueInitExprClass:
2806 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002807 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002808 return cast<ParenExpr>(this)->getSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002809 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Peter Collingbourne91147592011-04-15 00:35:48 +00002810 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002811 return cast<GenericSelectionExpr>(this)->getResultExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002812 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002813 case ChooseExprClass:
Abramo Bagnara847c6602014-05-22 19:20:46 +00002814 if (cast<ChooseExpr>(this)->isConditionDependent()) {
2815 if (Culprit)
2816 *Culprit = this;
Eli Friedman75807f22013-07-20 00:40:58 +00002817 return false;
Abramo Bagnara847c6602014-05-22 19:20:46 +00002818 }
Eli Friedman75807f22013-07-20 00:40:58 +00002819 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002820 ->isConstantInitializer(Ctx, IsForRef, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002821 case UnaryOperatorClass: {
2822 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002823 if (Exp->getOpcode() == UO_Extension)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002824 return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman384da272009-01-25 03:12:18 +00002825 break;
2826 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002827 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002828 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002829 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002830 case CStyleCastExprClass:
2831 case ObjCBridgedCastExprClass:
2832 case CXXDynamicCastExprClass:
2833 case CXXReinterpretCastExprClass:
2834 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002835 const CastExpr *CE = cast<CastExpr>(this);
2836
Eli Friedman13ec75b2011-12-21 00:43:02 +00002837 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002838 if (CE->getCastKind() == CK_NoOp ||
2839 CE->getCastKind() == CK_LValueToRValue ||
2840 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002841 CE->getCastKind() == CK_ConstructorConversion ||
2842 CE->getCastKind() == CK_NonAtomicToAtomic ||
2843 CE->getCastKind() == CK_AtomicToNonAtomic)
Abramo Bagnara847c6602014-05-22 19:20:46 +00002844 return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit);
Richard Smith161f09a2011-12-06 22:44:34 +00002845
Eli Friedman384da272009-01-25 03:12:18 +00002846 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002847 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002848 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002849 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002850 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002851
2852 case SubstNonTypeTemplateParmExprClass:
2853 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002854 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002855 case CXXDefaultArgExprClass:
2856 return cast<CXXDefaultArgExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002857 ->isConstantInitializer(Ctx, false, Culprit);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002858 case CXXDefaultInitExprClass:
2859 return cast<CXXDefaultInitExpr>(this)->getExpr()
Abramo Bagnara847c6602014-05-22 19:20:46 +00002860 ->isConstantInitializer(Ctx, false, Culprit);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002861 }
Abramo Bagnara847c6602014-05-22 19:20:46 +00002862 if (isEvaluatable(Ctx))
2863 return true;
2864 if (Culprit)
2865 *Culprit = this;
2866 return false;
Steve Naroffb03f5942007-09-02 20:30:18 +00002867}
2868
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002869bool Expr::HasSideEffects(const ASTContext &Ctx,
2870 bool IncludePossibleEffects) const {
2871 // In circumstances where we care about definite side effects instead of
2872 // potential side effects, we want to ignore expressions that are part of a
2873 // macro expansion as a potential side effect.
2874 if (!IncludePossibleEffects && getExprLoc().isMacroID())
2875 return false;
2876
Richard Smith0421ce72012-08-07 04:16:51 +00002877 if (isInstantiationDependent())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002878 return IncludePossibleEffects;
Richard Smith0421ce72012-08-07 04:16:51 +00002879
2880 switch (getStmtClass()) {
2881 case NoStmtClass:
2882 #define ABSTRACT_STMT(Type)
2883 #define STMT(Type, Base) case Type##Class:
2884 #define EXPR(Type, Base)
2885 #include "clang/AST/StmtNodes.inc"
2886 llvm_unreachable("unexpected Expr kind");
2887
2888 case DependentScopeDeclRefExprClass:
2889 case CXXUnresolvedConstructExprClass:
2890 case CXXDependentScopeMemberExprClass:
2891 case UnresolvedLookupExprClass:
2892 case UnresolvedMemberExprClass:
2893 case PackExpansionExprClass:
2894 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002895 case FunctionParmPackExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00002896 case TypoExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00002897 case CXXFoldExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002898 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2899
Richard Smitha33e4fe2012-08-07 05:18:29 +00002900 case DeclRefExprClass:
2901 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002902 case PredefinedExprClass:
2903 case IntegerLiteralClass:
2904 case FloatingLiteralClass:
2905 case ImaginaryLiteralClass:
2906 case StringLiteralClass:
2907 case CharacterLiteralClass:
2908 case OffsetOfExprClass:
2909 case ImplicitValueInitExprClass:
2910 case UnaryExprOrTypeTraitExprClass:
2911 case AddrLabelExprClass:
2912 case GNUNullExprClass:
2913 case CXXBoolLiteralExprClass:
2914 case CXXNullPtrLiteralExprClass:
2915 case CXXThisExprClass:
2916 case CXXScalarValueInitExprClass:
2917 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002918 case ArrayTypeTraitExprClass:
2919 case ExpressionTraitExprClass:
2920 case CXXNoexceptExprClass:
2921 case SizeOfPackExprClass:
2922 case ObjCStringLiteralClass:
2923 case ObjCEncodeExprClass:
2924 case ObjCBoolLiteralExprClass:
2925 case CXXUuidofExprClass:
2926 case OpaqueValueExprClass:
2927 // These never have a side-effect.
2928 return false;
2929
2930 case CallExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002931 case CXXOperatorCallExprClass:
2932 case CXXMemberCallExprClass:
2933 case CUDAKernelCallExprClass:
2934 case BlockExprClass:
2935 case CXXBindTemporaryExprClass:
2936 case UserDefinedLiteralClass:
2937 // We don't know a call definitely has side effects, but we can check the
2938 // call's operands.
2939 if (!IncludePossibleEffects)
2940 break;
2941 return true;
2942
John McCall5e77d762013-04-16 07:28:30 +00002943 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002944 case CompoundAssignOperatorClass:
2945 case VAArgExprClass:
2946 case AtomicExprClass:
2947 case StmtExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002948 case CXXThrowExprClass:
2949 case CXXNewExprClass:
2950 case CXXDeleteExprClass:
2951 case ExprWithCleanupsClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002952 // These always have a side-effect.
2953 return true;
2954
2955 case ParenExprClass:
2956 case ArraySubscriptExprClass:
2957 case MemberExprClass:
2958 case ConditionalOperatorClass:
2959 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002960 case CompoundLiteralExprClass:
2961 case ExtVectorElementExprClass:
2962 case DesignatedInitExprClass:
2963 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002964 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002965 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002966 case SubstNonTypeTemplateParmExprClass:
2967 case MaterializeTemporaryExprClass:
2968 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002969 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002970 case AsTypeExprClass:
2971 // These have a side-effect if any subexpression does.
2972 break;
2973
Richard Smitha33e4fe2012-08-07 05:18:29 +00002974 case UnaryOperatorClass:
2975 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002976 return true;
2977 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002978
2979 case BinaryOperatorClass:
2980 if (cast<BinaryOperator>(this)->isAssignmentOp())
2981 return true;
2982 break;
2983
Richard Smith0421ce72012-08-07 04:16:51 +00002984 case InitListExprClass:
2985 // FIXME: The children for an InitListExpr doesn't include the array filler.
2986 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002987 if (E->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00002988 return true;
2989 break;
2990
2991 case GenericSelectionExprClass:
2992 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002993 HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00002994
2995 case ChooseExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00002996 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(
2997 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00002998
2999 case CXXDefaultArgExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003000 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(
3001 Ctx, IncludePossibleEffects);
Richard Smith0421ce72012-08-07 04:16:51 +00003002
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003003 case CXXDefaultInitExprClass: {
3004 const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField();
3005 if (const Expr *E = FD->getInClassInitializer())
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003006 return E->HasSideEffects(Ctx, IncludePossibleEffects);
Richard Smith852c9db2013-04-20 22:23:05 +00003007 // If we've not yet parsed the initializer, assume it has side-effects.
3008 return true;
Reid Klecknerd60b82f2014-11-17 23:36:45 +00003009 }
Richard Smith852c9db2013-04-20 22:23:05 +00003010
Richard Smith0421ce72012-08-07 04:16:51 +00003011 case CXXDynamicCastExprClass: {
3012 // A dynamic_cast expression has side-effects if it can throw.
3013 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
3014 if (DCE->getTypeAsWritten()->isReferenceType() &&
3015 DCE->getCastKind() == CK_Dynamic)
3016 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003017 } // Fall through.
3018 case ImplicitCastExprClass:
3019 case CStyleCastExprClass:
3020 case CXXStaticCastExprClass:
3021 case CXXReinterpretCastExprClass:
3022 case CXXConstCastExprClass:
3023 case CXXFunctionalCastExprClass: {
Aaron Ballman409af502015-01-03 17:00:12 +00003024 // While volatile reads are side-effecting in both C and C++, we treat them
3025 // as having possible (not definite) side-effects. This allows idiomatic
3026 // code to behave without warning, such as sizeof(*v) for a volatile-
3027 // qualified pointer.
3028 if (!IncludePossibleEffects)
3029 break;
3030
Richard Smitha33e4fe2012-08-07 05:18:29 +00003031 const CastExpr *CE = cast<CastExpr>(this);
3032 if (CE->getCastKind() == CK_LValueToRValue &&
3033 CE->getSubExpr()->getType().isVolatileQualified())
3034 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00003035 break;
3036 }
3037
Richard Smithef8bf432012-08-13 20:08:14 +00003038 case CXXTypeidExprClass:
3039 // typeid might throw if its subexpression is potentially-evaluated, so has
3040 // side-effects in that case whether or not its subexpression does.
3041 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00003042
3043 case CXXConstructExprClass:
3044 case CXXTemporaryObjectExprClass: {
3045 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003046 if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects)
Richard Smith0421ce72012-08-07 04:16:51 +00003047 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00003048 // A trivial constructor does not add any side-effects of its own. Just look
3049 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00003050 break;
3051 }
3052
3053 case LambdaExprClass: {
3054 const LambdaExpr *LE = cast<LambdaExpr>(this);
3055 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
3056 E = LE->capture_end(); I != E; ++I)
3057 if (I->getCaptureKind() == LCK_ByCopy)
3058 // FIXME: Only has a side-effect if the variable is volatile or if
3059 // the copy would invoke a non-trivial copy constructor.
3060 return true;
3061 return false;
3062 }
3063
3064 case PseudoObjectExprClass: {
3065 // Only look for side-effects in the semantic form, and look past
3066 // OpaqueValueExpr bindings in that form.
3067 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
3068 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
3069 E = PO->semantics_end();
3070 I != E; ++I) {
3071 const Expr *Subexpr = *I;
3072 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
3073 Subexpr = OVE->getSourceExpr();
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003074 if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003075 return true;
3076 }
3077 return false;
3078 }
3079
3080 case ObjCBoxedExprClass:
3081 case ObjCArrayLiteralClass:
3082 case ObjCDictionaryLiteralClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003083 case ObjCSelectorExprClass:
3084 case ObjCProtocolExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00003085 case ObjCIsaExprClass:
3086 case ObjCIndirectCopyRestoreExprClass:
3087 case ObjCSubscriptRefExprClass:
3088 case ObjCBridgedCastExprClass:
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003089 case ObjCMessageExprClass:
3090 case ObjCPropertyRefExprClass:
3091 // FIXME: Classify these cases better.
3092 if (IncludePossibleEffects)
3093 return true;
3094 break;
Richard Smith0421ce72012-08-07 04:16:51 +00003095 }
3096
3097 // Recurse to children.
3098 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
3099 if (const Stmt *S = *SubStmts)
Aaron Ballman6c93b3e2014-12-17 21:57:17 +00003100 if (cast<Expr>(S)->HasSideEffects(Ctx, IncludePossibleEffects))
Richard Smith0421ce72012-08-07 04:16:51 +00003101 return true;
3102
3103 return false;
3104}
3105
Douglas Gregor1be329d2012-02-23 07:33:15 +00003106namespace {
3107 /// \brief Look for a call to a non-trivial function within an expression.
3108 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3109 {
3110 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3111
3112 bool NonTrivial;
3113
3114 public:
3115 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003116 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003117
3118 bool hasNonTrivialCall() const { return NonTrivial; }
3119
3120 void VisitCallExpr(CallExpr *E) {
3121 if (CXXMethodDecl *Method
3122 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3123 if (Method->isTrivial()) {
3124 // Recurse to children of the call.
3125 Inherited::VisitStmt(E);
3126 return;
3127 }
3128 }
3129
3130 NonTrivial = true;
3131 }
3132
3133 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3134 if (E->getConstructor()->isTrivial()) {
3135 // Recurse to children of the call.
3136 Inherited::VisitStmt(E);
3137 return;
3138 }
3139
3140 NonTrivial = true;
3141 }
3142
3143 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3144 if (E->getTemporary()->getDestructor()->isTrivial()) {
3145 Inherited::VisitStmt(E);
3146 return;
3147 }
3148
3149 NonTrivial = true;
3150 }
3151 };
3152}
3153
3154bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3155 NonTrivialCallFinder Finder(Ctx);
3156 Finder.Visit(this);
3157 return Finder.hasNonTrivialCall();
3158}
3159
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003160/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3161/// pointer constant or not, as well as the specific kind of constant detected.
3162/// Null pointer constants can be integer constant expressions with the
3163/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3164/// (a GNU extension).
3165Expr::NullPointerConstantKind
3166Expr::isNullPointerConstant(ASTContext &Ctx,
3167 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003168 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003169 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003170 switch (NPC) {
3171 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003172 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003173 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003174 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003175 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003176 else
3177 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003178
Douglas Gregor56751b52009-09-25 04:25:58 +00003179 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003180 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003181 }
3182 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003183
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003184 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003185 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003186 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003187 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003188 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003189 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003190 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003191 Pointee->isVoidType() && // to void*
3192 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003193 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003194 }
Steve Naroffada7d422007-05-20 17:54:12 +00003195 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003196 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3197 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003198 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003199 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3200 // Accept ((void*)0) as a null pointer constant, as many other
3201 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003202 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003203 } else if (const GenericSelectionExpr *GE =
3204 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003205 if (GE->isResultDependent())
3206 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003207 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003208 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3209 if (CE->isConditionDependent())
3210 return NPCK_NotNull;
3211 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003212 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003213 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003214 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003215 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003216 } else if (const CXXDefaultInitExpr *DefaultInit
3217 = dyn_cast<CXXDefaultInitExpr>(this)) {
3218 // See through default initializer expressions.
3219 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003220 } else if (isa<GNUNullExpr>(this)) {
3221 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003222 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003223 } else if (const MaterializeTemporaryExpr *M
3224 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3225 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003226 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3227 if (const Expr *Source = OVE->getSourceExpr())
3228 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003229 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003230
Richard Smith89645bc2013-01-02 12:01:23 +00003231 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003232 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003233 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003234
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003235 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003236 if (!Ctx.getLangOpts().CPlusPlus11 &&
3237 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003238 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3239 const Expr *InitExpr = CLE->getInitializer();
3240 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3241 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3242 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003243 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003244 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003245 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003246 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003247
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003248 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003249 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3250 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003251 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003252 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003253 if (Lit && !Lit->getValue())
3254 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003255 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003256 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003257 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003258 // If we have an integer constant expression, we need to *evaluate* it and
3259 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003260 if (!isIntegerConstantExpr(Ctx))
3261 return NPCK_NotNull;
3262 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003263
David Blaikie1c7c8f72012-08-08 17:33:31 +00003264 if (EvaluateKnownConstInt(Ctx) != 0)
3265 return NPCK_NotNull;
3266
3267 if (isa<IntegerLiteral>(this))
3268 return NPCK_ZeroLiteral;
3269 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003270}
Steve Narofff7a5da12007-07-28 23:10:27 +00003271
John McCall34376a62010-12-04 03:47:34 +00003272/// \brief If this expression is an l-value for an Objective C
3273/// property, find the underlying property reference expression.
3274const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3275 const Expr *E = this;
3276 while (true) {
3277 assert((E->getValueKind() == VK_LValue &&
3278 E->getObjectKind() == OK_ObjCProperty) &&
3279 "expression is not a property reference");
3280 E = E->IgnoreParenCasts();
3281 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3282 if (BO->getOpcode() == BO_Comma) {
3283 E = BO->getRHS();
3284 continue;
3285 }
3286 }
3287
3288 break;
3289 }
3290
3291 return cast<ObjCPropertyRefExpr>(E);
3292}
3293
Anna Zaks97c7ce32012-10-01 20:34:04 +00003294bool Expr::isObjCSelfExpr() const {
3295 const Expr *E = IgnoreParenImpCasts();
3296
3297 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3298 if (!DRE)
3299 return false;
3300
3301 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3302 if (!Param)
3303 return false;
3304
3305 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3306 if (!M)
3307 return false;
3308
3309 return M->getSelfDecl() == Param;
3310}
3311
John McCalld25db7e2013-05-06 21:39:12 +00003312FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003313 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003314
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003315 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003316 if (ICE->getCastKind() == CK_LValueToRValue ||
3317 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003318 E = ICE->getSubExpr()->IgnoreParens();
3319 else
3320 break;
3321 }
3322
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003323 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003324 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003325 if (Field->isBitField())
3326 return Field;
3327
John McCalld25db7e2013-05-06 21:39:12 +00003328 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3329 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3330 if (Ivar->isBitField())
3331 return Ivar;
3332
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003333 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3334 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3335 if (Field->isBitField())
3336 return Field;
3337
Eli Friedman609ada22011-07-13 02:05:57 +00003338 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003339 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003340 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003341
Eli Friedman609ada22011-07-13 02:05:57 +00003342 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003343 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003344 }
3345
Richard Smith5b571672014-09-24 23:55:00 +00003346 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E))
3347 if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp())
3348 return UnOp->getSubExpr()->getSourceBitField();
3349
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003351}
3352
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003353bool Expr::refersToVectorElement() const {
3354 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003355
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003356 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003357 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003358 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003359 E = ICE->getSubExpr()->IgnoreParens();
3360 else
3361 break;
3362 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003363
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003364 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3365 return ASE->getBase()->getType()->isVectorType();
3366
3367 if (isa<ExtVectorElementExpr>(E))
3368 return true;
3369
3370 return false;
3371}
3372
Chris Lattnerb8211f62009-02-16 22:14:05 +00003373/// isArrow - Return true if the base expression is a pointer to vector,
3374/// return false if the base expression is a vector.
3375bool ExtVectorElementExpr::isArrow() const {
3376 return getBase()->getType()->isPointerType();
3377}
3378
Nate Begemance4d7fc2008-04-18 23:10:10 +00003379unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003380 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003381 return VT->getNumElements();
3382 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003383}
3384
Nate Begemanf322eab2008-05-09 06:41:27 +00003385/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003386bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003387 // FIXME: Refactor this code to an accessor on the AST node which returns the
3388 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003389 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003390
3391 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003392 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003393 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003394
Nate Begeman7e5185b2009-01-18 02:01:21 +00003395 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003396 if (Comp[0] == 's' || Comp[0] == 'S')
3397 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003398
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003399 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003400 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003401 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003402
Steve Naroff0d595ca2007-07-30 03:29:09 +00003403 return false;
3404}
Chris Lattner885b4952007-08-02 23:36:59 +00003405
Nate Begemanf322eab2008-05-09 06:41:27 +00003406/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003407void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003408 SmallVectorImpl<unsigned> &Elts) const {
3409 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003410 if (Comp[0] == 's' || Comp[0] == 'S')
3411 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003412
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003413 bool isHi = Comp == "hi";
3414 bool isLo = Comp == "lo";
3415 bool isEven = Comp == "even";
3416 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003417
Nate Begemanf322eab2008-05-09 06:41:27 +00003418 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3419 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003420
Nate Begemanf322eab2008-05-09 06:41:27 +00003421 if (isHi)
3422 Index = e + i;
3423 else if (isLo)
3424 Index = i;
3425 else if (isEven)
3426 Index = 2 * i;
3427 else if (isOdd)
3428 Index = 2 * i + 1;
3429 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003430 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003431
Nate Begemand3862152008-05-13 21:03:02 +00003432 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003433 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003434}
3435
Douglas Gregor9a129192010-04-21 00:45:42 +00003436ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003437 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003438 SourceLocation LBracLoc,
3439 SourceLocation SuperLoc,
3440 bool IsInstanceSuper,
3441 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003442 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003443 ArrayRef<SourceLocation> SelLocs,
3444 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003445 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003446 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003447 SourceLocation RBracLoc,
3448 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003449 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003450 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003451 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003452 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003453 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3454 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003455 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Craig Topper36250ad2014-05-12 05:36:57 +00003456 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3457 IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
3458 RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003459{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003460 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003461 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003462}
3463
Douglas Gregor9a129192010-04-21 00:45:42 +00003464ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003465 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003466 SourceLocation LBracLoc,
3467 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003468 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003469 ArrayRef<SourceLocation> SelLocs,
3470 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003471 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003472 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003473 SourceLocation RBracLoc,
3474 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003475 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003476 T->isDependentType(), T->isInstantiationDependentType(),
3477 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003478 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3479 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003480 Kind(Class),
Craig Topper36250ad2014-05-12 05:36:57 +00003481 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3482 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003483{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003484 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003485 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003486}
3487
Douglas Gregor9a129192010-04-21 00:45:42 +00003488ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003489 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003490 SourceLocation LBracLoc,
3491 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003492 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003493 ArrayRef<SourceLocation> SelLocs,
3494 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003495 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003496 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003497 SourceLocation RBracLoc,
3498 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003499 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003500 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003501 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003502 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003503 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3504 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003505 Kind(Instance),
Craig Topper36250ad2014-05-12 05:36:57 +00003506 HasMethod(Method != nullptr), IsDelegateInitCall(false),
3507 IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003508{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003509 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003510 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003511}
3512
3513void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3514 ArrayRef<SourceLocation> SelLocs,
3515 SelectorLocationsKind SelLocsK) {
3516 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003517 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003518 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003519 if (Args[I]->isTypeDependent())
3520 ExprBits.TypeDependent = true;
3521 if (Args[I]->isValueDependent())
3522 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003523 if (Args[I]->isInstantiationDependent())
3524 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003525 if (Args[I]->containsUnexpandedParameterPack())
3526 ExprBits.ContainsUnexpandedParameterPack = true;
3527
3528 MyArgs[I] = Args[I];
3529 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003530
Benjamin Kramer2325b242012-02-20 00:20:48 +00003531 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003532 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003533 if (SelLocsK == SelLoc_NonStandard)
3534 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3535 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003536}
3537
Craig Topperce7167c2013-08-22 04:58:56 +00003538ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003539 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003540 SourceLocation LBracLoc,
3541 SourceLocation SuperLoc,
3542 bool IsInstanceSuper,
3543 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003544 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003545 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003546 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003547 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003548 SourceLocation RBracLoc,
3549 bool isImplicit) {
3550 assert((!SelLocs.empty() || isImplicit) &&
3551 "No selector locs for non-implicit message");
3552 ObjCMessageExpr *Mem;
3553 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3554 if (isImplicit)
3555 Mem = alloc(Context, Args.size(), 0);
3556 else
3557 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003558 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003559 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003560 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003561}
3562
Craig Topperce7167c2013-08-22 04:58:56 +00003563ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003564 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003565 SourceLocation LBracLoc,
3566 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003567 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003568 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003569 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003570 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003571 SourceLocation RBracLoc,
3572 bool isImplicit) {
3573 assert((!SelLocs.empty() || isImplicit) &&
3574 "No selector locs for non-implicit message");
3575 ObjCMessageExpr *Mem;
3576 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3577 if (isImplicit)
3578 Mem = alloc(Context, Args.size(), 0);
3579 else
3580 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003581 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003582 SelLocs, SelLocsK, Method, Args, RBracLoc,
3583 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003584}
3585
Craig Topperce7167c2013-08-22 04:58:56 +00003586ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003587 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003588 SourceLocation LBracLoc,
3589 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003590 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003591 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003592 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003593 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003594 SourceLocation RBracLoc,
3595 bool isImplicit) {
3596 assert((!SelLocs.empty() || isImplicit) &&
3597 "No selector locs for non-implicit message");
3598 ObjCMessageExpr *Mem;
3599 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3600 if (isImplicit)
3601 Mem = alloc(Context, Args.size(), 0);
3602 else
3603 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003604 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003605 SelLocs, SelLocsK, Method, Args, RBracLoc,
3606 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003607}
3608
Craig Topperce7167c2013-08-22 04:58:56 +00003609ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003610 unsigned NumArgs,
3611 unsigned NumStoredSelLocs) {
3612 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003613 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3614}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003615
Craig Topperce7167c2013-08-22 04:58:56 +00003616ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003617 ArrayRef<Expr *> Args,
3618 SourceLocation RBraceLoc,
3619 ArrayRef<SourceLocation> SelLocs,
3620 Selector Sel,
3621 SelectorLocationsKind &SelLocsK) {
3622 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3623 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3624 : 0;
3625 return alloc(C, Args.size(), NumStoredSelLocs);
3626}
3627
Craig Topperce7167c2013-08-22 04:58:56 +00003628ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003629 unsigned NumArgs,
3630 unsigned NumStoredSelLocs) {
3631 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3632 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3633 return (ObjCMessageExpr *)C.Allocate(Size,
3634 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3635}
3636
3637void ObjCMessageExpr::getSelectorLocs(
3638 SmallVectorImpl<SourceLocation> &SelLocs) const {
3639 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3640 SelLocs.push_back(getSelectorLoc(i));
3641}
3642
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003643SourceRange ObjCMessageExpr::getReceiverRange() const {
3644 switch (getReceiverKind()) {
3645 case Instance:
3646 return getInstanceReceiver()->getSourceRange();
3647
3648 case Class:
3649 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3650
3651 case SuperInstance:
3652 case SuperClass:
3653 return getSuperLoc();
3654 }
3655
David Blaikiee4d798f2012-01-20 21:50:17 +00003656 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003657}
3658
Douglas Gregor9a129192010-04-21 00:45:42 +00003659Selector ObjCMessageExpr::getSelector() const {
3660 if (HasMethod)
3661 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3662 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003663 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003664}
3665
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003666QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003667 switch (getReceiverKind()) {
3668 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003669 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003670 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003671 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003672 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003673 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003674 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003675 }
3676
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003677 llvm_unreachable("unexpected receiver kind");
3678}
3679
3680ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3681 QualType T = getReceiverType();
3682
3683 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3684 return Ptr->getInterfaceDecl();
3685
3686 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3687 return Ty->getInterface();
3688
Craig Topper36250ad2014-05-12 05:36:57 +00003689 return nullptr;
Ted Kremenek2c809302010-02-11 22:41:21 +00003690}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003691
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003692StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003693 switch (getBridgeKind()) {
3694 case OBC_Bridge:
3695 return "__bridge";
3696 case OBC_BridgeTransfer:
3697 return "__bridge_transfer";
3698 case OBC_BridgeRetained:
3699 return "__bridge_retained";
3700 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003701
3702 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003703}
3704
Craig Topper37932912013-08-18 10:09:15 +00003705ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003706 QualType Type, SourceLocation BLoc,
3707 SourceLocation RP)
3708 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3709 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003710 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003711 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003712 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003713{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003714 SubExprs = new (C) Stmt*[args.size()];
3715 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003716 if (args[i]->isTypeDependent())
3717 ExprBits.TypeDependent = true;
3718 if (args[i]->isValueDependent())
3719 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003720 if (args[i]->isInstantiationDependent())
3721 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003722 if (args[i]->containsUnexpandedParameterPack())
3723 ExprBits.ContainsUnexpandedParameterPack = true;
3724
3725 SubExprs[i] = args[i];
3726 }
3727}
3728
Craig Topper37932912013-08-18 10:09:15 +00003729void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003730 if (SubExprs) C.Deallocate(SubExprs);
3731
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003732 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003733 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003734 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003735}
Nate Begeman48745922009-08-12 02:28:50 +00003736
Craig Topper37932912013-08-18 10:09:15 +00003737GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003738 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003739 ArrayRef<TypeSourceInfo*> AssocTypes,
3740 ArrayRef<Expr*> AssocExprs,
3741 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003742 SourceLocation RParenLoc,
3743 bool ContainsUnexpandedParameterPack,
3744 unsigned ResultIndex)
3745 : Expr(GenericSelectionExprClass,
3746 AssocExprs[ResultIndex]->getType(),
3747 AssocExprs[ResultIndex]->getValueKind(),
3748 AssocExprs[ResultIndex]->getObjectKind(),
3749 AssocExprs[ResultIndex]->isTypeDependent(),
3750 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003751 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003752 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003753 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3754 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3755 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3756 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003757 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003758 assert(AssocTypes.size() == AssocExprs.size());
3759 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3760 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003761}
3762
Craig Topper37932912013-08-18 10:09:15 +00003763GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003764 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003765 ArrayRef<TypeSourceInfo*> AssocTypes,
3766 ArrayRef<Expr*> AssocExprs,
3767 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003768 SourceLocation RParenLoc,
3769 bool ContainsUnexpandedParameterPack)
3770 : Expr(GenericSelectionExprClass,
3771 Context.DependentTy,
3772 VK_RValue,
3773 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003774 /*isTypeDependent=*/true,
3775 /*isValueDependent=*/true,
3776 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003777 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003778 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3779 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3780 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3781 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003782 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003783 assert(AssocTypes.size() == AssocExprs.size());
3784 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3785 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003786}
3787
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003788//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003789// DesignatedInitExpr
3790//===----------------------------------------------------------------------===//
3791
Chandler Carruth631abd92011-06-16 06:47:06 +00003792IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003793 assert(Kind == FieldDesignator && "Only valid on a field designator");
3794 if (Field.NameOrField & 0x01)
3795 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3796 else
3797 return getField()->getIdentifier();
3798}
3799
Craig Topper37932912013-08-18 10:09:15 +00003800DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003801 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003802 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003803 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003804 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003805 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003806 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003807 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003808 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003809 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003810 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003811 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003812 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003813 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003814 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003815
3816 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003817 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003818 *Child++ = Init;
3819
3820 // Copy the designators and their subexpressions, computing
3821 // value-dependence along the way.
3822 unsigned IndexIdx = 0;
3823 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003824 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003825
3826 if (this->Designators[I].isArrayDesignator()) {
3827 // Compute type- and value-dependence.
3828 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003829 if (Index->isTypeDependent() || Index->isValueDependent())
David Majnemer4f217682015-01-09 01:39:09 +00003830 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003831 if (Index->isInstantiationDependent())
3832 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003833 // Propagate unexpanded parameter packs.
3834 if (Index->containsUnexpandedParameterPack())
3835 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003836
3837 // Copy the index expressions into permanent storage.
3838 *Child++ = IndexExprs[IndexIdx++];
3839 } else if (this->Designators[I].isArrayRangeDesignator()) {
3840 // Compute type- and value-dependence.
3841 Expr *Start = IndexExprs[IndexIdx];
3842 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003843 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003844 End->isTypeDependent() || End->isValueDependent()) {
David Majnemer4f217682015-01-09 01:39:09 +00003845 ExprBits.TypeDependent = ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003846 ExprBits.InstantiationDependent = true;
3847 } else if (Start->isInstantiationDependent() ||
3848 End->isInstantiationDependent()) {
3849 ExprBits.InstantiationDependent = true;
3850 }
3851
Douglas Gregora6e053e2010-12-15 01:34:56 +00003852 // Propagate unexpanded parameter packs.
3853 if (Start->containsUnexpandedParameterPack() ||
3854 End->containsUnexpandedParameterPack())
3855 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003856
3857 // Copy the start/end expressions into permanent storage.
3858 *Child++ = IndexExprs[IndexIdx++];
3859 *Child++ = IndexExprs[IndexIdx++];
3860 }
3861 }
3862
Benjamin Kramerc215e762012-08-24 11:54:20 +00003863 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003864}
3865
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003866DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003867DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003868 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003869 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003870 SourceLocation ColonOrEqualLoc,
3871 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003872 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003873 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003874 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003875 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003876 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003877}
3878
Craig Topper37932912013-08-18 10:09:15 +00003879DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003880 unsigned NumIndexExprs) {
3881 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3882 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3883 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3884}
3885
Craig Topper37932912013-08-18 10:09:15 +00003886void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003887 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003888 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003889 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003890 NumDesignators = NumDesigs;
3891 for (unsigned I = 0; I != NumDesigs; ++I)
3892 Designators[I] = Desigs[I];
3893}
3894
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003895SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3896 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3897 if (size() == 1)
3898 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003899 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3900 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003901}
3902
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003903SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003904 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003905 Designator &First =
3906 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003907 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003908 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003909 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3910 else
3911 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3912 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003913 StartLoc =
3914 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003915 return StartLoc;
3916}
3917
3918SourceLocation DesignatedInitExpr::getLocEnd() const {
3919 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003920}
3921
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003922Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003923 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003924 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003925 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3926}
3927
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003928Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003929 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003930 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003931 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003932 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3933}
3934
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003935Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003936 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003937 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003938 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003939 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3940}
3941
Douglas Gregord5846a12009-04-15 06:41:24 +00003942/// \brief Replaces the designator at index @p Idx with the series
3943/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003944void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003945 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003946 const Designator *Last) {
3947 unsigned NumNewDesignators = Last - First;
3948 if (NumNewDesignators == 0) {
3949 std::copy_backward(Designators + Idx + 1,
3950 Designators + NumDesignators,
3951 Designators + Idx);
3952 --NumNewDesignators;
3953 return;
3954 } else if (NumNewDesignators == 1) {
3955 Designators[Idx] = *First;
3956 return;
3957 }
3958
Mike Stump11289f42009-09-09 15:08:12 +00003959 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003960 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003961 std::copy(Designators, Designators + Idx, NewDesignators);
3962 std::copy(First, Last, NewDesignators + Idx);
3963 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3964 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003965 Designators = NewDesignators;
3966 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3967}
3968
Craig Topper37932912013-08-18 10:09:15 +00003969ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003970 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003971 SourceLocation rparenloc)
3972 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003973 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003974 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3975 Exprs = new (C) Stmt*[exprs.size()];
3976 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003977 if (exprs[i]->isTypeDependent())
3978 ExprBits.TypeDependent = true;
3979 if (exprs[i]->isValueDependent())
3980 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003981 if (exprs[i]->isInstantiationDependent())
3982 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003983 if (exprs[i]->containsUnexpandedParameterPack())
3984 ExprBits.ContainsUnexpandedParameterPack = true;
3985
Nate Begeman5ec4b312009-08-10 23:49:36 +00003986 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003987 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003988}
3989
John McCall1bf58462011-02-16 08:02:54 +00003990const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3991 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3992 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003993 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3994 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003995 e = cast<CXXConstructExpr>(e)->getArg(0);
3996 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3997 e = ice->getSubExpr();
3998 return cast<OpaqueValueExpr>(e);
3999}
4000
Craig Topper37932912013-08-18 10:09:15 +00004001PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
4002 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00004003 unsigned numSemanticExprs) {
4004 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
4005 (1 + numSemanticExprs) * sizeof(Expr*),
4006 llvm::alignOf<PseudoObjectExpr>());
4007 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
4008}
4009
4010PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
4011 : Expr(PseudoObjectExprClass, shell) {
4012 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
4013}
4014
Craig Topper37932912013-08-18 10:09:15 +00004015PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00004016 ArrayRef<Expr*> semantics,
4017 unsigned resultIndex) {
4018 assert(syntax && "no syntactic expression!");
4019 assert(semantics.size() && "no semantic expressions!");
4020
4021 QualType type;
4022 ExprValueKind VK;
4023 if (resultIndex == NoResult) {
4024 type = C.VoidTy;
4025 VK = VK_RValue;
4026 } else {
4027 assert(resultIndex < semantics.size());
4028 type = semantics[resultIndex]->getType();
4029 VK = semantics[resultIndex]->getValueKind();
4030 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
4031 }
4032
4033 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
4034 (1 + semantics.size()) * sizeof(Expr*),
4035 llvm::alignOf<PseudoObjectExpr>());
4036 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
4037 resultIndex);
4038}
4039
4040PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
4041 Expr *syntax, ArrayRef<Expr*> semantics,
4042 unsigned resultIndex)
4043 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
4044 /*filled in at end of ctor*/ false, false, false, false) {
4045 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
4046 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
4047
4048 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
4049 Expr *E = (i == 0 ? syntax : semantics[i-1]);
4050 getSubExprsBuffer()[i] = E;
4051
4052 if (E->isTypeDependent())
4053 ExprBits.TypeDependent = true;
4054 if (E->isValueDependent())
4055 ExprBits.ValueDependent = true;
4056 if (E->isInstantiationDependent())
4057 ExprBits.InstantiationDependent = true;
4058 if (E->containsUnexpandedParameterPack())
4059 ExprBits.ContainsUnexpandedParameterPack = true;
4060
4061 if (isa<OpaqueValueExpr>(E))
Craig Topper36250ad2014-05-12 05:36:57 +00004062 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
John McCallfe96e0b2011-11-06 09:01:30 +00004063 "opaque-value semantic expressions for pseudo-object "
4064 "operations must have sources");
4065 }
4066}
4067
Douglas Gregore4a0bb72009-01-22 00:58:24 +00004068//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00004069// ExprIterator.
4070//===----------------------------------------------------------------------===//
4071
4072Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
4073Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
4074Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
4075const Expr* ConstExprIterator::operator[](size_t idx) const {
4076 return cast<Expr>(I[idx]);
4077}
4078const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
4079const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
4080
4081//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00004082// Child Iterators for iterating over subexpressions/substatements
4083//===----------------------------------------------------------------------===//
4084
Peter Collingbournee190dee2011-03-11 19:24:49 +00004085// UnaryExprOrTypeTraitExpr
4086Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00004087 // If this is of a type and the type is a VLA type (and not a typedef), the
4088 // size expression of the VLA needs to be treated as an executable expression.
4089 // Why isn't this weirdness documented better in StmtIterator?
4090 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00004091 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00004092 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00004093 return child_range(child_iterator(T), child_iterator());
4094 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00004095 }
John McCallbd066782011-02-09 08:16:59 +00004096 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00004097}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004098
Steve Naroffd54978b2007-09-18 23:55:05 +00004099// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004100Stmt::child_range ObjCMessageExpr::children() {
4101 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004102 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004103 begin = reinterpret_cast<Stmt **>(this + 1);
4104 else
4105 begin = reinterpret_cast<Stmt **>(getArgs());
4106 return child_range(begin,
4107 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004108}
4109
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004110ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004111 QualType T, ObjCMethodDecl *Method,
4112 SourceRange SR)
4113 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4114 false, false, false, false),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004115 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004116{
4117 Expr **SaveElements = getElements();
4118 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4119 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4120 ExprBits.ValueDependent = true;
4121 if (Elements[I]->isInstantiationDependent())
4122 ExprBits.InstantiationDependent = true;
4123 if (Elements[I]->containsUnexpandedParameterPack())
4124 ExprBits.ContainsUnexpandedParameterPack = true;
4125
4126 SaveElements[I] = Elements[I];
4127 }
4128}
4129
Craig Topperce7167c2013-08-22 04:58:56 +00004130ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004131 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004132 QualType T, ObjCMethodDecl * Method,
4133 SourceRange SR) {
4134 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4135 + Elements.size() * sizeof(Expr *));
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004136 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004137}
4138
Craig Topperce7167c2013-08-22 04:58:56 +00004139ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004140 unsigned NumElements) {
4141
4142 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4143 + NumElements * sizeof(Expr *));
4144 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4145}
4146
4147ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4148 ArrayRef<ObjCDictionaryElement> VK,
4149 bool HasPackExpansions,
4150 QualType T, ObjCMethodDecl *method,
4151 SourceRange SR)
4152 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4153 false, false),
4154 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004155 DictWithObjectsMethod(method)
Ted Kremeneke65b0862012-03-06 20:05:56 +00004156{
4157 KeyValuePair *KeyValues = getKeyValues();
4158 ExpansionData *Expansions = getExpansionData();
4159 for (unsigned I = 0; I < NumElements; I++) {
4160 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4161 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4162 ExprBits.ValueDependent = true;
4163 if (VK[I].Key->isInstantiationDependent() ||
4164 VK[I].Value->isInstantiationDependent())
4165 ExprBits.InstantiationDependent = true;
4166 if (VK[I].EllipsisLoc.isInvalid() &&
4167 (VK[I].Key->containsUnexpandedParameterPack() ||
4168 VK[I].Value->containsUnexpandedParameterPack()))
4169 ExprBits.ContainsUnexpandedParameterPack = true;
4170
4171 KeyValues[I].Key = VK[I].Key;
4172 KeyValues[I].Value = VK[I].Value;
4173 if (Expansions) {
4174 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4175 if (VK[I].NumExpansions)
4176 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4177 else
4178 Expansions[I].NumExpansionsPlusOne = 0;
4179 }
4180 }
4181}
4182
4183ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004184ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004185 ArrayRef<ObjCDictionaryElement> VK,
4186 bool HasPackExpansions,
4187 QualType T, ObjCMethodDecl *method,
4188 SourceRange SR) {
4189 unsigned ExpansionsSize = 0;
4190 if (HasPackExpansions)
4191 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4192
4193 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4194 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
Fariborz Jahanian9ad94aa2014-10-28 18:28:16 +00004195 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
Ted Kremeneke65b0862012-03-06 20:05:56 +00004196}
4197
4198ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004199ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004200 bool HasPackExpansions) {
4201 unsigned ExpansionsSize = 0;
4202 if (HasPackExpansions)
4203 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4204 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4205 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4206 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4207 HasPackExpansions);
4208}
4209
Craig Topperce7167c2013-08-22 04:58:56 +00004210ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004211 Expr *base,
4212 Expr *key, QualType T,
4213 ObjCMethodDecl *getMethod,
4214 ObjCMethodDecl *setMethod,
4215 SourceLocation RB) {
4216 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4217 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4218 OK_ObjCSubscript,
4219 getMethod, setMethod, RB);
4220}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004221
Benjamin Kramerc215e762012-08-24 11:54:20 +00004222AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004223 QualType t, AtomicOp op, SourceLocation RP)
4224 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4225 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004226 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004227{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004228 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4229 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004230 if (args[i]->isTypeDependent())
4231 ExprBits.TypeDependent = true;
4232 if (args[i]->isValueDependent())
4233 ExprBits.ValueDependent = true;
4234 if (args[i]->isInstantiationDependent())
4235 ExprBits.InstantiationDependent = true;
4236 if (args[i]->containsUnexpandedParameterPack())
4237 ExprBits.ContainsUnexpandedParameterPack = true;
4238
4239 SubExprs[i] = args[i];
4240 }
4241}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004242
4243unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4244 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004245 case AO__c11_atomic_init:
4246 case AO__c11_atomic_load:
4247 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004248 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004249
4250 case AO__c11_atomic_store:
4251 case AO__c11_atomic_exchange:
4252 case AO__atomic_load:
4253 case AO__atomic_store:
4254 case AO__atomic_store_n:
4255 case AO__atomic_exchange_n:
4256 case AO__c11_atomic_fetch_add:
4257 case AO__c11_atomic_fetch_sub:
4258 case AO__c11_atomic_fetch_and:
4259 case AO__c11_atomic_fetch_or:
4260 case AO__c11_atomic_fetch_xor:
4261 case AO__atomic_fetch_add:
4262 case AO__atomic_fetch_sub:
4263 case AO__atomic_fetch_and:
4264 case AO__atomic_fetch_or:
4265 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004266 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004267 case AO__atomic_add_fetch:
4268 case AO__atomic_sub_fetch:
4269 case AO__atomic_and_fetch:
4270 case AO__atomic_or_fetch:
4271 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004272 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004273 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004274
4275 case AO__atomic_exchange:
4276 return 4;
4277
4278 case AO__c11_atomic_compare_exchange_strong:
4279 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004280 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004281
4282 case AO__atomic_compare_exchange:
4283 case AO__atomic_compare_exchange_n:
4284 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004285 }
4286 llvm_unreachable("unknown atomic op");
4287}