blob: 4bc455837c76e1cb14417189c8688b06130f5000 [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
108const Expr *
109Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
110 const Expr *E = this;
Richard Smith852c9db2013-04-20 22:23:05 +0000111
112 // This might be a default initializer for a reference member. Walk over the
113 // wrapper node for that.
114 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
115 E = DAE->getExpr();
116
Rafael Espindola9c006de2012-10-27 01:03:43 +0000117 // Look through single-element init lists that claim to be lvalues. They're
118 // just syntactic wrappers in this case.
119 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
Richard Smith852c9db2013-04-20 22:23:05 +0000120 if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +0000121 E = ILE->getInit(0);
Richard Smith852c9db2013-04-20 22:23:05 +0000122 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
123 E = DAE->getExpr();
124 }
Rafael Espindola9c006de2012-10-27 01:03:43 +0000125 }
126
127 // Look through expressions for materialized temporaries (for now).
128 if (const MaterializeTemporaryExpr *M
129 = dyn_cast<MaterializeTemporaryExpr>(E)) {
130 MTE = M;
131 E = M->GetTemporaryExpr();
132 }
133
134 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
135 E = DAE->getExpr();
136 return E;
137}
138
Chris Lattner4ebae652010-04-16 23:34:13 +0000139/// isKnownToHaveBooleanValue - Return true if this is an integer expression
140/// that is known to return 0 or 1. This happens for _Bool/bool expressions
141/// but also int expressions which are produced by things like comparisons in
142/// C.
143bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000144 const Expr *E = IgnoreParens();
145
Chris Lattner4ebae652010-04-16 23:34:13 +0000146 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000147 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000149 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000150
Peter Collingbourne91147592011-04-15 00:35:48 +0000151 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000152 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000153 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000154 return UO->getSubExpr()->isKnownToHaveBooleanValue();
155 default:
156 return false;
157 }
158 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000159
John McCall45d30c32010-06-12 01:56:02 +0000160 // Only look through implicit casts. If the user writes
161 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000162 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000163 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000164
Peter Collingbourne91147592011-04-15 00:35:48 +0000165 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000166 switch (BO->getOpcode()) {
167 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000168 case BO_LT: // Relational operators.
169 case BO_GT:
170 case BO_LE:
171 case BO_GE:
172 case BO_EQ: // Equality operators.
173 case BO_NE:
174 case BO_LAnd: // AND operator.
175 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000176 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000177
John McCalle3027922010-08-25 11:45:40 +0000178 case BO_And: // Bitwise AND operator.
179 case BO_Xor: // Bitwise XOR operator.
180 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000181 // Handle things like (x==2)|(y==12).
182 return BO->getLHS()->isKnownToHaveBooleanValue() &&
183 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000184
John McCalle3027922010-08-25 11:45:40 +0000185 case BO_Comma:
186 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000187 return BO->getRHS()->isKnownToHaveBooleanValue();
188 }
189 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000190
Peter Collingbourne91147592011-04-15 00:35:48 +0000191 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000192 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
193 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000194
Chris Lattner4ebae652010-04-16 23:34:13 +0000195 return false;
196}
197
John McCallbd066782011-02-09 08:16:59 +0000198// Amusing macro metaprogramming hack: check whether a class provides
199// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000200//
201// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000202namespace {
203 /// This implementation is used when a class provides a custom
204 /// implementation of getExprLoc.
205 template <class E, class T>
206 SourceLocation getExprLocImpl(const Expr *expr,
207 SourceLocation (T::*v)() const) {
208 return static_cast<const E*>(expr)->getExprLoc();
209 }
210
211 /// This implementation is used when a class doesn't provide
212 /// a custom implementation of getExprLoc. Overload resolution
213 /// should pick it over the implementation above because it's
214 /// more specialized according to function template partial ordering.
215 template <class E>
216 SourceLocation getExprLocImpl(const Expr *expr,
217 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000218 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000219 }
220}
221
222SourceLocation Expr::getExprLoc() const {
223 switch (getStmtClass()) {
224 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
225#define ABSTRACT_STMT(type)
226#define STMT(type, base) \
227 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
228#define EXPR(type, base) \
229 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
230#include "clang/AST/StmtNodes.inc"
231 }
232 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000233}
234
Chris Lattner0eedafe2006-08-24 04:56:27 +0000235//===----------------------------------------------------------------------===//
236// Primary Expressions.
237//===----------------------------------------------------------------------===//
238
Douglas Gregor678d76c2011-07-01 01:22:09 +0000239/// \brief Compute the type-, value-, and instantiation-dependence of a
240/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000242static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
243 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 bool &ValueDependent,
245 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000246 TypeDependent = false;
247 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000248 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000249
250 // (TD) C++ [temp.dep.expr]p3:
251 // An id-expression is type-dependent if it contains:
252 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000253 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000254 //
255 // (VD) C++ [temp.dep.constexpr]p2:
256 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000257
Douglas Gregored6c7442009-11-23 11:41:28 +0000258 // (TD) - an identifier that was declared with dependent type
259 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000260 if (T->isDependentType()) {
261 TypeDependent = true;
262 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000263 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000264 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000265 } else if (T->isInstantiationDependentType()) {
266 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000267 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000268
Douglas Gregored6c7442009-11-23 11:41:28 +0000269 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000270 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000271 == DeclarationName::CXXConversionFunctionName) {
272 QualType T = D->getDeclName().getCXXNameType();
273 if (T->isDependentType()) {
274 TypeDependent = true;
275 ValueDependent = true;
276 InstantiationDependent = true;
277 return;
278 }
279
280 if (T->isInstantiationDependentType())
281 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000282 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000283
Douglas Gregored6c7442009-11-23 11:41:28 +0000284 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000285 if (isa<NonTypeTemplateParmDecl>(D)) {
286 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000287 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000288 return;
289 }
290
Douglas Gregored6c7442009-11-23 11:41:28 +0000291 // (VD) - a constant with integral or enumeration type and is
292 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000293 // (VD) - a constant with literal type and is initialized with an
294 // expression that is value-dependent [C++11].
295 // (VD) - FIXME: Missing from the standard:
296 // - an entity with reference type and is initialized with an
297 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000298 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000299 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000300 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000301 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000302 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000303 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000304 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000305 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000306 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000307 InstantiationDependent = true;
308 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000309 }
310
Douglas Gregor0e4de762010-05-11 08:41:30 +0000311 // (VD) - FIXME: Missing from the standard:
312 // - a member function or a static data member of the current
313 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000314 if (Var->isStaticDataMember() &&
315 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000316 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000317 InstantiationDependent = true;
Richard Smith00f5d892013-11-14 22:40:45 +0000318 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
319 if (TInfo->getType()->isIncompleteArrayType())
320 TypeDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000321 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000322
323 return;
324 }
325
Douglas Gregor0e4de762010-05-11 08:41:30 +0000326 // (VD) - FIXME: Missing from the standard:
327 // - a member function or a static data member of the current
328 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000329 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
330 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000331 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000332 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000333}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000334
Craig Topperce7167c2013-08-22 04:58:56 +0000335void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000336 bool TypeDependent = false;
337 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000338 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000339 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
340 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000341
342 // (TD) C++ [temp.dep.expr]p3:
343 // An id-expression is type-dependent if it contains:
344 //
345 // and
346 //
347 // (VD) C++ [temp.dep.constexpr]p2:
348 // An identifier is value-dependent if it is:
349 if (!TypeDependent && !ValueDependent &&
350 hasExplicitTemplateArgs() &&
351 TemplateSpecializationType::anyDependentTemplateArguments(
352 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000353 getNumTemplateArgs(),
354 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000355 TypeDependent = true;
356 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000357 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000358 }
359
360 ExprBits.TypeDependent = TypeDependent;
361 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000362 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000363
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000364 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000365 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000366 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000367}
368
Craig Topperce7167c2013-08-22 04:58:56 +0000369DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000370 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000371 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000372 ValueDecl *D, bool RefersToEnclosingLocal,
373 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000374 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000375 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000376 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000377 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000378 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
379 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000380 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000381 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000382 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
383 if (FoundD)
384 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 DeclRefExprBits.HasTemplateKWAndArgsInfo
386 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000387 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000388 if (TemplateArgs) {
389 bool Dependent = false;
390 bool InstantiationDependent = false;
391 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000392 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
393 Dependent,
394 InstantiationDependent,
395 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000396 if (InstantiationDependent)
397 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000398 } else if (TemplateKWLoc.isValid()) {
399 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000400 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000401 DeclRefExprBits.HadMultipleCandidates = 0;
402
Daniel Dunbar9d355812012-03-09 01:51:51 +0000403 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000404}
405
Craig Topperce7167c2013-08-22 04:58:56 +0000406DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000407 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000408 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000409 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000410 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000411 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000412 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000413 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000414 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000415 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000416 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000417 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000418 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000419 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000420}
421
Craig Topperce7167c2013-08-22 04:58:56 +0000422DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000423 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000424 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000425 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000426 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000427 const DeclarationNameInfo &NameInfo,
428 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000429 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000430 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000431 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000432 // Filter out cases where the found Decl is the same as the value refenenced.
433 if (D == FoundD)
434 FoundD = 0;
435
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000436 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000437 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000438 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000439 if (FoundD)
440 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000441 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000442 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
443 else if (TemplateKWLoc.isValid())
444 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000445
Chris Lattner5c0b4052010-10-30 05:14:06 +0000446 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000447 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000448 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000449 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000450}
451
Craig Topperce7167c2013-08-22 04:58:56 +0000452DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000453 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000454 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000455 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000456 unsigned NumTemplateArgs) {
457 std::size_t Size = sizeof(DeclRefExpr);
458 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000459 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000460 if (HasFoundDecl)
461 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000462 if (HasTemplateKWAndArgsInfo)
463 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000464
Chris Lattner5c0b4052010-10-30 05:14:06 +0000465 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000466 return new (Mem) DeclRefExpr(EmptyShell());
467}
468
Daniel Dunbarb507f272012-03-09 15:39:15 +0000469SourceLocation DeclRefExpr::getLocStart() const {
470 if (hasQualifier())
471 return getQualifierLoc().getBeginLoc();
472 return getNameInfo().getLocStart();
473}
474SourceLocation DeclRefExpr::getLocEnd() const {
475 if (hasExplicitTemplateArgs())
476 return getRAngleLoc();
477 return getNameInfo().getLocEnd();
478}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000479
Anders Carlsson2fb08242009-09-08 18:24:21 +0000480// FIXME: Maybe this should use DeclPrinter with a special "print predefined
481// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000482std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
483 ASTContext &Context = CurrentDecl->getASTContext();
484
David Majnemerbed356a2013-11-06 23:31:56 +0000485 if (IT == PredefinedExpr::FuncDName) {
486 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
487 OwningPtr<MangleContext> MC;
488 MC.reset(Context.createMangleContext());
489
490 if (MC->shouldMangleDeclName(ND)) {
491 SmallString<256> Buffer;
492 llvm::raw_svector_ostream Out(Buffer);
493 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
494 MC->mangleCXXCtor(CD, Ctor_Base, Out);
495 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
496 MC->mangleCXXDtor(DD, Dtor_Base, Out);
497 else
498 MC->mangleName(ND, Out);
499
500 Out.flush();
501 if (!Buffer.empty() && Buffer.front() == '\01')
502 return Buffer.substr(1);
503 return Buffer.str();
504 } else
505 return ND->getIdentifier()->getName();
506 }
507 return "";
508 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000509 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000510 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000511 return FD->getNameAsString();
512
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000513 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000514 llvm::raw_svector_ostream Out(Name);
515
516 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000517 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000518 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000519 if (MD->isStatic())
520 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000521 }
522
David Blaikiebbafb8a2012-03-11 07:00:24 +0000523 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000524 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000525 llvm::raw_string_ostream POut(Proto);
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000526 FD->printQualifiedName(POut, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000527
Douglas Gregor11a434a2012-04-10 20:14:15 +0000528 const FunctionDecl *Decl = FD;
529 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
530 Decl = Pattern;
531 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000532 const FunctionProtoType *FT = 0;
533 if (FD->hasWrittenPrototype())
534 FT = dyn_cast<FunctionProtoType>(AFT);
535
Douglas Gregor11a434a2012-04-10 20:14:15 +0000536 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000537 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000538 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000539 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000540 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000541 }
542
543 if (FT->isVariadic()) {
544 if (FD->getNumParams()) POut << ", ";
545 POut << "...";
546 }
547 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000548 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000549
Sam Weinig4e83bd22009-12-27 01:38:20 +0000550 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000551 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000552 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000553 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000554 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000555 POut << " volatile";
556 RefQualifierKind Ref = MD->getRefQualifier();
557 if (Ref == RQ_LValue)
558 POut << " &";
559 else if (Ref == RQ_RValue)
560 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000561 }
562
Douglas Gregor11a434a2012-04-10 20:14:15 +0000563 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
564 SpecsTy Specs;
565 const DeclContext *Ctx = FD->getDeclContext();
566 while (Ctx && isa<NamedDecl>(Ctx)) {
567 const ClassTemplateSpecializationDecl *Spec
568 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
569 if (Spec && !Spec->isExplicitSpecialization())
570 Specs.push_back(Spec);
571 Ctx = Ctx->getParent();
572 }
573
574 std::string TemplateParams;
575 llvm::raw_string_ostream TOut(TemplateParams);
576 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
577 I != E; ++I) {
578 const TemplateParameterList *Params
579 = (*I)->getSpecializedTemplate()->getTemplateParameters();
580 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
581 assert(Params->size() == Args.size());
582 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
583 StringRef Param = Params->getParam(i)->getName();
584 if (Param.empty()) continue;
585 TOut << Param << " = ";
586 Args.get(i).print(Policy, TOut);
587 TOut << ", ";
588 }
589 }
590
591 FunctionTemplateSpecializationInfo *FSI
592 = FD->getTemplateSpecializationInfo();
593 if (FSI && !FSI->isExplicitSpecialization()) {
594 const TemplateParameterList* Params
595 = FSI->getTemplate()->getTemplateParameters();
596 const TemplateArgumentList* Args = FSI->TemplateArguments;
597 assert(Params->size() == Args->size());
598 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
599 StringRef Param = Params->getParam(i)->getName();
600 if (Param.empty()) continue;
601 TOut << Param << " = ";
602 Args->get(i).print(Policy, TOut);
603 TOut << ", ";
604 }
605 }
606
607 TOut.flush();
608 if (!TemplateParams.empty()) {
609 // remove the trailing comma and space
610 TemplateParams.resize(TemplateParams.size() - 2);
611 POut << " [" << TemplateParams << "]";
612 }
613
614 POut.flush();
615
Benjamin Kramer90f54222013-08-21 11:45:27 +0000616 // Print "auto" for all deduced return types. This includes C++1y return
617 // type deduction and lambdas. For trailing return types resolve the
618 // decltype expression. Otherwise print the real type when this is
619 // not a constructor or destructor.
620 if ((isa<CXXMethodDecl>(FD) &&
621 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
622 (FT && FT->getResultType()->getAs<AutoType>()))
623 Proto = "auto " + Proto;
624 else if (FT && FT->getResultType()->getAs<DecltypeType>())
625 FT->getResultType()->getAs<DecltypeType>()->getUnderlyingType()
626 .getAsStringInternal(Proto, Policy);
627 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Sam Weinigd060ed42009-12-06 23:55:13 +0000628 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000629
630 Out << Proto;
631
632 Out.flush();
633 return Name.str().str();
634 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000635 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
636 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
637 // Skip to its enclosing function or method, but not its enclosing
638 // CapturedDecl.
639 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
640 const Decl *D = Decl::castFromDeclContext(DC);
641 return ComputeName(IT, D);
642 }
643 llvm_unreachable("CapturedDecl not inside a function or method");
644 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000645 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000646 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000647 llvm::raw_svector_ostream Out(Name);
648 Out << (MD->isInstanceMethod() ? '-' : '+');
649 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000650
651 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
652 // a null check to avoid a crash.
653 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000654 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000655
Anders Carlsson2fb08242009-09-08 18:24:21 +0000656 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000657 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000658 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000659
Anders Carlsson2fb08242009-09-08 18:24:21 +0000660 Out << ' ';
661 Out << MD->getSelector().getAsString();
662 Out << ']';
663
664 Out.flush();
665 return Name.str().str();
666 }
667 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
668 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
669 return "top level";
670 }
671 return "";
672}
673
Craig Topper37932912013-08-18 10:09:15 +0000674void APNumericStorage::setIntValue(const ASTContext &C,
675 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000676 if (hasAllocation())
677 C.Deallocate(pVal);
678
679 BitWidth = Val.getBitWidth();
680 unsigned NumWords = Val.getNumWords();
681 const uint64_t* Words = Val.getRawData();
682 if (NumWords > 1) {
683 pVal = new (C) uint64_t[NumWords];
684 std::copy(Words, Words + NumWords, pVal);
685 } else if (NumWords == 1)
686 VAL = Words[0];
687 else
688 VAL = 0;
689}
690
Craig Topper37932912013-08-18 10:09:15 +0000691IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000692 QualType type, SourceLocation l)
693 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
694 false, false),
695 Loc(l) {
696 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
697 assert(V.getBitWidth() == C.getIntWidth(type) &&
698 "Integer type is not the correct size for constant.");
699 setValue(C, V);
700}
701
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000702IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000703IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000704 QualType type, SourceLocation l) {
705 return new (C) IntegerLiteral(C, V, type, l);
706}
707
708IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000709IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000710 return new (C) IntegerLiteral(Empty);
711}
712
Craig Topper37932912013-08-18 10:09:15 +0000713FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000714 bool isexact, QualType Type, SourceLocation L)
715 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
716 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000717 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000718 FloatingLiteralBits.IsExact = isexact;
719 setValue(C, V);
720}
721
Craig Topper37932912013-08-18 10:09:15 +0000722FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000723 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000724 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000725 FloatingLiteralBits.IsExact = false;
726}
727
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000728FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000729FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000730 bool isexact, QualType Type, SourceLocation L) {
731 return new (C) FloatingLiteral(C, V, isexact, Type, L);
732}
733
734FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000735FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000736 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000737}
738
Tim Northover178723a2013-01-22 09:46:51 +0000739const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
740 switch(FloatingLiteralBits.Semantics) {
741 case IEEEhalf:
742 return llvm::APFloat::IEEEhalf;
743 case IEEEsingle:
744 return llvm::APFloat::IEEEsingle;
745 case IEEEdouble:
746 return llvm::APFloat::IEEEdouble;
747 case x87DoubleExtended:
748 return llvm::APFloat::x87DoubleExtended;
749 case IEEEquad:
750 return llvm::APFloat::IEEEquad;
751 case PPCDoubleDouble:
752 return llvm::APFloat::PPCDoubleDouble;
753 }
754 llvm_unreachable("Unrecognised floating semantics");
755}
756
757void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
758 if (&Sem == &llvm::APFloat::IEEEhalf)
759 FloatingLiteralBits.Semantics = IEEEhalf;
760 else if (&Sem == &llvm::APFloat::IEEEsingle)
761 FloatingLiteralBits.Semantics = IEEEsingle;
762 else if (&Sem == &llvm::APFloat::IEEEdouble)
763 FloatingLiteralBits.Semantics = IEEEdouble;
764 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
765 FloatingLiteralBits.Semantics = x87DoubleExtended;
766 else if (&Sem == &llvm::APFloat::IEEEquad)
767 FloatingLiteralBits.Semantics = IEEEquad;
768 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
769 FloatingLiteralBits.Semantics = PPCDoubleDouble;
770 else
771 llvm_unreachable("Unknown floating semantics");
772}
773
Chris Lattnera0173132008-06-07 22:13:43 +0000774/// getValueAsApproximateDouble - This returns the value as an inaccurate
775/// double. Note that this may cause loss of precision, but is useful for
776/// debugging dumps, etc.
777double FloatingLiteral::getValueAsApproximateDouble() const {
778 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000779 bool ignored;
780 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
781 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000782 return V.convertToDouble();
783}
784
Nick Lewycky4ed84042012-02-24 09:07:53 +0000785int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000786 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000787 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000788 case Ascii:
789 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000790 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000791 break;
792 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000793 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000794 break;
795 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000796 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000797 break;
798 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000799 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000800 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000801 }
802 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
803 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000804 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000805 && "character byte widths supported are 1, 2, and 4 only");
806 return CharByteWidth;
807}
808
Craig Topper37932912013-08-18 10:09:15 +0000809StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000810 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000811 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000812 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000813 // Allocate enough space for the StringLiteral plus an array of locations for
814 // any concatenated string tokens.
815 void *Mem = C.Allocate(sizeof(StringLiteral)+
816 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000817 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000818 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000819
Steve Naroffdf7855b2007-02-21 23:46:25 +0000820 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000821 SL->setString(C,Str,Kind,Pascal);
822
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000823 SL->TokLocs[0] = Loc[0];
824 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000825
Chris Lattner630970d2009-02-18 05:49:11 +0000826 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000827 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
828 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000829}
830
Craig Topper37932912013-08-18 10:09:15 +0000831StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
832 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000833 void *Mem = C.Allocate(sizeof(StringLiteral)+
834 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000835 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000836 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000837 SL->CharByteWidth = 0;
838 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000839 SL->NumConcatenated = NumStrs;
840 return SL;
841}
842
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000843void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000844 switch (getKind()) {
845 case Ascii: break; // no prefix.
846 case Wide: OS << 'L'; break;
847 case UTF8: OS << "u8"; break;
848 case UTF16: OS << 'u'; break;
849 case UTF32: OS << 'U'; break;
850 }
851 OS << '"';
852 static const char Hex[] = "0123456789ABCDEF";
853
854 unsigned LastSlashX = getLength();
855 for (unsigned I = 0, N = getLength(); I != N; ++I) {
856 switch (uint32_t Char = getCodeUnit(I)) {
857 default:
858 // FIXME: Convert UTF-8 back to codepoints before rendering.
859
860 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
861 // Leave invalid surrogates alone; we'll use \x for those.
862 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
863 Char <= 0xdbff) {
864 uint32_t Trail = getCodeUnit(I + 1);
865 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
866 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
867 ++I;
868 }
869 }
870
871 if (Char > 0xff) {
872 // If this is a wide string, output characters over 0xff using \x
873 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
874 // codepoint: use \x escapes for invalid codepoints.
875 if (getKind() == Wide ||
876 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
877 // FIXME: Is this the best way to print wchar_t?
878 OS << "\\x";
879 int Shift = 28;
880 while ((Char >> Shift) == 0)
881 Shift -= 4;
882 for (/**/; Shift >= 0; Shift -= 4)
883 OS << Hex[(Char >> Shift) & 15];
884 LastSlashX = I;
885 break;
886 }
887
888 if (Char > 0xffff)
889 OS << "\\U00"
890 << Hex[(Char >> 20) & 15]
891 << Hex[(Char >> 16) & 15];
892 else
893 OS << "\\u";
894 OS << Hex[(Char >> 12) & 15]
895 << Hex[(Char >> 8) & 15]
896 << Hex[(Char >> 4) & 15]
897 << Hex[(Char >> 0) & 15];
898 break;
899 }
900
901 // If we used \x... for the previous character, and this character is a
902 // hexadecimal digit, prevent it being slurped as part of the \x.
903 if (LastSlashX + 1 == I) {
904 switch (Char) {
905 case '0': case '1': case '2': case '3': case '4':
906 case '5': case '6': case '7': case '8': case '9':
907 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
908 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
909 OS << "\"\"";
910 }
911 }
912
913 assert(Char <= 0xff &&
914 "Characters above 0xff should already have been handled.");
915
Jordan Rosea7d03842013-02-08 22:30:41 +0000916 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000917 OS << (char)Char;
918 else // Output anything hard as an octal escape.
919 OS << '\\'
920 << (char)('0' + ((Char >> 6) & 7))
921 << (char)('0' + ((Char >> 3) & 7))
922 << (char)('0' + ((Char >> 0) & 7));
923 break;
924 // Handle some common non-printable cases to make dumps prettier.
925 case '\\': OS << "\\\\"; break;
926 case '"': OS << "\\\""; break;
927 case '\n': OS << "\\n"; break;
928 case '\t': OS << "\\t"; break;
929 case '\a': OS << "\\a"; break;
930 case '\b': OS << "\\b"; break;
931 }
932 }
933 OS << '"';
934}
935
Craig Topper37932912013-08-18 10:09:15 +0000936void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000937 StringKind Kind, bool IsPascal) {
938 //FIXME: we assume that the string data comes from a target that uses the same
939 // code unit size and endianess for the type of string.
940 this->Kind = Kind;
941 this->IsPascal = IsPascal;
942
Nick Lewycky4ed84042012-02-24 09:07:53 +0000943 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000944 assert((Str.size()%CharByteWidth == 0)
945 && "size of data must be multiple of CharByteWidth");
946 Length = Str.size()/CharByteWidth;
947
948 switch(CharByteWidth) {
949 case 1: {
950 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000951 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000952 StrData.asChar = AStrData;
953 break;
954 }
955 case 2: {
956 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000957 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000958 StrData.asUInt16 = AStrData;
959 break;
960 }
961 case 4: {
962 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000963 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000964 StrData.asUInt32 = AStrData;
965 break;
966 }
967 default:
968 assert(false && "unsupported CharByteWidth");
969 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000970}
971
Chris Lattnere925d612010-11-17 07:37:15 +0000972/// getLocationOfByte - Return a source location that points to the specified
973/// byte of this string literal.
974///
975/// Strings are amazingly complex. They can be formed from multiple tokens and
976/// can have escape sequences in them in addition to the usual trigraph and
977/// escaped newline business. This routine handles this complexity.
978///
979SourceLocation StringLiteral::
980getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
981 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000982 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
983 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000984
Chris Lattnere925d612010-11-17 07:37:15 +0000985 // Loop over all of the tokens in this string until we find the one that
986 // contains the byte we're looking for.
987 unsigned TokNo = 0;
988 while (1) {
989 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
990 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
991
992 // Get the spelling of the string so that we can get the data that makes up
993 // the string literal, not the identifier for the macro it is potentially
994 // expanded through.
995 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
996
997 // Re-lex the token to get its length and original spelling.
998 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
999 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001000 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +00001001 if (Invalid)
1002 return StrTokSpellingLoc;
1003
1004 const char *StrData = Buffer.data()+LocInfo.second;
1005
Chris Lattnere925d612010-11-17 07:37:15 +00001006 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +00001007 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
1008 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +00001009 Token TheTok;
1010 TheLexer.LexFromRawLexer(TheTok);
1011
1012 // Use the StringLiteralParser to compute the length of the string in bytes.
1013 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
1014 unsigned TokNumBytes = SLP.GetStringLength();
1015
1016 // If the byte is in this token, return the location of the byte.
1017 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +00001018 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +00001019 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
1020
1021 // Now that we know the offset of the token in the spelling, use the
1022 // preprocessor to get the offset in the original source.
1023 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1024 }
1025
1026 // Move to the next string token.
1027 ++TokNo;
1028 ByteNo -= TokNumBytes;
1029 }
1030}
1031
1032
1033
Chris Lattner1b926492006-08-23 06:42:10 +00001034/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1035/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001036StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001037 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001038 case UO_PostInc: return "++";
1039 case UO_PostDec: return "--";
1040 case UO_PreInc: return "++";
1041 case UO_PreDec: return "--";
1042 case UO_AddrOf: return "&";
1043 case UO_Deref: return "*";
1044 case UO_Plus: return "+";
1045 case UO_Minus: return "-";
1046 case UO_Not: return "~";
1047 case UO_LNot: return "!";
1048 case UO_Real: return "__real";
1049 case UO_Imag: return "__imag";
1050 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001051 }
David Blaikief47fa302012-01-17 02:30:50 +00001052 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001053}
1054
John McCalle3027922010-08-25 11:45:40 +00001055UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001056UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1057 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001058 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001059 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1060 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1061 case OO_Amp: return UO_AddrOf;
1062 case OO_Star: return UO_Deref;
1063 case OO_Plus: return UO_Plus;
1064 case OO_Minus: return UO_Minus;
1065 case OO_Tilde: return UO_Not;
1066 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001067 }
1068}
1069
1070OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1071 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001072 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1073 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1074 case UO_AddrOf: return OO_Amp;
1075 case UO_Deref: return OO_Star;
1076 case UO_Plus: return OO_Plus;
1077 case UO_Minus: return OO_Minus;
1078 case UO_Not: return OO_Tilde;
1079 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001080 default: return OO_None;
1081 }
1082}
1083
1084
Chris Lattner0eedafe2006-08-24 04:56:27 +00001085//===----------------------------------------------------------------------===//
1086// Postfix Operators.
1087//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001088
Craig Topper37932912013-08-18 10:09:15 +00001089CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1090 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1091 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001092 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001093 fn->isTypeDependent(),
1094 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001095 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001096 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001097 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001098
Benjamin Kramerc215e762012-08-24 11:54:20 +00001099 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001100 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001101 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001102 if (args[i]->isTypeDependent())
1103 ExprBits.TypeDependent = true;
1104 if (args[i]->isValueDependent())
1105 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001106 if (args[i]->isInstantiationDependent())
1107 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001108 if (args[i]->containsUnexpandedParameterPack())
1109 ExprBits.ContainsUnexpandedParameterPack = true;
1110
Peter Collingbourne3a347252011-02-08 21:18:02 +00001111 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001112 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001113
Peter Collingbourne3a347252011-02-08 21:18:02 +00001114 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001115 RParenLoc = rparenloc;
1116}
Nate Begeman1e36a852008-01-17 17:46:27 +00001117
Craig Topper37932912013-08-18 10:09:15 +00001118CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001119 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1120 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001121 fn->isTypeDependent(),
1122 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001123 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001124 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001125 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001126
Benjamin Kramerc215e762012-08-24 11:54:20 +00001127 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001128 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001129 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001130 if (args[i]->isTypeDependent())
1131 ExprBits.TypeDependent = true;
1132 if (args[i]->isValueDependent())
1133 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001134 if (args[i]->isInstantiationDependent())
1135 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001136 if (args[i]->containsUnexpandedParameterPack())
1137 ExprBits.ContainsUnexpandedParameterPack = true;
1138
Peter Collingbourne3a347252011-02-08 21:18:02 +00001139 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001140 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001141
Peter Collingbourne3a347252011-02-08 21:18:02 +00001142 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001143 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001144}
1145
Craig Topper37932912013-08-18 10:09:15 +00001146CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump11289f42009-09-09 15:08:12 +00001147 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001148 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001149 SubExprs = new (C) Stmt*[PREARGS_START];
1150 CallExprBits.NumPreArgs = 0;
1151}
1152
Craig Topper37932912013-08-18 10:09:15 +00001153CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001154 EmptyShell Empty)
1155 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1156 // FIXME: Why do we allocate this?
1157 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1158 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001159}
1160
Nuno Lopes518e3702009-12-20 23:11:08 +00001161Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001162 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001163
1164 while (SubstNonTypeTemplateParmExpr *NTTP
1165 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1166 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1167 }
1168
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001169 // If we're calling a dereference, look at the pointer instead.
1170 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1171 if (BO->isPtrMemOp())
1172 CEE = BO->getRHS()->IgnoreParenCasts();
1173 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1174 if (UO->getOpcode() == UO_Deref)
1175 CEE = UO->getSubExpr()->IgnoreParenCasts();
1176 }
Chris Lattner52301912009-07-17 15:46:27 +00001177 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001178 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001179 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1180 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001181
1182 return 0;
1183}
1184
Nuno Lopes518e3702009-12-20 23:11:08 +00001185FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001186 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001187}
1188
Chris Lattnere4407ed2007-12-28 05:25:02 +00001189/// setNumArgs - This changes the number of arguments present in this call.
1190/// Any orphaned expressions are deleted by this, and any new operands are set
1191/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001192void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001193 // No change, just return.
1194 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattnere4407ed2007-12-28 05:25:02 +00001196 // If shrinking # arguments, just delete the extras and forgot them.
1197 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001198 this->NumArgs = NumArgs;
1199 return;
1200 }
1201
1202 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001203 unsigned NumPreArgs = getNumPreArgs();
1204 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001205 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001206 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001207 NewSubExprs[i] = SubExprs[i];
1208 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001209 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1210 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001211 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Douglas Gregorba6e5572009-04-17 21:46:47 +00001213 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001214 SubExprs = NewSubExprs;
1215 this->NumArgs = NumArgs;
1216}
1217
Chris Lattner01ff98a2008-10-06 05:00:53 +00001218/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1219/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +00001220unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001221 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001222 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001223 // ImplicitCastExpr.
1224 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1225 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001226 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001227
Steve Narofff6e3b3292008-01-31 01:07:12 +00001228 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1229 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001230 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001232 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1233 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001234 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001235
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001236 if (!FDecl->getIdentifier())
1237 return 0;
1238
Douglas Gregor15fc9562009-09-12 00:22:50 +00001239 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001240}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001241
Richard Smith5011a002013-01-17 23:46:04 +00001242bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1243 if (unsigned BI = isBuiltinCall())
1244 return Ctx.BuiltinInfo.isUnevaluated(BI);
1245 return false;
1246}
1247
Anders Carlsson00a27592009-05-26 04:57:27 +00001248QualType CallExpr::getCallReturnType() const {
1249 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001250 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001251 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001252 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001253 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001254 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1255 // This should never be overloaded and so should never return null.
1256 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001257
John McCall0009fcc2011-04-26 20:42:42 +00001258 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001259 return FnType->getResultType();
1260}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001261
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001262SourceLocation CallExpr::getLocStart() const {
1263 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001264 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001265
1266 SourceLocation begin = getCallee()->getLocStart();
1267 if (begin.isInvalid() && getNumArgs() > 0)
1268 begin = getArg(0)->getLocStart();
1269 return begin;
1270}
1271SourceLocation CallExpr::getLocEnd() const {
1272 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001273 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001274
1275 SourceLocation end = getRParenLoc();
1276 if (end.isInvalid() && getNumArgs() > 0)
1277 end = getArg(getNumArgs() - 1)->getLocEnd();
1278 return end;
1279}
John McCall701417a2011-02-21 06:23:05 +00001280
Craig Topper37932912013-08-18 10:09:15 +00001281OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001282 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001283 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001284 ArrayRef<OffsetOfNode> comps,
1285 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001286 SourceLocation RParenLoc) {
1287 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001288 sizeof(OffsetOfNode) * comps.size() +
1289 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001290
Benjamin Kramerc215e762012-08-24 11:54:20 +00001291 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1292 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001293}
1294
Craig Topper37932912013-08-18 10:09:15 +00001295OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001296 unsigned numComps, unsigned numExprs) {
1297 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1298 sizeof(OffsetOfNode) * numComps +
1299 sizeof(Expr*) * numExprs);
1300 return new (Mem) OffsetOfExpr(numComps, numExprs);
1301}
1302
Craig Topper37932912013-08-18 10:09:15 +00001303OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001304 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001305 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001306 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001307 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1308 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001309 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001310 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001311 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001312 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001313 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001314{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001315 for (unsigned i = 0; i != comps.size(); ++i) {
1316 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001317 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001318
Benjamin Kramerc215e762012-08-24 11:54:20 +00001319 for (unsigned i = 0; i != exprs.size(); ++i) {
1320 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001321 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001322 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001323 ExprBits.ContainsUnexpandedParameterPack = true;
1324
Benjamin Kramerc215e762012-08-24 11:54:20 +00001325 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001326 }
1327}
1328
1329IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1330 assert(getKind() == Field || getKind() == Identifier);
1331 if (getKind() == Field)
1332 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001333
Douglas Gregor882211c2010-04-28 22:16:22 +00001334 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1335}
1336
Craig Topper37932912013-08-18 10:09:15 +00001337MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001338 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001339 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001340 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001341 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001342 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001343 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001344 QualType ty,
1345 ExprValueKind vk,
1346 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001347 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001348
Douglas Gregorea972d32011-02-28 21:54:11 +00001349 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001350 founddecl.getDecl() != memberdecl ||
1351 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001352 if (hasQualOrFound)
1353 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001354
John McCall6b51f282009-11-23 01:53:49 +00001355 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001356 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1357 else if (TemplateKWLoc.isValid())
1358 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Chris Lattner5c0b4052010-10-30 05:14:06 +00001360 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001361 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1362 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001363
1364 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001365 // FIXME: Wrong. We should be looking at the member declaration we found.
1366 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001367 E->setValueDependent(true);
1368 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001369 E->setInstantiationDependent(true);
1370 }
1371 else if (QualifierLoc &&
1372 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1373 E->setInstantiationDependent(true);
1374
John McCall16df1e52010-03-30 21:47:33 +00001375 E->HasQualifierOrFoundDecl = true;
1376
1377 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001378 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001379 NQ->FoundDecl = founddecl;
1380 }
1381
Abramo Bagnara7945c982012-01-27 09:46:47 +00001382 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1383
John McCall16df1e52010-03-30 21:47:33 +00001384 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001385 bool Dependent = false;
1386 bool InstantiationDependent = false;
1387 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001388 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1389 Dependent,
1390 InstantiationDependent,
1391 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001392 if (InstantiationDependent)
1393 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001394 } else if (TemplateKWLoc.isValid()) {
1395 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001396 }
1397
1398 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001399}
1400
Daniel Dunbarb507f272012-03-09 15:39:15 +00001401SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001402 if (isImplicitAccess()) {
1403 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001404 return getQualifierLoc().getBeginLoc();
1405 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001406 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001407
Daniel Dunbarb507f272012-03-09 15:39:15 +00001408 // FIXME: We don't want this to happen. Rather, we should be able to
1409 // detect all kinds of implicit accesses more cleanly.
1410 SourceLocation BaseStartLoc = getBase()->getLocStart();
1411 if (BaseStartLoc.isValid())
1412 return BaseStartLoc;
1413 return MemberLoc;
1414}
1415SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001416 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001417 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001418 EndLoc = getRAngleLoc();
1419 else if (EndLoc.isInvalid())
1420 EndLoc = getBase()->getLocEnd();
1421 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001422}
1423
Alp Tokerc1086762013-12-07 13:51:35 +00001424bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001425 switch (getCastKind()) {
1426 case CK_DerivedToBase:
1427 case CK_UncheckedDerivedToBase:
1428 case CK_DerivedToBaseMemberPointer:
1429 case CK_BaseToDerived:
1430 case CK_BaseToDerivedMemberPointer:
1431 assert(!path_empty() && "Cast kind should have a base path!");
1432 break;
1433
1434 case CK_CPointerToObjCPointerCast:
1435 assert(getType()->isObjCObjectPointerType());
1436 assert(getSubExpr()->getType()->isPointerType());
1437 goto CheckNoBasePath;
1438
1439 case CK_BlockPointerToObjCPointerCast:
1440 assert(getType()->isObjCObjectPointerType());
1441 assert(getSubExpr()->getType()->isBlockPointerType());
1442 goto CheckNoBasePath;
1443
John McCallc62bb392012-02-15 01:22:51 +00001444 case CK_ReinterpretMemberPointer:
1445 assert(getType()->isMemberPointerType());
1446 assert(getSubExpr()->getType()->isMemberPointerType());
1447 goto CheckNoBasePath;
1448
John McCall9320b872011-09-09 05:25:32 +00001449 case CK_BitCast:
1450 // Arbitrary casts to C pointer types count as bitcasts.
1451 // Otherwise, we should only have block and ObjC pointer casts
1452 // here if they stay within the type kind.
1453 if (!getType()->isPointerType()) {
1454 assert(getType()->isObjCObjectPointerType() ==
1455 getSubExpr()->getType()->isObjCObjectPointerType());
1456 assert(getType()->isBlockPointerType() ==
1457 getSubExpr()->getType()->isBlockPointerType());
1458 }
1459 goto CheckNoBasePath;
1460
1461 case CK_AnyPointerToBlockPointerCast:
1462 assert(getType()->isBlockPointerType());
1463 assert(getSubExpr()->getType()->isAnyPointerType() &&
1464 !getSubExpr()->getType()->isBlockPointerType());
1465 goto CheckNoBasePath;
1466
Douglas Gregored90df32012-02-22 05:02:47 +00001467 case CK_CopyAndAutoreleaseBlockObject:
1468 assert(getType()->isBlockPointerType());
1469 assert(getSubExpr()->getType()->isBlockPointerType());
1470 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001471
1472 case CK_FunctionToPointerDecay:
1473 assert(getType()->isPointerType());
1474 assert(getSubExpr()->getType()->isFunctionType());
1475 goto CheckNoBasePath;
1476
David Tweede1468322013-12-11 13:39:46 +00001477 case CK_AddressSpaceConversion:
1478 assert(getType()->isPointerType());
1479 assert(getSubExpr()->getType()->isPointerType());
1480 assert(getType()->getPointeeType().getAddressSpace() !=
1481 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001482 // These should not have an inheritance path.
1483 case CK_Dynamic:
1484 case CK_ToUnion:
1485 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001486 case CK_NullToMemberPointer:
1487 case CK_NullToPointer:
1488 case CK_ConstructorConversion:
1489 case CK_IntegralToPointer:
1490 case CK_PointerToIntegral:
1491 case CK_ToVoid:
1492 case CK_VectorSplat:
1493 case CK_IntegralCast:
1494 case CK_IntegralToFloating:
1495 case CK_FloatingToIntegral:
1496 case CK_FloatingCast:
1497 case CK_ObjCObjectLValueCast:
1498 case CK_FloatingRealToComplex:
1499 case CK_FloatingComplexToReal:
1500 case CK_FloatingComplexCast:
1501 case CK_FloatingComplexToIntegralComplex:
1502 case CK_IntegralRealToComplex:
1503 case CK_IntegralComplexToReal:
1504 case CK_IntegralComplexCast:
1505 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001506 case CK_ARCProduceObject:
1507 case CK_ARCConsumeObject:
1508 case CK_ARCReclaimReturnedObject:
1509 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001510 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001511 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1512 goto CheckNoBasePath;
1513
1514 case CK_Dependent:
1515 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001516 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001517 case CK_AtomicToNonAtomic:
1518 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001519 case CK_PointerToBoolean:
1520 case CK_IntegralToBoolean:
1521 case CK_FloatingToBoolean:
1522 case CK_MemberPointerToBoolean:
1523 case CK_FloatingComplexToBoolean:
1524 case CK_IntegralComplexToBoolean:
1525 case CK_LValueBitCast: // -> bool&
1526 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001527 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001528 CheckNoBasePath:
1529 assert(path_empty() && "Cast kind should not have a base path!");
1530 break;
1531 }
Alp Tokerc1086762013-12-07 13:51:35 +00001532 return true;
John McCall9320b872011-09-09 05:25:32 +00001533}
1534
Anders Carlsson496335e2009-09-03 00:59:21 +00001535const char *CastExpr::getCastKindName() const {
1536 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001537 case CK_Dependent:
1538 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001539 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001540 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001541 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001542 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001543 case CK_LValueToRValue:
1544 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001545 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001546 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001547 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001548 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001549 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001550 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001551 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001552 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001553 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001554 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001555 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001556 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001557 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001558 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001559 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001560 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001561 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001562 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001563 case CK_NullToPointer:
1564 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001565 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001566 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001567 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001568 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001569 case CK_ReinterpretMemberPointer:
1570 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001571 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001572 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001573 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001574 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001575 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001576 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001577 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001578 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001579 case CK_PointerToBoolean:
1580 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001581 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001582 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001583 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001584 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001585 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001586 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001587 case CK_IntegralToBoolean:
1588 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001589 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001590 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001591 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001592 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001593 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001594 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001595 case CK_FloatingToBoolean:
1596 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001597 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001598 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001599 case CK_CPointerToObjCPointerCast:
1600 return "CPointerToObjCPointerCast";
1601 case CK_BlockPointerToObjCPointerCast:
1602 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001603 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001604 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001605 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001606 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001607 case CK_FloatingRealToComplex:
1608 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001609 case CK_FloatingComplexToReal:
1610 return "FloatingComplexToReal";
1611 case CK_FloatingComplexToBoolean:
1612 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001613 case CK_FloatingComplexCast:
1614 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001615 case CK_FloatingComplexToIntegralComplex:
1616 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001617 case CK_IntegralRealToComplex:
1618 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001619 case CK_IntegralComplexToReal:
1620 return "IntegralComplexToReal";
1621 case CK_IntegralComplexToBoolean:
1622 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001623 case CK_IntegralComplexCast:
1624 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001625 case CK_IntegralComplexToFloatingComplex:
1626 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001627 case CK_ARCConsumeObject:
1628 return "ARCConsumeObject";
1629 case CK_ARCProduceObject:
1630 return "ARCProduceObject";
1631 case CK_ARCReclaimReturnedObject:
1632 return "ARCReclaimReturnedObject";
1633 case CK_ARCExtendBlockObject:
1634 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001635 case CK_AtomicToNonAtomic:
1636 return "AtomicToNonAtomic";
1637 case CK_NonAtomicToAtomic:
1638 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001639 case CK_CopyAndAutoreleaseBlockObject:
1640 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001641 case CK_BuiltinFnToFnPtr:
1642 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001643 case CK_ZeroToOCLEvent:
1644 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001645 case CK_AddressSpaceConversion:
1646 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001647 }
Mike Stump11289f42009-09-09 15:08:12 +00001648
John McCallc5e62b42010-11-13 09:02:35 +00001649 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001650}
1651
Douglas Gregord196a582009-12-14 19:27:10 +00001652Expr *CastExpr::getSubExprAsWritten() {
1653 Expr *SubExpr = 0;
1654 CastExpr *E = this;
1655 do {
1656 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001657
1658 // Skip through reference binding to temporary.
1659 if (MaterializeTemporaryExpr *Materialize
1660 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1661 SubExpr = Materialize->GetTemporaryExpr();
1662
Douglas Gregord196a582009-12-14 19:27:10 +00001663 // Skip any temporary bindings; they're implicit.
1664 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1665 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001666
Douglas Gregord196a582009-12-14 19:27:10 +00001667 // Conversions by constructor and conversion functions have a
1668 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001669 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001670 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001671 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001672 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001673
Douglas Gregord196a582009-12-14 19:27:10 +00001674 // If the subexpression we're left with is an implicit cast, look
1675 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001676 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1677
Douglas Gregord196a582009-12-14 19:27:10 +00001678 return SubExpr;
1679}
1680
John McCallcf142162010-08-07 06:22:56 +00001681CXXBaseSpecifier **CastExpr::path_buffer() {
1682 switch (getStmtClass()) {
1683#define ABSTRACT_STMT(x)
1684#define CASTEXPR(Type, Base) \
1685 case Stmt::Type##Class: \
1686 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1687#define STMT(Type, Base)
1688#include "clang/AST/StmtNodes.inc"
1689 default:
1690 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001691 }
1692}
1693
1694void CastExpr::setCastPath(const CXXCastPath &Path) {
1695 assert(Path.size() == path_size());
1696 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1697}
1698
Craig Topper37932912013-08-18 10:09:15 +00001699ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001700 CastKind Kind, Expr *Operand,
1701 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001702 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001703 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1704 void *Buffer =
1705 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1706 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001707 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001708 if (PathSize) E->setCastPath(*BasePath);
1709 return E;
1710}
1711
Craig Topper37932912013-08-18 10:09:15 +00001712ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001713 unsigned PathSize) {
1714 void *Buffer =
1715 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1716 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1717}
1718
1719
Craig Topper37932912013-08-18 10:09:15 +00001720CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001721 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001722 const CXXCastPath *BasePath,
1723 TypeSourceInfo *WrittenTy,
1724 SourceLocation L, SourceLocation R) {
1725 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1726 void *Buffer =
1727 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1728 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001729 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001730 if (PathSize) E->setCastPath(*BasePath);
1731 return E;
1732}
1733
Craig Topper37932912013-08-18 10:09:15 +00001734CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1735 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001736 void *Buffer =
1737 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1738 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1739}
1740
Chris Lattner1b926492006-08-23 06:42:10 +00001741/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1742/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001743StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001744 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001745 case BO_PtrMemD: return ".*";
1746 case BO_PtrMemI: return "->*";
1747 case BO_Mul: return "*";
1748 case BO_Div: return "/";
1749 case BO_Rem: return "%";
1750 case BO_Add: return "+";
1751 case BO_Sub: return "-";
1752 case BO_Shl: return "<<";
1753 case BO_Shr: return ">>";
1754 case BO_LT: return "<";
1755 case BO_GT: return ">";
1756 case BO_LE: return "<=";
1757 case BO_GE: return ">=";
1758 case BO_EQ: return "==";
1759 case BO_NE: return "!=";
1760 case BO_And: return "&";
1761 case BO_Xor: return "^";
1762 case BO_Or: return "|";
1763 case BO_LAnd: return "&&";
1764 case BO_LOr: return "||";
1765 case BO_Assign: return "=";
1766 case BO_MulAssign: return "*=";
1767 case BO_DivAssign: return "/=";
1768 case BO_RemAssign: return "%=";
1769 case BO_AddAssign: return "+=";
1770 case BO_SubAssign: return "-=";
1771 case BO_ShlAssign: return "<<=";
1772 case BO_ShrAssign: return ">>=";
1773 case BO_AndAssign: return "&=";
1774 case BO_XorAssign: return "^=";
1775 case BO_OrAssign: return "|=";
1776 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001777 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001778
David Blaikiee4d798f2012-01-20 21:50:17 +00001779 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001780}
Steve Naroff47500512007-04-19 23:00:49 +00001781
John McCalle3027922010-08-25 11:45:40 +00001782BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001783BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1784 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001785 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001786 case OO_Plus: return BO_Add;
1787 case OO_Minus: return BO_Sub;
1788 case OO_Star: return BO_Mul;
1789 case OO_Slash: return BO_Div;
1790 case OO_Percent: return BO_Rem;
1791 case OO_Caret: return BO_Xor;
1792 case OO_Amp: return BO_And;
1793 case OO_Pipe: return BO_Or;
1794 case OO_Equal: return BO_Assign;
1795 case OO_Less: return BO_LT;
1796 case OO_Greater: return BO_GT;
1797 case OO_PlusEqual: return BO_AddAssign;
1798 case OO_MinusEqual: return BO_SubAssign;
1799 case OO_StarEqual: return BO_MulAssign;
1800 case OO_SlashEqual: return BO_DivAssign;
1801 case OO_PercentEqual: return BO_RemAssign;
1802 case OO_CaretEqual: return BO_XorAssign;
1803 case OO_AmpEqual: return BO_AndAssign;
1804 case OO_PipeEqual: return BO_OrAssign;
1805 case OO_LessLess: return BO_Shl;
1806 case OO_GreaterGreater: return BO_Shr;
1807 case OO_LessLessEqual: return BO_ShlAssign;
1808 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1809 case OO_EqualEqual: return BO_EQ;
1810 case OO_ExclaimEqual: return BO_NE;
1811 case OO_LessEqual: return BO_LE;
1812 case OO_GreaterEqual: return BO_GE;
1813 case OO_AmpAmp: return BO_LAnd;
1814 case OO_PipePipe: return BO_LOr;
1815 case OO_Comma: return BO_Comma;
1816 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001817 }
1818}
1819
1820OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1821 static const OverloadedOperatorKind OverOps[] = {
1822 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1823 OO_Star, OO_Slash, OO_Percent,
1824 OO_Plus, OO_Minus,
1825 OO_LessLess, OO_GreaterGreater,
1826 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1827 OO_EqualEqual, OO_ExclaimEqual,
1828 OO_Amp,
1829 OO_Caret,
1830 OO_Pipe,
1831 OO_AmpAmp,
1832 OO_PipePipe,
1833 OO_Equal, OO_StarEqual,
1834 OO_SlashEqual, OO_PercentEqual,
1835 OO_PlusEqual, OO_MinusEqual,
1836 OO_LessLessEqual, OO_GreaterGreaterEqual,
1837 OO_AmpEqual, OO_CaretEqual,
1838 OO_PipeEqual,
1839 OO_Comma
1840 };
1841 return OverOps[Opc];
1842}
1843
Craig Topper37932912013-08-18 10:09:15 +00001844InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001845 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001846 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001847 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001848 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001849 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001850{
1851 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001852 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001853 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001854 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001855 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001856 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001857 if (initExprs[I]->isInstantiationDependent())
1858 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001859 if (initExprs[I]->containsUnexpandedParameterPack())
1860 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001861 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001862
Benjamin Kramerc215e762012-08-24 11:54:20 +00001863 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001864}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001865
Craig Topper37932912013-08-18 10:09:15 +00001866void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001867 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001868 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001869}
1870
Craig Topper37932912013-08-18 10:09:15 +00001871void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001872 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001873}
1874
Craig Topper37932912013-08-18 10:09:15 +00001875Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001876 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001877 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Richard Smithc275da62013-12-06 01:27:24 +00001878 setInit(Init, expr);
Ted Kremenek013041e2010-02-19 01:50:18 +00001879 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001880 }
Mike Stump11289f42009-09-09 15:08:12 +00001881
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001882 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001883 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001884 return Result;
1885}
1886
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001887void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001888 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001889 ArrayFillerOrUnionFieldInit = filler;
1890 // Fill out any "holes" in the array due to designated initializers.
1891 Expr **inits = getInits();
1892 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1893 if (inits[i] == 0)
1894 inits[i] = filler;
1895}
1896
Richard Smith9ec1e482012-04-15 02:50:59 +00001897bool InitListExpr::isStringLiteralInit() const {
1898 if (getNumInits() != 1)
1899 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001900 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1901 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001902 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001903 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001904 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1905}
1906
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001907SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001908 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001909 return SyntacticForm->getLocStart();
1910 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001911 if (Beg.isInvalid()) {
1912 // Find the first non-null initializer.
1913 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1914 E = InitExprs.end();
1915 I != E; ++I) {
1916 if (Stmt *S = *I) {
1917 Beg = S->getLocStart();
1918 break;
1919 }
1920 }
1921 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001922 return Beg;
1923}
1924
1925SourceLocation InitListExpr::getLocEnd() const {
1926 if (InitListExpr *SyntacticForm = getSyntacticForm())
1927 return SyntacticForm->getLocEnd();
1928 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001929 if (End.isInvalid()) {
1930 // Find the first non-null initializer from the end.
1931 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001932 E = InitExprs.rend();
1933 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001934 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001935 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001936 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001937 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001938 }
1939 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001940 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001941}
1942
Steve Naroff991e99d2008-09-04 15:31:07 +00001943/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001944///
John McCallc833dea2012-02-17 03:32:35 +00001945const FunctionProtoType *BlockExpr::getFunctionType() const {
1946 // The block pointer is never sugared, but the function type might be.
1947 return cast<BlockPointerType>(getType())
1948 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001949}
1950
Mike Stump11289f42009-09-09 15:08:12 +00001951SourceLocation BlockExpr::getCaretLocation() const {
1952 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001953}
Mike Stump11289f42009-09-09 15:08:12 +00001954const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001955 return TheBlock->getBody();
1956}
Mike Stump11289f42009-09-09 15:08:12 +00001957Stmt *BlockExpr::getBody() {
1958 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001959}
Steve Naroff415d3d52008-10-08 17:01:13 +00001960
1961
Chris Lattner1ec5f562007-06-27 05:38:08 +00001962//===----------------------------------------------------------------------===//
1963// Generic Expression Routines
1964//===----------------------------------------------------------------------===//
1965
Chris Lattner237f2752009-02-14 07:37:35 +00001966/// isUnusedResultAWarning - Return true if this immediate expression should
1967/// be warned about if the result is unused. If so, fill in Loc and Ranges
1968/// with location to warn on and the source range[s] to report with the
1969/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001970bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1971 SourceRange &R1, SourceRange &R2,
1972 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001973 // Don't warn if the expr is type dependent. The type could end up
1974 // instantiating to void.
1975 if (isTypeDependent())
1976 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001977
Chris Lattner1ec5f562007-06-27 05:38:08 +00001978 switch (getStmtClass()) {
1979 default:
John McCallc493a732010-03-12 07:11:26 +00001980 if (getType()->isVoidType())
1981 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001982 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001983 Loc = getExprLoc();
1984 R1 = getSourceRange();
1985 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001986 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001987 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001988 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001989 case GenericSelectionExprClass:
1990 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001991 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00001992 case ChooseExprClass:
1993 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1994 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001995 case UnaryOperatorClass: {
1996 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001997
Chris Lattner1ec5f562007-06-27 05:38:08 +00001998 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001999 case UO_Plus:
2000 case UO_Minus:
2001 case UO_AddrOf:
2002 case UO_Not:
2003 case UO_LNot:
2004 case UO_Deref:
2005 break;
John McCalle3027922010-08-25 11:45:40 +00002006 case UO_PostInc:
2007 case UO_PostDec:
2008 case UO_PreInc:
2009 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00002010 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00002011 case UO_Real:
2012 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00002013 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00002014 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
2015 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00002016 return false;
2017 break;
John McCalle3027922010-08-25 11:45:40 +00002018 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002019 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002020 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002021 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002022 Loc = UO->getOperatorLoc();
2023 R1 = UO->getSubExpr()->getSourceRange();
2024 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002025 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002026 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002027 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002028 switch (BO->getOpcode()) {
2029 default:
2030 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002031 // Consider the RHS of comma for side effects. LHS was checked by
2032 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002033 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002034 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2035 // lvalue-ness) of an assignment written in a macro.
2036 if (IntegerLiteral *IE =
2037 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2038 if (IE->getValue() == 0)
2039 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002040 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002041 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002042 case BO_LAnd:
2043 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002044 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2045 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002046 return false;
2047 break;
John McCall1e3715a2010-02-16 04:10:53 +00002048 }
Chris Lattner237f2752009-02-14 07:37:35 +00002049 if (BO->isAssignmentOp())
2050 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002051 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002052 Loc = BO->getOperatorLoc();
2053 R1 = BO->getLHS()->getSourceRange();
2054 R2 = BO->getRHS()->getSourceRange();
2055 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002056 }
Chris Lattner86928112007-08-25 02:00:02 +00002057 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002058 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002059 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002060 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002061
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002062 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002063 // If only one of the LHS or RHS is a warning, the operator might
2064 // be being used for control flow. Only warn if both the LHS and
2065 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002066 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002067 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002068 return false;
2069 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002070 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002071 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002072 }
2073
Chris Lattnera44d1162007-06-27 05:58:59 +00002074 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002075 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002076 Loc = cast<MemberExpr>(this)->getMemberLoc();
2077 R1 = SourceRange(Loc, Loc);
2078 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2079 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002080
Chris Lattner1ec5f562007-06-27 05:38:08 +00002081 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002082 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002083 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2084 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2085 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2086 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002087
Chandler Carruth46339472011-08-17 09:49:44 +00002088 case CXXOperatorCallExprClass: {
2089 // We warn about operator== and operator!= even when user-defined operator
2090 // overloads as there is no reasonable way to define these such that they
2091 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2092 // warning: these operators are commonly typo'ed, and so warning on them
2093 // provides additional value as well. If this list is updated,
2094 // DiagnoseUnusedComparison should be as well.
2095 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2096 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002097 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002098 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002099 Loc = Op->getOperatorLoc();
2100 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002101 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002102 }
Chandler Carruth46339472011-08-17 09:49:44 +00002103
2104 // Fallthrough for generic call handling.
2105 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002106 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002107 case CXXMemberCallExprClass:
2108 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002109 // If this is a direct call, get the callee.
2110 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002111 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002112 // If the callee has attribute pure, const, or warn_unused_result, warn
2113 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002114 //
2115 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2116 // updated to match for QoI.
2117 if (FD->getAttr<WarnUnusedResultAttr>() ||
2118 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002119 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002120 Loc = CE->getCallee()->getLocStart();
2121 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002122
Chris Lattner1a6babf2009-10-13 04:53:48 +00002123 if (unsigned NumArgs = CE->getNumArgs())
2124 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2125 CE->getArg(NumArgs-1)->getLocEnd());
2126 return true;
2127 }
Chris Lattner237f2752009-02-14 07:37:35 +00002128 }
2129 return false;
2130 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002131
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002132 // If we don't know precisely what we're looking at, let's not warn.
2133 case UnresolvedLookupExprClass:
2134 case CXXUnresolvedConstructExprClass:
2135 return false;
2136
Anders Carlsson6aa50392009-11-17 17:11:23 +00002137 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002138 case CXXConstructExprClass: {
2139 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2140 if (Type->hasAttr<WarnUnusedAttr>()) {
2141 WarnE = this;
2142 Loc = getLocStart();
2143 R1 = getSourceRange();
2144 return true;
2145 }
2146 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002147 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002148 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002149
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002150 case ObjCMessageExprClass: {
2151 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002152 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002153 ME->isInstanceMessage() &&
2154 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002155 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002156 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002157 Loc = getExprLoc();
2158 R1 = ME->getSourceRange();
2159 return true;
2160 }
2161
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002162 const ObjCMethodDecl *MD = ME->getMethodDecl();
2163 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002164 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002165 Loc = getExprLoc();
2166 return true;
2167 }
Chris Lattner237f2752009-02-14 07:37:35 +00002168 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002169 }
Mike Stump11289f42009-09-09 15:08:12 +00002170
John McCallb7bd14f2010-12-02 01:19:52 +00002171 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002172 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002173 Loc = getExprLoc();
2174 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002175 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002176
John McCallfe96e0b2011-11-06 09:01:30 +00002177 case PseudoObjectExprClass: {
2178 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2179
2180 // Only complain about things that have the form of a getter.
2181 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2182 isa<BinaryOperator>(PO->getSyntacticForm()))
2183 return false;
2184
Eli Friedmanc11535c2012-05-24 00:47:05 +00002185 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002186 Loc = getExprLoc();
2187 R1 = getSourceRange();
2188 return true;
2189 }
2190
Chris Lattner944d3062008-07-26 19:51:01 +00002191 case StmtExprClass: {
2192 // Statement exprs don't logically have side effects themselves, but are
2193 // sometimes used in macros in ways that give them a type that is unused.
2194 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2195 // however, if the result of the stmt expr is dead, we don't want to emit a
2196 // warning.
2197 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002198 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002199 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002200 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002201 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2202 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002203 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002204 }
Mike Stump11289f42009-09-09 15:08:12 +00002205
John McCallc493a732010-03-12 07:11:26 +00002206 if (getType()->isVoidType())
2207 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002208 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002209 Loc = cast<StmtExpr>(this)->getLParenLoc();
2210 R1 = getSourceRange();
2211 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002212 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002213 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002214 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002215 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002216 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002217 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002218 if (CE->getCastKind() == CK_ToVoid) {
2219 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002220 CE->getSubExpr()->getType().isVolatileQualified()) {
2221 const DeclRefExpr *DRE =
2222 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2223 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2224 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2225 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2226 R1, R2, Ctx);
2227 }
2228 }
Chris Lattner2706a552009-07-28 18:25:28 +00002229 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002230 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002231
Eli Friedmanc11535c2012-05-24 00:47:05 +00002232 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002233 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002234 if (CE->getCastKind() == CK_ConstructorConversion)
2235 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002236
Eli Friedmanc11535c2012-05-24 00:47:05 +00002237 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002238 if (const CXXFunctionalCastExpr *CXXCE =
2239 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002240 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002241 R1 = CXXCE->getSubExpr()->getSourceRange();
2242 } else {
2243 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2244 Loc = CStyleCE->getLParenLoc();
2245 R1 = CStyleCE->getSubExpr()->getSourceRange();
2246 }
Chris Lattner237f2752009-02-14 07:37:35 +00002247 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002248 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002249 case ImplicitCastExprClass: {
2250 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002251
Eli Friedmanc11535c2012-05-24 00:47:05 +00002252 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2253 if (ICE->getCastKind() == CK_LValueToRValue &&
2254 ICE->getSubExpr()->getType().isVolatileQualified())
2255 return false;
2256
2257 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2258 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002259 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002260 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002261 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002262 case CXXDefaultInitExprClass:
2263 return (cast<CXXDefaultInitExpr>(this)
2264 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002265
2266 case CXXNewExprClass:
2267 // FIXME: In theory, there might be new expressions that don't have side
2268 // effects (e.g. a placement new with an uninitialized POD).
2269 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002270 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002271 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002272 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002273 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002274 case ExprWithCleanupsClass:
2275 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002276 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002277 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002278}
2279
Fariborz Jahanian07735332009-02-22 18:40:18 +00002280/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002281/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002282bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002283 const Expr *E = IgnoreParens();
2284 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002285 default:
2286 return false;
2287 case ObjCIvarRefExprClass:
2288 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002289 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002290 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002291 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002292 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002293 case MaterializeTemporaryExprClass:
2294 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2295 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002296 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002297 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002298 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002299 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002300
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002301 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2302 if (VD->hasGlobalStorage())
2303 return true;
2304 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002305 // dereferencing to a pointer is always a gc'able candidate,
2306 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002307 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002308 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002309 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002310 return false;
2311 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002312 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002313 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002314 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002315 }
2316 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002317 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002318 }
2319}
Sebastian Redlce354af2010-09-10 20:55:33 +00002320
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002321bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2322 if (isTypeDependent())
2323 return false;
John McCall086a4642010-11-24 05:12:34 +00002324 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002325}
2326
John McCall0009fcc2011-04-26 20:42:42 +00002327QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002328 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002329
2330 // Bound member expressions are always one of these possibilities:
2331 // x->m x.m x->*y x.*y
2332 // (possibly parenthesized)
2333
2334 expr = expr->IgnoreParens();
2335 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2336 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2337 return mem->getMemberDecl()->getType();
2338 }
2339
2340 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2341 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2342 ->getPointeeType();
2343 assert(type->isFunctionType());
2344 return type;
2345 }
2346
2347 assert(isa<UnresolvedMemberExpr>(expr));
2348 return QualType();
2349}
2350
Ted Kremenekfff70962008-01-17 16:57:34 +00002351Expr* Expr::IgnoreParens() {
2352 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002353 while (true) {
2354 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2355 E = P->getSubExpr();
2356 continue;
2357 }
2358 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2359 if (P->getOpcode() == UO_Extension) {
2360 E = P->getSubExpr();
2361 continue;
2362 }
2363 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002364 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2365 if (!P->isResultDependent()) {
2366 E = P->getResultExpr();
2367 continue;
2368 }
2369 }
Eli Friedman75807f22013-07-20 00:40:58 +00002370 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2371 if (!P->isConditionDependent()) {
2372 E = P->getChosenSubExpr();
2373 continue;
2374 }
2375 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002376 return E;
2377 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002378}
2379
Chris Lattnerf2660962008-02-13 01:02:39 +00002380/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2381/// or CastExprs or ImplicitCastExprs, returning their operand.
2382Expr *Expr::IgnoreParenCasts() {
2383 Expr *E = this;
2384 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002385 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002386 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002387 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002388 continue;
2389 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002390 if (MaterializeTemporaryExpr *Materialize
2391 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2392 E = Materialize->GetTemporaryExpr();
2393 continue;
2394 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002395 if (SubstNonTypeTemplateParmExpr *NTTP
2396 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2397 E = NTTP->getReplacement();
2398 continue;
2399 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002400 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002401 }
2402}
2403
John McCall5a4ce8b2010-12-04 08:24:19 +00002404/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2405/// casts. This is intended purely as a temporary workaround for code
2406/// that hasn't yet been rewritten to do the right thing about those
2407/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002408Expr *Expr::IgnoreParenLValueCasts() {
2409 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002410 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002411 E = E->IgnoreParens();
2412 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002413 if (P->getCastKind() == CK_LValueToRValue) {
2414 E = P->getSubExpr();
2415 continue;
2416 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002417 } else if (MaterializeTemporaryExpr *Materialize
2418 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2419 E = Materialize->GetTemporaryExpr();
2420 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002421 } else if (SubstNonTypeTemplateParmExpr *NTTP
2422 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2423 E = NTTP->getReplacement();
2424 continue;
John McCall34376a62010-12-04 03:47:34 +00002425 }
2426 break;
2427 }
2428 return E;
2429}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002430
2431Expr *Expr::ignoreParenBaseCasts() {
2432 Expr *E = this;
2433 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002434 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002435 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2436 if (CE->getCastKind() == CK_DerivedToBase ||
2437 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2438 CE->getCastKind() == CK_NoOp) {
2439 E = CE->getSubExpr();
2440 continue;
2441 }
2442 }
2443
2444 return E;
2445 }
2446}
2447
John McCalleebc8322010-05-05 22:59:52 +00002448Expr *Expr::IgnoreParenImpCasts() {
2449 Expr *E = this;
2450 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002451 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002452 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002453 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002454 continue;
2455 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002456 if (MaterializeTemporaryExpr *Materialize
2457 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2458 E = Materialize->GetTemporaryExpr();
2459 continue;
2460 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002461 if (SubstNonTypeTemplateParmExpr *NTTP
2462 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2463 E = NTTP->getReplacement();
2464 continue;
2465 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002466 return E;
John McCalleebc8322010-05-05 22:59:52 +00002467 }
2468}
2469
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002470Expr *Expr::IgnoreConversionOperator() {
2471 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002472 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002473 return MCE->getImplicitObjectArgument();
2474 }
2475 return this;
2476}
2477
Chris Lattneref26c772009-03-13 17:28:01 +00002478/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2479/// value (including ptr->int casts of the same size). Strip off any
2480/// ParenExpr or CastExprs, returning their operand.
2481Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2482 Expr *E = this;
2483 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002484 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002485
Chris Lattneref26c772009-03-13 17:28:01 +00002486 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2487 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002488 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002489 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002490
Chris Lattneref26c772009-03-13 17:28:01 +00002491 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2492 E = SE;
2493 continue;
2494 }
Mike Stump11289f42009-09-09 15:08:12 +00002495
Abramo Bagnara932e3932010-10-15 07:51:18 +00002496 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002497 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002498 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002499 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002500 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2501 E = SE;
2502 continue;
2503 }
2504 }
Mike Stump11289f42009-09-09 15:08:12 +00002505
Douglas Gregor6a40b082011-09-08 17:56:33 +00002506 if (SubstNonTypeTemplateParmExpr *NTTP
2507 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2508 E = NTTP->getReplacement();
2509 continue;
2510 }
2511
Chris Lattneref26c772009-03-13 17:28:01 +00002512 return E;
2513 }
2514}
2515
Douglas Gregord196a582009-12-14 19:27:10 +00002516bool Expr::isDefaultArgument() const {
2517 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002518 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2519 E = M->GetTemporaryExpr();
2520
Douglas Gregord196a582009-12-14 19:27:10 +00002521 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2522 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002523
Douglas Gregord196a582009-12-14 19:27:10 +00002524 return isa<CXXDefaultArgExpr>(E);
2525}
Chris Lattneref26c772009-03-13 17:28:01 +00002526
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002527/// \brief Skip over any no-op casts and any temporary-binding
2528/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002529static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002530 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2531 E = M->GetTemporaryExpr();
2532
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002533 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002534 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002535 E = ICE->getSubExpr();
2536 else
2537 break;
2538 }
2539
2540 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2541 E = BE->getSubExpr();
2542
2543 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002544 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002545 E = ICE->getSubExpr();
2546 else
2547 break;
2548 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002549
2550 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002551}
2552
John McCall7a626f62010-09-15 10:14:12 +00002553/// isTemporaryObject - Determines if this expression produces a
2554/// temporary of the given class type.
2555bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2556 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2557 return false;
2558
Anders Carlsson66bbf502010-11-28 16:40:49 +00002559 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002560
John McCall02dc8c72010-09-15 20:59:13 +00002561 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002562 if (!E->Classify(C).isPRValue()) {
2563 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002564 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002565 return false;
2566 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002567
John McCallf4ee1dd2010-09-16 06:57:56 +00002568 // Black-list a few cases which yield pr-values of class type that don't
2569 // refer to temporaries of that type:
2570
2571 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002572 if (isa<ImplicitCastExpr>(E)) {
2573 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2574 case CK_DerivedToBase:
2575 case CK_UncheckedDerivedToBase:
2576 return false;
2577 default:
2578 break;
2579 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002580 }
2581
John McCallf4ee1dd2010-09-16 06:57:56 +00002582 // - member expressions (all)
2583 if (isa<MemberExpr>(E))
2584 return false;
2585
Eli Friedman13ffdd82012-06-15 23:51:06 +00002586 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2587 if (BO->isPtrMemOp())
2588 return false;
2589
John McCallc07a0c72011-02-17 10:25:35 +00002590 // - opaque values (all)
2591 if (isa<OpaqueValueExpr>(E))
2592 return false;
2593
John McCall7a626f62010-09-15 10:14:12 +00002594 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002595}
2596
Douglas Gregor25b7e052011-03-02 21:06:53 +00002597bool Expr::isImplicitCXXThis() const {
2598 const Expr *E = this;
2599
2600 // Strip away parentheses and casts we don't care about.
2601 while (true) {
2602 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2603 E = Paren->getSubExpr();
2604 continue;
2605 }
2606
2607 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2608 if (ICE->getCastKind() == CK_NoOp ||
2609 ICE->getCastKind() == CK_LValueToRValue ||
2610 ICE->getCastKind() == CK_DerivedToBase ||
2611 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2612 E = ICE->getSubExpr();
2613 continue;
2614 }
2615 }
2616
2617 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2618 if (UnOp->getOpcode() == UO_Extension) {
2619 E = UnOp->getSubExpr();
2620 continue;
2621 }
2622 }
2623
Douglas Gregorfe314812011-06-21 17:03:29 +00002624 if (const MaterializeTemporaryExpr *M
2625 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2626 E = M->GetTemporaryExpr();
2627 continue;
2628 }
2629
Douglas Gregor25b7e052011-03-02 21:06:53 +00002630 break;
2631 }
2632
2633 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2634 return This->isImplicit();
2635
2636 return false;
2637}
2638
Douglas Gregor4619e432008-12-05 23:32:09 +00002639/// hasAnyTypeDependentArguments - Determines if any of the expressions
2640/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002641bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002642 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002643 if (Exprs[I]->isTypeDependent())
2644 return true;
2645
2646 return false;
2647}
2648
John McCall8b0f4ff2010-08-02 21:13:48 +00002649bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002650 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002651 // which can be evaluated at compile-time. It very closely parallels
2652 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2653 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2654 // to isEvaluatable most of the time.
2655 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002656 // If we ever capture reference-binding directly in the AST, we can
2657 // kill the second parameter.
2658
2659 if (IsForRef) {
2660 EvalResult Result;
2661 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2662 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002663
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002664 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002665 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002666 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002667 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002668 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002669 case CXXTemporaryObjectExprClass:
2670 case CXXConstructExprClass: {
2671 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002672
Eli Friedman4c27ac22013-07-16 22:40:53 +00002673 if (CE->getConstructor()->isTrivial() &&
2674 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2675 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002676 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002677
Eli Friedman4c27ac22013-07-16 22:40:53 +00002678 // Trivial copy constructor
2679 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2680 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002681 }
2682
Richard Smithd62306a2011-11-10 06:34:14 +00002683 break;
John McCall81c9cea2010-08-01 21:51:45 +00002684 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002685 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002686 // This handles gcc's extension that allows global initializers like
2687 // "struct x {int x;} x = (struct x) {};".
2688 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002689 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002690 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002691 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002692 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002693 const InitListExpr *ILE = cast<InitListExpr>(this);
2694 if (ILE->getType()->isArrayType()) {
2695 unsigned numInits = ILE->getNumInits();
2696 for (unsigned i = 0; i < numInits; i++) {
2697 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2698 return false;
2699 }
2700 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002701 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002702
2703 if (ILE->getType()->isRecordType()) {
2704 unsigned ElementNo = 0;
2705 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2706 for (RecordDecl::field_iterator Field = RD->field_begin(),
2707 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2708 // If this is a union, skip all the fields that aren't being initialized.
2709 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2710 continue;
2711
2712 // Don't emit anonymous bitfields, they just affect layout.
2713 if (Field->isUnnamedBitfield())
2714 continue;
2715
2716 if (ElementNo < ILE->getNumInits()) {
2717 const Expr *Elt = ILE->getInit(ElementNo++);
2718 if (Field->isBitField()) {
2719 // Bitfields have to evaluate to an integer.
2720 llvm::APSInt ResultTmp;
2721 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2722 return false;
2723 } else {
2724 bool RefType = Field->getType()->isReferenceType();
2725 if (!Elt->isConstantInitializer(Ctx, RefType))
2726 return false;
2727 }
2728 }
2729 }
2730 return true;
2731 }
2732
2733 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002734 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002735 case ImplicitValueInitExprClass:
2736 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002737 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002738 return cast<ParenExpr>(this)->getSubExpr()
2739 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002740 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002741 return cast<GenericSelectionExpr>(this)->getResultExpr()
2742 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002743 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002744 if (cast<ChooseExpr>(this)->isConditionDependent())
2745 return false;
2746 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002747 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002748 case UnaryOperatorClass: {
2749 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002750 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002751 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002752 break;
2753 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002754 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002755 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002756 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002757 case CStyleCastExprClass:
2758 case ObjCBridgedCastExprClass:
2759 case CXXDynamicCastExprClass:
2760 case CXXReinterpretCastExprClass:
2761 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002762 const CastExpr *CE = cast<CastExpr>(this);
2763
Eli Friedman13ec75b2011-12-21 00:43:02 +00002764 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002765 if (CE->getCastKind() == CK_NoOp ||
2766 CE->getCastKind() == CK_LValueToRValue ||
2767 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002768 CE->getCastKind() == CK_ConstructorConversion ||
2769 CE->getCastKind() == CK_NonAtomicToAtomic ||
2770 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002771 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2772
Eli Friedman384da272009-01-25 03:12:18 +00002773 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002774 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002775 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002776 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002777 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002778
2779 case SubstNonTypeTemplateParmExprClass:
2780 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2781 ->isConstantInitializer(Ctx, false);
2782 case CXXDefaultArgExprClass:
2783 return cast<CXXDefaultArgExpr>(this)->getExpr()
2784 ->isConstantInitializer(Ctx, false);
2785 case CXXDefaultInitExprClass:
2786 return cast<CXXDefaultInitExpr>(this)->getExpr()
2787 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002788 }
Eli Friedman384da272009-01-25 03:12:18 +00002789 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002790}
2791
Richard Smith0421ce72012-08-07 04:16:51 +00002792bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2793 if (isInstantiationDependent())
2794 return true;
2795
2796 switch (getStmtClass()) {
2797 case NoStmtClass:
2798 #define ABSTRACT_STMT(Type)
2799 #define STMT(Type, Base) case Type##Class:
2800 #define EXPR(Type, Base)
2801 #include "clang/AST/StmtNodes.inc"
2802 llvm_unreachable("unexpected Expr kind");
2803
2804 case DependentScopeDeclRefExprClass:
2805 case CXXUnresolvedConstructExprClass:
2806 case CXXDependentScopeMemberExprClass:
2807 case UnresolvedLookupExprClass:
2808 case UnresolvedMemberExprClass:
2809 case PackExpansionExprClass:
2810 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002811 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002812 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2813
Richard Smitha33e4fe2012-08-07 05:18:29 +00002814 case DeclRefExprClass:
2815 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002816 case PredefinedExprClass:
2817 case IntegerLiteralClass:
2818 case FloatingLiteralClass:
2819 case ImaginaryLiteralClass:
2820 case StringLiteralClass:
2821 case CharacterLiteralClass:
2822 case OffsetOfExprClass:
2823 case ImplicitValueInitExprClass:
2824 case UnaryExprOrTypeTraitExprClass:
2825 case AddrLabelExprClass:
2826 case GNUNullExprClass:
2827 case CXXBoolLiteralExprClass:
2828 case CXXNullPtrLiteralExprClass:
2829 case CXXThisExprClass:
2830 case CXXScalarValueInitExprClass:
2831 case TypeTraitExprClass:
2832 case UnaryTypeTraitExprClass:
2833 case BinaryTypeTraitExprClass:
2834 case ArrayTypeTraitExprClass:
2835 case ExpressionTraitExprClass:
2836 case CXXNoexceptExprClass:
2837 case SizeOfPackExprClass:
2838 case ObjCStringLiteralClass:
2839 case ObjCEncodeExprClass:
2840 case ObjCBoolLiteralExprClass:
2841 case CXXUuidofExprClass:
2842 case OpaqueValueExprClass:
2843 // These never have a side-effect.
2844 return false;
2845
2846 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002847 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002848 case CompoundAssignOperatorClass:
2849 case VAArgExprClass:
2850 case AtomicExprClass:
2851 case StmtExprClass:
2852 case CXXOperatorCallExprClass:
2853 case CXXMemberCallExprClass:
2854 case UserDefinedLiteralClass:
2855 case CXXThrowExprClass:
2856 case CXXNewExprClass:
2857 case CXXDeleteExprClass:
2858 case ExprWithCleanupsClass:
2859 case CXXBindTemporaryExprClass:
2860 case BlockExprClass:
2861 case CUDAKernelCallExprClass:
2862 // These always have a side-effect.
2863 return true;
2864
2865 case ParenExprClass:
2866 case ArraySubscriptExprClass:
2867 case MemberExprClass:
2868 case ConditionalOperatorClass:
2869 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002870 case CompoundLiteralExprClass:
2871 case ExtVectorElementExprClass:
2872 case DesignatedInitExprClass:
2873 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002874 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002875 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002876 case SubstNonTypeTemplateParmExprClass:
2877 case MaterializeTemporaryExprClass:
2878 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002879 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002880 case AsTypeExprClass:
2881 // These have a side-effect if any subexpression does.
2882 break;
2883
Richard Smitha33e4fe2012-08-07 05:18:29 +00002884 case UnaryOperatorClass:
2885 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002886 return true;
2887 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002888
2889 case BinaryOperatorClass:
2890 if (cast<BinaryOperator>(this)->isAssignmentOp())
2891 return true;
2892 break;
2893
Richard Smith0421ce72012-08-07 04:16:51 +00002894 case InitListExprClass:
2895 // FIXME: The children for an InitListExpr doesn't include the array filler.
2896 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2897 if (E->HasSideEffects(Ctx))
2898 return true;
2899 break;
2900
2901 case GenericSelectionExprClass:
2902 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2903 HasSideEffects(Ctx);
2904
2905 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002906 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002907
2908 case CXXDefaultArgExprClass:
2909 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2910
Richard Smith852c9db2013-04-20 22:23:05 +00002911 case CXXDefaultInitExprClass:
2912 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2913 return E->HasSideEffects(Ctx);
2914 // If we've not yet parsed the initializer, assume it has side-effects.
2915 return true;
2916
Richard Smith0421ce72012-08-07 04:16:51 +00002917 case CXXDynamicCastExprClass: {
2918 // A dynamic_cast expression has side-effects if it can throw.
2919 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2920 if (DCE->getTypeAsWritten()->isReferenceType() &&
2921 DCE->getCastKind() == CK_Dynamic)
2922 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002923 } // Fall through.
2924 case ImplicitCastExprClass:
2925 case CStyleCastExprClass:
2926 case CXXStaticCastExprClass:
2927 case CXXReinterpretCastExprClass:
2928 case CXXConstCastExprClass:
2929 case CXXFunctionalCastExprClass: {
2930 const CastExpr *CE = cast<CastExpr>(this);
2931 if (CE->getCastKind() == CK_LValueToRValue &&
2932 CE->getSubExpr()->getType().isVolatileQualified())
2933 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002934 break;
2935 }
2936
Richard Smithef8bf432012-08-13 20:08:14 +00002937 case CXXTypeidExprClass:
2938 // typeid might throw if its subexpression is potentially-evaluated, so has
2939 // side-effects in that case whether or not its subexpression does.
2940 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002941
2942 case CXXConstructExprClass:
2943 case CXXTemporaryObjectExprClass: {
2944 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002945 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002946 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002947 // A trivial constructor does not add any side-effects of its own. Just look
2948 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002949 break;
2950 }
2951
2952 case LambdaExprClass: {
2953 const LambdaExpr *LE = cast<LambdaExpr>(this);
2954 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2955 E = LE->capture_end(); I != E; ++I)
2956 if (I->getCaptureKind() == LCK_ByCopy)
2957 // FIXME: Only has a side-effect if the variable is volatile or if
2958 // the copy would invoke a non-trivial copy constructor.
2959 return true;
2960 return false;
2961 }
2962
2963 case PseudoObjectExprClass: {
2964 // Only look for side-effects in the semantic form, and look past
2965 // OpaqueValueExpr bindings in that form.
2966 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2967 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2968 E = PO->semantics_end();
2969 I != E; ++I) {
2970 const Expr *Subexpr = *I;
2971 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2972 Subexpr = OVE->getSourceExpr();
2973 if (Subexpr->HasSideEffects(Ctx))
2974 return true;
2975 }
2976 return false;
2977 }
2978
2979 case ObjCBoxedExprClass:
2980 case ObjCArrayLiteralClass:
2981 case ObjCDictionaryLiteralClass:
2982 case ObjCMessageExprClass:
2983 case ObjCSelectorExprClass:
2984 case ObjCProtocolExprClass:
2985 case ObjCPropertyRefExprClass:
2986 case ObjCIsaExprClass:
2987 case ObjCIndirectCopyRestoreExprClass:
2988 case ObjCSubscriptRefExprClass:
2989 case ObjCBridgedCastExprClass:
2990 // FIXME: Classify these cases better.
2991 return true;
2992 }
2993
2994 // Recurse to children.
2995 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2996 if (const Stmt *S = *SubStmts)
2997 if (cast<Expr>(S)->HasSideEffects(Ctx))
2998 return true;
2999
3000 return false;
3001}
3002
Douglas Gregor1be329d2012-02-23 07:33:15 +00003003namespace {
3004 /// \brief Look for a call to a non-trivial function within an expression.
3005 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
3006 {
3007 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
3008
3009 bool NonTrivial;
3010
3011 public:
3012 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00003013 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00003014
3015 bool hasNonTrivialCall() const { return NonTrivial; }
3016
3017 void VisitCallExpr(CallExpr *E) {
3018 if (CXXMethodDecl *Method
3019 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3020 if (Method->isTrivial()) {
3021 // Recurse to children of the call.
3022 Inherited::VisitStmt(E);
3023 return;
3024 }
3025 }
3026
3027 NonTrivial = true;
3028 }
3029
3030 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3031 if (E->getConstructor()->isTrivial()) {
3032 // Recurse to children of the call.
3033 Inherited::VisitStmt(E);
3034 return;
3035 }
3036
3037 NonTrivial = true;
3038 }
3039
3040 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3041 if (E->getTemporary()->getDestructor()->isTrivial()) {
3042 Inherited::VisitStmt(E);
3043 return;
3044 }
3045
3046 NonTrivial = true;
3047 }
3048 };
3049}
3050
3051bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3052 NonTrivialCallFinder Finder(Ctx);
3053 Finder.Visit(this);
3054 return Finder.hasNonTrivialCall();
3055}
3056
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003057/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3058/// pointer constant or not, as well as the specific kind of constant detected.
3059/// Null pointer constants can be integer constant expressions with the
3060/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3061/// (a GNU extension).
3062Expr::NullPointerConstantKind
3063Expr::isNullPointerConstant(ASTContext &Ctx,
3064 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003065 if (isValueDependent() &&
3066 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MicrosoftMode)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003067 switch (NPC) {
3068 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003069 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003070 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003071 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003072 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003073 else
3074 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003075
Douglas Gregor56751b52009-09-25 04:25:58 +00003076 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003077 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003078 }
3079 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003080
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003081 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003082 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003083 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003084 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003085 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003086 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003087 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003088 Pointee->isVoidType() && // to void*
3089 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003090 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003091 }
Steve Naroffada7d422007-05-20 17:54:12 +00003092 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003093 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3094 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003095 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003096 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3097 // Accept ((void*)0) as a null pointer constant, as many other
3098 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003099 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003100 } else if (const GenericSelectionExpr *GE =
3101 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003102 if (GE->isResultDependent())
3103 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003104 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003105 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3106 if (CE->isConditionDependent())
3107 return NPCK_NotNull;
3108 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003109 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003110 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003111 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003112 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003113 } else if (const CXXDefaultInitExpr *DefaultInit
3114 = dyn_cast<CXXDefaultInitExpr>(this)) {
3115 // See through default initializer expressions.
3116 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003117 } else if (isa<GNUNullExpr>(this)) {
3118 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003119 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003120 } else if (const MaterializeTemporaryExpr *M
3121 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3122 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003123 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3124 if (const Expr *Source = OVE->getSourceExpr())
3125 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003126 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003127
Richard Smith89645bc2013-01-02 12:01:23 +00003128 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003129 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003130 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003131
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003132 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003133 if (!Ctx.getLangOpts().CPlusPlus11 &&
3134 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003135 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3136 const Expr *InitExpr = CLE->getInitializer();
3137 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3138 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3139 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003140 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003141 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003142 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003143 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003144
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003145 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003146 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3147 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003148 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003149 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003150 if (Lit && !Lit->getValue())
3151 return NPCK_ZeroLiteral;
3152 else if (!Ctx.getLangOpts().MicrosoftMode ||
3153 !isCXX98IntegralConstantExpr(Ctx))
3154 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003155 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003156 // If we have an integer constant expression, we need to *evaluate* it and
3157 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003158 if (!isIntegerConstantExpr(Ctx))
3159 return NPCK_NotNull;
3160 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003161
David Blaikie1c7c8f72012-08-08 17:33:31 +00003162 if (EvaluateKnownConstInt(Ctx) != 0)
3163 return NPCK_NotNull;
3164
3165 if (isa<IntegerLiteral>(this))
3166 return NPCK_ZeroLiteral;
3167 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003168}
Steve Narofff7a5da12007-07-28 23:10:27 +00003169
John McCall34376a62010-12-04 03:47:34 +00003170/// \brief If this expression is an l-value for an Objective C
3171/// property, find the underlying property reference expression.
3172const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3173 const Expr *E = this;
3174 while (true) {
3175 assert((E->getValueKind() == VK_LValue &&
3176 E->getObjectKind() == OK_ObjCProperty) &&
3177 "expression is not a property reference");
3178 E = E->IgnoreParenCasts();
3179 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3180 if (BO->getOpcode() == BO_Comma) {
3181 E = BO->getRHS();
3182 continue;
3183 }
3184 }
3185
3186 break;
3187 }
3188
3189 return cast<ObjCPropertyRefExpr>(E);
3190}
3191
Anna Zaks97c7ce32012-10-01 20:34:04 +00003192bool Expr::isObjCSelfExpr() const {
3193 const Expr *E = IgnoreParenImpCasts();
3194
3195 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3196 if (!DRE)
3197 return false;
3198
3199 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3200 if (!Param)
3201 return false;
3202
3203 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3204 if (!M)
3205 return false;
3206
3207 return M->getSelfDecl() == Param;
3208}
3209
John McCalld25db7e2013-05-06 21:39:12 +00003210FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003211 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003212
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003213 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003214 if (ICE->getCastKind() == CK_LValueToRValue ||
3215 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003216 E = ICE->getSubExpr()->IgnoreParens();
3217 else
3218 break;
3219 }
3220
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003221 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003222 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003223 if (Field->isBitField())
3224 return Field;
3225
John McCalld25db7e2013-05-06 21:39:12 +00003226 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3227 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3228 if (Ivar->isBitField())
3229 return Ivar;
3230
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003231 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3232 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3233 if (Field->isBitField())
3234 return Field;
3235
Eli Friedman609ada22011-07-13 02:05:57 +00003236 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003237 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003238 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003239
Eli Friedman609ada22011-07-13 02:05:57 +00003240 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003241 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003242 }
3243
Douglas Gregor71235ec2009-05-02 02:18:30 +00003244 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003245}
3246
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003247bool Expr::refersToVectorElement() const {
3248 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003249
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003250 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003251 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003252 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003253 E = ICE->getSubExpr()->IgnoreParens();
3254 else
3255 break;
3256 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003257
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003258 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3259 return ASE->getBase()->getType()->isVectorType();
3260
3261 if (isa<ExtVectorElementExpr>(E))
3262 return true;
3263
3264 return false;
3265}
3266
Chris Lattnerb8211f62009-02-16 22:14:05 +00003267/// isArrow - Return true if the base expression is a pointer to vector,
3268/// return false if the base expression is a vector.
3269bool ExtVectorElementExpr::isArrow() const {
3270 return getBase()->getType()->isPointerType();
3271}
3272
Nate Begemance4d7fc2008-04-18 23:10:10 +00003273unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003274 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003275 return VT->getNumElements();
3276 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003277}
3278
Nate Begemanf322eab2008-05-09 06:41:27 +00003279/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003280bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003281 // FIXME: Refactor this code to an accessor on the AST node which returns the
3282 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003283 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003284
3285 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003286 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003287 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003288
Nate Begeman7e5185b2009-01-18 02:01:21 +00003289 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003290 if (Comp[0] == 's' || Comp[0] == 'S')
3291 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003292
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003293 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003294 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003295 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003296
Steve Naroff0d595ca2007-07-30 03:29:09 +00003297 return false;
3298}
Chris Lattner885b4952007-08-02 23:36:59 +00003299
Nate Begemanf322eab2008-05-09 06:41:27 +00003300/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003301void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003302 SmallVectorImpl<unsigned> &Elts) const {
3303 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003304 if (Comp[0] == 's' || Comp[0] == 'S')
3305 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003306
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003307 bool isHi = Comp == "hi";
3308 bool isLo = Comp == "lo";
3309 bool isEven = Comp == "even";
3310 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003311
Nate Begemanf322eab2008-05-09 06:41:27 +00003312 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3313 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003314
Nate Begemanf322eab2008-05-09 06:41:27 +00003315 if (isHi)
3316 Index = e + i;
3317 else if (isLo)
3318 Index = i;
3319 else if (isEven)
3320 Index = 2 * i;
3321 else if (isOdd)
3322 Index = 2 * i + 1;
3323 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003324 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003325
Nate Begemand3862152008-05-13 21:03:02 +00003326 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003327 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003328}
3329
Douglas Gregor9a129192010-04-21 00:45:42 +00003330ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003331 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003332 SourceLocation LBracLoc,
3333 SourceLocation SuperLoc,
3334 bool IsInstanceSuper,
3335 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +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,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003344 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003345 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003346 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003347 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3348 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003349 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003350 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3351 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003352{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003353 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003354 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003355}
3356
Douglas Gregor9a129192010-04-21 00:45:42 +00003357ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003358 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003359 SourceLocation LBracLoc,
3360 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003361 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003362 ArrayRef<SourceLocation> SelLocs,
3363 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003364 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003365 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003366 SourceLocation RBracLoc,
3367 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003368 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003369 T->isDependentType(), T->isInstantiationDependentType(),
3370 T->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(Class),
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);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003379}
3380
Douglas Gregor9a129192010-04-21 00:45:42 +00003381ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003382 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003383 SourceLocation LBracLoc,
3384 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003385 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003386 ArrayRef<SourceLocation> SelLocs,
3387 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003388 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003389 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003390 SourceLocation RBracLoc,
3391 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003392 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003393 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003394 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003395 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003396 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3397 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003398 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003399 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003400 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003401{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003402 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003403 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003404}
3405
3406void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3407 ArrayRef<SourceLocation> SelLocs,
3408 SelectorLocationsKind SelLocsK) {
3409 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003410 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003411 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003412 if (Args[I]->isTypeDependent())
3413 ExprBits.TypeDependent = true;
3414 if (Args[I]->isValueDependent())
3415 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003416 if (Args[I]->isInstantiationDependent())
3417 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003418 if (Args[I]->containsUnexpandedParameterPack())
3419 ExprBits.ContainsUnexpandedParameterPack = true;
3420
3421 MyArgs[I] = Args[I];
3422 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003423
Benjamin Kramer2325b242012-02-20 00:20:48 +00003424 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003425 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003426 if (SelLocsK == SelLoc_NonStandard)
3427 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3428 }
Chris Lattner7ec71da2009-04-26 00:44:05 +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 SourceLocation SuperLoc,
3435 bool IsInstanceSuper,
3436 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003437 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003438 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003439 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003440 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003441 SourceLocation RBracLoc,
3442 bool isImplicit) {
3443 assert((!SelLocs.empty() || isImplicit) &&
3444 "No selector locs for non-implicit message");
3445 ObjCMessageExpr *Mem;
3446 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3447 if (isImplicit)
3448 Mem = alloc(Context, Args.size(), 0);
3449 else
3450 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003451 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003452 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003453 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003454}
3455
Craig Topperce7167c2013-08-22 04:58:56 +00003456ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003457 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003458 SourceLocation LBracLoc,
3459 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003460 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003461 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003462 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003463 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003464 SourceLocation RBracLoc,
3465 bool isImplicit) {
3466 assert((!SelLocs.empty() || isImplicit) &&
3467 "No selector locs for non-implicit message");
3468 ObjCMessageExpr *Mem;
3469 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3470 if (isImplicit)
3471 Mem = alloc(Context, Args.size(), 0);
3472 else
3473 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003474 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003475 SelLocs, SelLocsK, Method, Args, RBracLoc,
3476 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003477}
3478
Craig Topperce7167c2013-08-22 04:58:56 +00003479ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003480 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003481 SourceLocation LBracLoc,
3482 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003483 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003484 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003485 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003486 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003487 SourceLocation RBracLoc,
3488 bool isImplicit) {
3489 assert((!SelLocs.empty() || isImplicit) &&
3490 "No selector locs for non-implicit message");
3491 ObjCMessageExpr *Mem;
3492 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3493 if (isImplicit)
3494 Mem = alloc(Context, Args.size(), 0);
3495 else
3496 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003497 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003498 SelLocs, SelLocsK, Method, Args, RBracLoc,
3499 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003500}
3501
Craig Topperce7167c2013-08-22 04:58:56 +00003502ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003503 unsigned NumArgs,
3504 unsigned NumStoredSelLocs) {
3505 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003506 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3507}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003508
Craig Topperce7167c2013-08-22 04:58:56 +00003509ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003510 ArrayRef<Expr *> Args,
3511 SourceLocation RBraceLoc,
3512 ArrayRef<SourceLocation> SelLocs,
3513 Selector Sel,
3514 SelectorLocationsKind &SelLocsK) {
3515 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3516 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3517 : 0;
3518 return alloc(C, Args.size(), NumStoredSelLocs);
3519}
3520
Craig Topperce7167c2013-08-22 04:58:56 +00003521ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003522 unsigned NumArgs,
3523 unsigned NumStoredSelLocs) {
3524 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3525 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3526 return (ObjCMessageExpr *)C.Allocate(Size,
3527 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3528}
3529
3530void ObjCMessageExpr::getSelectorLocs(
3531 SmallVectorImpl<SourceLocation> &SelLocs) const {
3532 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3533 SelLocs.push_back(getSelectorLoc(i));
3534}
3535
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003536SourceRange ObjCMessageExpr::getReceiverRange() const {
3537 switch (getReceiverKind()) {
3538 case Instance:
3539 return getInstanceReceiver()->getSourceRange();
3540
3541 case Class:
3542 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3543
3544 case SuperInstance:
3545 case SuperClass:
3546 return getSuperLoc();
3547 }
3548
David Blaikiee4d798f2012-01-20 21:50:17 +00003549 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003550}
3551
Douglas Gregor9a129192010-04-21 00:45:42 +00003552Selector ObjCMessageExpr::getSelector() const {
3553 if (HasMethod)
3554 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3555 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003556 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003557}
3558
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003559QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003560 switch (getReceiverKind()) {
3561 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003562 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003563 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003564 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003565 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003566 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003567 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003568 }
3569
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003570 llvm_unreachable("unexpected receiver kind");
3571}
3572
3573ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3574 QualType T = getReceiverType();
3575
3576 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3577 return Ptr->getInterfaceDecl();
3578
3579 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3580 return Ty->getInterface();
3581
Douglas Gregor9a129192010-04-21 00:45:42 +00003582 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003583}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003584
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003585StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003586 switch (getBridgeKind()) {
3587 case OBC_Bridge:
3588 return "__bridge";
3589 case OBC_BridgeTransfer:
3590 return "__bridge_transfer";
3591 case OBC_BridgeRetained:
3592 return "__bridge_retained";
3593 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003594
3595 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003596}
3597
Craig Topper37932912013-08-18 10:09:15 +00003598ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003599 QualType Type, SourceLocation BLoc,
3600 SourceLocation RP)
3601 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3602 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003603 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003604 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003605 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003606{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003607 SubExprs = new (C) Stmt*[args.size()];
3608 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003609 if (args[i]->isTypeDependent())
3610 ExprBits.TypeDependent = true;
3611 if (args[i]->isValueDependent())
3612 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003613 if (args[i]->isInstantiationDependent())
3614 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003615 if (args[i]->containsUnexpandedParameterPack())
3616 ExprBits.ContainsUnexpandedParameterPack = true;
3617
3618 SubExprs[i] = args[i];
3619 }
3620}
3621
Craig Topper37932912013-08-18 10:09:15 +00003622void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003623 if (SubExprs) C.Deallocate(SubExprs);
3624
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003625 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003626 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003627 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003628}
Nate Begeman48745922009-08-12 02:28:50 +00003629
Craig Topper37932912013-08-18 10:09:15 +00003630GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003631 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003632 ArrayRef<TypeSourceInfo*> AssocTypes,
3633 ArrayRef<Expr*> AssocExprs,
3634 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003635 SourceLocation RParenLoc,
3636 bool ContainsUnexpandedParameterPack,
3637 unsigned ResultIndex)
3638 : Expr(GenericSelectionExprClass,
3639 AssocExprs[ResultIndex]->getType(),
3640 AssocExprs[ResultIndex]->getValueKind(),
3641 AssocExprs[ResultIndex]->getObjectKind(),
3642 AssocExprs[ResultIndex]->isTypeDependent(),
3643 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003644 AssocExprs[ResultIndex]->isInstantiationDependent(),
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(ResultIndex),
3649 GenericLoc(GenericLoc), 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
Craig Topper37932912013-08-18 10:09:15 +00003656GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003657 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003658 ArrayRef<TypeSourceInfo*> AssocTypes,
3659 ArrayRef<Expr*> AssocExprs,
3660 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003661 SourceLocation RParenLoc,
3662 bool ContainsUnexpandedParameterPack)
3663 : Expr(GenericSelectionExprClass,
3664 Context.DependentTy,
3665 VK_RValue,
3666 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003667 /*isTypeDependent=*/true,
3668 /*isValueDependent=*/true,
3669 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003670 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003671 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3672 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3673 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3674 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003675 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003676 assert(AssocTypes.size() == AssocExprs.size());
3677 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3678 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003679}
3680
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003681//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003682// DesignatedInitExpr
3683//===----------------------------------------------------------------------===//
3684
Chandler Carruth631abd92011-06-16 06:47:06 +00003685IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003686 assert(Kind == FieldDesignator && "Only valid on a field designator");
3687 if (Field.NameOrField & 0x01)
3688 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3689 else
3690 return getField()->getIdentifier();
3691}
3692
Craig Topper37932912013-08-18 10:09:15 +00003693DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003694 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003695 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003696 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003697 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003698 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003699 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003700 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003701 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003702 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003703 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003704 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003705 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003706 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003707 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003708
3709 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003710 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003711 *Child++ = Init;
3712
3713 // Copy the designators and their subexpressions, computing
3714 // value-dependence along the way.
3715 unsigned IndexIdx = 0;
3716 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003717 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003718
3719 if (this->Designators[I].isArrayDesignator()) {
3720 // Compute type- and value-dependence.
3721 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003722 if (Index->isTypeDependent() || Index->isValueDependent())
3723 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003724 if (Index->isInstantiationDependent())
3725 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003726 // Propagate unexpanded parameter packs.
3727 if (Index->containsUnexpandedParameterPack())
3728 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003729
3730 // Copy the index expressions into permanent storage.
3731 *Child++ = IndexExprs[IndexIdx++];
3732 } else if (this->Designators[I].isArrayRangeDesignator()) {
3733 // Compute type- and value-dependence.
3734 Expr *Start = IndexExprs[IndexIdx];
3735 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003736 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003737 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003738 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003739 ExprBits.InstantiationDependent = true;
3740 } else if (Start->isInstantiationDependent() ||
3741 End->isInstantiationDependent()) {
3742 ExprBits.InstantiationDependent = true;
3743 }
3744
Douglas Gregora6e053e2010-12-15 01:34:56 +00003745 // Propagate unexpanded parameter packs.
3746 if (Start->containsUnexpandedParameterPack() ||
3747 End->containsUnexpandedParameterPack())
3748 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003749
3750 // Copy the start/end expressions into permanent storage.
3751 *Child++ = IndexExprs[IndexIdx++];
3752 *Child++ = IndexExprs[IndexIdx++];
3753 }
3754 }
3755
Benjamin Kramerc215e762012-08-24 11:54:20 +00003756 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003757}
3758
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003759DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003760DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003761 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003762 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003763 SourceLocation ColonOrEqualLoc,
3764 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003765 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003766 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003767 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003768 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003769 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003770}
3771
Craig Topper37932912013-08-18 10:09:15 +00003772DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003773 unsigned NumIndexExprs) {
3774 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3775 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3776 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3777}
3778
Craig Topper37932912013-08-18 10:09:15 +00003779void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003780 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003781 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003782 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003783 NumDesignators = NumDesigs;
3784 for (unsigned I = 0; I != NumDesigs; ++I)
3785 Designators[I] = Desigs[I];
3786}
3787
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003788SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3789 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3790 if (size() == 1)
3791 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003792 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3793 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003794}
3795
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003796SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003797 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003798 Designator &First =
3799 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003800 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003801 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003802 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3803 else
3804 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3805 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003806 StartLoc =
3807 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003808 return StartLoc;
3809}
3810
3811SourceLocation DesignatedInitExpr::getLocEnd() const {
3812 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003813}
3814
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003815Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003816 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003817 char *Ptr = static_cast<char *>(
3818 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003819 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003820 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3821 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3822}
3823
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003824Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003825 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003826 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003827 char *Ptr = static_cast<char *>(
3828 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003829 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003830 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3831 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3832}
3833
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003834Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003835 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003836 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003837 char *Ptr = static_cast<char *>(
3838 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003839 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003840 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3841 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3842}
3843
Douglas Gregord5846a12009-04-15 06:41:24 +00003844/// \brief Replaces the designator at index @p Idx with the series
3845/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003846void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003847 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003848 const Designator *Last) {
3849 unsigned NumNewDesignators = Last - First;
3850 if (NumNewDesignators == 0) {
3851 std::copy_backward(Designators + Idx + 1,
3852 Designators + NumDesignators,
3853 Designators + Idx);
3854 --NumNewDesignators;
3855 return;
3856 } else if (NumNewDesignators == 1) {
3857 Designators[Idx] = *First;
3858 return;
3859 }
3860
Mike Stump11289f42009-09-09 15:08:12 +00003861 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003862 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003863 std::copy(Designators, Designators + Idx, NewDesignators);
3864 std::copy(First, Last, NewDesignators + Idx);
3865 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3866 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003867 Designators = NewDesignators;
3868 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3869}
3870
Craig Topper37932912013-08-18 10:09:15 +00003871ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003872 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003873 SourceLocation rparenloc)
3874 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003875 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003876 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3877 Exprs = new (C) Stmt*[exprs.size()];
3878 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003879 if (exprs[i]->isTypeDependent())
3880 ExprBits.TypeDependent = true;
3881 if (exprs[i]->isValueDependent())
3882 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003883 if (exprs[i]->isInstantiationDependent())
3884 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003885 if (exprs[i]->containsUnexpandedParameterPack())
3886 ExprBits.ContainsUnexpandedParameterPack = true;
3887
Nate Begeman5ec4b312009-08-10 23:49:36 +00003888 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003889 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003890}
3891
John McCall1bf58462011-02-16 08:02:54 +00003892const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3893 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3894 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003895 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3896 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003897 e = cast<CXXConstructExpr>(e)->getArg(0);
3898 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3899 e = ice->getSubExpr();
3900 return cast<OpaqueValueExpr>(e);
3901}
3902
Craig Topper37932912013-08-18 10:09:15 +00003903PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3904 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003905 unsigned numSemanticExprs) {
3906 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3907 (1 + numSemanticExprs) * sizeof(Expr*),
3908 llvm::alignOf<PseudoObjectExpr>());
3909 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3910}
3911
3912PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3913 : Expr(PseudoObjectExprClass, shell) {
3914 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3915}
3916
Craig Topper37932912013-08-18 10:09:15 +00003917PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003918 ArrayRef<Expr*> semantics,
3919 unsigned resultIndex) {
3920 assert(syntax && "no syntactic expression!");
3921 assert(semantics.size() && "no semantic expressions!");
3922
3923 QualType type;
3924 ExprValueKind VK;
3925 if (resultIndex == NoResult) {
3926 type = C.VoidTy;
3927 VK = VK_RValue;
3928 } else {
3929 assert(resultIndex < semantics.size());
3930 type = semantics[resultIndex]->getType();
3931 VK = semantics[resultIndex]->getValueKind();
3932 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3933 }
3934
3935 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3936 (1 + semantics.size()) * sizeof(Expr*),
3937 llvm::alignOf<PseudoObjectExpr>());
3938 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3939 resultIndex);
3940}
3941
3942PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3943 Expr *syntax, ArrayRef<Expr*> semantics,
3944 unsigned resultIndex)
3945 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3946 /*filled in at end of ctor*/ false, false, false, false) {
3947 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3948 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3949
3950 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3951 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3952 getSubExprsBuffer()[i] = E;
3953
3954 if (E->isTypeDependent())
3955 ExprBits.TypeDependent = true;
3956 if (E->isValueDependent())
3957 ExprBits.ValueDependent = true;
3958 if (E->isInstantiationDependent())
3959 ExprBits.InstantiationDependent = true;
3960 if (E->containsUnexpandedParameterPack())
3961 ExprBits.ContainsUnexpandedParameterPack = true;
3962
3963 if (isa<OpaqueValueExpr>(E))
3964 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3965 "opaque-value semantic expressions for pseudo-object "
3966 "operations must have sources");
3967 }
3968}
3969
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003970//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003971// ExprIterator.
3972//===----------------------------------------------------------------------===//
3973
3974Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3975Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3976Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3977const Expr* ConstExprIterator::operator[](size_t idx) const {
3978 return cast<Expr>(I[idx]);
3979}
3980const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3981const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3982
3983//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003984// Child Iterators for iterating over subexpressions/substatements
3985//===----------------------------------------------------------------------===//
3986
Peter Collingbournee190dee2011-03-11 19:24:49 +00003987// UnaryExprOrTypeTraitExpr
3988Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003989 // If this is of a type and the type is a VLA type (and not a typedef), the
3990 // size expression of the VLA needs to be treated as an executable expression.
3991 // Why isn't this weirdness documented better in StmtIterator?
3992 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003993 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003994 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003995 return child_range(child_iterator(T), child_iterator());
3996 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003997 }
John McCallbd066782011-02-09 08:16:59 +00003998 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003999}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004000
Steve Naroffd54978b2007-09-18 23:55:05 +00004001// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00004002Stmt::child_range ObjCMessageExpr::children() {
4003 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00004004 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00004005 begin = reinterpret_cast<Stmt **>(this + 1);
4006 else
4007 begin = reinterpret_cast<Stmt **>(getArgs());
4008 return child_range(begin,
4009 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00004010}
4011
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004012ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004013 QualType T, ObjCMethodDecl *Method,
4014 SourceRange SR)
4015 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
4016 false, false, false, false),
4017 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
4018{
4019 Expr **SaveElements = getElements();
4020 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
4021 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
4022 ExprBits.ValueDependent = true;
4023 if (Elements[I]->isInstantiationDependent())
4024 ExprBits.InstantiationDependent = true;
4025 if (Elements[I]->containsUnexpandedParameterPack())
4026 ExprBits.ContainsUnexpandedParameterPack = true;
4027
4028 SaveElements[I] = Elements[I];
4029 }
4030}
4031
Craig Topperce7167c2013-08-22 04:58:56 +00004032ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004033 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004034 QualType T, ObjCMethodDecl * Method,
4035 SourceRange SR) {
4036 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4037 + Elements.size() * sizeof(Expr *));
4038 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4039}
4040
Craig Topperce7167c2013-08-22 04:58:56 +00004041ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004042 unsigned NumElements) {
4043
4044 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4045 + NumElements * sizeof(Expr *));
4046 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4047}
4048
4049ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4050 ArrayRef<ObjCDictionaryElement> VK,
4051 bool HasPackExpansions,
4052 QualType T, ObjCMethodDecl *method,
4053 SourceRange SR)
4054 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4055 false, false),
4056 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4057 DictWithObjectsMethod(method)
4058{
4059 KeyValuePair *KeyValues = getKeyValues();
4060 ExpansionData *Expansions = getExpansionData();
4061 for (unsigned I = 0; I < NumElements; I++) {
4062 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4063 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4064 ExprBits.ValueDependent = true;
4065 if (VK[I].Key->isInstantiationDependent() ||
4066 VK[I].Value->isInstantiationDependent())
4067 ExprBits.InstantiationDependent = true;
4068 if (VK[I].EllipsisLoc.isInvalid() &&
4069 (VK[I].Key->containsUnexpandedParameterPack() ||
4070 VK[I].Value->containsUnexpandedParameterPack()))
4071 ExprBits.ContainsUnexpandedParameterPack = true;
4072
4073 KeyValues[I].Key = VK[I].Key;
4074 KeyValues[I].Value = VK[I].Value;
4075 if (Expansions) {
4076 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4077 if (VK[I].NumExpansions)
4078 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4079 else
4080 Expansions[I].NumExpansionsPlusOne = 0;
4081 }
4082 }
4083}
4084
4085ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004086ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004087 ArrayRef<ObjCDictionaryElement> VK,
4088 bool HasPackExpansions,
4089 QualType T, ObjCMethodDecl *method,
4090 SourceRange SR) {
4091 unsigned ExpansionsSize = 0;
4092 if (HasPackExpansions)
4093 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4094
4095 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4096 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4097 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4098}
4099
4100ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004101ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004102 bool HasPackExpansions) {
4103 unsigned ExpansionsSize = 0;
4104 if (HasPackExpansions)
4105 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4106 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4107 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4108 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4109 HasPackExpansions);
4110}
4111
Craig Topperce7167c2013-08-22 04:58:56 +00004112ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004113 Expr *base,
4114 Expr *key, QualType T,
4115 ObjCMethodDecl *getMethod,
4116 ObjCMethodDecl *setMethod,
4117 SourceLocation RB) {
4118 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4119 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4120 OK_ObjCSubscript,
4121 getMethod, setMethod, RB);
4122}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004123
Benjamin Kramerc215e762012-08-24 11:54:20 +00004124AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004125 QualType t, AtomicOp op, SourceLocation RP)
4126 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4127 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004128 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004129{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004130 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4131 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004132 if (args[i]->isTypeDependent())
4133 ExprBits.TypeDependent = true;
4134 if (args[i]->isValueDependent())
4135 ExprBits.ValueDependent = true;
4136 if (args[i]->isInstantiationDependent())
4137 ExprBits.InstantiationDependent = true;
4138 if (args[i]->containsUnexpandedParameterPack())
4139 ExprBits.ContainsUnexpandedParameterPack = true;
4140
4141 SubExprs[i] = args[i];
4142 }
4143}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004144
4145unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4146 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004147 case AO__c11_atomic_init:
4148 case AO__c11_atomic_load:
4149 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004150 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004151
4152 case AO__c11_atomic_store:
4153 case AO__c11_atomic_exchange:
4154 case AO__atomic_load:
4155 case AO__atomic_store:
4156 case AO__atomic_store_n:
4157 case AO__atomic_exchange_n:
4158 case AO__c11_atomic_fetch_add:
4159 case AO__c11_atomic_fetch_sub:
4160 case AO__c11_atomic_fetch_and:
4161 case AO__c11_atomic_fetch_or:
4162 case AO__c11_atomic_fetch_xor:
4163 case AO__atomic_fetch_add:
4164 case AO__atomic_fetch_sub:
4165 case AO__atomic_fetch_and:
4166 case AO__atomic_fetch_or:
4167 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004168 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004169 case AO__atomic_add_fetch:
4170 case AO__atomic_sub_fetch:
4171 case AO__atomic_and_fetch:
4172 case AO__atomic_or_fetch:
4173 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004174 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004175 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004176
4177 case AO__atomic_exchange:
4178 return 4;
4179
4180 case AO__c11_atomic_compare_exchange_strong:
4181 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004182 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004183
4184 case AO__atomic_compare_exchange:
4185 case AO__atomic_compare_exchange_n:
4186 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004187 }
4188 llvm_unreachable("unknown atomic op");
4189}