blob: db4269bf163061a19fd02cb3e3ca7668b4228e1a [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"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000024#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000026#include "clang/Basic/CharInfo.h"
Chris Lattnere925d612010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000029#include "clang/Lex/Lexer.h"
30#include "clang/Lex/LiteralSupport.h"
31#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000032#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000033#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000034#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000035#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000036using namespace clang;
37
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000038const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000039 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000040
41 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000042 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
43 DerivedType = PTy->getPointeeType();
44
Rafael Espindola60a2bba2012-07-17 20:24:05 +000045 if (DerivedType->isDependentType())
46 return NULL;
47
Rafael Espindola49e860b2012-06-26 17:45:31 +000048 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000049 Decl *D = Ty->getDecl();
50 return cast<CXXRecordDecl>(D);
51}
52
Richard Smithf3fabd22013-06-03 00:17:11 +000053const Expr *Expr::skipRValueSubobjectAdjustments(
54 SmallVectorImpl<const Expr *> &CommaLHSs,
55 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
Rafael Espindola9c006de2012-10-27 01:03:43 +000056 const Expr *E = this;
57 while (true) {
58 E = E->IgnoreParens();
59
60 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
61 if ((CE->getCastKind() == CK_DerivedToBase ||
62 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
63 E->getType()->isRecordType()) {
64 E = CE->getSubExpr();
65 CXXRecordDecl *Derived
66 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
67 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
68 continue;
69 }
70
71 if (CE->getCastKind() == CK_NoOp) {
72 E = CE->getSubExpr();
73 continue;
74 }
75 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
76 if (!ME->isArrow() && ME->getBase()->isRValue()) {
77 assert(ME->getBase()->getType()->isRecordType());
78 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
Richard Smith2d187902013-06-03 07:13:35 +000079 if (!Field->isBitField()) {
80 E = ME->getBase();
81 Adjustments.push_back(SubobjectAdjustment(Field));
82 continue;
83 }
Rafael Espindola9c006de2012-10-27 01:03:43 +000084 }
85 }
86 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
87 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000088 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-10-27 01:03:43 +000089 E = BO->getLHS();
90 const MemberPointerType *MPT =
91 BO->getRHS()->getType()->getAs<MemberPointerType>();
92 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
Richard Smithf3fabd22013-06-03 00:17:11 +000093 continue;
94 } else if (BO->getOpcode() == BO_Comma) {
95 CommaLHSs.push_back(BO->getLHS());
96 E = BO->getRHS();
97 continue;
Rafael Espindola9c006de2012-10-27 01:03:43 +000098 }
99 }
100
101 // Nothing changed.
102 break;
103 }
104 return E;
105}
106
107const Expr *
108Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
109 const Expr *E = this;
Richard Smith852c9db2013-04-20 22:23:05 +0000110
111 // This might be a default initializer for a reference member. Walk over the
112 // wrapper node for that.
113 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
114 E = DAE->getExpr();
115
Rafael Espindola9c006de2012-10-27 01:03:43 +0000116 // Look through single-element init lists that claim to be lvalues. They're
117 // just syntactic wrappers in this case.
118 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
Richard Smith852c9db2013-04-20 22:23:05 +0000119 if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
Rafael Espindola9c006de2012-10-27 01:03:43 +0000120 E = ILE->getInit(0);
Richard Smith852c9db2013-04-20 22:23:05 +0000121 if (const CXXDefaultInitExpr *DAE = dyn_cast<CXXDefaultInitExpr>(E))
122 E = DAE->getExpr();
123 }
Rafael Espindola9c006de2012-10-27 01:03:43 +0000124 }
125
126 // Look through expressions for materialized temporaries (for now).
127 if (const MaterializeTemporaryExpr *M
128 = dyn_cast<MaterializeTemporaryExpr>(E)) {
129 MTE = M;
130 E = M->GetTemporaryExpr();
131 }
132
133 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
134 E = DAE->getExpr();
135 return E;
136}
137
Chris Lattner4ebae652010-04-16 23:34:13 +0000138/// isKnownToHaveBooleanValue - Return true if this is an integer expression
139/// that is known to return 0 or 1. This happens for _Bool/bool expressions
140/// but also int expressions which are produced by things like comparisons in
141/// C.
142bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000143 const Expr *E = IgnoreParens();
144
Chris Lattner4ebae652010-04-16 23:34:13 +0000145 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000146 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000147 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000148 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000149
Peter Collingbourne91147592011-04-15 00:35:48 +0000150 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000151 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000152 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000153 return UO->getSubExpr()->isKnownToHaveBooleanValue();
154 default:
155 return false;
156 }
157 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000158
John McCall45d30c32010-06-12 01:56:02 +0000159 // Only look through implicit casts. If the user writes
160 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000161 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000162 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000163
Peter Collingbourne91147592011-04-15 00:35:48 +0000164 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000165 switch (BO->getOpcode()) {
166 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000167 case BO_LT: // Relational operators.
168 case BO_GT:
169 case BO_LE:
170 case BO_GE:
171 case BO_EQ: // Equality operators.
172 case BO_NE:
173 case BO_LAnd: // AND operator.
174 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000175 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000176
John McCalle3027922010-08-25 11:45:40 +0000177 case BO_And: // Bitwise AND operator.
178 case BO_Xor: // Bitwise XOR operator.
179 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000180 // Handle things like (x==2)|(y==12).
181 return BO->getLHS()->isKnownToHaveBooleanValue() &&
182 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000183
John McCalle3027922010-08-25 11:45:40 +0000184 case BO_Comma:
185 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000186 return BO->getRHS()->isKnownToHaveBooleanValue();
187 }
188 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000189
Peter Collingbourne91147592011-04-15 00:35:48 +0000190 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000191 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
192 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000193
Chris Lattner4ebae652010-04-16 23:34:13 +0000194 return false;
195}
196
John McCallbd066782011-02-09 08:16:59 +0000197// Amusing macro metaprogramming hack: check whether a class provides
198// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000199//
200// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000201namespace {
202 /// This implementation is used when a class provides a custom
203 /// implementation of getExprLoc.
204 template <class E, class T>
205 SourceLocation getExprLocImpl(const Expr *expr,
206 SourceLocation (T::*v)() const) {
207 return static_cast<const E*>(expr)->getExprLoc();
208 }
209
210 /// This implementation is used when a class doesn't provide
211 /// a custom implementation of getExprLoc. Overload resolution
212 /// should pick it over the implementation above because it's
213 /// more specialized according to function template partial ordering.
214 template <class E>
215 SourceLocation getExprLocImpl(const Expr *expr,
216 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000217 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000218 }
219}
220
221SourceLocation Expr::getExprLoc() const {
222 switch (getStmtClass()) {
223 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
224#define ABSTRACT_STMT(type)
225#define STMT(type, base) \
226 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
227#define EXPR(type, base) \
228 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
229#include "clang/AST/StmtNodes.inc"
230 }
231 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000232}
233
Chris Lattner0eedafe2006-08-24 04:56:27 +0000234//===----------------------------------------------------------------------===//
235// Primary Expressions.
236//===----------------------------------------------------------------------===//
237
Douglas Gregor678d76c2011-07-01 01:22:09 +0000238/// \brief Compute the type-, value-, and instantiation-dependence of a
239/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000240/// based on the declaration being referenced.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000241static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000242 bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000243 bool &ValueDependent,
244 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000245 TypeDependent = false;
246 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000247 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000248
249 // (TD) C++ [temp.dep.expr]p3:
250 // An id-expression is type-dependent if it contains:
251 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000252 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000253 //
254 // (VD) C++ [temp.dep.constexpr]p2:
255 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256
Douglas Gregored6c7442009-11-23 11:41:28 +0000257 // (TD) - an identifier that was declared with dependent type
258 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000259 if (T->isDependentType()) {
260 TypeDependent = true;
261 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000262 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000263 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000264 } else if (T->isInstantiationDependentType()) {
265 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000266 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000267
Douglas Gregored6c7442009-11-23 11:41:28 +0000268 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000269 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000270 == DeclarationName::CXXConversionFunctionName) {
271 QualType T = D->getDeclName().getCXXNameType();
272 if (T->isDependentType()) {
273 TypeDependent = true;
274 ValueDependent = true;
275 InstantiationDependent = true;
276 return;
277 }
278
279 if (T->isInstantiationDependentType())
280 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000281 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000282
Douglas Gregored6c7442009-11-23 11:41:28 +0000283 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000284 if (isa<NonTypeTemplateParmDecl>(D)) {
285 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000286 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000287 return;
288 }
289
Douglas Gregored6c7442009-11-23 11:41:28 +0000290 // (VD) - a constant with integral or enumeration type and is
291 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000292 // (VD) - a constant with literal type and is initialized with an
293 // expression that is value-dependent [C++11].
294 // (VD) - FIXME: Missing from the standard:
295 // - an entity with reference type and is initialized with an
296 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000297 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000298 if ((Ctx.getLangOpts().CPlusPlus11 ?
Richard Smithd9f663b2013-04-22 15:31:51 +0000299 Var->getType()->isLiteralType(Ctx) :
Richard Smithec8dcd22011-11-08 01:31:09 +0000300 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000301 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000302 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000303 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000304 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000305 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000306 InstantiationDependent = true;
307 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000308 }
309
Douglas Gregor0e4de762010-05-11 08:41:30 +0000310 // (VD) - FIXME: Missing from the standard:
311 // - a member function or a static data member of the current
312 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000313 if (Var->isStaticDataMember() &&
314 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000315 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000316 InstantiationDependent = true;
317 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000318
319 return;
320 }
321
Douglas Gregor0e4de762010-05-11 08:41:30 +0000322 // (VD) - FIXME: Missing from the standard:
323 // - a member function or a static data member of the current
324 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000325 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
326 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000327 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000328 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000329}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000330
Daniel Dunbar9d355812012-03-09 01:51:51 +0000331void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000332 bool TypeDependent = false;
333 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000334 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000335 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
336 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000337
338 // (TD) C++ [temp.dep.expr]p3:
339 // An id-expression is type-dependent if it contains:
340 //
341 // and
342 //
343 // (VD) C++ [temp.dep.constexpr]p2:
344 // An identifier is value-dependent if it is:
345 if (!TypeDependent && !ValueDependent &&
346 hasExplicitTemplateArgs() &&
347 TemplateSpecializationType::anyDependentTemplateArguments(
348 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000349 getNumTemplateArgs(),
350 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000351 TypeDependent = true;
352 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000353 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000354 }
355
356 ExprBits.TypeDependent = TypeDependent;
357 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000358 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000359
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000360 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000361 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000362 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000363}
364
Daniel Dunbar9d355812012-03-09 01:51:51 +0000365DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000368 ValueDecl *D, bool RefersToEnclosingLocal,
369 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000370 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000371 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000372 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000373 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000374 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
375 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000376 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000377 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000378 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
379 if (FoundD)
380 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000381 DeclRefExprBits.HasTemplateKWAndArgsInfo
382 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000383 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000384 if (TemplateArgs) {
385 bool Dependent = false;
386 bool InstantiationDependent = false;
387 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000388 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
389 Dependent,
390 InstantiationDependent,
391 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000392 if (InstantiationDependent)
393 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000394 } else if (TemplateKWLoc.isValid()) {
395 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000396 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000397 DeclRefExprBits.HadMultipleCandidates = 0;
398
Daniel Dunbar9d355812012-03-09 01:51:51 +0000399 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000400}
401
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000402DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000403 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000404 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000405 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000406 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000407 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000408 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000409 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000410 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000411 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000412 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000413 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000414 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000415 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000416}
417
418DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000419 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000420 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000421 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000422 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000423 const DeclarationNameInfo &NameInfo,
424 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000425 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000426 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000427 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000428 // Filter out cases where the found Decl is the same as the value refenenced.
429 if (D == FoundD)
430 FoundD = 0;
431
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000432 std::size_t Size = sizeof(DeclRefExpr);
David Blaikie7d170102013-05-15 07:37:26 +0000433 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000434 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000435 if (FoundD)
436 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000437 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000438 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
439 else if (TemplateKWLoc.isValid())
440 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000441
Chris Lattner5c0b4052010-10-30 05:14:06 +0000442 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000443 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000444 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000445 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000446}
447
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000448DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000449 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000450 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000451 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000452 unsigned NumTemplateArgs) {
453 std::size_t Size = sizeof(DeclRefExpr);
454 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000455 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000456 if (HasFoundDecl)
457 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000458 if (HasTemplateKWAndArgsInfo)
459 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000460
Chris Lattner5c0b4052010-10-30 05:14:06 +0000461 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000462 return new (Mem) DeclRefExpr(EmptyShell());
463}
464
Daniel Dunbarb507f272012-03-09 15:39:15 +0000465SourceLocation DeclRefExpr::getLocStart() const {
466 if (hasQualifier())
467 return getQualifierLoc().getBeginLoc();
468 return getNameInfo().getLocStart();
469}
470SourceLocation DeclRefExpr::getLocEnd() const {
471 if (hasExplicitTemplateArgs())
472 return getRAngleLoc();
473 return getNameInfo().getLocEnd();
474}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000475
Anders Carlsson2fb08242009-09-08 18:24:21 +0000476// FIXME: Maybe this should use DeclPrinter with a special "print predefined
477// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000478std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
479 ASTContext &Context = CurrentDecl->getASTContext();
480
Anders Carlsson2fb08242009-09-08 18:24:21 +0000481 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000482 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000483 return FD->getNameAsString();
484
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000485 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000486 llvm::raw_svector_ostream Out(Name);
487
488 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000489 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000490 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000491 if (MD->isStatic())
492 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000493 }
494
David Blaikiebbafb8a2012-03-11 07:00:24 +0000495 PrintingPolicy Policy(Context.getLangOpts());
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000496 std::string Proto;
Douglas Gregor11a434a2012-04-10 20:14:15 +0000497 llvm::raw_string_ostream POut(Proto);
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000498 FD->printQualifiedName(POut, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000499
Douglas Gregor11a434a2012-04-10 20:14:15 +0000500 const FunctionDecl *Decl = FD;
501 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
502 Decl = Pattern;
503 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000504 const FunctionProtoType *FT = 0;
505 if (FD->hasWrittenPrototype())
506 FT = dyn_cast<FunctionProtoType>(AFT);
507
Douglas Gregor11a434a2012-04-10 20:14:15 +0000508 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000509 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000510 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000511 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000512 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000513 }
514
515 if (FT->isVariadic()) {
516 if (FD->getNumParams()) POut << ", ";
517 POut << "...";
518 }
519 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000520 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000521
Sam Weinig4e83bd22009-12-27 01:38:20 +0000522 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis53e3d6d2012-12-14 19:44:11 +0000523 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikief5697e52012-08-10 00:55:35 +0000524 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000525 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000526 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000527 POut << " volatile";
528 RefQualifierKind Ref = MD->getRefQualifier();
529 if (Ref == RQ_LValue)
530 POut << " &";
531 else if (Ref == RQ_RValue)
532 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000533 }
534
Douglas Gregor11a434a2012-04-10 20:14:15 +0000535 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
536 SpecsTy Specs;
537 const DeclContext *Ctx = FD->getDeclContext();
538 while (Ctx && isa<NamedDecl>(Ctx)) {
539 const ClassTemplateSpecializationDecl *Spec
540 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
541 if (Spec && !Spec->isExplicitSpecialization())
542 Specs.push_back(Spec);
543 Ctx = Ctx->getParent();
544 }
545
546 std::string TemplateParams;
547 llvm::raw_string_ostream TOut(TemplateParams);
548 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
549 I != E; ++I) {
550 const TemplateParameterList *Params
551 = (*I)->getSpecializedTemplate()->getTemplateParameters();
552 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
553 assert(Params->size() == Args.size());
554 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
555 StringRef Param = Params->getParam(i)->getName();
556 if (Param.empty()) continue;
557 TOut << Param << " = ";
558 Args.get(i).print(Policy, TOut);
559 TOut << ", ";
560 }
561 }
562
563 FunctionTemplateSpecializationInfo *FSI
564 = FD->getTemplateSpecializationInfo();
565 if (FSI && !FSI->isExplicitSpecialization()) {
566 const TemplateParameterList* Params
567 = FSI->getTemplate()->getTemplateParameters();
568 const TemplateArgumentList* Args = FSI->TemplateArguments;
569 assert(Params->size() == Args->size());
570 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
571 StringRef Param = Params->getParam(i)->getName();
572 if (Param.empty()) continue;
573 TOut << Param << " = ";
574 Args->get(i).print(Policy, TOut);
575 TOut << ", ";
576 }
577 }
578
579 TOut.flush();
580 if (!TemplateParams.empty()) {
581 // remove the trailing comma and space
582 TemplateParams.resize(TemplateParams.size() - 2);
583 POut << " [" << TemplateParams << "]";
584 }
585
586 POut.flush();
587
Sam Weinigd060ed42009-12-06 23:55:13 +0000588 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
589 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000590
591 Out << Proto;
592
593 Out.flush();
594 return Name.str().str();
595 }
596 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000597 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000598 llvm::raw_svector_ostream Out(Name);
599 Out << (MD->isInstanceMethod() ? '-' : '+');
600 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000601
602 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
603 // a null check to avoid a crash.
604 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000605 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000606
Anders Carlsson2fb08242009-09-08 18:24:21 +0000607 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000608 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000609 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000610
Anders Carlsson2fb08242009-09-08 18:24:21 +0000611 Out << ' ';
612 Out << MD->getSelector().getAsString();
613 Out << ']';
614
615 Out.flush();
616 return Name.str().str();
617 }
618 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
619 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
620 return "top level";
621 }
622 return "";
623}
624
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000625void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
626 if (hasAllocation())
627 C.Deallocate(pVal);
628
629 BitWidth = Val.getBitWidth();
630 unsigned NumWords = Val.getNumWords();
631 const uint64_t* Words = Val.getRawData();
632 if (NumWords > 1) {
633 pVal = new (C) uint64_t[NumWords];
634 std::copy(Words, Words + NumWords, pVal);
635 } else if (NumWords == 1)
636 VAL = Words[0];
637 else
638 VAL = 0;
639}
640
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000641IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
642 QualType type, SourceLocation l)
643 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
644 false, false),
645 Loc(l) {
646 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
647 assert(V.getBitWidth() == C.getIntWidth(type) &&
648 "Integer type is not the correct size for constant.");
649 setValue(C, V);
650}
651
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000652IntegerLiteral *
653IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
654 QualType type, SourceLocation l) {
655 return new (C) IntegerLiteral(C, V, type, l);
656}
657
658IntegerLiteral *
659IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
660 return new (C) IntegerLiteral(Empty);
661}
662
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000663FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
664 bool isexact, QualType Type, SourceLocation L)
665 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
666 false, false), Loc(L) {
Tim Northover178723a2013-01-22 09:46:51 +0000667 setSemantics(V.getSemantics());
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000668 FloatingLiteralBits.IsExact = isexact;
669 setValue(C, V);
670}
671
672FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
673 : Expr(FloatingLiteralClass, Empty) {
Tim Northover178723a2013-01-22 09:46:51 +0000674 setRawSemantics(IEEEhalf);
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000675 FloatingLiteralBits.IsExact = false;
676}
677
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000678FloatingLiteral *
679FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
680 bool isexact, QualType Type, SourceLocation L) {
681 return new (C) FloatingLiteral(C, V, isexact, Type, L);
682}
683
684FloatingLiteral *
685FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000686 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000687}
688
Tim Northover178723a2013-01-22 09:46:51 +0000689const llvm::fltSemantics &FloatingLiteral::getSemantics() const {
690 switch(FloatingLiteralBits.Semantics) {
691 case IEEEhalf:
692 return llvm::APFloat::IEEEhalf;
693 case IEEEsingle:
694 return llvm::APFloat::IEEEsingle;
695 case IEEEdouble:
696 return llvm::APFloat::IEEEdouble;
697 case x87DoubleExtended:
698 return llvm::APFloat::x87DoubleExtended;
699 case IEEEquad:
700 return llvm::APFloat::IEEEquad;
701 case PPCDoubleDouble:
702 return llvm::APFloat::PPCDoubleDouble;
703 }
704 llvm_unreachable("Unrecognised floating semantics");
705}
706
707void FloatingLiteral::setSemantics(const llvm::fltSemantics &Sem) {
708 if (&Sem == &llvm::APFloat::IEEEhalf)
709 FloatingLiteralBits.Semantics = IEEEhalf;
710 else if (&Sem == &llvm::APFloat::IEEEsingle)
711 FloatingLiteralBits.Semantics = IEEEsingle;
712 else if (&Sem == &llvm::APFloat::IEEEdouble)
713 FloatingLiteralBits.Semantics = IEEEdouble;
714 else if (&Sem == &llvm::APFloat::x87DoubleExtended)
715 FloatingLiteralBits.Semantics = x87DoubleExtended;
716 else if (&Sem == &llvm::APFloat::IEEEquad)
717 FloatingLiteralBits.Semantics = IEEEquad;
718 else if (&Sem == &llvm::APFloat::PPCDoubleDouble)
719 FloatingLiteralBits.Semantics = PPCDoubleDouble;
720 else
721 llvm_unreachable("Unknown floating semantics");
722}
723
Chris Lattnera0173132008-06-07 22:13:43 +0000724/// getValueAsApproximateDouble - This returns the value as an inaccurate
725/// double. Note that this may cause loss of precision, but is useful for
726/// debugging dumps, etc.
727double FloatingLiteral::getValueAsApproximateDouble() const {
728 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000729 bool ignored;
730 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
731 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000732 return V.convertToDouble();
733}
734
Nick Lewycky4ed84042012-02-24 09:07:53 +0000735int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000736 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000737 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000738 case Ascii:
739 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000740 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000741 break;
742 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000743 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000744 break;
745 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000746 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000747 break;
748 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000749 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000750 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000751 }
752 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
753 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000754 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000755 && "character byte widths supported are 1, 2, and 4 only");
756 return CharByteWidth;
757}
758
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000759StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000760 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000761 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000762 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000763 // Allocate enough space for the StringLiteral plus an array of locations for
764 // any concatenated string tokens.
765 void *Mem = C.Allocate(sizeof(StringLiteral)+
766 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000767 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000768 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000769
Steve Naroffdf7855b2007-02-21 23:46:25 +0000770 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000771 SL->setString(C,Str,Kind,Pascal);
772
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000773 SL->TokLocs[0] = Loc[0];
774 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000775
Chris Lattner630970d2009-02-18 05:49:11 +0000776 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000777 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
778 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000779}
780
Douglas Gregor958dfc92009-04-15 16:35:07 +0000781StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
782 void *Mem = C.Allocate(sizeof(StringLiteral)+
783 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000784 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000785 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000786 SL->CharByteWidth = 0;
787 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000788 SL->NumConcatenated = NumStrs;
789 return SL;
790}
791
Alexander Kornienko540bacb2013-02-01 12:35:51 +0000792void StringLiteral::outputString(raw_ostream &OS) const {
Richard Trieudc355912012-06-13 20:25:24 +0000793 switch (getKind()) {
794 case Ascii: break; // no prefix.
795 case Wide: OS << 'L'; break;
796 case UTF8: OS << "u8"; break;
797 case UTF16: OS << 'u'; break;
798 case UTF32: OS << 'U'; break;
799 }
800 OS << '"';
801 static const char Hex[] = "0123456789ABCDEF";
802
803 unsigned LastSlashX = getLength();
804 for (unsigned I = 0, N = getLength(); I != N; ++I) {
805 switch (uint32_t Char = getCodeUnit(I)) {
806 default:
807 // FIXME: Convert UTF-8 back to codepoints before rendering.
808
809 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
810 // Leave invalid surrogates alone; we'll use \x for those.
811 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
812 Char <= 0xdbff) {
813 uint32_t Trail = getCodeUnit(I + 1);
814 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
815 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
816 ++I;
817 }
818 }
819
820 if (Char > 0xff) {
821 // If this is a wide string, output characters over 0xff using \x
822 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
823 // codepoint: use \x escapes for invalid codepoints.
824 if (getKind() == Wide ||
825 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
826 // FIXME: Is this the best way to print wchar_t?
827 OS << "\\x";
828 int Shift = 28;
829 while ((Char >> Shift) == 0)
830 Shift -= 4;
831 for (/**/; Shift >= 0; Shift -= 4)
832 OS << Hex[(Char >> Shift) & 15];
833 LastSlashX = I;
834 break;
835 }
836
837 if (Char > 0xffff)
838 OS << "\\U00"
839 << Hex[(Char >> 20) & 15]
840 << Hex[(Char >> 16) & 15];
841 else
842 OS << "\\u";
843 OS << Hex[(Char >> 12) & 15]
844 << Hex[(Char >> 8) & 15]
845 << Hex[(Char >> 4) & 15]
846 << Hex[(Char >> 0) & 15];
847 break;
848 }
849
850 // If we used \x... for the previous character, and this character is a
851 // hexadecimal digit, prevent it being slurped as part of the \x.
852 if (LastSlashX + 1 == I) {
853 switch (Char) {
854 case '0': case '1': case '2': case '3': case '4':
855 case '5': case '6': case '7': case '8': case '9':
856 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
857 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
858 OS << "\"\"";
859 }
860 }
861
862 assert(Char <= 0xff &&
863 "Characters above 0xff should already have been handled.");
864
Jordan Rosea7d03842013-02-08 22:30:41 +0000865 if (isPrintable(Char))
Richard Trieudc355912012-06-13 20:25:24 +0000866 OS << (char)Char;
867 else // Output anything hard as an octal escape.
868 OS << '\\'
869 << (char)('0' + ((Char >> 6) & 7))
870 << (char)('0' + ((Char >> 3) & 7))
871 << (char)('0' + ((Char >> 0) & 7));
872 break;
873 // Handle some common non-printable cases to make dumps prettier.
874 case '\\': OS << "\\\\"; break;
875 case '"': OS << "\\\""; break;
876 case '\n': OS << "\\n"; break;
877 case '\t': OS << "\\t"; break;
878 case '\a': OS << "\\a"; break;
879 case '\b': OS << "\\b"; break;
880 }
881 }
882 OS << '"';
883}
884
Eli Friedmanfcec6302011-11-01 02:23:42 +0000885void StringLiteral::setString(ASTContext &C, StringRef Str,
886 StringKind Kind, bool IsPascal) {
887 //FIXME: we assume that the string data comes from a target that uses the same
888 // code unit size and endianess for the type of string.
889 this->Kind = Kind;
890 this->IsPascal = IsPascal;
891
Nick Lewycky4ed84042012-02-24 09:07:53 +0000892 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000893 assert((Str.size()%CharByteWidth == 0)
894 && "size of data must be multiple of CharByteWidth");
895 Length = Str.size()/CharByteWidth;
896
897 switch(CharByteWidth) {
898 case 1: {
899 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000900 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000901 StrData.asChar = AStrData;
902 break;
903 }
904 case 2: {
905 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000906 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000907 StrData.asUInt16 = AStrData;
908 break;
909 }
910 case 4: {
911 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000912 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000913 StrData.asUInt32 = AStrData;
914 break;
915 }
916 default:
917 assert(false && "unsupported CharByteWidth");
918 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000919}
920
Chris Lattnere925d612010-11-17 07:37:15 +0000921/// getLocationOfByte - Return a source location that points to the specified
922/// byte of this string literal.
923///
924/// Strings are amazingly complex. They can be formed from multiple tokens and
925/// can have escape sequences in them in addition to the usual trigraph and
926/// escaped newline business. This routine handles this complexity.
927///
928SourceLocation StringLiteral::
929getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
930 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000931 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
932 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000933
Chris Lattnere925d612010-11-17 07:37:15 +0000934 // Loop over all of the tokens in this string until we find the one that
935 // contains the byte we're looking for.
936 unsigned TokNo = 0;
937 while (1) {
938 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
939 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
940
941 // Get the spelling of the string so that we can get the data that makes up
942 // the string literal, not the identifier for the macro it is potentially
943 // expanded through.
944 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
945
946 // Re-lex the token to get its length and original spelling.
947 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
948 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000949 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000950 if (Invalid)
951 return StrTokSpellingLoc;
952
953 const char *StrData = Buffer.data()+LocInfo.second;
954
Chris Lattnere925d612010-11-17 07:37:15 +0000955 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000956 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
957 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000958 Token TheTok;
959 TheLexer.LexFromRawLexer(TheTok);
960
961 // Use the StringLiteralParser to compute the length of the string in bytes.
962 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
963 unsigned TokNumBytes = SLP.GetStringLength();
964
965 // If the byte is in this token, return the location of the byte.
966 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000967 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000968 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
969
970 // Now that we know the offset of the token in the spelling, use the
971 // preprocessor to get the offset in the original source.
972 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
973 }
974
975 // Move to the next string token.
976 ++TokNo;
977 ByteNo -= TokNumBytes;
978 }
979}
980
981
982
Chris Lattner1b926492006-08-23 06:42:10 +0000983/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
984/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +0000985StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000986 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000987 case UO_PostInc: return "++";
988 case UO_PostDec: return "--";
989 case UO_PreInc: return "++";
990 case UO_PreDec: return "--";
991 case UO_AddrOf: return "&";
992 case UO_Deref: return "*";
993 case UO_Plus: return "+";
994 case UO_Minus: return "-";
995 case UO_Not: return "~";
996 case UO_LNot: return "!";
997 case UO_Real: return "__real";
998 case UO_Imag: return "__imag";
999 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +00001000 }
David Blaikief47fa302012-01-17 02:30:50 +00001001 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +00001002}
1003
John McCalle3027922010-08-25 11:45:40 +00001004UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +00001005UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
1006 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001007 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +00001008 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
1009 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
1010 case OO_Amp: return UO_AddrOf;
1011 case OO_Star: return UO_Deref;
1012 case OO_Plus: return UO_Plus;
1013 case OO_Minus: return UO_Minus;
1014 case OO_Tilde: return UO_Not;
1015 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +00001016 }
1017}
1018
1019OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
1020 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +00001021 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
1022 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
1023 case UO_AddrOf: return OO_Amp;
1024 case UO_Deref: return OO_Star;
1025 case UO_Plus: return OO_Plus;
1026 case UO_Minus: return OO_Minus;
1027 case UO_Not: return OO_Tilde;
1028 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +00001029 default: return OO_None;
1030 }
1031}
1032
1033
Chris Lattner0eedafe2006-08-24 04:56:27 +00001034//===----------------------------------------------------------------------===//
1035// Postfix Operators.
1036//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +00001037
Peter Collingbourne3a347252011-02-08 21:18:02 +00001038CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001039 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +00001040 SourceLocation rparenloc)
1041 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001042 fn->isTypeDependent(),
1043 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001044 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001045 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001046 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001047
Benjamin Kramerc215e762012-08-24 11:54:20 +00001048 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001049 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001050 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001051 if (args[i]->isTypeDependent())
1052 ExprBits.TypeDependent = true;
1053 if (args[i]->isValueDependent())
1054 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001055 if (args[i]->isInstantiationDependent())
1056 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001057 if (args[i]->containsUnexpandedParameterPack())
1058 ExprBits.ContainsUnexpandedParameterPack = true;
1059
Peter Collingbourne3a347252011-02-08 21:18:02 +00001060 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001061 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001062
Peter Collingbourne3a347252011-02-08 21:18:02 +00001063 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001064 RParenLoc = rparenloc;
1065}
Nate Begeman1e36a852008-01-17 17:46:27 +00001066
Benjamin Kramerc215e762012-08-24 11:54:20 +00001067CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001068 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1069 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001070 fn->isTypeDependent(),
1071 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001072 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001073 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001074 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001075
Benjamin Kramerc215e762012-08-24 11:54:20 +00001076 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001077 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001078 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001079 if (args[i]->isTypeDependent())
1080 ExprBits.TypeDependent = true;
1081 if (args[i]->isValueDependent())
1082 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001083 if (args[i]->isInstantiationDependent())
1084 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001085 if (args[i]->containsUnexpandedParameterPack())
1086 ExprBits.ContainsUnexpandedParameterPack = true;
1087
Peter Collingbourne3a347252011-02-08 21:18:02 +00001088 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001089 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001090
Peter Collingbourne3a347252011-02-08 21:18:02 +00001091 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001092 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001093}
1094
Mike Stump11289f42009-09-09 15:08:12 +00001095CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1096 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001097 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001098 SubExprs = new (C) Stmt*[PREARGS_START];
1099 CallExprBits.NumPreArgs = 0;
1100}
1101
1102CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1103 EmptyShell Empty)
1104 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1105 // FIXME: Why do we allocate this?
1106 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1107 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001108}
1109
Nuno Lopes518e3702009-12-20 23:11:08 +00001110Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001111 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001112
1113 while (SubstNonTypeTemplateParmExpr *NTTP
1114 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1115 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1116 }
1117
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001118 // If we're calling a dereference, look at the pointer instead.
1119 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1120 if (BO->isPtrMemOp())
1121 CEE = BO->getRHS()->IgnoreParenCasts();
1122 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1123 if (UO->getOpcode() == UO_Deref)
1124 CEE = UO->getSubExpr()->IgnoreParenCasts();
1125 }
Chris Lattner52301912009-07-17 15:46:27 +00001126 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001127 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001128 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1129 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001130
1131 return 0;
1132}
1133
Nuno Lopes518e3702009-12-20 23:11:08 +00001134FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001135 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001136}
1137
Chris Lattnere4407ed2007-12-28 05:25:02 +00001138/// setNumArgs - This changes the number of arguments present in this call.
1139/// Any orphaned expressions are deleted by this, and any new operands are set
1140/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +00001141void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001142 // No change, just return.
1143 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001144
Chris Lattnere4407ed2007-12-28 05:25:02 +00001145 // If shrinking # arguments, just delete the extras and forgot them.
1146 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001147 this->NumArgs = NumArgs;
1148 return;
1149 }
1150
1151 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001152 unsigned NumPreArgs = getNumPreArgs();
1153 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001154 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001155 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001156 NewSubExprs[i] = SubExprs[i];
1157 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001158 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1159 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001160 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregorba6e5572009-04-17 21:46:47 +00001162 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001163 SubExprs = NewSubExprs;
1164 this->NumArgs = NumArgs;
1165}
1166
Chris Lattner01ff98a2008-10-06 05:00:53 +00001167/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1168/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +00001169unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001170 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001171 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001172 // ImplicitCastExpr.
1173 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1174 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001175 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001176
Steve Narofff6e3b3292008-01-31 01:07:12 +00001177 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1178 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001179 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001180
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001181 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1182 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001183 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001184
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001185 if (!FDecl->getIdentifier())
1186 return 0;
1187
Douglas Gregor15fc9562009-09-12 00:22:50 +00001188 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001189}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001190
Richard Smith5011a002013-01-17 23:46:04 +00001191bool CallExpr::isUnevaluatedBuiltinCall(ASTContext &Ctx) const {
1192 if (unsigned BI = isBuiltinCall())
1193 return Ctx.BuiltinInfo.isUnevaluated(BI);
1194 return false;
1195}
1196
Anders Carlsson00a27592009-05-26 04:57:27 +00001197QualType CallExpr::getCallReturnType() const {
1198 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001199 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001200 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001201 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001202 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001203 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1204 // This should never be overloaded and so should never return null.
1205 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001206
John McCall0009fcc2011-04-26 20:42:42 +00001207 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001208 return FnType->getResultType();
1209}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001210
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001211SourceLocation CallExpr::getLocStart() const {
1212 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001213 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001214
1215 SourceLocation begin = getCallee()->getLocStart();
1216 if (begin.isInvalid() && getNumArgs() > 0)
1217 begin = getArg(0)->getLocStart();
1218 return begin;
1219}
1220SourceLocation CallExpr::getLocEnd() const {
1221 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001222 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001223
1224 SourceLocation end = getRParenLoc();
1225 if (end.isInvalid() && getNumArgs() > 0)
1226 end = getArg(getNumArgs() - 1)->getLocEnd();
1227 return end;
1228}
John McCall701417a2011-02-21 06:23:05 +00001229
Alexis Hunta8136cc2010-05-05 15:23:54 +00001230OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001231 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001232 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001233 ArrayRef<OffsetOfNode> comps,
1234 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001235 SourceLocation RParenLoc) {
1236 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001237 sizeof(OffsetOfNode) * comps.size() +
1238 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001239
Benjamin Kramerc215e762012-08-24 11:54:20 +00001240 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1241 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001242}
1243
1244OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1245 unsigned numComps, unsigned numExprs) {
1246 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1247 sizeof(OffsetOfNode) * numComps +
1248 sizeof(Expr*) * numExprs);
1249 return new (Mem) OffsetOfExpr(numComps, numExprs);
1250}
1251
Alexis Hunta8136cc2010-05-05 15:23:54 +00001252OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001253 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001254 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001255 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001256 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1257 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001258 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001259 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001260 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001261 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001262 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001263{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001264 for (unsigned i = 0; i != comps.size(); ++i) {
1265 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001266 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001267
Benjamin Kramerc215e762012-08-24 11:54:20 +00001268 for (unsigned i = 0; i != exprs.size(); ++i) {
1269 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001270 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001271 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001272 ExprBits.ContainsUnexpandedParameterPack = true;
1273
Benjamin Kramerc215e762012-08-24 11:54:20 +00001274 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001275 }
1276}
1277
1278IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1279 assert(getKind() == Field || getKind() == Identifier);
1280 if (getKind() == Field)
1281 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001282
Douglas Gregor882211c2010-04-28 22:16:22 +00001283 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1284}
1285
Mike Stump11289f42009-09-09 15:08:12 +00001286MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001287 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001288 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001289 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001290 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001291 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001292 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001293 QualType ty,
1294 ExprValueKind vk,
1295 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001296 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001297
Douglas Gregorea972d32011-02-28 21:54:11 +00001298 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001299 founddecl.getDecl() != memberdecl ||
1300 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001301 if (hasQualOrFound)
1302 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001303
John McCall6b51f282009-11-23 01:53:49 +00001304 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001305 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1306 else if (TemplateKWLoc.isValid())
1307 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattner5c0b4052010-10-30 05:14:06 +00001309 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001310 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1311 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001312
1313 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001314 // FIXME: Wrong. We should be looking at the member declaration we found.
1315 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001316 E->setValueDependent(true);
1317 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001318 E->setInstantiationDependent(true);
1319 }
1320 else if (QualifierLoc &&
1321 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1322 E->setInstantiationDependent(true);
1323
John McCall16df1e52010-03-30 21:47:33 +00001324 E->HasQualifierOrFoundDecl = true;
1325
1326 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001327 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001328 NQ->FoundDecl = founddecl;
1329 }
1330
Abramo Bagnara7945c982012-01-27 09:46:47 +00001331 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1332
John McCall16df1e52010-03-30 21:47:33 +00001333 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001334 bool Dependent = false;
1335 bool InstantiationDependent = false;
1336 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001337 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1338 Dependent,
1339 InstantiationDependent,
1340 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001341 if (InstantiationDependent)
1342 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001343 } else if (TemplateKWLoc.isValid()) {
1344 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001345 }
1346
1347 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001348}
1349
Daniel Dunbarb507f272012-03-09 15:39:15 +00001350SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001351 if (isImplicitAccess()) {
1352 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001353 return getQualifierLoc().getBeginLoc();
1354 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001355 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001356
Daniel Dunbarb507f272012-03-09 15:39:15 +00001357 // FIXME: We don't want this to happen. Rather, we should be able to
1358 // detect all kinds of implicit accesses more cleanly.
1359 SourceLocation BaseStartLoc = getBase()->getLocStart();
1360 if (BaseStartLoc.isValid())
1361 return BaseStartLoc;
1362 return MemberLoc;
1363}
1364SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001365 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001366 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001367 EndLoc = getRAngleLoc();
1368 else if (EndLoc.isInvalid())
1369 EndLoc = getBase()->getLocEnd();
1370 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001371}
1372
John McCall9320b872011-09-09 05:25:32 +00001373void CastExpr::CheckCastConsistency() const {
1374 switch (getCastKind()) {
1375 case CK_DerivedToBase:
1376 case CK_UncheckedDerivedToBase:
1377 case CK_DerivedToBaseMemberPointer:
1378 case CK_BaseToDerived:
1379 case CK_BaseToDerivedMemberPointer:
1380 assert(!path_empty() && "Cast kind should have a base path!");
1381 break;
1382
1383 case CK_CPointerToObjCPointerCast:
1384 assert(getType()->isObjCObjectPointerType());
1385 assert(getSubExpr()->getType()->isPointerType());
1386 goto CheckNoBasePath;
1387
1388 case CK_BlockPointerToObjCPointerCast:
1389 assert(getType()->isObjCObjectPointerType());
1390 assert(getSubExpr()->getType()->isBlockPointerType());
1391 goto CheckNoBasePath;
1392
John McCallc62bb392012-02-15 01:22:51 +00001393 case CK_ReinterpretMemberPointer:
1394 assert(getType()->isMemberPointerType());
1395 assert(getSubExpr()->getType()->isMemberPointerType());
1396 goto CheckNoBasePath;
1397
John McCall9320b872011-09-09 05:25:32 +00001398 case CK_BitCast:
1399 // Arbitrary casts to C pointer types count as bitcasts.
1400 // Otherwise, we should only have block and ObjC pointer casts
1401 // here if they stay within the type kind.
1402 if (!getType()->isPointerType()) {
1403 assert(getType()->isObjCObjectPointerType() ==
1404 getSubExpr()->getType()->isObjCObjectPointerType());
1405 assert(getType()->isBlockPointerType() ==
1406 getSubExpr()->getType()->isBlockPointerType());
1407 }
1408 goto CheckNoBasePath;
1409
1410 case CK_AnyPointerToBlockPointerCast:
1411 assert(getType()->isBlockPointerType());
1412 assert(getSubExpr()->getType()->isAnyPointerType() &&
1413 !getSubExpr()->getType()->isBlockPointerType());
1414 goto CheckNoBasePath;
1415
Douglas Gregored90df32012-02-22 05:02:47 +00001416 case CK_CopyAndAutoreleaseBlockObject:
1417 assert(getType()->isBlockPointerType());
1418 assert(getSubExpr()->getType()->isBlockPointerType());
1419 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001420
1421 case CK_FunctionToPointerDecay:
1422 assert(getType()->isPointerType());
1423 assert(getSubExpr()->getType()->isFunctionType());
1424 goto CheckNoBasePath;
1425
John McCall9320b872011-09-09 05:25:32 +00001426 // These should not have an inheritance path.
1427 case CK_Dynamic:
1428 case CK_ToUnion:
1429 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001430 case CK_NullToMemberPointer:
1431 case CK_NullToPointer:
1432 case CK_ConstructorConversion:
1433 case CK_IntegralToPointer:
1434 case CK_PointerToIntegral:
1435 case CK_ToVoid:
1436 case CK_VectorSplat:
1437 case CK_IntegralCast:
1438 case CK_IntegralToFloating:
1439 case CK_FloatingToIntegral:
1440 case CK_FloatingCast:
1441 case CK_ObjCObjectLValueCast:
1442 case CK_FloatingRealToComplex:
1443 case CK_FloatingComplexToReal:
1444 case CK_FloatingComplexCast:
1445 case CK_FloatingComplexToIntegralComplex:
1446 case CK_IntegralRealToComplex:
1447 case CK_IntegralComplexToReal:
1448 case CK_IntegralComplexCast:
1449 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001450 case CK_ARCProduceObject:
1451 case CK_ARCConsumeObject:
1452 case CK_ARCReclaimReturnedObject:
1453 case CK_ARCExtendBlockObject:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001454 case CK_ZeroToOCLEvent:
John McCall9320b872011-09-09 05:25:32 +00001455 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1456 goto CheckNoBasePath;
1457
1458 case CK_Dependent:
1459 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001460 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001461 case CK_AtomicToNonAtomic:
1462 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001463 case CK_PointerToBoolean:
1464 case CK_IntegralToBoolean:
1465 case CK_FloatingToBoolean:
1466 case CK_MemberPointerToBoolean:
1467 case CK_FloatingComplexToBoolean:
1468 case CK_IntegralComplexToBoolean:
1469 case CK_LValueBitCast: // -> bool&
1470 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001471 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001472 CheckNoBasePath:
1473 assert(path_empty() && "Cast kind should not have a base path!");
1474 break;
1475 }
1476}
1477
Anders Carlsson496335e2009-09-03 00:59:21 +00001478const char *CastExpr::getCastKindName() const {
1479 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001480 case CK_Dependent:
1481 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001482 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001483 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001484 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001485 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001486 case CK_LValueToRValue:
1487 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001488 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001489 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001490 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001491 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001492 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001493 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001494 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001495 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001496 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001497 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001498 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001499 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001500 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001501 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001502 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001503 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001504 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001505 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001506 case CK_NullToPointer:
1507 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001508 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001509 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001510 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001511 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001512 case CK_ReinterpretMemberPointer:
1513 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001514 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001515 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001516 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001517 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001518 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001519 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001520 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001521 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001522 case CK_PointerToBoolean:
1523 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001524 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001525 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001526 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001527 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001528 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001529 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001530 case CK_IntegralToBoolean:
1531 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001532 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001533 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001534 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001535 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001536 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001537 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001538 case CK_FloatingToBoolean:
1539 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001540 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001541 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001542 case CK_CPointerToObjCPointerCast:
1543 return "CPointerToObjCPointerCast";
1544 case CK_BlockPointerToObjCPointerCast:
1545 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001546 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001547 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001548 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001549 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001550 case CK_FloatingRealToComplex:
1551 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001552 case CK_FloatingComplexToReal:
1553 return "FloatingComplexToReal";
1554 case CK_FloatingComplexToBoolean:
1555 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001556 case CK_FloatingComplexCast:
1557 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001558 case CK_FloatingComplexToIntegralComplex:
1559 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001560 case CK_IntegralRealToComplex:
1561 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001562 case CK_IntegralComplexToReal:
1563 return "IntegralComplexToReal";
1564 case CK_IntegralComplexToBoolean:
1565 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001566 case CK_IntegralComplexCast:
1567 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001568 case CK_IntegralComplexToFloatingComplex:
1569 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001570 case CK_ARCConsumeObject:
1571 return "ARCConsumeObject";
1572 case CK_ARCProduceObject:
1573 return "ARCProduceObject";
1574 case CK_ARCReclaimReturnedObject:
1575 return "ARCReclaimReturnedObject";
1576 case CK_ARCExtendBlockObject:
1577 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001578 case CK_AtomicToNonAtomic:
1579 return "AtomicToNonAtomic";
1580 case CK_NonAtomicToAtomic:
1581 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001582 case CK_CopyAndAutoreleaseBlockObject:
1583 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001584 case CK_BuiltinFnToFnPtr:
1585 return "BuiltinFnToFnPtr";
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00001586 case CK_ZeroToOCLEvent:
1587 return "ZeroToOCLEvent";
Anders Carlsson496335e2009-09-03 00:59:21 +00001588 }
Mike Stump11289f42009-09-09 15:08:12 +00001589
John McCallc5e62b42010-11-13 09:02:35 +00001590 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001591}
1592
Douglas Gregord196a582009-12-14 19:27:10 +00001593Expr *CastExpr::getSubExprAsWritten() {
1594 Expr *SubExpr = 0;
1595 CastExpr *E = this;
1596 do {
1597 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001598
1599 // Skip through reference binding to temporary.
1600 if (MaterializeTemporaryExpr *Materialize
1601 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1602 SubExpr = Materialize->GetTemporaryExpr();
1603
Douglas Gregord196a582009-12-14 19:27:10 +00001604 // Skip any temporary bindings; they're implicit.
1605 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1606 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001607
Douglas Gregord196a582009-12-14 19:27:10 +00001608 // Conversions by constructor and conversion functions have a
1609 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001610 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001611 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001612 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001613 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001614
Douglas Gregord196a582009-12-14 19:27:10 +00001615 // If the subexpression we're left with is an implicit cast, look
1616 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001617 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1618
Douglas Gregord196a582009-12-14 19:27:10 +00001619 return SubExpr;
1620}
1621
John McCallcf142162010-08-07 06:22:56 +00001622CXXBaseSpecifier **CastExpr::path_buffer() {
1623 switch (getStmtClass()) {
1624#define ABSTRACT_STMT(x)
1625#define CASTEXPR(Type, Base) \
1626 case Stmt::Type##Class: \
1627 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1628#define STMT(Type, Base)
1629#include "clang/AST/StmtNodes.inc"
1630 default:
1631 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001632 }
1633}
1634
1635void CastExpr::setCastPath(const CXXCastPath &Path) {
1636 assert(Path.size() == path_size());
1637 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1638}
1639
1640ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1641 CastKind Kind, Expr *Operand,
1642 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001643 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001644 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1645 void *Buffer =
1646 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1647 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001648 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001649 if (PathSize) E->setCastPath(*BasePath);
1650 return E;
1651}
1652
1653ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1654 unsigned PathSize) {
1655 void *Buffer =
1656 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1657 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1658}
1659
1660
1661CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001662 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001663 const CXXCastPath *BasePath,
1664 TypeSourceInfo *WrittenTy,
1665 SourceLocation L, SourceLocation R) {
1666 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1667 void *Buffer =
1668 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1669 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001670 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001671 if (PathSize) E->setCastPath(*BasePath);
1672 return E;
1673}
1674
1675CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1676 void *Buffer =
1677 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1678 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1679}
1680
Chris Lattner1b926492006-08-23 06:42:10 +00001681/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1682/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001683StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001684 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001685 case BO_PtrMemD: return ".*";
1686 case BO_PtrMemI: return "->*";
1687 case BO_Mul: return "*";
1688 case BO_Div: return "/";
1689 case BO_Rem: return "%";
1690 case BO_Add: return "+";
1691 case BO_Sub: return "-";
1692 case BO_Shl: return "<<";
1693 case BO_Shr: return ">>";
1694 case BO_LT: return "<";
1695 case BO_GT: return ">";
1696 case BO_LE: return "<=";
1697 case BO_GE: return ">=";
1698 case BO_EQ: return "==";
1699 case BO_NE: return "!=";
1700 case BO_And: return "&";
1701 case BO_Xor: return "^";
1702 case BO_Or: return "|";
1703 case BO_LAnd: return "&&";
1704 case BO_LOr: return "||";
1705 case BO_Assign: return "=";
1706 case BO_MulAssign: return "*=";
1707 case BO_DivAssign: return "/=";
1708 case BO_RemAssign: return "%=";
1709 case BO_AddAssign: return "+=";
1710 case BO_SubAssign: return "-=";
1711 case BO_ShlAssign: return "<<=";
1712 case BO_ShrAssign: return ">>=";
1713 case BO_AndAssign: return "&=";
1714 case BO_XorAssign: return "^=";
1715 case BO_OrAssign: return "|=";
1716 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001717 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001718
David Blaikiee4d798f2012-01-20 21:50:17 +00001719 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001720}
Steve Naroff47500512007-04-19 23:00:49 +00001721
John McCalle3027922010-08-25 11:45:40 +00001722BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001723BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1724 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001725 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001726 case OO_Plus: return BO_Add;
1727 case OO_Minus: return BO_Sub;
1728 case OO_Star: return BO_Mul;
1729 case OO_Slash: return BO_Div;
1730 case OO_Percent: return BO_Rem;
1731 case OO_Caret: return BO_Xor;
1732 case OO_Amp: return BO_And;
1733 case OO_Pipe: return BO_Or;
1734 case OO_Equal: return BO_Assign;
1735 case OO_Less: return BO_LT;
1736 case OO_Greater: return BO_GT;
1737 case OO_PlusEqual: return BO_AddAssign;
1738 case OO_MinusEqual: return BO_SubAssign;
1739 case OO_StarEqual: return BO_MulAssign;
1740 case OO_SlashEqual: return BO_DivAssign;
1741 case OO_PercentEqual: return BO_RemAssign;
1742 case OO_CaretEqual: return BO_XorAssign;
1743 case OO_AmpEqual: return BO_AndAssign;
1744 case OO_PipeEqual: return BO_OrAssign;
1745 case OO_LessLess: return BO_Shl;
1746 case OO_GreaterGreater: return BO_Shr;
1747 case OO_LessLessEqual: return BO_ShlAssign;
1748 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1749 case OO_EqualEqual: return BO_EQ;
1750 case OO_ExclaimEqual: return BO_NE;
1751 case OO_LessEqual: return BO_LE;
1752 case OO_GreaterEqual: return BO_GE;
1753 case OO_AmpAmp: return BO_LAnd;
1754 case OO_PipePipe: return BO_LOr;
1755 case OO_Comma: return BO_Comma;
1756 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001757 }
1758}
1759
1760OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1761 static const OverloadedOperatorKind OverOps[] = {
1762 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1763 OO_Star, OO_Slash, OO_Percent,
1764 OO_Plus, OO_Minus,
1765 OO_LessLess, OO_GreaterGreater,
1766 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1767 OO_EqualEqual, OO_ExclaimEqual,
1768 OO_Amp,
1769 OO_Caret,
1770 OO_Pipe,
1771 OO_AmpAmp,
1772 OO_PipePipe,
1773 OO_Equal, OO_StarEqual,
1774 OO_SlashEqual, OO_PercentEqual,
1775 OO_PlusEqual, OO_MinusEqual,
1776 OO_LessLessEqual, OO_GreaterGreaterEqual,
1777 OO_AmpEqual, OO_CaretEqual,
1778 OO_PipeEqual,
1779 OO_Comma
1780 };
1781 return OverOps[Opc];
1782}
1783
Ted Kremenekac034612010-04-13 23:39:13 +00001784InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001785 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001786 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001787 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001788 InitExprs(C, initExprs.size()),
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001789 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redlc83ed822012-02-17 08:42:25 +00001790{
1791 sawArrayRangeDesignator(false);
1792 setInitializesStdInitializerList(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001793 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001794 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001795 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001796 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001797 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001798 if (initExprs[I]->isInstantiationDependent())
1799 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001800 if (initExprs[I]->containsUnexpandedParameterPack())
1801 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001802 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001803
Benjamin Kramerc215e762012-08-24 11:54:20 +00001804 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001805}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001806
Ted Kremenekac034612010-04-13 23:39:13 +00001807void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001808 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001809 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001810}
1811
Ted Kremenekac034612010-04-13 23:39:13 +00001812void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001813 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001814}
1815
Ted Kremenekac034612010-04-13 23:39:13 +00001816Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001817 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001818 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001819 InitExprs.back() = expr;
1820 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001823 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1824 InitExprs[Init] = expr;
1825 return Result;
1826}
1827
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001828void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001829 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001830 ArrayFillerOrUnionFieldInit = filler;
1831 // Fill out any "holes" in the array due to designated initializers.
1832 Expr **inits = getInits();
1833 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1834 if (inits[i] == 0)
1835 inits[i] = filler;
1836}
1837
Richard Smith9ec1e482012-04-15 02:50:59 +00001838bool InitListExpr::isStringLiteralInit() const {
1839 if (getNumInits() != 1)
1840 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001841 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1842 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001843 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001844 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001845 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1846}
1847
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001848SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara8d16bd42012-11-08 18:41:43 +00001849 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001850 return SyntacticForm->getLocStart();
1851 SourceLocation Beg = LBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001852 if (Beg.isInvalid()) {
1853 // Find the first non-null initializer.
1854 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1855 E = InitExprs.end();
1856 I != E; ++I) {
1857 if (Stmt *S = *I) {
1858 Beg = S->getLocStart();
1859 break;
1860 }
1861 }
1862 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001863 return Beg;
1864}
1865
1866SourceLocation InitListExpr::getLocEnd() const {
1867 if (InitListExpr *SyntacticForm = getSyntacticForm())
1868 return SyntacticForm->getLocEnd();
1869 SourceLocation End = RBraceLoc;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001870 if (End.isInvalid()) {
1871 // Find the first non-null initializer from the end.
1872 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001873 E = InitExprs.rend();
1874 I != E; ++I) {
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001875 if (Stmt *S = *I) {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001876 End = S->getLocEnd();
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001877 break;
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001878 }
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001879 }
1880 }
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001881 return End;
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001882}
1883
Steve Naroff991e99d2008-09-04 15:31:07 +00001884/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001885///
John McCallc833dea2012-02-17 03:32:35 +00001886const FunctionProtoType *BlockExpr::getFunctionType() const {
1887 // The block pointer is never sugared, but the function type might be.
1888 return cast<BlockPointerType>(getType())
1889 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001890}
1891
Mike Stump11289f42009-09-09 15:08:12 +00001892SourceLocation BlockExpr::getCaretLocation() const {
1893 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001894}
Mike Stump11289f42009-09-09 15:08:12 +00001895const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001896 return TheBlock->getBody();
1897}
Mike Stump11289f42009-09-09 15:08:12 +00001898Stmt *BlockExpr::getBody() {
1899 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001900}
Steve Naroff415d3d52008-10-08 17:01:13 +00001901
1902
Chris Lattner1ec5f562007-06-27 05:38:08 +00001903//===----------------------------------------------------------------------===//
1904// Generic Expression Routines
1905//===----------------------------------------------------------------------===//
1906
Chris Lattner237f2752009-02-14 07:37:35 +00001907/// isUnusedResultAWarning - Return true if this immediate expression should
1908/// be warned about if the result is unused. If so, fill in Loc and Ranges
1909/// with location to warn on and the source range[s] to report with the
1910/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001911bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1912 SourceRange &R1, SourceRange &R2,
1913 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001914 // Don't warn if the expr is type dependent. The type could end up
1915 // instantiating to void.
1916 if (isTypeDependent())
1917 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001918
Chris Lattner1ec5f562007-06-27 05:38:08 +00001919 switch (getStmtClass()) {
1920 default:
John McCallc493a732010-03-12 07:11:26 +00001921 if (getType()->isVoidType())
1922 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001923 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001924 Loc = getExprLoc();
1925 R1 = getSourceRange();
1926 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001927 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001928 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001929 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001930 case GenericSelectionExprClass:
1931 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001932 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001933 case UnaryOperatorClass: {
1934 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001935
Chris Lattner1ec5f562007-06-27 05:38:08 +00001936 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001937 case UO_Plus:
1938 case UO_Minus:
1939 case UO_AddrOf:
1940 case UO_Not:
1941 case UO_LNot:
1942 case UO_Deref:
1943 break;
John McCalle3027922010-08-25 11:45:40 +00001944 case UO_PostInc:
1945 case UO_PostDec:
1946 case UO_PreInc:
1947 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001948 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001949 case UO_Real:
1950 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001951 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001952 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1953 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001954 return false;
1955 break;
John McCalle3027922010-08-25 11:45:40 +00001956 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001957 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001958 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00001959 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001960 Loc = UO->getOperatorLoc();
1961 R1 = UO->getSubExpr()->getSourceRange();
1962 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001963 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001964 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001965 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001966 switch (BO->getOpcode()) {
1967 default:
1968 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001969 // Consider the RHS of comma for side effects. LHS was checked by
1970 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001971 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001972 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1973 // lvalue-ness) of an assignment written in a macro.
1974 if (IntegerLiteral *IE =
1975 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1976 if (IE->getValue() == 0)
1977 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001978 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001979 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001980 case BO_LAnd:
1981 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001982 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1983 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001984 return false;
1985 break;
John McCall1e3715a2010-02-16 04:10:53 +00001986 }
Chris Lattner237f2752009-02-14 07:37:35 +00001987 if (BO->isAssignmentOp())
1988 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001989 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001990 Loc = BO->getOperatorLoc();
1991 R1 = BO->getLHS()->getSourceRange();
1992 R2 = BO->getRHS()->getSourceRange();
1993 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001994 }
Chris Lattner86928112007-08-25 02:00:02 +00001995 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001996 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001997 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001998 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001999
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002000 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00002001 // If only one of the LHS or RHS is a warning, the operator might
2002 // be being used for control flow. Only warn if both the LHS and
2003 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002004 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002005 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00002006 return false;
2007 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00002008 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002009 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00002010 }
2011
Chris Lattnera44d1162007-06-27 05:58:59 +00002012 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002013 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002014 Loc = cast<MemberExpr>(this)->getMemberLoc();
2015 R1 = SourceRange(Loc, Loc);
2016 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
2017 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002018
Chris Lattner1ec5f562007-06-27 05:38:08 +00002019 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002020 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002021 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
2022 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
2023 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
2024 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00002025
Chandler Carruth46339472011-08-17 09:49:44 +00002026 case CXXOperatorCallExprClass: {
2027 // We warn about operator== and operator!= even when user-defined operator
2028 // overloads as there is no reasonable way to define these such that they
2029 // have non-trivial, desirable side-effects. See the -Wunused-comparison
2030 // warning: these operators are commonly typo'ed, and so warning on them
2031 // provides additional value as well. If this list is updated,
2032 // DiagnoseUnusedComparison should be as well.
2033 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
2034 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002035 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002036 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002037 Loc = Op->getOperatorLoc();
2038 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00002039 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00002040 }
Chandler Carruth46339472011-08-17 09:49:44 +00002041
2042 // Fallthrough for generic call handling.
2043 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002044 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002045 case CXXMemberCallExprClass:
2046 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002047 // If this is a direct call, get the callee.
2048 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002049 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002050 // If the callee has attribute pure, const, or warn_unused_result, warn
2051 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002052 //
2053 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2054 // updated to match for QoI.
2055 if (FD->getAttr<WarnUnusedResultAttr>() ||
2056 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002057 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002058 Loc = CE->getCallee()->getLocStart();
2059 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002060
Chris Lattner1a6babf2009-10-13 04:53:48 +00002061 if (unsigned NumArgs = CE->getNumArgs())
2062 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2063 CE->getArg(NumArgs-1)->getLocEnd());
2064 return true;
2065 }
Chris Lattner237f2752009-02-14 07:37:35 +00002066 }
2067 return false;
2068 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002069
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002070 // If we don't know precisely what we're looking at, let's not warn.
2071 case UnresolvedLookupExprClass:
2072 case CXXUnresolvedConstructExprClass:
2073 return false;
2074
Anders Carlsson6aa50392009-11-17 17:11:23 +00002075 case CXXTemporaryObjectExprClass:
2076 case CXXConstructExprClass:
2077 return false;
2078
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002079 case ObjCMessageExprClass: {
2080 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002081 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002082 ME->isInstanceMessage() &&
2083 !ME->getType()->isVoidType() &&
2084 ME->getSelector().getIdentifierInfoForSlot(0) &&
2085 ME->getSelector().getIdentifierInfoForSlot(0)
2086 ->getName().startswith("init")) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002087 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002088 Loc = getExprLoc();
2089 R1 = ME->getSourceRange();
2090 return true;
2091 }
2092
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002093 const ObjCMethodDecl *MD = ME->getMethodDecl();
2094 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002095 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002096 Loc = getExprLoc();
2097 return true;
2098 }
Chris Lattner237f2752009-02-14 07:37:35 +00002099 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002100 }
Mike Stump11289f42009-09-09 15:08:12 +00002101
John McCallb7bd14f2010-12-02 01:19:52 +00002102 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002103 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002104 Loc = getExprLoc();
2105 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002106 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002107
John McCallfe96e0b2011-11-06 09:01:30 +00002108 case PseudoObjectExprClass: {
2109 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2110
2111 // Only complain about things that have the form of a getter.
2112 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2113 isa<BinaryOperator>(PO->getSyntacticForm()))
2114 return false;
2115
Eli Friedmanc11535c2012-05-24 00:47:05 +00002116 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002117 Loc = getExprLoc();
2118 R1 = getSourceRange();
2119 return true;
2120 }
2121
Chris Lattner944d3062008-07-26 19:51:01 +00002122 case StmtExprClass: {
2123 // Statement exprs don't logically have side effects themselves, but are
2124 // sometimes used in macros in ways that give them a type that is unused.
2125 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2126 // however, if the result of the stmt expr is dead, we don't want to emit a
2127 // warning.
2128 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002129 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002130 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002131 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002132 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2133 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002134 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002135 }
Mike Stump11289f42009-09-09 15:08:12 +00002136
John McCallc493a732010-03-12 07:11:26 +00002137 if (getType()->isVoidType())
2138 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002139 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002140 Loc = cast<StmtExpr>(this)->getLParenLoc();
2141 R1 = getSourceRange();
2142 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002143 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002144 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002145 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002146 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002147 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002148 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002149 if (CE->getCastKind() == CK_ToVoid) {
2150 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002151 CE->getSubExpr()->getType().isVolatileQualified()) {
2152 const DeclRefExpr *DRE =
2153 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2154 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2155 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2156 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2157 R1, R2, Ctx);
2158 }
2159 }
Chris Lattner2706a552009-07-28 18:25:28 +00002160 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002161 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002162
Eli Friedmanc11535c2012-05-24 00:47:05 +00002163 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002164 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002165 if (CE->getCastKind() == CK_ConstructorConversion)
2166 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002167
Eli Friedmanc11535c2012-05-24 00:47:05 +00002168 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002169 if (const CXXFunctionalCastExpr *CXXCE =
2170 dyn_cast<CXXFunctionalCastExpr>(this)) {
2171 Loc = CXXCE->getTypeBeginLoc();
2172 R1 = CXXCE->getSubExpr()->getSourceRange();
2173 } else {
2174 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2175 Loc = CStyleCE->getLParenLoc();
2176 R1 = CStyleCE->getSubExpr()->getSourceRange();
2177 }
Chris Lattner237f2752009-02-14 07:37:35 +00002178 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002179 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002180 case ImplicitCastExprClass: {
2181 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002182
Eli Friedmanc11535c2012-05-24 00:47:05 +00002183 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2184 if (ICE->getCastKind() == CK_LValueToRValue &&
2185 ICE->getSubExpr()->getType().isVolatileQualified())
2186 return false;
2187
2188 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2189 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002190 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002191 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002192 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Richard Smith852c9db2013-04-20 22:23:05 +00002193 case CXXDefaultInitExprClass:
2194 return (cast<CXXDefaultInitExpr>(this)
2195 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002196
2197 case CXXNewExprClass:
2198 // FIXME: In theory, there might be new expressions that don't have side
2199 // effects (e.g. a placement new with an uninitialized POD).
2200 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002201 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002202 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002203 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002204 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002205 case ExprWithCleanupsClass:
2206 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002207 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002208 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002209}
2210
Fariborz Jahanian07735332009-02-22 18:40:18 +00002211/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002212/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002213bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002214 const Expr *E = IgnoreParens();
2215 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002216 default:
2217 return false;
2218 case ObjCIvarRefExprClass:
2219 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002220 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002221 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002222 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002223 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002224 case MaterializeTemporaryExprClass:
2225 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2226 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002227 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002228 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002229 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002230 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002231
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002232 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2233 if (VD->hasGlobalStorage())
2234 return true;
2235 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002236 // dereferencing to a pointer is always a gc'able candidate,
2237 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002238 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002239 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002240 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002241 return false;
2242 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002243 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002244 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002245 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002246 }
2247 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002248 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002249 }
2250}
Sebastian Redlce354af2010-09-10 20:55:33 +00002251
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002252bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2253 if (isTypeDependent())
2254 return false;
John McCall086a4642010-11-24 05:12:34 +00002255 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002256}
2257
John McCall0009fcc2011-04-26 20:42:42 +00002258QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002259 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002260
2261 // Bound member expressions are always one of these possibilities:
2262 // x->m x.m x->*y x.*y
2263 // (possibly parenthesized)
2264
2265 expr = expr->IgnoreParens();
2266 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2267 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2268 return mem->getMemberDecl()->getType();
2269 }
2270
2271 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2272 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2273 ->getPointeeType();
2274 assert(type->isFunctionType());
2275 return type;
2276 }
2277
2278 assert(isa<UnresolvedMemberExpr>(expr));
2279 return QualType();
2280}
2281
Ted Kremenekfff70962008-01-17 16:57:34 +00002282Expr* Expr::IgnoreParens() {
2283 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002284 while (true) {
2285 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2286 E = P->getSubExpr();
2287 continue;
2288 }
2289 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2290 if (P->getOpcode() == UO_Extension) {
2291 E = P->getSubExpr();
2292 continue;
2293 }
2294 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002295 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2296 if (!P->isResultDependent()) {
2297 E = P->getResultExpr();
2298 continue;
2299 }
2300 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002301 return E;
2302 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002303}
2304
Chris Lattnerf2660962008-02-13 01:02:39 +00002305/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2306/// or CastExprs or ImplicitCastExprs, returning their operand.
2307Expr *Expr::IgnoreParenCasts() {
2308 Expr *E = this;
2309 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002310 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002311 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002312 continue;
2313 }
2314 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002315 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002316 continue;
2317 }
2318 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2319 if (P->getOpcode() == UO_Extension) {
2320 E = P->getSubExpr();
2321 continue;
2322 }
2323 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002324 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2325 if (!P->isResultDependent()) {
2326 E = P->getResultExpr();
2327 continue;
2328 }
2329 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002330 if (MaterializeTemporaryExpr *Materialize
2331 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2332 E = Materialize->GetTemporaryExpr();
2333 continue;
2334 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002335 if (SubstNonTypeTemplateParmExpr *NTTP
2336 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2337 E = NTTP->getReplacement();
2338 continue;
2339 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002340 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002341 }
2342}
2343
John McCall5a4ce8b2010-12-04 08:24:19 +00002344/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2345/// casts. This is intended purely as a temporary workaround for code
2346/// that hasn't yet been rewritten to do the right thing about those
2347/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002348Expr *Expr::IgnoreParenLValueCasts() {
2349 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002350 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002351 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2352 E = P->getSubExpr();
2353 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002354 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002355 if (P->getCastKind() == CK_LValueToRValue) {
2356 E = P->getSubExpr();
2357 continue;
2358 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002359 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2360 if (P->getOpcode() == UO_Extension) {
2361 E = P->getSubExpr();
2362 continue;
2363 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002364 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2365 if (!P->isResultDependent()) {
2366 E = P->getResultExpr();
2367 continue;
2368 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002369 } else if (MaterializeTemporaryExpr *Materialize
2370 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2371 E = Materialize->GetTemporaryExpr();
2372 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002373 } else if (SubstNonTypeTemplateParmExpr *NTTP
2374 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2375 E = NTTP->getReplacement();
2376 continue;
John McCall34376a62010-12-04 03:47:34 +00002377 }
2378 break;
2379 }
2380 return E;
2381}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002382
2383Expr *Expr::ignoreParenBaseCasts() {
2384 Expr *E = this;
2385 while (true) {
2386 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2387 E = P->getSubExpr();
2388 continue;
2389 }
2390 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2391 if (CE->getCastKind() == CK_DerivedToBase ||
2392 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2393 CE->getCastKind() == CK_NoOp) {
2394 E = CE->getSubExpr();
2395 continue;
2396 }
2397 }
2398
2399 return E;
2400 }
2401}
2402
John McCalleebc8322010-05-05 22:59:52 +00002403Expr *Expr::IgnoreParenImpCasts() {
2404 Expr *E = this;
2405 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002406 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002407 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002408 continue;
2409 }
2410 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002411 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002412 continue;
2413 }
2414 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2415 if (P->getOpcode() == UO_Extension) {
2416 E = P->getSubExpr();
2417 continue;
2418 }
2419 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002420 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2421 if (!P->isResultDependent()) {
2422 E = P->getResultExpr();
2423 continue;
2424 }
2425 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002426 if (MaterializeTemporaryExpr *Materialize
2427 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2428 E = Materialize->GetTemporaryExpr();
2429 continue;
2430 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002431 if (SubstNonTypeTemplateParmExpr *NTTP
2432 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2433 E = NTTP->getReplacement();
2434 continue;
2435 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002436 return E;
John McCalleebc8322010-05-05 22:59:52 +00002437 }
2438}
2439
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002440Expr *Expr::IgnoreConversionOperator() {
2441 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002442 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002443 return MCE->getImplicitObjectArgument();
2444 }
2445 return this;
2446}
2447
Chris Lattneref26c772009-03-13 17:28:01 +00002448/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2449/// value (including ptr->int casts of the same size). Strip off any
2450/// ParenExpr or CastExprs, returning their operand.
2451Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2452 Expr *E = this;
2453 while (true) {
2454 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2455 E = P->getSubExpr();
2456 continue;
2457 }
Mike Stump11289f42009-09-09 15:08:12 +00002458
Chris Lattneref26c772009-03-13 17:28:01 +00002459 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2460 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002461 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002462 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002463
Chris Lattneref26c772009-03-13 17:28:01 +00002464 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2465 E = SE;
2466 continue;
2467 }
Mike Stump11289f42009-09-09 15:08:12 +00002468
Abramo Bagnara932e3932010-10-15 07:51:18 +00002469 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002470 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002471 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002472 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002473 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2474 E = SE;
2475 continue;
2476 }
2477 }
Mike Stump11289f42009-09-09 15:08:12 +00002478
Abramo Bagnara932e3932010-10-15 07:51:18 +00002479 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2480 if (P->getOpcode() == UO_Extension) {
2481 E = P->getSubExpr();
2482 continue;
2483 }
2484 }
2485
Peter Collingbourne91147592011-04-15 00:35:48 +00002486 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2487 if (!P->isResultDependent()) {
2488 E = P->getResultExpr();
2489 continue;
2490 }
2491 }
2492
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
2638 // which can be evaluated at compile-time. isEvaluatable handles most
2639 // of the cases, but it can't deal with some initializer-specific
2640 // expressions, and it can't deal with aggregates; we deal with those here,
2641 // and fall back to isEvaluatable for the other cases.
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;
Richard Smith941aae02011-12-09 06:47:34 +00002653 case IntegerLiteralClass:
2654 case FloatingLiteralClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002655 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002656 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002657 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002658 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002659 case CXXTemporaryObjectExprClass:
2660 case CXXConstructExprClass: {
2661 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002662
2663 // Only if it's
Richard Smithd62306a2011-11-10 06:34:14 +00002664 if (CE->getConstructor()->isTrivial()) {
2665 // 1) an application of the trivial default constructor or
2666 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002667
Richard Smithd62306a2011-11-10 06:34:14 +00002668 // 2) an elidable trivial copy construction of an operand which is
2669 // itself a constant initializer. Note that we consider the
2670 // operand on its own, *not* as a reference binding.
2671 if (CE->isElidable() &&
2672 CE->getArg(0)->isConstantInitializer(Ctx, false))
2673 return true;
2674 }
2675
2676 // 3) a foldable constexpr constructor.
2677 break;
John McCall81c9cea2010-08-01 21:51:45 +00002678 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002679 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002680 // This handles gcc's extension that allows global initializers like
2681 // "struct x {int x;} x = (struct x) {};".
2682 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002683 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002684 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002685 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002686 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002687 // FIXME: This doesn't deal with fields with reference types correctly.
2688 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2689 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002690 const InitListExpr *Exp = cast<InitListExpr>(this);
2691 unsigned numInits = Exp->getNumInits();
2692 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002693 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002694 return false;
2695 }
Eli Friedman384da272009-01-25 03:12:18 +00002696 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002697 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002698 case ImplicitValueInitExprClass:
2699 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002700 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002701 return cast<ParenExpr>(this)->getSubExpr()
2702 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002703 case GenericSelectionExprClass:
2704 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2705 return false;
2706 return cast<GenericSelectionExpr>(this)->getResultExpr()
2707 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002708 case ChooseExprClass:
2709 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2710 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002711 case UnaryOperatorClass: {
2712 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002713 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002714 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002715 break;
2716 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002717 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002718 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002719 case ImplicitCastExprClass:
Richard Smith161f09a2011-12-06 22:44:34 +00002720 case CStyleCastExprClass: {
2721 const CastExpr *CE = cast<CastExpr>(this);
2722
David Chisnallfa35df62012-01-16 17:27:18 +00002723 // If we're promoting an integer to an _Atomic type then this is constant
2724 // if the integer is constant. We also need to check the converse in case
2725 // someone does something like:
2726 //
2727 // int a = (_Atomic(int))42;
2728 //
2729 // I doubt anyone would write code like this directly, but it's quite
2730 // possible as the result of macro expansions.
2731 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2732 CE->getCastKind() == CK_AtomicToNonAtomic)
2733 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2734
Richard Smith161f09a2011-12-06 22:44:34 +00002735 // Handle bitcasts of vector constants.
2736 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2737 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2738
Eli Friedman13ec75b2011-12-21 00:43:02 +00002739 // Handle misc casts we want to ignore.
2740 // FIXME: Is it really safe to ignore all these?
2741 if (CE->getCastKind() == CK_NoOp ||
2742 CE->getCastKind() == CK_LValueToRValue ||
2743 CE->getCastKind() == CK_ToUnion ||
2744 CE->getCastKind() == CK_ConstructorConversion)
Richard Smith161f09a2011-12-06 22:44:34 +00002745 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2746
Eli Friedman384da272009-01-25 03:12:18 +00002747 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002748 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002749 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002750 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002751 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002752 }
Eli Friedman384da272009-01-25 03:12:18 +00002753 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002754}
2755
Richard Smith0421ce72012-08-07 04:16:51 +00002756bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2757 if (isInstantiationDependent())
2758 return true;
2759
2760 switch (getStmtClass()) {
2761 case NoStmtClass:
2762 #define ABSTRACT_STMT(Type)
2763 #define STMT(Type, Base) case Type##Class:
2764 #define EXPR(Type, Base)
2765 #include "clang/AST/StmtNodes.inc"
2766 llvm_unreachable("unexpected Expr kind");
2767
2768 case DependentScopeDeclRefExprClass:
2769 case CXXUnresolvedConstructExprClass:
2770 case CXXDependentScopeMemberExprClass:
2771 case UnresolvedLookupExprClass:
2772 case UnresolvedMemberExprClass:
2773 case PackExpansionExprClass:
2774 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002775 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002776 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2777
Richard Smitha33e4fe2012-08-07 05:18:29 +00002778 case DeclRefExprClass:
2779 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002780 case PredefinedExprClass:
2781 case IntegerLiteralClass:
2782 case FloatingLiteralClass:
2783 case ImaginaryLiteralClass:
2784 case StringLiteralClass:
2785 case CharacterLiteralClass:
2786 case OffsetOfExprClass:
2787 case ImplicitValueInitExprClass:
2788 case UnaryExprOrTypeTraitExprClass:
2789 case AddrLabelExprClass:
2790 case GNUNullExprClass:
2791 case CXXBoolLiteralExprClass:
2792 case CXXNullPtrLiteralExprClass:
2793 case CXXThisExprClass:
2794 case CXXScalarValueInitExprClass:
2795 case TypeTraitExprClass:
2796 case UnaryTypeTraitExprClass:
2797 case BinaryTypeTraitExprClass:
2798 case ArrayTypeTraitExprClass:
2799 case ExpressionTraitExprClass:
2800 case CXXNoexceptExprClass:
2801 case SizeOfPackExprClass:
2802 case ObjCStringLiteralClass:
2803 case ObjCEncodeExprClass:
2804 case ObjCBoolLiteralExprClass:
2805 case CXXUuidofExprClass:
2806 case OpaqueValueExprClass:
2807 // These never have a side-effect.
2808 return false;
2809
2810 case CallExprClass:
John McCall5e77d762013-04-16 07:28:30 +00002811 case MSPropertyRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002812 case CompoundAssignOperatorClass:
2813 case VAArgExprClass:
2814 case AtomicExprClass:
2815 case StmtExprClass:
2816 case CXXOperatorCallExprClass:
2817 case CXXMemberCallExprClass:
2818 case UserDefinedLiteralClass:
2819 case CXXThrowExprClass:
2820 case CXXNewExprClass:
2821 case CXXDeleteExprClass:
2822 case ExprWithCleanupsClass:
2823 case CXXBindTemporaryExprClass:
2824 case BlockExprClass:
2825 case CUDAKernelCallExprClass:
2826 // These always have a side-effect.
2827 return true;
2828
2829 case ParenExprClass:
2830 case ArraySubscriptExprClass:
2831 case MemberExprClass:
2832 case ConditionalOperatorClass:
2833 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002834 case CompoundLiteralExprClass:
2835 case ExtVectorElementExprClass:
2836 case DesignatedInitExprClass:
2837 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002838 case CXXPseudoDestructorExprClass:
2839 case SubstNonTypeTemplateParmExprClass:
2840 case MaterializeTemporaryExprClass:
2841 case ShuffleVectorExprClass:
2842 case AsTypeExprClass:
2843 // These have a side-effect if any subexpression does.
2844 break;
2845
Richard Smitha33e4fe2012-08-07 05:18:29 +00002846 case UnaryOperatorClass:
2847 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002848 return true;
2849 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002850
2851 case BinaryOperatorClass:
2852 if (cast<BinaryOperator>(this)->isAssignmentOp())
2853 return true;
2854 break;
2855
Richard Smith0421ce72012-08-07 04:16:51 +00002856 case InitListExprClass:
2857 // FIXME: The children for an InitListExpr doesn't include the array filler.
2858 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2859 if (E->HasSideEffects(Ctx))
2860 return true;
2861 break;
2862
2863 case GenericSelectionExprClass:
2864 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2865 HasSideEffects(Ctx);
2866
2867 case ChooseExprClass:
2868 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2869
2870 case CXXDefaultArgExprClass:
2871 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2872
Richard Smith852c9db2013-04-20 22:23:05 +00002873 case CXXDefaultInitExprClass:
2874 if (const Expr *E = cast<CXXDefaultInitExpr>(this)->getExpr())
2875 return E->HasSideEffects(Ctx);
2876 // If we've not yet parsed the initializer, assume it has side-effects.
2877 return true;
2878
Richard Smith0421ce72012-08-07 04:16:51 +00002879 case CXXDynamicCastExprClass: {
2880 // A dynamic_cast expression has side-effects if it can throw.
2881 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2882 if (DCE->getTypeAsWritten()->isReferenceType() &&
2883 DCE->getCastKind() == CK_Dynamic)
2884 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002885 } // Fall through.
2886 case ImplicitCastExprClass:
2887 case CStyleCastExprClass:
2888 case CXXStaticCastExprClass:
2889 case CXXReinterpretCastExprClass:
2890 case CXXConstCastExprClass:
2891 case CXXFunctionalCastExprClass: {
2892 const CastExpr *CE = cast<CastExpr>(this);
2893 if (CE->getCastKind() == CK_LValueToRValue &&
2894 CE->getSubExpr()->getType().isVolatileQualified())
2895 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002896 break;
2897 }
2898
Richard Smithef8bf432012-08-13 20:08:14 +00002899 case CXXTypeidExprClass:
2900 // typeid might throw if its subexpression is potentially-evaluated, so has
2901 // side-effects in that case whether or not its subexpression does.
2902 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002903
2904 case CXXConstructExprClass:
2905 case CXXTemporaryObjectExprClass: {
2906 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002907 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002908 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002909 // A trivial constructor does not add any side-effects of its own. Just look
2910 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002911 break;
2912 }
2913
2914 case LambdaExprClass: {
2915 const LambdaExpr *LE = cast<LambdaExpr>(this);
2916 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2917 E = LE->capture_end(); I != E; ++I)
2918 if (I->getCaptureKind() == LCK_ByCopy)
2919 // FIXME: Only has a side-effect if the variable is volatile or if
2920 // the copy would invoke a non-trivial copy constructor.
2921 return true;
2922 return false;
2923 }
2924
2925 case PseudoObjectExprClass: {
2926 // Only look for side-effects in the semantic form, and look past
2927 // OpaqueValueExpr bindings in that form.
2928 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2929 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2930 E = PO->semantics_end();
2931 I != E; ++I) {
2932 const Expr *Subexpr = *I;
2933 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2934 Subexpr = OVE->getSourceExpr();
2935 if (Subexpr->HasSideEffects(Ctx))
2936 return true;
2937 }
2938 return false;
2939 }
2940
2941 case ObjCBoxedExprClass:
2942 case ObjCArrayLiteralClass:
2943 case ObjCDictionaryLiteralClass:
2944 case ObjCMessageExprClass:
2945 case ObjCSelectorExprClass:
2946 case ObjCProtocolExprClass:
2947 case ObjCPropertyRefExprClass:
2948 case ObjCIsaExprClass:
2949 case ObjCIndirectCopyRestoreExprClass:
2950 case ObjCSubscriptRefExprClass:
2951 case ObjCBridgedCastExprClass:
2952 // FIXME: Classify these cases better.
2953 return true;
2954 }
2955
2956 // Recurse to children.
2957 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2958 if (const Stmt *S = *SubStmts)
2959 if (cast<Expr>(S)->HasSideEffects(Ctx))
2960 return true;
2961
2962 return false;
2963}
2964
Douglas Gregor1be329d2012-02-23 07:33:15 +00002965namespace {
2966 /// \brief Look for a call to a non-trivial function within an expression.
2967 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2968 {
2969 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2970
2971 bool NonTrivial;
2972
2973 public:
2974 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002975 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002976
2977 bool hasNonTrivialCall() const { return NonTrivial; }
2978
2979 void VisitCallExpr(CallExpr *E) {
2980 if (CXXMethodDecl *Method
2981 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2982 if (Method->isTrivial()) {
2983 // Recurse to children of the call.
2984 Inherited::VisitStmt(E);
2985 return;
2986 }
2987 }
2988
2989 NonTrivial = true;
2990 }
2991
2992 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2993 if (E->getConstructor()->isTrivial()) {
2994 // Recurse to children of the call.
2995 Inherited::VisitStmt(E);
2996 return;
2997 }
2998
2999 NonTrivial = true;
3000 }
3001
3002 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3003 if (E->getTemporary()->getDestructor()->isTrivial()) {
3004 Inherited::VisitStmt(E);
3005 return;
3006 }
3007
3008 NonTrivial = true;
3009 }
3010 };
3011}
3012
3013bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
3014 NonTrivialCallFinder Finder(Ctx);
3015 Finder.Visit(this);
3016 return Finder.hasNonTrivialCall();
3017}
3018
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003019/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
3020/// pointer constant or not, as well as the specific kind of constant detected.
3021/// Null pointer constants can be integer constant expressions with the
3022/// value zero, casts of zero to void*, nullptr (C++0X), or __null
3023/// (a GNU extension).
3024Expr::NullPointerConstantKind
3025Expr::isNullPointerConstant(ASTContext &Ctx,
3026 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00003027 if (isValueDependent()) {
3028 switch (NPC) {
3029 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00003030 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00003031 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003032 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00003033 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003034 else
3035 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00003036
Douglas Gregor56751b52009-09-25 04:25:58 +00003037 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003038 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00003039 }
3040 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00003041
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003042 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00003043 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003044 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003045 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003046 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003047 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00003048 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003049 Pointee->isVoidType() && // to void*
3050 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003051 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003052 }
Steve Naroffada7d422007-05-20 17:54:12 +00003053 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003054 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3055 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003056 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003057 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3058 // Accept ((void*)0) as a null pointer constant, as many other
3059 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003060 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003061 } else if (const GenericSelectionExpr *GE =
3062 dyn_cast<GenericSelectionExpr>(this)) {
3063 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003064 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003065 = dyn_cast<CXXDefaultArgExpr>(this)) {
Richard Smith852c9db2013-04-20 22:23:05 +00003066 // See through default argument expressions.
Douglas Gregor56751b52009-09-25 04:25:58 +00003067 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Richard Smith852c9db2013-04-20 22:23:05 +00003068 } else if (const CXXDefaultInitExpr *DefaultInit
3069 = dyn_cast<CXXDefaultInitExpr>(this)) {
3070 // See through default initializer expressions.
3071 return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003072 } else if (isa<GNUNullExpr>(this)) {
3073 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003074 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003075 } else if (const MaterializeTemporaryExpr *M
3076 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3077 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003078 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3079 if (const Expr *Source = OVE->getSourceExpr())
3080 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003081 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003082
Richard Smith89645bc2013-01-02 12:01:23 +00003083 // C++11 nullptr_t is always a null pointer constant.
Sebastian Redl576fd422009-05-10 18:38:11 +00003084 if (getType()->isNullPtrType())
Richard Smith89645bc2013-01-02 12:01:23 +00003085 return NPCK_CXX11_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003086
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003087 if (const RecordType *UT = getType()->getAsUnionType())
3088 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3089 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3090 const Expr *InitExpr = CLE->getInitializer();
3091 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3092 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3093 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003094 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003095 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003096 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003097 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003098
Chris Lattner1abbd412007-06-08 17:58:43 +00003099 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith98a0a492012-02-14 21:38:30 +00003100 // test for the value 0. Don't use the C++11 constant expression semantics
3101 // for this, for now; once the dust settles on core issue 903, we might only
3102 // allow a literal 0 here in C++11 mode.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003103 if (Ctx.getLangOpts().CPlusPlus11) {
Richard Smith98a0a492012-02-14 21:38:30 +00003104 if (!isCXX98IntegralConstantExpr(Ctx))
3105 return NPCK_NotNull;
3106 } else {
3107 if (!isIntegerConstantExpr(Ctx))
3108 return NPCK_NotNull;
3109 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003110
David Blaikie1c7c8f72012-08-08 17:33:31 +00003111 if (EvaluateKnownConstInt(Ctx) != 0)
3112 return NPCK_NotNull;
3113
3114 if (isa<IntegerLiteral>(this))
3115 return NPCK_ZeroLiteral;
3116 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003117}
Steve Narofff7a5da12007-07-28 23:10:27 +00003118
John McCall34376a62010-12-04 03:47:34 +00003119/// \brief If this expression is an l-value for an Objective C
3120/// property, find the underlying property reference expression.
3121const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3122 const Expr *E = this;
3123 while (true) {
3124 assert((E->getValueKind() == VK_LValue &&
3125 E->getObjectKind() == OK_ObjCProperty) &&
3126 "expression is not a property reference");
3127 E = E->IgnoreParenCasts();
3128 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3129 if (BO->getOpcode() == BO_Comma) {
3130 E = BO->getRHS();
3131 continue;
3132 }
3133 }
3134
3135 break;
3136 }
3137
3138 return cast<ObjCPropertyRefExpr>(E);
3139}
3140
Anna Zaks97c7ce32012-10-01 20:34:04 +00003141bool Expr::isObjCSelfExpr() const {
3142 const Expr *E = IgnoreParenImpCasts();
3143
3144 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3145 if (!DRE)
3146 return false;
3147
3148 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3149 if (!Param)
3150 return false;
3151
3152 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3153 if (!M)
3154 return false;
3155
3156 return M->getSelfDecl() == Param;
3157}
3158
John McCalld25db7e2013-05-06 21:39:12 +00003159FieldDecl *Expr::getSourceBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003160 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003161
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003162 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003163 if (ICE->getCastKind() == CK_LValueToRValue ||
3164 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003165 E = ICE->getSubExpr()->IgnoreParens();
3166 else
3167 break;
3168 }
3169
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003170 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003171 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003172 if (Field->isBitField())
3173 return Field;
3174
John McCalld25db7e2013-05-06 21:39:12 +00003175 if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E))
3176 if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl()))
3177 if (Ivar->isBitField())
3178 return Ivar;
3179
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003180 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3181 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3182 if (Field->isBitField())
3183 return Field;
3184
Eli Friedman609ada22011-07-13 02:05:57 +00003185 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003186 if (BinOp->isAssignmentOp() && BinOp->getLHS())
John McCalld25db7e2013-05-06 21:39:12 +00003187 return BinOp->getLHS()->getSourceBitField();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003188
Eli Friedman609ada22011-07-13 02:05:57 +00003189 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
John McCalld25db7e2013-05-06 21:39:12 +00003190 return BinOp->getRHS()->getSourceBitField();
Eli Friedman609ada22011-07-13 02:05:57 +00003191 }
3192
Douglas Gregor71235ec2009-05-02 02:18:30 +00003193 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003194}
3195
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003196bool Expr::refersToVectorElement() const {
3197 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003198
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003199 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003200 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003201 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003202 E = ICE->getSubExpr()->IgnoreParens();
3203 else
3204 break;
3205 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003206
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003207 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3208 return ASE->getBase()->getType()->isVectorType();
3209
3210 if (isa<ExtVectorElementExpr>(E))
3211 return true;
3212
3213 return false;
3214}
3215
Chris Lattnerb8211f62009-02-16 22:14:05 +00003216/// isArrow - Return true if the base expression is a pointer to vector,
3217/// return false if the base expression is a vector.
3218bool ExtVectorElementExpr::isArrow() const {
3219 return getBase()->getType()->isPointerType();
3220}
3221
Nate Begemance4d7fc2008-04-18 23:10:10 +00003222unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003223 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003224 return VT->getNumElements();
3225 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003226}
3227
Nate Begemanf322eab2008-05-09 06:41:27 +00003228/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003229bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003230 // FIXME: Refactor this code to an accessor on the AST node which returns the
3231 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003232 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003233
3234 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003235 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003236 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003237
Nate Begeman7e5185b2009-01-18 02:01:21 +00003238 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003239 if (Comp[0] == 's' || Comp[0] == 'S')
3240 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003241
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003242 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003243 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003244 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003245
Steve Naroff0d595ca2007-07-30 03:29:09 +00003246 return false;
3247}
Chris Lattner885b4952007-08-02 23:36:59 +00003248
Nate Begemanf322eab2008-05-09 06:41:27 +00003249/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003250void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003251 SmallVectorImpl<unsigned> &Elts) const {
3252 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003253 if (Comp[0] == 's' || Comp[0] == 'S')
3254 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003255
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003256 bool isHi = Comp == "hi";
3257 bool isLo = Comp == "lo";
3258 bool isEven = Comp == "even";
3259 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003260
Nate Begemanf322eab2008-05-09 06:41:27 +00003261 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3262 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003263
Nate Begemanf322eab2008-05-09 06:41:27 +00003264 if (isHi)
3265 Index = e + i;
3266 else if (isLo)
3267 Index = i;
3268 else if (isEven)
3269 Index = 2 * i;
3270 else if (isOdd)
3271 Index = 2 * i + 1;
3272 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003273 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003274
Nate Begemand3862152008-05-13 21:03:02 +00003275 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003276 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003277}
3278
Douglas Gregor9a129192010-04-21 00:45:42 +00003279ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003280 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003281 SourceLocation LBracLoc,
3282 SourceLocation SuperLoc,
3283 bool IsInstanceSuper,
3284 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003285 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003286 ArrayRef<SourceLocation> SelLocs,
3287 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003288 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003289 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003290 SourceLocation RBracLoc,
3291 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003292 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003293 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003294 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003295 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003296 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3297 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003298 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003299 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3300 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003301{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003302 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003303 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003304}
3305
Douglas Gregor9a129192010-04-21 00:45:42 +00003306ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003307 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003308 SourceLocation LBracLoc,
3309 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003310 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003311 ArrayRef<SourceLocation> SelLocs,
3312 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003313 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003314 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003315 SourceLocation RBracLoc,
3316 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003317 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003318 T->isDependentType(), T->isInstantiationDependentType(),
3319 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003320 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3321 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003322 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003323 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003324 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003325{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003326 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003327 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003328}
3329
Douglas Gregor9a129192010-04-21 00:45:42 +00003330ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003331 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003332 SourceLocation LBracLoc,
3333 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003334 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003335 ArrayRef<SourceLocation> SelLocs,
3336 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003337 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003338 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003339 SourceLocation RBracLoc,
3340 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003341 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003342 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003343 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003344 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003345 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3346 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003347 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003348 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003349 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003350{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003351 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003352 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003353}
3354
3355void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3356 ArrayRef<SourceLocation> SelLocs,
3357 SelectorLocationsKind SelLocsK) {
3358 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003359 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003360 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003361 if (Args[I]->isTypeDependent())
3362 ExprBits.TypeDependent = true;
3363 if (Args[I]->isValueDependent())
3364 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003365 if (Args[I]->isInstantiationDependent())
3366 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003367 if (Args[I]->containsUnexpandedParameterPack())
3368 ExprBits.ContainsUnexpandedParameterPack = true;
3369
3370 MyArgs[I] = Args[I];
3371 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003372
Benjamin Kramer2325b242012-02-20 00:20:48 +00003373 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003374 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003375 if (SelLocsK == SelLoc_NonStandard)
3376 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3377 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003378}
3379
Douglas Gregor9a129192010-04-21 00:45:42 +00003380ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003381 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003382 SourceLocation LBracLoc,
3383 SourceLocation SuperLoc,
3384 bool IsInstanceSuper,
3385 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003386 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003387 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003388 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003389 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003390 SourceLocation RBracLoc,
3391 bool isImplicit) {
3392 assert((!SelLocs.empty() || isImplicit) &&
3393 "No selector locs for non-implicit message");
3394 ObjCMessageExpr *Mem;
3395 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3396 if (isImplicit)
3397 Mem = alloc(Context, Args.size(), 0);
3398 else
3399 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003400 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003401 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003402 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003403}
3404
3405ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003406 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003407 SourceLocation LBracLoc,
3408 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003409 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003410 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003411 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003412 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003413 SourceLocation RBracLoc,
3414 bool isImplicit) {
3415 assert((!SelLocs.empty() || isImplicit) &&
3416 "No selector locs for non-implicit message");
3417 ObjCMessageExpr *Mem;
3418 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3419 if (isImplicit)
3420 Mem = alloc(Context, Args.size(), 0);
3421 else
3422 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003423 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003424 SelLocs, SelLocsK, Method, Args, RBracLoc,
3425 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003426}
3427
3428ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003429 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003430 SourceLocation LBracLoc,
3431 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003432 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003433 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003434 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003435 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003436 SourceLocation RBracLoc,
3437 bool isImplicit) {
3438 assert((!SelLocs.empty() || isImplicit) &&
3439 "No selector locs for non-implicit message");
3440 ObjCMessageExpr *Mem;
3441 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3442 if (isImplicit)
3443 Mem = alloc(Context, Args.size(), 0);
3444 else
3445 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003446 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003447 SelLocs, SelLocsK, Method, Args, RBracLoc,
3448 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003449}
3450
Alexis Hunta8136cc2010-05-05 15:23:54 +00003451ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003452 unsigned NumArgs,
3453 unsigned NumStoredSelLocs) {
3454 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003455 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3456}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003457
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003458ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3459 ArrayRef<Expr *> Args,
3460 SourceLocation RBraceLoc,
3461 ArrayRef<SourceLocation> SelLocs,
3462 Selector Sel,
3463 SelectorLocationsKind &SelLocsK) {
3464 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3465 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3466 : 0;
3467 return alloc(C, Args.size(), NumStoredSelLocs);
3468}
3469
3470ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3471 unsigned NumArgs,
3472 unsigned NumStoredSelLocs) {
3473 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3474 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3475 return (ObjCMessageExpr *)C.Allocate(Size,
3476 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3477}
3478
3479void ObjCMessageExpr::getSelectorLocs(
3480 SmallVectorImpl<SourceLocation> &SelLocs) const {
3481 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3482 SelLocs.push_back(getSelectorLoc(i));
3483}
3484
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003485SourceRange ObjCMessageExpr::getReceiverRange() const {
3486 switch (getReceiverKind()) {
3487 case Instance:
3488 return getInstanceReceiver()->getSourceRange();
3489
3490 case Class:
3491 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3492
3493 case SuperInstance:
3494 case SuperClass:
3495 return getSuperLoc();
3496 }
3497
David Blaikiee4d798f2012-01-20 21:50:17 +00003498 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003499}
3500
Douglas Gregor9a129192010-04-21 00:45:42 +00003501Selector ObjCMessageExpr::getSelector() const {
3502 if (HasMethod)
3503 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3504 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003505 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003506}
3507
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003508QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003509 switch (getReceiverKind()) {
3510 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003511 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003512 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003513 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003514 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003515 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003516 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003517 }
3518
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003519 llvm_unreachable("unexpected receiver kind");
3520}
3521
3522ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3523 QualType T = getReceiverType();
3524
3525 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3526 return Ptr->getInterfaceDecl();
3527
3528 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3529 return Ty->getInterface();
3530
Douglas Gregor9a129192010-04-21 00:45:42 +00003531 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003532}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003533
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003534StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003535 switch (getBridgeKind()) {
3536 case OBC_Bridge:
3537 return "__bridge";
3538 case OBC_BridgeTransfer:
3539 return "__bridge_transfer";
3540 case OBC_BridgeRetained:
3541 return "__bridge_retained";
3542 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003543
3544 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003545}
3546
Jay Foad39c79802011-01-12 09:06:06 +00003547bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smithcaf33902011-10-10 18:28:20 +00003548 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00003549}
3550
Benjamin Kramerc215e762012-08-24 11:54:20 +00003551ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003552 QualType Type, SourceLocation BLoc,
3553 SourceLocation RP)
3554 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3555 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003556 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003557 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003558 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003559{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003560 SubExprs = new (C) Stmt*[args.size()];
3561 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003562 if (args[i]->isTypeDependent())
3563 ExprBits.TypeDependent = true;
3564 if (args[i]->isValueDependent())
3565 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003566 if (args[i]->isInstantiationDependent())
3567 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003568 if (args[i]->containsUnexpandedParameterPack())
3569 ExprBits.ContainsUnexpandedParameterPack = true;
3570
3571 SubExprs[i] = args[i];
3572 }
3573}
3574
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003575void ShuffleVectorExpr::setExprs(ASTContext &C, ArrayRef<Expr *> Exprs) {
Nate Begeman48745922009-08-12 02:28:50 +00003576 if (SubExprs) C.Deallocate(SubExprs);
3577
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003578 this->NumExprs = Exprs.size();
Dmitri Gribenko48d6daf2013-05-10 17:30:13 +00003579 SubExprs = new (C) Stmt*[NumExprs];
Dmitri Gribenko674eaa22013-05-10 00:43:44 +00003580 memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size());
Mike Stump11289f42009-09-09 15:08:12 +00003581}
Nate Begeman48745922009-08-12 02:28:50 +00003582
Peter Collingbourne91147592011-04-15 00:35:48 +00003583GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3584 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003585 ArrayRef<TypeSourceInfo*> AssocTypes,
3586 ArrayRef<Expr*> AssocExprs,
3587 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003588 SourceLocation RParenLoc,
3589 bool ContainsUnexpandedParameterPack,
3590 unsigned ResultIndex)
3591 : Expr(GenericSelectionExprClass,
3592 AssocExprs[ResultIndex]->getType(),
3593 AssocExprs[ResultIndex]->getValueKind(),
3594 AssocExprs[ResultIndex]->getObjectKind(),
3595 AssocExprs[ResultIndex]->isTypeDependent(),
3596 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003597 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003598 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003599 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3600 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3601 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3602 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003603 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003604 assert(AssocTypes.size() == AssocExprs.size());
3605 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3606 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003607}
3608
3609GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3610 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003611 ArrayRef<TypeSourceInfo*> AssocTypes,
3612 ArrayRef<Expr*> AssocExprs,
3613 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003614 SourceLocation RParenLoc,
3615 bool ContainsUnexpandedParameterPack)
3616 : Expr(GenericSelectionExprClass,
3617 Context.DependentTy,
3618 VK_RValue,
3619 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003620 /*isTypeDependent=*/true,
3621 /*isValueDependent=*/true,
3622 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003623 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003624 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3625 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3626 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3627 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003628 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003629 assert(AssocTypes.size() == AssocExprs.size());
3630 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3631 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003632}
3633
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003634//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003635// DesignatedInitExpr
3636//===----------------------------------------------------------------------===//
3637
Chandler Carruth631abd92011-06-16 06:47:06 +00003638IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003639 assert(Kind == FieldDesignator && "Only valid on a field designator");
3640 if (Field.NameOrField & 0x01)
3641 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3642 else
3643 return getField()->getIdentifier();
3644}
3645
Alexis Hunta8136cc2010-05-05 15:23:54 +00003646DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003647 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003648 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003649 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003650 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003651 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003652 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003653 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003654 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003655 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003656 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003657 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003658 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003659 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003660 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003661
3662 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003663 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003664 *Child++ = Init;
3665
3666 // Copy the designators and their subexpressions, computing
3667 // value-dependence along the way.
3668 unsigned IndexIdx = 0;
3669 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003670 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003671
3672 if (this->Designators[I].isArrayDesignator()) {
3673 // Compute type- and value-dependence.
3674 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003675 if (Index->isTypeDependent() || Index->isValueDependent())
3676 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003677 if (Index->isInstantiationDependent())
3678 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003679 // Propagate unexpanded parameter packs.
3680 if (Index->containsUnexpandedParameterPack())
3681 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003682
3683 // Copy the index expressions into permanent storage.
3684 *Child++ = IndexExprs[IndexIdx++];
3685 } else if (this->Designators[I].isArrayRangeDesignator()) {
3686 // Compute type- and value-dependence.
3687 Expr *Start = IndexExprs[IndexIdx];
3688 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003689 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003690 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003691 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003692 ExprBits.InstantiationDependent = true;
3693 } else if (Start->isInstantiationDependent() ||
3694 End->isInstantiationDependent()) {
3695 ExprBits.InstantiationDependent = true;
3696 }
3697
Douglas Gregora6e053e2010-12-15 01:34:56 +00003698 // Propagate unexpanded parameter packs.
3699 if (Start->containsUnexpandedParameterPack() ||
3700 End->containsUnexpandedParameterPack())
3701 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003702
3703 // Copy the start/end expressions into permanent storage.
3704 *Child++ = IndexExprs[IndexIdx++];
3705 *Child++ = IndexExprs[IndexIdx++];
3706 }
3707 }
3708
Benjamin Kramerc215e762012-08-24 11:54:20 +00003709 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003710}
3711
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003712DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003713DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003714 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003715 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003716 SourceLocation ColonOrEqualLoc,
3717 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003718 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003719 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003720 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003721 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003722 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003723}
3724
Mike Stump11289f42009-09-09 15:08:12 +00003725DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003726 unsigned NumIndexExprs) {
3727 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3728 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3729 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3730}
3731
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003732void DesignatedInitExpr::setDesignators(ASTContext &C,
3733 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003734 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003735 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003736 NumDesignators = NumDesigs;
3737 for (unsigned I = 0; I != NumDesigs; ++I)
3738 Designators[I] = Desigs[I];
3739}
3740
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003741SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3742 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3743 if (size() == 1)
3744 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003745 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3746 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003747}
3748
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003749SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003750 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003751 Designator &First =
3752 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003753 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003754 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003755 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3756 else
3757 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3758 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003759 StartLoc =
3760 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00003761 return StartLoc;
3762}
3763
3764SourceLocation DesignatedInitExpr::getLocEnd() const {
3765 return getInit()->getLocEnd();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003766}
3767
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003768Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003769 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003770 char *Ptr = static_cast<char *>(
3771 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003772 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003773 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3774 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3775}
3776
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003777Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003778 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003779 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003780 char *Ptr = static_cast<char *>(
3781 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003782 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003783 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3784 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3785}
3786
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003787Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const {
Mike Stump11289f42009-09-09 15:08:12 +00003788 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003789 "Requires array range designator");
Dmitri Gribenkod06f7ff2013-01-26 15:15:52 +00003790 char *Ptr = static_cast<char *>(
3791 const_cast<void *>(static_cast<const void *>(this)));
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003792 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003793 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3794 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3795}
3796
Douglas Gregord5846a12009-04-15 06:41:24 +00003797/// \brief Replaces the designator at index @p Idx with the series
3798/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003799void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003800 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003801 const Designator *Last) {
3802 unsigned NumNewDesignators = Last - First;
3803 if (NumNewDesignators == 0) {
3804 std::copy_backward(Designators + Idx + 1,
3805 Designators + NumDesignators,
3806 Designators + Idx);
3807 --NumNewDesignators;
3808 return;
3809 } else if (NumNewDesignators == 1) {
3810 Designators[Idx] = *First;
3811 return;
3812 }
3813
Mike Stump11289f42009-09-09 15:08:12 +00003814 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003815 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003816 std::copy(Designators, Designators + Idx, NewDesignators);
3817 std::copy(First, Last, NewDesignators + Idx);
3818 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3819 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003820 Designators = NewDesignators;
3821 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3822}
3823
Mike Stump11289f42009-09-09 15:08:12 +00003824ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003825 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003826 SourceLocation rparenloc)
3827 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003828 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003829 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3830 Exprs = new (C) Stmt*[exprs.size()];
3831 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003832 if (exprs[i]->isTypeDependent())
3833 ExprBits.TypeDependent = true;
3834 if (exprs[i]->isValueDependent())
3835 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003836 if (exprs[i]->isInstantiationDependent())
3837 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003838 if (exprs[i]->containsUnexpandedParameterPack())
3839 ExprBits.ContainsUnexpandedParameterPack = true;
3840
Nate Begeman5ec4b312009-08-10 23:49:36 +00003841 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003842 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003843}
3844
John McCall1bf58462011-02-16 08:02:54 +00003845const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3846 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3847 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003848 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3849 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003850 e = cast<CXXConstructExpr>(e)->getArg(0);
3851 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3852 e = ice->getSubExpr();
3853 return cast<OpaqueValueExpr>(e);
3854}
3855
John McCallfe96e0b2011-11-06 09:01:30 +00003856PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3857 unsigned numSemanticExprs) {
3858 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3859 (1 + numSemanticExprs) * sizeof(Expr*),
3860 llvm::alignOf<PseudoObjectExpr>());
3861 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3862}
3863
3864PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3865 : Expr(PseudoObjectExprClass, shell) {
3866 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3867}
3868
3869PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3870 ArrayRef<Expr*> semantics,
3871 unsigned resultIndex) {
3872 assert(syntax && "no syntactic expression!");
3873 assert(semantics.size() && "no semantic expressions!");
3874
3875 QualType type;
3876 ExprValueKind VK;
3877 if (resultIndex == NoResult) {
3878 type = C.VoidTy;
3879 VK = VK_RValue;
3880 } else {
3881 assert(resultIndex < semantics.size());
3882 type = semantics[resultIndex]->getType();
3883 VK = semantics[resultIndex]->getValueKind();
3884 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3885 }
3886
3887 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3888 (1 + semantics.size()) * sizeof(Expr*),
3889 llvm::alignOf<PseudoObjectExpr>());
3890 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3891 resultIndex);
3892}
3893
3894PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3895 Expr *syntax, ArrayRef<Expr*> semantics,
3896 unsigned resultIndex)
3897 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3898 /*filled in at end of ctor*/ false, false, false, false) {
3899 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3900 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3901
3902 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3903 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3904 getSubExprsBuffer()[i] = E;
3905
3906 if (E->isTypeDependent())
3907 ExprBits.TypeDependent = true;
3908 if (E->isValueDependent())
3909 ExprBits.ValueDependent = true;
3910 if (E->isInstantiationDependent())
3911 ExprBits.InstantiationDependent = true;
3912 if (E->containsUnexpandedParameterPack())
3913 ExprBits.ContainsUnexpandedParameterPack = true;
3914
3915 if (isa<OpaqueValueExpr>(E))
3916 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3917 "opaque-value semantic expressions for pseudo-object "
3918 "operations must have sources");
3919 }
3920}
3921
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003922//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003923// ExprIterator.
3924//===----------------------------------------------------------------------===//
3925
3926Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3927Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3928Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3929const Expr* ConstExprIterator::operator[](size_t idx) const {
3930 return cast<Expr>(I[idx]);
3931}
3932const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3933const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3934
3935//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003936// Child Iterators for iterating over subexpressions/substatements
3937//===----------------------------------------------------------------------===//
3938
Peter Collingbournee190dee2011-03-11 19:24:49 +00003939// UnaryExprOrTypeTraitExpr
3940Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003941 // If this is of a type and the type is a VLA type (and not a typedef), the
3942 // size expression of the VLA needs to be treated as an executable expression.
3943 // Why isn't this weirdness documented better in StmtIterator?
3944 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003945 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003946 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003947 return child_range(child_iterator(T), child_iterator());
3948 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003949 }
John McCallbd066782011-02-09 08:16:59 +00003950 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003951}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003952
Steve Naroffd54978b2007-09-18 23:55:05 +00003953// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003954Stmt::child_range ObjCMessageExpr::children() {
3955 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003956 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003957 begin = reinterpret_cast<Stmt **>(this + 1);
3958 else
3959 begin = reinterpret_cast<Stmt **>(getArgs());
3960 return child_range(begin,
3961 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003962}
3963
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003964ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003965 QualType T, ObjCMethodDecl *Method,
3966 SourceRange SR)
3967 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3968 false, false, false, false),
3969 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3970{
3971 Expr **SaveElements = getElements();
3972 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3973 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3974 ExprBits.ValueDependent = true;
3975 if (Elements[I]->isInstantiationDependent())
3976 ExprBits.InstantiationDependent = true;
3977 if (Elements[I]->containsUnexpandedParameterPack())
3978 ExprBits.ContainsUnexpandedParameterPack = true;
3979
3980 SaveElements[I] = Elements[I];
3981 }
3982}
3983
3984ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003985 ArrayRef<Expr *> Elements,
Ted Kremeneke65b0862012-03-06 20:05:56 +00003986 QualType T, ObjCMethodDecl * Method,
3987 SourceRange SR) {
3988 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3989 + Elements.size() * sizeof(Expr *));
3990 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3991}
3992
3993ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3994 unsigned NumElements) {
3995
3996 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3997 + NumElements * sizeof(Expr *));
3998 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3999}
4000
4001ObjCDictionaryLiteral::ObjCDictionaryLiteral(
4002 ArrayRef<ObjCDictionaryElement> VK,
4003 bool HasPackExpansions,
4004 QualType T, ObjCMethodDecl *method,
4005 SourceRange SR)
4006 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
4007 false, false),
4008 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
4009 DictWithObjectsMethod(method)
4010{
4011 KeyValuePair *KeyValues = getKeyValues();
4012 ExpansionData *Expansions = getExpansionData();
4013 for (unsigned I = 0; I < NumElements; I++) {
4014 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
4015 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
4016 ExprBits.ValueDependent = true;
4017 if (VK[I].Key->isInstantiationDependent() ||
4018 VK[I].Value->isInstantiationDependent())
4019 ExprBits.InstantiationDependent = true;
4020 if (VK[I].EllipsisLoc.isInvalid() &&
4021 (VK[I].Key->containsUnexpandedParameterPack() ||
4022 VK[I].Value->containsUnexpandedParameterPack()))
4023 ExprBits.ContainsUnexpandedParameterPack = true;
4024
4025 KeyValues[I].Key = VK[I].Key;
4026 KeyValues[I].Value = VK[I].Value;
4027 if (Expansions) {
4028 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
4029 if (VK[I].NumExpansions)
4030 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
4031 else
4032 Expansions[I].NumExpansionsPlusOne = 0;
4033 }
4034 }
4035}
4036
4037ObjCDictionaryLiteral *
4038ObjCDictionaryLiteral::Create(ASTContext &C,
4039 ArrayRef<ObjCDictionaryElement> VK,
4040 bool HasPackExpansions,
4041 QualType T, ObjCMethodDecl *method,
4042 SourceRange SR) {
4043 unsigned ExpansionsSize = 0;
4044 if (HasPackExpansions)
4045 ExpansionsSize = sizeof(ExpansionData) * VK.size();
4046
4047 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4048 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
4049 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
4050}
4051
4052ObjCDictionaryLiteral *
4053ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
4054 bool HasPackExpansions) {
4055 unsigned ExpansionsSize = 0;
4056 if (HasPackExpansions)
4057 ExpansionsSize = sizeof(ExpansionData) * NumElements;
4058 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
4059 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
4060 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
4061 HasPackExpansions);
4062}
4063
4064ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
4065 Expr *base,
4066 Expr *key, QualType T,
4067 ObjCMethodDecl *getMethod,
4068 ObjCMethodDecl *setMethod,
4069 SourceLocation RB) {
4070 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4071 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4072 OK_ObjCSubscript,
4073 getMethod, setMethod, RB);
4074}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004075
Benjamin Kramerc215e762012-08-24 11:54:20 +00004076AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004077 QualType t, AtomicOp op, SourceLocation RP)
4078 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4079 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004080 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004081{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004082 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4083 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004084 if (args[i]->isTypeDependent())
4085 ExprBits.TypeDependent = true;
4086 if (args[i]->isValueDependent())
4087 ExprBits.ValueDependent = true;
4088 if (args[i]->isInstantiationDependent())
4089 ExprBits.InstantiationDependent = true;
4090 if (args[i]->containsUnexpandedParameterPack())
4091 ExprBits.ContainsUnexpandedParameterPack = true;
4092
4093 SubExprs[i] = args[i];
4094 }
4095}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004096
4097unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4098 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004099 case AO__c11_atomic_init:
4100 case AO__c11_atomic_load:
4101 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004102 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004103
4104 case AO__c11_atomic_store:
4105 case AO__c11_atomic_exchange:
4106 case AO__atomic_load:
4107 case AO__atomic_store:
4108 case AO__atomic_store_n:
4109 case AO__atomic_exchange_n:
4110 case AO__c11_atomic_fetch_add:
4111 case AO__c11_atomic_fetch_sub:
4112 case AO__c11_atomic_fetch_and:
4113 case AO__c11_atomic_fetch_or:
4114 case AO__c11_atomic_fetch_xor:
4115 case AO__atomic_fetch_add:
4116 case AO__atomic_fetch_sub:
4117 case AO__atomic_fetch_and:
4118 case AO__atomic_fetch_or:
4119 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004120 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004121 case AO__atomic_add_fetch:
4122 case AO__atomic_sub_fetch:
4123 case AO__atomic_and_fetch:
4124 case AO__atomic_or_fetch:
4125 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004126 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004127 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004128
4129 case AO__atomic_exchange:
4130 return 4;
4131
4132 case AO__c11_atomic_compare_exchange_strong:
4133 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004134 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004135
4136 case AO__atomic_compare_exchange:
4137 case AO__atomic_compare_exchange_n:
4138 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004139 }
4140 llvm_unreachable("unknown atomic op");
4141}