blob: 842b8c7fdefbc94f0ce63a08d4bbfedc17f81a4e [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())
47 return NULL;
48
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();
124 default:
125 return false;
126 }
127 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000128
John McCall45d30c32010-06-12 01:56:02 +0000129 // Only look through implicit casts. If the user writes
130 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000131 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000132 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000133
Peter Collingbourne91147592011-04-15 00:35:48 +0000134 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000135 switch (BO->getOpcode()) {
136 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000137 case BO_LT: // Relational operators.
138 case BO_GT:
139 case BO_LE:
140 case BO_GE:
141 case BO_EQ: // Equality operators.
142 case BO_NE:
143 case BO_LAnd: // AND operator.
144 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000145 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000146
John McCalle3027922010-08-25 11:45:40 +0000147 case BO_And: // Bitwise AND operator.
148 case BO_Xor: // Bitwise XOR operator.
149 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000150 // Handle things like (x==2)|(y==12).
151 return BO->getLHS()->isKnownToHaveBooleanValue() &&
152 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000153
John McCalle3027922010-08-25 11:45:40 +0000154 case BO_Comma:
155 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000156 return BO->getRHS()->isKnownToHaveBooleanValue();
157 }
158 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000159
Peter Collingbourne91147592011-04-15 00:35:48 +0000160 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000161 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
162 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000163
Chris Lattner4ebae652010-04-16 23:34:13 +0000164 return false;
165}
166
John McCallbd066782011-02-09 08:16:59 +0000167// Amusing macro metaprogramming hack: check whether a class provides
168// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000169//
170// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000171namespace {
172 /// This implementation is used when a class provides a custom
173 /// implementation of getExprLoc.
174 template <class E, class T>
175 SourceLocation getExprLocImpl(const Expr *expr,
176 SourceLocation (T::*v)() const) {
177 return static_cast<const E*>(expr)->getExprLoc();
178 }
179
180 /// This implementation is used when a class doesn't provide
181 /// a custom implementation of getExprLoc. Overload resolution
182 /// should pick it over the implementation above because it's
183 /// more specialized according to function template partial ordering.
184 template <class E>
185 SourceLocation getExprLocImpl(const Expr *expr,
186 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000187 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000188 }
189}
190
191SourceLocation Expr::getExprLoc() const {
192 switch (getStmtClass()) {
193 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
194#define ABSTRACT_STMT(type)
195#define STMT(type, base) \
196 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
197#define EXPR(type, base) \
198 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
199#include "clang/AST/StmtNodes.inc"
200 }
201 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000202}
203
Chris Lattner0eedafe2006-08-24 04:56:27 +0000204//===----------------------------------------------------------------------===//
205// Primary Expressions.
206//===----------------------------------------------------------------------===//
207
Douglas Gregor678d76c2011-07-01 01:22:09 +0000208/// \brief Compute the type-, value-, and instantiation-dependence of a
209/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000210/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000211static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
212 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000213 bool &ValueDependent,
214 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000215 TypeDependent = false;
216 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000217 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000218
219 // (TD) C++ [temp.dep.expr]p3:
220 // An id-expression is type-dependent if it contains:
221 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000222 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000223 //
224 // (VD) C++ [temp.dep.constexpr]p2:
225 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000226
Douglas Gregored6c7442009-11-23 11:41:28 +0000227 // (TD) - an identifier that was declared with dependent type
228 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000229 if (T->isDependentType()) {
230 TypeDependent = true;
231 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000232 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000233 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000234 } else if (T->isInstantiationDependentType()) {
235 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000236 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000237
Douglas Gregored6c7442009-11-23 11:41:28 +0000238 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000240 == DeclarationName::CXXConversionFunctionName) {
241 QualType T = D->getDeclName().getCXXNameType();
242 if (T->isDependentType()) {
243 TypeDependent = true;
244 ValueDependent = true;
245 InstantiationDependent = true;
246 return;
247 }
248
249 if (T->isInstantiationDependentType())
250 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000251 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000252
Douglas Gregored6c7442009-11-23 11:41:28 +0000253 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000254 if (isa<NonTypeTemplateParmDecl>(D)) {
255 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000256 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000257 return;
258 }
259
Douglas Gregored6c7442009-11-23 11:41:28 +0000260 // (VD) - a constant with integral or enumeration type and is
261 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000262 // (VD) - a constant with literal type and is initialized with an
263 // expression that is value-dependent [C++11].
264 // (VD) - FIXME: Missing from the standard:
265 // - an entity with reference type and is initialized with an
266 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000267 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000268 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000269 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000270 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000271 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000272 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000273 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000274 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000275 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000276 InstantiationDependent = true;
277 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000278 }
279
Douglas Gregor0e4de762010-05-11 08:41:30 +0000280 // (VD) - FIXME: Missing from the standard:
281 // - a member function or a static data member of the current
282 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000283 if (Var->isStaticDataMember() &&
284 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000285 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000286 InstantiationDependent = true;
Richard Smith00f5d892013-11-14 22:40:45 +0000287 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
288 if (TInfo->getType()->isIncompleteArrayType())
289 TypeDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000290 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000291
292 return;
293 }
294
Douglas Gregor0e4de762010-05-11 08:41:30 +0000295 // (VD) - FIXME: Missing from the standard:
296 // - a member function or a static data member of the current
297 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000298 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
299 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000300 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000301 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000302}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000303
Craig Topperce7167c2013-08-22 04:58:56 +0000304void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000305 bool TypeDependent = false;
306 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000307 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000308 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
309 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000310
311 // (TD) C++ [temp.dep.expr]p3:
312 // An id-expression is type-dependent if it contains:
313 //
314 // and
315 //
316 // (VD) C++ [temp.dep.constexpr]p2:
317 // An identifier is value-dependent if it is:
318 if (!TypeDependent && !ValueDependent &&
319 hasExplicitTemplateArgs() &&
320 TemplateSpecializationType::anyDependentTemplateArguments(
321 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000322 getNumTemplateArgs(),
323 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000324 TypeDependent = true;
325 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000326 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000327 }
328
329 ExprBits.TypeDependent = TypeDependent;
330 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000331 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000332
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000333 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000334 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000335 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000336}
337
Craig Topperce7167c2013-08-22 04:58:56 +0000338DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000339 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000340 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000341 ValueDecl *D, bool RefersToEnclosingLocal,
342 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000343 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000344 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000345 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000346 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000347 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
348 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000349 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000350 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000351 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
352 if (FoundD)
353 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000354 DeclRefExprBits.HasTemplateKWAndArgsInfo
355 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000356 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000357 if (TemplateArgs) {
358 bool Dependent = false;
359 bool InstantiationDependent = false;
360 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000361 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
362 Dependent,
363 InstantiationDependent,
364 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000365 if (InstantiationDependent)
366 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000367 } else if (TemplateKWLoc.isValid()) {
368 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000369 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000370 DeclRefExprBits.HadMultipleCandidates = 0;
371
Daniel Dunbar9d355812012-03-09 01:51:51 +0000372 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000373}
374
Craig Topperce7167c2013-08-22 04:58:56 +0000375DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000376 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000378 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000379 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000380 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000381 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000382 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000383 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000384 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000386 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000387 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000388 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000389}
390
Craig Topperce7167c2013-08-22 04:58:56 +0000391DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000392 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000393 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000394 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000395 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000396 const DeclarationNameInfo &NameInfo,
397 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000398 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000399 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000400 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000401 // Filter out cases where the found Decl is the same as the value refenenced.
402 if (D == FoundD)
403 FoundD = 0;
404
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000405 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000406 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000407 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000408 if (FoundD)
409 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000410 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000411 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
412 else if (TemplateKWLoc.isValid())
413 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000414
Chris Lattner5c0b4052010-10-30 05:14:06 +0000415 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000416 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000417 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000418 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000419}
420
Craig Topperce7167c2013-08-22 04:58:56 +0000421DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000422 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000423 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000424 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000425 unsigned NumTemplateArgs) {
426 std::size_t Size = sizeof(DeclRefExpr);
427 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000428 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000429 if (HasFoundDecl)
430 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000431 if (HasTemplateKWAndArgsInfo)
432 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000433
Chris Lattner5c0b4052010-10-30 05:14:06 +0000434 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000435 return new (Mem) DeclRefExpr(EmptyShell());
436}
437
Daniel Dunbarb507f272012-03-09 15:39:15 +0000438SourceLocation DeclRefExpr::getLocStart() const {
439 if (hasQualifier())
440 return getQualifierLoc().getBeginLoc();
441 return getNameInfo().getLocStart();
442}
443SourceLocation DeclRefExpr::getLocEnd() const {
444 if (hasExplicitTemplateArgs())
445 return getRAngleLoc();
446 return getNameInfo().getLocEnd();
447}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000448
Anders Carlsson2fb08242009-09-08 18:24:21 +0000449// FIXME: Maybe this should use DeclPrinter with a special "print predefined
450// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000451std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
452 ASTContext &Context = CurrentDecl->getASTContext();
453
David Majnemerbed356a2013-11-06 23:31:56 +0000454 if (IT == PredefinedExpr::FuncDName) {
455 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
456 OwningPtr<MangleContext> MC;
457 MC.reset(Context.createMangleContext());
458
459 if (MC->shouldMangleDeclName(ND)) {
460 SmallString<256> Buffer;
461 llvm::raw_svector_ostream Out(Buffer);
462 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
463 MC->mangleCXXCtor(CD, Ctor_Base, Out);
464 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
465 MC->mangleCXXDtor(DD, Dtor_Base, Out);
466 else
467 MC->mangleName(ND, Out);
468
469 Out.flush();
470 if (!Buffer.empty() && Buffer.front() == '\01')
471 return Buffer.substr(1);
472 return Buffer.str();
473 } else
474 return ND->getIdentifier()->getName();
475 }
476 return "";
477 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000478 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000479 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000480 return FD->getNameAsString();
481
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000482 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000483 llvm::raw_svector_ostream Out(Name);
484
485 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000486 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000487 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000488 if (MD->isStatic())
489 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000490 }
491
David Blaikiebbafb8a2012-03-11 07:00:24 +0000492 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000493 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000494 llvm::raw_string_ostream POut(Proto);
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000495 FD->printQualifiedName(POut, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000496
Douglas Gregor11a434a2012-04-10 20:14:15 +0000497 const FunctionDecl *Decl = FD;
498 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
499 Decl = Pattern;
500 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000501 const FunctionProtoType *FT = 0;
502 if (FD->hasWrittenPrototype())
503 FT = dyn_cast<FunctionProtoType>(AFT);
504
Douglas Gregor11a434a2012-04-10 20:14:15 +0000505 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000506 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000507 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000508 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000509 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000510 }
511
512 if (FT->isVariadic()) {
513 if (FD->getNumParams()) POut << ", ";
514 POut << "...";
515 }
516 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000517 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000518
Sam Weinig4e83bd22009-12-27 01:38:20 +0000519 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000520 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000521 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000522 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000523 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000524 POut << " volatile";
525 RefQualifierKind Ref = MD->getRefQualifier();
526 if (Ref == RQ_LValue)
527 POut << " &";
528 else if (Ref == RQ_RValue)
529 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000530 }
531
Douglas Gregor11a434a2012-04-10 20:14:15 +0000532 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
533 SpecsTy Specs;
534 const DeclContext *Ctx = FD->getDeclContext();
535 while (Ctx && isa<NamedDecl>(Ctx)) {
536 const ClassTemplateSpecializationDecl *Spec
537 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
538 if (Spec && !Spec->isExplicitSpecialization())
539 Specs.push_back(Spec);
540 Ctx = Ctx->getParent();
541 }
542
543 std::string TemplateParams;
544 llvm::raw_string_ostream TOut(TemplateParams);
545 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
546 I != E; ++I) {
547 const TemplateParameterList *Params
548 = (*I)->getSpecializedTemplate()->getTemplateParameters();
549 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
550 assert(Params->size() == Args.size());
551 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
552 StringRef Param = Params->getParam(i)->getName();
553 if (Param.empty()) continue;
554 TOut << Param << " = ";
555 Args.get(i).print(Policy, TOut);
556 TOut << ", ";
557 }
558 }
559
560 FunctionTemplateSpecializationInfo *FSI
561 = FD->getTemplateSpecializationInfo();
562 if (FSI && !FSI->isExplicitSpecialization()) {
563 const TemplateParameterList* Params
564 = FSI->getTemplate()->getTemplateParameters();
565 const TemplateArgumentList* Args = FSI->TemplateArguments;
566 assert(Params->size() == Args->size());
567 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
568 StringRef Param = Params->getParam(i)->getName();
569 if (Param.empty()) continue;
570 TOut << Param << " = ";
571 Args->get(i).print(Policy, TOut);
572 TOut << ", ";
573 }
574 }
575
576 TOut.flush();
577 if (!TemplateParams.empty()) {
578 // remove the trailing comma and space
579 TemplateParams.resize(TemplateParams.size() - 2);
580 POut << " [" << TemplateParams << "]";
581 }
582
583 POut.flush();
584
Benjamin Kramer90f54222013-08-21 11:45:27 +0000585 // Print "auto" for all deduced return types. This includes C++1y return
586 // type deduction and lambdas. For trailing return types resolve the
587 // decltype expression. Otherwise print the real type when this is
588 // not a constructor or destructor.
589 if ((isa<CXXMethodDecl>(FD) &&
590 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
591 (FT && FT->getResultType()->getAs<AutoType>()))
592 Proto = "auto " + Proto;
593 else if (FT && FT->getResultType()->getAs<DecltypeType>())
594 FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
595 .getAsStringInternal(Proto, Policy);
596 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Sam Weinigd060ed42009-12-06 23:55:13 +0000597 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000598
599 Out << Proto;
600
601 Out.flush();
602 return Name.str().str();
603 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000604 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
605 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
606 // Skip to its enclosing function or method, but not its enclosing
607 // CapturedDecl.
608 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
609 const Decl *D = Decl::castFromDeclContext(DC);
610 return ComputeName(IT, D);
611 }
612 llvm_unreachable("CapturedDecl not inside a function or method");
613 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000614 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000615 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000616 llvm::raw_svector_ostream Out(Name);
617 Out << (MD->isInstanceMethod() ? '-' : '+');
618 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000619
620 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
621 // a null check to avoid a crash.
622 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000623 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000624
Anders Carlsson2fb08242009-09-08 18:24:21 +0000625 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000626 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000627 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000628
Anders Carlsson2fb08242009-09-08 18:24:21 +0000629 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000630 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000631 Out << ']';
632
633 Out.flush();
634 return Name.str().str();
635 }
636 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
637 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
638 return "top level";
639 }
640 return "";
641}
642
Craig Topper37932912013-08-18 10:09:15 +0000643void APNumericStorage::setIntValue(const ASTContext &C,
644 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000645 if (hasAllocation())
646 C.Deallocate(pVal);
647
648 BitWidth = Val.getBitWidth();
649 unsigned NumWords = Val.getNumWords();
650 const uint64_t* Words = Val.getRawData();
651 if (NumWords > 1) {
652 pVal = new (C) uint64_t[NumWords];
653 std::copy(Words, Words + NumWords, pVal);
654 } else if (NumWords == 1)
655 VAL = Words[0];
656 else
657 VAL = 0;
658}
659
Craig Topper37932912013-08-18 10:09:15 +0000660IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000661 QualType type, SourceLocation l)
662 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
663 false, false),
664 Loc(l) {
665 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
666 assert(V.getBitWidth() == C.getIntWidth(type) &&
667 "Integer type is not the correct size for constant.");
668 setValue(C, V);
669}
670
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000671IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000672IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000673 QualType type, SourceLocation l) {
674 return new (C) IntegerLiteral(C, V, type, l);
675}
676
677IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000678IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000679 return new (C) IntegerLiteral(Empty);
680}
681
Craig Topper37932912013-08-18 10:09:15 +0000682FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000683 bool isexact, QualType Type, SourceLocation L)
684 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
685 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000686 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000687 FloatingLiteralBits.IsExact = isexact;
688 setValue(C, V);
689}
690
Craig Topper37932912013-08-18 10:09:15 +0000691FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000692 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000693 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000694 FloatingLiteralBits.IsExact = false;
695}
696
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000697FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000698FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000699 bool isexact, QualType Type, SourceLocation L) {
700 return new (C) FloatingLiteral(C, V, isexact, Type, L);
701}
702
703FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000704FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000705 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000706}
707
Tim Northover178723a2013-01-22 09:46:51 +0000708const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
709 switch(FloatingLiteralBits.Semantics) {
710 case IEEEhalf:
711 return llvm::APFloat::IEEEhalf;
712 case IEEEsingle:
713 return llvm::APFloat::IEEEsingle;
714 case IEEEdouble:
715 return llvm::APFloat::IEEEdouble;
716 case x87DoubleExtended:
717 return llvm::APFloat::x87DoubleExtended;
718 case IEEEquad:
719 return llvm::APFloat::IEEEquad;
720 case PPCDoubleDouble:
721 return llvm::APFloat::PPCDoubleDouble;
722 }
723 llvm_unreachable("Unrecognised floating semantics");
724}
725
726void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
727 if (&Sem == &llvm::APFloat::IEEEhalf)
728 FloatingLiteralBits.Semantics = IEEEhalf;
729 else if (&Sem == &llvm::APFloat::IEEEsingle)
730 FloatingLiteralBits.Semantics = IEEEsingle;
731 else if (&Sem == &llvm::APFloat::IEEEdouble)
732 FloatingLiteralBits.Semantics = IEEEdouble;
733 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
734 FloatingLiteralBits.Semantics = x87DoubleExtended;
735 else if (&Sem == &llvm::APFloat::IEEEquad)
736 FloatingLiteralBits.Semantics = IEEEquad;
737 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
738 FloatingLiteralBits.Semantics = PPCDoubleDouble;
739 else
740 llvm_unreachable("Unknown floating semantics");
741}
742
Chris Lattnera0173132008-06-07 22:13:43 +0000743/// getValueAsApproximateDouble - This returns the value as an inaccurate
744/// double. Note that this may cause loss of precision, but is useful for
745/// debugging dumps, etc.
746double FloatingLiteral::getValueAsApproximateDouble() const {
747 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000748 bool ignored;
749 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
750 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000751 return V.convertToDouble();
752}
753
Nick Lewycky4ed84042012-02-24 09:07:53 +0000754int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000755 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000756 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000757 case Ascii:
758 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000759 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000760 break;
761 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000762 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000763 break;
764 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000765 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000766 break;
767 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000768 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000769 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000770 }
771 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
772 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000773 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000774 && "character byte widths supported are 1, 2, and 4 only");
775 return CharByteWidth;
776}
777
Craig Topper37932912013-08-18 10:09:15 +0000778StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000779 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000780 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000781 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000782 // Allocate enough space for the StringLiteral plus an array of locations for
783 // any concatenated string tokens.
784 void *Mem = C.Allocate(sizeof(StringLiteral)+
785 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000786 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000787 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000788
Steve Naroffdf7855b2007-02-21 23:46:25 +0000789 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000790 SL->setString(C,Str,Kind,Pascal);
791
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000792 SL->TokLocs[0] = Loc[0];
793 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000794
Chris Lattner630970d2009-02-18 05:49:11 +0000795 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000796 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
797 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000798}
799
Craig Topper37932912013-08-18 10:09:15 +0000800StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
801 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000802 void *Mem = C.Allocate(sizeof(StringLiteral)+
803 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000804 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000805 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000806 SL->CharByteWidth = 0;
807 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000808 SL->NumConcatenated = NumStrs;
809 return SL;
810}
811
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000812void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000813 switch (getKind()) {
814 case Ascii: break; // no prefix.
815 case Wide: OS << 'L'; break;
816 case UTF8: OS << "u8"; break;
817 case UTF16: OS << 'u'; break;
818 case UTF32: OS << 'U'; break;
819 }
820 OS << '"';
821 static const char Hex[] = "0123456789ABCDEF";
822
823 unsigned LastSlashX = getLength();
824 for (unsigned I = 0, N = getLength(); I != N; ++I) {
825 switch (uint32_t Char = getCodeUnit(I)) {
826 default:
827 // FIXME: Convert UTF-8 back to codepoints before rendering.
828
829 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
830 // Leave invalid surrogates alone; we'll use \x for those.
831 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
832 Char <= 0xdbff) {
833 uint32_t Trail = getCodeUnit(I + 1);
834 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
835 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
836 ++I;
837 }
838 }
839
840 if (Char > 0xff) {
841 // If this is a wide string, output characters over 0xff using \x
842 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
843 // codepoint: use \x escapes for invalid codepoints.
844 if (getKind() == Wide ||
845 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
846 // FIXME: Is this the best way to print wchar_t?
847 OS << "\\x";
848 int Shift = 28;
849 while ((Char >> Shift) == 0)
850 Shift -= 4;
851 for (/**/; Shift >= 0; Shift -= 4)
852 OS << Hex[(Char >> Shift) & 15];
853 LastSlashX = I;
854 break;
855 }
856
857 if (Char > 0xffff)
858 OS << "\\U00"
859 << Hex[(Char >> 20) & 15]
860 << Hex[(Char >> 16) & 15];
861 else
862 OS << "\\u";
863 OS << Hex[(Char >> 12) & 15]
864 << Hex[(Char >> 8) & 15]
865 << Hex[(Char >> 4) & 15]
866 << Hex[(Char >> 0) & 15];
867 break;
868 }
869
870 // If we used \x... for the previous character, and this character is a
871 // hexadecimal digit, prevent it being slurped as part of the \x.
872 if (LastSlashX + 1 == I) {
873 switch (Char) {
874 case '0': case '1': case '2': case '3': case '4':
875 case '5': case '6': case '7': case '8': case '9':
876 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
877 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
878 OS << "\"\"";
879 }
880 }
881
882 assert(Char <= 0xff &&
883 "Characters above 0xff should already have been handled.");
884
Jordan Rosea7d03842013-02-08 22:30:41 +0000885 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000886 OS << (char)Char;
887 else // Output anything hard as an octal escape.
888 OS << '\\'
889 << (char)('0' + ((Char >> 6) & 7))
890 << (char)('0' + ((Char >> 3) & 7))
891 << (char)('0' + ((Char >> 0) & 7));
892 break;
893 // Handle some common non-printable cases to make dumps prettier.
894 case '\\': OS << "\\\\"; break;
895 case '"': OS << "\\\""; break;
896 case '\n': OS << "\\n"; break;
897 case '\t': OS << "\\t"; break;
898 case '\a': OS << "\\a"; break;
899 case '\b': OS << "\\b"; break;
900 }
901 }
902 OS << '"';
903}
904
Craig Topper37932912013-08-18 10:09:15 +0000905void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000906 StringKind Kind, bool IsPascal) {
907 //FIXME: we assume that the string data comes from a target that uses the same
908 // code unit size and endianess for the type of string.
909 this->Kind = Kind;
910 this->IsPascal = IsPascal;
911
Nick Lewycky4ed84042012-02-24 09:07:53 +0000912 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000913 assert((Str.size()%CharByteWidth == 0)
914 && "size of data must be multiple of CharByteWidth");
915 Length = Str.size()/CharByteWidth;
916
917 switch(CharByteWidth) {
918 case 1: {
919 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000920 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000921 StrData.asChar = AStrData;
922 break;
923 }
924 case 2: {
925 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000926 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000927 StrData.asUInt16 = AStrData;
928 break;
929 }
930 case 4: {
931 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000932 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000933 StrData.asUInt32 = AStrData;
934 break;
935 }
936 default:
937 assert(false && "unsupported CharByteWidth");
938 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000939}
940
Chris Lattnere925d612010-11-17 07:37:15 +0000941/// getLocationOfByte - Return a source location that points to the specified
942/// byte of this string literal.
943///
944/// Strings are amazingly complex. They can be formed from multiple tokens and
945/// can have escape sequences in them in addition to the usual trigraph and
946/// escaped newline business. This routine handles this complexity.
947///
948SourceLocation StringLiteral::
949getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
950 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000951 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
952 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000953
Chris Lattnere925d612010-11-17 07:37:15 +0000954 // Loop over all of the tokens in this string until we find the one that
955 // contains the byte we're looking for.
956 unsigned TokNo = 0;
957 while (1) {
958 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
959 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
960
961 // Get the spelling of the string so that we can get the data that makes up
962 // the string literal, not the identifier for the macro it is potentially
963 // expanded through.
964 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
965
966 // Re-lex the token to get its length and original spelling.
967 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
968 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000969 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000970 if (Invalid)
971 return StrTokSpellingLoc;
972
973 const char *StrData = Buffer.data()+LocInfo.second;
974
Chris Lattnere925d612010-11-17 07:37:15 +0000975 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000976 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
977 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000978 Token TheTok;
979 TheLexer.LexFromRawLexer(TheTok);
980
981 // Use the StringLiteralParser to compute the length of the string in bytes.
982 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
983 unsigned TokNumBytes = SLP.GetStringLength();
984
985 // If the byte is in this token, return the location of the byte.
986 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000987 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000988 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
989
990 // Now that we know the offset of the token in the spelling, use the
991 // preprocessor to get the offset in the original source.
992 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
993 }
994
995 // Move to the next string token.
996 ++TokNo;
997 ByteNo -= TokNumBytes;
998 }
999}
1000
1001
1002
Chris Lattner1b926492006-08-23 06:42:10 +00001003/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1004/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001005StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001006 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001007 case UO_PostInc: return "++";
1008 case UO_PostDec: return "--";
1009 case UO_PreInc: return "++";
1010 case UO_PreDec: return "--";
1011 case UO_AddrOf: return "&";
1012 case UO_Deref: return "*";
1013 case UO_Plus: return "+";
1014 case UO_Minus: return "-";
1015 case UO_Not: return "~";
1016 case UO_LNot: return "!";
1017 case UO_Real: return "__real";
1018 case UO_Imag: return "__imag";
1019 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001020 }
David Blaikief47fa302012-01-17 02:30:50 +00001021 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001022}
1023
John McCalle3027922010-08-25 11:45:40 +00001024UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001025UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1026 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001027 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001028 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1029 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1030 case OO_Amp: return UO_AddrOf;
1031 case OO_Star: return UO_Deref;
1032 case OO_Plus: return UO_Plus;
1033 case OO_Minus: return UO_Minus;
1034 case OO_Tilde: return UO_Not;
1035 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001036 }
1037}
1038
1039OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1040 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001041 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1042 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1043 case UO_AddrOf: return OO_Amp;
1044 case UO_Deref: return OO_Star;
1045 case UO_Plus: return OO_Plus;
1046 case UO_Minus: return OO_Minus;
1047 case UO_Not: return OO_Tilde;
1048 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001049 default: return OO_None;
1050 }
1051}
1052
1053
Chris Lattner0eedafe2006-08-24 04:56:27 +00001054//===----------------------------------------------------------------------===//
1055// Postfix Operators.
1056//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001057
Craig Topper37932912013-08-18 10:09:15 +00001058CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1059 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1060 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001061 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001062 fn->isTypeDependent(),
1063 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001064 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001065 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001066 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001067
Benjamin Kramerc215e762012-08-24 11:54:20 +00001068 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001069 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001070 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001071 if (args[i]->isTypeDependent())
1072 ExprBits.TypeDependent = true;
1073 if (args[i]->isValueDependent())
1074 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001075 if (args[i]->isInstantiationDependent())
1076 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001077 if (args[i]->containsUnexpandedParameterPack())
1078 ExprBits.ContainsUnexpandedParameterPack = true;
1079
Peter Collingbourne3a347252011-02-08 21:18:02 +00001080 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001081 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001082
Peter Collingbourne3a347252011-02-08 21:18:02 +00001083 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001084 RParenLoc = rparenloc;
1085}
Nate Begeman1e36a852008-01-17 17:46:27 +00001086
Craig Topper37932912013-08-18 10:09:15 +00001087CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001088 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1089 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001090 fn->isTypeDependent(),
1091 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001092 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001093 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001094 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001095
Benjamin Kramerc215e762012-08-24 11:54:20 +00001096 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001097 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001098 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001099 if (args[i]->isTypeDependent())
1100 ExprBits.TypeDependent = true;
1101 if (args[i]->isValueDependent())
1102 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001103 if (args[i]->isInstantiationDependent())
1104 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001105 if (args[i]->containsUnexpandedParameterPack())
1106 ExprBits.ContainsUnexpandedParameterPack = true;
1107
Peter Collingbourne3a347252011-02-08 21:18:02 +00001108 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001109 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001110
Peter Collingbourne3a347252011-02-08 21:18:02 +00001111 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001112 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001113}
1114
Craig Topper37932912013-08-18 10:09:15 +00001115CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump11289f42009-09-09 15:08:12 +00001116 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001117 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001118 SubExprs = new (C) Stmt*[PREARGS_START];
1119 CallExprBits.NumPreArgs = 0;
1120}
1121
Craig Topper37932912013-08-18 10:09:15 +00001122CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001123 EmptyShell Empty)
1124 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1125 // FIXME: Why do we allocate this?
1126 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1127 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001128}
1129
Nuno Lopes518e3702009-12-20 23:11:08 +00001130Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001131 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001132
1133 while (SubstNonTypeTemplateParmExpr *NTTP
1134 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1135 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1136 }
1137
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001138 // If we're calling a dereference, look at the pointer instead.
1139 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1140 if (BO->isPtrMemOp())
1141 CEE = BO->getRHS()->IgnoreParenCasts();
1142 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1143 if (UO->getOpcode() == UO_Deref)
1144 CEE = UO->getSubExpr()->IgnoreParenCasts();
1145 }
Chris Lattner52301912009-07-17 15:46:27 +00001146 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001147 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001148 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1149 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001150
1151 return 0;
1152}
1153
Nuno Lopes518e3702009-12-20 23:11:08 +00001154FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001155 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001156}
1157
Chris Lattnere4407ed2007-12-28 05:25:02 +00001158/// setNumArgs - This changes the number of arguments present in this call.
1159/// Any orphaned expressions are deleted by this, and any new operands are set
1160/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001161void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001162 // No change, just return.
1163 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattnere4407ed2007-12-28 05:25:02 +00001165 // If shrinking # arguments, just delete the extras and forgot them.
1166 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001167 this->NumArgs = NumArgs;
1168 return;
1169 }
1170
1171 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001172 unsigned NumPreArgs = getNumPreArgs();
1173 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001174 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001175 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001176 NewSubExprs[i] = SubExprs[i];
1177 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001178 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1179 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001180 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Douglas Gregorba6e5572009-04-17 21:46:47 +00001182 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001183 SubExprs = NewSubExprs;
1184 this->NumArgs = NumArgs;
1185}
1186
Alp Tokera724cff2013-12-28 21:59:02 +00001187/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001188/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001189unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001190 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001191 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001192 // ImplicitCastExpr.
1193 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1194 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001195 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001196
Steve Narofff6e3b3292008-01-31 01:07:12 +00001197 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1198 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001199 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001200
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001201 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1202 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001203 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001205 if (!FDecl->getIdentifier())
1206 return 0;
1207
Douglas Gregor15fc9562009-09-12 00:22:50 +00001208 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001209}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001210
Richard Smith5011a002013-01-17 23:46:04 +00001211bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001212 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001213 return Ctx.BuiltinInfo.isUnevaluated(BI);
1214 return false;
1215}
1216
Anders Carlsson00a27592009-05-26 04:57:27 +00001217QualType CallExpr::getCallReturnType() const {
1218 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001219 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001220 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001221 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001222 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001223 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1224 // This should never be overloaded and so should never return null.
1225 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001226
John McCall0009fcc2011-04-26 20:42:42 +00001227 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001228 return FnType->getResultType();
1229}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001230
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001231SourceLocation CallExpr::getLocStart() const {
1232 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001233 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001234
1235 SourceLocation begin = getCallee()->getLocStart();
1236 if (begin.isInvalid() && getNumArgs() > 0)
1237 begin = getArg(0)->getLocStart();
1238 return begin;
1239}
1240SourceLocation CallExpr::getLocEnd() const {
1241 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001242 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001243
1244 SourceLocation end = getRParenLoc();
1245 if (end.isInvalid() && getNumArgs() > 0)
1246 end = getArg(getNumArgs() - 1)->getLocEnd();
1247 return end;
1248}
John McCall701417a2011-02-21 06:23:05 +00001249
Craig Topper37932912013-08-18 10:09:15 +00001250OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001251 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001252 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001253 ArrayRef<OffsetOfNode> comps,
1254 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001255 SourceLocation RParenLoc) {
1256 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001257 sizeof(OffsetOfNode) * comps.size() +
1258 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001259
Benjamin Kramerc215e762012-08-24 11:54:20 +00001260 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1261 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001262}
1263
Craig Topper37932912013-08-18 10:09:15 +00001264OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001265 unsigned numComps, unsigned numExprs) {
1266 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1267 sizeof(OffsetOfNode) * numComps +
1268 sizeof(Expr*) * numExprs);
1269 return new (Mem) OffsetOfExpr(numComps, numExprs);
1270}
1271
Craig Topper37932912013-08-18 10:09:15 +00001272OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001273 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001274 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001275 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001276 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1277 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001278 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001279 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001280 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001281 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001282 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001283{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001284 for (unsigned i = 0; i != comps.size(); ++i) {
1285 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001286 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001287
Benjamin Kramerc215e762012-08-24 11:54:20 +00001288 for (unsigned i = 0; i != exprs.size(); ++i) {
1289 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001290 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001291 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001292 ExprBits.ContainsUnexpandedParameterPack = true;
1293
Benjamin Kramerc215e762012-08-24 11:54:20 +00001294 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001295 }
1296}
1297
1298IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1299 assert(getKind() == Field || getKind() == Identifier);
1300 if (getKind() == Field)
1301 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001302
Douglas Gregor882211c2010-04-28 22:16:22 +00001303 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1304}
1305
Craig Topper37932912013-08-18 10:09:15 +00001306MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001307 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001308 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001309 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001310 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001311 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001312 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001313 QualType ty,
1314 ExprValueKind vk,
1315 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001316 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001317
Douglas Gregorea972d32011-02-28 21:54:11 +00001318 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001319 founddecl.getDecl() != memberdecl ||
1320 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001321 if (hasQualOrFound)
1322 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001323
John McCall6b51f282009-11-23 01:53:49 +00001324 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001325 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1326 else if (TemplateKWLoc.isValid())
1327 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattner5c0b4052010-10-30 05:14:06 +00001329 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001330 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1331 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001332
1333 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001334 // FIXME: Wrong. We should be looking at the member declaration we found.
1335 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001336 E->setValueDependent(true);
1337 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001338 E->setInstantiationDependent(true);
1339 }
1340 else if (QualifierLoc &&
1341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1342 E->setInstantiationDependent(true);
1343
John McCall16df1e52010-03-30 21:47:33 +00001344 E->HasQualifierOrFoundDecl = true;
1345
1346 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001347 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001348 NQ->FoundDecl = founddecl;
1349 }
1350
Abramo Bagnara7945c982012-01-27 09:46:47 +00001351 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1352
John McCall16df1e52010-03-30 21:47:33 +00001353 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001354 bool Dependent = false;
1355 bool InstantiationDependent = false;
1356 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001357 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1358 Dependent,
1359 InstantiationDependent,
1360 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001361 if (InstantiationDependent)
1362 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001363 } else if (TemplateKWLoc.isValid()) {
1364 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001365 }
1366
1367 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001368}
1369
Daniel Dunbarb507f272012-03-09 15:39:15 +00001370SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001371 if (isImplicitAccess()) {
1372 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001373 return getQualifierLoc().getBeginLoc();
1374 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001375 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001376
Daniel Dunbarb507f272012-03-09 15:39:15 +00001377 // FIXME: We don't want this to happen. Rather, we should be able to
1378 // detect all kinds of implicit accesses more cleanly.
1379 SourceLocation BaseStartLoc = getBase()->getLocStart();
1380 if (BaseStartLoc.isValid())
1381 return BaseStartLoc;
1382 return MemberLoc;
1383}
1384SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001385 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001386 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001387 EndLoc = getRAngleLoc();
1388 else if (EndLoc.isInvalid())
1389 EndLoc = getBase()->getLocEnd();
1390 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001391}
1392
Alp Tokerc1086762013-12-07 13:51:35 +00001393bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001394 switch (getCastKind()) {
1395 case CK_DerivedToBase:
1396 case CK_UncheckedDerivedToBase:
1397 case CK_DerivedToBaseMemberPointer:
1398 case CK_BaseToDerived:
1399 case CK_BaseToDerivedMemberPointer:
1400 assert(!path_empty() && "Cast kind should have a base path!");
1401 break;
1402
1403 case CK_CPointerToObjCPointerCast:
1404 assert(getType()->isObjCObjectPointerType());
1405 assert(getSubExpr()->getType()->isPointerType());
1406 goto CheckNoBasePath;
1407
1408 case CK_BlockPointerToObjCPointerCast:
1409 assert(getType()->isObjCObjectPointerType());
1410 assert(getSubExpr()->getType()->isBlockPointerType());
1411 goto CheckNoBasePath;
1412
John McCallc62bb392012-02-15 01:22:51 +00001413 case CK_ReinterpretMemberPointer:
1414 assert(getType()->isMemberPointerType());
1415 assert(getSubExpr()->getType()->isMemberPointerType());
1416 goto CheckNoBasePath;
1417
John McCall9320b872011-09-09 05:25:32 +00001418 case CK_BitCast:
1419 // Arbitrary casts to C pointer types count as bitcasts.
1420 // Otherwise, we should only have block and ObjC pointer casts
1421 // here if they stay within the type kind.
1422 if (!getType()->isPointerType()) {
1423 assert(getType()->isObjCObjectPointerType() ==
1424 getSubExpr()->getType()->isObjCObjectPointerType());
1425 assert(getType()->isBlockPointerType() ==
1426 getSubExpr()->getType()->isBlockPointerType());
1427 }
1428 goto CheckNoBasePath;
1429
1430 case CK_AnyPointerToBlockPointerCast:
1431 assert(getType()->isBlockPointerType());
1432 assert(getSubExpr()->getType()->isAnyPointerType() &&
1433 !getSubExpr()->getType()->isBlockPointerType());
1434 goto CheckNoBasePath;
1435
Douglas Gregored90df32012-02-22 05:02:47 +00001436 case CK_CopyAndAutoreleaseBlockObject:
1437 assert(getType()->isBlockPointerType());
1438 assert(getSubExpr()->getType()->isBlockPointerType());
1439 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001440
1441 case CK_FunctionToPointerDecay:
1442 assert(getType()->isPointerType());
1443 assert(getSubExpr()->getType()->isFunctionType());
1444 goto CheckNoBasePath;
1445
David Tweede1468322013-12-11 13:39:46 +00001446 case CK_AddressSpaceConversion:
1447 assert(getType()->isPointerType());
1448 assert(getSubExpr()->getType()->isPointerType());
1449 assert(getType()->getPointeeType().getAddressSpace() !=
1450 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001451 // These should not have an inheritance path.
1452 case CK_Dynamic:
1453 case CK_ToUnion:
1454 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001455 case CK_NullToMemberPointer:
1456 case CK_NullToPointer:
1457 case CK_ConstructorConversion:
1458 case CK_IntegralToPointer:
1459 case CK_PointerToIntegral:
1460 case CK_ToVoid:
1461 case CK_VectorSplat:
1462 case CK_IntegralCast:
1463 case CK_IntegralToFloating:
1464 case CK_FloatingToIntegral:
1465 case CK_FloatingCast:
1466 case CK_ObjCObjectLValueCast:
1467 case CK_FloatingRealToComplex:
1468 case CK_FloatingComplexToReal:
1469 case CK_FloatingComplexCast:
1470 case CK_FloatingComplexToIntegralComplex:
1471 case CK_IntegralRealToComplex:
1472 case CK_IntegralComplexToReal:
1473 case CK_IntegralComplexCast:
1474 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001475 case CK_ARCProduceObject:
1476 case CK_ARCConsumeObject:
1477 case CK_ARCReclaimReturnedObject:
1478 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001479 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001480 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1481 goto CheckNoBasePath;
1482
1483 case CK_Dependent:
1484 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001485 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001486 case CK_AtomicToNonAtomic:
1487 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001488 case CK_PointerToBoolean:
1489 case CK_IntegralToBoolean:
1490 case CK_FloatingToBoolean:
1491 case CK_MemberPointerToBoolean:
1492 case CK_FloatingComplexToBoolean:
1493 case CK_IntegralComplexToBoolean:
1494 case CK_LValueBitCast: // -> bool&
1495 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001496 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001497 CheckNoBasePath:
1498 assert(path_empty() && "Cast kind should not have a base path!");
1499 break;
1500 }
Alp Tokerc1086762013-12-07 13:51:35 +00001501 return true;
John McCall9320b872011-09-09 05:25:32 +00001502}
1503
Anders Carlsson496335e2009-09-03 00:59:21 +00001504const char *CastExpr::getCastKindName() const {
1505 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001506 case CK_Dependent:
1507 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001508 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001509 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001510 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001511 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001512 case CK_LValueToRValue:
1513 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001514 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001515 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001516 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001517 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001518 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001519 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001520 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001521 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001522 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001523 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001524 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001525 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001526 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001527 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001528 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001529 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001530 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001531 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001532 case CK_NullToPointer:
1533 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001534 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001535 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001536 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001537 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001538 case CK_ReinterpretMemberPointer:
1539 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001540 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001541 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001542 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001543 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001544 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001545 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001546 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001547 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001548 case CK_PointerToBoolean:
1549 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001550 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001551 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001552 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001553 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001554 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001555 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001556 case CK_IntegralToBoolean:
1557 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001558 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001559 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001560 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001561 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001562 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001563 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001564 case CK_FloatingToBoolean:
1565 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001566 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001567 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001568 case CK_CPointerToObjCPointerCast:
1569 return "CPointerToObjCPointerCast";
1570 case CK_BlockPointerToObjCPointerCast:
1571 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001572 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001573 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001574 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001575 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001576 case CK_FloatingRealToComplex:
1577 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001578 case CK_FloatingComplexToReal:
1579 return "FloatingComplexToReal";
1580 case CK_FloatingComplexToBoolean:
1581 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001582 case CK_FloatingComplexCast:
1583 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001584 case CK_FloatingComplexToIntegralComplex:
1585 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001586 case CK_IntegralRealToComplex:
1587 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001588 case CK_IntegralComplexToReal:
1589 return "IntegralComplexToReal";
1590 case CK_IntegralComplexToBoolean:
1591 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001592 case CK_IntegralComplexCast:
1593 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001594 case CK_IntegralComplexToFloatingComplex:
1595 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001596 case CK_ARCConsumeObject:
1597 return "ARCConsumeObject";
1598 case CK_ARCProduceObject:
1599 return "ARCProduceObject";
1600 case CK_ARCReclaimReturnedObject:
1601 return "ARCReclaimReturnedObject";
1602 case CK_ARCExtendBlockObject:
1603 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001604 case CK_AtomicToNonAtomic:
1605 return "AtomicToNonAtomic";
1606 case CK_NonAtomicToAtomic:
1607 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001608 case CK_CopyAndAutoreleaseBlockObject:
1609 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001610 case CK_BuiltinFnToFnPtr:
1611 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001612 case CK_ZeroToOCLEvent:
1613 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001614 case CK_AddressSpaceConversion:
1615 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001616 }
Mike Stump11289f42009-09-09 15:08:12 +00001617
John McCallc5e62b42010-11-13 09:02:35 +00001618 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001619}
1620
Douglas Gregord196a582009-12-14 19:27:10 +00001621Expr *CastExpr::getSubExprAsWritten() {
1622 Expr *SubExpr = 0;
1623 CastExpr *E = this;
1624 do {
1625 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001626
1627 // Skip through reference binding to temporary.
1628 if (MaterializeTemporaryExpr *Materialize
1629 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1630 SubExpr = Materialize->GetTemporaryExpr();
1631
Douglas Gregord196a582009-12-14 19:27:10 +00001632 // Skip any temporary bindings; they're implicit.
1633 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1634 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001635
Douglas Gregord196a582009-12-14 19:27:10 +00001636 // Conversions by constructor and conversion functions have a
1637 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001638 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001639 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001640 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001641 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001642
Douglas Gregord196a582009-12-14 19:27:10 +00001643 // If the subexpression we're left with is an implicit cast, look
1644 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001645 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1646
Douglas Gregord196a582009-12-14 19:27:10 +00001647 return SubExpr;
1648}
1649
John McCallcf142162010-08-07 06:22:56 +00001650CXXBaseSpecifier **CastExpr::path_buffer() {
1651 switch (getStmtClass()) {
1652#define ABSTRACT_STMT(x)
1653#define CASTEXPR(Type, Base) \
1654 case Stmt::Type##Class: \
1655 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1656#define STMT(Type, Base)
1657#include "clang/AST/StmtNodes.inc"
1658 default:
1659 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001660 }
1661}
1662
1663void CastExpr::setCastPath(const CXXCastPath &Path) {
1664 assert(Path.size() == path_size());
1665 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1666}
1667
Craig Topper37932912013-08-18 10:09:15 +00001668ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001669 CastKind Kind, Expr *Operand,
1670 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001671 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001672 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1673 void *Buffer =
1674 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1675 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001676 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001677 if (PathSize) E->setCastPath(*BasePath);
1678 return E;
1679}
1680
Craig Topper37932912013-08-18 10:09:15 +00001681ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001682 unsigned PathSize) {
1683 void *Buffer =
1684 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1685 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1686}
1687
1688
Craig Topper37932912013-08-18 10:09:15 +00001689CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001690 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001691 const CXXCastPath *BasePath,
1692 TypeSourceInfo *WrittenTy,
1693 SourceLocation L, SourceLocation R) {
1694 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1695 void *Buffer =
1696 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1697 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001698 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001699 if (PathSize) E->setCastPath(*BasePath);
1700 return E;
1701}
1702
Craig Topper37932912013-08-18 10:09:15 +00001703CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1704 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001705 void *Buffer =
1706 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1707 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1708}
1709
Chris Lattner1b926492006-08-23 06:42:10 +00001710/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1711/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001712StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001713 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001714 case BO_PtrMemD: return ".*";
1715 case BO_PtrMemI: return "->*";
1716 case BO_Mul: return "*";
1717 case BO_Div: return "/";
1718 case BO_Rem: return "%";
1719 case BO_Add: return "+";
1720 case BO_Sub: return "-";
1721 case BO_Shl: return "<<";
1722 case BO_Shr: return ">>";
1723 case BO_LT: return "<";
1724 case BO_GT: return ">";
1725 case BO_LE: return "<=";
1726 case BO_GE: return ">=";
1727 case BO_EQ: return "==";
1728 case BO_NE: return "!=";
1729 case BO_And: return "&";
1730 case BO_Xor: return "^";
1731 case BO_Or: return "|";
1732 case BO_LAnd: return "&&";
1733 case BO_LOr: return "||";
1734 case BO_Assign: return "=";
1735 case BO_MulAssign: return "*=";
1736 case BO_DivAssign: return "/=";
1737 case BO_RemAssign: return "%=";
1738 case BO_AddAssign: return "+=";
1739 case BO_SubAssign: return "-=";
1740 case BO_ShlAssign: return "<<=";
1741 case BO_ShrAssign: return ">>=";
1742 case BO_AndAssign: return "&=";
1743 case BO_XorAssign: return "^=";
1744 case BO_OrAssign: return "|=";
1745 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001746 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001747
David Blaikiee4d798f2012-01-20 21:50:17 +00001748 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001749}
Steve Naroff47500512007-04-19 23:00:49 +00001750
John McCalle3027922010-08-25 11:45:40 +00001751BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001752BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1753 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001754 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001755 case OO_Plus: return BO_Add;
1756 case OO_Minus: return BO_Sub;
1757 case OO_Star: return BO_Mul;
1758 case OO_Slash: return BO_Div;
1759 case OO_Percent: return BO_Rem;
1760 case OO_Caret: return BO_Xor;
1761 case OO_Amp: return BO_And;
1762 case OO_Pipe: return BO_Or;
1763 case OO_Equal: return BO_Assign;
1764 case OO_Less: return BO_LT;
1765 case OO_Greater: return BO_GT;
1766 case OO_PlusEqual: return BO_AddAssign;
1767 case OO_MinusEqual: return BO_SubAssign;
1768 case OO_StarEqual: return BO_MulAssign;
1769 case OO_SlashEqual: return BO_DivAssign;
1770 case OO_PercentEqual: return BO_RemAssign;
1771 case OO_CaretEqual: return BO_XorAssign;
1772 case OO_AmpEqual: return BO_AndAssign;
1773 case OO_PipeEqual: return BO_OrAssign;
1774 case OO_LessLess: return BO_Shl;
1775 case OO_GreaterGreater: return BO_Shr;
1776 case OO_LessLessEqual: return BO_ShlAssign;
1777 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1778 case OO_EqualEqual: return BO_EQ;
1779 case OO_ExclaimEqual: return BO_NE;
1780 case OO_LessEqual: return BO_LE;
1781 case OO_GreaterEqual: return BO_GE;
1782 case OO_AmpAmp: return BO_LAnd;
1783 case OO_PipePipe: return BO_LOr;
1784 case OO_Comma: return BO_Comma;
1785 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001786 }
1787}
1788
1789OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1790 static const OverloadedOperatorKind OverOps[] = {
1791 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1792 OO_Star, OO_Slash, OO_Percent,
1793 OO_Plus, OO_Minus,
1794 OO_LessLess, OO_GreaterGreater,
1795 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1796 OO_EqualEqual, OO_ExclaimEqual,
1797 OO_Amp,
1798 OO_Caret,
1799 OO_Pipe,
1800 OO_AmpAmp,
1801 OO_PipePipe,
1802 OO_Equal, OO_StarEqual,
1803 OO_SlashEqual, OO_PercentEqual,
1804 OO_PlusEqual, OO_MinusEqual,
1805 OO_LessLessEqual, OO_GreaterGreaterEqual,
1806 OO_AmpEqual, OO_CaretEqual,
1807 OO_PipeEqual,
1808 OO_Comma
1809 };
1810 return OverOps[Opc];
1811}
1812
Craig Topper37932912013-08-18 10:09:15 +00001813InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001814 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001815 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001816 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001817 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001818 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001819{
1820 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001821 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001822 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001823 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001824 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001825 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001826 if (initExprs[I]->isInstantiationDependent())
1827 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001828 if (initExprs[I]->containsUnexpandedParameterPack())
1829 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001830 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001831
Benjamin Kramerc215e762012-08-24 11:54:20 +00001832 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001833}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001834
Craig Topper37932912013-08-18 10:09:15 +00001835void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001836 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001837 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001838}
1839
Craig Topper37932912013-08-18 10:09:15 +00001840void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001841 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001842}
1843
Craig Topper37932912013-08-18 10:09:15 +00001844Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001845 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001846 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Richard Smithc275da62013-12-06 01:27:24 +00001847 setInit(Init, expr);
Ted Kremenek013041e2010-02-19 01:50:18 +00001848 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001851 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001852 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001853 return Result;
1854}
1855
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001856void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001857 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001858 ArrayFillerOrUnionFieldInit = filler;
1859 // Fill out any "holes" in the array due to designated initializers.
1860 Expr **inits = getInits();
1861 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1862 if (inits[i] == 0)
1863 inits[i] = filler;
1864}
1865
Richard Smith9ec1e482012-04-15 02:50:59 +00001866bool InitListExpr::isStringLiteralInit() const {
1867 if (getNumInits() != 1)
1868 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001869 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1870 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001871 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001872 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001873 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1874}
1875
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001876SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001877 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001878 return SyntacticForm->getLocStart();
1879 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001880 if (Beg.isInvalid()) {
1881 // Find the first non-null initializer.
1882 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1883 E = InitExprs.end();
1884 I != E; ++I) {
1885 if (Stmt *S = *I) {
1886 Beg = S->getLocStart();
1887 break;
1888 }
1889 }
1890 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001891 return Beg;
1892}
1893
1894SourceLocation InitListExpr::getLocEnd() const {
1895 if (InitListExpr *SyntacticForm = getSyntacticForm())
1896 return SyntacticForm->getLocEnd();
1897 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001898 if (End.isInvalid()) {
1899 // Find the first non-null initializer from the end.
1900 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001901 E = InitExprs.rend();
1902 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001903 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001904 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001905 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001906 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001907 }
1908 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001909 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001910}
1911
Steve Naroff991e99d2008-09-04 15:31:07 +00001912/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001913///
John McCallc833dea2012-02-17 03:32:35 +00001914const FunctionProtoType *BlockExpr::getFunctionType() const {
1915 // The block pointer is never sugared, but the function type might be.
1916 return cast<BlockPointerType>(getType())
1917 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001918}
1919
Mike Stump11289f42009-09-09 15:08:12 +00001920SourceLocation BlockExpr::getCaretLocation() const {
1921 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001922}
Mike Stump11289f42009-09-09 15:08:12 +00001923const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001924 return TheBlock->getBody();
1925}
Mike Stump11289f42009-09-09 15:08:12 +00001926Stmt *BlockExpr::getBody() {
1927 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001928}
Steve Naroff415d3d52008-10-08 17:01:13 +00001929
1930
Chris Lattner1ec5f562007-06-27 05:38:08 +00001931//===----------------------------------------------------------------------===//
1932// Generic Expression Routines
1933//===----------------------------------------------------------------------===//
1934
Chris Lattner237f2752009-02-14 07:37:35 +00001935/// isUnusedResultAWarning - Return true if this immediate expression should
1936/// be warned about if the result is unused. If so, fill in Loc and Ranges
1937/// with location to warn on and the source range[s] to report with the
1938/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001939bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1940 SourceRange &R1, SourceRange &R2,
1941 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001942 // Don't warn if the expr is type dependent. The type could end up
1943 // instantiating to void.
1944 if (isTypeDependent())
1945 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001946
Chris Lattner1ec5f562007-06-27 05:38:08 +00001947 switch (getStmtClass()) {
1948 default:
John McCallc493a732010-03-12 07:11:26 +00001949 if (getType()->isVoidType())
1950 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001951 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001952 Loc = getExprLoc();
1953 R1 = getSourceRange();
1954 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001955 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001956 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001957 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001958 case GenericSelectionExprClass:
1959 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001960 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00001961 case ChooseExprClass:
1962 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1963 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001964 case UnaryOperatorClass: {
1965 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattner1ec5f562007-06-27 05:38:08 +00001967 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001968 case UO_Plus:
1969 case UO_Minus:
1970 case UO_AddrOf:
1971 case UO_Not:
1972 case UO_LNot:
1973 case UO_Deref:
1974 break;
John McCalle3027922010-08-25 11:45:40 +00001975 case UO_PostInc:
1976 case UO_PostDec:
1977 case UO_PreInc:
1978 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001979 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001980 case UO_Real:
1981 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001982 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001983 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1984 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001985 return false;
1986 break;
John McCalle3027922010-08-25 11:45:40 +00001987 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001988 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001989 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00001990 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001991 Loc = UO->getOperatorLoc();
1992 R1 = UO->getSubExpr()->getSourceRange();
1993 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001994 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001995 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001996 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001997 switch (BO->getOpcode()) {
1998 default:
1999 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002000 // Consider the RHS of comma for side effects. LHS was checked by
2001 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002002 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002003 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2004 // lvalue-ness) of an assignment written in a macro.
2005 if (IntegerLiteral *IE =
2006 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2007 if (IE->getValue() == 0)
2008 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002009 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002010 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002011 case BO_LAnd:
2012 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002013 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2014 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002015 return false;
2016 break;
John McCall1e3715a2010-02-16 04:10:53 +00002017 }
Chris Lattner237f2752009-02-14 07:37:35 +00002018 if (BO->isAssignmentOp())
2019 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002020 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002021 Loc = BO->getOperatorLoc();
2022 R1 = BO->getLHS()->getSourceRange();
2023 R2 = BO->getRHS()->getSourceRange();
2024 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002025 }
Chris Lattner86928112007-08-25 02:00:02 +00002026 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002027 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002028 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002029 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002030
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002031 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002032 // If only one of the LHS or RHS is a warning, the operator might
2033 // be being used for control flow. Only warn if both the LHS and
2034 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002035 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002036 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002037 return false;
2038 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002039 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002040 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002041 }
2042
Chris Lattnera44d1162007-06-27 05:58:59 +00002043 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002044 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002045 Loc = cast<MemberExpr>(this)->getMemberLoc();
2046 R1 = SourceRange(Loc, Loc);
2047 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2048 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002049
Chris Lattner1ec5f562007-06-27 05:38:08 +00002050 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002051 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002052 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2053 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2054 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2055 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002056
Chandler Carruth46339472011-08-17 09:49:44 +00002057 case CXXOperatorCallExprClass: {
2058 // We warn about operator== and operator!= even when user-defined operator
2059 // overloads as there is no reasonable way to define these such that they
2060 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2061 // warning: these operators are commonly typo'ed, and so warning on them
2062 // provides additional value as well. If this list is updated,
2063 // DiagnoseUnusedComparison should be as well.
2064 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2065 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002066 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002067 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002068 Loc = Op->getOperatorLoc();
2069 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002070 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002071 }
Chandler Carruth46339472011-08-17 09:49:44 +00002072
2073 // Fallthrough for generic call handling.
2074 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002075 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002076 case CXXMemberCallExprClass:
2077 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002078 // If this is a direct call, get the callee.
2079 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002080 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002081 // If the callee has attribute pure, const, or warn_unused_result, warn
2082 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002083 //
2084 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2085 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002086 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2087 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002088 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002089 Loc = CE->getCallee()->getLocStart();
2090 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002091
Chris Lattner1a6babf2009-10-13 04:53:48 +00002092 if (unsigned NumArgs = CE->getNumArgs())
2093 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2094 CE->getArg(NumArgs-1)->getLocEnd());
2095 return true;
2096 }
Chris Lattner237f2752009-02-14 07:37:35 +00002097 }
2098 return false;
2099 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002100
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002101 // If we don't know precisely what we're looking at, let's not warn.
2102 case UnresolvedLookupExprClass:
2103 case CXXUnresolvedConstructExprClass:
2104 return false;
2105
Anders Carlsson6aa50392009-11-17 17:11:23 +00002106 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002107 case CXXConstructExprClass: {
2108 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2109 if (Type->hasAttr<WarnUnusedAttr>()) {
2110 WarnE = this;
2111 Loc = getLocStart();
2112 R1 = getSourceRange();
2113 return true;
2114 }
2115 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002116 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002117 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002118
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002119 case ObjCMessageExprClass: {
2120 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002121 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002122 ME->isInstanceMessage() &&
2123 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002124 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002125 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002126 Loc = getExprLoc();
2127 R1 = ME->getSourceRange();
2128 return true;
2129 }
2130
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002131 const ObjCMethodDecl *MD = ME->getMethodDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +00002132 if (MD && MD->hasAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002133 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002134 Loc = getExprLoc();
2135 return true;
2136 }
Chris Lattner237f2752009-02-14 07:37:35 +00002137 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002138 }
Mike Stump11289f42009-09-09 15:08:12 +00002139
John McCallb7bd14f2010-12-02 01:19:52 +00002140 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002141 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002142 Loc = getExprLoc();
2143 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002144 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002145
John McCallfe96e0b2011-11-06 09:01:30 +00002146 case PseudoObjectExprClass: {
2147 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2148
2149 // Only complain about things that have the form of a getter.
2150 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2151 isa<BinaryOperator>(PO->getSyntacticForm()))
2152 return false;
2153
Eli Friedmanc11535c2012-05-24 00:47:05 +00002154 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002155 Loc = getExprLoc();
2156 R1 = getSourceRange();
2157 return true;
2158 }
2159
Chris Lattner944d3062008-07-26 19:51:01 +00002160 case StmtExprClass: {
2161 // Statement exprs don't logically have side effects themselves, but are
2162 // sometimes used in macros in ways that give them a type that is unused.
2163 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2164 // however, if the result of the stmt expr is dead, we don't want to emit a
2165 // warning.
2166 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002167 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002168 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002169 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002170 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2171 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002172 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002173 }
Mike Stump11289f42009-09-09 15:08:12 +00002174
John McCallc493a732010-03-12 07:11:26 +00002175 if (getType()->isVoidType())
2176 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002177 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002178 Loc = cast<StmtExpr>(this)->getLParenLoc();
2179 R1 = getSourceRange();
2180 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002181 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002182 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002183 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002184 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002185 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002186 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002187 if (CE->getCastKind() == CK_ToVoid) {
2188 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002189 CE->getSubExpr()->getType().isVolatileQualified()) {
2190 const DeclRefExpr *DRE =
2191 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2192 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2193 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2194 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2195 R1, R2, Ctx);
2196 }
2197 }
Chris Lattner2706a552009-07-28 18:25:28 +00002198 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002199 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002200
Eli Friedmanc11535c2012-05-24 00:47:05 +00002201 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002202 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002203 if (CE->getCastKind() == CK_ConstructorConversion)
2204 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002205
Eli Friedmanc11535c2012-05-24 00:47:05 +00002206 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002207 if (const CXXFunctionalCastExpr *CXXCE =
2208 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002209 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002210 R1 = CXXCE->getSubExpr()->getSourceRange();
2211 } else {
2212 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2213 Loc = CStyleCE->getLParenLoc();
2214 R1 = CStyleCE->getSubExpr()->getSourceRange();
2215 }
Chris Lattner237f2752009-02-14 07:37:35 +00002216 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002217 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002218 case ImplicitCastExprClass: {
2219 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002220
Eli Friedmanc11535c2012-05-24 00:47:05 +00002221 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2222 if (ICE->getCastKind() == CK_LValueToRValue &&
2223 ICE->getSubExpr()->getType().isVolatileQualified())
2224 return false;
2225
2226 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2227 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002228 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002229 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002230 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002231 case CXXDefaultInitExprClass:
2232 return (cast<CXXDefaultInitExpr>(this)
2233 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002234
2235 case CXXNewExprClass:
2236 // FIXME: In theory, there might be new expressions that don't have side
2237 // effects (e.g. a placement new with an uninitialized POD).
2238 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002239 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002240 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002241 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002242 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002243 case ExprWithCleanupsClass:
2244 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002245 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002246 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002247}
2248
Fariborz Jahanian07735332009-02-22 18:40:18 +00002249/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002250/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002251bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002252 const Expr *E = IgnoreParens();
2253 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002254 default:
2255 return false;
2256 case ObjCIvarRefExprClass:
2257 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002258 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002259 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002260 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002261 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002262 case MaterializeTemporaryExprClass:
2263 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2264 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002265 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002266 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002267 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002268 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002269
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002270 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2271 if (VD->hasGlobalStorage())
2272 return true;
2273 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002274 // dereferencing to a pointer is always a gc'able candidate,
2275 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002276 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002277 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002278 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002279 return false;
2280 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002281 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002282 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002283 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002284 }
2285 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002286 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002287 }
2288}
Sebastian Redlce354af2010-09-10 20:55:33 +00002289
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002290bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2291 if (isTypeDependent())
2292 return false;
John McCall086a4642010-11-24 05:12:34 +00002293 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002294}
2295
John McCall0009fcc2011-04-26 20:42:42 +00002296QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002297 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002298
2299 // Bound member expressions are always one of these possibilities:
2300 // x->m x.m x->*y x.*y
2301 // (possibly parenthesized)
2302
2303 expr = expr->IgnoreParens();
2304 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2305 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2306 return mem->getMemberDecl()->getType();
2307 }
2308
2309 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2310 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2311 ->getPointeeType();
2312 assert(type->isFunctionType());
2313 return type;
2314 }
2315
2316 assert(isa<UnresolvedMemberExpr>(expr));
2317 return QualType();
2318}
2319
Ted Kremenekfff70962008-01-17 16:57:34 +00002320Expr* Expr::IgnoreParens() {
2321 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002322 while (true) {
2323 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2324 E = P->getSubExpr();
2325 continue;
2326 }
2327 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2328 if (P->getOpcode() == UO_Extension) {
2329 E = P->getSubExpr();
2330 continue;
2331 }
2332 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002333 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2334 if (!P->isResultDependent()) {
2335 E = P->getResultExpr();
2336 continue;
2337 }
2338 }
Eli Friedman75807f22013-07-20 00:40:58 +00002339 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2340 if (!P->isConditionDependent()) {
2341 E = P->getChosenSubExpr();
2342 continue;
2343 }
2344 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002345 return E;
2346 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002347}
2348
Chris Lattnerf2660962008-02-13 01:02:39 +00002349/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2350/// or CastExprs or ImplicitCastExprs, returning their operand.
2351Expr *Expr::IgnoreParenCasts() {
2352 Expr *E = this;
2353 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002354 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002355 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002356 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002357 continue;
2358 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002359 if (MaterializeTemporaryExpr *Materialize
2360 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2361 E = Materialize->GetTemporaryExpr();
2362 continue;
2363 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002364 if (SubstNonTypeTemplateParmExpr *NTTP
2365 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2366 E = NTTP->getReplacement();
2367 continue;
2368 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002369 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002370 }
2371}
2372
John McCall5a4ce8b2010-12-04 08:24:19 +00002373/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2374/// casts. This is intended purely as a temporary workaround for code
2375/// that hasn't yet been rewritten to do the right thing about those
2376/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002377Expr *Expr::IgnoreParenLValueCasts() {
2378 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002379 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002380 E = E->IgnoreParens();
2381 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002382 if (P->getCastKind() == CK_LValueToRValue) {
2383 E = P->getSubExpr();
2384 continue;
2385 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002386 } else if (MaterializeTemporaryExpr *Materialize
2387 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2388 E = Materialize->GetTemporaryExpr();
2389 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002390 } else if (SubstNonTypeTemplateParmExpr *NTTP
2391 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2392 E = NTTP->getReplacement();
2393 continue;
John McCall34376a62010-12-04 03:47:34 +00002394 }
2395 break;
2396 }
2397 return E;
2398}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002399
2400Expr *Expr::ignoreParenBaseCasts() {
2401 Expr *E = this;
2402 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002403 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002404 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2405 if (CE->getCastKind() == CK_DerivedToBase ||
2406 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2407 CE->getCastKind() == CK_NoOp) {
2408 E = CE->getSubExpr();
2409 continue;
2410 }
2411 }
2412
2413 return E;
2414 }
2415}
2416
John McCalleebc8322010-05-05 22:59:52 +00002417Expr *Expr::IgnoreParenImpCasts() {
2418 Expr *E = this;
2419 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002420 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002421 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002422 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002423 continue;
2424 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002425 if (MaterializeTemporaryExpr *Materialize
2426 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2427 E = Materialize->GetTemporaryExpr();
2428 continue;
2429 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002430 if (SubstNonTypeTemplateParmExpr *NTTP
2431 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2432 E = NTTP->getReplacement();
2433 continue;
2434 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002435 return E;
John McCalleebc8322010-05-05 22:59:52 +00002436 }
2437}
2438
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002439Expr *Expr::IgnoreConversionOperator() {
2440 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002441 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002442 return MCE->getImplicitObjectArgument();
2443 }
2444 return this;
2445}
2446
Chris Lattneref26c772009-03-13 17:28:01 +00002447/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2448/// value (including ptr->int casts of the same size). Strip off any
2449/// ParenExpr or CastExprs, returning their operand.
2450Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2451 Expr *E = this;
2452 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002453 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002454
Chris Lattneref26c772009-03-13 17:28:01 +00002455 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2456 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002457 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002458 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002459
Chris Lattneref26c772009-03-13 17:28:01 +00002460 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2461 E = SE;
2462 continue;
2463 }
Mike Stump11289f42009-09-09 15:08:12 +00002464
Abramo Bagnara932e3932010-10-15 07:51:18 +00002465 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002466 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002467 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002468 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002469 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2470 E = SE;
2471 continue;
2472 }
2473 }
Mike Stump11289f42009-09-09 15:08:12 +00002474
Douglas Gregor6a40b082011-09-08 17:56:33 +00002475 if (SubstNonTypeTemplateParmExpr *NTTP
2476 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2477 E = NTTP->getReplacement();
2478 continue;
2479 }
2480
Chris Lattneref26c772009-03-13 17:28:01 +00002481 return E;
2482 }
2483}
2484
Douglas Gregord196a582009-12-14 19:27:10 +00002485bool Expr::isDefaultArgument() const {
2486 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002487 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2488 E = M->GetTemporaryExpr();
2489
Douglas Gregord196a582009-12-14 19:27:10 +00002490 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2491 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002492
Douglas Gregord196a582009-12-14 19:27:10 +00002493 return isa<CXXDefaultArgExpr>(E);
2494}
Chris Lattneref26c772009-03-13 17:28:01 +00002495
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002496/// \brief Skip over any no-op casts and any temporary-binding
2497/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002498static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002499 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2500 E = M->GetTemporaryExpr();
2501
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002502 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002503 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002504 E = ICE->getSubExpr();
2505 else
2506 break;
2507 }
2508
2509 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2510 E = BE->getSubExpr();
2511
2512 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002513 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002514 E = ICE->getSubExpr();
2515 else
2516 break;
2517 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002518
2519 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002520}
2521
John McCall7a626f62010-09-15 10:14:12 +00002522/// isTemporaryObject - Determines if this expression produces a
2523/// temporary of the given class type.
2524bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2525 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2526 return false;
2527
Anders Carlsson66bbf502010-11-28 16:40:49 +00002528 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002529
John McCall02dc8c72010-09-15 20:59:13 +00002530 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002531 if (!E->Classify(C).isPRValue()) {
2532 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002533 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002534 return false;
2535 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002536
John McCallf4ee1dd2010-09-16 06:57:56 +00002537 // Black-list a few cases which yield pr-values of class type that don't
2538 // refer to temporaries of that type:
2539
2540 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002541 if (isa<ImplicitCastExpr>(E)) {
2542 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2543 case CK_DerivedToBase:
2544 case CK_UncheckedDerivedToBase:
2545 return false;
2546 default:
2547 break;
2548 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002549 }
2550
John McCallf4ee1dd2010-09-16 06:57:56 +00002551 // - member expressions (all)
2552 if (isa<MemberExpr>(E))
2553 return false;
2554
Eli Friedman13ffdd82012-06-15 23:51:06 +00002555 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2556 if (BO->isPtrMemOp())
2557 return false;
2558
John McCallc07a0c72011-02-17 10:25:35 +00002559 // - opaque values (all)
2560 if (isa<OpaqueValueExpr>(E))
2561 return false;
2562
John McCall7a626f62010-09-15 10:14:12 +00002563 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002564}
2565
Douglas Gregor25b7e052011-03-02 21:06:53 +00002566bool Expr::isImplicitCXXThis() const {
2567 const Expr *E = this;
2568
2569 // Strip away parentheses and casts we don't care about.
2570 while (true) {
2571 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2572 E = Paren->getSubExpr();
2573 continue;
2574 }
2575
2576 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2577 if (ICE->getCastKind() == CK_NoOp ||
2578 ICE->getCastKind() == CK_LValueToRValue ||
2579 ICE->getCastKind() == CK_DerivedToBase ||
2580 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2581 E = ICE->getSubExpr();
2582 continue;
2583 }
2584 }
2585
2586 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2587 if (UnOp->getOpcode() == UO_Extension) {
2588 E = UnOp->getSubExpr();
2589 continue;
2590 }
2591 }
2592
Douglas Gregorfe314812011-06-21 17:03:29 +00002593 if (const MaterializeTemporaryExpr *M
2594 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2595 E = M->GetTemporaryExpr();
2596 continue;
2597 }
2598
Douglas Gregor25b7e052011-03-02 21:06:53 +00002599 break;
2600 }
2601
2602 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2603 return This->isImplicit();
2604
2605 return false;
2606}
2607
Douglas Gregor4619e432008-12-05 23:32:09 +00002608/// hasAnyTypeDependentArguments - Determines if any of the expressions
2609/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002610bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002611 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002612 if (Exprs[I]->isTypeDependent())
2613 return true;
2614
2615 return false;
2616}
2617
John McCall8b0f4ff2010-08-02 21:13:48 +00002618bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002619 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002620 // which can be evaluated at compile-time. It very closely parallels
2621 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2622 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2623 // to isEvaluatable most of the time.
2624 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002625 // If we ever capture reference-binding directly in the AST, we can
2626 // kill the second parameter.
2627
2628 if (IsForRef) {
2629 EvalResult Result;
2630 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2631 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002632
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002633 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002634 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002635 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002636 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002637 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002638 case CXXTemporaryObjectExprClass:
2639 case CXXConstructExprClass: {
2640 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002641
Eli Friedman4c27ac22013-07-16 22:40:53 +00002642 if (CE->getConstructor()->isTrivial() &&
2643 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2644 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002645 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002646
Eli Friedman4c27ac22013-07-16 22:40:53 +00002647 // Trivial copy constructor
2648 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2649 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002650 }
2651
Richard Smithd62306a2011-11-10 06:34:14 +00002652 break;
John McCall81c9cea2010-08-01 21:51:45 +00002653 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002654 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002655 // This handles gcc's extension that allows global initializers like
2656 // "struct x {int x;} x = (struct x) {};".
2657 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002658 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002659 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002660 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002661 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002662 const InitListExpr *ILE = cast<InitListExpr>(this);
2663 if (ILE->getType()->isArrayType()) {
2664 unsigned numInits = ILE->getNumInits();
2665 for (unsigned i = 0; i < numInits; i++) {
2666 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2667 return false;
2668 }
2669 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002670 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002671
2672 if (ILE->getType()->isRecordType()) {
2673 unsigned ElementNo = 0;
2674 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2675 for (RecordDecl::field_iterator Field = RD->field_begin(),
2676 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2677 // If this is a union, skip all the fields that aren't being initialized.
2678 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2679 continue;
2680
2681 // Don't emit anonymous bitfields, they just affect layout.
2682 if (Field->isUnnamedBitfield())
2683 continue;
2684
2685 if (ElementNo < ILE->getNumInits()) {
2686 const Expr *Elt = ILE->getInit(ElementNo++);
2687 if (Field->isBitField()) {
2688 // Bitfields have to evaluate to an integer.
2689 llvm::APSInt ResultTmp;
2690 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2691 return false;
2692 } else {
2693 bool RefType = Field->getType()->isReferenceType();
2694 if (!Elt->isConstantInitializer(Ctx, RefType))
2695 return false;
2696 }
2697 }
2698 }
2699 return true;
2700 }
2701
2702 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002703 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002704 case ImplicitValueInitExprClass:
2705 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002706 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002707 return cast<ParenExpr>(this)->getSubExpr()
2708 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002709 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002710 return cast<GenericSelectionExpr>(this)->getResultExpr()
2711 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002712 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002713 if (cast<ChooseExpr>(this)->isConditionDependent())
2714 return false;
2715 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002716 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002717 case UnaryOperatorClass: {
2718 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002719 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002720 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002721 break;
2722 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002723 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002724 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002725 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002726 case CStyleCastExprClass:
2727 case ObjCBridgedCastExprClass:
2728 case CXXDynamicCastExprClass:
2729 case CXXReinterpretCastExprClass:
2730 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002731 const CastExpr *CE = cast<CastExpr>(this);
2732
Eli Friedman13ec75b2011-12-21 00:43:02 +00002733 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002734 if (CE->getCastKind() == CK_NoOp ||
2735 CE->getCastKind() == CK_LValueToRValue ||
2736 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002737 CE->getCastKind() == CK_ConstructorConversion ||
2738 CE->getCastKind() == CK_NonAtomicToAtomic ||
2739 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002740 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2741
Eli Friedman384da272009-01-25 03:12:18 +00002742 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002743 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002744 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002745 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002746 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002747
2748 case SubstNonTypeTemplateParmExprClass:
2749 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2750 ->isConstantInitializer(Ctx, false);
2751 case CXXDefaultArgExprClass:
2752 return cast<CXXDefaultArgExpr>(this)->getExpr()
2753 ->isConstantInitializer(Ctx, false);
2754 case CXXDefaultInitExprClass:
2755 return cast<CXXDefaultInitExpr>(this)->getExpr()
2756 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002757 }
Eli Friedman384da272009-01-25 03:12:18 +00002758 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002759}
2760
Richard Smith0421ce72012-08-07 04:16:51 +00002761bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2762 if (isInstantiationDependent())
2763 return true;
2764
2765 switch (getStmtClass()) {
2766 case NoStmtClass:
2767 #define ABSTRACT_STMT(Type)
2768 #define STMT(Type, Base) case Type##Class:
2769 #define EXPR(Type, Base)
2770 #include "clang/AST/StmtNodes.inc"
2771 llvm_unreachable("unexpected Expr kind");
2772
2773 case DependentScopeDeclRefExprClass:
2774 case CXXUnresolvedConstructExprClass:
2775 case CXXDependentScopeMemberExprClass:
2776 case UnresolvedLookupExprClass:
2777 case UnresolvedMemberExprClass:
2778 case PackExpansionExprClass:
2779 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002780 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002781 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2782
Richard Smitha33e4fe2012-08-07 05:18:29 +00002783 case DeclRefExprClass:
2784 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002785 case PredefinedExprClass:
2786 case IntegerLiteralClass:
2787 case FloatingLiteralClass:
2788 case ImaginaryLiteralClass:
2789 case StringLiteralClass:
2790 case CharacterLiteralClass:
2791 case OffsetOfExprClass:
2792 case ImplicitValueInitExprClass:
2793 case UnaryExprOrTypeTraitExprClass:
2794 case AddrLabelExprClass:
2795 case GNUNullExprClass:
2796 case CXXBoolLiteralExprClass:
2797 case CXXNullPtrLiteralExprClass:
2798 case CXXThisExprClass:
2799 case CXXScalarValueInitExprClass:
2800 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002801 case ArrayTypeTraitExprClass:
2802 case ExpressionTraitExprClass:
2803 case CXXNoexceptExprClass:
2804 case SizeOfPackExprClass:
2805 case ObjCStringLiteralClass:
2806 case ObjCEncodeExprClass:
2807 case ObjCBoolLiteralExprClass:
2808 case CXXUuidofExprClass:
2809 case OpaqueValueExprClass:
2810 // These never have a side-effect.
2811 return false;
2812
2813 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002814 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002815 case CompoundAssignOperatorClass:
2816 case VAArgExprClass:
2817 case AtomicExprClass:
2818 case StmtExprClass:
2819 case CXXOperatorCallExprClass:
2820 case CXXMemberCallExprClass:
2821 case UserDefinedLiteralClass:
2822 case CXXThrowExprClass:
2823 case CXXNewExprClass:
2824 case CXXDeleteExprClass:
2825 case ExprWithCleanupsClass:
2826 case CXXBindTemporaryExprClass:
2827 case BlockExprClass:
2828 case CUDAKernelCallExprClass:
2829 // These always have a side-effect.
2830 return true;
2831
2832 case ParenExprClass:
2833 case ArraySubscriptExprClass:
2834 case MemberExprClass:
2835 case ConditionalOperatorClass:
2836 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002837 case CompoundLiteralExprClass:
2838 case ExtVectorElementExprClass:
2839 case DesignatedInitExprClass:
2840 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002841 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002842 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002843 case SubstNonTypeTemplateParmExprClass:
2844 case MaterializeTemporaryExprClass:
2845 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002846 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002847 case AsTypeExprClass:
2848 // These have a side-effect if any subexpression does.
2849 break;
2850
Richard Smitha33e4fe2012-08-07 05:18:29 +00002851 case UnaryOperatorClass:
2852 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002853 return true;
2854 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002855
2856 case BinaryOperatorClass:
2857 if (cast<BinaryOperator>(this)->isAssignmentOp())
2858 return true;
2859 break;
2860
Richard Smith0421ce72012-08-07 04:16:51 +00002861 case InitListExprClass:
2862 // FIXME: The children for an InitListExpr doesn't include the array filler.
2863 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2864 if (E->HasSideEffects(Ctx))
2865 return true;
2866 break;
2867
2868 case GenericSelectionExprClass:
2869 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2870 HasSideEffects(Ctx);
2871
2872 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002873 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002874
2875 case CXXDefaultArgExprClass:
2876 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2877
Richard Smith852c9db2013-04-20 22:23:05 +00002878 case CXXDefaultInitExprClass:
2879 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2880 return E->HasSideEffects(Ctx);
2881 // If we've not yet parsed the initializer, assume it has side-effects.
2882 return true;
2883
Richard Smith0421ce72012-08-07 04:16:51 +00002884 case CXXDynamicCastExprClass: {
2885 // A dynamic_cast expression has side-effects if it can throw.
2886 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2887 if (DCE->getTypeAsWritten()->isReferenceType() &&
2888 DCE->getCastKind() == CK_Dynamic)
2889 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002890 } // Fall through.
2891 case ImplicitCastExprClass:
2892 case CStyleCastExprClass:
2893 case CXXStaticCastExprClass:
2894 case CXXReinterpretCastExprClass:
2895 case CXXConstCastExprClass:
2896 case CXXFunctionalCastExprClass: {
2897 const CastExpr *CE = cast<CastExpr>(this);
2898 if (CE->getCastKind() == CK_LValueToRValue &&
2899 CE->getSubExpr()->getType().isVolatileQualified())
2900 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002901 break;
2902 }
2903
Richard Smithef8bf432012-08-13 20:08:14 +00002904 case CXXTypeidExprClass:
2905 // typeid might throw if its subexpression is potentially-evaluated, so has
2906 // side-effects in that case whether or not its subexpression does.
2907 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002908
2909 case CXXConstructExprClass:
2910 case CXXTemporaryObjectExprClass: {
2911 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002912 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002913 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002914 // A trivial constructor does not add any side-effects of its own. Just look
2915 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002916 break;
2917 }
2918
2919 case LambdaExprClass: {
2920 const LambdaExpr *LE = cast<LambdaExpr>(this);
2921 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2922 E = LE->capture_end(); I != E; ++I)
2923 if (I->getCaptureKind() == LCK_ByCopy)
2924 // FIXME: Only has a side-effect if the variable is volatile or if
2925 // the copy would invoke a non-trivial copy constructor.
2926 return true;
2927 return false;
2928 }
2929
2930 case PseudoObjectExprClass: {
2931 // Only look for side-effects in the semantic form, and look past
2932 // OpaqueValueExpr bindings in that form.
2933 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2934 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2935 E = PO->semantics_end();
2936 I != E; ++I) {
2937 const Expr *Subexpr = *I;
2938 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2939 Subexpr = OVE->getSourceExpr();
2940 if (Subexpr->HasSideEffects(Ctx))
2941 return true;
2942 }
2943 return false;
2944 }
2945
2946 case ObjCBoxedExprClass:
2947 case ObjCArrayLiteralClass:
2948 case ObjCDictionaryLiteralClass:
2949 case ObjCMessageExprClass:
2950 case ObjCSelectorExprClass:
2951 case ObjCProtocolExprClass:
2952 case ObjCPropertyRefExprClass:
2953 case ObjCIsaExprClass:
2954 case ObjCIndirectCopyRestoreExprClass:
2955 case ObjCSubscriptRefExprClass:
2956 case ObjCBridgedCastExprClass:
2957 // FIXME: Classify these cases better.
2958 return true;
2959 }
2960
2961 // Recurse to children.
2962 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2963 if (const Stmt *S = *SubStmts)
2964 if (cast<Expr>(S)->HasSideEffects(Ctx))
2965 return true;
2966
2967 return false;
2968}
2969
Douglas Gregor1be329d2012-02-23 07:33:15 +00002970namespace {
2971 /// \brief Look for a call to a non-trivial function within an expression.
2972 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2973 {
2974 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2975
2976 bool NonTrivial;
2977
2978 public:
2979 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002980 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002981
2982 bool hasNonTrivialCall() const { return NonTrivial; }
2983
2984 void VisitCallExpr(CallExpr *E) {
2985 if (CXXMethodDecl *Method
2986 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2987 if (Method->isTrivial()) {
2988 // Recurse to children of the call.
2989 Inherited::VisitStmt(E);
2990 return;
2991 }
2992 }
2993
2994 NonTrivial = true;
2995 }
2996
2997 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2998 if (E->getConstructor()->isTrivial()) {
2999 // Recurse to children of the call.
3000 Inherited::VisitStmt(E);
3001 return;
3002 }
3003
3004 NonTrivial = true;
3005 }
3006
3007 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3008 if (E->getTemporary()->getDestructor()->isTrivial()) {
3009 Inherited::VisitStmt(E);
3010 return;
3011 }
3012
3013 NonTrivial = true;
3014 }
3015 };
3016}
3017
3018bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3019 NonTrivialCallFinder Finder(Ctx);
3020 Finder.Visit(this);
3021 return Finder.hasNonTrivialCall();
3022}
3023
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003024/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3025/// pointer constant or not, as well as the specific kind of constant detected.
3026/// Null pointer constants can be integer constant expressions with the
3027/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3028/// (a GNU extension).
3029Expr::NullPointerConstantKind
3030Expr::isNullPointerConstant(ASTContext &Ctx,
3031 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003032 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003033 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003034 switch (NPC) {
3035 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003036 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003037 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003038 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003039 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003040 else
3041 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003042
Douglas Gregor56751b52009-09-25 04:25:58 +00003043 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003044 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003045 }
3046 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003047
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003048 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003049 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003050 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003051 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003052 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003053 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003054 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003055 Pointee->isVoidType() && // to void*
3056 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003057 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003058 }
Steve Naroffada7d422007-05-20 17:54:12 +00003059 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003060 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3061 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003062 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003063 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3064 // Accept ((void*)0) as a null pointer constant, as many other
3065 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003066 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003067 } else if (const GenericSelectionExpr *GE =
3068 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003069 if (GE->isResultDependent())
3070 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003071 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003072 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3073 if (CE->isConditionDependent())
3074 return NPCK_NotNull;
3075 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003076 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003077 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003078 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003079 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003080 } else if (const CXXDefaultInitExpr *DefaultInit
3081 = dyn_cast<CXXDefaultInitExpr>(this)) {
3082 // See through default initializer expressions.
3083 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003084 } else if (isa<GNUNullExpr>(this)) {
3085 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003086 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003087 } else if (const MaterializeTemporaryExpr *M
3088 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3089 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003090 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3091 if (const Expr *Source = OVE->getSourceExpr())
3092 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003093 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003094
Richard Smith89645bc2013-01-02 12:01:23 +00003095 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003096 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003097 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003098
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003099 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003100 if (!Ctx.getLangOpts().CPlusPlus11 &&
3101 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003102 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3103 const Expr *InitExpr = CLE->getInitializer();
3104 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3105 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3106 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003107 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003108 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003109 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003110 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003111
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003112 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003113 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3114 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003115 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003116 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003117 if (Lit && !Lit->getValue())
3118 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003119 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003120 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003121 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003122 // If we have an integer constant expression, we need to *evaluate* it and
3123 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003124 if (!isIntegerConstantExpr(Ctx))
3125 return NPCK_NotNull;
3126 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003127
David Blaikie1c7c8f72012-08-08 17:33:31 +00003128 if (EvaluateKnownConstInt(Ctx) != 0)
3129 return NPCK_NotNull;
3130
3131 if (isa<IntegerLiteral>(this))
3132 return NPCK_ZeroLiteral;
3133 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003134}
Steve Narofff7a5da12007-07-28 23:10:27 +00003135
John McCall34376a62010-12-04 03:47:34 +00003136/// \brief If this expression is an l-value for an Objective C
3137/// property, find the underlying property reference expression.
3138const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3139 const Expr *E = this;
3140 while (true) {
3141 assert((E->getValueKind() == VK_LValue &&
3142 E->getObjectKind() == OK_ObjCProperty) &&
3143 "expression is not a property reference");
3144 E = E->IgnoreParenCasts();
3145 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3146 if (BO->getOpcode() == BO_Comma) {
3147 E = BO->getRHS();
3148 continue;
3149 }
3150 }
3151
3152 break;
3153 }
3154
3155 return cast<ObjCPropertyRefExpr>(E);
3156}
3157
Anna Zaks97c7ce32012-10-01 20:34:04 +00003158bool Expr::isObjCSelfExpr() const {
3159 const Expr *E = IgnoreParenImpCasts();
3160
3161 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3162 if (!DRE)
3163 return false;
3164
3165 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3166 if (!Param)
3167 return false;
3168
3169 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3170 if (!M)
3171 return false;
3172
3173 return M->getSelfDecl() == Param;
3174}
3175
John McCalld25db7e2013-05-06 21:39:12 +00003176FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003177 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003178
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003179 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003180 if (ICE->getCastKind() == CK_LValueToRValue ||
3181 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003182 E = ICE->getSubExpr()->IgnoreParens();
3183 else
3184 break;
3185 }
3186
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003187 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003188 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003189 if (Field->isBitField())
3190 return Field;
3191
John McCalld25db7e2013-05-06 21:39:12 +00003192 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3193 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3194 if (Ivar->isBitField())
3195 return Ivar;
3196
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003197 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3198 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3199 if (Field->isBitField())
3200 return Field;
3201
Eli Friedman609ada22011-07-13 02:05:57 +00003202 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003203 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003204 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003205
Eli Friedman609ada22011-07-13 02:05:57 +00003206 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003207 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003208 }
3209
Douglas Gregor71235ec2009-05-02 02:18:30 +00003210 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003211}
3212
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003213bool Expr::refersToVectorElement() const {
3214 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003215
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003216 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003217 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003218 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003219 E = ICE->getSubExpr()->IgnoreParens();
3220 else
3221 break;
3222 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003223
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003224 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3225 return ASE->getBase()->getType()->isVectorType();
3226
3227 if (isa<ExtVectorElementExpr>(E))
3228 return true;
3229
3230 return false;
3231}
3232
Chris Lattnerb8211f62009-02-16 22:14:05 +00003233/// isArrow - Return true if the base expression is a pointer to vector,
3234/// return false if the base expression is a vector.
3235bool ExtVectorElementExpr::isArrow() const {
3236 return getBase()->getType()->isPointerType();
3237}
3238
Nate Begemance4d7fc2008-04-18 23:10:10 +00003239unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003240 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003241 return VT->getNumElements();
3242 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003243}
3244
Nate Begemanf322eab2008-05-09 06:41:27 +00003245/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003246bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003247 // FIXME: Refactor this code to an accessor on the AST node which returns the
3248 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003249 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003250
3251 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003252 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003253 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003254
Nate Begeman7e5185b2009-01-18 02:01:21 +00003255 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003256 if (Comp[0] == 's' || Comp[0] == 'S')
3257 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003258
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003259 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003260 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003261 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003262
Steve Naroff0d595ca2007-07-30 03:29:09 +00003263 return false;
3264}
Chris Lattner885b4952007-08-02 23:36:59 +00003265
Nate Begemanf322eab2008-05-09 06:41:27 +00003266/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003267void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003268 SmallVectorImpl<unsigned> &Elts) const {
3269 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003270 if (Comp[0] == 's' || Comp[0] == 'S')
3271 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003272
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003273 bool isHi = Comp == "hi";
3274 bool isLo = Comp == "lo";
3275 bool isEven = Comp == "even";
3276 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003277
Nate Begemanf322eab2008-05-09 06:41:27 +00003278 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3279 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003280
Nate Begemanf322eab2008-05-09 06:41:27 +00003281 if (isHi)
3282 Index = e + i;
3283 else if (isLo)
3284 Index = i;
3285 else if (isEven)
3286 Index = 2 * i;
3287 else if (isOdd)
3288 Index = 2 * i + 1;
3289 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003290 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003291
Nate Begemand3862152008-05-13 21:03:02 +00003292 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003293 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003294}
3295
Douglas Gregor9a129192010-04-21 00:45:42 +00003296ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003297 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003298 SourceLocation LBracLoc,
3299 SourceLocation SuperLoc,
3300 bool IsInstanceSuper,
3301 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003302 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003303 ArrayRef<SourceLocation> SelLocs,
3304 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003305 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003306 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003307 SourceLocation RBracLoc,
3308 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003309 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003310 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003311 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003312 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003313 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3314 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003315 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003316 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3317 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003318{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003319 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003320 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003321}
3322
Douglas Gregor9a129192010-04-21 00:45:42 +00003323ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003324 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003325 SourceLocation LBracLoc,
3326 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003327 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003328 ArrayRef<SourceLocation> SelLocs,
3329 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003330 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003331 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003332 SourceLocation RBracLoc,
3333 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003334 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003335 T->isDependentType(), T->isInstantiationDependentType(),
3336 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003337 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3338 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003339 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003340 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003341 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003342{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003343 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003344 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003345}
3346
Douglas Gregor9a129192010-04-21 00:45:42 +00003347ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003348 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003349 SourceLocation LBracLoc,
3350 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003351 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003352 ArrayRef<SourceLocation> SelLocs,
3353 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003354 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003355 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003356 SourceLocation RBracLoc,
3357 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003358 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003359 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003360 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003361 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003362 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3363 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003364 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003365 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003366 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003367{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003368 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003369 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003370}
3371
3372void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3373 ArrayRef<SourceLocation> SelLocs,
3374 SelectorLocationsKind SelLocsK) {
3375 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003376 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003377 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003378 if (Args[I]->isTypeDependent())
3379 ExprBits.TypeDependent = true;
3380 if (Args[I]->isValueDependent())
3381 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003382 if (Args[I]->isInstantiationDependent())
3383 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003384 if (Args[I]->containsUnexpandedParameterPack())
3385 ExprBits.ContainsUnexpandedParameterPack = true;
3386
3387 MyArgs[I] = Args[I];
3388 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003389
Benjamin Kramer2325b242012-02-20 00:20:48 +00003390 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003391 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003392 if (SelLocsK == SelLoc_NonStandard)
3393 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3394 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003395}
3396
Craig Topperce7167c2013-08-22 04:58:56 +00003397ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003398 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003399 SourceLocation LBracLoc,
3400 SourceLocation SuperLoc,
3401 bool IsInstanceSuper,
3402 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003403 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003404 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003405 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003406 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003407 SourceLocation RBracLoc,
3408 bool isImplicit) {
3409 assert((!SelLocs.empty() || isImplicit) &&
3410 "No selector locs for non-implicit message");
3411 ObjCMessageExpr *Mem;
3412 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3413 if (isImplicit)
3414 Mem = alloc(Context, Args.size(), 0);
3415 else
3416 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003417 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003418 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003419 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003420}
3421
Craig Topperce7167c2013-08-22 04:58:56 +00003422ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003423 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003424 SourceLocation LBracLoc,
3425 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003426 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003427 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003428 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003429 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003430 SourceLocation RBracLoc,
3431 bool isImplicit) {
3432 assert((!SelLocs.empty() || isImplicit) &&
3433 "No selector locs for non-implicit message");
3434 ObjCMessageExpr *Mem;
3435 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3436 if (isImplicit)
3437 Mem = alloc(Context, Args.size(), 0);
3438 else
3439 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003440 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003441 SelLocs, SelLocsK, Method, Args, RBracLoc,
3442 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003443}
3444
Craig Topperce7167c2013-08-22 04:58:56 +00003445ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003446 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003447 SourceLocation LBracLoc,
3448 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003449 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003450 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003451 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003452 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003453 SourceLocation RBracLoc,
3454 bool isImplicit) {
3455 assert((!SelLocs.empty() || isImplicit) &&
3456 "No selector locs for non-implicit message");
3457 ObjCMessageExpr *Mem;
3458 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3459 if (isImplicit)
3460 Mem = alloc(Context, Args.size(), 0);
3461 else
3462 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003463 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003464 SelLocs, SelLocsK, Method, Args, RBracLoc,
3465 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003466}
3467
Craig Topperce7167c2013-08-22 04:58:56 +00003468ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003469 unsigned NumArgs,
3470 unsigned NumStoredSelLocs) {
3471 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003472 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3473}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003474
Craig Topperce7167c2013-08-22 04:58:56 +00003475ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003476 ArrayRef<Expr *> Args,
3477 SourceLocation RBraceLoc,
3478 ArrayRef<SourceLocation> SelLocs,
3479 Selector Sel,
3480 SelectorLocationsKind &SelLocsK) {
3481 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3482 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3483 : 0;
3484 return alloc(C, Args.size(), NumStoredSelLocs);
3485}
3486
Craig Topperce7167c2013-08-22 04:58:56 +00003487ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003488 unsigned NumArgs,
3489 unsigned NumStoredSelLocs) {
3490 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3491 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3492 return (ObjCMessageExpr *)C.Allocate(Size,
3493 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3494}
3495
3496void ObjCMessageExpr::getSelectorLocs(
3497 SmallVectorImpl<SourceLocation> &SelLocs) const {
3498 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3499 SelLocs.push_back(getSelectorLoc(i));
3500}
3501
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003502SourceRange ObjCMessageExpr::getReceiverRange() const {
3503 switch (getReceiverKind()) {
3504 case Instance:
3505 return getInstanceReceiver()->getSourceRange();
3506
3507 case Class:
3508 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3509
3510 case SuperInstance:
3511 case SuperClass:
3512 return getSuperLoc();
3513 }
3514
David Blaikiee4d798f2012-01-20 21:50:17 +00003515 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003516}
3517
Douglas Gregor9a129192010-04-21 00:45:42 +00003518Selector ObjCMessageExpr::getSelector() const {
3519 if (HasMethod)
3520 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3521 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003522 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003523}
3524
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003525QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003526 switch (getReceiverKind()) {
3527 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003528 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003529 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003530 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003531 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003532 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003533 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003534 }
3535
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003536 llvm_unreachable("unexpected receiver kind");
3537}
3538
3539ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3540 QualType T = getReceiverType();
3541
3542 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3543 return Ptr->getInterfaceDecl();
3544
3545 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3546 return Ty->getInterface();
3547
Douglas Gregor9a129192010-04-21 00:45:42 +00003548 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003549}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003550
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003551StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003552 switch (getBridgeKind()) {
3553 case OBC_Bridge:
3554 return "__bridge";
3555 case OBC_BridgeTransfer:
3556 return "__bridge_transfer";
3557 case OBC_BridgeRetained:
3558 return "__bridge_retained";
3559 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003560
3561 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003562}
3563
Craig Topper37932912013-08-18 10:09:15 +00003564ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003565 QualType Type, SourceLocation BLoc,
3566 SourceLocation RP)
3567 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3568 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003569 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003570 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003571 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003572{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003573 SubExprs = new (C) Stmt*[args.size()];
3574 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003575 if (args[i]->isTypeDependent())
3576 ExprBits.TypeDependent = true;
3577 if (args[i]->isValueDependent())
3578 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003579 if (args[i]->isInstantiationDependent())
3580 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003581 if (args[i]->containsUnexpandedParameterPack())
3582 ExprBits.ContainsUnexpandedParameterPack = true;
3583
3584 SubExprs[i] = args[i];
3585 }
3586}
3587
Craig Topper37932912013-08-18 10:09:15 +00003588void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003589 if (SubExprs) C.Deallocate(SubExprs);
3590
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003591 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003592 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003593 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003594}
Nate Begeman48745922009-08-12 02:28:50 +00003595
Craig Topper37932912013-08-18 10:09:15 +00003596GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003597 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003598 ArrayRef<TypeSourceInfo*> AssocTypes,
3599 ArrayRef<Expr*> AssocExprs,
3600 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003601 SourceLocation RParenLoc,
3602 bool ContainsUnexpandedParameterPack,
3603 unsigned ResultIndex)
3604 : Expr(GenericSelectionExprClass,
3605 AssocExprs[ResultIndex]->getType(),
3606 AssocExprs[ResultIndex]->getValueKind(),
3607 AssocExprs[ResultIndex]->getObjectKind(),
3608 AssocExprs[ResultIndex]->isTypeDependent(),
3609 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003610 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003611 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003612 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3613 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3614 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3615 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003616 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003617 assert(AssocTypes.size() == AssocExprs.size());
3618 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3619 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003620}
3621
Craig Topper37932912013-08-18 10:09:15 +00003622GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003623 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003624 ArrayRef<TypeSourceInfo*> AssocTypes,
3625 ArrayRef<Expr*> AssocExprs,
3626 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003627 SourceLocation RParenLoc,
3628 bool ContainsUnexpandedParameterPack)
3629 : Expr(GenericSelectionExprClass,
3630 Context.DependentTy,
3631 VK_RValue,
3632 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003633 /*isTypeDependent=*/true,
3634 /*isValueDependent=*/true,
3635 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003636 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003637 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3638 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3639 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3640 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003641 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003642 assert(AssocTypes.size() == AssocExprs.size());
3643 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3644 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003645}
3646
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003647//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003648// DesignatedInitExpr
3649//===----------------------------------------------------------------------===//
3650
Chandler Carruth631abd92011-06-16 06:47:06 +00003651IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003652 assert(Kind == FieldDesignator && "Only valid on a field designator");
3653 if (Field.NameOrField & 0x01)
3654 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3655 else
3656 return getField()->getIdentifier();
3657}
3658
Craig Topper37932912013-08-18 10:09:15 +00003659DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003660 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003661 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003662 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003663 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003664 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003665 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003666 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003667 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003668 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003669 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003670 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003671 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003672 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003673 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003674
3675 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003676 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003677 *Child++ = Init;
3678
3679 // Copy the designators and their subexpressions, computing
3680 // value-dependence along the way.
3681 unsigned IndexIdx = 0;
3682 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003683 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003684
3685 if (this->Designators[I].isArrayDesignator()) {
3686 // Compute type- and value-dependence.
3687 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003688 if (Index->isTypeDependent() || Index->isValueDependent())
3689 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003690 if (Index->isInstantiationDependent())
3691 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003692 // Propagate unexpanded parameter packs.
3693 if (Index->containsUnexpandedParameterPack())
3694 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003695
3696 // Copy the index expressions into permanent storage.
3697 *Child++ = IndexExprs[IndexIdx++];
3698 } else if (this->Designators[I].isArrayRangeDesignator()) {
3699 // Compute type- and value-dependence.
3700 Expr *Start = IndexExprs[IndexIdx];
3701 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003702 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003703 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003704 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003705 ExprBits.InstantiationDependent = true;
3706 } else if (Start->isInstantiationDependent() ||
3707 End->isInstantiationDependent()) {
3708 ExprBits.InstantiationDependent = true;
3709 }
3710
Douglas Gregora6e053e2010-12-15 01:34:56 +00003711 // Propagate unexpanded parameter packs.
3712 if (Start->containsUnexpandedParameterPack() ||
3713 End->containsUnexpandedParameterPack())
3714 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003715
3716 // Copy the start/end expressions into permanent storage.
3717 *Child++ = IndexExprs[IndexIdx++];
3718 *Child++ = IndexExprs[IndexIdx++];
3719 }
3720 }
3721
Benjamin Kramerc215e762012-08-24 11:54:20 +00003722 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003723}
3724
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003725DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003726DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003727 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003728 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003729 SourceLocation ColonOrEqualLoc,
3730 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003731 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003732 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003733 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003734 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003735 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003736}
3737
Craig Topper37932912013-08-18 10:09:15 +00003738DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003739 unsigned NumIndexExprs) {
3740 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3741 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3742 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3743}
3744
Craig Topper37932912013-08-18 10:09:15 +00003745void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003746 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003747 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003748 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003749 NumDesignators = NumDesigs;
3750 for (unsigned I = 0; I != NumDesigs; ++I)
3751 Designators[I] = Desigs[I];
3752}
3753
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003754SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3755 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3756 if (size() == 1)
3757 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003758 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3759 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003760}
3761
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003762SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003763 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003764 Designator &First =
3765 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003766 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003767 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003768 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3769 else
3770 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3771 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003772 StartLoc =
3773 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003774 return StartLoc;
3775}
3776
3777SourceLocation DesignatedInitExpr::getLocEnd() const {
3778 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003779}
3780
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003781Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003782 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003783 char *Ptr = static_cast<char *>(
3784 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003785 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003786 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3787 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3788}
3789
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003790Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003791 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003792 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003793 char *Ptr = static_cast<char *>(
3794 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003795 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003796 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3797 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3798}
3799
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003800Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003801 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003802 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003803 char *Ptr = static_cast<char *>(
3804 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003805 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003806 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3807 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3808}
3809
Douglas Gregord5846a12009-04-15 06:41:24 +00003810/// \brief Replaces the designator at index @p Idx with the series
3811/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003812void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003813 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003814 const Designator *Last) {
3815 unsigned NumNewDesignators = Last - First;
3816 if (NumNewDesignators == 0) {
3817 std::copy_backward(Designators + Idx + 1,
3818 Designators + NumDesignators,
3819 Designators + Idx);
3820 --NumNewDesignators;
3821 return;
3822 } else if (NumNewDesignators == 1) {
3823 Designators[Idx] = *First;
3824 return;
3825 }
3826
Mike Stump11289f42009-09-09 15:08:12 +00003827 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003828 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003829 std::copy(Designators, Designators + Idx, NewDesignators);
3830 std::copy(First, Last, NewDesignators + Idx);
3831 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3832 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003833 Designators = NewDesignators;
3834 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3835}
3836
Craig Topper37932912013-08-18 10:09:15 +00003837ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003838 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003839 SourceLocation rparenloc)
3840 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003841 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003842 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3843 Exprs = new (C) Stmt*[exprs.size()];
3844 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003845 if (exprs[i]->isTypeDependent())
3846 ExprBits.TypeDependent = true;
3847 if (exprs[i]->isValueDependent())
3848 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003849 if (exprs[i]->isInstantiationDependent())
3850 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003851 if (exprs[i]->containsUnexpandedParameterPack())
3852 ExprBits.ContainsUnexpandedParameterPack = true;
3853
Nate Begeman5ec4b312009-08-10 23:49:36 +00003854 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003855 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003856}
3857
John McCall1bf58462011-02-16 08:02:54 +00003858const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3859 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3860 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003861 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3862 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003863 e = cast<CXXConstructExpr>(e)->getArg(0);
3864 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3865 e = ice->getSubExpr();
3866 return cast<OpaqueValueExpr>(e);
3867}
3868
Craig Topper37932912013-08-18 10:09:15 +00003869PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3870 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003871 unsigned numSemanticExprs) {
3872 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3873 (1 + numSemanticExprs) * sizeof(Expr*),
3874 llvm::alignOf<PseudoObjectExpr>());
3875 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3876}
3877
3878PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3879 : Expr(PseudoObjectExprClass, shell) {
3880 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3881}
3882
Craig Topper37932912013-08-18 10:09:15 +00003883PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003884 ArrayRef<Expr*> semantics,
3885 unsigned resultIndex) {
3886 assert(syntax && "no syntactic expression!");
3887 assert(semantics.size() && "no semantic expressions!");
3888
3889 QualType type;
3890 ExprValueKind VK;
3891 if (resultIndex == NoResult) {
3892 type = C.VoidTy;
3893 VK = VK_RValue;
3894 } else {
3895 assert(resultIndex < semantics.size());
3896 type = semantics[resultIndex]->getType();
3897 VK = semantics[resultIndex]->getValueKind();
3898 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3899 }
3900
3901 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3902 (1 + semantics.size()) * sizeof(Expr*),
3903 llvm::alignOf<PseudoObjectExpr>());
3904 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3905 resultIndex);
3906}
3907
3908PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3909 Expr *syntax, ArrayRef<Expr*> semantics,
3910 unsigned resultIndex)
3911 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3912 /*filled in at end of ctor*/ false, false, false, false) {
3913 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3914 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3915
3916 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3917 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3918 getSubExprsBuffer()[i] = E;
3919
3920 if (E->isTypeDependent())
3921 ExprBits.TypeDependent = true;
3922 if (E->isValueDependent())
3923 ExprBits.ValueDependent = true;
3924 if (E->isInstantiationDependent())
3925 ExprBits.InstantiationDependent = true;
3926 if (E->containsUnexpandedParameterPack())
3927 ExprBits.ContainsUnexpandedParameterPack = true;
3928
3929 if (isa<OpaqueValueExpr>(E))
3930 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3931 "opaque-value semantic expressions for pseudo-object "
3932 "operations must have sources");
3933 }
3934}
3935
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003936//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003937// ExprIterator.
3938//===----------------------------------------------------------------------===//
3939
3940Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3941Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3942Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3943const Expr* ConstExprIterator::operator[](size_t idx) const {
3944 return cast<Expr>(I[idx]);
3945}
3946const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3947const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3948
3949//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003950// Child Iterators for iterating over subexpressions/substatements
3951//===----------------------------------------------------------------------===//
3952
Peter Collingbournee190dee2011-03-11 19:24:49 +00003953// UnaryExprOrTypeTraitExpr
3954Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003955 // If this is of a type and the type is a VLA type (and not a typedef), the
3956 // size expression of the VLA needs to be treated as an executable expression.
3957 // Why isn't this weirdness documented better in StmtIterator?
3958 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003959 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003960 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003961 return child_range(child_iterator(T), child_iterator());
3962 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003963 }
John McCallbd066782011-02-09 08:16:59 +00003964 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003965}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003966
Steve Naroffd54978b2007-09-18 23:55:05 +00003967// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003968Stmt::child_range ObjCMessageExpr::children() {
3969 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003970 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003971 begin = reinterpret_cast<Stmt **>(this + 1);
3972 else
3973 begin = reinterpret_cast<Stmt **>(getArgs());
3974 return child_range(begin,
3975 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003976}
3977
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003978ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003979 QualType T, ObjCMethodDecl *Method,
3980 SourceRange SR)
3981 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3982 false, false, false, false),
3983 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3984{
3985 Expr **SaveElements = getElements();
3986 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3987 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3988 ExprBits.ValueDependent = true;
3989 if (Elements[I]->isInstantiationDependent())
3990 ExprBits.InstantiationDependent = true;
3991 if (Elements[I]->containsUnexpandedParameterPack())
3992 ExprBits.ContainsUnexpandedParameterPack = true;
3993
3994 SaveElements[I] = Elements[I];
3995 }
3996}
3997
Craig Topperce7167c2013-08-22 04:58:56 +00003998ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003999 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004000 QualType T, ObjCMethodDecl * Method,
4001 SourceRange SR) {
4002 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4003 + Elements.size() * sizeof(Expr *));
4004 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4005}
4006
Craig Topperce7167c2013-08-22 04:58:56 +00004007ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004008 unsigned NumElements) {
4009
4010 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4011 + NumElements * sizeof(Expr *));
4012 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4013}
4014
4015ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4016 ArrayRef<ObjCDictionaryElement> VK,
4017 bool HasPackExpansions,
4018 QualType T, ObjCMethodDecl *method,
4019 SourceRange SR)
4020 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4021 false, false),
4022 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4023 DictWithObjectsMethod(method)
4024{
4025 KeyValuePair *KeyValues = getKeyValues();
4026 ExpansionData *Expansions = getExpansionData();
4027 for (unsigned I = 0; I < NumElements; I++) {
4028 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4029 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4030 ExprBits.ValueDependent = true;
4031 if (VK[I].Key->isInstantiationDependent() ||
4032 VK[I].Value->isInstantiationDependent())
4033 ExprBits.InstantiationDependent = true;
4034 if (VK[I].EllipsisLoc.isInvalid() &&
4035 (VK[I].Key->containsUnexpandedParameterPack() ||
4036 VK[I].Value->containsUnexpandedParameterPack()))
4037 ExprBits.ContainsUnexpandedParameterPack = true;
4038
4039 KeyValues[I].Key = VK[I].Key;
4040 KeyValues[I].Value = VK[I].Value;
4041 if (Expansions) {
4042 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4043 if (VK[I].NumExpansions)
4044 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4045 else
4046 Expansions[I].NumExpansionsPlusOne = 0;
4047 }
4048 }
4049}
4050
4051ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004052ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004053 ArrayRef<ObjCDictionaryElement> VK,
4054 bool HasPackExpansions,
4055 QualType T, ObjCMethodDecl *method,
4056 SourceRange SR) {
4057 unsigned ExpansionsSize = 0;
4058 if (HasPackExpansions)
4059 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4060
4061 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4062 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4063 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4064}
4065
4066ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004067ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004068 bool HasPackExpansions) {
4069 unsigned ExpansionsSize = 0;
4070 if (HasPackExpansions)
4071 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4072 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4073 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4074 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4075 HasPackExpansions);
4076}
4077
Craig Topperce7167c2013-08-22 04:58:56 +00004078ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004079 Expr *base,
4080 Expr *key, QualType T,
4081 ObjCMethodDecl *getMethod,
4082 ObjCMethodDecl *setMethod,
4083 SourceLocation RB) {
4084 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4085 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4086 OK_ObjCSubscript,
4087 getMethod, setMethod, RB);
4088}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004089
Benjamin Kramerc215e762012-08-24 11:54:20 +00004090AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004091 QualType t, AtomicOp op, SourceLocation RP)
4092 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4093 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004094 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004095{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004096 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4097 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004098 if (args[i]->isTypeDependent())
4099 ExprBits.TypeDependent = true;
4100 if (args[i]->isValueDependent())
4101 ExprBits.ValueDependent = true;
4102 if (args[i]->isInstantiationDependent())
4103 ExprBits.InstantiationDependent = true;
4104 if (args[i]->containsUnexpandedParameterPack())
4105 ExprBits.ContainsUnexpandedParameterPack = true;
4106
4107 SubExprs[i] = args[i];
4108 }
4109}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004110
4111unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4112 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004113 case AO__c11_atomic_init:
4114 case AO__c11_atomic_load:
4115 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004116 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004117
4118 case AO__c11_atomic_store:
4119 case AO__c11_atomic_exchange:
4120 case AO__atomic_load:
4121 case AO__atomic_store:
4122 case AO__atomic_store_n:
4123 case AO__atomic_exchange_n:
4124 case AO__c11_atomic_fetch_add:
4125 case AO__c11_atomic_fetch_sub:
4126 case AO__c11_atomic_fetch_and:
4127 case AO__c11_atomic_fetch_or:
4128 case AO__c11_atomic_fetch_xor:
4129 case AO__atomic_fetch_add:
4130 case AO__atomic_fetch_sub:
4131 case AO__atomic_fetch_and:
4132 case AO__atomic_fetch_or:
4133 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004134 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004135 case AO__atomic_add_fetch:
4136 case AO__atomic_sub_fetch:
4137 case AO__atomic_and_fetch:
4138 case AO__atomic_or_fetch:
4139 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004140 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004141 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004142
4143 case AO__atomic_exchange:
4144 return 4;
4145
4146 case AO__c11_atomic_compare_exchange_strong:
4147 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004148 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004149
4150 case AO__atomic_compare_exchange:
4151 case AO__atomic_compare_exchange_n:
4152 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004153 }
4154 llvm_unreachable("unknown atomic op");
4155}