blob: 0b8948e5fa4d4aa88042a3d41423b76d2bb5d0fa [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: {
2067 // We warn about operator== and operator!= even when user-defined operator
2068 // overloads as there is no reasonable way to define these such that they
2069 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2070 // warning: these operators are commonly typo'ed, and so warning on them
2071 // provides additional value as well. If this list is updated,
2072 // DiagnoseUnusedComparison should be as well.
2073 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2074 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002075 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002076 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002077 Loc = Op->getOperatorLoc();
2078 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002079 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002080 }
Chandler Carruth46339472011-08-17 09:49:44 +00002081
2082 // Fallthrough for generic call handling.
2083 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002084 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002085 case CXXMemberCallExprClass:
2086 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002087 // If this is a direct call, get the callee.
2088 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002089 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002090 // If the callee has attribute pure, const, or warn_unused_result, warn
2091 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002092 //
2093 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2094 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002095 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2096 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002097 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002098 Loc = CE->getCallee()->getLocStart();
2099 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002100
Chris Lattner1a6babf2009-10-13 04:53:48 +00002101 if (unsigned NumArgs = CE->getNumArgs())
2102 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2103 CE->getArg(NumArgs-1)->getLocEnd());
2104 return true;
2105 }
Chris Lattner237f2752009-02-14 07:37:35 +00002106 }
2107 return false;
2108 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002109
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002110 // If we don't know precisely what we're looking at, let's not warn.
2111 case UnresolvedLookupExprClass:
2112 case CXXUnresolvedConstructExprClass:
2113 return false;
2114
Anders Carlsson6aa50392009-11-17 17:11:23 +00002115 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002116 case CXXConstructExprClass: {
2117 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2118 if (Type->hasAttr<WarnUnusedAttr>()) {
2119 WarnE = this;
2120 Loc = getLocStart();
2121 R1 = getSourceRange();
2122 return true;
2123 }
2124 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002125 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002126 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002127
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002128 case ObjCMessageExprClass: {
2129 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002130 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002131 ME->isInstanceMessage() &&
2132 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002133 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002134 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002135 Loc = getExprLoc();
2136 R1 = ME->getSourceRange();
2137 return true;
2138 }
2139
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002140 const ObjCMethodDecl *MD = ME->getMethodDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +00002141 if (MD && MD->hasAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002142 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002143 Loc = getExprLoc();
2144 return true;
2145 }
Chris Lattner237f2752009-02-14 07:37:35 +00002146 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002147 }
Mike Stump11289f42009-09-09 15:08:12 +00002148
John McCallb7bd14f2010-12-02 01:19:52 +00002149 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002150 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002151 Loc = getExprLoc();
2152 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002153 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002154
John McCallfe96e0b2011-11-06 09:01:30 +00002155 case PseudoObjectExprClass: {
2156 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2157
2158 // Only complain about things that have the form of a getter.
2159 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2160 isa<BinaryOperator>(PO->getSyntacticForm()))
2161 return false;
2162
Eli Friedmanc11535c2012-05-24 00:47:05 +00002163 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002164 Loc = getExprLoc();
2165 R1 = getSourceRange();
2166 return true;
2167 }
2168
Chris Lattner944d3062008-07-26 19:51:01 +00002169 case StmtExprClass: {
2170 // Statement exprs don't logically have side effects themselves, but are
2171 // sometimes used in macros in ways that give them a type that is unused.
2172 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2173 // however, if the result of the stmt expr is dead, we don't want to emit a
2174 // warning.
2175 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002176 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002177 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002178 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002179 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2180 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002181 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002182 }
Mike Stump11289f42009-09-09 15:08:12 +00002183
John McCallc493a732010-03-12 07:11:26 +00002184 if (getType()->isVoidType())
2185 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002186 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002187 Loc = cast<StmtExpr>(this)->getLParenLoc();
2188 R1 = getSourceRange();
2189 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002190 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002191 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002192 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002193 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002194 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002195 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002196 if (CE->getCastKind() == CK_ToVoid) {
2197 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002198 CE->getSubExpr()->getType().isVolatileQualified()) {
2199 const DeclRefExpr *DRE =
2200 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2201 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2202 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2203 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2204 R1, R2, Ctx);
2205 }
2206 }
Chris Lattner2706a552009-07-28 18:25:28 +00002207 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002208 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002209
Eli Friedmanc11535c2012-05-24 00:47:05 +00002210 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002211 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002212 if (CE->getCastKind() == CK_ConstructorConversion)
2213 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002214
Eli Friedmanc11535c2012-05-24 00:47:05 +00002215 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002216 if (const CXXFunctionalCastExpr *CXXCE =
2217 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002218 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002219 R1 = CXXCE->getSubExpr()->getSourceRange();
2220 } else {
2221 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2222 Loc = CStyleCE->getLParenLoc();
2223 R1 = CStyleCE->getSubExpr()->getSourceRange();
2224 }
Chris Lattner237f2752009-02-14 07:37:35 +00002225 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002226 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002227 case ImplicitCastExprClass: {
2228 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002229
Eli Friedmanc11535c2012-05-24 00:47:05 +00002230 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2231 if (ICE->getCastKind() == CK_LValueToRValue &&
2232 ICE->getSubExpr()->getType().isVolatileQualified())
2233 return false;
2234
2235 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2236 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002237 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002238 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002239 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002240 case CXXDefaultInitExprClass:
2241 return (cast<CXXDefaultInitExpr>(this)
2242 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002243
2244 case CXXNewExprClass:
2245 // FIXME: In theory, there might be new expressions that don't have side
2246 // effects (e.g. a placement new with an uninitialized POD).
2247 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002248 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002249 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002250 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002251 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002252 case ExprWithCleanupsClass:
2253 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002254 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002255 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002256}
2257
Fariborz Jahanian07735332009-02-22 18:40:18 +00002258/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002259/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002260bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002261 const Expr *E = IgnoreParens();
2262 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002263 default:
2264 return false;
2265 case ObjCIvarRefExprClass:
2266 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002267 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002268 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002269 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002270 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002271 case MaterializeTemporaryExprClass:
2272 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2273 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002274 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002275 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002276 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002277 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002278
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002279 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2280 if (VD->hasGlobalStorage())
2281 return true;
2282 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002283 // dereferencing to a pointer is always a gc'able candidate,
2284 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002285 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002286 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002287 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002288 return false;
2289 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002290 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002291 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002292 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002293 }
2294 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002295 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002296 }
2297}
Sebastian Redlce354af2010-09-10 20:55:33 +00002298
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002299bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2300 if (isTypeDependent())
2301 return false;
John McCall086a4642010-11-24 05:12:34 +00002302 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002303}
2304
John McCall0009fcc2011-04-26 20:42:42 +00002305QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002306 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002307
2308 // Bound member expressions are always one of these possibilities:
2309 // x->m x.m x->*y x.*y
2310 // (possibly parenthesized)
2311
2312 expr = expr->IgnoreParens();
2313 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2314 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2315 return mem->getMemberDecl()->getType();
2316 }
2317
2318 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2319 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2320 ->getPointeeType();
2321 assert(type->isFunctionType());
2322 return type;
2323 }
2324
2325 assert(isa<UnresolvedMemberExpr>(expr));
2326 return QualType();
2327}
2328
Ted Kremenekfff70962008-01-17 16:57:34 +00002329Expr* Expr::IgnoreParens() {
2330 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002331 while (true) {
2332 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2333 E = P->getSubExpr();
2334 continue;
2335 }
2336 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2337 if (P->getOpcode() == UO_Extension) {
2338 E = P->getSubExpr();
2339 continue;
2340 }
2341 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002342 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2343 if (!P->isResultDependent()) {
2344 E = P->getResultExpr();
2345 continue;
2346 }
2347 }
Eli Friedman75807f22013-07-20 00:40:58 +00002348 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2349 if (!P->isConditionDependent()) {
2350 E = P->getChosenSubExpr();
2351 continue;
2352 }
2353 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002354 return E;
2355 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002356}
2357
Chris Lattnerf2660962008-02-13 01:02:39 +00002358/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2359/// or CastExprs or ImplicitCastExprs, returning their operand.
2360Expr *Expr::IgnoreParenCasts() {
2361 Expr *E = this;
2362 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002363 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002364 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002365 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002366 continue;
2367 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002368 if (MaterializeTemporaryExpr *Materialize
2369 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2370 E = Materialize->GetTemporaryExpr();
2371 continue;
2372 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002373 if (SubstNonTypeTemplateParmExpr *NTTP
2374 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2375 E = NTTP->getReplacement();
2376 continue;
2377 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002378 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002379 }
2380}
2381
John McCall5a4ce8b2010-12-04 08:24:19 +00002382/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2383/// casts. This is intended purely as a temporary workaround for code
2384/// that hasn't yet been rewritten to do the right thing about those
2385/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002386Expr *Expr::IgnoreParenLValueCasts() {
2387 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002388 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002389 E = E->IgnoreParens();
2390 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002391 if (P->getCastKind() == CK_LValueToRValue) {
2392 E = P->getSubExpr();
2393 continue;
2394 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002395 } else if (MaterializeTemporaryExpr *Materialize
2396 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2397 E = Materialize->GetTemporaryExpr();
2398 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002399 } else if (SubstNonTypeTemplateParmExpr *NTTP
2400 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2401 E = NTTP->getReplacement();
2402 continue;
John McCall34376a62010-12-04 03:47:34 +00002403 }
2404 break;
2405 }
2406 return E;
2407}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002408
2409Expr *Expr::ignoreParenBaseCasts() {
2410 Expr *E = this;
2411 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002412 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002413 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2414 if (CE->getCastKind() == CK_DerivedToBase ||
2415 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2416 CE->getCastKind() == CK_NoOp) {
2417 E = CE->getSubExpr();
2418 continue;
2419 }
2420 }
2421
2422 return E;
2423 }
2424}
2425
John McCalleebc8322010-05-05 22:59:52 +00002426Expr *Expr::IgnoreParenImpCasts() {
2427 Expr *E = this;
2428 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002429 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002430 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002431 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002432 continue;
2433 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002434 if (MaterializeTemporaryExpr *Materialize
2435 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2436 E = Materialize->GetTemporaryExpr();
2437 continue;
2438 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002439 if (SubstNonTypeTemplateParmExpr *NTTP
2440 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2441 E = NTTP->getReplacement();
2442 continue;
2443 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002444 return E;
John McCalleebc8322010-05-05 22:59:52 +00002445 }
2446}
2447
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002448Expr *Expr::IgnoreConversionOperator() {
2449 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002450 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002451 return MCE->getImplicitObjectArgument();
2452 }
2453 return this;
2454}
2455
Chris Lattneref26c772009-03-13 17:28:01 +00002456/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2457/// value (including ptr->int casts of the same size). Strip off any
2458/// ParenExpr or CastExprs, returning their operand.
2459Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2460 Expr *E = this;
2461 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002462 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002463
Chris Lattneref26c772009-03-13 17:28:01 +00002464 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2465 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002466 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002467 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002468
Chris Lattneref26c772009-03-13 17:28:01 +00002469 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2470 E = SE;
2471 continue;
2472 }
Mike Stump11289f42009-09-09 15:08:12 +00002473
Abramo Bagnara932e3932010-10-15 07:51:18 +00002474 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002475 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002476 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002477 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002478 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2479 E = SE;
2480 continue;
2481 }
2482 }
Mike Stump11289f42009-09-09 15:08:12 +00002483
Douglas Gregor6a40b082011-09-08 17:56:33 +00002484 if (SubstNonTypeTemplateParmExpr *NTTP
2485 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2486 E = NTTP->getReplacement();
2487 continue;
2488 }
2489
Chris Lattneref26c772009-03-13 17:28:01 +00002490 return E;
2491 }
2492}
2493
Douglas Gregord196a582009-12-14 19:27:10 +00002494bool Expr::isDefaultArgument() const {
2495 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002496 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2497 E = M->GetTemporaryExpr();
2498
Douglas Gregord196a582009-12-14 19:27:10 +00002499 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2500 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002501
Douglas Gregord196a582009-12-14 19:27:10 +00002502 return isa<CXXDefaultArgExpr>(E);
2503}
Chris Lattneref26c772009-03-13 17:28:01 +00002504
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002505/// \brief Skip over any no-op casts and any temporary-binding
2506/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002507static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002508 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2509 E = M->GetTemporaryExpr();
2510
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002511 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002512 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002513 E = ICE->getSubExpr();
2514 else
2515 break;
2516 }
2517
2518 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2519 E = BE->getSubExpr();
2520
2521 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002522 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002523 E = ICE->getSubExpr();
2524 else
2525 break;
2526 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002527
2528 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002529}
2530
John McCall7a626f62010-09-15 10:14:12 +00002531/// isTemporaryObject - Determines if this expression produces a
2532/// temporary of the given class type.
2533bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2534 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2535 return false;
2536
Anders Carlsson66bbf502010-11-28 16:40:49 +00002537 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002538
John McCall02dc8c72010-09-15 20:59:13 +00002539 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002540 if (!E->Classify(C).isPRValue()) {
2541 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002542 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002543 return false;
2544 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002545
John McCallf4ee1dd2010-09-16 06:57:56 +00002546 // Black-list a few cases which yield pr-values of class type that don't
2547 // refer to temporaries of that type:
2548
2549 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002550 if (isa<ImplicitCastExpr>(E)) {
2551 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2552 case CK_DerivedToBase:
2553 case CK_UncheckedDerivedToBase:
2554 return false;
2555 default:
2556 break;
2557 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002558 }
2559
John McCallf4ee1dd2010-09-16 06:57:56 +00002560 // - member expressions (all)
2561 if (isa<MemberExpr>(E))
2562 return false;
2563
Eli Friedman13ffdd82012-06-15 23:51:06 +00002564 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2565 if (BO->isPtrMemOp())
2566 return false;
2567
John McCallc07a0c72011-02-17 10:25:35 +00002568 // - opaque values (all)
2569 if (isa<OpaqueValueExpr>(E))
2570 return false;
2571
John McCall7a626f62010-09-15 10:14:12 +00002572 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002573}
2574
Douglas Gregor25b7e052011-03-02 21:06:53 +00002575bool Expr::isImplicitCXXThis() const {
2576 const Expr *E = this;
2577
2578 // Strip away parentheses and casts we don't care about.
2579 while (true) {
2580 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2581 E = Paren->getSubExpr();
2582 continue;
2583 }
2584
2585 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2586 if (ICE->getCastKind() == CK_NoOp ||
2587 ICE->getCastKind() == CK_LValueToRValue ||
2588 ICE->getCastKind() == CK_DerivedToBase ||
2589 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2590 E = ICE->getSubExpr();
2591 continue;
2592 }
2593 }
2594
2595 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2596 if (UnOp->getOpcode() == UO_Extension) {
2597 E = UnOp->getSubExpr();
2598 continue;
2599 }
2600 }
2601
Douglas Gregorfe314812011-06-21 17:03:29 +00002602 if (const MaterializeTemporaryExpr *M
2603 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2604 E = M->GetTemporaryExpr();
2605 continue;
2606 }
2607
Douglas Gregor25b7e052011-03-02 21:06:53 +00002608 break;
2609 }
2610
2611 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2612 return This->isImplicit();
2613
2614 return false;
2615}
2616
Douglas Gregor4619e432008-12-05 23:32:09 +00002617/// hasAnyTypeDependentArguments - Determines if any of the expressions
2618/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002619bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002620 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002621 if (Exprs[I]->isTypeDependent())
2622 return true;
2623
2624 return false;
2625}
2626
John McCall8b0f4ff2010-08-02 21:13:48 +00002627bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002628 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002629 // which can be evaluated at compile-time. It very closely parallels
2630 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2631 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2632 // to isEvaluatable most of the time.
2633 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002634 // If we ever capture reference-binding directly in the AST, we can
2635 // kill the second parameter.
2636
2637 if (IsForRef) {
2638 EvalResult Result;
2639 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2640 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002641
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002642 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002643 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002644 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002645 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002646 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002647 case CXXTemporaryObjectExprClass:
2648 case CXXConstructExprClass: {
2649 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002650
Eli Friedman4c27ac22013-07-16 22:40:53 +00002651 if (CE->getConstructor()->isTrivial() &&
2652 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2653 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002654 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002655
Eli Friedman4c27ac22013-07-16 22:40:53 +00002656 // Trivial copy constructor
2657 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2658 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002659 }
2660
Richard Smithd62306a2011-11-10 06:34:14 +00002661 break;
John McCall81c9cea2010-08-01 21:51:45 +00002662 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002663 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002664 // This handles gcc's extension that allows global initializers like
2665 // "struct x {int x;} x = (struct x) {};".
2666 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002667 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002668 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002669 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002670 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002671 const InitListExpr *ILE = cast<InitListExpr>(this);
2672 if (ILE->getType()->isArrayType()) {
2673 unsigned numInits = ILE->getNumInits();
2674 for (unsigned i = 0; i < numInits; i++) {
2675 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2676 return false;
2677 }
2678 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002679 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002680
2681 if (ILE->getType()->isRecordType()) {
2682 unsigned ElementNo = 0;
2683 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2684 for (RecordDecl::field_iterator Field = RD->field_begin(),
2685 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2686 // If this is a union, skip all the fields that aren't being initialized.
2687 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2688 continue;
2689
2690 // Don't emit anonymous bitfields, they just affect layout.
2691 if (Field->isUnnamedBitfield())
2692 continue;
2693
2694 if (ElementNo < ILE->getNumInits()) {
2695 const Expr *Elt = ILE->getInit(ElementNo++);
2696 if (Field->isBitField()) {
2697 // Bitfields have to evaluate to an integer.
2698 llvm::APSInt ResultTmp;
2699 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2700 return false;
2701 } else {
2702 bool RefType = Field->getType()->isReferenceType();
2703 if (!Elt->isConstantInitializer(Ctx, RefType))
2704 return false;
2705 }
2706 }
2707 }
2708 return true;
2709 }
2710
2711 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002712 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002713 case ImplicitValueInitExprClass:
2714 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002715 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002716 return cast<ParenExpr>(this)->getSubExpr()
2717 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002718 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002719 return cast<GenericSelectionExpr>(this)->getResultExpr()
2720 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002721 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002722 if (cast<ChooseExpr>(this)->isConditionDependent())
2723 return false;
2724 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002725 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002726 case UnaryOperatorClass: {
2727 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002728 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002729 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002730 break;
2731 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002732 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002733 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002734 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002735 case CStyleCastExprClass:
2736 case ObjCBridgedCastExprClass:
2737 case CXXDynamicCastExprClass:
2738 case CXXReinterpretCastExprClass:
2739 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002740 const CastExpr *CE = cast<CastExpr>(this);
2741
Eli Friedman13ec75b2011-12-21 00:43:02 +00002742 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002743 if (CE->getCastKind() == CK_NoOp ||
2744 CE->getCastKind() == CK_LValueToRValue ||
2745 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002746 CE->getCastKind() == CK_ConstructorConversion ||
2747 CE->getCastKind() == CK_NonAtomicToAtomic ||
2748 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002749 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2750
Eli Friedman384da272009-01-25 03:12:18 +00002751 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002752 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002753 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002754 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002755 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002756
2757 case SubstNonTypeTemplateParmExprClass:
2758 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2759 ->isConstantInitializer(Ctx, false);
2760 case CXXDefaultArgExprClass:
2761 return cast<CXXDefaultArgExpr>(this)->getExpr()
2762 ->isConstantInitializer(Ctx, false);
2763 case CXXDefaultInitExprClass:
2764 return cast<CXXDefaultInitExpr>(this)->getExpr()
2765 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002766 }
Eli Friedman384da272009-01-25 03:12:18 +00002767 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002768}
2769
Richard Smith0421ce72012-08-07 04:16:51 +00002770bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2771 if (isInstantiationDependent())
2772 return true;
2773
2774 switch (getStmtClass()) {
2775 case NoStmtClass:
2776 #define ABSTRACT_STMT(Type)
2777 #define STMT(Type, Base) case Type##Class:
2778 #define EXPR(Type, Base)
2779 #include "clang/AST/StmtNodes.inc"
2780 llvm_unreachable("unexpected Expr kind");
2781
2782 case DependentScopeDeclRefExprClass:
2783 case CXXUnresolvedConstructExprClass:
2784 case CXXDependentScopeMemberExprClass:
2785 case UnresolvedLookupExprClass:
2786 case UnresolvedMemberExprClass:
2787 case PackExpansionExprClass:
2788 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002789 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002790 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2791
Richard Smitha33e4fe2012-08-07 05:18:29 +00002792 case DeclRefExprClass:
2793 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002794 case PredefinedExprClass:
2795 case IntegerLiteralClass:
2796 case FloatingLiteralClass:
2797 case ImaginaryLiteralClass:
2798 case StringLiteralClass:
2799 case CharacterLiteralClass:
2800 case OffsetOfExprClass:
2801 case ImplicitValueInitExprClass:
2802 case UnaryExprOrTypeTraitExprClass:
2803 case AddrLabelExprClass:
2804 case GNUNullExprClass:
2805 case CXXBoolLiteralExprClass:
2806 case CXXNullPtrLiteralExprClass:
2807 case CXXThisExprClass:
2808 case CXXScalarValueInitExprClass:
2809 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002810 case ArrayTypeTraitExprClass:
2811 case ExpressionTraitExprClass:
2812 case CXXNoexceptExprClass:
2813 case SizeOfPackExprClass:
2814 case ObjCStringLiteralClass:
2815 case ObjCEncodeExprClass:
2816 case ObjCBoolLiteralExprClass:
2817 case CXXUuidofExprClass:
2818 case OpaqueValueExprClass:
2819 // These never have a side-effect.
2820 return false;
2821
2822 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002823 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002824 case CompoundAssignOperatorClass:
2825 case VAArgExprClass:
2826 case AtomicExprClass:
2827 case StmtExprClass:
2828 case CXXOperatorCallExprClass:
2829 case CXXMemberCallExprClass:
2830 case UserDefinedLiteralClass:
2831 case CXXThrowExprClass:
2832 case CXXNewExprClass:
2833 case CXXDeleteExprClass:
2834 case ExprWithCleanupsClass:
2835 case CXXBindTemporaryExprClass:
2836 case BlockExprClass:
2837 case CUDAKernelCallExprClass:
2838 // These always have a side-effect.
2839 return true;
2840
2841 case ParenExprClass:
2842 case ArraySubscriptExprClass:
2843 case MemberExprClass:
2844 case ConditionalOperatorClass:
2845 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002846 case CompoundLiteralExprClass:
2847 case ExtVectorElementExprClass:
2848 case DesignatedInitExprClass:
2849 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002850 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002851 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002852 case SubstNonTypeTemplateParmExprClass:
2853 case MaterializeTemporaryExprClass:
2854 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002855 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002856 case AsTypeExprClass:
2857 // These have a side-effect if any subexpression does.
2858 break;
2859
Richard Smitha33e4fe2012-08-07 05:18:29 +00002860 case UnaryOperatorClass:
2861 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002862 return true;
2863 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002864
2865 case BinaryOperatorClass:
2866 if (cast<BinaryOperator>(this)->isAssignmentOp())
2867 return true;
2868 break;
2869
Richard Smith0421ce72012-08-07 04:16:51 +00002870 case InitListExprClass:
2871 // FIXME: The children for an InitListExpr doesn't include the array filler.
2872 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2873 if (E->HasSideEffects(Ctx))
2874 return true;
2875 break;
2876
2877 case GenericSelectionExprClass:
2878 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2879 HasSideEffects(Ctx);
2880
2881 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002882 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002883
2884 case CXXDefaultArgExprClass:
2885 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2886
Richard Smith852c9db2013-04-20 22:23:05 +00002887 case CXXDefaultInitExprClass:
2888 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2889 return E->HasSideEffects(Ctx);
2890 // If we've not yet parsed the initializer, assume it has side-effects.
2891 return true;
2892
Richard Smith0421ce72012-08-07 04:16:51 +00002893 case CXXDynamicCastExprClass: {
2894 // A dynamic_cast expression has side-effects if it can throw.
2895 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2896 if (DCE->getTypeAsWritten()->isReferenceType() &&
2897 DCE->getCastKind() == CK_Dynamic)
2898 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002899 } // Fall through.
2900 case ImplicitCastExprClass:
2901 case CStyleCastExprClass:
2902 case CXXStaticCastExprClass:
2903 case CXXReinterpretCastExprClass:
2904 case CXXConstCastExprClass:
2905 case CXXFunctionalCastExprClass: {
2906 const CastExpr *CE = cast<CastExpr>(this);
2907 if (CE->getCastKind() == CK_LValueToRValue &&
2908 CE->getSubExpr()->getType().isVolatileQualified())
2909 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002910 break;
2911 }
2912
Richard Smithef8bf432012-08-13 20:08:14 +00002913 case CXXTypeidExprClass:
2914 // typeid might throw if its subexpression is potentially-evaluated, so has
2915 // side-effects in that case whether or not its subexpression does.
2916 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002917
2918 case CXXConstructExprClass:
2919 case CXXTemporaryObjectExprClass: {
2920 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002921 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002922 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002923 // A trivial constructor does not add any side-effects of its own. Just look
2924 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002925 break;
2926 }
2927
2928 case LambdaExprClass: {
2929 const LambdaExpr *LE = cast<LambdaExpr>(this);
2930 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2931 E = LE->capture_end(); I != E; ++I)
2932 if (I->getCaptureKind() == LCK_ByCopy)
2933 // FIXME: Only has a side-effect if the variable is volatile or if
2934 // the copy would invoke a non-trivial copy constructor.
2935 return true;
2936 return false;
2937 }
2938
2939 case PseudoObjectExprClass: {
2940 // Only look for side-effects in the semantic form, and look past
2941 // OpaqueValueExpr bindings in that form.
2942 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2943 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2944 E = PO->semantics_end();
2945 I != E; ++I) {
2946 const Expr *Subexpr = *I;
2947 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2948 Subexpr = OVE->getSourceExpr();
2949 if (Subexpr->HasSideEffects(Ctx))
2950 return true;
2951 }
2952 return false;
2953 }
2954
2955 case ObjCBoxedExprClass:
2956 case ObjCArrayLiteralClass:
2957 case ObjCDictionaryLiteralClass:
2958 case ObjCMessageExprClass:
2959 case ObjCSelectorExprClass:
2960 case ObjCProtocolExprClass:
2961 case ObjCPropertyRefExprClass:
2962 case ObjCIsaExprClass:
2963 case ObjCIndirectCopyRestoreExprClass:
2964 case ObjCSubscriptRefExprClass:
2965 case ObjCBridgedCastExprClass:
2966 // FIXME: Classify these cases better.
2967 return true;
2968 }
2969
2970 // Recurse to children.
2971 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2972 if (const Stmt *S = *SubStmts)
2973 if (cast<Expr>(S)->HasSideEffects(Ctx))
2974 return true;
2975
2976 return false;
2977}
2978
Douglas Gregor1be329d2012-02-23 07:33:15 +00002979namespace {
2980 /// \brief Look for a call to a non-trivial function within an expression.
2981 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2982 {
2983 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2984
2985 bool NonTrivial;
2986
2987 public:
2988 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002989 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002990
2991 bool hasNonTrivialCall() const { return NonTrivial; }
2992
2993 void VisitCallExpr(CallExpr *E) {
2994 if (CXXMethodDecl *Method
2995 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2996 if (Method->isTrivial()) {
2997 // Recurse to children of the call.
2998 Inherited::VisitStmt(E);
2999 return;
3000 }
3001 }
3002
3003 NonTrivial = true;
3004 }
3005
3006 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3007 if (E->getConstructor()->isTrivial()) {
3008 // Recurse to children of the call.
3009 Inherited::VisitStmt(E);
3010 return;
3011 }
3012
3013 NonTrivial = true;
3014 }
3015
3016 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3017 if (E->getTemporary()->getDestructor()->isTrivial()) {
3018 Inherited::VisitStmt(E);
3019 return;
3020 }
3021
3022 NonTrivial = true;
3023 }
3024 };
3025}
3026
3027bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3028 NonTrivialCallFinder Finder(Ctx);
3029 Finder.Visit(this);
3030 return Finder.hasNonTrivialCall();
3031}
3032
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003033/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3034/// pointer constant or not, as well as the specific kind of constant detected.
3035/// Null pointer constants can be integer constant expressions with the
3036/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3037/// (a GNU extension).
3038Expr::NullPointerConstantKind
3039Expr::isNullPointerConstant(ASTContext &Ctx,
3040 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003041 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003042 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003043 switch (NPC) {
3044 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003045 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003046 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003047 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003048 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003049 else
3050 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003051
Douglas Gregor56751b52009-09-25 04:25:58 +00003052 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003053 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003054 }
3055 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003056
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003057 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003058 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003059 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003060 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003061 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003062 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003063 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003064 Pointee->isVoidType() && // to void*
3065 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003066 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003067 }
Steve Naroffada7d422007-05-20 17:54:12 +00003068 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003069 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3070 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003071 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003072 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3073 // Accept ((void*)0) as a null pointer constant, as many other
3074 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003075 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003076 } else if (const GenericSelectionExpr *GE =
3077 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003078 if (GE->isResultDependent())
3079 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003080 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003081 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3082 if (CE->isConditionDependent())
3083 return NPCK_NotNull;
3084 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003085 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003086 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003087 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003088 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003089 } else if (const CXXDefaultInitExpr *DefaultInit
3090 = dyn_cast<CXXDefaultInitExpr>(this)) {
3091 // See through default initializer expressions.
3092 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003093 } else if (isa<GNUNullExpr>(this)) {
3094 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003095 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003096 } else if (const MaterializeTemporaryExpr *M
3097 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3098 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003099 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3100 if (const Expr *Source = OVE->getSourceExpr())
3101 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003102 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003103
Richard Smith89645bc2013-01-02 12:01:23 +00003104 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003105 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003106 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003107
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003108 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003109 if (!Ctx.getLangOpts().CPlusPlus11 &&
3110 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003111 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3112 const Expr *InitExpr = CLE->getInitializer();
3113 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3114 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3115 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003116 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003117 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003118 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003119 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003120
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003121 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003122 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3123 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003124 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003125 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003126 if (Lit && !Lit->getValue())
3127 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003128 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003129 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003130 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003131 // If we have an integer constant expression, we need to *evaluate* it and
3132 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003133 if (!isIntegerConstantExpr(Ctx))
3134 return NPCK_NotNull;
3135 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003136
David Blaikie1c7c8f72012-08-08 17:33:31 +00003137 if (EvaluateKnownConstInt(Ctx) != 0)
3138 return NPCK_NotNull;
3139
3140 if (isa<IntegerLiteral>(this))
3141 return NPCK_ZeroLiteral;
3142 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003143}
Steve Narofff7a5da12007-07-28 23:10:27 +00003144
John McCall34376a62010-12-04 03:47:34 +00003145/// \brief If this expression is an l-value for an Objective C
3146/// property, find the underlying property reference expression.
3147const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3148 const Expr *E = this;
3149 while (true) {
3150 assert((E->getValueKind() == VK_LValue &&
3151 E->getObjectKind() == OK_ObjCProperty) &&
3152 "expression is not a property reference");
3153 E = E->IgnoreParenCasts();
3154 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3155 if (BO->getOpcode() == BO_Comma) {
3156 E = BO->getRHS();
3157 continue;
3158 }
3159 }
3160
3161 break;
3162 }
3163
3164 return cast<ObjCPropertyRefExpr>(E);
3165}
3166
Anna Zaks97c7ce32012-10-01 20:34:04 +00003167bool Expr::isObjCSelfExpr() const {
3168 const Expr *E = IgnoreParenImpCasts();
3169
3170 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3171 if (!DRE)
3172 return false;
3173
3174 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3175 if (!Param)
3176 return false;
3177
3178 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3179 if (!M)
3180 return false;
3181
3182 return M->getSelfDecl() == Param;
3183}
3184
John McCalld25db7e2013-05-06 21:39:12 +00003185FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003186 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003187
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003188 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003189 if (ICE->getCastKind() == CK_LValueToRValue ||
3190 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003191 E = ICE->getSubExpr()->IgnoreParens();
3192 else
3193 break;
3194 }
3195
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003196 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003197 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003198 if (Field->isBitField())
3199 return Field;
3200
John McCalld25db7e2013-05-06 21:39:12 +00003201 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3202 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3203 if (Ivar->isBitField())
3204 return Ivar;
3205
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003206 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3207 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3208 if (Field->isBitField())
3209 return Field;
3210
Eli Friedman609ada22011-07-13 02:05:57 +00003211 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003212 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003213 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003214
Eli Friedman609ada22011-07-13 02:05:57 +00003215 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003216 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003217 }
3218
Douglas Gregor71235ec2009-05-02 02:18:30 +00003219 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003220}
3221
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003222bool Expr::refersToVectorElement() const {
3223 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003224
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003225 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003226 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003227 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003228 E = ICE->getSubExpr()->IgnoreParens();
3229 else
3230 break;
3231 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003232
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003233 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3234 return ASE->getBase()->getType()->isVectorType();
3235
3236 if (isa<ExtVectorElementExpr>(E))
3237 return true;
3238
3239 return false;
3240}
3241
Chris Lattnerb8211f62009-02-16 22:14:05 +00003242/// isArrow - Return true if the base expression is a pointer to vector,
3243/// return false if the base expression is a vector.
3244bool ExtVectorElementExpr::isArrow() const {
3245 return getBase()->getType()->isPointerType();
3246}
3247
Nate Begemance4d7fc2008-04-18 23:10:10 +00003248unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003249 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003250 return VT->getNumElements();
3251 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003252}
3253
Nate Begemanf322eab2008-05-09 06:41:27 +00003254/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003255bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003256 // FIXME: Refactor this code to an accessor on the AST node which returns the
3257 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003258 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003259
3260 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003261 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003262 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003263
Nate Begeman7e5185b2009-01-18 02:01:21 +00003264 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003265 if (Comp[0] == 's' || Comp[0] == 'S')
3266 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003267
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003268 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003269 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003270 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003271
Steve Naroff0d595ca2007-07-30 03:29:09 +00003272 return false;
3273}
Chris Lattner885b4952007-08-02 23:36:59 +00003274
Nate Begemanf322eab2008-05-09 06:41:27 +00003275/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003276void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003277 SmallVectorImpl<unsigned> &Elts) const {
3278 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003279 if (Comp[0] == 's' || Comp[0] == 'S')
3280 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003281
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003282 bool isHi = Comp == "hi";
3283 bool isLo = Comp == "lo";
3284 bool isEven = Comp == "even";
3285 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003286
Nate Begemanf322eab2008-05-09 06:41:27 +00003287 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3288 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003289
Nate Begemanf322eab2008-05-09 06:41:27 +00003290 if (isHi)
3291 Index = e + i;
3292 else if (isLo)
3293 Index = i;
3294 else if (isEven)
3295 Index = 2 * i;
3296 else if (isOdd)
3297 Index = 2 * i + 1;
3298 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003299 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003300
Nate Begemand3862152008-05-13 21:03:02 +00003301 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003302 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003303}
3304
Douglas Gregor9a129192010-04-21 00:45:42 +00003305ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003306 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003307 SourceLocation LBracLoc,
3308 SourceLocation SuperLoc,
3309 bool IsInstanceSuper,
3310 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003311 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003312 ArrayRef<SourceLocation> SelLocs,
3313 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003314 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003315 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003316 SourceLocation RBracLoc,
3317 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003318 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003319 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003320 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003321 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003322 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3323 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003324 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003325 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3326 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003327{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003328 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003329 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003330}
3331
Douglas Gregor9a129192010-04-21 00:45:42 +00003332ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003333 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003334 SourceLocation LBracLoc,
3335 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003336 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003337 ArrayRef<SourceLocation> SelLocs,
3338 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003339 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003340 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003341 SourceLocation RBracLoc,
3342 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003343 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003344 T->isDependentType(), T->isInstantiationDependentType(),
3345 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003346 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3347 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003348 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003349 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003350 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003351{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003352 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003353 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003354}
3355
Douglas Gregor9a129192010-04-21 00:45:42 +00003356ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003357 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003358 SourceLocation LBracLoc,
3359 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003360 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003361 ArrayRef<SourceLocation> SelLocs,
3362 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003363 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003364 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003365 SourceLocation RBracLoc,
3366 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003367 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003368 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003369 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003370 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003371 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3372 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003373 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003374 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003375 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003376{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003377 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003378 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003379}
3380
3381void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3382 ArrayRef<SourceLocation> SelLocs,
3383 SelectorLocationsKind SelLocsK) {
3384 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003385 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003386 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003387 if (Args[I]->isTypeDependent())
3388 ExprBits.TypeDependent = true;
3389 if (Args[I]->isValueDependent())
3390 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003391 if (Args[I]->isInstantiationDependent())
3392 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003393 if (Args[I]->containsUnexpandedParameterPack())
3394 ExprBits.ContainsUnexpandedParameterPack = true;
3395
3396 MyArgs[I] = Args[I];
3397 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003398
Benjamin Kramer2325b242012-02-20 00:20:48 +00003399 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003400 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003401 if (SelLocsK == SelLoc_NonStandard)
3402 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3403 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003404}
3405
Craig Topperce7167c2013-08-22 04:58:56 +00003406ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003407 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003408 SourceLocation LBracLoc,
3409 SourceLocation SuperLoc,
3410 bool IsInstanceSuper,
3411 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003412 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003413 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003414 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003415 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003416 SourceLocation RBracLoc,
3417 bool isImplicit) {
3418 assert((!SelLocs.empty() || isImplicit) &&
3419 "No selector locs for non-implicit message");
3420 ObjCMessageExpr *Mem;
3421 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3422 if (isImplicit)
3423 Mem = alloc(Context, Args.size(), 0);
3424 else
3425 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003426 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003427 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003428 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003429}
3430
Craig Topperce7167c2013-08-22 04:58:56 +00003431ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003432 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003433 SourceLocation LBracLoc,
3434 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003435 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003436 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003437 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003438 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003439 SourceLocation RBracLoc,
3440 bool isImplicit) {
3441 assert((!SelLocs.empty() || isImplicit) &&
3442 "No selector locs for non-implicit message");
3443 ObjCMessageExpr *Mem;
3444 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3445 if (isImplicit)
3446 Mem = alloc(Context, Args.size(), 0);
3447 else
3448 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003449 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003450 SelLocs, SelLocsK, Method, Args, RBracLoc,
3451 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003452}
3453
Craig Topperce7167c2013-08-22 04:58:56 +00003454ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003455 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003456 SourceLocation LBracLoc,
3457 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003458 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003459 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003460 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003461 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003462 SourceLocation RBracLoc,
3463 bool isImplicit) {
3464 assert((!SelLocs.empty() || isImplicit) &&
3465 "No selector locs for non-implicit message");
3466 ObjCMessageExpr *Mem;
3467 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3468 if (isImplicit)
3469 Mem = alloc(Context, Args.size(), 0);
3470 else
3471 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003472 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003473 SelLocs, SelLocsK, Method, Args, RBracLoc,
3474 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003475}
3476
Craig Topperce7167c2013-08-22 04:58:56 +00003477ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003478 unsigned NumArgs,
3479 unsigned NumStoredSelLocs) {
3480 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003481 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3482}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003483
Craig Topperce7167c2013-08-22 04:58:56 +00003484ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003485 ArrayRef<Expr *> Args,
3486 SourceLocation RBraceLoc,
3487 ArrayRef<SourceLocation> SelLocs,
3488 Selector Sel,
3489 SelectorLocationsKind &SelLocsK) {
3490 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3491 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3492 : 0;
3493 return alloc(C, Args.size(), NumStoredSelLocs);
3494}
3495
Craig Topperce7167c2013-08-22 04:58:56 +00003496ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003497 unsigned NumArgs,
3498 unsigned NumStoredSelLocs) {
3499 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3500 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3501 return (ObjCMessageExpr *)C.Allocate(Size,
3502 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3503}
3504
3505void ObjCMessageExpr::getSelectorLocs(
3506 SmallVectorImpl<SourceLocation> &SelLocs) const {
3507 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3508 SelLocs.push_back(getSelectorLoc(i));
3509}
3510
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003511SourceRange ObjCMessageExpr::getReceiverRange() const {
3512 switch (getReceiverKind()) {
3513 case Instance:
3514 return getInstanceReceiver()->getSourceRange();
3515
3516 case Class:
3517 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3518
3519 case SuperInstance:
3520 case SuperClass:
3521 return getSuperLoc();
3522 }
3523
David Blaikiee4d798f2012-01-20 21:50:17 +00003524 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003525}
3526
Douglas Gregor9a129192010-04-21 00:45:42 +00003527Selector ObjCMessageExpr::getSelector() const {
3528 if (HasMethod)
3529 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3530 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003531 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003532}
3533
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003534QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003535 switch (getReceiverKind()) {
3536 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003537 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003538 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003539 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003540 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003541 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003542 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003543 }
3544
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003545 llvm_unreachable("unexpected receiver kind");
3546}
3547
3548ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3549 QualType T = getReceiverType();
3550
3551 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3552 return Ptr->getInterfaceDecl();
3553
3554 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3555 return Ty->getInterface();
3556
Douglas Gregor9a129192010-04-21 00:45:42 +00003557 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003558}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003559
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003560StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003561 switch (getBridgeKind()) {
3562 case OBC_Bridge:
3563 return "__bridge";
3564 case OBC_BridgeTransfer:
3565 return "__bridge_transfer";
3566 case OBC_BridgeRetained:
3567 return "__bridge_retained";
3568 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003569
3570 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003571}
3572
Craig Topper37932912013-08-18 10:09:15 +00003573ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003574 QualType Type, SourceLocation BLoc,
3575 SourceLocation RP)
3576 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3577 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003578 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003579 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003580 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003581{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003582 SubExprs = new (C) Stmt*[args.size()];
3583 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003584 if (args[i]->isTypeDependent())
3585 ExprBits.TypeDependent = true;
3586 if (args[i]->isValueDependent())
3587 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003588 if (args[i]->isInstantiationDependent())
3589 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003590 if (args[i]->containsUnexpandedParameterPack())
3591 ExprBits.ContainsUnexpandedParameterPack = true;
3592
3593 SubExprs[i] = args[i];
3594 }
3595}
3596
Craig Topper37932912013-08-18 10:09:15 +00003597void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003598 if (SubExprs) C.Deallocate(SubExprs);
3599
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003600 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003601 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003602 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003603}
Nate Begeman48745922009-08-12 02:28:50 +00003604
Craig Topper37932912013-08-18 10:09:15 +00003605GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003606 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003607 ArrayRef<TypeSourceInfo*> AssocTypes,
3608 ArrayRef<Expr*> AssocExprs,
3609 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003610 SourceLocation RParenLoc,
3611 bool ContainsUnexpandedParameterPack,
3612 unsigned ResultIndex)
3613 : Expr(GenericSelectionExprClass,
3614 AssocExprs[ResultIndex]->getType(),
3615 AssocExprs[ResultIndex]->getValueKind(),
3616 AssocExprs[ResultIndex]->getObjectKind(),
3617 AssocExprs[ResultIndex]->isTypeDependent(),
3618 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003619 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003620 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003621 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3622 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3623 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3624 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003625 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003626 assert(AssocTypes.size() == AssocExprs.size());
3627 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3628 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003629}
3630
Craig Topper37932912013-08-18 10:09:15 +00003631GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003632 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003633 ArrayRef<TypeSourceInfo*> AssocTypes,
3634 ArrayRef<Expr*> AssocExprs,
3635 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003636 SourceLocation RParenLoc,
3637 bool ContainsUnexpandedParameterPack)
3638 : Expr(GenericSelectionExprClass,
3639 Context.DependentTy,
3640 VK_RValue,
3641 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003642 /*isTypeDependent=*/true,
3643 /*isValueDependent=*/true,
3644 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003645 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003646 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3647 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3648 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3649 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003650 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003651 assert(AssocTypes.size() == AssocExprs.size());
3652 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3653 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003654}
3655
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003656//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003657// DesignatedInitExpr
3658//===----------------------------------------------------------------------===//
3659
Chandler Carruth631abd92011-06-16 06:47:06 +00003660IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003661 assert(Kind == FieldDesignator && "Only valid on a field designator");
3662 if (Field.NameOrField & 0x01)
3663 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3664 else
3665 return getField()->getIdentifier();
3666}
3667
Craig Topper37932912013-08-18 10:09:15 +00003668DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003669 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003670 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003671 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003672 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003673 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003674 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003675 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003676 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003677 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003678 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003679 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003680 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003681 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003682 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003683
3684 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003685 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003686 *Child++ = Init;
3687
3688 // Copy the designators and their subexpressions, computing
3689 // value-dependence along the way.
3690 unsigned IndexIdx = 0;
3691 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003692 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003693
3694 if (this->Designators[I].isArrayDesignator()) {
3695 // Compute type- and value-dependence.
3696 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003697 if (Index->isTypeDependent() || Index->isValueDependent())
3698 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003699 if (Index->isInstantiationDependent())
3700 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003701 // Propagate unexpanded parameter packs.
3702 if (Index->containsUnexpandedParameterPack())
3703 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003704
3705 // Copy the index expressions into permanent storage.
3706 *Child++ = IndexExprs[IndexIdx++];
3707 } else if (this->Designators[I].isArrayRangeDesignator()) {
3708 // Compute type- and value-dependence.
3709 Expr *Start = IndexExprs[IndexIdx];
3710 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003711 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003712 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003713 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003714 ExprBits.InstantiationDependent = true;
3715 } else if (Start->isInstantiationDependent() ||
3716 End->isInstantiationDependent()) {
3717 ExprBits.InstantiationDependent = true;
3718 }
3719
Douglas Gregora6e053e2010-12-15 01:34:56 +00003720 // Propagate unexpanded parameter packs.
3721 if (Start->containsUnexpandedParameterPack() ||
3722 End->containsUnexpandedParameterPack())
3723 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003724
3725 // Copy the start/end expressions into permanent storage.
3726 *Child++ = IndexExprs[IndexIdx++];
3727 *Child++ = IndexExprs[IndexIdx++];
3728 }
3729 }
3730
Benjamin Kramerc215e762012-08-24 11:54:20 +00003731 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003732}
3733
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003734DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003735DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003736 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003737 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003738 SourceLocation ColonOrEqualLoc,
3739 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003740 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003741 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003742 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003743 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003744 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003745}
3746
Craig Topper37932912013-08-18 10:09:15 +00003747DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003748 unsigned NumIndexExprs) {
3749 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3750 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3751 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3752}
3753
Craig Topper37932912013-08-18 10:09:15 +00003754void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003755 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003756 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003757 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003758 NumDesignators = NumDesigs;
3759 for (unsigned I = 0; I != NumDesigs; ++I)
3760 Designators[I] = Desigs[I];
3761}
3762
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003763SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3764 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3765 if (size() == 1)
3766 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003767 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3768 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003769}
3770
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003771SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003772 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003773 Designator &First =
3774 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003775 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003776 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003777 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3778 else
3779 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3780 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003781 StartLoc =
3782 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003783 return StartLoc;
3784}
3785
3786SourceLocation DesignatedInitExpr::getLocEnd() const {
3787 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003788}
3789
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003790Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003791 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003792 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003793 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3794}
3795
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003796Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003797 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003798 "Requires array range 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::getArrayRangeEnd(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 + 2));
3808}
3809
Douglas Gregord5846a12009-04-15 06:41:24 +00003810/// \brief Replaces the designator at index @p Idx with the series
3811/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003812void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003813 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003814 const Designator *Last) {
3815 unsigned NumNewDesignators = Last - First;
3816 if (NumNewDesignators == 0) {
3817 std::copy_backward(Designators + Idx + 1,
3818 Designators + NumDesignators,
3819 Designators + Idx);
3820 --NumNewDesignators;
3821 return;
3822 } else if (NumNewDesignators == 1) {
3823 Designators[Idx] = *First;
3824 return;
3825 }
3826
Mike Stump11289f42009-09-09 15:08:12 +00003827 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003828 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003829 std::copy(Designators, Designators + Idx, NewDesignators);
3830 std::copy(First, Last, NewDesignators + Idx);
3831 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3832 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003833 Designators = NewDesignators;
3834 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3835}
3836
Craig Topper37932912013-08-18 10:09:15 +00003837ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003838 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003839 SourceLocation rparenloc)
3840 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003841 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003842 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3843 Exprs = new (C) Stmt*[exprs.size()];
3844 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003845 if (exprs[i]->isTypeDependent())
3846 ExprBits.TypeDependent = true;
3847 if (exprs[i]->isValueDependent())
3848 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003849 if (exprs[i]->isInstantiationDependent())
3850 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003851 if (exprs[i]->containsUnexpandedParameterPack())
3852 ExprBits.ContainsUnexpandedParameterPack = true;
3853
Nate Begeman5ec4b312009-08-10 23:49:36 +00003854 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003855 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003856}
3857
John McCall1bf58462011-02-16 08:02:54 +00003858const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3859 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3860 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003861 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3862 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003863 e = cast<CXXConstructExpr>(e)->getArg(0);
3864 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3865 e = ice->getSubExpr();
3866 return cast<OpaqueValueExpr>(e);
3867}
3868
Craig Topper37932912013-08-18 10:09:15 +00003869PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3870 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003871 unsigned numSemanticExprs) {
3872 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3873 (1 + numSemanticExprs) * sizeof(Expr*),
3874 llvm::alignOf<PseudoObjectExpr>());
3875 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3876}
3877
3878PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3879 : Expr(PseudoObjectExprClass, shell) {
3880 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3881}
3882
Craig Topper37932912013-08-18 10:09:15 +00003883PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003884 ArrayRef<Expr*> semantics,
3885 unsigned resultIndex) {
3886 assert(syntax && "no syntactic expression!");
3887 assert(semantics.size() && "no semantic expressions!");
3888
3889 QualType type;
3890 ExprValueKind VK;
3891 if (resultIndex == NoResult) {
3892 type = C.VoidTy;
3893 VK = VK_RValue;
3894 } else {
3895 assert(resultIndex < semantics.size());
3896 type = semantics[resultIndex]->getType();
3897 VK = semantics[resultIndex]->getValueKind();
3898 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3899 }
3900
3901 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3902 (1 + semantics.size()) * sizeof(Expr*),
3903 llvm::alignOf<PseudoObjectExpr>());
3904 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3905 resultIndex);
3906}
3907
3908PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3909 Expr *syntax, ArrayRef<Expr*> semantics,
3910 unsigned resultIndex)
3911 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3912 /*filled in at end of ctor*/ false, false, false, false) {
3913 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3914 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3915
3916 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3917 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3918 getSubExprsBuffer()[i] = E;
3919
3920 if (E->isTypeDependent())
3921 ExprBits.TypeDependent = true;
3922 if (E->isValueDependent())
3923 ExprBits.ValueDependent = true;
3924 if (E->isInstantiationDependent())
3925 ExprBits.InstantiationDependent = true;
3926 if (E->containsUnexpandedParameterPack())
3927 ExprBits.ContainsUnexpandedParameterPack = true;
3928
3929 if (isa<OpaqueValueExpr>(E))
3930 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3931 "opaque-value semantic expressions for pseudo-object "
3932 "operations must have sources");
3933 }
3934}
3935
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003936//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003937// ExprIterator.
3938//===----------------------------------------------------------------------===//
3939
3940Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3941Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3942Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3943const Expr* ConstExprIterator::operator[](size_t idx) const {
3944 return cast<Expr>(I[idx]);
3945}
3946const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3947const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3948
3949//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003950// Child Iterators for iterating over subexpressions/substatements
3951//===----------------------------------------------------------------------===//
3952
Peter Collingbournee190dee2011-03-11 19:24:49 +00003953// UnaryExprOrTypeTraitExpr
3954Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003955 // If this is of a type and the type is a VLA type (and not a typedef), the
3956 // size expression of the VLA needs to be treated as an executable expression.
3957 // Why isn't this weirdness documented better in StmtIterator?
3958 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003959 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003960 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003961 return child_range(child_iterator(T), child_iterator());
3962 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003963 }
John McCallbd066782011-02-09 08:16:59 +00003964 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003965}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003966
Steve Naroffd54978b2007-09-18 23:55:05 +00003967// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003968Stmt::child_range ObjCMessageExpr::children() {
3969 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003970 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003971 begin = reinterpret_cast<Stmt **>(this + 1);
3972 else
3973 begin = reinterpret_cast<Stmt **>(getArgs());
3974 return child_range(begin,
3975 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003976}
3977
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003978ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003979 QualType T, ObjCMethodDecl *Method,
3980 SourceRange SR)
3981 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3982 false, false, false, false),
3983 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3984{
3985 Expr **SaveElements = getElements();
3986 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3987 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3988 ExprBits.ValueDependent = true;
3989 if (Elements[I]->isInstantiationDependent())
3990 ExprBits.InstantiationDependent = true;
3991 if (Elements[I]->containsUnexpandedParameterPack())
3992 ExprBits.ContainsUnexpandedParameterPack = true;
3993
3994 SaveElements[I] = Elements[I];
3995 }
3996}
3997
Craig Topperce7167c2013-08-22 04:58:56 +00003998ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003999 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004000 QualType T, ObjCMethodDecl * Method,
4001 SourceRange SR) {
4002 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4003 + Elements.size() * sizeof(Expr *));
4004 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4005}
4006
Craig Topperce7167c2013-08-22 04:58:56 +00004007ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004008 unsigned NumElements) {
4009
4010 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4011 + NumElements * sizeof(Expr *));
4012 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4013}
4014
4015ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4016 ArrayRef<ObjCDictionaryElement> VK,
4017 bool HasPackExpansions,
4018 QualType T, ObjCMethodDecl *method,
4019 SourceRange SR)
4020 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4021 false, false),
4022 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4023 DictWithObjectsMethod(method)
4024{
4025 KeyValuePair *KeyValues = getKeyValues();
4026 ExpansionData *Expansions = getExpansionData();
4027 for (unsigned I = 0; I < NumElements; I++) {
4028 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4029 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4030 ExprBits.ValueDependent = true;
4031 if (VK[I].Key->isInstantiationDependent() ||
4032 VK[I].Value->isInstantiationDependent())
4033 ExprBits.InstantiationDependent = true;
4034 if (VK[I].EllipsisLoc.isInvalid() &&
4035 (VK[I].Key->containsUnexpandedParameterPack() ||
4036 VK[I].Value->containsUnexpandedParameterPack()))
4037 ExprBits.ContainsUnexpandedParameterPack = true;
4038
4039 KeyValues[I].Key = VK[I].Key;
4040 KeyValues[I].Value = VK[I].Value;
4041 if (Expansions) {
4042 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4043 if (VK[I].NumExpansions)
4044 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4045 else
4046 Expansions[I].NumExpansionsPlusOne = 0;
4047 }
4048 }
4049}
4050
4051ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004052ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004053 ArrayRef<ObjCDictionaryElement> VK,
4054 bool HasPackExpansions,
4055 QualType T, ObjCMethodDecl *method,
4056 SourceRange SR) {
4057 unsigned ExpansionsSize = 0;
4058 if (HasPackExpansions)
4059 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4060
4061 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4062 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4063 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4064}
4065
4066ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004067ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004068 bool HasPackExpansions) {
4069 unsigned ExpansionsSize = 0;
4070 if (HasPackExpansions)
4071 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4072 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4073 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4074 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4075 HasPackExpansions);
4076}
4077
Craig Topperce7167c2013-08-22 04:58:56 +00004078ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004079 Expr *base,
4080 Expr *key, QualType T,
4081 ObjCMethodDecl *getMethod,
4082 ObjCMethodDecl *setMethod,
4083 SourceLocation RB) {
4084 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4085 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4086 OK_ObjCSubscript,
4087 getMethod, setMethod, RB);
4088}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004089
Benjamin Kramerc215e762012-08-24 11:54:20 +00004090AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004091 QualType t, AtomicOp op, SourceLocation RP)
4092 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4093 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004094 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004095{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004096 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4097 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004098 if (args[i]->isTypeDependent())
4099 ExprBits.TypeDependent = true;
4100 if (args[i]->isValueDependent())
4101 ExprBits.ValueDependent = true;
4102 if (args[i]->isInstantiationDependent())
4103 ExprBits.InstantiationDependent = true;
4104 if (args[i]->containsUnexpandedParameterPack())
4105 ExprBits.ContainsUnexpandedParameterPack = true;
4106
4107 SubExprs[i] = args[i];
4108 }
4109}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004110
4111unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4112 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004113 case AO__c11_atomic_init:
4114 case AO__c11_atomic_load:
4115 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004116 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004117
4118 case AO__c11_atomic_store:
4119 case AO__c11_atomic_exchange:
4120 case AO__atomic_load:
4121 case AO__atomic_store:
4122 case AO__atomic_store_n:
4123 case AO__atomic_exchange_n:
4124 case AO__c11_atomic_fetch_add:
4125 case AO__c11_atomic_fetch_sub:
4126 case AO__c11_atomic_fetch_and:
4127 case AO__c11_atomic_fetch_or:
4128 case AO__c11_atomic_fetch_xor:
4129 case AO__atomic_fetch_add:
4130 case AO__atomic_fetch_sub:
4131 case AO__atomic_fetch_and:
4132 case AO__atomic_fetch_or:
4133 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004134 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004135 case AO__atomic_add_fetch:
4136 case AO__atomic_sub_fetch:
4137 case AO__atomic_and_fetch:
4138 case AO__atomic_or_fetch:
4139 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004140 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004141 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004142
4143 case AO__atomic_exchange:
4144 return 4;
4145
4146 case AO__c11_atomic_compare_exchange_strong:
4147 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004148 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004149
4150 case AO__atomic_compare_exchange:
4151 case AO__atomic_compare_exchange_n:
4152 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004153 }
4154 llvm_unreachable("unknown atomic op");
4155}