blob: 2f5f14f38e3dcf6e5df0b7131d8d077b1282567d [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)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000456 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000457 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()) ||
Alp Toker314cc812014-01-25 16:55:45 +0000591 (FT && FT->getReturnType()->getAs<AutoType>()))
Benjamin Kramer90f54222013-08-21 11:45:27 +0000592 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000593 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
594 FT->getReturnType()
595 ->getAs<DecltypeType>()
596 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000597 .getAsStringInternal(Proto, Policy);
598 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000599 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000600
601 Out << Proto;
602
603 Out.flush();
604 return Name.str().str();
605 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000606 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
607 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
608 // Skip to its enclosing function or method, but not its enclosing
609 // CapturedDecl.
610 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
611 const Decl *D = Decl::castFromDeclContext(DC);
612 return ComputeName(IT, D);
613 }
614 llvm_unreachable("CapturedDecl not inside a function or method");
615 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000616 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000617 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000618 llvm::raw_svector_ostream Out(Name);
619 Out << (MD->isInstanceMethod() ? '-' : '+');
620 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000621
622 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
623 // a null check to avoid a crash.
624 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000625 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000626
Anders Carlsson2fb08242009-09-08 18:24:21 +0000627 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000628 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000629 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000630
Anders Carlsson2fb08242009-09-08 18:24:21 +0000631 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000632 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000633 Out << ']';
634
635 Out.flush();
636 return Name.str().str();
637 }
638 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
639 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
640 return "top level";
641 }
642 return "";
643}
644
Craig Topper37932912013-08-18 10:09:15 +0000645void APNumericStorage::setIntValue(const ASTContext &C,
646 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000647 if (hasAllocation())
648 C.Deallocate(pVal);
649
650 BitWidth = Val.getBitWidth();
651 unsigned NumWords = Val.getNumWords();
652 const uint64_t* Words = Val.getRawData();
653 if (NumWords > 1) {
654 pVal = new (C) uint64_t[NumWords];
655 std::copy(Words, Words + NumWords, pVal);
656 } else if (NumWords == 1)
657 VAL = Words[0];
658 else
659 VAL = 0;
660}
661
Craig Topper37932912013-08-18 10:09:15 +0000662IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000663 QualType type, SourceLocation l)
664 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
665 false, false),
666 Loc(l) {
667 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
668 assert(V.getBitWidth() == C.getIntWidth(type) &&
669 "Integer type is not the correct size for constant.");
670 setValue(C, V);
671}
672
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000673IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000674IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000675 QualType type, SourceLocation l) {
676 return new (C) IntegerLiteral(C, V, type, l);
677}
678
679IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000680IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000681 return new (C) IntegerLiteral(Empty);
682}
683
Craig Topper37932912013-08-18 10:09:15 +0000684FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000685 bool isexact, QualType Type, SourceLocation L)
686 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
687 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000688 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000689 FloatingLiteralBits.IsExact = isexact;
690 setValue(C, V);
691}
692
Craig Topper37932912013-08-18 10:09:15 +0000693FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000694 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000695 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000696 FloatingLiteralBits.IsExact = false;
697}
698
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000699FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000700FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000701 bool isexact, QualType Type, SourceLocation L) {
702 return new (C) FloatingLiteral(C, V, isexact, Type, L);
703}
704
705FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000706FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000707 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000708}
709
Tim Northover178723a2013-01-22 09:46:51 +0000710const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
711 switch(FloatingLiteralBits.Semantics) {
712 case IEEEhalf:
713 return llvm::APFloat::IEEEhalf;
714 case IEEEsingle:
715 return llvm::APFloat::IEEEsingle;
716 case IEEEdouble:
717 return llvm::APFloat::IEEEdouble;
718 case x87DoubleExtended:
719 return llvm::APFloat::x87DoubleExtended;
720 case IEEEquad:
721 return llvm::APFloat::IEEEquad;
722 case PPCDoubleDouble:
723 return llvm::APFloat::PPCDoubleDouble;
724 }
725 llvm_unreachable("Unrecognised floating semantics");
726}
727
728void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
729 if (&Sem == &llvm::APFloat::IEEEhalf)
730 FloatingLiteralBits.Semantics = IEEEhalf;
731 else if (&Sem == &llvm::APFloat::IEEEsingle)
732 FloatingLiteralBits.Semantics = IEEEsingle;
733 else if (&Sem == &llvm::APFloat::IEEEdouble)
734 FloatingLiteralBits.Semantics = IEEEdouble;
735 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
736 FloatingLiteralBits.Semantics = x87DoubleExtended;
737 else if (&Sem == &llvm::APFloat::IEEEquad)
738 FloatingLiteralBits.Semantics = IEEEquad;
739 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
740 FloatingLiteralBits.Semantics = PPCDoubleDouble;
741 else
742 llvm_unreachable("Unknown floating semantics");
743}
744
Chris Lattnera0173132008-06-07 22:13:43 +0000745/// getValueAsApproximateDouble - This returns the value as an inaccurate
746/// double. Note that this may cause loss of precision, but is useful for
747/// debugging dumps, etc.
748double FloatingLiteral::getValueAsApproximateDouble() const {
749 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000750 bool ignored;
751 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
752 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000753 return V.convertToDouble();
754}
755
Nick Lewycky4ed84042012-02-24 09:07:53 +0000756int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000757 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000758 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000759 case Ascii:
760 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000761 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000762 break;
763 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000764 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000765 break;
766 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000767 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000768 break;
769 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000770 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000771 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000772 }
773 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
774 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000775 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000776 && "character byte widths supported are 1, 2, and 4 only");
777 return CharByteWidth;
778}
779
Craig Topper37932912013-08-18 10:09:15 +0000780StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000781 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000782 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000783 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000784 assert(C.getAsConstantArrayType(Ty) &&
785 "StringLiteral must be of constant array type!");
786
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000787 // Allocate enough space for the StringLiteral plus an array of locations for
788 // any concatenated string tokens.
789 void *Mem = C.Allocate(sizeof(StringLiteral)+
790 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000791 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000792 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Steve Naroffdf7855b2007-02-21 23:46:25 +0000794 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000795 SL->setString(C,Str,Kind,Pascal);
796
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000797 SL->TokLocs[0] = Loc[0];
798 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000799
Chris Lattner630970d2009-02-18 05:49:11 +0000800 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000801 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
802 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000803}
804
Craig Topper37932912013-08-18 10:09:15 +0000805StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
806 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000807 void *Mem = C.Allocate(sizeof(StringLiteral)+
808 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000809 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000810 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000811 SL->CharByteWidth = 0;
812 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000813 SL->NumConcatenated = NumStrs;
814 return SL;
815}
816
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000817void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000818 switch (getKind()) {
819 case Ascii: break; // no prefix.
820 case Wide: OS << 'L'; break;
821 case UTF8: OS << "u8"; break;
822 case UTF16: OS << 'u'; break;
823 case UTF32: OS << 'U'; break;
824 }
825 OS << '"';
826 static const char Hex[] = "0123456789ABCDEF";
827
828 unsigned LastSlashX = getLength();
829 for (unsigned I = 0, N = getLength(); I != N; ++I) {
830 switch (uint32_t Char = getCodeUnit(I)) {
831 default:
832 // FIXME: Convert UTF-8 back to codepoints before rendering.
833
834 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
835 // Leave invalid surrogates alone; we'll use \x for those.
836 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
837 Char <= 0xdbff) {
838 uint32_t Trail = getCodeUnit(I + 1);
839 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
840 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
841 ++I;
842 }
843 }
844
845 if (Char > 0xff) {
846 // If this is a wide string, output characters over 0xff using \x
847 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
848 // codepoint: use \x escapes for invalid codepoints.
849 if (getKind() == Wide ||
850 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
851 // FIXME: Is this the best way to print wchar_t?
852 OS << "\\x";
853 int Shift = 28;
854 while ((Char >> Shift) == 0)
855 Shift -= 4;
856 for (/**/; Shift >= 0; Shift -= 4)
857 OS << Hex[(Char >> Shift) & 15];
858 LastSlashX = I;
859 break;
860 }
861
862 if (Char > 0xffff)
863 OS << "\\U00"
864 << Hex[(Char >> 20) & 15]
865 << Hex[(Char >> 16) & 15];
866 else
867 OS << "\\u";
868 OS << Hex[(Char >> 12) & 15]
869 << Hex[(Char >> 8) & 15]
870 << Hex[(Char >> 4) & 15]
871 << Hex[(Char >> 0) & 15];
872 break;
873 }
874
875 // If we used \x... for the previous character, and this character is a
876 // hexadecimal digit, prevent it being slurped as part of the \x.
877 if (LastSlashX + 1 == I) {
878 switch (Char) {
879 case '0': case '1': case '2': case '3': case '4':
880 case '5': case '6': case '7': case '8': case '9':
881 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
882 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
883 OS << "\"\"";
884 }
885 }
886
887 assert(Char <= 0xff &&
888 "Characters above 0xff should already have been handled.");
889
Jordan Rosea7d03842013-02-08 22:30:41 +0000890 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000891 OS << (char)Char;
892 else // Output anything hard as an octal escape.
893 OS << '\\'
894 << (char)('0' + ((Char >> 6) & 7))
895 << (char)('0' + ((Char >> 3) & 7))
896 << (char)('0' + ((Char >> 0) & 7));
897 break;
898 // Handle some common non-printable cases to make dumps prettier.
899 case '\\': OS << "\\\\"; break;
900 case '"': OS << "\\\""; break;
901 case '\n': OS << "\\n"; break;
902 case '\t': OS << "\\t"; break;
903 case '\a': OS << "\\a"; break;
904 case '\b': OS << "\\b"; break;
905 }
906 }
907 OS << '"';
908}
909
Craig Topper37932912013-08-18 10:09:15 +0000910void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000911 StringKind Kind, bool IsPascal) {
912 //FIXME: we assume that the string data comes from a target that uses the same
913 // code unit size and endianess for the type of string.
914 this->Kind = Kind;
915 this->IsPascal = IsPascal;
916
Nick Lewycky4ed84042012-02-24 09:07:53 +0000917 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000918 assert((Str.size()%CharByteWidth == 0)
919 && "size of data must be multiple of CharByteWidth");
920 Length = Str.size()/CharByteWidth;
921
922 switch(CharByteWidth) {
923 case 1: {
924 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000925 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000926 StrData.asChar = AStrData;
927 break;
928 }
929 case 2: {
930 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000931 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000932 StrData.asUInt16 = AStrData;
933 break;
934 }
935 case 4: {
936 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000937 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000938 StrData.asUInt32 = AStrData;
939 break;
940 }
941 default:
942 assert(false && "unsupported CharByteWidth");
943 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000944}
945
Chris Lattnere925d612010-11-17 07:37:15 +0000946/// getLocationOfByte - Return a source location that points to the specified
947/// byte of this string literal.
948///
949/// Strings are amazingly complex. They can be formed from multiple tokens and
950/// can have escape sequences in them in addition to the usual trigraph and
951/// escaped newline business. This routine handles this complexity.
952///
953SourceLocation StringLiteral::
954getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
955 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000956 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
957 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000958
Chris Lattnere925d612010-11-17 07:37:15 +0000959 // Loop over all of the tokens in this string until we find the one that
960 // contains the byte we're looking for.
961 unsigned TokNo = 0;
962 while (1) {
963 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
964 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
965
966 // Get the spelling of the string so that we can get the data that makes up
967 // the string literal, not the identifier for the macro it is potentially
968 // expanded through.
969 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
970
971 // Re-lex the token to get its length and original spelling.
972 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
973 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000974 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000975 if (Invalid)
976 return StrTokSpellingLoc;
977
978 const char *StrData = Buffer.data()+LocInfo.second;
979
Chris Lattnere925d612010-11-17 07:37:15 +0000980 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000981 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
982 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000983 Token TheTok;
984 TheLexer.LexFromRawLexer(TheTok);
985
986 // Use the StringLiteralParser to compute the length of the string in bytes.
987 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
988 unsigned TokNumBytes = SLP.GetStringLength();
989
990 // If the byte is in this token, return the location of the byte.
991 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000992 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000993 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
994
995 // Now that we know the offset of the token in the spelling, use the
996 // preprocessor to get the offset in the original source.
997 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
998 }
999
1000 // Move to the next string token.
1001 ++TokNo;
1002 ByteNo -= TokNumBytes;
1003 }
1004}
1005
1006
1007
Chris Lattner1b926492006-08-23 06:42:10 +00001008/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1009/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001010StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001011 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001012 case UO_PostInc: return "++";
1013 case UO_PostDec: return "--";
1014 case UO_PreInc: return "++";
1015 case UO_PreDec: return "--";
1016 case UO_AddrOf: return "&";
1017 case UO_Deref: return "*";
1018 case UO_Plus: return "+";
1019 case UO_Minus: return "-";
1020 case UO_Not: return "~";
1021 case UO_LNot: return "!";
1022 case UO_Real: return "__real";
1023 case UO_Imag: return "__imag";
1024 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001025 }
David Blaikief47fa302012-01-17 02:30:50 +00001026 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001027}
1028
John McCalle3027922010-08-25 11:45:40 +00001029UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001030UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1031 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001032 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001033 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1034 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1035 case OO_Amp: return UO_AddrOf;
1036 case OO_Star: return UO_Deref;
1037 case OO_Plus: return UO_Plus;
1038 case OO_Minus: return UO_Minus;
1039 case OO_Tilde: return UO_Not;
1040 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001041 }
1042}
1043
1044OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1045 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001046 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1047 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1048 case UO_AddrOf: return OO_Amp;
1049 case UO_Deref: return OO_Star;
1050 case UO_Plus: return OO_Plus;
1051 case UO_Minus: return OO_Minus;
1052 case UO_Not: return OO_Tilde;
1053 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001054 default: return OO_None;
1055 }
1056}
1057
1058
Chris Lattner0eedafe2006-08-24 04:56:27 +00001059//===----------------------------------------------------------------------===//
1060// Postfix Operators.
1061//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001062
Craig Topper37932912013-08-18 10:09:15 +00001063CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1064 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1065 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001066 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001067 fn->isTypeDependent(),
1068 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001069 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001070 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001071 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001072
Benjamin Kramerc215e762012-08-24 11:54:20 +00001073 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001074 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001075 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001076 if (args[i]->isTypeDependent())
1077 ExprBits.TypeDependent = true;
1078 if (args[i]->isValueDependent())
1079 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001080 if (args[i]->isInstantiationDependent())
1081 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001082 if (args[i]->containsUnexpandedParameterPack())
1083 ExprBits.ContainsUnexpandedParameterPack = true;
1084
Peter Collingbourne3a347252011-02-08 21:18:02 +00001085 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001086 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001087
Peter Collingbourne3a347252011-02-08 21:18:02 +00001088 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001089 RParenLoc = rparenloc;
1090}
Nate Begeman1e36a852008-01-17 17:46:27 +00001091
Craig Topper37932912013-08-18 10:09:15 +00001092CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001093 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1094 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001095 fn->isTypeDependent(),
1096 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001097 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001098 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001099 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001100
Benjamin Kramerc215e762012-08-24 11:54:20 +00001101 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001102 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001103 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001104 if (args[i]->isTypeDependent())
1105 ExprBits.TypeDependent = true;
1106 if (args[i]->isValueDependent())
1107 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001108 if (args[i]->isInstantiationDependent())
1109 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001110 if (args[i]->containsUnexpandedParameterPack())
1111 ExprBits.ContainsUnexpandedParameterPack = true;
1112
Peter Collingbourne3a347252011-02-08 21:18:02 +00001113 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001114 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001115
Peter Collingbourne3a347252011-02-08 21:18:02 +00001116 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001117 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001118}
1119
Craig Topper37932912013-08-18 10:09:15 +00001120CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump11289f42009-09-09 15:08:12 +00001121 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001122 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001123 SubExprs = new (C) Stmt*[PREARGS_START];
1124 CallExprBits.NumPreArgs = 0;
1125}
1126
Craig Topper37932912013-08-18 10:09:15 +00001127CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001128 EmptyShell Empty)
1129 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1130 // FIXME: Why do we allocate this?
1131 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1132 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001133}
1134
Nuno Lopes518e3702009-12-20 23:11:08 +00001135Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001136 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001137
1138 while (SubstNonTypeTemplateParmExpr *NTTP
1139 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1140 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1141 }
1142
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001143 // If we're calling a dereference, look at the pointer instead.
1144 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1145 if (BO->isPtrMemOp())
1146 CEE = BO->getRHS()->IgnoreParenCasts();
1147 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1148 if (UO->getOpcode() == UO_Deref)
1149 CEE = UO->getSubExpr()->IgnoreParenCasts();
1150 }
Chris Lattner52301912009-07-17 15:46:27 +00001151 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001152 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001153 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1154 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001155
1156 return 0;
1157}
1158
Nuno Lopes518e3702009-12-20 23:11:08 +00001159FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001160 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001161}
1162
Chris Lattnere4407ed2007-12-28 05:25:02 +00001163/// setNumArgs - This changes the number of arguments present in this call.
1164/// Any orphaned expressions are deleted by this, and any new operands are set
1165/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001166void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001167 // No change, just return.
1168 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001169
Chris Lattnere4407ed2007-12-28 05:25:02 +00001170 // If shrinking # arguments, just delete the extras and forgot them.
1171 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001172 this->NumArgs = NumArgs;
1173 return;
1174 }
1175
1176 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001177 unsigned NumPreArgs = getNumPreArgs();
1178 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001179 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001180 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001181 NewSubExprs[i] = SubExprs[i];
1182 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001183 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1184 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001185 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregorba6e5572009-04-17 21:46:47 +00001187 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001188 SubExprs = NewSubExprs;
1189 this->NumArgs = NumArgs;
1190}
1191
Alp Tokera724cff2013-12-28 21:59:02 +00001192/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001193/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001194unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001195 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001196 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001197 // ImplicitCastExpr.
1198 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1199 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001200 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001201
Steve Narofff6e3b3292008-01-31 01:07:12 +00001202 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1203 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001204 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001205
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001206 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1207 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001208 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001210 if (!FDecl->getIdentifier())
1211 return 0;
1212
Douglas Gregor15fc9562009-09-12 00:22:50 +00001213 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001214}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001215
Richard Smith5011a002013-01-17 23:46:04 +00001216bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001217 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001218 return Ctx.BuiltinInfo.isUnevaluated(BI);
1219 return false;
1220}
1221
Anders Carlsson00a27592009-05-26 04:57:27 +00001222QualType CallExpr::getCallReturnType() const {
1223 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001224 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001225 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001226 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001227 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001228 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1229 // This should never be overloaded and so should never return null.
1230 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001231
John McCall0009fcc2011-04-26 20:42:42 +00001232 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001233 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001234}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001235
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001236SourceLocation CallExpr::getLocStart() const {
1237 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001238 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001239
1240 SourceLocation begin = getCallee()->getLocStart();
1241 if (begin.isInvalid() && getNumArgs() > 0)
1242 begin = getArg(0)->getLocStart();
1243 return begin;
1244}
1245SourceLocation CallExpr::getLocEnd() const {
1246 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001247 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001248
1249 SourceLocation end = getRParenLoc();
1250 if (end.isInvalid() && getNumArgs() > 0)
1251 end = getArg(getNumArgs() - 1)->getLocEnd();
1252 return end;
1253}
John McCall701417a2011-02-21 06:23:05 +00001254
Craig Topper37932912013-08-18 10:09:15 +00001255OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001256 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001257 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001258 ArrayRef<OffsetOfNode> comps,
1259 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001260 SourceLocation RParenLoc) {
1261 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001262 sizeof(OffsetOfNode) * comps.size() +
1263 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001264
Benjamin Kramerc215e762012-08-24 11:54:20 +00001265 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1266 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001267}
1268
Craig Topper37932912013-08-18 10:09:15 +00001269OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001270 unsigned numComps, unsigned numExprs) {
1271 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1272 sizeof(OffsetOfNode) * numComps +
1273 sizeof(Expr*) * numExprs);
1274 return new (Mem) OffsetOfExpr(numComps, numExprs);
1275}
1276
Craig Topper37932912013-08-18 10:09:15 +00001277OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001278 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001279 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001280 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001281 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1282 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001283 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001284 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001285 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001286 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001287 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001288{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001289 for (unsigned i = 0; i != comps.size(); ++i) {
1290 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001291 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001292
Benjamin Kramerc215e762012-08-24 11:54:20 +00001293 for (unsigned i = 0; i != exprs.size(); ++i) {
1294 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001295 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001296 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001297 ExprBits.ContainsUnexpandedParameterPack = true;
1298
Benjamin Kramerc215e762012-08-24 11:54:20 +00001299 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001300 }
1301}
1302
1303IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1304 assert(getKind() == Field || getKind() == Identifier);
1305 if (getKind() == Field)
1306 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001307
Douglas Gregor882211c2010-04-28 22:16:22 +00001308 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1309}
1310
Craig Topper37932912013-08-18 10:09:15 +00001311MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001312 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001313 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001314 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001315 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001316 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001317 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001318 QualType ty,
1319 ExprValueKind vk,
1320 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001321 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001322
Douglas Gregorea972d32011-02-28 21:54:11 +00001323 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001324 founddecl.getDecl() != memberdecl ||
1325 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001326 if (hasQualOrFound)
1327 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001328
John McCall6b51f282009-11-23 01:53:49 +00001329 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001330 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1331 else if (TemplateKWLoc.isValid())
1332 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattner5c0b4052010-10-30 05:14:06 +00001334 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001335 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1336 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001337
1338 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001339 // FIXME: Wrong. We should be looking at the member declaration we found.
1340 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001341 E->setValueDependent(true);
1342 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001343 E->setInstantiationDependent(true);
1344 }
1345 else if (QualifierLoc &&
1346 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1347 E->setInstantiationDependent(true);
1348
John McCall16df1e52010-03-30 21:47:33 +00001349 E->HasQualifierOrFoundDecl = true;
1350
1351 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001352 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001353 NQ->FoundDecl = founddecl;
1354 }
1355
Abramo Bagnara7945c982012-01-27 09:46:47 +00001356 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1357
John McCall16df1e52010-03-30 21:47:33 +00001358 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001359 bool Dependent = false;
1360 bool InstantiationDependent = false;
1361 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001362 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1363 Dependent,
1364 InstantiationDependent,
1365 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001366 if (InstantiationDependent)
1367 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001368 } else if (TemplateKWLoc.isValid()) {
1369 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001370 }
1371
1372 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001373}
1374
Daniel Dunbarb507f272012-03-09 15:39:15 +00001375SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001376 if (isImplicitAccess()) {
1377 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001378 return getQualifierLoc().getBeginLoc();
1379 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001380 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001381
Daniel Dunbarb507f272012-03-09 15:39:15 +00001382 // FIXME: We don't want this to happen. Rather, we should be able to
1383 // detect all kinds of implicit accesses more cleanly.
1384 SourceLocation BaseStartLoc = getBase()->getLocStart();
1385 if (BaseStartLoc.isValid())
1386 return BaseStartLoc;
1387 return MemberLoc;
1388}
1389SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001390 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001391 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001392 EndLoc = getRAngleLoc();
1393 else if (EndLoc.isInvalid())
1394 EndLoc = getBase()->getLocEnd();
1395 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001396}
1397
Alp Tokerc1086762013-12-07 13:51:35 +00001398bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001399 switch (getCastKind()) {
1400 case CK_DerivedToBase:
1401 case CK_UncheckedDerivedToBase:
1402 case CK_DerivedToBaseMemberPointer:
1403 case CK_BaseToDerived:
1404 case CK_BaseToDerivedMemberPointer:
1405 assert(!path_empty() && "Cast kind should have a base path!");
1406 break;
1407
1408 case CK_CPointerToObjCPointerCast:
1409 assert(getType()->isObjCObjectPointerType());
1410 assert(getSubExpr()->getType()->isPointerType());
1411 goto CheckNoBasePath;
1412
1413 case CK_BlockPointerToObjCPointerCast:
1414 assert(getType()->isObjCObjectPointerType());
1415 assert(getSubExpr()->getType()->isBlockPointerType());
1416 goto CheckNoBasePath;
1417
John McCallc62bb392012-02-15 01:22:51 +00001418 case CK_ReinterpretMemberPointer:
1419 assert(getType()->isMemberPointerType());
1420 assert(getSubExpr()->getType()->isMemberPointerType());
1421 goto CheckNoBasePath;
1422
John McCall9320b872011-09-09 05:25:32 +00001423 case CK_BitCast:
1424 // Arbitrary casts to C pointer types count as bitcasts.
1425 // Otherwise, we should only have block and ObjC pointer casts
1426 // here if they stay within the type kind.
1427 if (!getType()->isPointerType()) {
1428 assert(getType()->isObjCObjectPointerType() ==
1429 getSubExpr()->getType()->isObjCObjectPointerType());
1430 assert(getType()->isBlockPointerType() ==
1431 getSubExpr()->getType()->isBlockPointerType());
1432 }
1433 goto CheckNoBasePath;
1434
1435 case CK_AnyPointerToBlockPointerCast:
1436 assert(getType()->isBlockPointerType());
1437 assert(getSubExpr()->getType()->isAnyPointerType() &&
1438 !getSubExpr()->getType()->isBlockPointerType());
1439 goto CheckNoBasePath;
1440
Douglas Gregored90df32012-02-22 05:02:47 +00001441 case CK_CopyAndAutoreleaseBlockObject:
1442 assert(getType()->isBlockPointerType());
1443 assert(getSubExpr()->getType()->isBlockPointerType());
1444 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001445
1446 case CK_FunctionToPointerDecay:
1447 assert(getType()->isPointerType());
1448 assert(getSubExpr()->getType()->isFunctionType());
1449 goto CheckNoBasePath;
1450
David Tweede1468322013-12-11 13:39:46 +00001451 case CK_AddressSpaceConversion:
1452 assert(getType()->isPointerType());
1453 assert(getSubExpr()->getType()->isPointerType());
1454 assert(getType()->getPointeeType().getAddressSpace() !=
1455 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001456 // These should not have an inheritance path.
1457 case CK_Dynamic:
1458 case CK_ToUnion:
1459 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001460 case CK_NullToMemberPointer:
1461 case CK_NullToPointer:
1462 case CK_ConstructorConversion:
1463 case CK_IntegralToPointer:
1464 case CK_PointerToIntegral:
1465 case CK_ToVoid:
1466 case CK_VectorSplat:
1467 case CK_IntegralCast:
1468 case CK_IntegralToFloating:
1469 case CK_FloatingToIntegral:
1470 case CK_FloatingCast:
1471 case CK_ObjCObjectLValueCast:
1472 case CK_FloatingRealToComplex:
1473 case CK_FloatingComplexToReal:
1474 case CK_FloatingComplexCast:
1475 case CK_FloatingComplexToIntegralComplex:
1476 case CK_IntegralRealToComplex:
1477 case CK_IntegralComplexToReal:
1478 case CK_IntegralComplexCast:
1479 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001480 case CK_ARCProduceObject:
1481 case CK_ARCConsumeObject:
1482 case CK_ARCReclaimReturnedObject:
1483 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001484 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001485 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1486 goto CheckNoBasePath;
1487
1488 case CK_Dependent:
1489 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001490 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001491 case CK_AtomicToNonAtomic:
1492 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001493 case CK_PointerToBoolean:
1494 case CK_IntegralToBoolean:
1495 case CK_FloatingToBoolean:
1496 case CK_MemberPointerToBoolean:
1497 case CK_FloatingComplexToBoolean:
1498 case CK_IntegralComplexToBoolean:
1499 case CK_LValueBitCast: // -> bool&
1500 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001501 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001502 CheckNoBasePath:
1503 assert(path_empty() && "Cast kind should not have a base path!");
1504 break;
1505 }
Alp Tokerc1086762013-12-07 13:51:35 +00001506 return true;
John McCall9320b872011-09-09 05:25:32 +00001507}
1508
Anders Carlsson496335e2009-09-03 00:59:21 +00001509const char *CastExpr::getCastKindName() const {
1510 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001511 case CK_Dependent:
1512 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001513 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001514 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001515 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001516 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001517 case CK_LValueToRValue:
1518 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001519 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001520 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001521 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001522 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001523 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001524 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001525 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001526 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001527 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001528 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001529 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001530 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001531 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001532 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001533 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001534 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001535 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001536 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001537 case CK_NullToPointer:
1538 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001539 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001540 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001541 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001542 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001543 case CK_ReinterpretMemberPointer:
1544 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001545 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001546 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001547 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001548 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001549 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001550 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001551 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001552 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001553 case CK_PointerToBoolean:
1554 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001555 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001556 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001557 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001558 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001559 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001560 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001561 case CK_IntegralToBoolean:
1562 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001563 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001564 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001565 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001566 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001567 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001568 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001569 case CK_FloatingToBoolean:
1570 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001571 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001572 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001573 case CK_CPointerToObjCPointerCast:
1574 return "CPointerToObjCPointerCast";
1575 case CK_BlockPointerToObjCPointerCast:
1576 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001577 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001578 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001579 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001580 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001581 case CK_FloatingRealToComplex:
1582 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001583 case CK_FloatingComplexToReal:
1584 return "FloatingComplexToReal";
1585 case CK_FloatingComplexToBoolean:
1586 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001587 case CK_FloatingComplexCast:
1588 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001589 case CK_FloatingComplexToIntegralComplex:
1590 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001591 case CK_IntegralRealToComplex:
1592 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001593 case CK_IntegralComplexToReal:
1594 return "IntegralComplexToReal";
1595 case CK_IntegralComplexToBoolean:
1596 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001597 case CK_IntegralComplexCast:
1598 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001599 case CK_IntegralComplexToFloatingComplex:
1600 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001601 case CK_ARCConsumeObject:
1602 return "ARCConsumeObject";
1603 case CK_ARCProduceObject:
1604 return "ARCProduceObject";
1605 case CK_ARCReclaimReturnedObject:
1606 return "ARCReclaimReturnedObject";
1607 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001608 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001609 case CK_AtomicToNonAtomic:
1610 return "AtomicToNonAtomic";
1611 case CK_NonAtomicToAtomic:
1612 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001613 case CK_CopyAndAutoreleaseBlockObject:
1614 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001615 case CK_BuiltinFnToFnPtr:
1616 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001617 case CK_ZeroToOCLEvent:
1618 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001619 case CK_AddressSpaceConversion:
1620 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001621 }
Mike Stump11289f42009-09-09 15:08:12 +00001622
John McCallc5e62b42010-11-13 09:02:35 +00001623 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001624}
1625
Douglas Gregord196a582009-12-14 19:27:10 +00001626Expr *CastExpr::getSubExprAsWritten() {
1627 Expr *SubExpr = 0;
1628 CastExpr *E = this;
1629 do {
1630 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001631
1632 // Skip through reference binding to temporary.
1633 if (MaterializeTemporaryExpr *Materialize
1634 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1635 SubExpr = Materialize->GetTemporaryExpr();
1636
Douglas Gregord196a582009-12-14 19:27:10 +00001637 // Skip any temporary bindings; they're implicit.
1638 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1639 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001640
Douglas Gregord196a582009-12-14 19:27:10 +00001641 // Conversions by constructor and conversion functions have a
1642 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001643 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001644 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001645 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001646 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001647
Douglas Gregord196a582009-12-14 19:27:10 +00001648 // If the subexpression we're left with is an implicit cast, look
1649 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001650 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1651
Douglas Gregord196a582009-12-14 19:27:10 +00001652 return SubExpr;
1653}
1654
John McCallcf142162010-08-07 06:22:56 +00001655CXXBaseSpecifier **CastExpr::path_buffer() {
1656 switch (getStmtClass()) {
1657#define ABSTRACT_STMT(x)
1658#define CASTEXPR(Type, Base) \
1659 case Stmt::Type##Class: \
1660 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1661#define STMT(Type, Base)
1662#include "clang/AST/StmtNodes.inc"
1663 default:
1664 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001665 }
1666}
1667
1668void CastExpr::setCastPath(const CXXCastPath &Path) {
1669 assert(Path.size() == path_size());
1670 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1671}
1672
Craig Topper37932912013-08-18 10:09:15 +00001673ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001674 CastKind Kind, Expr *Operand,
1675 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001676 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001677 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1678 void *Buffer =
1679 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1680 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001681 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001682 if (PathSize) E->setCastPath(*BasePath);
1683 return E;
1684}
1685
Craig Topper37932912013-08-18 10:09:15 +00001686ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001687 unsigned PathSize) {
1688 void *Buffer =
1689 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1690 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1691}
1692
1693
Craig Topper37932912013-08-18 10:09:15 +00001694CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001695 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001696 const CXXCastPath *BasePath,
1697 TypeSourceInfo *WrittenTy,
1698 SourceLocation L, SourceLocation R) {
1699 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1700 void *Buffer =
1701 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1702 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001703 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001704 if (PathSize) E->setCastPath(*BasePath);
1705 return E;
1706}
1707
Craig Topper37932912013-08-18 10:09:15 +00001708CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1709 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001710 void *Buffer =
1711 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1712 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1713}
1714
Chris Lattner1b926492006-08-23 06:42:10 +00001715/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1716/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001717StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001718 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001719 case BO_PtrMemD: return ".*";
1720 case BO_PtrMemI: return "->*";
1721 case BO_Mul: return "*";
1722 case BO_Div: return "/";
1723 case BO_Rem: return "%";
1724 case BO_Add: return "+";
1725 case BO_Sub: return "-";
1726 case BO_Shl: return "<<";
1727 case BO_Shr: return ">>";
1728 case BO_LT: return "<";
1729 case BO_GT: return ">";
1730 case BO_LE: return "<=";
1731 case BO_GE: return ">=";
1732 case BO_EQ: return "==";
1733 case BO_NE: return "!=";
1734 case BO_And: return "&";
1735 case BO_Xor: return "^";
1736 case BO_Or: return "|";
1737 case BO_LAnd: return "&&";
1738 case BO_LOr: return "||";
1739 case BO_Assign: return "=";
1740 case BO_MulAssign: return "*=";
1741 case BO_DivAssign: return "/=";
1742 case BO_RemAssign: return "%=";
1743 case BO_AddAssign: return "+=";
1744 case BO_SubAssign: return "-=";
1745 case BO_ShlAssign: return "<<=";
1746 case BO_ShrAssign: return ">>=";
1747 case BO_AndAssign: return "&=";
1748 case BO_XorAssign: return "^=";
1749 case BO_OrAssign: return "|=";
1750 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001751 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001752
David Blaikiee4d798f2012-01-20 21:50:17 +00001753 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001754}
Steve Naroff47500512007-04-19 23:00:49 +00001755
John McCalle3027922010-08-25 11:45:40 +00001756BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001757BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1758 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001759 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001760 case OO_Plus: return BO_Add;
1761 case OO_Minus: return BO_Sub;
1762 case OO_Star: return BO_Mul;
1763 case OO_Slash: return BO_Div;
1764 case OO_Percent: return BO_Rem;
1765 case OO_Caret: return BO_Xor;
1766 case OO_Amp: return BO_And;
1767 case OO_Pipe: return BO_Or;
1768 case OO_Equal: return BO_Assign;
1769 case OO_Less: return BO_LT;
1770 case OO_Greater: return BO_GT;
1771 case OO_PlusEqual: return BO_AddAssign;
1772 case OO_MinusEqual: return BO_SubAssign;
1773 case OO_StarEqual: return BO_MulAssign;
1774 case OO_SlashEqual: return BO_DivAssign;
1775 case OO_PercentEqual: return BO_RemAssign;
1776 case OO_CaretEqual: return BO_XorAssign;
1777 case OO_AmpEqual: return BO_AndAssign;
1778 case OO_PipeEqual: return BO_OrAssign;
1779 case OO_LessLess: return BO_Shl;
1780 case OO_GreaterGreater: return BO_Shr;
1781 case OO_LessLessEqual: return BO_ShlAssign;
1782 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1783 case OO_EqualEqual: return BO_EQ;
1784 case OO_ExclaimEqual: return BO_NE;
1785 case OO_LessEqual: return BO_LE;
1786 case OO_GreaterEqual: return BO_GE;
1787 case OO_AmpAmp: return BO_LAnd;
1788 case OO_PipePipe: return BO_LOr;
1789 case OO_Comma: return BO_Comma;
1790 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001791 }
1792}
1793
1794OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1795 static const OverloadedOperatorKind OverOps[] = {
1796 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1797 OO_Star, OO_Slash, OO_Percent,
1798 OO_Plus, OO_Minus,
1799 OO_LessLess, OO_GreaterGreater,
1800 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1801 OO_EqualEqual, OO_ExclaimEqual,
1802 OO_Amp,
1803 OO_Caret,
1804 OO_Pipe,
1805 OO_AmpAmp,
1806 OO_PipePipe,
1807 OO_Equal, OO_StarEqual,
1808 OO_SlashEqual, OO_PercentEqual,
1809 OO_PlusEqual, OO_MinusEqual,
1810 OO_LessLessEqual, OO_GreaterGreaterEqual,
1811 OO_AmpEqual, OO_CaretEqual,
1812 OO_PipeEqual,
1813 OO_Comma
1814 };
1815 return OverOps[Opc];
1816}
1817
Craig Topper37932912013-08-18 10:09:15 +00001818InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001819 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001820 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001821 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001822 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001823 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001824{
1825 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001826 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001827 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001828 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001829 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001830 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001831 if (initExprs[I]->isInstantiationDependent())
1832 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001833 if (initExprs[I]->containsUnexpandedParameterPack())
1834 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001835 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001836
Benjamin Kramerc215e762012-08-24 11:54:20 +00001837 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001838}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001839
Craig Topper37932912013-08-18 10:09:15 +00001840void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001841 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001842 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001843}
1844
Craig Topper37932912013-08-18 10:09:15 +00001845void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001846 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001847}
1848
Craig Topper37932912013-08-18 10:09:15 +00001849Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001850 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001851 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Richard Smithc275da62013-12-06 01:27:24 +00001852 setInit(Init, expr);
Ted Kremenek013041e2010-02-19 01:50:18 +00001853 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001854 }
Mike Stump11289f42009-09-09 15:08:12 +00001855
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001856 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001857 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001858 return Result;
1859}
1860
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001861void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001862 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001863 ArrayFillerOrUnionFieldInit = filler;
1864 // Fill out any "holes" in the array due to designated initializers.
1865 Expr **inits = getInits();
1866 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1867 if (inits[i] == 0)
1868 inits[i] = filler;
1869}
1870
Richard Smith9ec1e482012-04-15 02:50:59 +00001871bool InitListExpr::isStringLiteralInit() const {
1872 if (getNumInits() != 1)
1873 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001874 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1875 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001876 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001877 // It is possible for getInit() to return null.
1878 const Expr *Init = getInit(0);
1879 if (!Init)
1880 return false;
1881 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001882 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1883}
1884
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001885SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001886 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001887 return SyntacticForm->getLocStart();
1888 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001889 if (Beg.isInvalid()) {
1890 // Find the first non-null initializer.
1891 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1892 E = InitExprs.end();
1893 I != E; ++I) {
1894 if (Stmt *S = *I) {
1895 Beg = S->getLocStart();
1896 break;
1897 }
1898 }
1899 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001900 return Beg;
1901}
1902
1903SourceLocation InitListExpr::getLocEnd() const {
1904 if (InitListExpr *SyntacticForm = getSyntacticForm())
1905 return SyntacticForm->getLocEnd();
1906 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001907 if (End.isInvalid()) {
1908 // Find the first non-null initializer from the end.
1909 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001910 E = InitExprs.rend();
1911 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001912 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001913 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001914 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001915 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001916 }
1917 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001918 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001919}
1920
Steve Naroff991e99d2008-09-04 15:31:07 +00001921/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001922///
John McCallc833dea2012-02-17 03:32:35 +00001923const FunctionProtoType *BlockExpr::getFunctionType() const {
1924 // The block pointer is never sugared, but the function type might be.
1925 return cast<BlockPointerType>(getType())
1926 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001927}
1928
Mike Stump11289f42009-09-09 15:08:12 +00001929SourceLocation BlockExpr::getCaretLocation() const {
1930 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001931}
Mike Stump11289f42009-09-09 15:08:12 +00001932const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001933 return TheBlock->getBody();
1934}
Mike Stump11289f42009-09-09 15:08:12 +00001935Stmt *BlockExpr::getBody() {
1936 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001937}
Steve Naroff415d3d52008-10-08 17:01:13 +00001938
1939
Chris Lattner1ec5f562007-06-27 05:38:08 +00001940//===----------------------------------------------------------------------===//
1941// Generic Expression Routines
1942//===----------------------------------------------------------------------===//
1943
Chris Lattner237f2752009-02-14 07:37:35 +00001944/// isUnusedResultAWarning - Return true if this immediate expression should
1945/// be warned about if the result is unused. If so, fill in Loc and Ranges
1946/// with location to warn on and the source range[s] to report with the
1947/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001948bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1949 SourceRange &R1, SourceRange &R2,
1950 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001951 // Don't warn if the expr is type dependent. The type could end up
1952 // instantiating to void.
1953 if (isTypeDependent())
1954 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001955
Chris Lattner1ec5f562007-06-27 05:38:08 +00001956 switch (getStmtClass()) {
1957 default:
John McCallc493a732010-03-12 07:11:26 +00001958 if (getType()->isVoidType())
1959 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001960 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001961 Loc = getExprLoc();
1962 R1 = getSourceRange();
1963 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001964 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001965 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001966 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001967 case GenericSelectionExprClass:
1968 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001969 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00001970 case ChooseExprClass:
1971 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1972 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001973 case UnaryOperatorClass: {
1974 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001975
Chris Lattner1ec5f562007-06-27 05:38:08 +00001976 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001977 case UO_Plus:
1978 case UO_Minus:
1979 case UO_AddrOf:
1980 case UO_Not:
1981 case UO_LNot:
1982 case UO_Deref:
1983 break;
John McCalle3027922010-08-25 11:45:40 +00001984 case UO_PostInc:
1985 case UO_PostDec:
1986 case UO_PreInc:
1987 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001988 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001989 case UO_Real:
1990 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001991 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001992 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1993 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001994 return false;
1995 break;
John McCalle3027922010-08-25 11:45:40 +00001996 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001997 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001998 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00001999 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002000 Loc = UO->getOperatorLoc();
2001 R1 = UO->getSubExpr()->getSourceRange();
2002 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002003 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002004 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002005 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002006 switch (BO->getOpcode()) {
2007 default:
2008 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002009 // Consider the RHS of comma for side effects. LHS was checked by
2010 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002011 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002012 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2013 // lvalue-ness) of an assignment written in a macro.
2014 if (IntegerLiteral *IE =
2015 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2016 if (IE->getValue() == 0)
2017 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002018 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002019 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002020 case BO_LAnd:
2021 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002022 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2023 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002024 return false;
2025 break;
John McCall1e3715a2010-02-16 04:10:53 +00002026 }
Chris Lattner237f2752009-02-14 07:37:35 +00002027 if (BO->isAssignmentOp())
2028 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002029 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002030 Loc = BO->getOperatorLoc();
2031 R1 = BO->getLHS()->getSourceRange();
2032 R2 = BO->getRHS()->getSourceRange();
2033 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002034 }
Chris Lattner86928112007-08-25 02:00:02 +00002035 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002036 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002037 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002038 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002039
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002040 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002041 // If only one of the LHS or RHS is a warning, the operator might
2042 // be being used for control flow. Only warn if both the LHS and
2043 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002044 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002045 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002046 return false;
2047 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002048 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002049 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002050 }
2051
Chris Lattnera44d1162007-06-27 05:58:59 +00002052 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002053 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002054 Loc = cast<MemberExpr>(this)->getMemberLoc();
2055 R1 = SourceRange(Loc, Loc);
2056 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2057 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner1ec5f562007-06-27 05:38:08 +00002059 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002060 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002061 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2062 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2063 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2064 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002065
Chandler Carruth46339472011-08-17 09:49:44 +00002066 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002067 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002068 // overloads as there is no reasonable way to define these such that they
2069 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002070 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002071 // provides additional value as well. If this list is updated,
2072 // DiagnoseUnusedComparison should be as well.
2073 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002074 switch (Op->getOperator()) {
2075 default:
2076 break;
2077 case OO_EqualEqual:
2078 case OO_ExclaimEqual:
2079 case OO_Less:
2080 case OO_Greater:
2081 case OO_GreaterEqual:
2082 case OO_LessEqual:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002083 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002084 Loc = Op->getOperatorLoc();
2085 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002086 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002087 }
Chandler Carruth46339472011-08-17 09:49:44 +00002088
2089 // Fallthrough for generic call handling.
2090 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002091 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002092 case CXXMemberCallExprClass:
2093 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002094 // If this is a direct call, get the callee.
2095 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002096 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002097 // If the callee has attribute pure, const, or warn_unused_result, warn
2098 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002099 //
2100 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2101 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002102 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2103 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002104 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002105 Loc = CE->getCallee()->getLocStart();
2106 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002107
Chris Lattner1a6babf2009-10-13 04:53:48 +00002108 if (unsigned NumArgs = CE->getNumArgs())
2109 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2110 CE->getArg(NumArgs-1)->getLocEnd());
2111 return true;
2112 }
Chris Lattner237f2752009-02-14 07:37:35 +00002113 }
2114 return false;
2115 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002116
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002117 // If we don't know precisely what we're looking at, let's not warn.
2118 case UnresolvedLookupExprClass:
2119 case CXXUnresolvedConstructExprClass:
2120 return false;
2121
Anders Carlsson6aa50392009-11-17 17:11:23 +00002122 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002123 case CXXConstructExprClass: {
2124 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2125 if (Type->hasAttr<WarnUnusedAttr>()) {
2126 WarnE = this;
2127 Loc = getLocStart();
2128 R1 = getSourceRange();
2129 return true;
2130 }
2131 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002132 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002133 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002134
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002135 case ObjCMessageExprClass: {
2136 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002137 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002138 ME->isInstanceMessage() &&
2139 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002140 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002141 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002142 Loc = getExprLoc();
2143 R1 = ME->getSourceRange();
2144 return true;
2145 }
2146
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002147 const ObjCMethodDecl *MD = ME->getMethodDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +00002148 if (MD && MD->hasAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002149 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002150 Loc = getExprLoc();
2151 return true;
2152 }
Chris Lattner237f2752009-02-14 07:37:35 +00002153 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002154 }
Mike Stump11289f42009-09-09 15:08:12 +00002155
John McCallb7bd14f2010-12-02 01:19:52 +00002156 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002157 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002158 Loc = getExprLoc();
2159 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002160 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002161
John McCallfe96e0b2011-11-06 09:01:30 +00002162 case PseudoObjectExprClass: {
2163 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2164
2165 // Only complain about things that have the form of a getter.
2166 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2167 isa<BinaryOperator>(PO->getSyntacticForm()))
2168 return false;
2169
Eli Friedmanc11535c2012-05-24 00:47:05 +00002170 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002171 Loc = getExprLoc();
2172 R1 = getSourceRange();
2173 return true;
2174 }
2175
Chris Lattner944d3062008-07-26 19:51:01 +00002176 case StmtExprClass: {
2177 // Statement exprs don't logically have side effects themselves, but are
2178 // sometimes used in macros in ways that give them a type that is unused.
2179 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2180 // however, if the result of the stmt expr is dead, we don't want to emit a
2181 // warning.
2182 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002183 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002184 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002185 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002186 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2187 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002188 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002189 }
Mike Stump11289f42009-09-09 15:08:12 +00002190
John McCallc493a732010-03-12 07:11:26 +00002191 if (getType()->isVoidType())
2192 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002193 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002194 Loc = cast<StmtExpr>(this)->getLParenLoc();
2195 R1 = getSourceRange();
2196 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002197 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002198 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002199 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002200 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002201 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002202 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002203 if (CE->getCastKind() == CK_ToVoid) {
2204 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002205 CE->getSubExpr()->getType().isVolatileQualified()) {
2206 const DeclRefExpr *DRE =
2207 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2208 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2209 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2210 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2211 R1, R2, Ctx);
2212 }
2213 }
Chris Lattner2706a552009-07-28 18:25:28 +00002214 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002215 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002216
Eli Friedmanc11535c2012-05-24 00:47:05 +00002217 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002218 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002219 if (CE->getCastKind() == CK_ConstructorConversion)
2220 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002221
Eli Friedmanc11535c2012-05-24 00:47:05 +00002222 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002223 if (const CXXFunctionalCastExpr *CXXCE =
2224 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002225 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002226 R1 = CXXCE->getSubExpr()->getSourceRange();
2227 } else {
2228 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2229 Loc = CStyleCE->getLParenLoc();
2230 R1 = CStyleCE->getSubExpr()->getSourceRange();
2231 }
Chris Lattner237f2752009-02-14 07:37:35 +00002232 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002233 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002234 case ImplicitCastExprClass: {
2235 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002236
Eli Friedmanc11535c2012-05-24 00:47:05 +00002237 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2238 if (ICE->getCastKind() == CK_LValueToRValue &&
2239 ICE->getSubExpr()->getType().isVolatileQualified())
2240 return false;
2241
2242 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2243 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002244 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002245 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002246 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002247 case CXXDefaultInitExprClass:
2248 return (cast<CXXDefaultInitExpr>(this)
2249 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002250
2251 case CXXNewExprClass:
2252 // FIXME: In theory, there might be new expressions that don't have side
2253 // effects (e.g. a placement new with an uninitialized POD).
2254 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002255 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002256 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002257 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002258 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002259 case ExprWithCleanupsClass:
2260 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002261 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002262 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002263}
2264
Fariborz Jahanian07735332009-02-22 18:40:18 +00002265/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002266/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002267bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002268 const Expr *E = IgnoreParens();
2269 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002270 default:
2271 return false;
2272 case ObjCIvarRefExprClass:
2273 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002274 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002275 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002276 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002277 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002278 case MaterializeTemporaryExprClass:
2279 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2280 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002281 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002282 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002283 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002284 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002285
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002286 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2287 if (VD->hasGlobalStorage())
2288 return true;
2289 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002290 // dereferencing to a pointer is always a gc'able candidate,
2291 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002292 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002293 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002294 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002295 return false;
2296 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002297 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002298 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002299 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002300 }
2301 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002302 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002303 }
2304}
Sebastian Redlce354af2010-09-10 20:55:33 +00002305
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002306bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2307 if (isTypeDependent())
2308 return false;
John McCall086a4642010-11-24 05:12:34 +00002309 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002310}
2311
John McCall0009fcc2011-04-26 20:42:42 +00002312QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002313 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002314
2315 // Bound member expressions are always one of these possibilities:
2316 // x->m x.m x->*y x.*y
2317 // (possibly parenthesized)
2318
2319 expr = expr->IgnoreParens();
2320 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2321 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2322 return mem->getMemberDecl()->getType();
2323 }
2324
2325 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2326 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2327 ->getPointeeType();
2328 assert(type->isFunctionType());
2329 return type;
2330 }
2331
2332 assert(isa<UnresolvedMemberExpr>(expr));
2333 return QualType();
2334}
2335
Ted Kremenekfff70962008-01-17 16:57:34 +00002336Expr* Expr::IgnoreParens() {
2337 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002338 while (true) {
2339 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2340 E = P->getSubExpr();
2341 continue;
2342 }
2343 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2344 if (P->getOpcode() == UO_Extension) {
2345 E = P->getSubExpr();
2346 continue;
2347 }
2348 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002349 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2350 if (!P->isResultDependent()) {
2351 E = P->getResultExpr();
2352 continue;
2353 }
2354 }
Eli Friedman75807f22013-07-20 00:40:58 +00002355 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2356 if (!P->isConditionDependent()) {
2357 E = P->getChosenSubExpr();
2358 continue;
2359 }
2360 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002361 return E;
2362 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002363}
2364
Chris Lattnerf2660962008-02-13 01:02:39 +00002365/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2366/// or CastExprs or ImplicitCastExprs, returning their operand.
2367Expr *Expr::IgnoreParenCasts() {
2368 Expr *E = this;
2369 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002370 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002371 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002372 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002373 continue;
2374 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002375 if (MaterializeTemporaryExpr *Materialize
2376 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2377 E = Materialize->GetTemporaryExpr();
2378 continue;
2379 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002380 if (SubstNonTypeTemplateParmExpr *NTTP
2381 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2382 E = NTTP->getReplacement();
2383 continue;
2384 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002385 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002386 }
2387}
2388
John McCall5a4ce8b2010-12-04 08:24:19 +00002389/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2390/// casts. This is intended purely as a temporary workaround for code
2391/// that hasn't yet been rewritten to do the right thing about those
2392/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002393Expr *Expr::IgnoreParenLValueCasts() {
2394 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002395 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002396 E = E->IgnoreParens();
2397 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002398 if (P->getCastKind() == CK_LValueToRValue) {
2399 E = P->getSubExpr();
2400 continue;
2401 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002402 } else if (MaterializeTemporaryExpr *Materialize
2403 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2404 E = Materialize->GetTemporaryExpr();
2405 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002406 } else if (SubstNonTypeTemplateParmExpr *NTTP
2407 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2408 E = NTTP->getReplacement();
2409 continue;
John McCall34376a62010-12-04 03:47:34 +00002410 }
2411 break;
2412 }
2413 return E;
2414}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002415
2416Expr *Expr::ignoreParenBaseCasts() {
2417 Expr *E = this;
2418 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002419 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002420 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2421 if (CE->getCastKind() == CK_DerivedToBase ||
2422 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2423 CE->getCastKind() == CK_NoOp) {
2424 E = CE->getSubExpr();
2425 continue;
2426 }
2427 }
2428
2429 return E;
2430 }
2431}
2432
John McCalleebc8322010-05-05 22:59:52 +00002433Expr *Expr::IgnoreParenImpCasts() {
2434 Expr *E = this;
2435 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002436 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002437 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002438 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002439 continue;
2440 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002441 if (MaterializeTemporaryExpr *Materialize
2442 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2443 E = Materialize->GetTemporaryExpr();
2444 continue;
2445 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002446 if (SubstNonTypeTemplateParmExpr *NTTP
2447 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2448 E = NTTP->getReplacement();
2449 continue;
2450 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002451 return E;
John McCalleebc8322010-05-05 22:59:52 +00002452 }
2453}
2454
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002455Expr *Expr::IgnoreConversionOperator() {
2456 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002457 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002458 return MCE->getImplicitObjectArgument();
2459 }
2460 return this;
2461}
2462
Chris Lattneref26c772009-03-13 17:28:01 +00002463/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2464/// value (including ptr->int casts of the same size). Strip off any
2465/// ParenExpr or CastExprs, returning their operand.
2466Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2467 Expr *E = this;
2468 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002469 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002470
Chris Lattneref26c772009-03-13 17:28:01 +00002471 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2472 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002473 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002474 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002475
Chris Lattneref26c772009-03-13 17:28:01 +00002476 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2477 E = SE;
2478 continue;
2479 }
Mike Stump11289f42009-09-09 15:08:12 +00002480
Abramo Bagnara932e3932010-10-15 07:51:18 +00002481 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002482 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002483 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002484 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002485 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2486 E = SE;
2487 continue;
2488 }
2489 }
Mike Stump11289f42009-09-09 15:08:12 +00002490
Douglas Gregor6a40b082011-09-08 17:56:33 +00002491 if (SubstNonTypeTemplateParmExpr *NTTP
2492 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2493 E = NTTP->getReplacement();
2494 continue;
2495 }
2496
Chris Lattneref26c772009-03-13 17:28:01 +00002497 return E;
2498 }
2499}
2500
Douglas Gregord196a582009-12-14 19:27:10 +00002501bool Expr::isDefaultArgument() const {
2502 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002503 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2504 E = M->GetTemporaryExpr();
2505
Douglas Gregord196a582009-12-14 19:27:10 +00002506 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2507 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002508
Douglas Gregord196a582009-12-14 19:27:10 +00002509 return isa<CXXDefaultArgExpr>(E);
2510}
Chris Lattneref26c772009-03-13 17:28:01 +00002511
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002512/// \brief Skip over any no-op casts and any temporary-binding
2513/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002514static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002515 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2516 E = M->GetTemporaryExpr();
2517
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002518 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002519 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002520 E = ICE->getSubExpr();
2521 else
2522 break;
2523 }
2524
2525 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2526 E = BE->getSubExpr();
2527
2528 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002529 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002530 E = ICE->getSubExpr();
2531 else
2532 break;
2533 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002534
2535 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002536}
2537
John McCall7a626f62010-09-15 10:14:12 +00002538/// isTemporaryObject - Determines if this expression produces a
2539/// temporary of the given class type.
2540bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2541 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2542 return false;
2543
Anders Carlsson66bbf502010-11-28 16:40:49 +00002544 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002545
John McCall02dc8c72010-09-15 20:59:13 +00002546 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002547 if (!E->Classify(C).isPRValue()) {
2548 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002549 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002550 return false;
2551 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002552
John McCallf4ee1dd2010-09-16 06:57:56 +00002553 // Black-list a few cases which yield pr-values of class type that don't
2554 // refer to temporaries of that type:
2555
2556 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002557 if (isa<ImplicitCastExpr>(E)) {
2558 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2559 case CK_DerivedToBase:
2560 case CK_UncheckedDerivedToBase:
2561 return false;
2562 default:
2563 break;
2564 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002565 }
2566
John McCallf4ee1dd2010-09-16 06:57:56 +00002567 // - member expressions (all)
2568 if (isa<MemberExpr>(E))
2569 return false;
2570
Eli Friedman13ffdd82012-06-15 23:51:06 +00002571 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2572 if (BO->isPtrMemOp())
2573 return false;
2574
John McCallc07a0c72011-02-17 10:25:35 +00002575 // - opaque values (all)
2576 if (isa<OpaqueValueExpr>(E))
2577 return false;
2578
John McCall7a626f62010-09-15 10:14:12 +00002579 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002580}
2581
Douglas Gregor25b7e052011-03-02 21:06:53 +00002582bool Expr::isImplicitCXXThis() const {
2583 const Expr *E = this;
2584
2585 // Strip away parentheses and casts we don't care about.
2586 while (true) {
2587 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2588 E = Paren->getSubExpr();
2589 continue;
2590 }
2591
2592 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2593 if (ICE->getCastKind() == CK_NoOp ||
2594 ICE->getCastKind() == CK_LValueToRValue ||
2595 ICE->getCastKind() == CK_DerivedToBase ||
2596 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2597 E = ICE->getSubExpr();
2598 continue;
2599 }
2600 }
2601
2602 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2603 if (UnOp->getOpcode() == UO_Extension) {
2604 E = UnOp->getSubExpr();
2605 continue;
2606 }
2607 }
2608
Douglas Gregorfe314812011-06-21 17:03:29 +00002609 if (const MaterializeTemporaryExpr *M
2610 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2611 E = M->GetTemporaryExpr();
2612 continue;
2613 }
2614
Douglas Gregor25b7e052011-03-02 21:06:53 +00002615 break;
2616 }
2617
2618 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2619 return This->isImplicit();
2620
2621 return false;
2622}
2623
Douglas Gregor4619e432008-12-05 23:32:09 +00002624/// hasAnyTypeDependentArguments - Determines if any of the expressions
2625/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002626bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002627 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002628 if (Exprs[I]->isTypeDependent())
2629 return true;
2630
2631 return false;
2632}
2633
John McCall8b0f4ff2010-08-02 21:13:48 +00002634bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002635 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002636 // which can be evaluated at compile-time. It very closely parallels
2637 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2638 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2639 // to isEvaluatable most of the time.
2640 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002641 // If we ever capture reference-binding directly in the AST, we can
2642 // kill the second parameter.
2643
2644 if (IsForRef) {
2645 EvalResult Result;
2646 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2647 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002648
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002649 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002650 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002651 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002652 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002653 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002654 case CXXTemporaryObjectExprClass:
2655 case CXXConstructExprClass: {
2656 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002657
Eli Friedman4c27ac22013-07-16 22:40:53 +00002658 if (CE->getConstructor()->isTrivial() &&
2659 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2660 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002661 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002662
Eli Friedman4c27ac22013-07-16 22:40:53 +00002663 // Trivial copy constructor
2664 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2665 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002666 }
2667
Richard Smithd62306a2011-11-10 06:34:14 +00002668 break;
John McCall81c9cea2010-08-01 21:51:45 +00002669 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002670 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002671 // This handles gcc's extension that allows global initializers like
2672 // "struct x {int x;} x = (struct x) {};".
2673 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002674 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002675 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002676 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002677 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002678 const InitListExpr *ILE = cast<InitListExpr>(this);
2679 if (ILE->getType()->isArrayType()) {
2680 unsigned numInits = ILE->getNumInits();
2681 for (unsigned i = 0; i < numInits; i++) {
2682 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2683 return false;
2684 }
2685 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002686 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002687
2688 if (ILE->getType()->isRecordType()) {
2689 unsigned ElementNo = 0;
2690 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2691 for (RecordDecl::field_iterator Field = RD->field_begin(),
2692 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2693 // If this is a union, skip all the fields that aren't being initialized.
2694 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2695 continue;
2696
2697 // Don't emit anonymous bitfields, they just affect layout.
2698 if (Field->isUnnamedBitfield())
2699 continue;
2700
2701 if (ElementNo < ILE->getNumInits()) {
2702 const Expr *Elt = ILE->getInit(ElementNo++);
2703 if (Field->isBitField()) {
2704 // Bitfields have to evaluate to an integer.
2705 llvm::APSInt ResultTmp;
2706 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2707 return false;
2708 } else {
2709 bool RefType = Field->getType()->isReferenceType();
2710 if (!Elt->isConstantInitializer(Ctx, RefType))
2711 return false;
2712 }
2713 }
2714 }
2715 return true;
2716 }
2717
2718 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002719 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002720 case ImplicitValueInitExprClass:
2721 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002722 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002723 return cast<ParenExpr>(this)->getSubExpr()
2724 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002725 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002726 return cast<GenericSelectionExpr>(this)->getResultExpr()
2727 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002728 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002729 if (cast<ChooseExpr>(this)->isConditionDependent())
2730 return false;
2731 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002732 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002733 case UnaryOperatorClass: {
2734 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002735 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002736 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002737 break;
2738 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002739 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002740 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002741 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002742 case CStyleCastExprClass:
2743 case ObjCBridgedCastExprClass:
2744 case CXXDynamicCastExprClass:
2745 case CXXReinterpretCastExprClass:
2746 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002747 const CastExpr *CE = cast<CastExpr>(this);
2748
Eli Friedman13ec75b2011-12-21 00:43:02 +00002749 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002750 if (CE->getCastKind() == CK_NoOp ||
2751 CE->getCastKind() == CK_LValueToRValue ||
2752 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002753 CE->getCastKind() == CK_ConstructorConversion ||
2754 CE->getCastKind() == CK_NonAtomicToAtomic ||
2755 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002756 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2757
Eli Friedman384da272009-01-25 03:12:18 +00002758 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002759 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002760 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002761 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002762 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002763
2764 case SubstNonTypeTemplateParmExprClass:
2765 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2766 ->isConstantInitializer(Ctx, false);
2767 case CXXDefaultArgExprClass:
2768 return cast<CXXDefaultArgExpr>(this)->getExpr()
2769 ->isConstantInitializer(Ctx, false);
2770 case CXXDefaultInitExprClass:
2771 return cast<CXXDefaultInitExpr>(this)->getExpr()
2772 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002773 }
Eli Friedman384da272009-01-25 03:12:18 +00002774 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002775}
2776
Richard Smith0421ce72012-08-07 04:16:51 +00002777bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2778 if (isInstantiationDependent())
2779 return true;
2780
2781 switch (getStmtClass()) {
2782 case NoStmtClass:
2783 #define ABSTRACT_STMT(Type)
2784 #define STMT(Type, Base) case Type##Class:
2785 #define EXPR(Type, Base)
2786 #include "clang/AST/StmtNodes.inc"
2787 llvm_unreachable("unexpected Expr kind");
2788
2789 case DependentScopeDeclRefExprClass:
2790 case CXXUnresolvedConstructExprClass:
2791 case CXXDependentScopeMemberExprClass:
2792 case UnresolvedLookupExprClass:
2793 case UnresolvedMemberExprClass:
2794 case PackExpansionExprClass:
2795 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002796 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002797 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2798
Richard Smitha33e4fe2012-08-07 05:18:29 +00002799 case DeclRefExprClass:
2800 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002801 case PredefinedExprClass:
2802 case IntegerLiteralClass:
2803 case FloatingLiteralClass:
2804 case ImaginaryLiteralClass:
2805 case StringLiteralClass:
2806 case CharacterLiteralClass:
2807 case OffsetOfExprClass:
2808 case ImplicitValueInitExprClass:
2809 case UnaryExprOrTypeTraitExprClass:
2810 case AddrLabelExprClass:
2811 case GNUNullExprClass:
2812 case CXXBoolLiteralExprClass:
2813 case CXXNullPtrLiteralExprClass:
2814 case CXXThisExprClass:
2815 case CXXScalarValueInitExprClass:
2816 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002817 case ArrayTypeTraitExprClass:
2818 case ExpressionTraitExprClass:
2819 case CXXNoexceptExprClass:
2820 case SizeOfPackExprClass:
2821 case ObjCStringLiteralClass:
2822 case ObjCEncodeExprClass:
2823 case ObjCBoolLiteralExprClass:
2824 case CXXUuidofExprClass:
2825 case OpaqueValueExprClass:
2826 // These never have a side-effect.
2827 return false;
2828
2829 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002830 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002831 case CompoundAssignOperatorClass:
2832 case VAArgExprClass:
2833 case AtomicExprClass:
2834 case StmtExprClass:
2835 case CXXOperatorCallExprClass:
2836 case CXXMemberCallExprClass:
2837 case UserDefinedLiteralClass:
2838 case CXXThrowExprClass:
2839 case CXXNewExprClass:
2840 case CXXDeleteExprClass:
2841 case ExprWithCleanupsClass:
2842 case CXXBindTemporaryExprClass:
2843 case BlockExprClass:
2844 case CUDAKernelCallExprClass:
2845 // These always have a side-effect.
2846 return true;
2847
2848 case ParenExprClass:
2849 case ArraySubscriptExprClass:
2850 case MemberExprClass:
2851 case ConditionalOperatorClass:
2852 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002853 case CompoundLiteralExprClass:
2854 case ExtVectorElementExprClass:
2855 case DesignatedInitExprClass:
2856 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002857 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002858 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002859 case SubstNonTypeTemplateParmExprClass:
2860 case MaterializeTemporaryExprClass:
2861 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002862 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002863 case AsTypeExprClass:
2864 // These have a side-effect if any subexpression does.
2865 break;
2866
Richard Smitha33e4fe2012-08-07 05:18:29 +00002867 case UnaryOperatorClass:
2868 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002869 return true;
2870 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002871
2872 case BinaryOperatorClass:
2873 if (cast<BinaryOperator>(this)->isAssignmentOp())
2874 return true;
2875 break;
2876
Richard Smith0421ce72012-08-07 04:16:51 +00002877 case InitListExprClass:
2878 // FIXME: The children for an InitListExpr doesn't include the array filler.
2879 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2880 if (E->HasSideEffects(Ctx))
2881 return true;
2882 break;
2883
2884 case GenericSelectionExprClass:
2885 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2886 HasSideEffects(Ctx);
2887
2888 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002889 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002890
2891 case CXXDefaultArgExprClass:
2892 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2893
Richard Smith852c9db2013-04-20 22:23:05 +00002894 case CXXDefaultInitExprClass:
2895 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2896 return E->HasSideEffects(Ctx);
2897 // If we've not yet parsed the initializer, assume it has side-effects.
2898 return true;
2899
Richard Smith0421ce72012-08-07 04:16:51 +00002900 case CXXDynamicCastExprClass: {
2901 // A dynamic_cast expression has side-effects if it can throw.
2902 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2903 if (DCE->getTypeAsWritten()->isReferenceType() &&
2904 DCE->getCastKind() == CK_Dynamic)
2905 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002906 } // Fall through.
2907 case ImplicitCastExprClass:
2908 case CStyleCastExprClass:
2909 case CXXStaticCastExprClass:
2910 case CXXReinterpretCastExprClass:
2911 case CXXConstCastExprClass:
2912 case CXXFunctionalCastExprClass: {
2913 const CastExpr *CE = cast<CastExpr>(this);
2914 if (CE->getCastKind() == CK_LValueToRValue &&
2915 CE->getSubExpr()->getType().isVolatileQualified())
2916 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002917 break;
2918 }
2919
Richard Smithef8bf432012-08-13 20:08:14 +00002920 case CXXTypeidExprClass:
2921 // typeid might throw if its subexpression is potentially-evaluated, so has
2922 // side-effects in that case whether or not its subexpression does.
2923 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002924
2925 case CXXConstructExprClass:
2926 case CXXTemporaryObjectExprClass: {
2927 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002928 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002929 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002930 // A trivial constructor does not add any side-effects of its own. Just look
2931 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002932 break;
2933 }
2934
2935 case LambdaExprClass: {
2936 const LambdaExpr *LE = cast<LambdaExpr>(this);
2937 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2938 E = LE->capture_end(); I != E; ++I)
2939 if (I->getCaptureKind() == LCK_ByCopy)
2940 // FIXME: Only has a side-effect if the variable is volatile or if
2941 // the copy would invoke a non-trivial copy constructor.
2942 return true;
2943 return false;
2944 }
2945
2946 case PseudoObjectExprClass: {
2947 // Only look for side-effects in the semantic form, and look past
2948 // OpaqueValueExpr bindings in that form.
2949 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2950 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2951 E = PO->semantics_end();
2952 I != E; ++I) {
2953 const Expr *Subexpr = *I;
2954 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2955 Subexpr = OVE->getSourceExpr();
2956 if (Subexpr->HasSideEffects(Ctx))
2957 return true;
2958 }
2959 return false;
2960 }
2961
2962 case ObjCBoxedExprClass:
2963 case ObjCArrayLiteralClass:
2964 case ObjCDictionaryLiteralClass:
2965 case ObjCMessageExprClass:
2966 case ObjCSelectorExprClass:
2967 case ObjCProtocolExprClass:
2968 case ObjCPropertyRefExprClass:
2969 case ObjCIsaExprClass:
2970 case ObjCIndirectCopyRestoreExprClass:
2971 case ObjCSubscriptRefExprClass:
2972 case ObjCBridgedCastExprClass:
2973 // FIXME: Classify these cases better.
2974 return true;
2975 }
2976
2977 // Recurse to children.
2978 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2979 if (const Stmt *S = *SubStmts)
2980 if (cast<Expr>(S)->HasSideEffects(Ctx))
2981 return true;
2982
2983 return false;
2984}
2985
Douglas Gregor1be329d2012-02-23 07:33:15 +00002986namespace {
2987 /// \brief Look for a call to a non-trivial function within an expression.
2988 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2989 {
2990 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2991
2992 bool NonTrivial;
2993
2994 public:
2995 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002996 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002997
2998 bool hasNonTrivialCall() const { return NonTrivial; }
2999
3000 void VisitCallExpr(CallExpr *E) {
3001 if (CXXMethodDecl *Method
3002 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3003 if (Method->isTrivial()) {
3004 // Recurse to children of the call.
3005 Inherited::VisitStmt(E);
3006 return;
3007 }
3008 }
3009
3010 NonTrivial = true;
3011 }
3012
3013 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3014 if (E->getConstructor()->isTrivial()) {
3015 // Recurse to children of the call.
3016 Inherited::VisitStmt(E);
3017 return;
3018 }
3019
3020 NonTrivial = true;
3021 }
3022
3023 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3024 if (E->getTemporary()->getDestructor()->isTrivial()) {
3025 Inherited::VisitStmt(E);
3026 return;
3027 }
3028
3029 NonTrivial = true;
3030 }
3031 };
3032}
3033
3034bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3035 NonTrivialCallFinder Finder(Ctx);
3036 Finder.Visit(this);
3037 return Finder.hasNonTrivialCall();
3038}
3039
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003040/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3041/// pointer constant or not, as well as the specific kind of constant detected.
3042/// Null pointer constants can be integer constant expressions with the
3043/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3044/// (a GNU extension).
3045Expr::NullPointerConstantKind
3046Expr::isNullPointerConstant(ASTContext &Ctx,
3047 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003048 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003049 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003050 switch (NPC) {
3051 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003052 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003053 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003054 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003055 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003056 else
3057 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003058
Douglas Gregor56751b52009-09-25 04:25:58 +00003059 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003060 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003061 }
3062 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003063
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003064 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003065 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003066 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003067 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003068 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003069 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003070 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003071 Pointee->isVoidType() && // to void*
3072 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003073 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003074 }
Steve Naroffada7d422007-05-20 17:54:12 +00003075 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003076 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3077 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003078 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003079 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3080 // Accept ((void*)0) as a null pointer constant, as many other
3081 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003082 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003083 } else if (const GenericSelectionExpr *GE =
3084 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003085 if (GE->isResultDependent())
3086 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003087 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003088 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3089 if (CE->isConditionDependent())
3090 return NPCK_NotNull;
3091 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003092 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003093 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003094 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003095 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003096 } else if (const CXXDefaultInitExpr *DefaultInit
3097 = dyn_cast<CXXDefaultInitExpr>(this)) {
3098 // See through default initializer expressions.
3099 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003100 } else if (isa<GNUNullExpr>(this)) {
3101 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003102 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003103 } else if (const MaterializeTemporaryExpr *M
3104 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3105 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003106 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3107 if (const Expr *Source = OVE->getSourceExpr())
3108 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003109 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003110
Richard Smith89645bc2013-01-02 12:01:23 +00003111 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003112 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003113 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003114
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003115 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003116 if (!Ctx.getLangOpts().CPlusPlus11 &&
3117 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003118 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3119 const Expr *InitExpr = CLE->getInitializer();
3120 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3121 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3122 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003123 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003124 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003125 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003126 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003127
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003128 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003129 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3130 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003131 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003132 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003133 if (Lit && !Lit->getValue())
3134 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003135 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003136 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003137 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003138 // If we have an integer constant expression, we need to *evaluate* it and
3139 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003140 if (!isIntegerConstantExpr(Ctx))
3141 return NPCK_NotNull;
3142 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003143
David Blaikie1c7c8f72012-08-08 17:33:31 +00003144 if (EvaluateKnownConstInt(Ctx) != 0)
3145 return NPCK_NotNull;
3146
3147 if (isa<IntegerLiteral>(this))
3148 return NPCK_ZeroLiteral;
3149 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003150}
Steve Narofff7a5da12007-07-28 23:10:27 +00003151
John McCall34376a62010-12-04 03:47:34 +00003152/// \brief If this expression is an l-value for an Objective C
3153/// property, find the underlying property reference expression.
3154const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3155 const Expr *E = this;
3156 while (true) {
3157 assert((E->getValueKind() == VK_LValue &&
3158 E->getObjectKind() == OK_ObjCProperty) &&
3159 "expression is not a property reference");
3160 E = E->IgnoreParenCasts();
3161 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3162 if (BO->getOpcode() == BO_Comma) {
3163 E = BO->getRHS();
3164 continue;
3165 }
3166 }
3167
3168 break;
3169 }
3170
3171 return cast<ObjCPropertyRefExpr>(E);
3172}
3173
Anna Zaks97c7ce32012-10-01 20:34:04 +00003174bool Expr::isObjCSelfExpr() const {
3175 const Expr *E = IgnoreParenImpCasts();
3176
3177 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3178 if (!DRE)
3179 return false;
3180
3181 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3182 if (!Param)
3183 return false;
3184
3185 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3186 if (!M)
3187 return false;
3188
3189 return M->getSelfDecl() == Param;
3190}
3191
John McCalld25db7e2013-05-06 21:39:12 +00003192FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003193 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003194
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003195 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003196 if (ICE->getCastKind() == CK_LValueToRValue ||
3197 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003198 E = ICE->getSubExpr()->IgnoreParens();
3199 else
3200 break;
3201 }
3202
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003203 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003204 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003205 if (Field->isBitField())
3206 return Field;
3207
John McCalld25db7e2013-05-06 21:39:12 +00003208 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3209 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3210 if (Ivar->isBitField())
3211 return Ivar;
3212
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003213 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3214 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3215 if (Field->isBitField())
3216 return Field;
3217
Eli Friedman609ada22011-07-13 02:05:57 +00003218 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003219 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003220 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003221
Eli Friedman609ada22011-07-13 02:05:57 +00003222 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003223 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003224 }
3225
Douglas Gregor71235ec2009-05-02 02:18:30 +00003226 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003227}
3228
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003229bool Expr::refersToVectorElement() const {
3230 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003231
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003232 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003233 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003234 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003235 E = ICE->getSubExpr()->IgnoreParens();
3236 else
3237 break;
3238 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003239
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003240 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3241 return ASE->getBase()->getType()->isVectorType();
3242
3243 if (isa<ExtVectorElementExpr>(E))
3244 return true;
3245
3246 return false;
3247}
3248
Chris Lattnerb8211f62009-02-16 22:14:05 +00003249/// isArrow - Return true if the base expression is a pointer to vector,
3250/// return false if the base expression is a vector.
3251bool ExtVectorElementExpr::isArrow() const {
3252 return getBase()->getType()->isPointerType();
3253}
3254
Nate Begemance4d7fc2008-04-18 23:10:10 +00003255unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003256 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003257 return VT->getNumElements();
3258 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003259}
3260
Nate Begemanf322eab2008-05-09 06:41:27 +00003261/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003262bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003263 // FIXME: Refactor this code to an accessor on the AST node which returns the
3264 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003265 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003266
3267 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003268 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003269 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003270
Nate Begeman7e5185b2009-01-18 02:01:21 +00003271 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003272 if (Comp[0] == 's' || Comp[0] == 'S')
3273 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003274
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003275 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003276 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003277 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003278
Steve Naroff0d595ca2007-07-30 03:29:09 +00003279 return false;
3280}
Chris Lattner885b4952007-08-02 23:36:59 +00003281
Nate Begemanf322eab2008-05-09 06:41:27 +00003282/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003283void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003284 SmallVectorImpl<unsigned> &Elts) const {
3285 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003286 if (Comp[0] == 's' || Comp[0] == 'S')
3287 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003288
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003289 bool isHi = Comp == "hi";
3290 bool isLo = Comp == "lo";
3291 bool isEven = Comp == "even";
3292 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003293
Nate Begemanf322eab2008-05-09 06:41:27 +00003294 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3295 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003296
Nate Begemanf322eab2008-05-09 06:41:27 +00003297 if (isHi)
3298 Index = e + i;
3299 else if (isLo)
3300 Index = i;
3301 else if (isEven)
3302 Index = 2 * i;
3303 else if (isOdd)
3304 Index = 2 * i + 1;
3305 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003306 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003307
Nate Begemand3862152008-05-13 21:03:02 +00003308 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003309 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003310}
3311
Douglas Gregor9a129192010-04-21 00:45:42 +00003312ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003313 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003314 SourceLocation LBracLoc,
3315 SourceLocation SuperLoc,
3316 bool IsInstanceSuper,
3317 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003318 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003319 ArrayRef<SourceLocation> SelLocs,
3320 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003321 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003322 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003323 SourceLocation RBracLoc,
3324 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003325 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003326 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003327 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003328 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003329 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3330 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003331 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003332 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3333 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003334{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003335 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003336 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003337}
3338
Douglas Gregor9a129192010-04-21 00:45:42 +00003339ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003340 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003341 SourceLocation LBracLoc,
3342 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003343 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003344 ArrayRef<SourceLocation> SelLocs,
3345 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003346 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003347 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003348 SourceLocation RBracLoc,
3349 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003350 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003351 T->isDependentType(), T->isInstantiationDependentType(),
3352 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003353 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3354 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003355 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003356 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003357 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003358{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003359 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003360 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003361}
3362
Douglas Gregor9a129192010-04-21 00:45:42 +00003363ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003364 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003365 SourceLocation LBracLoc,
3366 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003367 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003368 ArrayRef<SourceLocation> SelLocs,
3369 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003370 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003371 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003372 SourceLocation RBracLoc,
3373 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003374 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003375 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003376 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003377 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003378 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3379 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003380 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003381 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003382 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003383{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003384 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003385 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003386}
3387
3388void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3389 ArrayRef<SourceLocation> SelLocs,
3390 SelectorLocationsKind SelLocsK) {
3391 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003392 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003393 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003394 if (Args[I]->isTypeDependent())
3395 ExprBits.TypeDependent = true;
3396 if (Args[I]->isValueDependent())
3397 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003398 if (Args[I]->isInstantiationDependent())
3399 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003400 if (Args[I]->containsUnexpandedParameterPack())
3401 ExprBits.ContainsUnexpandedParameterPack = true;
3402
3403 MyArgs[I] = Args[I];
3404 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003405
Benjamin Kramer2325b242012-02-20 00:20:48 +00003406 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003407 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003408 if (SelLocsK == SelLoc_NonStandard)
3409 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3410 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003411}
3412
Craig Topperce7167c2013-08-22 04:58:56 +00003413ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003414 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003415 SourceLocation LBracLoc,
3416 SourceLocation SuperLoc,
3417 bool IsInstanceSuper,
3418 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003419 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003420 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003421 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003422 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003423 SourceLocation RBracLoc,
3424 bool isImplicit) {
3425 assert((!SelLocs.empty() || isImplicit) &&
3426 "No selector locs for non-implicit message");
3427 ObjCMessageExpr *Mem;
3428 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3429 if (isImplicit)
3430 Mem = alloc(Context, Args.size(), 0);
3431 else
3432 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003433 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003434 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003435 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003436}
3437
Craig Topperce7167c2013-08-22 04:58:56 +00003438ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003439 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003440 SourceLocation LBracLoc,
3441 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003442 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003443 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003444 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003445 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003446 SourceLocation RBracLoc,
3447 bool isImplicit) {
3448 assert((!SelLocs.empty() || isImplicit) &&
3449 "No selector locs for non-implicit message");
3450 ObjCMessageExpr *Mem;
3451 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3452 if (isImplicit)
3453 Mem = alloc(Context, Args.size(), 0);
3454 else
3455 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003456 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003457 SelLocs, SelLocsK, Method, Args, RBracLoc,
3458 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003459}
3460
Craig Topperce7167c2013-08-22 04:58:56 +00003461ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003462 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003463 SourceLocation LBracLoc,
3464 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003465 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003466 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003467 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003468 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003469 SourceLocation RBracLoc,
3470 bool isImplicit) {
3471 assert((!SelLocs.empty() || isImplicit) &&
3472 "No selector locs for non-implicit message");
3473 ObjCMessageExpr *Mem;
3474 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3475 if (isImplicit)
3476 Mem = alloc(Context, Args.size(), 0);
3477 else
3478 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003479 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003480 SelLocs, SelLocsK, Method, Args, RBracLoc,
3481 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003482}
3483
Craig Topperce7167c2013-08-22 04:58:56 +00003484ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003485 unsigned NumArgs,
3486 unsigned NumStoredSelLocs) {
3487 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003488 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3489}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003490
Craig Topperce7167c2013-08-22 04:58:56 +00003491ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003492 ArrayRef<Expr *> Args,
3493 SourceLocation RBraceLoc,
3494 ArrayRef<SourceLocation> SelLocs,
3495 Selector Sel,
3496 SelectorLocationsKind &SelLocsK) {
3497 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3498 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3499 : 0;
3500 return alloc(C, Args.size(), NumStoredSelLocs);
3501}
3502
Craig Topperce7167c2013-08-22 04:58:56 +00003503ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003504 unsigned NumArgs,
3505 unsigned NumStoredSelLocs) {
3506 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3507 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3508 return (ObjCMessageExpr *)C.Allocate(Size,
3509 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3510}
3511
3512void ObjCMessageExpr::getSelectorLocs(
3513 SmallVectorImpl<SourceLocation> &SelLocs) const {
3514 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3515 SelLocs.push_back(getSelectorLoc(i));
3516}
3517
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003518SourceRange ObjCMessageExpr::getReceiverRange() const {
3519 switch (getReceiverKind()) {
3520 case Instance:
3521 return getInstanceReceiver()->getSourceRange();
3522
3523 case Class:
3524 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3525
3526 case SuperInstance:
3527 case SuperClass:
3528 return getSuperLoc();
3529 }
3530
David Blaikiee4d798f2012-01-20 21:50:17 +00003531 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003532}
3533
Douglas Gregor9a129192010-04-21 00:45:42 +00003534Selector ObjCMessageExpr::getSelector() const {
3535 if (HasMethod)
3536 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3537 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003538 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003539}
3540
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003541QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003542 switch (getReceiverKind()) {
3543 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003544 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003545 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003546 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003547 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003548 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003549 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003550 }
3551
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003552 llvm_unreachable("unexpected receiver kind");
3553}
3554
3555ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3556 QualType T = getReceiverType();
3557
3558 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3559 return Ptr->getInterfaceDecl();
3560
3561 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3562 return Ty->getInterface();
3563
Douglas Gregor9a129192010-04-21 00:45:42 +00003564 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003565}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003566
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003567StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003568 switch (getBridgeKind()) {
3569 case OBC_Bridge:
3570 return "__bridge";
3571 case OBC_BridgeTransfer:
3572 return "__bridge_transfer";
3573 case OBC_BridgeRetained:
3574 return "__bridge_retained";
3575 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003576
3577 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003578}
3579
Craig Topper37932912013-08-18 10:09:15 +00003580ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003581 QualType Type, SourceLocation BLoc,
3582 SourceLocation RP)
3583 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3584 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003585 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003586 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003587 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003588{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003589 SubExprs = new (C) Stmt*[args.size()];
3590 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003591 if (args[i]->isTypeDependent())
3592 ExprBits.TypeDependent = true;
3593 if (args[i]->isValueDependent())
3594 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003595 if (args[i]->isInstantiationDependent())
3596 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003597 if (args[i]->containsUnexpandedParameterPack())
3598 ExprBits.ContainsUnexpandedParameterPack = true;
3599
3600 SubExprs[i] = args[i];
3601 }
3602}
3603
Craig Topper37932912013-08-18 10:09:15 +00003604void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003605 if (SubExprs) C.Deallocate(SubExprs);
3606
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003607 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003608 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003609 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003610}
Nate Begeman48745922009-08-12 02:28:50 +00003611
Craig Topper37932912013-08-18 10:09:15 +00003612GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003613 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003614 ArrayRef<TypeSourceInfo*> AssocTypes,
3615 ArrayRef<Expr*> AssocExprs,
3616 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003617 SourceLocation RParenLoc,
3618 bool ContainsUnexpandedParameterPack,
3619 unsigned ResultIndex)
3620 : Expr(GenericSelectionExprClass,
3621 AssocExprs[ResultIndex]->getType(),
3622 AssocExprs[ResultIndex]->getValueKind(),
3623 AssocExprs[ResultIndex]->getObjectKind(),
3624 AssocExprs[ResultIndex]->isTypeDependent(),
3625 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003626 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003627 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003628 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3629 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3630 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3631 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003632 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003633 assert(AssocTypes.size() == AssocExprs.size());
3634 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3635 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003636}
3637
Craig Topper37932912013-08-18 10:09:15 +00003638GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003639 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003640 ArrayRef<TypeSourceInfo*> AssocTypes,
3641 ArrayRef<Expr*> AssocExprs,
3642 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003643 SourceLocation RParenLoc,
3644 bool ContainsUnexpandedParameterPack)
3645 : Expr(GenericSelectionExprClass,
3646 Context.DependentTy,
3647 VK_RValue,
3648 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003649 /*isTypeDependent=*/true,
3650 /*isValueDependent=*/true,
3651 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003652 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003653 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3654 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3655 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3656 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003657 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003658 assert(AssocTypes.size() == AssocExprs.size());
3659 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3660 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003661}
3662
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003663//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003664// DesignatedInitExpr
3665//===----------------------------------------------------------------------===//
3666
Chandler Carruth631abd92011-06-16 06:47:06 +00003667IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003668 assert(Kind == FieldDesignator && "Only valid on a field designator");
3669 if (Field.NameOrField & 0x01)
3670 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3671 else
3672 return getField()->getIdentifier();
3673}
3674
Craig Topper37932912013-08-18 10:09:15 +00003675DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003676 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003677 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003678 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003679 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003680 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003681 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003682 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003683 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003684 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003685 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003686 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003687 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003688 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003689 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003690
3691 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003692 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003693 *Child++ = Init;
3694
3695 // Copy the designators and their subexpressions, computing
3696 // value-dependence along the way.
3697 unsigned IndexIdx = 0;
3698 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003699 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003700
3701 if (this->Designators[I].isArrayDesignator()) {
3702 // Compute type- and value-dependence.
3703 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003704 if (Index->isTypeDependent() || Index->isValueDependent())
3705 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003706 if (Index->isInstantiationDependent())
3707 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003708 // Propagate unexpanded parameter packs.
3709 if (Index->containsUnexpandedParameterPack())
3710 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003711
3712 // Copy the index expressions into permanent storage.
3713 *Child++ = IndexExprs[IndexIdx++];
3714 } else if (this->Designators[I].isArrayRangeDesignator()) {
3715 // Compute type- and value-dependence.
3716 Expr *Start = IndexExprs[IndexIdx];
3717 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003718 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003719 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003720 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003721 ExprBits.InstantiationDependent = true;
3722 } else if (Start->isInstantiationDependent() ||
3723 End->isInstantiationDependent()) {
3724 ExprBits.InstantiationDependent = true;
3725 }
3726
Douglas Gregora6e053e2010-12-15 01:34:56 +00003727 // Propagate unexpanded parameter packs.
3728 if (Start->containsUnexpandedParameterPack() ||
3729 End->containsUnexpandedParameterPack())
3730 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003731
3732 // Copy the start/end expressions into permanent storage.
3733 *Child++ = IndexExprs[IndexIdx++];
3734 *Child++ = IndexExprs[IndexIdx++];
3735 }
3736 }
3737
Benjamin Kramerc215e762012-08-24 11:54:20 +00003738 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003739}
3740
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003741DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003742DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003743 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003744 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003745 SourceLocation ColonOrEqualLoc,
3746 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003747 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003748 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003749 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003750 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003751 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003752}
3753
Craig Topper37932912013-08-18 10:09:15 +00003754DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003755 unsigned NumIndexExprs) {
3756 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3757 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3758 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3759}
3760
Craig Topper37932912013-08-18 10:09:15 +00003761void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003762 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003763 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003764 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003765 NumDesignators = NumDesigs;
3766 for (unsigned I = 0; I != NumDesigs; ++I)
3767 Designators[I] = Desigs[I];
3768}
3769
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003770SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3771 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3772 if (size() == 1)
3773 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003774 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3775 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003776}
3777
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003778SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003779 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003780 Designator &First =
3781 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003782 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003783 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003784 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3785 else
3786 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3787 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003788 StartLoc =
3789 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003790 return StartLoc;
3791}
3792
3793SourceLocation DesignatedInitExpr::getLocEnd() const {
3794 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003795}
3796
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003797Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003798 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003799 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003800 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3801}
3802
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003803Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003804 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003805 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003806 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003807 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3808}
3809
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003810Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003811 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003812 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003813 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003814 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3815}
3816
Douglas Gregord5846a12009-04-15 06:41:24 +00003817/// \brief Replaces the designator at index @p Idx with the series
3818/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003819void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003820 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003821 const Designator *Last) {
3822 unsigned NumNewDesignators = Last - First;
3823 if (NumNewDesignators == 0) {
3824 std::copy_backward(Designators + Idx + 1,
3825 Designators + NumDesignators,
3826 Designators + Idx);
3827 --NumNewDesignators;
3828 return;
3829 } else if (NumNewDesignators == 1) {
3830 Designators[Idx] = *First;
3831 return;
3832 }
3833
Mike Stump11289f42009-09-09 15:08:12 +00003834 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003835 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003836 std::copy(Designators, Designators + Idx, NewDesignators);
3837 std::copy(First, Last, NewDesignators + Idx);
3838 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3839 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003840 Designators = NewDesignators;
3841 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3842}
3843
Craig Topper37932912013-08-18 10:09:15 +00003844ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003845 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003846 SourceLocation rparenloc)
3847 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003848 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003849 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3850 Exprs = new (C) Stmt*[exprs.size()];
3851 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003852 if (exprs[i]->isTypeDependent())
3853 ExprBits.TypeDependent = true;
3854 if (exprs[i]->isValueDependent())
3855 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003856 if (exprs[i]->isInstantiationDependent())
3857 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003858 if (exprs[i]->containsUnexpandedParameterPack())
3859 ExprBits.ContainsUnexpandedParameterPack = true;
3860
Nate Begeman5ec4b312009-08-10 23:49:36 +00003861 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003862 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003863}
3864
John McCall1bf58462011-02-16 08:02:54 +00003865const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3866 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3867 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003868 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3869 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003870 e = cast<CXXConstructExpr>(e)->getArg(0);
3871 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3872 e = ice->getSubExpr();
3873 return cast<OpaqueValueExpr>(e);
3874}
3875
Craig Topper37932912013-08-18 10:09:15 +00003876PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3877 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003878 unsigned numSemanticExprs) {
3879 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3880 (1 + numSemanticExprs) * sizeof(Expr*),
3881 llvm::alignOf<PseudoObjectExpr>());
3882 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3883}
3884
3885PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3886 : Expr(PseudoObjectExprClass, shell) {
3887 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3888}
3889
Craig Topper37932912013-08-18 10:09:15 +00003890PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003891 ArrayRef<Expr*> semantics,
3892 unsigned resultIndex) {
3893 assert(syntax && "no syntactic expression!");
3894 assert(semantics.size() && "no semantic expressions!");
3895
3896 QualType type;
3897 ExprValueKind VK;
3898 if (resultIndex == NoResult) {
3899 type = C.VoidTy;
3900 VK = VK_RValue;
3901 } else {
3902 assert(resultIndex < semantics.size());
3903 type = semantics[resultIndex]->getType();
3904 VK = semantics[resultIndex]->getValueKind();
3905 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3906 }
3907
3908 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3909 (1 + semantics.size()) * sizeof(Expr*),
3910 llvm::alignOf<PseudoObjectExpr>());
3911 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3912 resultIndex);
3913}
3914
3915PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3916 Expr *syntax, ArrayRef<Expr*> semantics,
3917 unsigned resultIndex)
3918 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3919 /*filled in at end of ctor*/ false, false, false, false) {
3920 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3921 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3922
3923 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3924 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3925 getSubExprsBuffer()[i] = E;
3926
3927 if (E->isTypeDependent())
3928 ExprBits.TypeDependent = true;
3929 if (E->isValueDependent())
3930 ExprBits.ValueDependent = true;
3931 if (E->isInstantiationDependent())
3932 ExprBits.InstantiationDependent = true;
3933 if (E->containsUnexpandedParameterPack())
3934 ExprBits.ContainsUnexpandedParameterPack = true;
3935
3936 if (isa<OpaqueValueExpr>(E))
3937 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3938 "opaque-value semantic expressions for pseudo-object "
3939 "operations must have sources");
3940 }
3941}
3942
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003943//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003944// ExprIterator.
3945//===----------------------------------------------------------------------===//
3946
3947Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3948Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3949Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3950const Expr* ConstExprIterator::operator[](size_t idx) const {
3951 return cast<Expr>(I[idx]);
3952}
3953const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3954const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3955
3956//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003957// Child Iterators for iterating over subexpressions/substatements
3958//===----------------------------------------------------------------------===//
3959
Peter Collingbournee190dee2011-03-11 19:24:49 +00003960// UnaryExprOrTypeTraitExpr
3961Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003962 // If this is of a type and the type is a VLA type (and not a typedef), the
3963 // size expression of the VLA needs to be treated as an executable expression.
3964 // Why isn't this weirdness documented better in StmtIterator?
3965 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003966 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003967 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003968 return child_range(child_iterator(T), child_iterator());
3969 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003970 }
John McCallbd066782011-02-09 08:16:59 +00003971 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003972}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003973
Steve Naroffd54978b2007-09-18 23:55:05 +00003974// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003975Stmt::child_range ObjCMessageExpr::children() {
3976 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003977 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003978 begin = reinterpret_cast<Stmt **>(this + 1);
3979 else
3980 begin = reinterpret_cast<Stmt **>(getArgs());
3981 return child_range(begin,
3982 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003983}
3984
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003985ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003986 QualType T, ObjCMethodDecl *Method,
3987 SourceRange SR)
3988 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3989 false, false, false, false),
3990 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3991{
3992 Expr **SaveElements = getElements();
3993 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3994 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3995 ExprBits.ValueDependent = true;
3996 if (Elements[I]->isInstantiationDependent())
3997 ExprBits.InstantiationDependent = true;
3998 if (Elements[I]->containsUnexpandedParameterPack())
3999 ExprBits.ContainsUnexpandedParameterPack = true;
4000
4001 SaveElements[I] = Elements[I];
4002 }
4003}
4004
Craig Topperce7167c2013-08-22 04:58:56 +00004005ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004006 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004007 QualType T, ObjCMethodDecl * Method,
4008 SourceRange SR) {
4009 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4010 + Elements.size() * sizeof(Expr *));
4011 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4012}
4013
Craig Topperce7167c2013-08-22 04:58:56 +00004014ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004015 unsigned NumElements) {
4016
4017 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4018 + NumElements * sizeof(Expr *));
4019 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4020}
4021
4022ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4023 ArrayRef<ObjCDictionaryElement> VK,
4024 bool HasPackExpansions,
4025 QualType T, ObjCMethodDecl *method,
4026 SourceRange SR)
4027 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4028 false, false),
4029 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4030 DictWithObjectsMethod(method)
4031{
4032 KeyValuePair *KeyValues = getKeyValues();
4033 ExpansionData *Expansions = getExpansionData();
4034 for (unsigned I = 0; I < NumElements; I++) {
4035 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4036 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4037 ExprBits.ValueDependent = true;
4038 if (VK[I].Key->isInstantiationDependent() ||
4039 VK[I].Value->isInstantiationDependent())
4040 ExprBits.InstantiationDependent = true;
4041 if (VK[I].EllipsisLoc.isInvalid() &&
4042 (VK[I].Key->containsUnexpandedParameterPack() ||
4043 VK[I].Value->containsUnexpandedParameterPack()))
4044 ExprBits.ContainsUnexpandedParameterPack = true;
4045
4046 KeyValues[I].Key = VK[I].Key;
4047 KeyValues[I].Value = VK[I].Value;
4048 if (Expansions) {
4049 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4050 if (VK[I].NumExpansions)
4051 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4052 else
4053 Expansions[I].NumExpansionsPlusOne = 0;
4054 }
4055 }
4056}
4057
4058ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004059ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004060 ArrayRef<ObjCDictionaryElement> VK,
4061 bool HasPackExpansions,
4062 QualType T, ObjCMethodDecl *method,
4063 SourceRange SR) {
4064 unsigned ExpansionsSize = 0;
4065 if (HasPackExpansions)
4066 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4067
4068 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4069 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4070 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4071}
4072
4073ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004074ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004075 bool HasPackExpansions) {
4076 unsigned ExpansionsSize = 0;
4077 if (HasPackExpansions)
4078 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4079 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4080 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4081 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4082 HasPackExpansions);
4083}
4084
Craig Topperce7167c2013-08-22 04:58:56 +00004085ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004086 Expr *base,
4087 Expr *key, QualType T,
4088 ObjCMethodDecl *getMethod,
4089 ObjCMethodDecl *setMethod,
4090 SourceLocation RB) {
4091 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4092 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4093 OK_ObjCSubscript,
4094 getMethod, setMethod, RB);
4095}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004096
Benjamin Kramerc215e762012-08-24 11:54:20 +00004097AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004098 QualType t, AtomicOp op, SourceLocation RP)
4099 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4100 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004101 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004102{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004103 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4104 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004105 if (args[i]->isTypeDependent())
4106 ExprBits.TypeDependent = true;
4107 if (args[i]->isValueDependent())
4108 ExprBits.ValueDependent = true;
4109 if (args[i]->isInstantiationDependent())
4110 ExprBits.InstantiationDependent = true;
4111 if (args[i]->containsUnexpandedParameterPack())
4112 ExprBits.ContainsUnexpandedParameterPack = true;
4113
4114 SubExprs[i] = args[i];
4115 }
4116}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004117
4118unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4119 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004120 case AO__c11_atomic_init:
4121 case AO__c11_atomic_load:
4122 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004123 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004124
4125 case AO__c11_atomic_store:
4126 case AO__c11_atomic_exchange:
4127 case AO__atomic_load:
4128 case AO__atomic_store:
4129 case AO__atomic_store_n:
4130 case AO__atomic_exchange_n:
4131 case AO__c11_atomic_fetch_add:
4132 case AO__c11_atomic_fetch_sub:
4133 case AO__c11_atomic_fetch_and:
4134 case AO__c11_atomic_fetch_or:
4135 case AO__c11_atomic_fetch_xor:
4136 case AO__atomic_fetch_add:
4137 case AO__atomic_fetch_sub:
4138 case AO__atomic_fetch_and:
4139 case AO__atomic_fetch_or:
4140 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004141 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004142 case AO__atomic_add_fetch:
4143 case AO__atomic_sub_fetch:
4144 case AO__atomic_and_fetch:
4145 case AO__atomic_or_fetch:
4146 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004147 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004148 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004149
4150 case AO__atomic_exchange:
4151 return 4;
4152
4153 case AO__c11_atomic_compare_exchange_strong:
4154 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004155 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004156
4157 case AO__atomic_compare_exchange:
4158 case AO__atomic_compare_exchange_n:
4159 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004160 }
4161 llvm_unreachable("unknown atomic op");
4162}