blob: f29e9fe4c0729a99a762356025c64cc07a782c6f [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner86ee2862008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor1be329d2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
David Majnemerbed356a2013-11-06 23:31:56 +000023#include "clang/AST/Mangle.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000025#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000027#include "clang/Basic/CharInfo.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000030#include "clang/Lex/Lexer.h"
31#include "clang/Lex/LiteralSupport.h"
32#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000033#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000035#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000036#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000037using namespace clang;
38
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000039const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000040 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000041
42 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000043 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
44 DerivedType = PTy->getPointeeType();
45
Rafael Espindola60a2bba2012-07-17 20:24:05 +000046 if (DerivedType->isDependentType())
47 return NULL;
48
Rafael Espindola49e860b2012-06-26 17:45:31 +000049 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000050 Decl *D = Ty->getDecl();
51 return cast<CXXRecordDecl>(D);
52}
53
Richard Smithf3fabd22013-06-03 00:17:11 +000054const Expr *Expr::skipRValueSubobjectAdjustments(
55 SmallVectorImpl<const Expr *> &CommaLHSs,
56 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola9c006de2012-10-27 01:03:43 +000057 const Expr *E = this;
58 while (true) {
59 E = E->IgnoreParens();
60
61 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
62 if ((CE->getCastKind() == CK_DerivedToBase ||
63 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
64 E->getType()->isRecordType()) {
65 E = CE->getSubExpr();
66 CXXRecordDecl *Derived
67 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
68 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
69 continue;
70 }
71
72 if (CE->getCastKind() == CK_NoOp) {
73 E = CE->getSubExpr();
74 continue;
75 }
76 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000077 if (!ME->isArrow()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +000078 assert(ME->getBase()->getType()->isRecordType());
79 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smith6b6f8aa2013-06-15 00:30:29 +000080 if (!Field->isBitField() && !Field->getType()->isReferenceType()) {
Richard Smith2d187902013-06-03 07:13:35 +000081 E = ME->getBase();
82 Adjustments.push_back(SubobjectAdjustment(Field));
83 continue;
84 }
Rafael Espindola9c006de2012-10-27 01:03:43 +000085 }
86 }
87 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
88 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000089 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-10-27 01:03:43 +000090 E = BO->getLHS();
91 const MemberPointerType *MPT =
92 BO->getRHS()->getType()->getAs<MemberPointerType>();
93 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smithf3fabd22013-06-03 00:17:11 +000094 continue;
95 } else if (BO->getOpcode() == BO_Comma) {
96 CommaLHSs.push_back(BO->getLHS());
97 E = BO->getRHS();
98 continue;
Rafael Espindola9c006de2012-10-27 01:03:43 +000099 }
100 }
101
102 // Nothing changed.
103 break;
104 }
105 return E;
106}
107
Chris Lattner4ebae652010-04-16 23:34:13 +0000108/// isKnownToHaveBooleanValue - Return true if this is an integer expression
109/// that is known to return 0 or 1. This happens for _Bool/bool expressions
110/// but also int expressions which are produced by things like comparisons in
111/// C.
112bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000113 const Expr *E = IgnoreParens();
114
Chris Lattner4ebae652010-04-16 23:34:13 +0000115 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000116 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000117 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000118 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000119
Peter Collingbourne91147592011-04-15 00:35:48 +0000120 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000121 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000122 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000123 return UO->getSubExpr()->isKnownToHaveBooleanValue();
Richard Trieu0f097742014-04-04 04:13:47 +0000124 case UO_LNot:
125 return true;
Chris Lattner4ebae652010-04-16 23:34:13 +0000126 default:
127 return false;
128 }
129 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000130
John McCall45d30c32010-06-12 01:56:02 +0000131 // Only look through implicit casts. If the user writes
132 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000133 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000134 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000135
Peter Collingbourne91147592011-04-15 00:35:48 +0000136 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000137 switch (BO->getOpcode()) {
138 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000139 case BO_LT: // Relational operators.
140 case BO_GT:
141 case BO_LE:
142 case BO_GE:
143 case BO_EQ: // Equality operators.
144 case BO_NE:
145 case BO_LAnd: // AND operator.
146 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000147 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148
John McCalle3027922010-08-25 11:45:40 +0000149 case BO_And: // Bitwise AND operator.
150 case BO_Xor: // Bitwise XOR operator.
151 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000152 // Handle things like (x==2)|(y==12).
153 return BO->getLHS()->isKnownToHaveBooleanValue() &&
154 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000155
John McCalle3027922010-08-25 11:45:40 +0000156 case BO_Comma:
157 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000158 return BO->getRHS()->isKnownToHaveBooleanValue();
159 }
160 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000161
Peter Collingbourne91147592011-04-15 00:35:48 +0000162 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000163 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
164 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000165
Chris Lattner4ebae652010-04-16 23:34:13 +0000166 return false;
167}
168
John McCallbd066782011-02-09 08:16:59 +0000169// Amusing macro metaprogramming hack: check whether a class provides
170// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000171//
172// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000173namespace {
174 /// This implementation is used when a class provides a custom
175 /// implementation of getExprLoc.
176 template <class E, class T>
177 SourceLocation getExprLocImpl(const Expr *expr,
178 SourceLocation (T::*v)() const) {
179 return static_cast<const E*>(expr)->getExprLoc();
180 }
181
182 /// This implementation is used when a class doesn't provide
183 /// a custom implementation of getExprLoc. Overload resolution
184 /// should pick it over the implementation above because it's
185 /// more specialized according to function template partial ordering.
186 template <class E>
187 SourceLocation getExprLocImpl(const Expr *expr,
188 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000189 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000190 }
191}
192
193SourceLocation Expr::getExprLoc() const {
194 switch (getStmtClass()) {
195 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
196#define ABSTRACT_STMT(type)
197#define STMT(type, base) \
198 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
199#define EXPR(type, base) \
200 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
201#include "clang/AST/StmtNodes.inc"
202 }
203 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000204}
205
Chris Lattner0eedafe2006-08-24 04:56:27 +0000206//===----------------------------------------------------------------------===//
207// Primary Expressions.
208//===----------------------------------------------------------------------===//
209
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210/// \brief Compute the type-, value-, and instantiation-dependence of a
211/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000212/// based on the declaration being referenced.
Craig Topperce7167c2013-08-22 04:58:56 +0000213static void computeDeclRefDependence(const ASTContext &Ctx, NamedDecl *D,
214 QualType T, bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000215 bool &ValueDependent,
216 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000217 TypeDependent = false;
218 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000219 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000224 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000228
Douglas Gregored6c7442009-11-23 11:41:28 +0000229 // (TD) - an identifier that was declared with dependent type
230 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000231 if (T->isDependentType()) {
232 TypeDependent = true;
233 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000234 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000235 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000236 } else if (T->isInstantiationDependentType()) {
237 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000238 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239
Douglas Gregored6c7442009-11-23 11:41:28 +0000240 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000242 == DeclarationName::CXXConversionFunctionName) {
243 QualType T = D->getDeclName().getCXXNameType();
244 if (T->isDependentType()) {
245 TypeDependent = true;
246 ValueDependent = true;
247 InstantiationDependent = true;
248 return;
249 }
250
251 if (T->isInstantiationDependentType())
252 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000253 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000254
Douglas Gregored6c7442009-11-23 11:41:28 +0000255 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256 if (isa<NonTypeTemplateParmDecl>(D)) {
257 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000259 return;
260 }
261
Douglas Gregored6c7442009-11-23 11:41:28 +0000262 // (VD) - a constant with integral or enumeration type and is
263 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000264 // (VD) - a constant with literal type and is initialized with an
265 // expression that is value-dependent [C++11].
266 // (VD) - FIXME: Missing from the standard:
267 // - an entity with reference type and is initialized with an
268 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000269 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000270 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000271 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000272 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000273 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000274 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000275 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000276 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000277 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000278 InstantiationDependent = true;
279 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000280 }
281
Douglas Gregor0e4de762010-05-11 08:41:30 +0000282 // (VD) - FIXME: Missing from the standard:
283 // - a member function or a static data member of the current
284 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000285 if (Var->isStaticDataMember() &&
286 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000287 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 InstantiationDependent = true;
Richard Smith00f5d892013-11-14 22:40:45 +0000289 TypeSourceInfo *TInfo = Var->getFirstDecl()->getTypeSourceInfo();
290 if (TInfo->getType()->isIncompleteArrayType())
291 TypeDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000292 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000293
294 return;
295 }
296
Douglas Gregor0e4de762010-05-11 08:41:30 +0000297 // (VD) - FIXME: Missing from the standard:
298 // - a member function or a static data member of the current
299 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000300 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
301 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000302 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000303 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000304}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000305
Craig Topperce7167c2013-08-22 04:58:56 +0000306void DeclRefExpr::computeDependence(const ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000307 bool TypeDependent = false;
308 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000309 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000310 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
311 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000312
313 // (TD) C++ [temp.dep.expr]p3:
314 // An id-expression is type-dependent if it contains:
315 //
316 // and
317 //
318 // (VD) C++ [temp.dep.constexpr]p2:
319 // An identifier is value-dependent if it is:
320 if (!TypeDependent && !ValueDependent &&
321 hasExplicitTemplateArgs() &&
322 TemplateSpecializationType::anyDependentTemplateArguments(
323 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000324 getNumTemplateArgs(),
325 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000326 TypeDependent = true;
327 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000328 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000329 }
330
331 ExprBits.TypeDependent = TypeDependent;
332 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000333 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000334
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000335 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000336 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000337 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000338}
339
Craig Topperce7167c2013-08-22 04:58:56 +0000340DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000341 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000342 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000343 ValueDecl *D, bool RefersToEnclosingLocal,
344 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000345 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000346 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000347 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000348 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000349 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
350 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000351 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000352 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000353 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
354 if (FoundD)
355 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000356 DeclRefExprBits.HasTemplateKWAndArgsInfo
357 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000358 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000359 if (TemplateArgs) {
360 bool Dependent = false;
361 bool InstantiationDependent = false;
362 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000363 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
364 Dependent,
365 InstantiationDependent,
366 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000367 if (InstantiationDependent)
368 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 } else if (TemplateKWLoc.isValid()) {
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000371 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000372 DeclRefExprBits.HadMultipleCandidates = 0;
373
Daniel Dunbar9d355812012-03-09 01:51:51 +0000374 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000375}
376
Craig Topperce7167c2013-08-22 04:58:56 +0000377DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000378 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000379 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000380 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000381 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000382 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000383 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000384 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000385 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000386 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000387 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000388 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000389 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000390 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000391}
392
Craig Topperce7167c2013-08-22 04:58:56 +0000393DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000394 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000395 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000396 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000397 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000398 const DeclarationNameInfo &NameInfo,
399 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000400 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000401 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000402 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000403 // Filter out cases where the found Decl is the same as the value refenenced.
404 if (D == FoundD)
405 FoundD = 0;
406
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000407 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000408 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000409 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000410 if (FoundD)
411 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000412 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000413 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
414 else if (TemplateKWLoc.isValid())
415 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000416
Chris Lattner5c0b4052010-10-30 05:14:06 +0000417 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000418 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000419 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000420 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000421}
422
Craig Topperce7167c2013-08-22 04:58:56 +0000423DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000424 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000425 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000426 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000427 unsigned NumTemplateArgs) {
428 std::size_t Size = sizeof(DeclRefExpr);
429 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000430 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000431 if (HasFoundDecl)
432 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000433 if (HasTemplateKWAndArgsInfo)
434 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000435
Chris Lattner5c0b4052010-10-30 05:14:06 +0000436 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000437 return new (Mem) DeclRefExpr(EmptyShell());
438}
439
Daniel Dunbarb507f272012-03-09 15:39:15 +0000440SourceLocation DeclRefExpr::getLocStart() const {
441 if (hasQualifier())
442 return getQualifierLoc().getBeginLoc();
443 return getNameInfo().getLocStart();
444}
445SourceLocation DeclRefExpr::getLocEnd() const {
446 if (hasExplicitTemplateArgs())
447 return getRAngleLoc();
448 return getNameInfo().getLocEnd();
449}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000450
Anders Carlsson2fb08242009-09-08 18:24:21 +0000451// FIXME: Maybe this should use DeclPrinter with a special "print predefined
452// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000453std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
454 ASTContext &Context = CurrentDecl->getASTContext();
455
David Majnemerbed356a2013-11-06 23:31:56 +0000456 if (IT == PredefinedExpr::FuncDName) {
457 if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurrentDecl)) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000458 std::unique_ptr<MangleContext> MC;
David Majnemerbed356a2013-11-06 23:31:56 +0000459 MC.reset(Context.createMangleContext());
460
461 if (MC->shouldMangleDeclName(ND)) {
462 SmallString<256> Buffer;
463 llvm::raw_svector_ostream Out(Buffer);
464 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(ND))
465 MC->mangleCXXCtor(CD, Ctor_Base, Out);
466 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(ND))
467 MC->mangleCXXDtor(DD, Dtor_Base, Out);
468 else
469 MC->mangleName(ND, Out);
470
471 Out.flush();
472 if (!Buffer.empty() && Buffer.front() == '\01')
473 return Buffer.substr(1);
474 return Buffer.str();
475 } else
476 return ND->getIdentifier()->getName();
477 }
478 return "";
479 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000480 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000481 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000482 return FD->getNameAsString();
483
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000484 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000485 llvm::raw_svector_ostream Out(Name);
486
487 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000488 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000489 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000490 if (MD->isStatic())
491 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000492 }
493
David Blaikiebbafb8a2012-03-11 07:00:24 +0000494 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000495 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000496 llvm::raw_string_ostream POut(Proto);
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000497 FD->printQualifiedName(POut, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000498
Douglas Gregor11a434a2012-04-10 20:14:15 +0000499 const FunctionDecl *Decl = FD;
500 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
501 Decl = Pattern;
502 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000503 const FunctionProtoType *FT = 0;
504 if (FD->hasWrittenPrototype())
505 FT = dyn_cast<FunctionProtoType>(AFT);
506
Douglas Gregor11a434a2012-04-10 20:14:15 +0000507 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000508 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000509 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000510 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000511 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000512 }
513
514 if (FT->isVariadic()) {
515 if (FD->getNumParams()) POut << ", ";
516 POut << "...";
517 }
518 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000519 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000520
Sam Weinig4e83bd22009-12-27 01:38:20 +0000521 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000522 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000523 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000524 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000525 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000526 POut << " volatile";
527 RefQualifierKind Ref = MD->getRefQualifier();
528 if (Ref == RQ_LValue)
529 POut << " &";
530 else if (Ref == RQ_RValue)
531 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000532 }
533
Douglas Gregor11a434a2012-04-10 20:14:15 +0000534 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
535 SpecsTy Specs;
536 const DeclContext *Ctx = FD->getDeclContext();
537 while (Ctx && isa<NamedDecl>(Ctx)) {
538 const ClassTemplateSpecializationDecl *Spec
539 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
540 if (Spec && !Spec->isExplicitSpecialization())
541 Specs.push_back(Spec);
542 Ctx = Ctx->getParent();
543 }
544
545 std::string TemplateParams;
546 llvm::raw_string_ostream TOut(TemplateParams);
547 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
548 I != E; ++I) {
549 const TemplateParameterList *Params
550 = (*I)->getSpecializedTemplate()->getTemplateParameters();
551 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
552 assert(Params->size() == Args.size());
553 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
554 StringRef Param = Params->getParam(i)->getName();
555 if (Param.empty()) continue;
556 TOut << Param << " = ";
557 Args.get(i).print(Policy, TOut);
558 TOut << ", ";
559 }
560 }
561
562 FunctionTemplateSpecializationInfo *FSI
563 = FD->getTemplateSpecializationInfo();
564 if (FSI && !FSI->isExplicitSpecialization()) {
565 const TemplateParameterList* Params
566 = FSI->getTemplate()->getTemplateParameters();
567 const TemplateArgumentList* Args = FSI->TemplateArguments;
568 assert(Params->size() == Args->size());
569 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
570 StringRef Param = Params->getParam(i)->getName();
571 if (Param.empty()) continue;
572 TOut << Param << " = ";
573 Args->get(i).print(Policy, TOut);
574 TOut << ", ";
575 }
576 }
577
578 TOut.flush();
579 if (!TemplateParams.empty()) {
580 // remove the trailing comma and space
581 TemplateParams.resize(TemplateParams.size() - 2);
582 POut << " [" << TemplateParams << "]";
583 }
584
585 POut.flush();
586
Benjamin Kramer90f54222013-08-21 11:45:27 +0000587 // Print "auto" for all deduced return types. This includes C++1y return
588 // type deduction and lambdas. For trailing return types resolve the
589 // decltype expression. Otherwise print the real type when this is
590 // not a constructor or destructor.
591 if ((isa<CXXMethodDecl>(FD) &&
592 cast<CXXMethodDecl>(FD)->getParent()->isLambda()) ||
Alp Toker314cc812014-01-25 16:55:45 +0000593 (FT && FT->getReturnType()->getAs<AutoType>()))
Benjamin Kramer90f54222013-08-21 11:45:27 +0000594 Proto = "auto " + Proto;
Alp Toker314cc812014-01-25 16:55:45 +0000595 else if (FT && FT->getReturnType()->getAs<DecltypeType>())
596 FT->getReturnType()
597 ->getAs<DecltypeType>()
598 ->getUnderlyingType()
Benjamin Kramer90f54222013-08-21 11:45:27 +0000599 .getAsStringInternal(Proto, Policy);
600 else if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
Alp Toker314cc812014-01-25 16:55:45 +0000601 AFT->getReturnType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000602
603 Out << Proto;
604
605 Out.flush();
606 return Name.str().str();
607 }
Wei Pan8d6b19a2013-08-26 14:27:34 +0000608 if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
609 for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
610 // Skip to its enclosing function or method, but not its enclosing
611 // CapturedDecl.
612 if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
613 const Decl *D = Decl::castFromDeclContext(DC);
614 return ComputeName(IT, D);
615 }
616 llvm_unreachable("CapturedDecl not inside a function or method");
617 }
Anders Carlsson2fb08242009-09-08 18:24:21 +0000618 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000619 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000620 llvm::raw_svector_ostream Out(Name);
621 Out << (MD->isInstanceMethod() ? '-' : '+');
622 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000623
624 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
625 // a null check to avoid a crash.
626 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000627 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000628
Anders Carlsson2fb08242009-09-08 18:24:21 +0000629 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000630 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000631 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000632
Anders Carlsson2fb08242009-09-08 18:24:21 +0000633 Out << ' ';
Aaron Ballmanb190f972014-01-03 17:59:55 +0000634 MD->getSelector().print(Out);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000635 Out << ']';
636
637 Out.flush();
638 return Name.str().str();
639 }
640 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
641 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
642 return "top level";
643 }
644 return "";
645}
646
Craig Topper37932912013-08-18 10:09:15 +0000647void APNumericStorage::setIntValue(const ASTContext &C,
648 const llvm::APInt &Val) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000649 if (hasAllocation())
650 C.Deallocate(pVal);
651
652 BitWidth = Val.getBitWidth();
653 unsigned NumWords = Val.getNumWords();
654 const uint64_t* Words = Val.getRawData();
655 if (NumWords > 1) {
656 pVal = new (C) uint64_t[NumWords];
657 std::copy(Words, Words + NumWords, pVal);
658 } else if (NumWords == 1)
659 VAL = Words[0];
660 else
661 VAL = 0;
662}
663
Craig Topper37932912013-08-18 10:09:15 +0000664IntegerLiteral::IntegerLiteral(const ASTContext &C, const llvm::APInt &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000665 QualType type, SourceLocation l)
666 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
667 false, false),
668 Loc(l) {
669 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
670 assert(V.getBitWidth() == C.getIntWidth(type) &&
671 "Integer type is not the correct size for constant.");
672 setValue(C, V);
673}
674
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000675IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000676IntegerLiteral::Create(const ASTContext &C, const llvm::APInt &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000677 QualType type, SourceLocation l) {
678 return new (C) IntegerLiteral(C, V, type, l);
679}
680
681IntegerLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000682IntegerLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000683 return new (C) IntegerLiteral(Empty);
684}
685
Craig Topper37932912013-08-18 10:09:15 +0000686FloatingLiteral::FloatingLiteral(const ASTContext &C, const llvm::APFloat &V,
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000687 bool isexact, QualType Type, SourceLocation L)
688 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
689 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000690 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000691 FloatingLiteralBits.IsExact = isexact;
692 setValue(C, V);
693}
694
Craig Topper37932912013-08-18 10:09:15 +0000695FloatingLiteral::FloatingLiteral(const ASTContext &C, EmptyShell Empty)
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000696 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000697 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000698 FloatingLiteralBits.IsExact = false;
699}
700
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000701FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000702FloatingLiteral::Create(const ASTContext &C, const llvm::APFloat &V,
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000703 bool isexact, QualType Type, SourceLocation L) {
704 return new (C) FloatingLiteral(C, V, isexact, Type, L);
705}
706
707FloatingLiteral *
Craig Topper37932912013-08-18 10:09:15 +0000708FloatingLiteral::Create(const ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000709 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000710}
711
Tim Northover178723a2013-01-22 09:46:51 +0000712const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
713 switch(FloatingLiteralBits.Semantics) {
714 case IEEEhalf:
715 return llvm::APFloat::IEEEhalf;
716 case IEEEsingle:
717 return llvm::APFloat::IEEEsingle;
718 case IEEEdouble:
719 return llvm::APFloat::IEEEdouble;
720 case x87DoubleExtended:
721 return llvm::APFloat::x87DoubleExtended;
722 case IEEEquad:
723 return llvm::APFloat::IEEEquad;
724 case PPCDoubleDouble:
725 return llvm::APFloat::PPCDoubleDouble;
726 }
727 llvm_unreachable("Unrecognised floating semantics");
728}
729
730void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
731 if (&Sem == &llvm::APFloat::IEEEhalf)
732 FloatingLiteralBits.Semantics = IEEEhalf;
733 else if (&Sem == &llvm::APFloat::IEEEsingle)
734 FloatingLiteralBits.Semantics = IEEEsingle;
735 else if (&Sem == &llvm::APFloat::IEEEdouble)
736 FloatingLiteralBits.Semantics = IEEEdouble;
737 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
738 FloatingLiteralBits.Semantics = x87DoubleExtended;
739 else if (&Sem == &llvm::APFloat::IEEEquad)
740 FloatingLiteralBits.Semantics = IEEEquad;
741 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
742 FloatingLiteralBits.Semantics = PPCDoubleDouble;
743 else
744 llvm_unreachable("Unknown floating semantics");
745}
746
Chris Lattnera0173132008-06-07 22:13:43 +0000747/// getValueAsApproximateDouble - This returns the value as an inaccurate
748/// double. Note that this may cause loss of precision, but is useful for
749/// debugging dumps, etc.
750double FloatingLiteral::getValueAsApproximateDouble() const {
751 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000752 bool ignored;
753 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
754 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000755 return V.convertToDouble();
756}
757
Nick Lewycky4ed84042012-02-24 09:07:53 +0000758int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000759 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000760 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000761 case Ascii:
762 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000763 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000764 break;
765 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000766 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000767 break;
768 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000769 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000770 break;
771 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000772 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000773 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000774 }
775 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
776 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000777 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000778 && "character byte widths supported are 1, 2, and 4 only");
779 return CharByteWidth;
780}
781
Craig Topper37932912013-08-18 10:09:15 +0000782StringLiteral *StringLiteral::Create(const ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000783 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000784 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000785 unsigned NumStrs) {
Benjamin Kramercdac7612014-02-25 12:26:20 +0000786 assert(C.getAsConstantArrayType(Ty) &&
787 "StringLiteral must be of constant array type!");
788
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000789 // Allocate enough space for the StringLiteral plus an array of locations for
790 // any concatenated string tokens.
791 void *Mem = C.Allocate(sizeof(StringLiteral)+
792 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000793 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000794 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Steve Naroffdf7855b2007-02-21 23:46:25 +0000796 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000797 SL->setString(C,Str,Kind,Pascal);
798
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000799 SL->TokLocs[0] = Loc[0];
800 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000801
Chris Lattner630970d2009-02-18 05:49:11 +0000802 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000803 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
804 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000805}
806
Craig Topper37932912013-08-18 10:09:15 +0000807StringLiteral *StringLiteral::CreateEmpty(const ASTContext &C,
808 unsigned NumStrs) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000809 void *Mem = C.Allocate(sizeof(StringLiteral)+
810 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000811 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000812 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000813 SL->CharByteWidth = 0;
814 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000815 SL->NumConcatenated = NumStrs;
816 return SL;
817}
818
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000819void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000820 switch (getKind()) {
821 case Ascii: break; // no prefix.
822 case Wide: OS << 'L'; break;
823 case UTF8: OS << "u8"; break;
824 case UTF16: OS << 'u'; break;
825 case UTF32: OS << 'U'; break;
826 }
827 OS << '"';
828 static const char Hex[] = "0123456789ABCDEF";
829
830 unsigned LastSlashX = getLength();
831 for (unsigned I = 0, N = getLength(); I != N; ++I) {
832 switch (uint32_t Char = getCodeUnit(I)) {
833 default:
834 // FIXME: Convert UTF-8 back to codepoints before rendering.
835
836 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
837 // Leave invalid surrogates alone; we'll use \x for those.
838 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
839 Char <= 0xdbff) {
840 uint32_t Trail = getCodeUnit(I + 1);
841 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
842 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
843 ++I;
844 }
845 }
846
847 if (Char > 0xff) {
848 // If this is a wide string, output characters over 0xff using \x
849 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
850 // codepoint: use \x escapes for invalid codepoints.
851 if (getKind() == Wide ||
852 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
853 // FIXME: Is this the best way to print wchar_t?
854 OS << "\\x";
855 int Shift = 28;
856 while ((Char >> Shift) == 0)
857 Shift -= 4;
858 for (/**/; Shift >= 0; Shift -= 4)
859 OS << Hex[(Char >> Shift) & 15];
860 LastSlashX = I;
861 break;
862 }
863
864 if (Char > 0xffff)
865 OS << "\\U00"
866 << Hex[(Char >> 20) & 15]
867 << Hex[(Char >> 16) & 15];
868 else
869 OS << "\\u";
870 OS << Hex[(Char >> 12) & 15]
871 << Hex[(Char >> 8) & 15]
872 << Hex[(Char >> 4) & 15]
873 << Hex[(Char >> 0) & 15];
874 break;
875 }
876
877 // If we used \x... for the previous character, and this character is a
878 // hexadecimal digit, prevent it being slurped as part of the \x.
879 if (LastSlashX + 1 == I) {
880 switch (Char) {
881 case '0': case '1': case '2': case '3': case '4':
882 case '5': case '6': case '7': case '8': case '9':
883 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
884 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
885 OS << "\"\"";
886 }
887 }
888
889 assert(Char <= 0xff &&
890 "Characters above 0xff should already have been handled.");
891
Jordan Rosea7d03842013-02-08 22:30:41 +0000892 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000893 OS << (char)Char;
894 else // Output anything hard as an octal escape.
895 OS << '\\'
896 << (char)('0' + ((Char >> 6) & 7))
897 << (char)('0' + ((Char >> 3) & 7))
898 << (char)('0' + ((Char >> 0) & 7));
899 break;
900 // Handle some common non-printable cases to make dumps prettier.
901 case '\\': OS << "\\\\"; break;
902 case '"': OS << "\\\""; break;
903 case '\n': OS << "\\n"; break;
904 case '\t': OS << "\\t"; break;
905 case '\a': OS << "\\a"; break;
906 case '\b': OS << "\\b"; break;
907 }
908 }
909 OS << '"';
910}
911
Craig Topper37932912013-08-18 10:09:15 +0000912void StringLiteral::setString(const ASTContext &C, StringRef Str,
Eli Friedmanfcec6302011-11-01 02:23:42 +0000913 StringKind Kind, bool IsPascal) {
914 //FIXME: we assume that the string data comes from a target that uses the same
915 // code unit size and endianess for the type of string.
916 this->Kind = Kind;
917 this->IsPascal = IsPascal;
918
Nick Lewycky4ed84042012-02-24 09:07:53 +0000919 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000920 assert((Str.size()%CharByteWidth == 0)
921 && "size of data must be multiple of CharByteWidth");
922 Length = Str.size()/CharByteWidth;
923
924 switch(CharByteWidth) {
925 case 1: {
926 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000927 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000928 StrData.asChar = AStrData;
929 break;
930 }
931 case 2: {
932 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000933 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000934 StrData.asUInt16 = AStrData;
935 break;
936 }
937 case 4: {
938 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000939 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000940 StrData.asUInt32 = AStrData;
941 break;
942 }
943 default:
944 assert(false && "unsupported CharByteWidth");
945 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000946}
947
Chris Lattnere925d612010-11-17 07:37:15 +0000948/// getLocationOfByte - Return a source location that points to the specified
949/// byte of this string literal.
950///
951/// Strings are amazingly complex. They can be formed from multiple tokens and
952/// can have escape sequences in them in addition to the usual trigraph and
953/// escaped newline business. This routine handles this complexity.
954///
955SourceLocation StringLiteral::
956getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
957 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000958 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
959 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000960
Chris Lattnere925d612010-11-17 07:37:15 +0000961 // Loop over all of the tokens in this string until we find the one that
962 // contains the byte we're looking for.
963 unsigned TokNo = 0;
964 while (1) {
965 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
966 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
967
968 // Get the spelling of the string so that we can get the data that makes up
969 // the string literal, not the identifier for the macro it is potentially
970 // expanded through.
971 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
972
973 // Re-lex the token to get its length and original spelling.
974 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
975 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000976 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000977 if (Invalid)
978 return StrTokSpellingLoc;
979
980 const char *StrData = Buffer.data()+LocInfo.second;
981
Chris Lattnere925d612010-11-17 07:37:15 +0000982 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000983 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
984 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000985 Token TheTok;
986 TheLexer.LexFromRawLexer(TheTok);
987
988 // Use the StringLiteralParser to compute the length of the string in bytes.
989 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
990 unsigned TokNumBytes = SLP.GetStringLength();
991
992 // If the byte is in this token, return the location of the byte.
993 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000994 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000995 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
996
997 // Now that we know the offset of the token in the spelling, use the
998 // preprocessor to get the offset in the original source.
999 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
1000 }
1001
1002 // Move to the next string token.
1003 ++TokNo;
1004 ByteNo -= TokNumBytes;
1005 }
1006}
1007
1008
1009
Chris Lattner1b926492006-08-23 06:42:10 +00001010/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1011/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +00001012StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001013 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001014 case UO_PostInc: return "++";
1015 case UO_PostDec: return "--";
1016 case UO_PreInc: return "++";
1017 case UO_PreDec: return "--";
1018 case UO_AddrOf: return "&";
1019 case UO_Deref: return "*";
1020 case UO_Plus: return "+";
1021 case UO_Minus: return "-";
1022 case UO_Not: return "~";
1023 case UO_LNot: return "!";
1024 case UO_Real: return "__real";
1025 case UO_Imag: return "__imag";
1026 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001027 }
David Blaikief47fa302012-01-17 02:30:50 +00001028 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001029}
1030
John McCalle3027922010-08-25 11:45:40 +00001031UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001032UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1033 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001034 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001035 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1036 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1037 case OO_Amp: return UO_AddrOf;
1038 case OO_Star: return UO_Deref;
1039 case OO_Plus: return UO_Plus;
1040 case OO_Minus: return UO_Minus;
1041 case OO_Tilde: return UO_Not;
1042 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001043 }
1044}
1045
1046OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1047 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001048 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1049 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1050 case UO_AddrOf: return OO_Amp;
1051 case UO_Deref: return OO_Star;
1052 case UO_Plus: return OO_Plus;
1053 case UO_Minus: return OO_Minus;
1054 case UO_Not: return OO_Tilde;
1055 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001056 default: return OO_None;
1057 }
1058}
1059
1060
Chris Lattner0eedafe2006-08-24 04:56:27 +00001061//===----------------------------------------------------------------------===//
1062// Postfix Operators.
1063//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001064
Craig Topper37932912013-08-18 10:09:15 +00001065CallExpr::CallExpr(const ASTContext& C, StmtClass SC, Expr *fn,
1066 unsigned NumPreArgs, ArrayRef<Expr*> args, QualType t,
1067 ExprValueKind VK, SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00001068 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001069 fn->isTypeDependent(),
1070 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001071 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001072 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001073 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001074
Benjamin Kramerc215e762012-08-24 11:54:20 +00001075 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001076 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001077 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001078 if (args[i]->isTypeDependent())
1079 ExprBits.TypeDependent = true;
1080 if (args[i]->isValueDependent())
1081 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001082 if (args[i]->isInstantiationDependent())
1083 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001084 if (args[i]->containsUnexpandedParameterPack())
1085 ExprBits.ContainsUnexpandedParameterPack = true;
1086
Peter Collingbourne3a347252011-02-08 21:18:02 +00001087 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001088 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001089
Peter Collingbourne3a347252011-02-08 21:18:02 +00001090 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001091 RParenLoc = rparenloc;
1092}
Nate Begeman1e36a852008-01-17 17:46:27 +00001093
Craig Topper37932912013-08-18 10:09:15 +00001094CallExpr::CallExpr(const ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001095 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1096 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001097 fn->isTypeDependent(),
1098 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001099 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001100 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001101 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001102
Benjamin Kramerc215e762012-08-24 11:54:20 +00001103 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001104 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001105 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001106 if (args[i]->isTypeDependent())
1107 ExprBits.TypeDependent = true;
1108 if (args[i]->isValueDependent())
1109 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001110 if (args[i]->isInstantiationDependent())
1111 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001112 if (args[i]->containsUnexpandedParameterPack())
1113 ExprBits.ContainsUnexpandedParameterPack = true;
1114
Peter Collingbourne3a347252011-02-08 21:18:02 +00001115 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001116 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001117
Peter Collingbourne3a347252011-02-08 21:18:02 +00001118 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001119 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001120}
1121
Craig Topper37932912013-08-18 10:09:15 +00001122CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty)
Mike Stump11289f42009-09-09 15:08:12 +00001123 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001124 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001125 SubExprs = new (C) Stmt*[PREARGS_START];
1126 CallExprBits.NumPreArgs = 0;
1127}
1128
Craig Topper37932912013-08-18 10:09:15 +00001129CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs,
Peter Collingbourne3a347252011-02-08 21:18:02 +00001130 EmptyShell Empty)
1131 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1132 // FIXME: Why do we allocate this?
1133 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1134 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001135}
1136
Nuno Lopes518e3702009-12-20 23:11:08 +00001137Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001138 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001139
1140 while (SubstNonTypeTemplateParmExpr *NTTP
1141 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1142 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1143 }
1144
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001145 // If we're calling a dereference, look at the pointer instead.
1146 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1147 if (BO->isPtrMemOp())
1148 CEE = BO->getRHS()->IgnoreParenCasts();
1149 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1150 if (UO->getOpcode() == UO_Deref)
1151 CEE = UO->getSubExpr()->IgnoreParenCasts();
1152 }
Chris Lattner52301912009-07-17 15:46:27 +00001153 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001154 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001155 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1156 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001157
1158 return 0;
1159}
1160
Nuno Lopes518e3702009-12-20 23:11:08 +00001161FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001162 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001163}
1164
Chris Lattnere4407ed2007-12-28 05:25:02 +00001165/// setNumArgs - This changes the number of arguments present in this call.
1166/// Any orphaned expressions are deleted by this, and any new operands are set
1167/// to null.
Craig Topper37932912013-08-18 10:09:15 +00001168void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001169 // No change, just return.
1170 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001171
Chris Lattnere4407ed2007-12-28 05:25:02 +00001172 // If shrinking # arguments, just delete the extras and forgot them.
1173 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001174 this->NumArgs = NumArgs;
1175 return;
1176 }
1177
1178 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001179 unsigned NumPreArgs = getNumPreArgs();
1180 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001181 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001182 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001183 NewSubExprs[i] = SubExprs[i];
1184 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001185 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1186 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001187 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001188
Douglas Gregorba6e5572009-04-17 21:46:47 +00001189 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001190 SubExprs = NewSubExprs;
1191 this->NumArgs = NumArgs;
1192}
1193
Alp Tokera724cff2013-12-28 21:59:02 +00001194/// getBuiltinCallee - If this is a call to a builtin, return the builtin ID. If
Chris Lattner01ff98a2008-10-06 05:00:53 +00001195/// not, return 0.
Alp Tokera724cff2013-12-28 21:59:02 +00001196unsigned CallExpr::getBuiltinCallee() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001197 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001198 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001199 // ImplicitCastExpr.
1200 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1201 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001202 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Steve Narofff6e3b3292008-01-31 01:07:12 +00001204 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1205 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001206 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001208 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1209 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001210 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001212 if (!FDecl->getIdentifier())
1213 return 0;
1214
Douglas Gregor15fc9562009-09-12 00:22:50 +00001215 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001216}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001217
Richard Smith5011a002013-01-17 23:46:04 +00001218bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
Alp Tokera724cff2013-12-28 21:59:02 +00001219 if (unsigned BI = getBuiltinCallee())
Richard Smith5011a002013-01-17 23:46:04 +00001220 return Ctx.BuiltinInfo.isUnevaluated(BI);
1221 return false;
1222}
1223
Anders Carlsson00a27592009-05-26 04:57:27 +00001224QualType CallExpr::getCallReturnType() const {
1225 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001226 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001227 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001228 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001229 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001230 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1231 // This should never be overloaded and so should never return null.
1232 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001233
John McCall0009fcc2011-04-26 20:42:42 +00001234 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00001235 return FnType->getReturnType();
Anders Carlsson00a27592009-05-26 04:57:27 +00001236}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001237
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001238SourceLocation CallExpr::getLocStart() const {
1239 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001240 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001241
1242 SourceLocation begin = getCallee()->getLocStart();
1243 if (begin.isInvalid() && getNumArgs() > 0)
1244 begin = getArg(0)->getLocStart();
1245 return begin;
1246}
1247SourceLocation CallExpr::getLocEnd() const {
1248 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001249 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001250
1251 SourceLocation end = getRParenLoc();
1252 if (end.isInvalid() && getNumArgs() > 0)
1253 end = getArg(getNumArgs() - 1)->getLocEnd();
1254 return end;
1255}
John McCall701417a2011-02-21 06:23:05 +00001256
Craig Topper37932912013-08-18 10:09:15 +00001257OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001258 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001259 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001260 ArrayRef<OffsetOfNode> comps,
1261 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001262 SourceLocation RParenLoc) {
1263 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001264 sizeof(OffsetOfNode) * comps.size() +
1265 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001266
Benjamin Kramerc215e762012-08-24 11:54:20 +00001267 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1268 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001269}
1270
Craig Topper37932912013-08-18 10:09:15 +00001271OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor882211c2010-04-28 22:16:22 +00001272 unsigned numComps, unsigned numExprs) {
1273 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1274 sizeof(OffsetOfNode) * numComps +
1275 sizeof(Expr*) * numExprs);
1276 return new (Mem) OffsetOfExpr(numComps, numExprs);
1277}
1278
Craig Topper37932912013-08-18 10:09:15 +00001279OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001280 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001281 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001282 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001283 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1284 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001285 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001286 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001287 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001288 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001289 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001290{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001291 for (unsigned i = 0; i != comps.size(); ++i) {
1292 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001293 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001294
Benjamin Kramerc215e762012-08-24 11:54:20 +00001295 for (unsigned i = 0; i != exprs.size(); ++i) {
1296 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001297 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001298 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001299 ExprBits.ContainsUnexpandedParameterPack = true;
1300
Benjamin Kramerc215e762012-08-24 11:54:20 +00001301 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001302 }
1303}
1304
1305IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1306 assert(getKind() == Field || getKind() == Identifier);
1307 if (getKind() == Field)
1308 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001309
Douglas Gregor882211c2010-04-28 22:16:22 +00001310 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1311}
1312
Craig Topper37932912013-08-18 10:09:15 +00001313MemberExpr *MemberExpr::Create(const ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001314 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001315 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001316 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001317 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001318 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001319 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001320 QualType ty,
1321 ExprValueKind vk,
1322 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001323 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001324
Douglas Gregorea972d32011-02-28 21:54:11 +00001325 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001326 founddecl.getDecl() != memberdecl ||
1327 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001328 if (hasQualOrFound)
1329 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001330
John McCall6b51f282009-11-23 01:53:49 +00001331 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001332 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1333 else if (TemplateKWLoc.isValid())
1334 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattner5c0b4052010-10-30 05:14:06 +00001336 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001337 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1338 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001339
1340 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001341 // FIXME: Wrong. We should be looking at the member declaration we found.
1342 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001343 E->setValueDependent(true);
1344 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001345 E->setInstantiationDependent(true);
1346 }
1347 else if (QualifierLoc &&
1348 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1349 E->setInstantiationDependent(true);
1350
John McCall16df1e52010-03-30 21:47:33 +00001351 E->HasQualifierOrFoundDecl = true;
1352
1353 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001354 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001355 NQ->FoundDecl = founddecl;
1356 }
1357
Abramo Bagnara7945c982012-01-27 09:46:47 +00001358 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1359
John McCall16df1e52010-03-30 21:47:33 +00001360 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001361 bool Dependent = false;
1362 bool InstantiationDependent = false;
1363 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001364 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1365 Dependent,
1366 InstantiationDependent,
1367 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001368 if (InstantiationDependent)
1369 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001370 } else if (TemplateKWLoc.isValid()) {
1371 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001372 }
1373
1374 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001375}
1376
Daniel Dunbarb507f272012-03-09 15:39:15 +00001377SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001378 if (isImplicitAccess()) {
1379 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001380 return getQualifierLoc().getBeginLoc();
1381 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001382 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001383
Daniel Dunbarb507f272012-03-09 15:39:15 +00001384 // FIXME: We don't want this to happen. Rather, we should be able to
1385 // detect all kinds of implicit accesses more cleanly.
1386 SourceLocation BaseStartLoc = getBase()->getLocStart();
1387 if (BaseStartLoc.isValid())
1388 return BaseStartLoc;
1389 return MemberLoc;
1390}
1391SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001392 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001393 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001394 EndLoc = getRAngleLoc();
1395 else if (EndLoc.isInvalid())
1396 EndLoc = getBase()->getLocEnd();
1397 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001398}
1399
Alp Tokerc1086762013-12-07 13:51:35 +00001400bool CastExpr::CastConsistency() const {
John McCall9320b872011-09-09 05:25:32 +00001401 switch (getCastKind()) {
1402 case CK_DerivedToBase:
1403 case CK_UncheckedDerivedToBase:
1404 case CK_DerivedToBaseMemberPointer:
1405 case CK_BaseToDerived:
1406 case CK_BaseToDerivedMemberPointer:
1407 assert(!path_empty() && "Cast kind should have a base path!");
1408 break;
1409
1410 case CK_CPointerToObjCPointerCast:
1411 assert(getType()->isObjCObjectPointerType());
1412 assert(getSubExpr()->getType()->isPointerType());
1413 goto CheckNoBasePath;
1414
1415 case CK_BlockPointerToObjCPointerCast:
1416 assert(getType()->isObjCObjectPointerType());
1417 assert(getSubExpr()->getType()->isBlockPointerType());
1418 goto CheckNoBasePath;
1419
John McCallc62bb392012-02-15 01:22:51 +00001420 case CK_ReinterpretMemberPointer:
1421 assert(getType()->isMemberPointerType());
1422 assert(getSubExpr()->getType()->isMemberPointerType());
1423 goto CheckNoBasePath;
1424
John McCall9320b872011-09-09 05:25:32 +00001425 case CK_BitCast:
1426 // Arbitrary casts to C pointer types count as bitcasts.
1427 // Otherwise, we should only have block and ObjC pointer casts
1428 // here if they stay within the type kind.
1429 if (!getType()->isPointerType()) {
1430 assert(getType()->isObjCObjectPointerType() ==
1431 getSubExpr()->getType()->isObjCObjectPointerType());
1432 assert(getType()->isBlockPointerType() ==
1433 getSubExpr()->getType()->isBlockPointerType());
1434 }
1435 goto CheckNoBasePath;
1436
1437 case CK_AnyPointerToBlockPointerCast:
1438 assert(getType()->isBlockPointerType());
1439 assert(getSubExpr()->getType()->isAnyPointerType() &&
1440 !getSubExpr()->getType()->isBlockPointerType());
1441 goto CheckNoBasePath;
1442
Douglas Gregored90df32012-02-22 05:02:47 +00001443 case CK_CopyAndAutoreleaseBlockObject:
1444 assert(getType()->isBlockPointerType());
1445 assert(getSubExpr()->getType()->isBlockPointerType());
1446 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001447
1448 case CK_FunctionToPointerDecay:
1449 assert(getType()->isPointerType());
1450 assert(getSubExpr()->getType()->isFunctionType());
1451 goto CheckNoBasePath;
1452
David Tweede1468322013-12-11 13:39:46 +00001453 case CK_AddressSpaceConversion:
1454 assert(getType()->isPointerType());
1455 assert(getSubExpr()->getType()->isPointerType());
1456 assert(getType()->getPointeeType().getAddressSpace() !=
1457 getSubExpr()->getType()->getPointeeType().getAddressSpace());
John McCall9320b872011-09-09 05:25:32 +00001458 // These should not have an inheritance path.
1459 case CK_Dynamic:
1460 case CK_ToUnion:
1461 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001462 case CK_NullToMemberPointer:
1463 case CK_NullToPointer:
1464 case CK_ConstructorConversion:
1465 case CK_IntegralToPointer:
1466 case CK_PointerToIntegral:
1467 case CK_ToVoid:
1468 case CK_VectorSplat:
1469 case CK_IntegralCast:
1470 case CK_IntegralToFloating:
1471 case CK_FloatingToIntegral:
1472 case CK_FloatingCast:
1473 case CK_ObjCObjectLValueCast:
1474 case CK_FloatingRealToComplex:
1475 case CK_FloatingComplexToReal:
1476 case CK_FloatingComplexCast:
1477 case CK_FloatingComplexToIntegralComplex:
1478 case CK_IntegralRealToComplex:
1479 case CK_IntegralComplexToReal:
1480 case CK_IntegralComplexCast:
1481 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001482 case CK_ARCProduceObject:
1483 case CK_ARCConsumeObject:
1484 case CK_ARCReclaimReturnedObject:
1485 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001486 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001487 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1488 goto CheckNoBasePath;
1489
1490 case CK_Dependent:
1491 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001492 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001493 case CK_AtomicToNonAtomic:
1494 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001495 case CK_PointerToBoolean:
1496 case CK_IntegralToBoolean:
1497 case CK_FloatingToBoolean:
1498 case CK_MemberPointerToBoolean:
1499 case CK_FloatingComplexToBoolean:
1500 case CK_IntegralComplexToBoolean:
1501 case CK_LValueBitCast: // -> bool&
1502 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001503 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001504 CheckNoBasePath:
1505 assert(path_empty() && "Cast kind should not have a base path!");
1506 break;
1507 }
Alp Tokerc1086762013-12-07 13:51:35 +00001508 return true;
John McCall9320b872011-09-09 05:25:32 +00001509}
1510
Anders Carlsson496335e2009-09-03 00:59:21 +00001511const char *CastExpr::getCastKindName() const {
1512 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001513 case CK_Dependent:
1514 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001515 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001516 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001517 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001518 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001519 case CK_LValueToRValue:
1520 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001521 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001522 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001523 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001524 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001525 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001526 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001527 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001528 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001529 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001530 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001531 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001532 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001533 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001534 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001535 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001536 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001537 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001538 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001539 case CK_NullToPointer:
1540 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001541 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001542 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001543 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001544 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001545 case CK_ReinterpretMemberPointer:
1546 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001547 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001548 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001549 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001550 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001551 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001552 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001553 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001554 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001555 case CK_PointerToBoolean:
1556 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001557 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001558 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001559 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001560 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001561 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001562 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001563 case CK_IntegralToBoolean:
1564 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001565 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001566 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001567 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001568 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001569 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001570 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001571 case CK_FloatingToBoolean:
1572 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001573 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001574 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001575 case CK_CPointerToObjCPointerCast:
1576 return "CPointerToObjCPointerCast";
1577 case CK_BlockPointerToObjCPointerCast:
1578 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001579 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001580 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001581 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001582 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001583 case CK_FloatingRealToComplex:
1584 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001585 case CK_FloatingComplexToReal:
1586 return "FloatingComplexToReal";
1587 case CK_FloatingComplexToBoolean:
1588 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001589 case CK_FloatingComplexCast:
1590 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001591 case CK_FloatingComplexToIntegralComplex:
1592 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001593 case CK_IntegralRealToComplex:
1594 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001595 case CK_IntegralComplexToReal:
1596 return "IntegralComplexToReal";
1597 case CK_IntegralComplexToBoolean:
1598 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001599 case CK_IntegralComplexCast:
1600 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001601 case CK_IntegralComplexToFloatingComplex:
1602 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001603 case CK_ARCConsumeObject:
1604 return "ARCConsumeObject";
1605 case CK_ARCProduceObject:
1606 return "ARCProduceObject";
1607 case CK_ARCReclaimReturnedObject:
1608 return "ARCReclaimReturnedObject";
1609 case CK_ARCExtendBlockObject:
Jordan Rose749b5812014-02-05 03:49:45 +00001610 return "ARCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001611 case CK_AtomicToNonAtomic:
1612 return "AtomicToNonAtomic";
1613 case CK_NonAtomicToAtomic:
1614 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001615 case CK_CopyAndAutoreleaseBlockObject:
1616 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001617 case CK_BuiltinFnToFnPtr:
1618 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001619 case CK_ZeroToOCLEvent:
1620 return "ZeroToOCLEvent";
David Tweede1468322013-12-11 13:39:46 +00001621 case CK_AddressSpaceConversion:
1622 return "AddressSpaceConversion";
Anders Carlsson496335e2009-09-03 00:59:21 +00001623 }
Mike Stump11289f42009-09-09 15:08:12 +00001624
John McCallc5e62b42010-11-13 09:02:35 +00001625 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001626}
1627
Douglas Gregord196a582009-12-14 19:27:10 +00001628Expr *CastExpr::getSubExprAsWritten() {
1629 Expr *SubExpr = 0;
1630 CastExpr *E = this;
1631 do {
1632 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001633
1634 // Skip through reference binding to temporary.
1635 if (MaterializeTemporaryExpr *Materialize
1636 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1637 SubExpr = Materialize->GetTemporaryExpr();
1638
Douglas Gregord196a582009-12-14 19:27:10 +00001639 // Skip any temporary bindings; they're implicit.
1640 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1641 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001642
Douglas Gregord196a582009-12-14 19:27:10 +00001643 // Conversions by constructor and conversion functions have a
1644 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001645 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001646 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001647 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001648 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001649
Douglas Gregord196a582009-12-14 19:27:10 +00001650 // If the subexpression we're left with is an implicit cast, look
1651 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001652 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1653
Douglas Gregord196a582009-12-14 19:27:10 +00001654 return SubExpr;
1655}
1656
John McCallcf142162010-08-07 06:22:56 +00001657CXXBaseSpecifier **CastExpr::path_buffer() {
1658 switch (getStmtClass()) {
1659#define ABSTRACT_STMT(x)
1660#define CASTEXPR(Type, Base) \
1661 case Stmt::Type##Class: \
1662 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1663#define STMT(Type, Base)
1664#include "clang/AST/StmtNodes.inc"
1665 default:
1666 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001667 }
1668}
1669
1670void CastExpr::setCastPath(const CXXCastPath &Path) {
1671 assert(Path.size() == path_size());
1672 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1673}
1674
Craig Topper37932912013-08-18 10:09:15 +00001675ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T,
John McCallcf142162010-08-07 06:22:56 +00001676 CastKind Kind, Expr *Operand,
1677 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001678 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1680 void *Buffer =
1681 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1682 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001683 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001684 if (PathSize) E->setCastPath(*BasePath);
1685 return E;
1686}
1687
Craig Topper37932912013-08-18 10:09:15 +00001688ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +00001689 unsigned PathSize) {
1690 void *Buffer =
1691 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1692 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1693}
1694
1695
Craig Topper37932912013-08-18 10:09:15 +00001696CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001697 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001698 const CXXCastPath *BasePath,
1699 TypeSourceInfo *WrittenTy,
1700 SourceLocation L, SourceLocation R) {
1701 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1702 void *Buffer =
1703 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1704 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001705 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001706 if (PathSize) E->setCastPath(*BasePath);
1707 return E;
1708}
1709
Craig Topper37932912013-08-18 10:09:15 +00001710CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C,
1711 unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +00001712 void *Buffer =
1713 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1714 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1715}
1716
Chris Lattner1b926492006-08-23 06:42:10 +00001717/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1718/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001719StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001720 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001721 case BO_PtrMemD: return ".*";
1722 case BO_PtrMemI: return "->*";
1723 case BO_Mul: return "*";
1724 case BO_Div: return "/";
1725 case BO_Rem: return "%";
1726 case BO_Add: return "+";
1727 case BO_Sub: return "-";
1728 case BO_Shl: return "<<";
1729 case BO_Shr: return ">>";
1730 case BO_LT: return "<";
1731 case BO_GT: return ">";
1732 case BO_LE: return "<=";
1733 case BO_GE: return ">=";
1734 case BO_EQ: return "==";
1735 case BO_NE: return "!=";
1736 case BO_And: return "&";
1737 case BO_Xor: return "^";
1738 case BO_Or: return "|";
1739 case BO_LAnd: return "&&";
1740 case BO_LOr: return "||";
1741 case BO_Assign: return "=";
1742 case BO_MulAssign: return "*=";
1743 case BO_DivAssign: return "/=";
1744 case BO_RemAssign: return "%=";
1745 case BO_AddAssign: return "+=";
1746 case BO_SubAssign: return "-=";
1747 case BO_ShlAssign: return "<<=";
1748 case BO_ShrAssign: return ">>=";
1749 case BO_AndAssign: return "&=";
1750 case BO_XorAssign: return "^=";
1751 case BO_OrAssign: return "|=";
1752 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001753 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001754
David Blaikiee4d798f2012-01-20 21:50:17 +00001755 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001756}
Steve Naroff47500512007-04-19 23:00:49 +00001757
John McCalle3027922010-08-25 11:45:40 +00001758BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001759BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1760 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001761 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001762 case OO_Plus: return BO_Add;
1763 case OO_Minus: return BO_Sub;
1764 case OO_Star: return BO_Mul;
1765 case OO_Slash: return BO_Div;
1766 case OO_Percent: return BO_Rem;
1767 case OO_Caret: return BO_Xor;
1768 case OO_Amp: return BO_And;
1769 case OO_Pipe: return BO_Or;
1770 case OO_Equal: return BO_Assign;
1771 case OO_Less: return BO_LT;
1772 case OO_Greater: return BO_GT;
1773 case OO_PlusEqual: return BO_AddAssign;
1774 case OO_MinusEqual: return BO_SubAssign;
1775 case OO_StarEqual: return BO_MulAssign;
1776 case OO_SlashEqual: return BO_DivAssign;
1777 case OO_PercentEqual: return BO_RemAssign;
1778 case OO_CaretEqual: return BO_XorAssign;
1779 case OO_AmpEqual: return BO_AndAssign;
1780 case OO_PipeEqual: return BO_OrAssign;
1781 case OO_LessLess: return BO_Shl;
1782 case OO_GreaterGreater: return BO_Shr;
1783 case OO_LessLessEqual: return BO_ShlAssign;
1784 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1785 case OO_EqualEqual: return BO_EQ;
1786 case OO_ExclaimEqual: return BO_NE;
1787 case OO_LessEqual: return BO_LE;
1788 case OO_GreaterEqual: return BO_GE;
1789 case OO_AmpAmp: return BO_LAnd;
1790 case OO_PipePipe: return BO_LOr;
1791 case OO_Comma: return BO_Comma;
1792 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001793 }
1794}
1795
1796OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1797 static const OverloadedOperatorKind OverOps[] = {
1798 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1799 OO_Star, OO_Slash, OO_Percent,
1800 OO_Plus, OO_Minus,
1801 OO_LessLess, OO_GreaterGreater,
1802 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1803 OO_EqualEqual, OO_ExclaimEqual,
1804 OO_Amp,
1805 OO_Caret,
1806 OO_Pipe,
1807 OO_AmpAmp,
1808 OO_PipePipe,
1809 OO_Equal, OO_StarEqual,
1810 OO_SlashEqual, OO_PercentEqual,
1811 OO_PlusEqual, OO_MinusEqual,
1812 OO_LessLessEqual, OO_GreaterGreaterEqual,
1813 OO_AmpEqual, OO_CaretEqual,
1814 OO_PipeEqual,
1815 OO_Comma
1816 };
1817 return OverOps[Opc];
1818}
1819
Craig Topper37932912013-08-18 10:09:15 +00001820InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001821 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001822 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001823 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001824 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001825 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001826{
1827 sawArrayRangeDesignator(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001828 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001829 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001830 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001831 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001832 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001833 if (initExprs[I]->isInstantiationDependent())
1834 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001835 if (initExprs[I]->containsUnexpandedParameterPack())
1836 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001837 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001838
Benjamin Kramerc215e762012-08-24 11:54:20 +00001839 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001840}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001841
Craig Topper37932912013-08-18 10:09:15 +00001842void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001843 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001844 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001845}
1846
Craig Topper37932912013-08-18 10:09:15 +00001847void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001848 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001849}
1850
Craig Topper37932912013-08-18 10:09:15 +00001851Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001852 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001853 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Richard Smithc275da62013-12-06 01:27:24 +00001854 setInit(Init, expr);
Ted Kremenek013041e2010-02-19 01:50:18 +00001855 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001856 }
Mike Stump11289f42009-09-09 15:08:12 +00001857
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001858 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
Richard Smithc275da62013-12-06 01:27:24 +00001859 setInit(Init, expr);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001860 return Result;
1861}
1862
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001863void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001864 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001865 ArrayFillerOrUnionFieldInit = filler;
1866 // Fill out any "holes" in the array due to designated initializers.
1867 Expr **inits = getInits();
1868 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1869 if (inits[i] == 0)
1870 inits[i] = filler;
1871}
1872
Richard Smith9ec1e482012-04-15 02:50:59 +00001873bool InitListExpr::isStringLiteralInit() const {
1874 if (getNumInits() != 1)
1875 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001876 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1877 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001878 return false;
Ted Kremenek256bd962014-01-19 06:31:34 +00001879 // It is possible for getInit() to return null.
1880 const Expr *Init = getInit(0);
1881 if (!Init)
1882 return false;
1883 Init = Init->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001884 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1885}
1886
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001887SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001888 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001889 return SyntacticForm->getLocStart();
1890 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001891 if (Beg.isInvalid()) {
1892 // Find the first non-null initializer.
1893 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1894 E = InitExprs.end();
1895 I != E; ++I) {
1896 if (Stmt *S = *I) {
1897 Beg = S->getLocStart();
1898 break;
1899 }
1900 }
1901 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001902 return Beg;
1903}
1904
1905SourceLocation InitListExpr::getLocEnd() const {
1906 if (InitListExpr *SyntacticForm = getSyntacticForm())
1907 return SyntacticForm->getLocEnd();
1908 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001909 if (End.isInvalid()) {
1910 // Find the first non-null initializer from the end.
1911 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001912 E = InitExprs.rend();
1913 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001914 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001915 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001916 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001917 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001918 }
1919 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001920 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001921}
1922
Steve Naroff991e99d2008-09-04 15:31:07 +00001923/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001924///
John McCallc833dea2012-02-17 03:32:35 +00001925const FunctionProtoType *BlockExpr::getFunctionType() const {
1926 // The block pointer is never sugared, but the function type might be.
1927 return cast<BlockPointerType>(getType())
1928 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001929}
1930
Mike Stump11289f42009-09-09 15:08:12 +00001931SourceLocation BlockExpr::getCaretLocation() const {
1932 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001933}
Mike Stump11289f42009-09-09 15:08:12 +00001934const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001935 return TheBlock->getBody();
1936}
Mike Stump11289f42009-09-09 15:08:12 +00001937Stmt *BlockExpr::getBody() {
1938 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001939}
Steve Naroff415d3d52008-10-08 17:01:13 +00001940
1941
Chris Lattner1ec5f562007-06-27 05:38:08 +00001942//===----------------------------------------------------------------------===//
1943// Generic Expression Routines
1944//===----------------------------------------------------------------------===//
1945
Chris Lattner237f2752009-02-14 07:37:35 +00001946/// isUnusedResultAWarning - Return true if this immediate expression should
1947/// be warned about if the result is unused. If so, fill in Loc and Ranges
1948/// with location to warn on and the source range[s] to report with the
1949/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001950bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1951 SourceRange &R1, SourceRange &R2,
1952 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001953 // Don't warn if the expr is type dependent. The type could end up
1954 // instantiating to void.
1955 if (isTypeDependent())
1956 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Chris Lattner1ec5f562007-06-27 05:38:08 +00001958 switch (getStmtClass()) {
1959 default:
John McCallc493a732010-03-12 07:11:26 +00001960 if (getType()->isVoidType())
1961 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001962 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001963 Loc = getExprLoc();
1964 R1 = getSourceRange();
1965 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001966 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001967 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001968 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001969 case GenericSelectionExprClass:
1970 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001971 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman75807f22013-07-20 00:40:58 +00001972 case ChooseExprClass:
1973 return cast<ChooseExpr>(this)->getChosenSubExpr()->
1974 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001975 case UnaryOperatorClass: {
1976 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001977
Chris Lattner1ec5f562007-06-27 05:38:08 +00001978 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001979 case UO_Plus:
1980 case UO_Minus:
1981 case UO_AddrOf:
1982 case UO_Not:
1983 case UO_LNot:
1984 case UO_Deref:
1985 break;
John McCalle3027922010-08-25 11:45:40 +00001986 case UO_PostInc:
1987 case UO_PostDec:
1988 case UO_PreInc:
1989 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001990 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001991 case UO_Real:
1992 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001993 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001994 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1995 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001996 return false;
1997 break;
John McCalle3027922010-08-25 11:45:40 +00001998 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001999 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00002000 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002001 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002002 Loc = UO->getOperatorLoc();
2003 R1 = UO->getSubExpr()->getSourceRange();
2004 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002005 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00002006 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002007 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00002008 switch (BO->getOpcode()) {
2009 default:
2010 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002011 // Consider the RHS of comma for side effects. LHS was checked by
2012 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00002013 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00002014 // ((foo = <blah>), 0) is an idiom for hiding the result (and
2015 // lvalue-ness) of an assignment written in a macro.
2016 if (IntegerLiteral *IE =
2017 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
2018 if (IE->getValue() == 0)
2019 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002020 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002021 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00002022 case BO_LAnd:
2023 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002024 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
2025 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00002026 return false;
2027 break;
John McCall1e3715a2010-02-16 04:10:53 +00002028 }
Chris Lattner237f2752009-02-14 07:37:35 +00002029 if (BO->isAssignmentOp())
2030 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002031 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002032 Loc = BO->getOperatorLoc();
2033 R1 = BO->getLHS()->getSourceRange();
2034 R2 = BO->getRHS()->getSourceRange();
2035 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00002036 }
Chris Lattner86928112007-08-25 02:00:02 +00002037 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00002038 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00002039 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002040 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00002041
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002042 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002043 // If only one of the LHS or RHS is a warning, the operator might
2044 // be being used for control flow. Only warn if both the LHS and
2045 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002046 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002047 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002048 return false;
2049 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002050 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002051 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002052 }
2053
Chris Lattnera44d1162007-06-27 05:58:59 +00002054 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002055 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002056 Loc = cast<MemberExpr>(this)->getMemberLoc();
2057 R1 = SourceRange(Loc, Loc);
2058 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2059 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002060
Chris Lattner1ec5f562007-06-27 05:38:08 +00002061 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002062 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002063 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2064 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2065 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2066 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002067
Chandler Carruth46339472011-08-17 09:49:44 +00002068 case CXXOperatorCallExprClass: {
Richard Trieu99e1c952014-03-11 03:11:08 +00002069 // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator
Chandler Carruth46339472011-08-17 09:49:44 +00002070 // overloads as there is no reasonable way to define these such that they
2071 // have non-trivial, desirable side-effects. See the -Wunused-comparison
Richard Trieu99e1c952014-03-11 03:11:08 +00002072 // warning: operators == and != are commonly typo'ed, and so warning on them
Chandler Carruth46339472011-08-17 09:49:44 +00002073 // provides additional value as well. If this list is updated,
2074 // DiagnoseUnusedComparison should be as well.
2075 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
Richard Trieu99e1c952014-03-11 03:11:08 +00002076 switch (Op->getOperator()) {
2077 default:
2078 break;
2079 case OO_EqualEqual:
2080 case OO_ExclaimEqual:
2081 case OO_Less:
2082 case OO_Greater:
2083 case OO_GreaterEqual:
2084 case OO_LessEqual:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002085 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002086 Loc = Op->getOperatorLoc();
2087 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002088 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002089 }
Chandler Carruth46339472011-08-17 09:49:44 +00002090
2091 // Fallthrough for generic call handling.
2092 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002093 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002094 case CXXMemberCallExprClass:
2095 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002096 // If this is a direct call, get the callee.
2097 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002098 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002099 // If the callee has attribute pure, const, or warn_unused_result, warn
2100 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002101 //
2102 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2103 // updated to match for QoI.
Aaron Ballman9ead1242013-12-19 02:39:40 +00002104 if (FD->hasAttr<WarnUnusedResultAttr>() ||
2105 FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002106 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002107 Loc = CE->getCallee()->getLocStart();
2108 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002109
Chris Lattner1a6babf2009-10-13 04:53:48 +00002110 if (unsigned NumArgs = CE->getNumArgs())
2111 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2112 CE->getArg(NumArgs-1)->getLocEnd());
2113 return true;
2114 }
Chris Lattner237f2752009-02-14 07:37:35 +00002115 }
2116 return false;
2117 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002118
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002119 // If we don't know precisely what we're looking at, let's not warn.
2120 case UnresolvedLookupExprClass:
2121 case CXXUnresolvedConstructExprClass:
2122 return false;
2123
Anders Carlsson6aa50392009-11-17 17:11:23 +00002124 case CXXTemporaryObjectExprClass:
Lubos Lunak1f490f32013-07-21 13:15:58 +00002125 case CXXConstructExprClass: {
2126 if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) {
2127 if (Type->hasAttr<WarnUnusedAttr>()) {
2128 WarnE = this;
2129 Loc = getLocStart();
2130 R1 = getSourceRange();
2131 return true;
2132 }
2133 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002134 return false;
Lubos Lunak1f490f32013-07-21 13:15:58 +00002135 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002136
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002137 case ObjCMessageExprClass: {
2138 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002139 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002140 ME->isInstanceMessage() &&
2141 !ME->getType()->isVoidType() &&
Jean-Daniel Dupas06028a52013-07-19 20:25:56 +00002142 ME->getMethodFamily() == OMF_init) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002143 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002144 Loc = getExprLoc();
2145 R1 = ME->getSourceRange();
2146 return true;
2147 }
2148
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002149 const ObjCMethodDecl *MD = ME->getMethodDecl();
Aaron Ballman9ead1242013-12-19 02:39:40 +00002150 if (MD && MD->hasAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002151 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002152 Loc = getExprLoc();
2153 return true;
2154 }
Chris Lattner237f2752009-02-14 07:37:35 +00002155 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002156 }
Mike Stump11289f42009-09-09 15:08:12 +00002157
John McCallb7bd14f2010-12-02 01:19:52 +00002158 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002159 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002160 Loc = getExprLoc();
2161 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002162 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002163
John McCallfe96e0b2011-11-06 09:01:30 +00002164 case PseudoObjectExprClass: {
2165 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2166
2167 // Only complain about things that have the form of a getter.
2168 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2169 isa<BinaryOperator>(PO->getSyntacticForm()))
2170 return false;
2171
Eli Friedmanc11535c2012-05-24 00:47:05 +00002172 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002173 Loc = getExprLoc();
2174 R1 = getSourceRange();
2175 return true;
2176 }
2177
Chris Lattner944d3062008-07-26 19:51:01 +00002178 case StmtExprClass: {
2179 // Statement exprs don't logically have side effects themselves, but are
2180 // sometimes used in macros in ways that give them a type that is unused.
2181 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2182 // however, if the result of the stmt expr is dead, we don't want to emit a
2183 // warning.
2184 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002185 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002186 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002187 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002188 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2189 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002190 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002191 }
Mike Stump11289f42009-09-09 15:08:12 +00002192
John McCallc493a732010-03-12 07:11:26 +00002193 if (getType()->isVoidType())
2194 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002195 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002196 Loc = cast<StmtExpr>(this)->getLParenLoc();
2197 R1 = getSourceRange();
2198 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002199 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002200 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002201 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002202 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002203 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002204 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002205 if (CE->getCastKind() == CK_ToVoid) {
2206 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002207 CE->getSubExpr()->getType().isVolatileQualified()) {
2208 const DeclRefExpr *DRE =
2209 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2210 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2211 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2212 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2213 R1, R2, Ctx);
2214 }
2215 }
Chris Lattner2706a552009-07-28 18:25:28 +00002216 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002217 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002218
Eli Friedmanc11535c2012-05-24 00:47:05 +00002219 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002220 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002221 if (CE->getCastKind() == CK_ConstructorConversion)
2222 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002223
Eli Friedmanc11535c2012-05-24 00:47:05 +00002224 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002225 if (const CXXFunctionalCastExpr *CXXCE =
2226 dyn_cast<CXXFunctionalCastExpr>(this)) {
Eli Friedman89fe0d52013-08-15 22:02:56 +00002227 Loc = CXXCE->getLocStart();
Eli Friedmanf92f6452012-05-24 21:05:41 +00002228 R1 = CXXCE->getSubExpr()->getSourceRange();
2229 } else {
2230 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2231 Loc = CStyleCE->getLParenLoc();
2232 R1 = CStyleCE->getSubExpr()->getSourceRange();
2233 }
Chris Lattner237f2752009-02-14 07:37:35 +00002234 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002235 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002236 case ImplicitCastExprClass: {
2237 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002238
Eli Friedmanc11535c2012-05-24 00:47:05 +00002239 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2240 if (ICE->getCastKind() == CK_LValueToRValue &&
2241 ICE->getSubExpr()->getType().isVolatileQualified())
2242 return false;
2243
2244 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2245 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002246 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002247 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002248 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002249 case CXXDefaultInitExprClass:
2250 return (cast<CXXDefaultInitExpr>(this)
2251 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002252
2253 case CXXNewExprClass:
2254 // FIXME: In theory, there might be new expressions that don't have side
2255 // effects (e.g. a placement new with an uninitialized POD).
2256 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002257 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002258 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002259 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002260 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002261 case ExprWithCleanupsClass:
2262 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002263 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002264 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002265}
2266
Fariborz Jahanian07735332009-02-22 18:40:18 +00002267/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002268/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002269bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002270 const Expr *E = IgnoreParens();
2271 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002272 default:
2273 return false;
2274 case ObjCIvarRefExprClass:
2275 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002276 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002277 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002278 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002279 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002280 case MaterializeTemporaryExprClass:
2281 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2282 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002283 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002284 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002285 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002286 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002287
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002288 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2289 if (VD->hasGlobalStorage())
2290 return true;
2291 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002292 // dereferencing to a pointer is always a gc'able candidate,
2293 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002294 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002295 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002296 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002297 return false;
2298 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002299 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002300 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002301 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002302 }
2303 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002304 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002305 }
2306}
Sebastian Redlce354af2010-09-10 20:55:33 +00002307
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002308bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2309 if (isTypeDependent())
2310 return false;
John McCall086a4642010-11-24 05:12:34 +00002311 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002312}
2313
John McCall0009fcc2011-04-26 20:42:42 +00002314QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002315 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002316
2317 // Bound member expressions are always one of these possibilities:
2318 // x->m x.m x->*y x.*y
2319 // (possibly parenthesized)
2320
2321 expr = expr->IgnoreParens();
2322 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2323 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2324 return mem->getMemberDecl()->getType();
2325 }
2326
2327 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2328 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2329 ->getPointeeType();
2330 assert(type->isFunctionType());
2331 return type;
2332 }
2333
2334 assert(isa<UnresolvedMemberExpr>(expr));
2335 return QualType();
2336}
2337
Ted Kremenekfff70962008-01-17 16:57:34 +00002338Expr* Expr::IgnoreParens() {
2339 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002340 while (true) {
2341 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2342 E = P->getSubExpr();
2343 continue;
2344 }
2345 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2346 if (P->getOpcode() == UO_Extension) {
2347 E = P->getSubExpr();
2348 continue;
2349 }
2350 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002351 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2352 if (!P->isResultDependent()) {
2353 E = P->getResultExpr();
2354 continue;
2355 }
2356 }
Eli Friedman75807f22013-07-20 00:40:58 +00002357 if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) {
2358 if (!P->isConditionDependent()) {
2359 E = P->getChosenSubExpr();
2360 continue;
2361 }
2362 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002363 return E;
2364 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002365}
2366
Chris Lattnerf2660962008-02-13 01:02:39 +00002367/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2368/// or CastExprs or ImplicitCastExprs, returning their operand.
2369Expr *Expr::IgnoreParenCasts() {
2370 Expr *E = this;
2371 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002372 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002373 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002374 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002375 continue;
2376 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002377 if (MaterializeTemporaryExpr *Materialize
2378 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2379 E = Materialize->GetTemporaryExpr();
2380 continue;
2381 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002382 if (SubstNonTypeTemplateParmExpr *NTTP
2383 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2384 E = NTTP->getReplacement();
2385 continue;
2386 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002387 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002388 }
2389}
2390
John McCall5a4ce8b2010-12-04 08:24:19 +00002391/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2392/// casts. This is intended purely as a temporary workaround for code
2393/// that hasn't yet been rewritten to do the right thing about those
2394/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002395Expr *Expr::IgnoreParenLValueCasts() {
2396 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002397 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002398 E = E->IgnoreParens();
2399 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002400 if (P->getCastKind() == CK_LValueToRValue) {
2401 E = P->getSubExpr();
2402 continue;
2403 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002404 } else if (MaterializeTemporaryExpr *Materialize
2405 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2406 E = Materialize->GetTemporaryExpr();
2407 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002408 } else if (SubstNonTypeTemplateParmExpr *NTTP
2409 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2410 E = NTTP->getReplacement();
2411 continue;
John McCall34376a62010-12-04 03:47:34 +00002412 }
2413 break;
2414 }
2415 return E;
2416}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002417
2418Expr *Expr::ignoreParenBaseCasts() {
2419 Expr *E = this;
2420 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002421 E = E->IgnoreParens();
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002422 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2423 if (CE->getCastKind() == CK_DerivedToBase ||
2424 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2425 CE->getCastKind() == CK_NoOp) {
2426 E = CE->getSubExpr();
2427 continue;
2428 }
2429 }
2430
2431 return E;
2432 }
2433}
2434
John McCalleebc8322010-05-05 22:59:52 +00002435Expr *Expr::IgnoreParenImpCasts() {
2436 Expr *E = this;
2437 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002438 E = E->IgnoreParens();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002439 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002440 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002441 continue;
2442 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002443 if (MaterializeTemporaryExpr *Materialize
2444 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2445 E = Materialize->GetTemporaryExpr();
2446 continue;
2447 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002448 if (SubstNonTypeTemplateParmExpr *NTTP
2449 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2450 E = NTTP->getReplacement();
2451 continue;
2452 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002453 return E;
John McCalleebc8322010-05-05 22:59:52 +00002454 }
2455}
2456
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002457Expr *Expr::IgnoreConversionOperator() {
2458 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002459 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002460 return MCE->getImplicitObjectArgument();
2461 }
2462 return this;
2463}
2464
Chris Lattneref26c772009-03-13 17:28:01 +00002465/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2466/// value (including ptr->int casts of the same size). Strip off any
2467/// ParenExpr or CastExprs, returning their operand.
2468Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2469 Expr *E = this;
2470 while (true) {
Eli Friedman75807f22013-07-20 00:40:58 +00002471 E = E->IgnoreParens();
Mike Stump11289f42009-09-09 15:08:12 +00002472
Chris Lattneref26c772009-03-13 17:28:01 +00002473 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2474 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002475 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002476 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002477
Chris Lattneref26c772009-03-13 17:28:01 +00002478 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2479 E = SE;
2480 continue;
2481 }
Mike Stump11289f42009-09-09 15:08:12 +00002482
Abramo Bagnara932e3932010-10-15 07:51:18 +00002483 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002484 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002485 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002486 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002487 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2488 E = SE;
2489 continue;
2490 }
2491 }
Mike Stump11289f42009-09-09 15:08:12 +00002492
Douglas Gregor6a40b082011-09-08 17:56:33 +00002493 if (SubstNonTypeTemplateParmExpr *NTTP
2494 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2495 E = NTTP->getReplacement();
2496 continue;
2497 }
2498
Chris Lattneref26c772009-03-13 17:28:01 +00002499 return E;
2500 }
2501}
2502
Douglas Gregord196a582009-12-14 19:27:10 +00002503bool Expr::isDefaultArgument() const {
2504 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002505 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2506 E = M->GetTemporaryExpr();
2507
Douglas Gregord196a582009-12-14 19:27:10 +00002508 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2509 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002510
Douglas Gregord196a582009-12-14 19:27:10 +00002511 return isa<CXXDefaultArgExpr>(E);
2512}
Chris Lattneref26c772009-03-13 17:28:01 +00002513
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002514/// \brief Skip over any no-op casts and any temporary-binding
2515/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002516static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002517 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2518 E = M->GetTemporaryExpr();
2519
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002520 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002521 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002522 E = ICE->getSubExpr();
2523 else
2524 break;
2525 }
2526
2527 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2528 E = BE->getSubExpr();
2529
2530 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002531 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002532 E = ICE->getSubExpr();
2533 else
2534 break;
2535 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002536
2537 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002538}
2539
John McCall7a626f62010-09-15 10:14:12 +00002540/// isTemporaryObject - Determines if this expression produces a
2541/// temporary of the given class type.
2542bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2543 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2544 return false;
2545
Anders Carlsson66bbf502010-11-28 16:40:49 +00002546 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002547
John McCall02dc8c72010-09-15 20:59:13 +00002548 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002549 if (!E->Classify(C).isPRValue()) {
2550 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002551 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002552 return false;
2553 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002554
John McCallf4ee1dd2010-09-16 06:57:56 +00002555 // Black-list a few cases which yield pr-values of class type that don't
2556 // refer to temporaries of that type:
2557
2558 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002559 if (isa<ImplicitCastExpr>(E)) {
2560 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2561 case CK_DerivedToBase:
2562 case CK_UncheckedDerivedToBase:
2563 return false;
2564 default:
2565 break;
2566 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002567 }
2568
John McCallf4ee1dd2010-09-16 06:57:56 +00002569 // - member expressions (all)
2570 if (isa<MemberExpr>(E))
2571 return false;
2572
Eli Friedman13ffdd82012-06-15 23:51:06 +00002573 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2574 if (BO->isPtrMemOp())
2575 return false;
2576
John McCallc07a0c72011-02-17 10:25:35 +00002577 // - opaque values (all)
2578 if (isa<OpaqueValueExpr>(E))
2579 return false;
2580
John McCall7a626f62010-09-15 10:14:12 +00002581 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002582}
2583
Douglas Gregor25b7e052011-03-02 21:06:53 +00002584bool Expr::isImplicitCXXThis() const {
2585 const Expr *E = this;
2586
2587 // Strip away parentheses and casts we don't care about.
2588 while (true) {
2589 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2590 E = Paren->getSubExpr();
2591 continue;
2592 }
2593
2594 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2595 if (ICE->getCastKind() == CK_NoOp ||
2596 ICE->getCastKind() == CK_LValueToRValue ||
2597 ICE->getCastKind() == CK_DerivedToBase ||
2598 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2599 E = ICE->getSubExpr();
2600 continue;
2601 }
2602 }
2603
2604 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2605 if (UnOp->getOpcode() == UO_Extension) {
2606 E = UnOp->getSubExpr();
2607 continue;
2608 }
2609 }
2610
Douglas Gregorfe314812011-06-21 17:03:29 +00002611 if (const MaterializeTemporaryExpr *M
2612 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2613 E = M->GetTemporaryExpr();
2614 continue;
2615 }
2616
Douglas Gregor25b7e052011-03-02 21:06:53 +00002617 break;
2618 }
2619
2620 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2621 return This->isImplicit();
2622
2623 return false;
2624}
2625
Douglas Gregor4619e432008-12-05 23:32:09 +00002626/// hasAnyTypeDependentArguments - Determines if any of the expressions
2627/// in Exprs is type-dependent.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002628bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002629 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002630 if (Exprs[I]->isTypeDependent())
2631 return true;
2632
2633 return false;
2634}
2635
John McCall8b0f4ff2010-08-02 21:13:48 +00002636bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002637 // This function is attempting whether an expression is an initializer
Eli Friedman4c27ac22013-07-16 22:40:53 +00002638 // which can be evaluated at compile-time. It very closely parallels
2639 // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it
2640 // will lead to unexpected results. Like ConstExprEmitter, it falls back
2641 // to isEvaluatable most of the time.
2642 //
John McCall8b0f4ff2010-08-02 21:13:48 +00002643 // If we ever capture reference-binding directly in the AST, we can
2644 // kill the second parameter.
2645
2646 if (IsForRef) {
2647 EvalResult Result;
2648 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2649 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002650
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002651 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002652 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002653 case StringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002654 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002655 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002656 case CXXTemporaryObjectExprClass:
2657 case CXXConstructExprClass: {
2658 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002659
Eli Friedman4c27ac22013-07-16 22:40:53 +00002660 if (CE->getConstructor()->isTrivial() &&
2661 CE->getConstructor()->getParent()->hasTrivialDestructor()) {
2662 // Trivial default constructor
Richard Smithd62306a2011-11-10 06:34:14 +00002663 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002664
Eli Friedman4c27ac22013-07-16 22:40:53 +00002665 // Trivial copy constructor
2666 assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument");
2667 return CE->getArg(0)->isConstantInitializer(Ctx, false);
Richard Smithd62306a2011-11-10 06:34:14 +00002668 }
2669
Richard Smithd62306a2011-11-10 06:34:14 +00002670 break;
John McCall81c9cea2010-08-01 21:51:45 +00002671 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002672 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002673 // This handles gcc's extension that allows global initializers like
2674 // "struct x {int x;} x = (struct x) {};".
2675 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002676 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002677 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002678 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002679 case InitListExprClass: {
Eli Friedman4c27ac22013-07-16 22:40:53 +00002680 const InitListExpr *ILE = cast<InitListExpr>(this);
2681 if (ILE->getType()->isArrayType()) {
2682 unsigned numInits = ILE->getNumInits();
2683 for (unsigned i = 0; i < numInits; i++) {
2684 if (!ILE->getInit(i)->isConstantInitializer(Ctx, false))
2685 return false;
2686 }
2687 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002688 }
Eli Friedman4c27ac22013-07-16 22:40:53 +00002689
2690 if (ILE->getType()->isRecordType()) {
2691 unsigned ElementNo = 0;
2692 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
2693 for (RecordDecl::field_iterator Field = RD->field_begin(),
2694 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
2695 // If this is a union, skip all the fields that aren't being initialized.
2696 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
2697 continue;
2698
2699 // Don't emit anonymous bitfields, they just affect layout.
2700 if (Field->isUnnamedBitfield())
2701 continue;
2702
2703 if (ElementNo < ILE->getNumInits()) {
2704 const Expr *Elt = ILE->getInit(ElementNo++);
2705 if (Field->isBitField()) {
2706 // Bitfields have to evaluate to an integer.
2707 llvm::APSInt ResultTmp;
2708 if (!Elt->EvaluateAsInt(ResultTmp, Ctx))
2709 return false;
2710 } else {
2711 bool RefType = Field->getType()->isReferenceType();
2712 if (!Elt->isConstantInitializer(Ctx, RefType))
2713 return false;
2714 }
2715 }
2716 }
2717 return true;
2718 }
2719
2720 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002721 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002722 case ImplicitValueInitExprClass:
2723 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002724 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002725 return cast<ParenExpr>(this)->getSubExpr()
2726 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002727 case GenericSelectionExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002728 return cast<GenericSelectionExpr>(this)->getResultExpr()
2729 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002730 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002731 if (cast<ChooseExpr>(this)->isConditionDependent())
2732 return false;
2733 return cast<ChooseExpr>(this)->getChosenSubExpr()
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002734 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002735 case UnaryOperatorClass: {
2736 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002737 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002738 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002739 break;
2740 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002741 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002742 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002743 case ImplicitCastExprClass:
Eli Friedman4c27ac22013-07-16 22:40:53 +00002744 case CStyleCastExprClass:
2745 case ObjCBridgedCastExprClass:
2746 case CXXDynamicCastExprClass:
2747 case CXXReinterpretCastExprClass:
2748 case CXXConstCastExprClass: {
Richard Smith161f09a2011-12-06 22:44:34 +00002749 const CastExpr *CE = cast<CastExpr>(this);
2750
Eli Friedman13ec75b2011-12-21 00:43:02 +00002751 // Handle misc casts we want to ignore.
Eli Friedman13ec75b2011-12-21 00:43:02 +00002752 if (CE->getCastKind() == CK_NoOp ||
2753 CE->getCastKind() == CK_LValueToRValue ||
2754 CE->getCastKind() == CK_ToUnion ||
Eli Friedman4c27ac22013-07-16 22:40:53 +00002755 CE->getCastKind() == CK_ConstructorConversion ||
2756 CE->getCastKind() == CK_NonAtomicToAtomic ||
2757 CE->getCastKind() == CK_AtomicToNonAtomic)
Richard Smith161f09a2011-12-06 22:44:34 +00002758 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2759
Eli Friedman384da272009-01-25 03:12:18 +00002760 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002761 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002762 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002764 ->isConstantInitializer(Ctx, false);
Eli Friedman4c27ac22013-07-16 22:40:53 +00002765
2766 case SubstNonTypeTemplateParmExprClass:
2767 return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement()
2768 ->isConstantInitializer(Ctx, false);
2769 case CXXDefaultArgExprClass:
2770 return cast<CXXDefaultArgExpr>(this)->getExpr()
2771 ->isConstantInitializer(Ctx, false);
2772 case CXXDefaultInitExprClass:
2773 return cast<CXXDefaultInitExpr>(this)->getExpr()
2774 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002775 }
Eli Friedman384da272009-01-25 03:12:18 +00002776 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002777}
2778
Richard Smith0421ce72012-08-07 04:16:51 +00002779bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2780 if (isInstantiationDependent())
2781 return true;
2782
2783 switch (getStmtClass()) {
2784 case NoStmtClass:
2785 #define ABSTRACT_STMT(Type)
2786 #define STMT(Type, Base) case Type##Class:
2787 #define EXPR(Type, Base)
2788 #include "clang/AST/StmtNodes.inc"
2789 llvm_unreachable("unexpected Expr kind");
2790
2791 case DependentScopeDeclRefExprClass:
2792 case CXXUnresolvedConstructExprClass:
2793 case CXXDependentScopeMemberExprClass:
2794 case UnresolvedLookupExprClass:
2795 case UnresolvedMemberExprClass:
2796 case PackExpansionExprClass:
2797 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002798 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002799 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2800
Richard Smitha33e4fe2012-08-07 05:18:29 +00002801 case DeclRefExprClass:
2802 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002803 case PredefinedExprClass:
2804 case IntegerLiteralClass:
2805 case FloatingLiteralClass:
2806 case ImaginaryLiteralClass:
2807 case StringLiteralClass:
2808 case CharacterLiteralClass:
2809 case OffsetOfExprClass:
2810 case ImplicitValueInitExprClass:
2811 case UnaryExprOrTypeTraitExprClass:
2812 case AddrLabelExprClass:
2813 case GNUNullExprClass:
2814 case CXXBoolLiteralExprClass:
2815 case CXXNullPtrLiteralExprClass:
2816 case CXXThisExprClass:
2817 case CXXScalarValueInitExprClass:
2818 case TypeTraitExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002819 case ArrayTypeTraitExprClass:
2820 case ExpressionTraitExprClass:
2821 case CXXNoexceptExprClass:
2822 case SizeOfPackExprClass:
2823 case ObjCStringLiteralClass:
2824 case ObjCEncodeExprClass:
2825 case ObjCBoolLiteralExprClass:
2826 case CXXUuidofExprClass:
2827 case OpaqueValueExprClass:
2828 // These never have a side-effect.
2829 return false;
2830
2831 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002832 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002833 case CompoundAssignOperatorClass:
2834 case VAArgExprClass:
2835 case AtomicExprClass:
2836 case StmtExprClass:
2837 case CXXOperatorCallExprClass:
2838 case CXXMemberCallExprClass:
2839 case UserDefinedLiteralClass:
2840 case CXXThrowExprClass:
2841 case CXXNewExprClass:
2842 case CXXDeleteExprClass:
2843 case ExprWithCleanupsClass:
2844 case CXXBindTemporaryExprClass:
2845 case BlockExprClass:
2846 case CUDAKernelCallExprClass:
2847 // These always have a side-effect.
2848 return true;
2849
2850 case ParenExprClass:
2851 case ArraySubscriptExprClass:
2852 case MemberExprClass:
2853 case ConditionalOperatorClass:
2854 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002855 case CompoundLiteralExprClass:
2856 case ExtVectorElementExprClass:
2857 case DesignatedInitExprClass:
2858 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002859 case CXXPseudoDestructorExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00002860 case CXXStdInitializerListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002861 case SubstNonTypeTemplateParmExprClass:
2862 case MaterializeTemporaryExprClass:
2863 case ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00002864 case ConvertVectorExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002865 case AsTypeExprClass:
2866 // These have a side-effect if any subexpression does.
2867 break;
2868
Richard Smitha33e4fe2012-08-07 05:18:29 +00002869 case UnaryOperatorClass:
2870 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002871 return true;
2872 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002873
2874 case BinaryOperatorClass:
2875 if (cast<BinaryOperator>(this)->isAssignmentOp())
2876 return true;
2877 break;
2878
Richard Smith0421ce72012-08-07 04:16:51 +00002879 case InitListExprClass:
2880 // FIXME: The children for an InitListExpr doesn't include the array filler.
2881 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2882 if (E->HasSideEffects(Ctx))
2883 return true;
2884 break;
2885
2886 case GenericSelectionExprClass:
2887 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2888 HasSideEffects(Ctx);
2889
2890 case ChooseExprClass:
Eli Friedman75807f22013-07-20 00:40:58 +00002891 return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects(Ctx);
Richard Smith0421ce72012-08-07 04:16:51 +00002892
2893 case CXXDefaultArgExprClass:
2894 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2895
Richard Smith852c9db2013-04-20 22:23:05 +00002896 case CXXDefaultInitExprClass:
2897 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2898 return E->HasSideEffects(Ctx);
2899 // If we've not yet parsed the initializer, assume it has side-effects.
2900 return true;
2901
Richard Smith0421ce72012-08-07 04:16:51 +00002902 case CXXDynamicCastExprClass: {
2903 // A dynamic_cast expression has side-effects if it can throw.
2904 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2905 if (DCE->getTypeAsWritten()->isReferenceType() &&
2906 DCE->getCastKind() == CK_Dynamic)
2907 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002908 } // Fall through.
2909 case ImplicitCastExprClass:
2910 case CStyleCastExprClass:
2911 case CXXStaticCastExprClass:
2912 case CXXReinterpretCastExprClass:
2913 case CXXConstCastExprClass:
2914 case CXXFunctionalCastExprClass: {
2915 const CastExpr *CE = cast<CastExpr>(this);
2916 if (CE->getCastKind() == CK_LValueToRValue &&
2917 CE->getSubExpr()->getType().isVolatileQualified())
2918 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002919 break;
2920 }
2921
Richard Smithef8bf432012-08-13 20:08:14 +00002922 case CXXTypeidExprClass:
2923 // typeid might throw if its subexpression is potentially-evaluated, so has
2924 // side-effects in that case whether or not its subexpression does.
2925 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002926
2927 case CXXConstructExprClass:
2928 case CXXTemporaryObjectExprClass: {
2929 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002930 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002931 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002932 // A trivial constructor does not add any side-effects of its own. Just look
2933 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002934 break;
2935 }
2936
2937 case LambdaExprClass: {
2938 const LambdaExpr *LE = cast<LambdaExpr>(this);
2939 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2940 E = LE->capture_end(); I != E; ++I)
2941 if (I->getCaptureKind() == LCK_ByCopy)
2942 // FIXME: Only has a side-effect if the variable is volatile or if
2943 // the copy would invoke a non-trivial copy constructor.
2944 return true;
2945 return false;
2946 }
2947
2948 case PseudoObjectExprClass: {
2949 // Only look for side-effects in the semantic form, and look past
2950 // OpaqueValueExpr bindings in that form.
2951 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2952 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2953 E = PO->semantics_end();
2954 I != E; ++I) {
2955 const Expr *Subexpr = *I;
2956 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2957 Subexpr = OVE->getSourceExpr();
2958 if (Subexpr->HasSideEffects(Ctx))
2959 return true;
2960 }
2961 return false;
2962 }
2963
2964 case ObjCBoxedExprClass:
2965 case ObjCArrayLiteralClass:
2966 case ObjCDictionaryLiteralClass:
2967 case ObjCMessageExprClass:
2968 case ObjCSelectorExprClass:
2969 case ObjCProtocolExprClass:
2970 case ObjCPropertyRefExprClass:
2971 case ObjCIsaExprClass:
2972 case ObjCIndirectCopyRestoreExprClass:
2973 case ObjCSubscriptRefExprClass:
2974 case ObjCBridgedCastExprClass:
2975 // FIXME: Classify these cases better.
2976 return true;
2977 }
2978
2979 // Recurse to children.
2980 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2981 if (const Stmt *S = *SubStmts)
2982 if (cast<Expr>(S)->HasSideEffects(Ctx))
2983 return true;
2984
2985 return false;
2986}
2987
Douglas Gregor1be329d2012-02-23 07:33:15 +00002988namespace {
2989 /// \brief Look for a call to a non-trivial function within an expression.
2990 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2991 {
2992 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2993
2994 bool NonTrivial;
2995
2996 public:
2997 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002998 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002999
3000 bool hasNonTrivialCall() const { return NonTrivial; }
3001
3002 void VisitCallExpr(CallExpr *E) {
3003 if (CXXMethodDecl *Method
3004 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
3005 if (Method->isTrivial()) {
3006 // Recurse to children of the call.
3007 Inherited::VisitStmt(E);
3008 return;
3009 }
3010 }
3011
3012 NonTrivial = true;
3013 }
3014
3015 void VisitCXXConstructExpr(CXXConstructExpr *E) {
3016 if (E->getConstructor()->isTrivial()) {
3017 // Recurse to children of the call.
3018 Inherited::VisitStmt(E);
3019 return;
3020 }
3021
3022 NonTrivial = true;
3023 }
3024
3025 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3026 if (E->getTemporary()->getDestructor()->isTrivial()) {
3027 Inherited::VisitStmt(E);
3028 return;
3029 }
3030
3031 NonTrivial = true;
3032 }
3033 };
3034}
3035
3036bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3037 NonTrivialCallFinder Finder(Ctx);
3038 Finder.Visit(this);
3039 return Finder.hasNonTrivialCall();
3040}
3041
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003042/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3043/// pointer constant or not, as well as the specific kind of constant detected.
3044/// Null pointer constants can be integer constant expressions with the
3045/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3046/// (a GNU extension).
3047Expr::NullPointerConstantKind
3048Expr::isNullPointerConstant(ASTContext &Ctx,
3049 NullPointerConstantValueDependence NPC) const {
Reid Klecknera5eef142013-11-12 02:22:34 +00003050 if (isValueDependent() &&
Alp Tokerbfa39342014-01-14 12:51:41 +00003051 (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) {
Douglas Gregor56751b52009-09-25 04:25:58 +00003052 switch (NPC) {
3053 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003054 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003055 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003056 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003057 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003058 else
3059 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003060
Douglas Gregor56751b52009-09-25 04:25:58 +00003061 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003062 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003063 }
3064 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003065
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003066 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003067 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003068 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003069 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003070 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003071 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003072 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003073 Pointee->isVoidType() && // to void*
3074 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003075 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003076 }
Steve Naroffada7d422007-05-20 17:54:12 +00003077 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003078 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3079 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003080 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003081 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3082 // Accept ((void*)0) as a null pointer constant, as many other
3083 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003084 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003085 } else if (const GenericSelectionExpr *GE =
3086 dyn_cast<GenericSelectionExpr>(this)) {
Eli Friedman75807f22013-07-20 00:40:58 +00003087 if (GE->isResultDependent())
3088 return NPCK_NotNull;
Peter Collingbourne91147592011-04-15 00:35:48 +00003089 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Eli Friedman75807f22013-07-20 00:40:58 +00003090 } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) {
3091 if (CE->isConditionDependent())
3092 return NPCK_NotNull;
3093 return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003094 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003095 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003096 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003097 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003098 } else if (const CXXDefaultInitExpr *DefaultInit
3099 = dyn_cast<CXXDefaultInitExpr>(this)) {
3100 // See through default initializer expressions.
3101 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003102 } else if (isa<GNUNullExpr>(this)) {
3103 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003104 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003105 } else if (const MaterializeTemporaryExpr *M
3106 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3107 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003108 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3109 if (const Expr *Source = OVE->getSourceExpr())
3110 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003111 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003112
Richard Smith89645bc2013-01-02 12:01:23 +00003113 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003114 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003115 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003116
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003117 if (const RecordType *UT = getType()->getAsUnionType())
Richard Smith4055de42013-06-13 02:46:14 +00003118 if (!Ctx.getLangOpts().CPlusPlus11 &&
3119 UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003120 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3121 const Expr *InitExpr = CLE->getInitializer();
3122 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3123 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3124 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003125 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003126 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003127 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003128 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003129
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003130 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith4055de42013-06-13 02:46:14 +00003131 // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with
3132 // value zero or a prvalue of type std::nullptr_t.
Reid Klecknera5eef142013-11-12 02:22:34 +00003133 // Microsoft mode permits C++98 rules reflecting MSVC behavior.
Richard Smith4055de42013-06-13 02:46:14 +00003134 const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this);
Reid Klecknera5eef142013-11-12 02:22:34 +00003135 if (Lit && !Lit->getValue())
3136 return NPCK_ZeroLiteral;
Alp Tokerbfa39342014-01-14 12:51:41 +00003137 else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx))
Reid Klecknera5eef142013-11-12 02:22:34 +00003138 return NPCK_NotNull;
Richard Smith98a0a492012-02-14 21:38:30 +00003139 } else {
Richard Smith4055de42013-06-13 02:46:14 +00003140 // If we have an integer constant expression, we need to *evaluate* it and
3141 // test for the value 0.
Richard Smith98a0a492012-02-14 21:38:30 +00003142 if (!isIntegerConstantExpr(Ctx))
3143 return NPCK_NotNull;
3144 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003145
David Blaikie1c7c8f72012-08-08 17:33:31 +00003146 if (EvaluateKnownConstInt(Ctx) != 0)
3147 return NPCK_NotNull;
3148
3149 if (isa<IntegerLiteral>(this))
3150 return NPCK_ZeroLiteral;
3151 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003152}
Steve Narofff7a5da12007-07-28 23:10:27 +00003153
John McCall34376a62010-12-04 03:47:34 +00003154/// \brief If this expression is an l-value for an Objective C
3155/// property, find the underlying property reference expression.
3156const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3157 const Expr *E = this;
3158 while (true) {
3159 assert((E->getValueKind() == VK_LValue &&
3160 E->getObjectKind() == OK_ObjCProperty) &&
3161 "expression is not a property reference");
3162 E = E->IgnoreParenCasts();
3163 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3164 if (BO->getOpcode() == BO_Comma) {
3165 E = BO->getRHS();
3166 continue;
3167 }
3168 }
3169
3170 break;
3171 }
3172
3173 return cast<ObjCPropertyRefExpr>(E);
3174}
3175
Anna Zaks97c7ce32012-10-01 20:34:04 +00003176bool Expr::isObjCSelfExpr() const {
3177 const Expr *E = IgnoreParenImpCasts();
3178
3179 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3180 if (!DRE)
3181 return false;
3182
3183 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3184 if (!Param)
3185 return false;
3186
3187 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3188 if (!M)
3189 return false;
3190
3191 return M->getSelfDecl() == Param;
3192}
3193
John McCalld25db7e2013-05-06 21:39:12 +00003194FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003195 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003196
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003197 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003198 if (ICE->getCastKind() == CK_LValueToRValue ||
3199 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003200 E = ICE->getSubExpr()->IgnoreParens();
3201 else
3202 break;
3203 }
3204
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003205 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003206 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003207 if (Field->isBitField())
3208 return Field;
3209
John McCalld25db7e2013-05-06 21:39:12 +00003210 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3211 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3212 if (Ivar->isBitField())
3213 return Ivar;
3214
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003215 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3216 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3217 if (Field->isBitField())
3218 return Field;
3219
Eli Friedman609ada22011-07-13 02:05:57 +00003220 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003221 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003222 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003223
Eli Friedman609ada22011-07-13 02:05:57 +00003224 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003225 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003226 }
3227
Douglas Gregor71235ec2009-05-02 02:18:30 +00003228 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003229}
3230
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003231bool Expr::refersToVectorElement() const {
3232 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003233
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003234 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003235 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003236 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003237 E = ICE->getSubExpr()->IgnoreParens();
3238 else
3239 break;
3240 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003241
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003242 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3243 return ASE->getBase()->getType()->isVectorType();
3244
3245 if (isa<ExtVectorElementExpr>(E))
3246 return true;
3247
3248 return false;
3249}
3250
Chris Lattnerb8211f62009-02-16 22:14:05 +00003251/// isArrow - Return true if the base expression is a pointer to vector,
3252/// return false if the base expression is a vector.
3253bool ExtVectorElementExpr::isArrow() const {
3254 return getBase()->getType()->isPointerType();
3255}
3256
Nate Begemance4d7fc2008-04-18 23:10:10 +00003257unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003258 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003259 return VT->getNumElements();
3260 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003261}
3262
Nate Begemanf322eab2008-05-09 06:41:27 +00003263/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003264bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003265 // FIXME: Refactor this code to an accessor on the AST node which returns the
3266 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003267 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003268
3269 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003270 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003271 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003272
Nate Begeman7e5185b2009-01-18 02:01:21 +00003273 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003274 if (Comp[0] == 's' || Comp[0] == 'S')
3275 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003276
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003277 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003278 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003279 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003280
Steve Naroff0d595ca2007-07-30 03:29:09 +00003281 return false;
3282}
Chris Lattner885b4952007-08-02 23:36:59 +00003283
Nate Begemanf322eab2008-05-09 06:41:27 +00003284/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003285void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003286 SmallVectorImpl<unsigned> &Elts) const {
3287 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003288 if (Comp[0] == 's' || Comp[0] == 'S')
3289 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003290
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003291 bool isHi = Comp == "hi";
3292 bool isLo = Comp == "lo";
3293 bool isEven = Comp == "even";
3294 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003295
Nate Begemanf322eab2008-05-09 06:41:27 +00003296 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3297 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003298
Nate Begemanf322eab2008-05-09 06:41:27 +00003299 if (isHi)
3300 Index = e + i;
3301 else if (isLo)
3302 Index = i;
3303 else if (isEven)
3304 Index = 2 * i;
3305 else if (isOdd)
3306 Index = 2 * i + 1;
3307 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003308 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003309
Nate Begemand3862152008-05-13 21:03:02 +00003310 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003311 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003312}
3313
Douglas Gregor9a129192010-04-21 00:45:42 +00003314ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003315 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003316 SourceLocation LBracLoc,
3317 SourceLocation SuperLoc,
3318 bool IsInstanceSuper,
3319 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003320 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003321 ArrayRef<SourceLocation> SelLocs,
3322 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003323 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003324 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003325 SourceLocation RBracLoc,
3326 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003327 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003328 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003329 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003330 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003331 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3332 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003333 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003334 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3335 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003336{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003337 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003338 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003339}
3340
Douglas Gregor9a129192010-04-21 00:45:42 +00003341ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003342 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003343 SourceLocation LBracLoc,
3344 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003345 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003346 ArrayRef<SourceLocation> SelLocs,
3347 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003348 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003349 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003350 SourceLocation RBracLoc,
3351 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003352 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003353 T->isDependentType(), T->isInstantiationDependentType(),
3354 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003355 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3356 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003357 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003358 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003359 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003360{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003361 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003362 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003363}
3364
Douglas Gregor9a129192010-04-21 00:45:42 +00003365ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003366 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003367 SourceLocation LBracLoc,
3368 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003369 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003370 ArrayRef<SourceLocation> SelLocs,
3371 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003372 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003373 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003374 SourceLocation RBracLoc,
3375 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003376 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003377 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003378 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003379 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003380 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3381 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003382 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003383 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003384 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003385{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003386 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003387 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003388}
3389
3390void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3391 ArrayRef<SourceLocation> SelLocs,
3392 SelectorLocationsKind SelLocsK) {
3393 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003394 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003395 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003396 if (Args[I]->isTypeDependent())
3397 ExprBits.TypeDependent = true;
3398 if (Args[I]->isValueDependent())
3399 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003400 if (Args[I]->isInstantiationDependent())
3401 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003402 if (Args[I]->containsUnexpandedParameterPack())
3403 ExprBits.ContainsUnexpandedParameterPack = true;
3404
3405 MyArgs[I] = Args[I];
3406 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003407
Benjamin Kramer2325b242012-02-20 00:20:48 +00003408 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003409 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003410 if (SelLocsK == SelLoc_NonStandard)
3411 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3412 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003413}
3414
Craig Topperce7167c2013-08-22 04:58:56 +00003415ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003416 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003417 SourceLocation LBracLoc,
3418 SourceLocation SuperLoc,
3419 bool IsInstanceSuper,
3420 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003421 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003422 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003423 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003424 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003425 SourceLocation RBracLoc,
3426 bool isImplicit) {
3427 assert((!SelLocs.empty() || isImplicit) &&
3428 "No selector locs for non-implicit message");
3429 ObjCMessageExpr *Mem;
3430 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3431 if (isImplicit)
3432 Mem = alloc(Context, Args.size(), 0);
3433 else
3434 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003435 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003436 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003437 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003438}
3439
Craig Topperce7167c2013-08-22 04:58:56 +00003440ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003441 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003442 SourceLocation LBracLoc,
3443 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003444 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003445 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003446 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003447 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003448 SourceLocation RBracLoc,
3449 bool isImplicit) {
3450 assert((!SelLocs.empty() || isImplicit) &&
3451 "No selector locs for non-implicit message");
3452 ObjCMessageExpr *Mem;
3453 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3454 if (isImplicit)
3455 Mem = alloc(Context, Args.size(), 0);
3456 else
3457 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003458 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003459 SelLocs, SelLocsK, Method, Args, RBracLoc,
3460 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003461}
3462
Craig Topperce7167c2013-08-22 04:58:56 +00003463ObjCMessageExpr *ObjCMessageExpr::Create(const ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003464 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003465 SourceLocation LBracLoc,
3466 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003467 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003468 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003469 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003470 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003471 SourceLocation RBracLoc,
3472 bool isImplicit) {
3473 assert((!SelLocs.empty() || isImplicit) &&
3474 "No selector locs for non-implicit message");
3475 ObjCMessageExpr *Mem;
3476 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3477 if (isImplicit)
3478 Mem = alloc(Context, Args.size(), 0);
3479 else
3480 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003481 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003482 SelLocs, SelLocsK, Method, Args, RBracLoc,
3483 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003484}
3485
Craig Topperce7167c2013-08-22 04:58:56 +00003486ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003487 unsigned NumArgs,
3488 unsigned NumStoredSelLocs) {
3489 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003490 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3491}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003492
Craig Topperce7167c2013-08-22 04:58:56 +00003493ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003494 ArrayRef<Expr *> Args,
3495 SourceLocation RBraceLoc,
3496 ArrayRef<SourceLocation> SelLocs,
3497 Selector Sel,
3498 SelectorLocationsKind &SelLocsK) {
3499 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3500 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3501 : 0;
3502 return alloc(C, Args.size(), NumStoredSelLocs);
3503}
3504
Craig Topperce7167c2013-08-22 04:58:56 +00003505ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003506 unsigned NumArgs,
3507 unsigned NumStoredSelLocs) {
3508 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3509 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3510 return (ObjCMessageExpr *)C.Allocate(Size,
3511 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3512}
3513
3514void ObjCMessageExpr::getSelectorLocs(
3515 SmallVectorImpl<SourceLocation> &SelLocs) const {
3516 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3517 SelLocs.push_back(getSelectorLoc(i));
3518}
3519
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003520SourceRange ObjCMessageExpr::getReceiverRange() const {
3521 switch (getReceiverKind()) {
3522 case Instance:
3523 return getInstanceReceiver()->getSourceRange();
3524
3525 case Class:
3526 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3527
3528 case SuperInstance:
3529 case SuperClass:
3530 return getSuperLoc();
3531 }
3532
David Blaikiee4d798f2012-01-20 21:50:17 +00003533 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003534}
3535
Douglas Gregor9a129192010-04-21 00:45:42 +00003536Selector ObjCMessageExpr::getSelector() const {
3537 if (HasMethod)
3538 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3539 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003540 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003541}
3542
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003543QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003544 switch (getReceiverKind()) {
3545 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003546 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003547 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003548 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003549 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003550 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003551 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003552 }
3553
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003554 llvm_unreachable("unexpected receiver kind");
3555}
3556
3557ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3558 QualType T = getReceiverType();
3559
3560 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3561 return Ptr->getInterfaceDecl();
3562
3563 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3564 return Ty->getInterface();
3565
Douglas Gregor9a129192010-04-21 00:45:42 +00003566 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003567}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003568
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003569StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003570 switch (getBridgeKind()) {
3571 case OBC_Bridge:
3572 return "__bridge";
3573 case OBC_BridgeTransfer:
3574 return "__bridge_transfer";
3575 case OBC_BridgeRetained:
3576 return "__bridge_retained";
3577 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003578
3579 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003580}
3581
Craig Topper37932912013-08-18 10:09:15 +00003582ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003583 QualType Type, SourceLocation BLoc,
3584 SourceLocation RP)
3585 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3586 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003587 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003588 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003589 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003590{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003591 SubExprs = new (C) Stmt*[args.size()];
3592 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003593 if (args[i]->isTypeDependent())
3594 ExprBits.TypeDependent = true;
3595 if (args[i]->isValueDependent())
3596 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003597 if (args[i]->isInstantiationDependent())
3598 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003599 if (args[i]->containsUnexpandedParameterPack())
3600 ExprBits.ContainsUnexpandedParameterPack = true;
3601
3602 SubExprs[i] = args[i];
3603 }
3604}
3605
Craig Topper37932912013-08-18 10:09:15 +00003606void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003607 if (SubExprs) C.Deallocate(SubExprs);
3608
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003609 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003610 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003611 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003612}
Nate Begeman48745922009-08-12 02:28:50 +00003613
Craig Topper37932912013-08-18 10:09:15 +00003614GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003615 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003616 ArrayRef<TypeSourceInfo*> AssocTypes,
3617 ArrayRef<Expr*> AssocExprs,
3618 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003619 SourceLocation RParenLoc,
3620 bool ContainsUnexpandedParameterPack,
3621 unsigned ResultIndex)
3622 : Expr(GenericSelectionExprClass,
3623 AssocExprs[ResultIndex]->getType(),
3624 AssocExprs[ResultIndex]->getValueKind(),
3625 AssocExprs[ResultIndex]->getObjectKind(),
3626 AssocExprs[ResultIndex]->isTypeDependent(),
3627 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003628 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003629 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003630 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3631 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3632 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3633 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003634 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003635 assert(AssocTypes.size() == AssocExprs.size());
3636 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3637 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003638}
3639
Craig Topper37932912013-08-18 10:09:15 +00003640GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context,
Peter Collingbourne91147592011-04-15 00:35:48 +00003641 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003642 ArrayRef<TypeSourceInfo*> AssocTypes,
3643 ArrayRef<Expr*> AssocExprs,
3644 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003645 SourceLocation RParenLoc,
3646 bool ContainsUnexpandedParameterPack)
3647 : Expr(GenericSelectionExprClass,
3648 Context.DependentTy,
3649 VK_RValue,
3650 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003651 /*isTypeDependent=*/true,
3652 /*isValueDependent=*/true,
3653 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003654 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003655 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3656 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3657 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3658 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003659 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003660 assert(AssocTypes.size() == AssocExprs.size());
3661 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3662 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003663}
3664
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003665//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003666// DesignatedInitExpr
3667//===----------------------------------------------------------------------===//
3668
Chandler Carruth631abd92011-06-16 06:47:06 +00003669IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003670 assert(Kind == FieldDesignator && "Only valid on a field designator");
3671 if (Field.NameOrField & 0x01)
3672 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3673 else
3674 return getField()->getIdentifier();
3675}
3676
Craig Topper37932912013-08-18 10:09:15 +00003677DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003678 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003679 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003680 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003681 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003682 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003683 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003684 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003685 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003686 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003687 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003688 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003689 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003690 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003691 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003692
3693 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003694 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003695 *Child++ = Init;
3696
3697 // Copy the designators and their subexpressions, computing
3698 // value-dependence along the way.
3699 unsigned IndexIdx = 0;
3700 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003701 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003702
3703 if (this->Designators[I].isArrayDesignator()) {
3704 // Compute type- and value-dependence.
3705 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003706 if (Index->isTypeDependent() || Index->isValueDependent())
3707 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003708 if (Index->isInstantiationDependent())
3709 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003710 // Propagate unexpanded parameter packs.
3711 if (Index->containsUnexpandedParameterPack())
3712 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003713
3714 // Copy the index expressions into permanent storage.
3715 *Child++ = IndexExprs[IndexIdx++];
3716 } else if (this->Designators[I].isArrayRangeDesignator()) {
3717 // Compute type- and value-dependence.
3718 Expr *Start = IndexExprs[IndexIdx];
3719 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003720 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003721 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003722 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003723 ExprBits.InstantiationDependent = true;
3724 } else if (Start->isInstantiationDependent() ||
3725 End->isInstantiationDependent()) {
3726 ExprBits.InstantiationDependent = true;
3727 }
3728
Douglas Gregora6e053e2010-12-15 01:34:56 +00003729 // Propagate unexpanded parameter packs.
3730 if (Start->containsUnexpandedParameterPack() ||
3731 End->containsUnexpandedParameterPack())
3732 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003733
3734 // Copy the start/end expressions into permanent storage.
3735 *Child++ = IndexExprs[IndexIdx++];
3736 *Child++ = IndexExprs[IndexIdx++];
3737 }
3738 }
3739
Benjamin Kramerc215e762012-08-24 11:54:20 +00003740 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003741}
3742
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003743DesignatedInitExpr *
Craig Topper37932912013-08-18 10:09:15 +00003744DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003745 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003746 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003747 SourceLocation ColonOrEqualLoc,
3748 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003749 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003750 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003751 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003752 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003753 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003754}
3755
Craig Topper37932912013-08-18 10:09:15 +00003756DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003757 unsigned NumIndexExprs) {
3758 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3759 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3760 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3761}
3762
Craig Topper37932912013-08-18 10:09:15 +00003763void DesignatedInitExpr::setDesignators(const ASTContext &C,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003764 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003765 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003766 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003767 NumDesignators = NumDesigs;
3768 for (unsigned I = 0; I != NumDesigs; ++I)
3769 Designators[I] = Desigs[I];
3770}
3771
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003772SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3773 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3774 if (size() == 1)
3775 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003776 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3777 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003778}
3779
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003780SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003781 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003782 Designator &First =
3783 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003784 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003785 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003786 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3787 else
3788 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3789 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003790 StartLoc =
3791 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003792 return StartLoc;
3793}
3794
3795SourceLocation DesignatedInitExpr::getLocEnd() const {
3796 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003797}
3798
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003799Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003800 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003801 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003802 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3803}
3804
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003805Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003806 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003807 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003808 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003809 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3810}
3811
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003812Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003813 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003814 "Requires array range designator");
Benjamin Kramerc24767b2014-02-05 21:29:05 +00003815 Stmt *const *SubExprs = reinterpret_cast<Stmt *const *>(this + 1);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003816 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3817}
3818
Douglas Gregord5846a12009-04-15 06:41:24 +00003819/// \brief Replaces the designator at index @p Idx with the series
3820/// of designators in [First, Last).
Craig Topper37932912013-08-18 10:09:15 +00003821void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003822 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003823 const Designator *Last) {
3824 unsigned NumNewDesignators = Last - First;
3825 if (NumNewDesignators == 0) {
3826 std::copy_backward(Designators + Idx + 1,
3827 Designators + NumDesignators,
3828 Designators + Idx);
3829 --NumNewDesignators;
3830 return;
3831 } else if (NumNewDesignators == 1) {
3832 Designators[Idx] = *First;
3833 return;
3834 }
3835
Mike Stump11289f42009-09-09 15:08:12 +00003836 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003837 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003838 std::copy(Designators, Designators + Idx, NewDesignators);
3839 std::copy(First, Last, NewDesignators + Idx);
3840 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3841 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003842 Designators = NewDesignators;
3843 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3844}
3845
Craig Topper37932912013-08-18 10:09:15 +00003846ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003847 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003848 SourceLocation rparenloc)
3849 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003850 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003851 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3852 Exprs = new (C) Stmt*[exprs.size()];
3853 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003854 if (exprs[i]->isTypeDependent())
3855 ExprBits.TypeDependent = true;
3856 if (exprs[i]->isValueDependent())
3857 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003858 if (exprs[i]->isInstantiationDependent())
3859 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003860 if (exprs[i]->containsUnexpandedParameterPack())
3861 ExprBits.ContainsUnexpandedParameterPack = true;
3862
Nate Begeman5ec4b312009-08-10 23:49:36 +00003863 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003864 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003865}
3866
John McCall1bf58462011-02-16 08:02:54 +00003867const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3868 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3869 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003870 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3871 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003872 e = cast<CXXConstructExpr>(e)->getArg(0);
3873 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3874 e = ice->getSubExpr();
3875 return cast<OpaqueValueExpr>(e);
3876}
3877
Craig Topper37932912013-08-18 10:09:15 +00003878PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context,
3879 EmptyShell sh,
John McCallfe96e0b2011-11-06 09:01:30 +00003880 unsigned numSemanticExprs) {
3881 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3882 (1 + numSemanticExprs) * sizeof(Expr*),
3883 llvm::alignOf<PseudoObjectExpr>());
3884 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3885}
3886
3887PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3888 : Expr(PseudoObjectExprClass, shell) {
3889 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3890}
3891
Craig Topper37932912013-08-18 10:09:15 +00003892PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax,
John McCallfe96e0b2011-11-06 09:01:30 +00003893 ArrayRef<Expr*> semantics,
3894 unsigned resultIndex) {
3895 assert(syntax && "no syntactic expression!");
3896 assert(semantics.size() && "no semantic expressions!");
3897
3898 QualType type;
3899 ExprValueKind VK;
3900 if (resultIndex == NoResult) {
3901 type = C.VoidTy;
3902 VK = VK_RValue;
3903 } else {
3904 assert(resultIndex < semantics.size());
3905 type = semantics[resultIndex]->getType();
3906 VK = semantics[resultIndex]->getValueKind();
3907 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3908 }
3909
3910 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3911 (1 + semantics.size()) * sizeof(Expr*),
3912 llvm::alignOf<PseudoObjectExpr>());
3913 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3914 resultIndex);
3915}
3916
3917PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3918 Expr *syntax, ArrayRef<Expr*> semantics,
3919 unsigned resultIndex)
3920 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3921 /*filled in at end of ctor*/ false, false, false, false) {
3922 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3923 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3924
3925 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3926 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3927 getSubExprsBuffer()[i] = E;
3928
3929 if (E->isTypeDependent())
3930 ExprBits.TypeDependent = true;
3931 if (E->isValueDependent())
3932 ExprBits.ValueDependent = true;
3933 if (E->isInstantiationDependent())
3934 ExprBits.InstantiationDependent = true;
3935 if (E->containsUnexpandedParameterPack())
3936 ExprBits.ContainsUnexpandedParameterPack = true;
3937
3938 if (isa<OpaqueValueExpr>(E))
3939 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3940 "opaque-value semantic expressions for pseudo-object "
3941 "operations must have sources");
3942 }
3943}
3944
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003945//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003946// ExprIterator.
3947//===----------------------------------------------------------------------===//
3948
3949Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3950Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3951Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3952const Expr* ConstExprIterator::operator[](size_t idx) const {
3953 return cast<Expr>(I[idx]);
3954}
3955const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3956const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3957
3958//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003959// Child Iterators for iterating over subexpressions/substatements
3960//===----------------------------------------------------------------------===//
3961
Peter Collingbournee190dee2011-03-11 19:24:49 +00003962// UnaryExprOrTypeTraitExpr
3963Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003964 // If this is of a type and the type is a VLA type (and not a typedef), the
3965 // size expression of the VLA needs to be treated as an executable expression.
3966 // Why isn't this weirdness documented better in StmtIterator?
3967 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003968 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003969 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003970 return child_range(child_iterator(T), child_iterator());
3971 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003972 }
John McCallbd066782011-02-09 08:16:59 +00003973 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003974}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003975
Steve Naroffd54978b2007-09-18 23:55:05 +00003976// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003977Stmt::child_range ObjCMessageExpr::children() {
3978 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003979 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003980 begin = reinterpret_cast<Stmt **>(this + 1);
3981 else
3982 begin = reinterpret_cast<Stmt **>(getArgs());
3983 return child_range(begin,
3984 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003985}
3986
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003987ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003988 QualType T, ObjCMethodDecl *Method,
3989 SourceRange SR)
3990 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3991 false, false, false, false),
3992 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3993{
3994 Expr **SaveElements = getElements();
3995 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3996 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3997 ExprBits.ValueDependent = true;
3998 if (Elements[I]->isInstantiationDependent())
3999 ExprBits.InstantiationDependent = true;
4000 if (Elements[I]->containsUnexpandedParameterPack())
4001 ExprBits.ContainsUnexpandedParameterPack = true;
4002
4003 SaveElements[I] = Elements[I];
4004 }
4005}
4006
Craig Topperce7167c2013-08-22 04:58:56 +00004007ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004008 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004009 QualType T, ObjCMethodDecl * Method,
4010 SourceRange SR) {
4011 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4012 + Elements.size() * sizeof(Expr *));
4013 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
4014}
4015
Craig Topperce7167c2013-08-22 04:58:56 +00004016ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004017 unsigned NumElements) {
4018
4019 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
4020 + NumElements * sizeof(Expr *));
4021 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
4022}
4023
4024ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4025 ArrayRef<ObjCDictionaryElement> VK,
4026 bool HasPackExpansions,
4027 QualType T, ObjCMethodDecl *method,
4028 SourceRange SR)
4029 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4030 false, false),
4031 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4032 DictWithObjectsMethod(method)
4033{
4034 KeyValuePair *KeyValues = getKeyValues();
4035 ExpansionData *Expansions = getExpansionData();
4036 for (unsigned I = 0; I < NumElements; I++) {
4037 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4038 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4039 ExprBits.ValueDependent = true;
4040 if (VK[I].Key->isInstantiationDependent() ||
4041 VK[I].Value->isInstantiationDependent())
4042 ExprBits.InstantiationDependent = true;
4043 if (VK[I].EllipsisLoc.isInvalid() &&
4044 (VK[I].Key->containsUnexpandedParameterPack() ||
4045 VK[I].Value->containsUnexpandedParameterPack()))
4046 ExprBits.ContainsUnexpandedParameterPack = true;
4047
4048 KeyValues[I].Key = VK[I].Key;
4049 KeyValues[I].Value = VK[I].Value;
4050 if (Expansions) {
4051 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4052 if (VK[I].NumExpansions)
4053 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4054 else
4055 Expansions[I].NumExpansionsPlusOne = 0;
4056 }
4057 }
4058}
4059
4060ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004061ObjCDictionaryLiteral::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004062 ArrayRef<ObjCDictionaryElement> VK,
4063 bool HasPackExpansions,
4064 QualType T, ObjCMethodDecl *method,
4065 SourceRange SR) {
4066 unsigned ExpansionsSize = 0;
4067 if (HasPackExpansions)
4068 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4069
4070 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4071 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4072 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4073}
4074
4075ObjCDictionaryLiteral *
Craig Topperce7167c2013-08-22 04:58:56 +00004076ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004077 bool HasPackExpansions) {
4078 unsigned ExpansionsSize = 0;
4079 if (HasPackExpansions)
4080 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4081 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4082 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4083 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4084 HasPackExpansions);
4085}
4086
Craig Topperce7167c2013-08-22 04:58:56 +00004087ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(const ASTContext &C,
Ted Kremeneke65b0862012-03-06 20:05:56 +00004088 Expr *base,
4089 Expr *key, QualType T,
4090 ObjCMethodDecl *getMethod,
4091 ObjCMethodDecl *setMethod,
4092 SourceLocation RB) {
4093 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4094 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4095 OK_ObjCSubscript,
4096 getMethod, setMethod, RB);
4097}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004098
Benjamin Kramerc215e762012-08-24 11:54:20 +00004099AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004100 QualType t, AtomicOp op, SourceLocation RP)
4101 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4102 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004103 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004104{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004105 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4106 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004107 if (args[i]->isTypeDependent())
4108 ExprBits.TypeDependent = true;
4109 if (args[i]->isValueDependent())
4110 ExprBits.ValueDependent = true;
4111 if (args[i]->isInstantiationDependent())
4112 ExprBits.InstantiationDependent = true;
4113 if (args[i]->containsUnexpandedParameterPack())
4114 ExprBits.ContainsUnexpandedParameterPack = true;
4115
4116 SubExprs[i] = args[i];
4117 }
4118}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004119
4120unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4121 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004122 case AO__c11_atomic_init:
4123 case AO__c11_atomic_load:
4124 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004125 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004126
4127 case AO__c11_atomic_store:
4128 case AO__c11_atomic_exchange:
4129 case AO__atomic_load:
4130 case AO__atomic_store:
4131 case AO__atomic_store_n:
4132 case AO__atomic_exchange_n:
4133 case AO__c11_atomic_fetch_add:
4134 case AO__c11_atomic_fetch_sub:
4135 case AO__c11_atomic_fetch_and:
4136 case AO__c11_atomic_fetch_or:
4137 case AO__c11_atomic_fetch_xor:
4138 case AO__atomic_fetch_add:
4139 case AO__atomic_fetch_sub:
4140 case AO__atomic_fetch_and:
4141 case AO__atomic_fetch_or:
4142 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004143 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004144 case AO__atomic_add_fetch:
4145 case AO__atomic_sub_fetch:
4146 case AO__atomic_and_fetch:
4147 case AO__atomic_or_fetch:
4148 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004149 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004150 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004151
4152 case AO__atomic_exchange:
4153 return 4;
4154
4155 case AO__c11_atomic_compare_exchange_strong:
4156 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004157 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004158
4159 case AO__atomic_compare_exchange:
4160 case AO__atomic_compare_exchange_n:
4161 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004162 }
4163 llvm_unreachable("unknown atomic op");
4164}