blob: 9dec1e8131dd390a779cf3783cde70e620ec9919 [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
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor1be329d2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattnere925d612010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith938f40b2011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000033#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000034using namespace clang;
35
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000036const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindolaecbe2e92012-06-28 01:56:38 +000037 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola49e860b2012-06-26 17:45:31 +000038
39 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000040 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
41 DerivedType = PTy->getPointeeType();
42
Rafael Espindola60a2bba2012-07-17 20:24:05 +000043 if (DerivedType->isDependentType())
44 return NULL;
45
Rafael Espindola49e860b2012-06-26 17:45:31 +000046 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000047 Decl *D = Ty->getDecl();
48 return cast<CXXRecordDecl>(D);
49}
50
Rafael Espindola9c006de2012-10-27 01:03:43 +000051const Expr *
52Expr::skipRValueSubobjectAdjustments(
53 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
54 const Expr *E = this;
55 while (true) {
56 E = E->IgnoreParens();
57
58 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
59 if ((CE->getCastKind() == CK_DerivedToBase ||
60 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
61 E->getType()->isRecordType()) {
62 E = CE->getSubExpr();
63 CXXRecordDecl *Derived
64 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
65 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
66 continue;
67 }
68
69 if (CE->getCastKind() == CK_NoOp) {
70 E = CE->getSubExpr();
71 continue;
72 }
73 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
74 if (!ME->isArrow() && ME->getBase()->isRValue()) {
75 assert(ME->getBase()->getType()->isRecordType());
76 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
77 E = ME->getBase();
78 Adjustments.push_back(SubobjectAdjustment(Field));
79 continue;
80 }
81 }
82 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
83 if (BO->isPtrMemOp()) {
Rafael Espindola973aa202012-11-01 14:32:20 +000084 assert(BO->getRHS()->isRValue());
Rafael Espindola9c006de2012-10-27 01:03:43 +000085 E = BO->getLHS();
86 const MemberPointerType *MPT =
87 BO->getRHS()->getType()->getAs<MemberPointerType>();
88 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
89 }
90 }
91
92 // Nothing changed.
93 break;
94 }
95 return E;
96}
97
98const Expr *
99Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
100 const Expr *E = this;
101 // Look through single-element init lists that claim to be lvalues. They're
102 // just syntactic wrappers in this case.
103 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
104 if (ILE->getNumInits() == 1 && ILE->isGLValue())
105 E = ILE->getInit(0);
106 }
107
108 // Look through expressions for materialized temporaries (for now).
109 if (const MaterializeTemporaryExpr *M
110 = dyn_cast<MaterializeTemporaryExpr>(E)) {
111 MTE = M;
112 E = M->GetTemporaryExpr();
113 }
114
115 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
116 E = DAE->getExpr();
117 return E;
118}
119
Chris Lattner4ebae652010-04-16 23:34:13 +0000120/// isKnownToHaveBooleanValue - Return true if this is an integer expression
121/// that is known to return 0 or 1. This happens for _Bool/bool expressions
122/// but also int expressions which are produced by things like comparisons in
123/// C.
124bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +0000125 const Expr *E = IgnoreParens();
126
Chris Lattner4ebae652010-04-16 23:34:13 +0000127 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +0000128 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000129 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +0000130 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000131
Peter Collingbourne91147592011-04-15 00:35:48 +0000132 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000133 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +0000134 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +0000135 return UO->getSubExpr()->isKnownToHaveBooleanValue();
136 default:
137 return false;
138 }
139 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000140
John McCall45d30c32010-06-12 01:56:02 +0000141 // Only look through implicit casts. If the user writes
142 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +0000143 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000144 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000145
Peter Collingbourne91147592011-04-15 00:35:48 +0000146 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +0000147 switch (BO->getOpcode()) {
148 default: return false;
John McCalle3027922010-08-25 11:45:40 +0000149 case BO_LT: // Relational operators.
150 case BO_GT:
151 case BO_LE:
152 case BO_GE:
153 case BO_EQ: // Equality operators.
154 case BO_NE:
155 case BO_LAnd: // AND operator.
156 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000157 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000158
John McCalle3027922010-08-25 11:45:40 +0000159 case BO_And: // Bitwise AND operator.
160 case BO_Xor: // Bitwise XOR operator.
161 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000162 // Handle things like (x==2)|(y==12).
163 return BO->getLHS()->isKnownToHaveBooleanValue() &&
164 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000165
John McCalle3027922010-08-25 11:45:40 +0000166 case BO_Comma:
167 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000168 return BO->getRHS()->isKnownToHaveBooleanValue();
169 }
170 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000171
Peter Collingbourne91147592011-04-15 00:35:48 +0000172 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000173 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
174 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000175
Chris Lattner4ebae652010-04-16 23:34:13 +0000176 return false;
177}
178
John McCallbd066782011-02-09 08:16:59 +0000179// Amusing macro metaprogramming hack: check whether a class provides
180// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000181//
182// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000183namespace {
184 /// This implementation is used when a class provides a custom
185 /// implementation of getExprLoc.
186 template <class E, class T>
187 SourceLocation getExprLocImpl(const Expr *expr,
188 SourceLocation (T::*v)() const) {
189 return static_cast<const E*>(expr)->getExprLoc();
190 }
191
192 /// This implementation is used when a class doesn't provide
193 /// a custom implementation of getExprLoc. Overload resolution
194 /// should pick it over the implementation above because it's
195 /// more specialized according to function template partial ordering.
196 template <class E>
197 SourceLocation getExprLocImpl(const Expr *expr,
198 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000199 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000200 }
201}
202
203SourceLocation Expr::getExprLoc() const {
204 switch (getStmtClass()) {
205 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
206#define ABSTRACT_STMT(type)
207#define STMT(type, base) \
208 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
209#define EXPR(type, base) \
210 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
211#include "clang/AST/StmtNodes.inc"
212 }
213 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000214}
215
Chris Lattner0eedafe2006-08-24 04:56:27 +0000216//===----------------------------------------------------------------------===//
217// Primary Expressions.
218//===----------------------------------------------------------------------===//
219
Douglas Gregor678d76c2011-07-01 01:22:09 +0000220/// \brief Compute the type-, value-, and instantiation-dependence of a
221/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000222/// based on the declaration being referenced.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000223static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000224 bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000225 bool &ValueDependent,
226 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000227 TypeDependent = false;
228 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000229 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000230
231 // (TD) C++ [temp.dep.expr]p3:
232 // An id-expression is type-dependent if it contains:
233 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000234 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000235 //
236 // (VD) C++ [temp.dep.constexpr]p2:
237 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000238
Douglas Gregored6c7442009-11-23 11:41:28 +0000239 // (TD) - an identifier that was declared with dependent type
240 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000241 if (T->isDependentType()) {
242 TypeDependent = true;
243 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000245 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000246 } else if (T->isInstantiationDependentType()) {
247 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000248 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000249
Douglas Gregored6c7442009-11-23 11:41:28 +0000250 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000251 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000252 == DeclarationName::CXXConversionFunctionName) {
253 QualType T = D->getDeclName().getCXXNameType();
254 if (T->isDependentType()) {
255 TypeDependent = true;
256 ValueDependent = true;
257 InstantiationDependent = true;
258 return;
259 }
260
261 if (T->isInstantiationDependentType())
262 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000263 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000264
Douglas Gregored6c7442009-11-23 11:41:28 +0000265 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000266 if (isa<NonTypeTemplateParmDecl>(D)) {
267 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000268 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000269 return;
270 }
271
Douglas Gregored6c7442009-11-23 11:41:28 +0000272 // (VD) - a constant with integral or enumeration type and is
273 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000274 // (VD) - a constant with literal type and is initialized with an
275 // expression that is value-dependent [C++11].
276 // (VD) - FIXME: Missing from the standard:
277 // - an entity with reference type and is initialized with an
278 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000279 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000280 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithec8dcd22011-11-08 01:31:09 +0000281 Var->getType()->isLiteralType() :
282 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikief5697e52012-08-10 00:55:35 +0000283 (Var->getType().isConstQualified() ||
Richard Smithec8dcd22011-11-08 01:31:09 +0000284 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000285 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000286 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000287 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 InstantiationDependent = true;
289 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000290 }
291
Douglas Gregor0e4de762010-05-11 08:41:30 +0000292 // (VD) - FIXME: Missing from the standard:
293 // - a member function or a static data member of the current
294 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000295 if (Var->isStaticDataMember() &&
296 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000297 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000298 InstantiationDependent = true;
299 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000300
301 return;
302 }
303
Douglas Gregor0e4de762010-05-11 08:41:30 +0000304 // (VD) - FIXME: Missing from the standard:
305 // - a member function or a static data member of the current
306 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000307 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
308 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000309 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000310 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000311}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000312
Daniel Dunbar9d355812012-03-09 01:51:51 +0000313void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000314 bool TypeDependent = false;
315 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000316 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000317 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
318 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000319
320 // (TD) C++ [temp.dep.expr]p3:
321 // An id-expression is type-dependent if it contains:
322 //
323 // and
324 //
325 // (VD) C++ [temp.dep.constexpr]p2:
326 // An identifier is value-dependent if it is:
327 if (!TypeDependent && !ValueDependent &&
328 hasExplicitTemplateArgs() &&
329 TemplateSpecializationType::anyDependentTemplateArguments(
330 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000331 getNumTemplateArgs(),
332 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000333 TypeDependent = true;
334 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000335 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000336 }
337
338 ExprBits.TypeDependent = TypeDependent;
339 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000340 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000341
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000342 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000343 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000344 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000345}
346
Daniel Dunbar9d355812012-03-09 01:51:51 +0000347DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
348 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000349 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000350 ValueDecl *D, bool RefersToEnclosingLocal,
351 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000352 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000353 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000354 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000355 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000356 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
357 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000358 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000359 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000360 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
361 if (FoundD)
362 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000363 DeclRefExprBits.HasTemplateKWAndArgsInfo
364 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000365 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000366 if (TemplateArgs) {
367 bool Dependent = false;
368 bool InstantiationDependent = false;
369 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
371 Dependent,
372 InstantiationDependent,
373 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000374 if (InstantiationDependent)
375 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000376 } else if (TemplateKWLoc.isValid()) {
377 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000378 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000379 DeclRefExprBits.HadMultipleCandidates = 0;
380
Daniel Dunbar9d355812012-03-09 01:51:51 +0000381 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000382}
383
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000384DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000385 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000387 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000388 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000389 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000390 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000391 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000392 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000393 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000394 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000395 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000396 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000397 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000398}
399
400DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000401 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000402 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000403 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000404 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000405 const DeclarationNameInfo &NameInfo,
406 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000407 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000408 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000409 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000410 // Filter out cases where the found Decl is the same as the value refenenced.
411 if (D == FoundD)
412 FoundD = 0;
413
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000414 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000415 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000416 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000417 if (FoundD)
418 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000419 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000420 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
421 else if (TemplateKWLoc.isValid())
422 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000423
Chris Lattner5c0b4052010-10-30 05:14:06 +0000424 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000425 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000426 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000427 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000428}
429
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000430DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000431 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000432 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000433 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000434 unsigned NumTemplateArgs) {
435 std::size_t Size = sizeof(DeclRefExpr);
436 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000437 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000438 if (HasFoundDecl)
439 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000440 if (HasTemplateKWAndArgsInfo)
441 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000442
Chris Lattner5c0b4052010-10-30 05:14:06 +0000443 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000444 return new (Mem) DeclRefExpr(EmptyShell());
445}
446
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000447SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000448 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000449 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000450 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000451 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000452 R.setEnd(getRAngleLoc());
453 return R;
454}
Daniel Dunbarb507f272012-03-09 15:39:15 +0000455SourceLocation DeclRefExpr::getLocStart() const {
456 if (hasQualifier())
457 return getQualifierLoc().getBeginLoc();
458 return getNameInfo().getLocStart();
459}
460SourceLocation DeclRefExpr::getLocEnd() const {
461 if (hasExplicitTemplateArgs())
462 return getRAngleLoc();
463 return getNameInfo().getLocEnd();
464}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000465
Anders Carlsson2fb08242009-09-08 18:24:21 +0000466// FIXME: Maybe this should use DeclPrinter with a special "print predefined
467// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000468std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
469 ASTContext &Context = CurrentDecl->getASTContext();
470
Anders Carlsson2fb08242009-09-08 18:24:21 +0000471 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000472 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000473 return FD->getNameAsString();
474
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000475 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000476 llvm::raw_svector_ostream Out(Name);
477
478 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000479 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000480 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000481 if (MD->isStatic())
482 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000483 }
484
David Blaikiebbafb8a2012-03-11 07:00:24 +0000485 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000486 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregor11a434a2012-04-10 20:14:15 +0000487 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000488
Douglas Gregor11a434a2012-04-10 20:14:15 +0000489 const FunctionDecl *Decl = FD;
490 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
491 Decl = Pattern;
492 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000493 const FunctionProtoType *FT = 0;
494 if (FD->hasWrittenPrototype())
495 FT = dyn_cast<FunctionProtoType>(AFT);
496
Douglas Gregor11a434a2012-04-10 20:14:15 +0000497 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000498 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000499 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000500 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000501 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000502 }
503
504 if (FT->isVariadic()) {
505 if (FD->getNumParams()) POut << ", ";
506 POut << "...";
507 }
508 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000509 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000510
Sam Weinig4e83bd22009-12-27 01:38:20 +0000511 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
David Blaikief5697e52012-08-10 00:55:35 +0000512 const FunctionType *FT = cast<FunctionType>(MD->getType().getTypePtr());
513 if (FT->isConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000514 POut << " const";
David Blaikief5697e52012-08-10 00:55:35 +0000515 if (FT->isVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000516 POut << " volatile";
517 RefQualifierKind Ref = MD->getRefQualifier();
518 if (Ref == RQ_LValue)
519 POut << " &";
520 else if (Ref == RQ_RValue)
521 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000522 }
523
Douglas Gregor11a434a2012-04-10 20:14:15 +0000524 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
525 SpecsTy Specs;
526 const DeclContext *Ctx = FD->getDeclContext();
527 while (Ctx && isa<NamedDecl>(Ctx)) {
528 const ClassTemplateSpecializationDecl *Spec
529 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
530 if (Spec && !Spec->isExplicitSpecialization())
531 Specs.push_back(Spec);
532 Ctx = Ctx->getParent();
533 }
534
535 std::string TemplateParams;
536 llvm::raw_string_ostream TOut(TemplateParams);
537 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
538 I != E; ++I) {
539 const TemplateParameterList *Params
540 = (*I)->getSpecializedTemplate()->getTemplateParameters();
541 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
542 assert(Params->size() == Args.size());
543 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
544 StringRef Param = Params->getParam(i)->getName();
545 if (Param.empty()) continue;
546 TOut << Param << " = ";
547 Args.get(i).print(Policy, TOut);
548 TOut << ", ";
549 }
550 }
551
552 FunctionTemplateSpecializationInfo *FSI
553 = FD->getTemplateSpecializationInfo();
554 if (FSI && !FSI->isExplicitSpecialization()) {
555 const TemplateParameterList* Params
556 = FSI->getTemplate()->getTemplateParameters();
557 const TemplateArgumentList* Args = FSI->TemplateArguments;
558 assert(Params->size() == Args->size());
559 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
560 StringRef Param = Params->getParam(i)->getName();
561 if (Param.empty()) continue;
562 TOut << Param << " = ";
563 Args->get(i).print(Policy, TOut);
564 TOut << ", ";
565 }
566 }
567
568 TOut.flush();
569 if (!TemplateParams.empty()) {
570 // remove the trailing comma and space
571 TemplateParams.resize(TemplateParams.size() - 2);
572 POut << " [" << TemplateParams << "]";
573 }
574
575 POut.flush();
576
Sam Weinigd060ed42009-12-06 23:55:13 +0000577 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
578 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000579
580 Out << Proto;
581
582 Out.flush();
583 return Name.str().str();
584 }
585 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000586 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000587 llvm::raw_svector_ostream Out(Name);
588 Out << (MD->isInstanceMethod() ? '-' : '+');
589 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000590
591 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
592 // a null check to avoid a crash.
593 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000594 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000595
Anders Carlsson2fb08242009-09-08 18:24:21 +0000596 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000597 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000598 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000599
Anders Carlsson2fb08242009-09-08 18:24:21 +0000600 Out << ' ';
601 Out << MD->getSelector().getAsString();
602 Out << ']';
603
604 Out.flush();
605 return Name.str().str();
606 }
607 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
608 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
609 return "top level";
610 }
611 return "";
612}
613
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000614void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
615 if (hasAllocation())
616 C.Deallocate(pVal);
617
618 BitWidth = Val.getBitWidth();
619 unsigned NumWords = Val.getNumWords();
620 const uint64_t* Words = Val.getRawData();
621 if (NumWords > 1) {
622 pVal = new (C) uint64_t[NumWords];
623 std::copy(Words, Words + NumWords, pVal);
624 } else if (NumWords == 1)
625 VAL = Words[0];
626 else
627 VAL = 0;
628}
629
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000630IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
631 QualType type, SourceLocation l)
632 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
633 false, false),
634 Loc(l) {
635 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
636 assert(V.getBitWidth() == C.getIntWidth(type) &&
637 "Integer type is not the correct size for constant.");
638 setValue(C, V);
639}
640
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000641IntegerLiteral *
642IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
643 QualType type, SourceLocation l) {
644 return new (C) IntegerLiteral(C, V, type, l);
645}
646
647IntegerLiteral *
648IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
649 return new (C) IntegerLiteral(Empty);
650}
651
Benjamin Kramer1ea8e092012-07-04 17:04:04 +0000652FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
653 bool isexact, QualType Type, SourceLocation L)
654 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
655 false, false), Loc(L) {
656 FloatingLiteralBits.IsIEEE =
657 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
658 FloatingLiteralBits.IsExact = isexact;
659 setValue(C, V);
660}
661
662FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
663 : Expr(FloatingLiteralClass, Empty) {
664 FloatingLiteralBits.IsIEEE =
665 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
666 FloatingLiteralBits.IsExact = false;
667}
668
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000669FloatingLiteral *
670FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
671 bool isexact, QualType Type, SourceLocation L) {
672 return new (C) FloatingLiteral(C, V, isexact, Type, L);
673}
674
675FloatingLiteral *
676FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000677 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000678}
679
Chris Lattnera0173132008-06-07 22:13:43 +0000680/// getValueAsApproximateDouble - This returns the value as an inaccurate
681/// double. Note that this may cause loss of precision, but is useful for
682/// debugging dumps, etc.
683double FloatingLiteral::getValueAsApproximateDouble() const {
684 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000685 bool ignored;
686 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
687 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000688 return V.convertToDouble();
689}
690
Nick Lewycky4ed84042012-02-24 09:07:53 +0000691int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000692 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000693 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000694 case Ascii:
695 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000696 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000697 break;
698 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000699 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000700 break;
701 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000702 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000703 break;
704 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000705 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000706 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000707 }
708 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
709 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000710 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000711 && "character byte widths supported are 1, 2, and 4 only");
712 return CharByteWidth;
713}
714
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000715StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000716 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000717 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000718 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000719 // Allocate enough space for the StringLiteral plus an array of locations for
720 // any concatenated string tokens.
721 void *Mem = C.Allocate(sizeof(StringLiteral)+
722 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000723 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000724 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000725
Steve Naroffdf7855b2007-02-21 23:46:25 +0000726 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000727 SL->setString(C,Str,Kind,Pascal);
728
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000729 SL->TokLocs[0] = Loc[0];
730 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000731
Chris Lattner630970d2009-02-18 05:49:11 +0000732 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000733 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
734 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000735}
736
Douglas Gregor958dfc92009-04-15 16:35:07 +0000737StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
738 void *Mem = C.Allocate(sizeof(StringLiteral)+
739 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000740 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000741 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000742 SL->CharByteWidth = 0;
743 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000744 SL->NumConcatenated = NumStrs;
745 return SL;
746}
747
Richard Trieudc355912012-06-13 20:25:24 +0000748void StringLiteral::outputString(raw_ostream &OS) {
749 switch (getKind()) {
750 case Ascii: break; // no prefix.
751 case Wide: OS << 'L'; break;
752 case UTF8: OS << "u8"; break;
753 case UTF16: OS << 'u'; break;
754 case UTF32: OS << 'U'; break;
755 }
756 OS << '"';
757 static const char Hex[] = "0123456789ABCDEF";
758
759 unsigned LastSlashX = getLength();
760 for (unsigned I = 0, N = getLength(); I != N; ++I) {
761 switch (uint32_t Char = getCodeUnit(I)) {
762 default:
763 // FIXME: Convert UTF-8 back to codepoints before rendering.
764
765 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
766 // Leave invalid surrogates alone; we'll use \x for those.
767 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
768 Char <= 0xdbff) {
769 uint32_t Trail = getCodeUnit(I + 1);
770 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
771 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
772 ++I;
773 }
774 }
775
776 if (Char > 0xff) {
777 // If this is a wide string, output characters over 0xff using \x
778 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
779 // codepoint: use \x escapes for invalid codepoints.
780 if (getKind() == Wide ||
781 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
782 // FIXME: Is this the best way to print wchar_t?
783 OS << "\\x";
784 int Shift = 28;
785 while ((Char >> Shift) == 0)
786 Shift -= 4;
787 for (/**/; Shift >= 0; Shift -= 4)
788 OS << Hex[(Char >> Shift) & 15];
789 LastSlashX = I;
790 break;
791 }
792
793 if (Char > 0xffff)
794 OS << "\\U00"
795 << Hex[(Char >> 20) & 15]
796 << Hex[(Char >> 16) & 15];
797 else
798 OS << "\\u";
799 OS << Hex[(Char >> 12) & 15]
800 << Hex[(Char >> 8) & 15]
801 << Hex[(Char >> 4) & 15]
802 << Hex[(Char >> 0) & 15];
803 break;
804 }
805
806 // If we used \x... for the previous character, and this character is a
807 // hexadecimal digit, prevent it being slurped as part of the \x.
808 if (LastSlashX + 1 == I) {
809 switch (Char) {
810 case '0': case '1': case '2': case '3': case '4':
811 case '5': case '6': case '7': case '8': case '9':
812 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
813 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
814 OS << "\"\"";
815 }
816 }
817
818 assert(Char <= 0xff &&
819 "Characters above 0xff should already have been handled.");
820
821 if (isprint(Char))
822 OS << (char)Char;
823 else // Output anything hard as an octal escape.
824 OS << '\\'
825 << (char)('0' + ((Char >> 6) & 7))
826 << (char)('0' + ((Char >> 3) & 7))
827 << (char)('0' + ((Char >> 0) & 7));
828 break;
829 // Handle some common non-printable cases to make dumps prettier.
830 case '\\': OS << "\\\\"; break;
831 case '"': OS << "\\\""; break;
832 case '\n': OS << "\\n"; break;
833 case '\t': OS << "\\t"; break;
834 case '\a': OS << "\\a"; break;
835 case '\b': OS << "\\b"; break;
836 }
837 }
838 OS << '"';
839}
840
Eli Friedmanfcec6302011-11-01 02:23:42 +0000841void StringLiteral::setString(ASTContext &C, StringRef Str,
842 StringKind Kind, bool IsPascal) {
843 //FIXME: we assume that the string data comes from a target that uses the same
844 // code unit size and endianess for the type of string.
845 this->Kind = Kind;
846 this->IsPascal = IsPascal;
847
Nick Lewycky4ed84042012-02-24 09:07:53 +0000848 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000849 assert((Str.size()%CharByteWidth == 0)
850 && "size of data must be multiple of CharByteWidth");
851 Length = Str.size()/CharByteWidth;
852
853 switch(CharByteWidth) {
854 case 1: {
855 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000856 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000857 StrData.asChar = AStrData;
858 break;
859 }
860 case 2: {
861 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000862 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000863 StrData.asUInt16 = AStrData;
864 break;
865 }
866 case 4: {
867 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis61710892012-09-14 21:17:41 +0000868 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedmanfcec6302011-11-01 02:23:42 +0000869 StrData.asUInt32 = AStrData;
870 break;
871 }
872 default:
873 assert(false && "unsupported CharByteWidth");
874 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000875}
876
Chris Lattnere925d612010-11-17 07:37:15 +0000877/// getLocationOfByte - Return a source location that points to the specified
878/// byte of this string literal.
879///
880/// Strings are amazingly complex. They can be formed from multiple tokens and
881/// can have escape sequences in them in addition to the usual trigraph and
882/// escaped newline business. This routine handles this complexity.
883///
884SourceLocation StringLiteral::
885getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
886 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000887 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
888 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000889
Chris Lattnere925d612010-11-17 07:37:15 +0000890 // Loop over all of the tokens in this string until we find the one that
891 // contains the byte we're looking for.
892 unsigned TokNo = 0;
893 while (1) {
894 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
895 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
896
897 // Get the spelling of the string so that we can get the data that makes up
898 // the string literal, not the identifier for the macro it is potentially
899 // expanded through.
900 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
901
902 // Re-lex the token to get its length and original spelling.
903 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
904 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000906 if (Invalid)
907 return StrTokSpellingLoc;
908
909 const char *StrData = Buffer.data()+LocInfo.second;
910
Chris Lattnere925d612010-11-17 07:37:15 +0000911 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000912 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
913 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000914 Token TheTok;
915 TheLexer.LexFromRawLexer(TheTok);
916
917 // Use the StringLiteralParser to compute the length of the string in bytes.
918 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
919 unsigned TokNumBytes = SLP.GetStringLength();
920
921 // If the byte is in this token, return the location of the byte.
922 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000923 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000924 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
925
926 // Now that we know the offset of the token in the spelling, use the
927 // preprocessor to get the offset in the original source.
928 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
929 }
930
931 // Move to the next string token.
932 ++TokNo;
933 ByteNo -= TokNumBytes;
934 }
935}
936
937
938
Chris Lattner1b926492006-08-23 06:42:10 +0000939/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
940/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie1d202a62012-10-08 01:11:04 +0000941StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +0000942 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000943 case UO_PostInc: return "++";
944 case UO_PostDec: return "--";
945 case UO_PreInc: return "++";
946 case UO_PreDec: return "--";
947 case UO_AddrOf: return "&";
948 case UO_Deref: return "*";
949 case UO_Plus: return "+";
950 case UO_Minus: return "-";
951 case UO_Not: return "~";
952 case UO_LNot: return "!";
953 case UO_Real: return "__real";
954 case UO_Imag: return "__imag";
955 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000956 }
David Blaikief47fa302012-01-17 02:30:50 +0000957 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +0000958}
959
John McCalle3027922010-08-25 11:45:40 +0000960UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000961UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
962 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +0000963 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000964 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
965 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
966 case OO_Amp: return UO_AddrOf;
967 case OO_Star: return UO_Deref;
968 case OO_Plus: return UO_Plus;
969 case OO_Minus: return UO_Minus;
970 case OO_Tilde: return UO_Not;
971 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000972 }
973}
974
975OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
976 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000977 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
978 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
979 case UO_AddrOf: return OO_Amp;
980 case UO_Deref: return OO_Star;
981 case UO_Plus: return OO_Plus;
982 case UO_Minus: return OO_Minus;
983 case UO_Not: return OO_Tilde;
984 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000985 default: return OO_None;
986 }
987}
988
989
Chris Lattner0eedafe2006-08-24 04:56:27 +0000990//===----------------------------------------------------------------------===//
991// Postfix Operators.
992//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000993
Peter Collingbourne3a347252011-02-08 21:18:02 +0000994CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000995 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000996 SourceLocation rparenloc)
997 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000998 fn->isTypeDependent(),
999 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001000 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001001 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001002 NumArgs(args.size()) {
Mike Stump11289f42009-09-09 15:08:12 +00001003
Benjamin Kramerc215e762012-08-24 11:54:20 +00001004 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +00001005 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001006 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001007 if (args[i]->isTypeDependent())
1008 ExprBits.TypeDependent = true;
1009 if (args[i]->isValueDependent())
1010 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001011 if (args[i]->isInstantiationDependent())
1012 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001013 if (args[i]->containsUnexpandedParameterPack())
1014 ExprBits.ContainsUnexpandedParameterPack = true;
1015
Peter Collingbourne3a347252011-02-08 21:18:02 +00001016 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001017 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001018
Peter Collingbourne3a347252011-02-08 21:18:02 +00001019 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +00001020 RParenLoc = rparenloc;
1021}
Nate Begeman1e36a852008-01-17 17:46:27 +00001022
Benjamin Kramerc215e762012-08-24 11:54:20 +00001023CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCall7decc9e2010-11-18 06:31:45 +00001024 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1025 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001026 fn->isTypeDependent(),
1027 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001028 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001029 fn->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001030 NumArgs(args.size()) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001031
Benjamin Kramerc215e762012-08-24 11:54:20 +00001032 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001033 SubExprs[FN] = fn;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001034 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001035 if (args[i]->isTypeDependent())
1036 ExprBits.TypeDependent = true;
1037 if (args[i]->isValueDependent())
1038 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001039 if (args[i]->isInstantiationDependent())
1040 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001041 if (args[i]->containsUnexpandedParameterPack())
1042 ExprBits.ContainsUnexpandedParameterPack = true;
1043
Peter Collingbourne3a347252011-02-08 21:18:02 +00001044 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00001045 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +00001046
Peter Collingbourne3a347252011-02-08 21:18:02 +00001047 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +00001048 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +00001049}
1050
Mike Stump11289f42009-09-09 15:08:12 +00001051CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1052 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001053 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +00001054 SubExprs = new (C) Stmt*[PREARGS_START];
1055 CallExprBits.NumPreArgs = 0;
1056}
1057
1058CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1059 EmptyShell Empty)
1060 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1061 // FIXME: Why do we allocate this?
1062 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1063 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +00001064}
1065
Nuno Lopes518e3702009-12-20 23:11:08 +00001066Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +00001067 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +00001068
1069 while (SubstNonTypeTemplateParmExpr *NTTP
1070 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1071 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1072 }
1073
Sebastian Redl2b1832e2010-09-10 20:55:30 +00001074 // If we're calling a dereference, look at the pointer instead.
1075 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1076 if (BO->isPtrMemOp())
1077 CEE = BO->getRHS()->IgnoreParenCasts();
1078 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1079 if (UO->getOpcode() == UO_Deref)
1080 CEE = UO->getSubExpr()->IgnoreParenCasts();
1081 }
Chris Lattner52301912009-07-17 15:46:27 +00001082 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +00001083 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +00001084 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1085 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001086
1087 return 0;
1088}
1089
Nuno Lopes518e3702009-12-20 23:11:08 +00001090FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001091 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001092}
1093
Chris Lattnere4407ed2007-12-28 05:25:02 +00001094/// setNumArgs - This changes the number of arguments present in this call.
1095/// Any orphaned expressions are deleted by this, and any new operands are set
1096/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +00001097void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001098 // No change, just return.
1099 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattnere4407ed2007-12-28 05:25:02 +00001101 // If shrinking # arguments, just delete the extras and forgot them.
1102 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001103 this->NumArgs = NumArgs;
1104 return;
1105 }
1106
1107 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001108 unsigned NumPreArgs = getNumPreArgs();
1109 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001110 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001111 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001112 NewSubExprs[i] = SubExprs[i];
1113 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001114 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1115 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001116 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Douglas Gregorba6e5572009-04-17 21:46:47 +00001118 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001119 SubExprs = NewSubExprs;
1120 this->NumArgs = NumArgs;
1121}
1122
Chris Lattner01ff98a2008-10-06 05:00:53 +00001123/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1124/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +00001125unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001126 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001127 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001128 // ImplicitCastExpr.
1129 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1130 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001131 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001132
Steve Narofff6e3b3292008-01-31 01:07:12 +00001133 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1134 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001135 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001136
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001137 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1138 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001139 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001141 if (!FDecl->getIdentifier())
1142 return 0;
1143
Douglas Gregor15fc9562009-09-12 00:22:50 +00001144 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001145}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001146
Anders Carlsson00a27592009-05-26 04:57:27 +00001147QualType CallExpr::getCallReturnType() const {
1148 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001149 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001150 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001151 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001152 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001153 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1154 // This should never be overloaded and so should never return null.
1155 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001156
John McCall0009fcc2011-04-26 20:42:42 +00001157 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001158 return FnType->getResultType();
1159}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001160
John McCall701417a2011-02-21 06:23:05 +00001161SourceRange CallExpr::getSourceRange() const {
1162 if (isa<CXXOperatorCallExpr>(this))
1163 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
1164
1165 SourceLocation begin = getCallee()->getLocStart();
1166 if (begin.isInvalid() && getNumArgs() > 0)
1167 begin = getArg(0)->getLocStart();
1168 SourceLocation end = getRParenLoc();
1169 if (end.isInvalid() && getNumArgs() > 0)
1170 end = getArg(getNumArgs() - 1)->getLocEnd();
1171 return SourceRange(begin, end);
1172}
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001173SourceLocation CallExpr::getLocStart() const {
1174 if (isa<CXXOperatorCallExpr>(this))
1175 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
1176
1177 SourceLocation begin = getCallee()->getLocStart();
1178 if (begin.isInvalid() && getNumArgs() > 0)
1179 begin = getArg(0)->getLocStart();
1180 return begin;
1181}
1182SourceLocation CallExpr::getLocEnd() const {
1183 if (isa<CXXOperatorCallExpr>(this))
1184 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
1185
1186 SourceLocation end = getRParenLoc();
1187 if (end.isInvalid() && getNumArgs() > 0)
1188 end = getArg(getNumArgs() - 1)->getLocEnd();
1189 return end;
1190}
John McCall701417a2011-02-21 06:23:05 +00001191
Alexis Hunta8136cc2010-05-05 15:23:54 +00001192OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001193 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001194 TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001195 ArrayRef<OffsetOfNode> comps,
1196 ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001197 SourceLocation RParenLoc) {
1198 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001199 sizeof(OffsetOfNode) * comps.size() +
1200 sizeof(Expr*) * exprs.size());
Douglas Gregor882211c2010-04-28 22:16:22 +00001201
Benjamin Kramerc215e762012-08-24 11:54:20 +00001202 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1203 RParenLoc);
Douglas Gregor882211c2010-04-28 22:16:22 +00001204}
1205
1206OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1207 unsigned numComps, unsigned numExprs) {
1208 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1209 sizeof(OffsetOfNode) * numComps +
1210 sizeof(Expr*) * numExprs);
1211 return new (Mem) OffsetOfExpr(numComps, numExprs);
1212}
1213
Alexis Hunta8136cc2010-05-05 15:23:54 +00001214OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001215 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001216 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor882211c2010-04-28 22:16:22 +00001217 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001218 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1219 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001220 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001221 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001222 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001223 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001224 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor882211c2010-04-28 22:16:22 +00001225{
Benjamin Kramerc215e762012-08-24 11:54:20 +00001226 for (unsigned i = 0; i != comps.size(); ++i) {
1227 setComponent(i, comps[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001228 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001229
Benjamin Kramerc215e762012-08-24 11:54:20 +00001230 for (unsigned i = 0; i != exprs.size(); ++i) {
1231 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001232 ExprBits.ValueDependent = true;
Benjamin Kramerc215e762012-08-24 11:54:20 +00001233 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +00001234 ExprBits.ContainsUnexpandedParameterPack = true;
1235
Benjamin Kramerc215e762012-08-24 11:54:20 +00001236 setIndexExpr(i, exprs[i]);
Douglas Gregor882211c2010-04-28 22:16:22 +00001237 }
1238}
1239
1240IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1241 assert(getKind() == Field || getKind() == Identifier);
1242 if (getKind() == Field)
1243 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001244
Douglas Gregor882211c2010-04-28 22:16:22 +00001245 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1246}
1247
Mike Stump11289f42009-09-09 15:08:12 +00001248MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001249 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001250 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001251 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001252 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001253 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001254 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001255 QualType ty,
1256 ExprValueKind vk,
1257 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001258 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001259
Douglas Gregorea972d32011-02-28 21:54:11 +00001260 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001261 founddecl.getDecl() != memberdecl ||
1262 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001263 if (hasQualOrFound)
1264 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001265
John McCall6b51f282009-11-23 01:53:49 +00001266 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001267 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1268 else if (TemplateKWLoc.isValid())
1269 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001270
Chris Lattner5c0b4052010-10-30 05:14:06 +00001271 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001272 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1273 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001274
1275 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001276 // FIXME: Wrong. We should be looking at the member declaration we found.
1277 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001278 E->setValueDependent(true);
1279 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001280 E->setInstantiationDependent(true);
1281 }
1282 else if (QualifierLoc &&
1283 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1284 E->setInstantiationDependent(true);
1285
John McCall16df1e52010-03-30 21:47:33 +00001286 E->HasQualifierOrFoundDecl = true;
1287
1288 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001289 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001290 NQ->FoundDecl = founddecl;
1291 }
1292
Abramo Bagnara7945c982012-01-27 09:46:47 +00001293 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1294
John McCall16df1e52010-03-30 21:47:33 +00001295 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001296 bool Dependent = false;
1297 bool InstantiationDependent = false;
1298 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001299 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1300 Dependent,
1301 InstantiationDependent,
1302 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001303 if (InstantiationDependent)
1304 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001305 } else if (TemplateKWLoc.isValid()) {
1306 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001307 }
1308
1309 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001310}
1311
Douglas Gregor25b7e052011-03-02 21:06:53 +00001312SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbarb507f272012-03-09 15:39:15 +00001313 return SourceRange(getLocStart(), getLocEnd());
1314}
1315SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001316 if (isImplicitAccess()) {
1317 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001318 return getQualifierLoc().getBeginLoc();
1319 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001320 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001321
Daniel Dunbarb507f272012-03-09 15:39:15 +00001322 // FIXME: We don't want this to happen. Rather, we should be able to
1323 // detect all kinds of implicit accesses more cleanly.
1324 SourceLocation BaseStartLoc = getBase()->getLocStart();
1325 if (BaseStartLoc.isValid())
1326 return BaseStartLoc;
1327 return MemberLoc;
1328}
1329SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001330 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbarb507f272012-03-09 15:39:15 +00001331 if (hasExplicitTemplateArgs())
Abramo Bagnara9b836fb2012-11-08 13:52:58 +00001332 EndLoc = getRAngleLoc();
1333 else if (EndLoc.isInvalid())
1334 EndLoc = getBase()->getLocEnd();
1335 return EndLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001336}
1337
John McCall9320b872011-09-09 05:25:32 +00001338void CastExpr::CheckCastConsistency() const {
1339 switch (getCastKind()) {
1340 case CK_DerivedToBase:
1341 case CK_UncheckedDerivedToBase:
1342 case CK_DerivedToBaseMemberPointer:
1343 case CK_BaseToDerived:
1344 case CK_BaseToDerivedMemberPointer:
1345 assert(!path_empty() && "Cast kind should have a base path!");
1346 break;
1347
1348 case CK_CPointerToObjCPointerCast:
1349 assert(getType()->isObjCObjectPointerType());
1350 assert(getSubExpr()->getType()->isPointerType());
1351 goto CheckNoBasePath;
1352
1353 case CK_BlockPointerToObjCPointerCast:
1354 assert(getType()->isObjCObjectPointerType());
1355 assert(getSubExpr()->getType()->isBlockPointerType());
1356 goto CheckNoBasePath;
1357
John McCallc62bb392012-02-15 01:22:51 +00001358 case CK_ReinterpretMemberPointer:
1359 assert(getType()->isMemberPointerType());
1360 assert(getSubExpr()->getType()->isMemberPointerType());
1361 goto CheckNoBasePath;
1362
John McCall9320b872011-09-09 05:25:32 +00001363 case CK_BitCast:
1364 // Arbitrary casts to C pointer types count as bitcasts.
1365 // Otherwise, we should only have block and ObjC pointer casts
1366 // here if they stay within the type kind.
1367 if (!getType()->isPointerType()) {
1368 assert(getType()->isObjCObjectPointerType() ==
1369 getSubExpr()->getType()->isObjCObjectPointerType());
1370 assert(getType()->isBlockPointerType() ==
1371 getSubExpr()->getType()->isBlockPointerType());
1372 }
1373 goto CheckNoBasePath;
1374
1375 case CK_AnyPointerToBlockPointerCast:
1376 assert(getType()->isBlockPointerType());
1377 assert(getSubExpr()->getType()->isAnyPointerType() &&
1378 !getSubExpr()->getType()->isBlockPointerType());
1379 goto CheckNoBasePath;
1380
Douglas Gregored90df32012-02-22 05:02:47 +00001381 case CK_CopyAndAutoreleaseBlockObject:
1382 assert(getType()->isBlockPointerType());
1383 assert(getSubExpr()->getType()->isBlockPointerType());
1384 goto CheckNoBasePath;
Eli Friedman34866c72012-08-31 00:14:07 +00001385
1386 case CK_FunctionToPointerDecay:
1387 assert(getType()->isPointerType());
1388 assert(getSubExpr()->getType()->isFunctionType());
1389 goto CheckNoBasePath;
1390
John McCall9320b872011-09-09 05:25:32 +00001391 // These should not have an inheritance path.
1392 case CK_Dynamic:
1393 case CK_ToUnion:
1394 case CK_ArrayToPointerDecay:
John McCall9320b872011-09-09 05:25:32 +00001395 case CK_NullToMemberPointer:
1396 case CK_NullToPointer:
1397 case CK_ConstructorConversion:
1398 case CK_IntegralToPointer:
1399 case CK_PointerToIntegral:
1400 case CK_ToVoid:
1401 case CK_VectorSplat:
1402 case CK_IntegralCast:
1403 case CK_IntegralToFloating:
1404 case CK_FloatingToIntegral:
1405 case CK_FloatingCast:
1406 case CK_ObjCObjectLValueCast:
1407 case CK_FloatingRealToComplex:
1408 case CK_FloatingComplexToReal:
1409 case CK_FloatingComplexCast:
1410 case CK_FloatingComplexToIntegralComplex:
1411 case CK_IntegralRealToComplex:
1412 case CK_IntegralComplexToReal:
1413 case CK_IntegralComplexCast:
1414 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001415 case CK_ARCProduceObject:
1416 case CK_ARCConsumeObject:
1417 case CK_ARCReclaimReturnedObject:
1418 case CK_ARCExtendBlockObject:
John McCall9320b872011-09-09 05:25:32 +00001419 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1420 goto CheckNoBasePath;
1421
1422 case CK_Dependent:
1423 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001424 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001425 case CK_AtomicToNonAtomic:
1426 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001427 case CK_PointerToBoolean:
1428 case CK_IntegralToBoolean:
1429 case CK_FloatingToBoolean:
1430 case CK_MemberPointerToBoolean:
1431 case CK_FloatingComplexToBoolean:
1432 case CK_IntegralComplexToBoolean:
1433 case CK_LValueBitCast: // -> bool&
1434 case CK_UserDefinedConversion: // operator bool()
Eli Friedman34866c72012-08-31 00:14:07 +00001435 case CK_BuiltinFnToFnPtr:
John McCall9320b872011-09-09 05:25:32 +00001436 CheckNoBasePath:
1437 assert(path_empty() && "Cast kind should not have a base path!");
1438 break;
1439 }
1440}
1441
Anders Carlsson496335e2009-09-03 00:59:21 +00001442const char *CastExpr::getCastKindName() const {
1443 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001444 case CK_Dependent:
1445 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001446 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001447 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001448 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001449 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001450 case CK_LValueToRValue:
1451 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001452 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001453 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001454 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001455 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001456 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001457 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001458 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001459 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001460 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001461 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001462 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001463 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001464 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001465 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001466 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001467 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001468 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001469 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001470 case CK_NullToPointer:
1471 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001472 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001473 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001474 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001475 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001476 case CK_ReinterpretMemberPointer:
1477 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001478 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001479 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001480 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001481 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001482 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001483 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001484 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001485 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001486 case CK_PointerToBoolean:
1487 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001488 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001489 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001490 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001491 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001492 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001493 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001494 case CK_IntegralToBoolean:
1495 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001496 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001497 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001498 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001499 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001500 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001501 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001502 case CK_FloatingToBoolean:
1503 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001504 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001505 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001506 case CK_CPointerToObjCPointerCast:
1507 return "CPointerToObjCPointerCast";
1508 case CK_BlockPointerToObjCPointerCast:
1509 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001510 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001511 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001512 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001513 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001514 case CK_FloatingRealToComplex:
1515 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001516 case CK_FloatingComplexToReal:
1517 return "FloatingComplexToReal";
1518 case CK_FloatingComplexToBoolean:
1519 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001520 case CK_FloatingComplexCast:
1521 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001522 case CK_FloatingComplexToIntegralComplex:
1523 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001524 case CK_IntegralRealToComplex:
1525 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001526 case CK_IntegralComplexToReal:
1527 return "IntegralComplexToReal";
1528 case CK_IntegralComplexToBoolean:
1529 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001530 case CK_IntegralComplexCast:
1531 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001532 case CK_IntegralComplexToFloatingComplex:
1533 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001534 case CK_ARCConsumeObject:
1535 return "ARCConsumeObject";
1536 case CK_ARCProduceObject:
1537 return "ARCProduceObject";
1538 case CK_ARCReclaimReturnedObject:
1539 return "ARCReclaimReturnedObject";
1540 case CK_ARCExtendBlockObject:
1541 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001542 case CK_AtomicToNonAtomic:
1543 return "AtomicToNonAtomic";
1544 case CK_NonAtomicToAtomic:
1545 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001546 case CK_CopyAndAutoreleaseBlockObject:
1547 return "CopyAndAutoreleaseBlockObject";
Eli Friedman34866c72012-08-31 00:14:07 +00001548 case CK_BuiltinFnToFnPtr:
1549 return "BuiltinFnToFnPtr";
Anders Carlsson496335e2009-09-03 00:59:21 +00001550 }
Mike Stump11289f42009-09-09 15:08:12 +00001551
John McCallc5e62b42010-11-13 09:02:35 +00001552 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001553}
1554
Douglas Gregord196a582009-12-14 19:27:10 +00001555Expr *CastExpr::getSubExprAsWritten() {
1556 Expr *SubExpr = 0;
1557 CastExpr *E = this;
1558 do {
1559 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001560
1561 // Skip through reference binding to temporary.
1562 if (MaterializeTemporaryExpr *Materialize
1563 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1564 SubExpr = Materialize->GetTemporaryExpr();
1565
Douglas Gregord196a582009-12-14 19:27:10 +00001566 // Skip any temporary bindings; they're implicit.
1567 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1568 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001569
Douglas Gregord196a582009-12-14 19:27:10 +00001570 // Conversions by constructor and conversion functions have a
1571 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001572 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001573 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001574 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001575 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001576
Douglas Gregord196a582009-12-14 19:27:10 +00001577 // If the subexpression we're left with is an implicit cast, look
1578 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001579 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1580
Douglas Gregord196a582009-12-14 19:27:10 +00001581 return SubExpr;
1582}
1583
John McCallcf142162010-08-07 06:22:56 +00001584CXXBaseSpecifier **CastExpr::path_buffer() {
1585 switch (getStmtClass()) {
1586#define ABSTRACT_STMT(x)
1587#define CASTEXPR(Type, Base) \
1588 case Stmt::Type##Class: \
1589 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1590#define STMT(Type, Base)
1591#include "clang/AST/StmtNodes.inc"
1592 default:
1593 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001594 }
1595}
1596
1597void CastExpr::setCastPath(const CXXCastPath &Path) {
1598 assert(Path.size() == path_size());
1599 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1600}
1601
1602ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1603 CastKind Kind, Expr *Operand,
1604 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001605 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001606 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1607 void *Buffer =
1608 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1609 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001610 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001611 if (PathSize) E->setCastPath(*BasePath);
1612 return E;
1613}
1614
1615ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1616 unsigned PathSize) {
1617 void *Buffer =
1618 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1619 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1620}
1621
1622
1623CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001624 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001625 const CXXCastPath *BasePath,
1626 TypeSourceInfo *WrittenTy,
1627 SourceLocation L, SourceLocation R) {
1628 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1629 void *Buffer =
1630 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1631 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001632 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001633 if (PathSize) E->setCastPath(*BasePath);
1634 return E;
1635}
1636
1637CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1638 void *Buffer =
1639 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1640 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1641}
1642
Chris Lattner1b926492006-08-23 06:42:10 +00001643/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1644/// corresponds to, e.g. "<<=".
David Blaikie1d202a62012-10-08 01:11:04 +00001645StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Chris Lattner1b926492006-08-23 06:42:10 +00001646 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001647 case BO_PtrMemD: return ".*";
1648 case BO_PtrMemI: return "->*";
1649 case BO_Mul: return "*";
1650 case BO_Div: return "/";
1651 case BO_Rem: return "%";
1652 case BO_Add: return "+";
1653 case BO_Sub: return "-";
1654 case BO_Shl: return "<<";
1655 case BO_Shr: return ">>";
1656 case BO_LT: return "<";
1657 case BO_GT: return ">";
1658 case BO_LE: return "<=";
1659 case BO_GE: return ">=";
1660 case BO_EQ: return "==";
1661 case BO_NE: return "!=";
1662 case BO_And: return "&";
1663 case BO_Xor: return "^";
1664 case BO_Or: return "|";
1665 case BO_LAnd: return "&&";
1666 case BO_LOr: return "||";
1667 case BO_Assign: return "=";
1668 case BO_MulAssign: return "*=";
1669 case BO_DivAssign: return "/=";
1670 case BO_RemAssign: return "%=";
1671 case BO_AddAssign: return "+=";
1672 case BO_SubAssign: return "-=";
1673 case BO_ShlAssign: return "<<=";
1674 case BO_ShrAssign: return ">>=";
1675 case BO_AndAssign: return "&=";
1676 case BO_XorAssign: return "^=";
1677 case BO_OrAssign: return "|=";
1678 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001679 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001680
David Blaikiee4d798f2012-01-20 21:50:17 +00001681 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001682}
Steve Naroff47500512007-04-19 23:00:49 +00001683
John McCalle3027922010-08-25 11:45:40 +00001684BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001685BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1686 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001687 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001688 case OO_Plus: return BO_Add;
1689 case OO_Minus: return BO_Sub;
1690 case OO_Star: return BO_Mul;
1691 case OO_Slash: return BO_Div;
1692 case OO_Percent: return BO_Rem;
1693 case OO_Caret: return BO_Xor;
1694 case OO_Amp: return BO_And;
1695 case OO_Pipe: return BO_Or;
1696 case OO_Equal: return BO_Assign;
1697 case OO_Less: return BO_LT;
1698 case OO_Greater: return BO_GT;
1699 case OO_PlusEqual: return BO_AddAssign;
1700 case OO_MinusEqual: return BO_SubAssign;
1701 case OO_StarEqual: return BO_MulAssign;
1702 case OO_SlashEqual: return BO_DivAssign;
1703 case OO_PercentEqual: return BO_RemAssign;
1704 case OO_CaretEqual: return BO_XorAssign;
1705 case OO_AmpEqual: return BO_AndAssign;
1706 case OO_PipeEqual: return BO_OrAssign;
1707 case OO_LessLess: return BO_Shl;
1708 case OO_GreaterGreater: return BO_Shr;
1709 case OO_LessLessEqual: return BO_ShlAssign;
1710 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1711 case OO_EqualEqual: return BO_EQ;
1712 case OO_ExclaimEqual: return BO_NE;
1713 case OO_LessEqual: return BO_LE;
1714 case OO_GreaterEqual: return BO_GE;
1715 case OO_AmpAmp: return BO_LAnd;
1716 case OO_PipePipe: return BO_LOr;
1717 case OO_Comma: return BO_Comma;
1718 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001719 }
1720}
1721
1722OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1723 static const OverloadedOperatorKind OverOps[] = {
1724 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1725 OO_Star, OO_Slash, OO_Percent,
1726 OO_Plus, OO_Minus,
1727 OO_LessLess, OO_GreaterGreater,
1728 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1729 OO_EqualEqual, OO_ExclaimEqual,
1730 OO_Amp,
1731 OO_Caret,
1732 OO_Pipe,
1733 OO_AmpAmp,
1734 OO_PipePipe,
1735 OO_Equal, OO_StarEqual,
1736 OO_SlashEqual, OO_PercentEqual,
1737 OO_PlusEqual, OO_MinusEqual,
1738 OO_LessLessEqual, OO_GreaterGreaterEqual,
1739 OO_AmpEqual, OO_CaretEqual,
1740 OO_PipeEqual,
1741 OO_Comma
1742 };
1743 return OverOps[Opc];
1744}
1745
Ted Kremenekac034612010-04-13 23:39:13 +00001746InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001747 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001748 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001749 false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001750 InitExprs(C, initExprs.size()),
Sebastian Redlc83ed822012-02-17 08:42:25 +00001751 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1752{
1753 sawArrayRangeDesignator(false);
1754 setInitializesStdInitializerList(false);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001755 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001756 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001757 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001758 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001759 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001760 if (initExprs[I]->isInstantiationDependent())
1761 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001762 if (initExprs[I]->containsUnexpandedParameterPack())
1763 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001764 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001765
Benjamin Kramerc215e762012-08-24 11:54:20 +00001766 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson4692db02007-08-31 04:56:16 +00001767}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001768
Ted Kremenekac034612010-04-13 23:39:13 +00001769void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001770 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001771 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001772}
1773
Ted Kremenekac034612010-04-13 23:39:13 +00001774void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001775 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001776}
1777
Ted Kremenekac034612010-04-13 23:39:13 +00001778Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001779 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001780 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001781 InitExprs.back() = expr;
1782 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001783 }
Mike Stump11289f42009-09-09 15:08:12 +00001784
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001785 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1786 InitExprs[Init] = expr;
1787 return Result;
1788}
1789
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001790void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001791 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001792 ArrayFillerOrUnionFieldInit = filler;
1793 // Fill out any "holes" in the array due to designated initializers.
1794 Expr **inits = getInits();
1795 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1796 if (inits[i] == 0)
1797 inits[i] = filler;
1798}
1799
Richard Smith9ec1e482012-04-15 02:50:59 +00001800bool InitListExpr::isStringLiteralInit() const {
1801 if (getNumInits() != 1)
1802 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001803 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1804 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smith9ec1e482012-04-15 02:50:59 +00001805 return false;
Eli Friedmancf4ab082012-08-20 20:55:45 +00001806 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smith9ec1e482012-04-15 02:50:59 +00001807 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1808}
1809
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001810SourceRange InitListExpr::getSourceRange() const {
1811 if (SyntacticForm)
1812 return SyntacticForm->getSourceRange();
1813 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1814 if (Beg.isInvalid()) {
1815 // Find the first non-null initializer.
1816 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1817 E = InitExprs.end();
1818 I != E; ++I) {
1819 if (Stmt *S = *I) {
1820 Beg = S->getLocStart();
1821 break;
1822 }
1823 }
1824 }
1825 if (End.isInvalid()) {
1826 // Find the first non-null initializer from the end.
1827 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1828 E = InitExprs.rend();
1829 I != E; ++I) {
1830 if (Stmt *S = *I) {
1831 End = S->getSourceRange().getEnd();
1832 break;
1833 }
1834 }
1835 }
1836 return SourceRange(Beg, End);
1837}
1838
Steve Naroff991e99d2008-09-04 15:31:07 +00001839/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001840///
John McCallc833dea2012-02-17 03:32:35 +00001841const FunctionProtoType *BlockExpr::getFunctionType() const {
1842 // The block pointer is never sugared, but the function type might be.
1843 return cast<BlockPointerType>(getType())
1844 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001845}
1846
Mike Stump11289f42009-09-09 15:08:12 +00001847SourceLocation BlockExpr::getCaretLocation() const {
1848 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001849}
Mike Stump11289f42009-09-09 15:08:12 +00001850const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001851 return TheBlock->getBody();
1852}
Mike Stump11289f42009-09-09 15:08:12 +00001853Stmt *BlockExpr::getBody() {
1854 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001855}
Steve Naroff415d3d52008-10-08 17:01:13 +00001856
1857
Chris Lattner1ec5f562007-06-27 05:38:08 +00001858//===----------------------------------------------------------------------===//
1859// Generic Expression Routines
1860//===----------------------------------------------------------------------===//
1861
Chris Lattner237f2752009-02-14 07:37:35 +00001862/// isUnusedResultAWarning - Return true if this immediate expression should
1863/// be warned about if the result is unused. If so, fill in Loc and Ranges
1864/// with location to warn on and the source range[s] to report with the
1865/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001866bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1867 SourceRange &R1, SourceRange &R2,
1868 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001869 // Don't warn if the expr is type dependent. The type could end up
1870 // instantiating to void.
1871 if (isTypeDependent())
1872 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001873
Chris Lattner1ec5f562007-06-27 05:38:08 +00001874 switch (getStmtClass()) {
1875 default:
John McCallc493a732010-03-12 07:11:26 +00001876 if (getType()->isVoidType())
1877 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001878 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001879 Loc = getExprLoc();
1880 R1 = getSourceRange();
1881 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001882 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001883 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001884 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001885 case GenericSelectionExprClass:
1886 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001887 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001888 case UnaryOperatorClass: {
1889 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001890
Chris Lattner1ec5f562007-06-27 05:38:08 +00001891 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001892 case UO_Plus:
1893 case UO_Minus:
1894 case UO_AddrOf:
1895 case UO_Not:
1896 case UO_LNot:
1897 case UO_Deref:
1898 break;
John McCalle3027922010-08-25 11:45:40 +00001899 case UO_PostInc:
1900 case UO_PostDec:
1901 case UO_PreInc:
1902 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001903 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001904 case UO_Real:
1905 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001906 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001907 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1908 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001909 return false;
1910 break;
John McCalle3027922010-08-25 11:45:40 +00001911 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001912 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001913 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00001914 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001915 Loc = UO->getOperatorLoc();
1916 R1 = UO->getSubExpr()->getSourceRange();
1917 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001918 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001919 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001920 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001921 switch (BO->getOpcode()) {
1922 default:
1923 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001924 // Consider the RHS of comma for side effects. LHS was checked by
1925 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001926 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001927 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1928 // lvalue-ness) of an assignment written in a macro.
1929 if (IntegerLiteral *IE =
1930 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1931 if (IE->getValue() == 0)
1932 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001933 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001934 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001935 case BO_LAnd:
1936 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001937 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1938 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001939 return false;
1940 break;
John McCall1e3715a2010-02-16 04:10:53 +00001941 }
Chris Lattner237f2752009-02-14 07:37:35 +00001942 if (BO->isAssignmentOp())
1943 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001944 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001945 Loc = BO->getOperatorLoc();
1946 R1 = BO->getLHS()->getSourceRange();
1947 R2 = BO->getRHS()->getSourceRange();
1948 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001949 }
Chris Lattner86928112007-08-25 02:00:02 +00001950 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001951 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001952 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001953 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001954
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001955 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001956 // If only one of the LHS or RHS is a warning, the operator might
1957 // be being used for control flow. Only warn if both the LHS and
1958 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001959 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00001960 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00001961 return false;
1962 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001963 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001964 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001965 }
1966
Chris Lattnera44d1162007-06-27 05:58:59 +00001967 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001968 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001969 Loc = cast<MemberExpr>(this)->getMemberLoc();
1970 R1 = SourceRange(Loc, Loc);
1971 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1972 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001973
Chris Lattner1ec5f562007-06-27 05:38:08 +00001974 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001975 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001976 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1977 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1978 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1979 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001980
Chandler Carruth46339472011-08-17 09:49:44 +00001981 case CXXOperatorCallExprClass: {
1982 // We warn about operator== and operator!= even when user-defined operator
1983 // overloads as there is no reasonable way to define these such that they
1984 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1985 // warning: these operators are commonly typo'ed, and so warning on them
1986 // provides additional value as well. If this list is updated,
1987 // DiagnoseUnusedComparison should be as well.
1988 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1989 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001990 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001991 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001992 Loc = Op->getOperatorLoc();
1993 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00001994 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001995 }
Chandler Carruth46339472011-08-17 09:49:44 +00001996
1997 // Fallthrough for generic call handling.
1998 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001999 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00002000 case CXXMemberCallExprClass:
2001 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00002002 // If this is a direct call, get the callee.
2003 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00002004 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00002005 // If the callee has attribute pure, const, or warn_unused_result, warn
2006 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00002007 //
2008 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2009 // updated to match for QoI.
2010 if (FD->getAttr<WarnUnusedResultAttr>() ||
2011 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002012 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00002013 Loc = CE->getCallee()->getLocStart();
2014 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00002015
Chris Lattner1a6babf2009-10-13 04:53:48 +00002016 if (unsigned NumArgs = CE->getNumArgs())
2017 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2018 CE->getArg(NumArgs-1)->getLocEnd());
2019 return true;
2020 }
Chris Lattner237f2752009-02-14 07:37:35 +00002021 }
2022 return false;
2023 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00002024
Matt Beaumont-Gayabf836c2012-10-23 06:15:26 +00002025 // If we don't know precisely what we're looking at, let's not warn.
2026 case UnresolvedLookupExprClass:
2027 case CXXUnresolvedConstructExprClass:
2028 return false;
2029
Anders Carlsson6aa50392009-11-17 17:11:23 +00002030 case CXXTemporaryObjectExprClass:
2031 case CXXConstructExprClass:
2032 return false;
2033
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002034 case ObjCMessageExprClass: {
2035 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00002036 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002037 ME->isInstanceMessage() &&
2038 !ME->getType()->isVoidType() &&
2039 ME->getSelector().getIdentifierInfoForSlot(0) &&
2040 ME->getSelector().getIdentifierInfoForSlot(0)
2041 ->getName().startswith("init")) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002042 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00002043 Loc = getExprLoc();
2044 R1 = ME->getSourceRange();
2045 return true;
2046 }
2047
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002048 const ObjCMethodDecl *MD = ME->getMethodDecl();
2049 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00002050 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002051 Loc = getExprLoc();
2052 return true;
2053 }
Chris Lattner237f2752009-02-14 07:37:35 +00002054 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00002055 }
Mike Stump11289f42009-09-09 15:08:12 +00002056
John McCallb7bd14f2010-12-02 01:19:52 +00002057 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002058 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00002059 Loc = getExprLoc();
2060 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00002061 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00002062
John McCallfe96e0b2011-11-06 09:01:30 +00002063 case PseudoObjectExprClass: {
2064 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2065
2066 // Only complain about things that have the form of a getter.
2067 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2068 isa<BinaryOperator>(PO->getSyntacticForm()))
2069 return false;
2070
Eli Friedmanc11535c2012-05-24 00:47:05 +00002071 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00002072 Loc = getExprLoc();
2073 R1 = getSourceRange();
2074 return true;
2075 }
2076
Chris Lattner944d3062008-07-26 19:51:01 +00002077 case StmtExprClass: {
2078 // Statement exprs don't logically have side effects themselves, but are
2079 // sometimes used in macros in ways that give them a type that is unused.
2080 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2081 // however, if the result of the stmt expr is dead, we don't want to emit a
2082 // warning.
2083 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002084 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00002085 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002086 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002087 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2088 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00002089 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00002090 }
Mike Stump11289f42009-09-09 15:08:12 +00002091
John McCallc493a732010-03-12 07:11:26 +00002092 if (getType()->isVoidType())
2093 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002094 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00002095 Loc = cast<StmtExpr>(this)->getLParenLoc();
2096 R1 = getSourceRange();
2097 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00002098 }
Eli Friedmanbdd57532012-09-24 23:02:26 +00002099 case CXXFunctionalCastExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00002100 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002101 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002102 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002103 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002104 if (CE->getCastKind() == CK_ToVoid) {
2105 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002106 CE->getSubExpr()->getType().isVolatileQualified()) {
2107 const DeclRefExpr *DRE =
2108 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2109 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2110 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2111 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2112 R1, R2, Ctx);
2113 }
2114 }
Chris Lattner2706a552009-07-28 18:25:28 +00002115 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002116 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002117
Matt Beaumont-Gay53e767b2012-10-24 01:14:28 +00002118 // Ignore casts within macro expansions.
2119 if (getExprLoc().isMacroID())
2120 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2121
Eli Friedmanc11535c2012-05-24 00:47:05 +00002122 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002123 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002124 if (CE->getCastKind() == CK_ConstructorConversion)
2125 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002126
Eli Friedmanc11535c2012-05-24 00:47:05 +00002127 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002128 if (const CXXFunctionalCastExpr *CXXCE =
2129 dyn_cast<CXXFunctionalCastExpr>(this)) {
2130 Loc = CXXCE->getTypeBeginLoc();
2131 R1 = CXXCE->getSubExpr()->getSourceRange();
2132 } else {
2133 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2134 Loc = CStyleCE->getLParenLoc();
2135 R1 = CStyleCE->getSubExpr()->getSourceRange();
2136 }
Chris Lattner237f2752009-02-14 07:37:35 +00002137 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002138 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002139 case ImplicitCastExprClass: {
2140 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002141
Eli Friedmanc11535c2012-05-24 00:47:05 +00002142 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2143 if (ICE->getCastKind() == CK_LValueToRValue &&
2144 ICE->getSubExpr()->getType().isVolatileQualified())
2145 return false;
2146
2147 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2148 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002149 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002150 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002151 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002152
2153 case CXXNewExprClass:
2154 // FIXME: In theory, there might be new expressions that don't have side
2155 // effects (e.g. a placement new with an uninitialized POD).
2156 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002157 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002158 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002159 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002160 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002161 case ExprWithCleanupsClass:
2162 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002163 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002164 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002165}
2166
Fariborz Jahanian07735332009-02-22 18:40:18 +00002167/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002168/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002169bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002170 const Expr *E = IgnoreParens();
2171 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002172 default:
2173 return false;
2174 case ObjCIvarRefExprClass:
2175 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002176 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002177 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002178 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002179 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002180 case MaterializeTemporaryExprClass:
2181 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2182 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002183 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002184 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002185 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002186 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002187
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002188 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2189 if (VD->hasGlobalStorage())
2190 return true;
2191 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002192 // dereferencing to a pointer is always a gc'able candidate,
2193 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002194 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002195 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002196 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002197 return false;
2198 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002199 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002200 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002201 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002202 }
2203 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002204 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002205 }
2206}
Sebastian Redlce354af2010-09-10 20:55:33 +00002207
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002208bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2209 if (isTypeDependent())
2210 return false;
John McCall086a4642010-11-24 05:12:34 +00002211 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002212}
2213
John McCall0009fcc2011-04-26 20:42:42 +00002214QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002215 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002216
2217 // Bound member expressions are always one of these possibilities:
2218 // x->m x.m x->*y x.*y
2219 // (possibly parenthesized)
2220
2221 expr = expr->IgnoreParens();
2222 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2223 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2224 return mem->getMemberDecl()->getType();
2225 }
2226
2227 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2228 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2229 ->getPointeeType();
2230 assert(type->isFunctionType());
2231 return type;
2232 }
2233
2234 assert(isa<UnresolvedMemberExpr>(expr));
2235 return QualType();
2236}
2237
Ted Kremenekfff70962008-01-17 16:57:34 +00002238Expr* Expr::IgnoreParens() {
2239 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002240 while (true) {
2241 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2242 E = P->getSubExpr();
2243 continue;
2244 }
2245 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2246 if (P->getOpcode() == UO_Extension) {
2247 E = P->getSubExpr();
2248 continue;
2249 }
2250 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002251 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2252 if (!P->isResultDependent()) {
2253 E = P->getResultExpr();
2254 continue;
2255 }
2256 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002257 return E;
2258 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002259}
2260
Chris Lattnerf2660962008-02-13 01:02:39 +00002261/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2262/// or CastExprs or ImplicitCastExprs, returning their operand.
2263Expr *Expr::IgnoreParenCasts() {
2264 Expr *E = this;
2265 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002266 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002267 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002268 continue;
2269 }
2270 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002271 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002272 continue;
2273 }
2274 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2275 if (P->getOpcode() == UO_Extension) {
2276 E = P->getSubExpr();
2277 continue;
2278 }
2279 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002280 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2281 if (!P->isResultDependent()) {
2282 E = P->getResultExpr();
2283 continue;
2284 }
2285 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002286 if (MaterializeTemporaryExpr *Materialize
2287 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2288 E = Materialize->GetTemporaryExpr();
2289 continue;
2290 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002291 if (SubstNonTypeTemplateParmExpr *NTTP
2292 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2293 E = NTTP->getReplacement();
2294 continue;
2295 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002296 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002297 }
2298}
2299
John McCall5a4ce8b2010-12-04 08:24:19 +00002300/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2301/// casts. This is intended purely as a temporary workaround for code
2302/// that hasn't yet been rewritten to do the right thing about those
2303/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002304Expr *Expr::IgnoreParenLValueCasts() {
2305 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002306 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002307 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2308 E = P->getSubExpr();
2309 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002310 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002311 if (P->getCastKind() == CK_LValueToRValue) {
2312 E = P->getSubExpr();
2313 continue;
2314 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002315 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2316 if (P->getOpcode() == UO_Extension) {
2317 E = P->getSubExpr();
2318 continue;
2319 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002320 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2321 if (!P->isResultDependent()) {
2322 E = P->getResultExpr();
2323 continue;
2324 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002325 } else if (MaterializeTemporaryExpr *Materialize
2326 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2327 E = Materialize->GetTemporaryExpr();
2328 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002329 } else if (SubstNonTypeTemplateParmExpr *NTTP
2330 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2331 E = NTTP->getReplacement();
2332 continue;
John McCall34376a62010-12-04 03:47:34 +00002333 }
2334 break;
2335 }
2336 return E;
2337}
Rafael Espindolaecbe2e92012-06-28 01:56:38 +00002338
2339Expr *Expr::ignoreParenBaseCasts() {
2340 Expr *E = this;
2341 while (true) {
2342 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2343 E = P->getSubExpr();
2344 continue;
2345 }
2346 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2347 if (CE->getCastKind() == CK_DerivedToBase ||
2348 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2349 CE->getCastKind() == CK_NoOp) {
2350 E = CE->getSubExpr();
2351 continue;
2352 }
2353 }
2354
2355 return E;
2356 }
2357}
2358
John McCalleebc8322010-05-05 22:59:52 +00002359Expr *Expr::IgnoreParenImpCasts() {
2360 Expr *E = this;
2361 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002362 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002363 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002364 continue;
2365 }
2366 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002367 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002368 continue;
2369 }
2370 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2371 if (P->getOpcode() == UO_Extension) {
2372 E = P->getSubExpr();
2373 continue;
2374 }
2375 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002376 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2377 if (!P->isResultDependent()) {
2378 E = P->getResultExpr();
2379 continue;
2380 }
2381 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002382 if (MaterializeTemporaryExpr *Materialize
2383 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2384 E = Materialize->GetTemporaryExpr();
2385 continue;
2386 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002387 if (SubstNonTypeTemplateParmExpr *NTTP
2388 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2389 E = NTTP->getReplacement();
2390 continue;
2391 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002392 return E;
John McCalleebc8322010-05-05 22:59:52 +00002393 }
2394}
2395
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002396Expr *Expr::IgnoreConversionOperator() {
2397 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002398 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002399 return MCE->getImplicitObjectArgument();
2400 }
2401 return this;
2402}
2403
Chris Lattneref26c772009-03-13 17:28:01 +00002404/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2405/// value (including ptr->int casts of the same size). Strip off any
2406/// ParenExpr or CastExprs, returning their operand.
2407Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2408 Expr *E = this;
2409 while (true) {
2410 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2411 E = P->getSubExpr();
2412 continue;
2413 }
Mike Stump11289f42009-09-09 15:08:12 +00002414
Chris Lattneref26c772009-03-13 17:28:01 +00002415 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2416 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002417 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002418 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002419
Chris Lattneref26c772009-03-13 17:28:01 +00002420 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2421 E = SE;
2422 continue;
2423 }
Mike Stump11289f42009-09-09 15:08:12 +00002424
Abramo Bagnara932e3932010-10-15 07:51:18 +00002425 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002426 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002427 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002428 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002429 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2430 E = SE;
2431 continue;
2432 }
2433 }
Mike Stump11289f42009-09-09 15:08:12 +00002434
Abramo Bagnara932e3932010-10-15 07:51:18 +00002435 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2436 if (P->getOpcode() == UO_Extension) {
2437 E = P->getSubExpr();
2438 continue;
2439 }
2440 }
2441
Peter Collingbourne91147592011-04-15 00:35:48 +00002442 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2443 if (!P->isResultDependent()) {
2444 E = P->getResultExpr();
2445 continue;
2446 }
2447 }
2448
Douglas Gregor6a40b082011-09-08 17:56:33 +00002449 if (SubstNonTypeTemplateParmExpr *NTTP
2450 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2451 E = NTTP->getReplacement();
2452 continue;
2453 }
2454
Chris Lattneref26c772009-03-13 17:28:01 +00002455 return E;
2456 }
2457}
2458
Douglas Gregord196a582009-12-14 19:27:10 +00002459bool Expr::isDefaultArgument() const {
2460 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002461 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2462 E = M->GetTemporaryExpr();
2463
Douglas Gregord196a582009-12-14 19:27:10 +00002464 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2465 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002466
Douglas Gregord196a582009-12-14 19:27:10 +00002467 return isa<CXXDefaultArgExpr>(E);
2468}
Chris Lattneref26c772009-03-13 17:28:01 +00002469
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002470/// \brief Skip over any no-op casts and any temporary-binding
2471/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002472static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002473 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2474 E = M->GetTemporaryExpr();
2475
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002476 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002477 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002478 E = ICE->getSubExpr();
2479 else
2480 break;
2481 }
2482
2483 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2484 E = BE->getSubExpr();
2485
2486 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002487 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002488 E = ICE->getSubExpr();
2489 else
2490 break;
2491 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002492
2493 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002494}
2495
John McCall7a626f62010-09-15 10:14:12 +00002496/// isTemporaryObject - Determines if this expression produces a
2497/// temporary of the given class type.
2498bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2499 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2500 return false;
2501
Anders Carlsson66bbf502010-11-28 16:40:49 +00002502 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002503
John McCall02dc8c72010-09-15 20:59:13 +00002504 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002505 if (!E->Classify(C).isPRValue()) {
2506 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002507 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002508 return false;
2509 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002510
John McCallf4ee1dd2010-09-16 06:57:56 +00002511 // Black-list a few cases which yield pr-values of class type that don't
2512 // refer to temporaries of that type:
2513
2514 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002515 if (isa<ImplicitCastExpr>(E)) {
2516 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2517 case CK_DerivedToBase:
2518 case CK_UncheckedDerivedToBase:
2519 return false;
2520 default:
2521 break;
2522 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002523 }
2524
John McCallf4ee1dd2010-09-16 06:57:56 +00002525 // - member expressions (all)
2526 if (isa<MemberExpr>(E))
2527 return false;
2528
Eli Friedman13ffdd82012-06-15 23:51:06 +00002529 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2530 if (BO->isPtrMemOp())
2531 return false;
2532
John McCallc07a0c72011-02-17 10:25:35 +00002533 // - opaque values (all)
2534 if (isa<OpaqueValueExpr>(E))
2535 return false;
2536
John McCall7a626f62010-09-15 10:14:12 +00002537 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002538}
2539
Douglas Gregor25b7e052011-03-02 21:06:53 +00002540bool Expr::isImplicitCXXThis() const {
2541 const Expr *E = this;
2542
2543 // Strip away parentheses and casts we don't care about.
2544 while (true) {
2545 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2546 E = Paren->getSubExpr();
2547 continue;
2548 }
2549
2550 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2551 if (ICE->getCastKind() == CK_NoOp ||
2552 ICE->getCastKind() == CK_LValueToRValue ||
2553 ICE->getCastKind() == CK_DerivedToBase ||
2554 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2555 E = ICE->getSubExpr();
2556 continue;
2557 }
2558 }
2559
2560 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2561 if (UnOp->getOpcode() == UO_Extension) {
2562 E = UnOp->getSubExpr();
2563 continue;
2564 }
2565 }
2566
Douglas Gregorfe314812011-06-21 17:03:29 +00002567 if (const MaterializeTemporaryExpr *M
2568 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2569 E = M->GetTemporaryExpr();
2570 continue;
2571 }
2572
Douglas Gregor25b7e052011-03-02 21:06:53 +00002573 break;
2574 }
2575
2576 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2577 return This->isImplicit();
2578
2579 return false;
2580}
2581
Douglas Gregor4619e432008-12-05 23:32:09 +00002582/// hasAnyTypeDependentArguments - Determines if any of the expressions
2583/// in Exprs is type-dependent.
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002584bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2585 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002586 if (Exprs[I]->isTypeDependent())
2587 return true;
2588
2589 return false;
2590}
2591
John McCall8b0f4ff2010-08-02 21:13:48 +00002592bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002593 // This function is attempting whether an expression is an initializer
2594 // which can be evaluated at compile-time. isEvaluatable handles most
2595 // of the cases, but it can't deal with some initializer-specific
2596 // expressions, and it can't deal with aggregates; we deal with those here,
2597 // and fall back to isEvaluatable for the other cases.
2598
John McCall8b0f4ff2010-08-02 21:13:48 +00002599 // If we ever capture reference-binding directly in the AST, we can
2600 // kill the second parameter.
2601
2602 if (IsForRef) {
2603 EvalResult Result;
2604 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2605 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002606
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002607 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002608 default: break;
Richard Smith941aae02011-12-09 06:47:34 +00002609 case IntegerLiteralClass:
2610 case FloatingLiteralClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002611 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002612 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002613 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002614 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002615 case CXXTemporaryObjectExprClass:
2616 case CXXConstructExprClass: {
2617 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002618
2619 // Only if it's
Richard Smithd62306a2011-11-10 06:34:14 +00002620 if (CE->getConstructor()->isTrivial()) {
2621 // 1) an application of the trivial default constructor or
2622 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002623
Richard Smithd62306a2011-11-10 06:34:14 +00002624 // 2) an elidable trivial copy construction of an operand which is
2625 // itself a constant initializer. Note that we consider the
2626 // operand on its own, *not* as a reference binding.
2627 if (CE->isElidable() &&
2628 CE->getArg(0)->isConstantInitializer(Ctx, false))
2629 return true;
2630 }
2631
2632 // 3) a foldable constexpr constructor.
2633 break;
John McCall81c9cea2010-08-01 21:51:45 +00002634 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002635 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002636 // This handles gcc's extension that allows global initializers like
2637 // "struct x {int x;} x = (struct x) {};".
2638 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002639 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002640 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002641 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002642 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002643 // FIXME: This doesn't deal with fields with reference types correctly.
2644 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2645 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002646 const InitListExpr *Exp = cast<InitListExpr>(this);
2647 unsigned numInits = Exp->getNumInits();
2648 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002649 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002650 return false;
2651 }
Eli Friedman384da272009-01-25 03:12:18 +00002652 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002653 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002654 case ImplicitValueInitExprClass:
2655 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002656 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002657 return cast<ParenExpr>(this)->getSubExpr()
2658 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002659 case GenericSelectionExprClass:
2660 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2661 return false;
2662 return cast<GenericSelectionExpr>(this)->getResultExpr()
2663 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002664 case ChooseExprClass:
2665 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2666 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002667 case UnaryOperatorClass: {
2668 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002669 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002670 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002671 break;
2672 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002673 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002674 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002675 case ImplicitCastExprClass:
Richard Smith161f09a2011-12-06 22:44:34 +00002676 case CStyleCastExprClass: {
2677 const CastExpr *CE = cast<CastExpr>(this);
2678
David Chisnallfa35df62012-01-16 17:27:18 +00002679 // If we're promoting an integer to an _Atomic type then this is constant
2680 // if the integer is constant. We also need to check the converse in case
2681 // someone does something like:
2682 //
2683 // int a = (_Atomic(int))42;
2684 //
2685 // I doubt anyone would write code like this directly, but it's quite
2686 // possible as the result of macro expansions.
2687 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2688 CE->getCastKind() == CK_AtomicToNonAtomic)
2689 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2690
Richard Smith161f09a2011-12-06 22:44:34 +00002691 // Handle bitcasts of vector constants.
2692 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2693 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2694
Eli Friedman13ec75b2011-12-21 00:43:02 +00002695 // Handle misc casts we want to ignore.
2696 // FIXME: Is it really safe to ignore all these?
2697 if (CE->getCastKind() == CK_NoOp ||
2698 CE->getCastKind() == CK_LValueToRValue ||
2699 CE->getCastKind() == CK_ToUnion ||
2700 CE->getCastKind() == CK_ConstructorConversion)
Richard Smith161f09a2011-12-06 22:44:34 +00002701 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2702
Eli Friedman384da272009-01-25 03:12:18 +00002703 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002704 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002705 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002706 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002707 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002708 }
Eli Friedman384da272009-01-25 03:12:18 +00002709 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002710}
2711
Richard Smith0421ce72012-08-07 04:16:51 +00002712bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2713 if (isInstantiationDependent())
2714 return true;
2715
2716 switch (getStmtClass()) {
2717 case NoStmtClass:
2718 #define ABSTRACT_STMT(Type)
2719 #define STMT(Type, Base) case Type##Class:
2720 #define EXPR(Type, Base)
2721 #include "clang/AST/StmtNodes.inc"
2722 llvm_unreachable("unexpected Expr kind");
2723
2724 case DependentScopeDeclRefExprClass:
2725 case CXXUnresolvedConstructExprClass:
2726 case CXXDependentScopeMemberExprClass:
2727 case UnresolvedLookupExprClass:
2728 case UnresolvedMemberExprClass:
2729 case PackExpansionExprClass:
2730 case SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00002731 case FunctionParmPackExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002732 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2733
Richard Smitha33e4fe2012-08-07 05:18:29 +00002734 case DeclRefExprClass:
2735 case ObjCIvarRefExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002736 case PredefinedExprClass:
2737 case IntegerLiteralClass:
2738 case FloatingLiteralClass:
2739 case ImaginaryLiteralClass:
2740 case StringLiteralClass:
2741 case CharacterLiteralClass:
2742 case OffsetOfExprClass:
2743 case ImplicitValueInitExprClass:
2744 case UnaryExprOrTypeTraitExprClass:
2745 case AddrLabelExprClass:
2746 case GNUNullExprClass:
2747 case CXXBoolLiteralExprClass:
2748 case CXXNullPtrLiteralExprClass:
2749 case CXXThisExprClass:
2750 case CXXScalarValueInitExprClass:
2751 case TypeTraitExprClass:
2752 case UnaryTypeTraitExprClass:
2753 case BinaryTypeTraitExprClass:
2754 case ArrayTypeTraitExprClass:
2755 case ExpressionTraitExprClass:
2756 case CXXNoexceptExprClass:
2757 case SizeOfPackExprClass:
2758 case ObjCStringLiteralClass:
2759 case ObjCEncodeExprClass:
2760 case ObjCBoolLiteralExprClass:
2761 case CXXUuidofExprClass:
2762 case OpaqueValueExprClass:
2763 // These never have a side-effect.
2764 return false;
2765
2766 case CallExprClass:
2767 case CompoundAssignOperatorClass:
2768 case VAArgExprClass:
2769 case AtomicExprClass:
2770 case StmtExprClass:
2771 case CXXOperatorCallExprClass:
2772 case CXXMemberCallExprClass:
2773 case UserDefinedLiteralClass:
2774 case CXXThrowExprClass:
2775 case CXXNewExprClass:
2776 case CXXDeleteExprClass:
2777 case ExprWithCleanupsClass:
2778 case CXXBindTemporaryExprClass:
2779 case BlockExprClass:
2780 case CUDAKernelCallExprClass:
2781 // These always have a side-effect.
2782 return true;
2783
2784 case ParenExprClass:
2785 case ArraySubscriptExprClass:
2786 case MemberExprClass:
2787 case ConditionalOperatorClass:
2788 case BinaryConditionalOperatorClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002789 case CompoundLiteralExprClass:
2790 case ExtVectorElementExprClass:
2791 case DesignatedInitExprClass:
2792 case ParenListExprClass:
Richard Smith0421ce72012-08-07 04:16:51 +00002793 case CXXPseudoDestructorExprClass:
2794 case SubstNonTypeTemplateParmExprClass:
2795 case MaterializeTemporaryExprClass:
2796 case ShuffleVectorExprClass:
2797 case AsTypeExprClass:
2798 // These have a side-effect if any subexpression does.
2799 break;
2800
Richard Smitha33e4fe2012-08-07 05:18:29 +00002801 case UnaryOperatorClass:
2802 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith0421ce72012-08-07 04:16:51 +00002803 return true;
2804 break;
Richard Smith0421ce72012-08-07 04:16:51 +00002805
2806 case BinaryOperatorClass:
2807 if (cast<BinaryOperator>(this)->isAssignmentOp())
2808 return true;
2809 break;
2810
Richard Smith0421ce72012-08-07 04:16:51 +00002811 case InitListExprClass:
2812 // FIXME: The children for an InitListExpr doesn't include the array filler.
2813 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2814 if (E->HasSideEffects(Ctx))
2815 return true;
2816 break;
2817
2818 case GenericSelectionExprClass:
2819 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2820 HasSideEffects(Ctx);
2821
2822 case ChooseExprClass:
2823 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2824
2825 case CXXDefaultArgExprClass:
2826 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2827
2828 case CXXDynamicCastExprClass: {
2829 // A dynamic_cast expression has side-effects if it can throw.
2830 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2831 if (DCE->getTypeAsWritten()->isReferenceType() &&
2832 DCE->getCastKind() == CK_Dynamic)
2833 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002834 } // Fall through.
2835 case ImplicitCastExprClass:
2836 case CStyleCastExprClass:
2837 case CXXStaticCastExprClass:
2838 case CXXReinterpretCastExprClass:
2839 case CXXConstCastExprClass:
2840 case CXXFunctionalCastExprClass: {
2841 const CastExpr *CE = cast<CastExpr>(this);
2842 if (CE->getCastKind() == CK_LValueToRValue &&
2843 CE->getSubExpr()->getType().isVolatileQualified())
2844 return true;
Richard Smith0421ce72012-08-07 04:16:51 +00002845 break;
2846 }
2847
Richard Smithef8bf432012-08-13 20:08:14 +00002848 case CXXTypeidExprClass:
2849 // typeid might throw if its subexpression is potentially-evaluated, so has
2850 // side-effects in that case whether or not its subexpression does.
2851 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith0421ce72012-08-07 04:16:51 +00002852
2853 case CXXConstructExprClass:
2854 case CXXTemporaryObjectExprClass: {
2855 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smitha33e4fe2012-08-07 05:18:29 +00002856 if (!CE->getConstructor()->isTrivial())
Richard Smith0421ce72012-08-07 04:16:51 +00002857 return true;
Richard Smitha33e4fe2012-08-07 05:18:29 +00002858 // A trivial constructor does not add any side-effects of its own. Just look
2859 // at its arguments.
Richard Smith0421ce72012-08-07 04:16:51 +00002860 break;
2861 }
2862
2863 case LambdaExprClass: {
2864 const LambdaExpr *LE = cast<LambdaExpr>(this);
2865 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2866 E = LE->capture_end(); I != E; ++I)
2867 if (I->getCaptureKind() == LCK_ByCopy)
2868 // FIXME: Only has a side-effect if the variable is volatile or if
2869 // the copy would invoke a non-trivial copy constructor.
2870 return true;
2871 return false;
2872 }
2873
2874 case PseudoObjectExprClass: {
2875 // Only look for side-effects in the semantic form, and look past
2876 // OpaqueValueExpr bindings in that form.
2877 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2878 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2879 E = PO->semantics_end();
2880 I != E; ++I) {
2881 const Expr *Subexpr = *I;
2882 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2883 Subexpr = OVE->getSourceExpr();
2884 if (Subexpr->HasSideEffects(Ctx))
2885 return true;
2886 }
2887 return false;
2888 }
2889
2890 case ObjCBoxedExprClass:
2891 case ObjCArrayLiteralClass:
2892 case ObjCDictionaryLiteralClass:
2893 case ObjCMessageExprClass:
2894 case ObjCSelectorExprClass:
2895 case ObjCProtocolExprClass:
2896 case ObjCPropertyRefExprClass:
2897 case ObjCIsaExprClass:
2898 case ObjCIndirectCopyRestoreExprClass:
2899 case ObjCSubscriptRefExprClass:
2900 case ObjCBridgedCastExprClass:
2901 // FIXME: Classify these cases better.
2902 return true;
2903 }
2904
2905 // Recurse to children.
2906 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2907 if (const Stmt *S = *SubStmts)
2908 if (cast<Expr>(S)->HasSideEffects(Ctx))
2909 return true;
2910
2911 return false;
2912}
2913
Douglas Gregor1be329d2012-02-23 07:33:15 +00002914namespace {
2915 /// \brief Look for a call to a non-trivial function within an expression.
2916 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2917 {
2918 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2919
2920 bool NonTrivial;
2921
2922 public:
2923 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002924 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002925
2926 bool hasNonTrivialCall() const { return NonTrivial; }
2927
2928 void VisitCallExpr(CallExpr *E) {
2929 if (CXXMethodDecl *Method
2930 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2931 if (Method->isTrivial()) {
2932 // Recurse to children of the call.
2933 Inherited::VisitStmt(E);
2934 return;
2935 }
2936 }
2937
2938 NonTrivial = true;
2939 }
2940
2941 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2942 if (E->getConstructor()->isTrivial()) {
2943 // Recurse to children of the call.
2944 Inherited::VisitStmt(E);
2945 return;
2946 }
2947
2948 NonTrivial = true;
2949 }
2950
2951 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2952 if (E->getTemporary()->getDestructor()->isTrivial()) {
2953 Inherited::VisitStmt(E);
2954 return;
2955 }
2956
2957 NonTrivial = true;
2958 }
2959 };
2960}
2961
2962bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2963 NonTrivialCallFinder Finder(Ctx);
2964 Finder.Visit(this);
2965 return Finder.hasNonTrivialCall();
2966}
2967
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002968/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2969/// pointer constant or not, as well as the specific kind of constant detected.
2970/// Null pointer constants can be integer constant expressions with the
2971/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2972/// (a GNU extension).
2973Expr::NullPointerConstantKind
2974Expr::isNullPointerConstant(ASTContext &Ctx,
2975 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002976 if (isValueDependent()) {
2977 switch (NPC) {
2978 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00002979 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00002980 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002981 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie1c7c8f72012-08-08 17:33:31 +00002982 return NPCK_ZeroExpression;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002983 else
2984 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002985
Douglas Gregor56751b52009-09-25 04:25:58 +00002986 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002987 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002988 }
2989 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002990
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002991 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002992 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002993 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002994 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002995 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002996 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002997 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002998 Pointee->isVoidType() && // to void*
2999 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00003000 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00003001 }
Steve Naroffada7d422007-05-20 17:54:12 +00003002 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003003 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3004 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00003005 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00003006 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3007 // Accept ((void*)0) as a null pointer constant, as many other
3008 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00003009 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00003010 } else if (const GenericSelectionExpr *GE =
3011 dyn_cast<GenericSelectionExpr>(this)) {
3012 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00003013 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00003014 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003015 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00003016 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00003017 } else if (isa<GNUNullExpr>(this)) {
3018 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003019 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00003020 } else if (const MaterializeTemporaryExpr *M
3021 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3022 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00003023 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3024 if (const Expr *Source = OVE->getSourceExpr())
3025 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00003026 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00003027
Sebastian Redl576fd422009-05-10 18:38:11 +00003028 // C++0x nullptr_t is always a null pointer constant.
3029 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003030 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00003031
Fariborz Jahanian3567c422010-09-27 22:42:37 +00003032 if (const RecordType *UT = getType()->getAsUnionType())
3033 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3034 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3035 const Expr *InitExpr = CLE->getInitializer();
3036 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3037 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3038 }
Steve Naroff4871fe02008-01-14 16:10:57 +00003039 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00003040 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003041 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003042 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00003043
Chris Lattner1abbd412007-06-08 17:58:43 +00003044 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith98a0a492012-02-14 21:38:30 +00003045 // test for the value 0. Don't use the C++11 constant expression semantics
3046 // for this, for now; once the dust settles on core issue 903, we might only
3047 // allow a literal 0 here in C++11 mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003048 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith98a0a492012-02-14 21:38:30 +00003049 if (!isCXX98IntegralConstantExpr(Ctx))
3050 return NPCK_NotNull;
3051 } else {
3052 if (!isIntegerConstantExpr(Ctx))
3053 return NPCK_NotNull;
3054 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00003055
David Blaikie1c7c8f72012-08-08 17:33:31 +00003056 if (EvaluateKnownConstInt(Ctx) != 0)
3057 return NPCK_NotNull;
3058
3059 if (isa<IntegerLiteral>(this))
3060 return NPCK_ZeroLiteral;
3061 return NPCK_ZeroExpression;
Steve Naroff218bc2b2007-05-04 21:54:46 +00003062}
Steve Narofff7a5da12007-07-28 23:10:27 +00003063
John McCall34376a62010-12-04 03:47:34 +00003064/// \brief If this expression is an l-value for an Objective C
3065/// property, find the underlying property reference expression.
3066const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3067 const Expr *E = this;
3068 while (true) {
3069 assert((E->getValueKind() == VK_LValue &&
3070 E->getObjectKind() == OK_ObjCProperty) &&
3071 "expression is not a property reference");
3072 E = E->IgnoreParenCasts();
3073 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3074 if (BO->getOpcode() == BO_Comma) {
3075 E = BO->getRHS();
3076 continue;
3077 }
3078 }
3079
3080 break;
3081 }
3082
3083 return cast<ObjCPropertyRefExpr>(E);
3084}
3085
Anna Zaks97c7ce32012-10-01 20:34:04 +00003086bool Expr::isObjCSelfExpr() const {
3087 const Expr *E = IgnoreParenImpCasts();
3088
3089 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3090 if (!DRE)
3091 return false;
3092
3093 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3094 if (!Param)
3095 return false;
3096
3097 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3098 if (!M)
3099 return false;
3100
3101 return M->getSelfDecl() == Param;
3102}
3103
Douglas Gregor71235ec2009-05-02 02:18:30 +00003104FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00003105 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00003106
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003107 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00003108 if (ICE->getCastKind() == CK_LValueToRValue ||
3109 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00003110 E = ICE->getSubExpr()->IgnoreParens();
3111 else
3112 break;
3113 }
3114
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003115 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00003116 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00003117 if (Field->isBitField())
3118 return Field;
3119
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00003120 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3121 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3122 if (Field->isBitField())
3123 return Field;
3124
Eli Friedman609ada22011-07-13 02:05:57 +00003125 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00003126 if (BinOp->isAssignmentOp() && BinOp->getLHS())
3127 return BinOp->getLHS()->getBitField();
3128
Eli Friedman609ada22011-07-13 02:05:57 +00003129 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3130 return BinOp->getRHS()->getBitField();
3131 }
3132
Douglas Gregor71235ec2009-05-02 02:18:30 +00003133 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00003134}
3135
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003136bool Expr::refersToVectorElement() const {
3137 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003138
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003139 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00003140 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00003141 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003142 E = ICE->getSubExpr()->IgnoreParens();
3143 else
3144 break;
3145 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00003146
Anders Carlsson8abde4b2010-01-31 17:18:49 +00003147 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3148 return ASE->getBase()->getType()->isVectorType();
3149
3150 if (isa<ExtVectorElementExpr>(E))
3151 return true;
3152
3153 return false;
3154}
3155
Chris Lattnerb8211f62009-02-16 22:14:05 +00003156/// isArrow - Return true if the base expression is a pointer to vector,
3157/// return false if the base expression is a vector.
3158bool ExtVectorElementExpr::isArrow() const {
3159 return getBase()->getType()->isPointerType();
3160}
3161
Nate Begemance4d7fc2008-04-18 23:10:10 +00003162unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00003163 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00003164 return VT->getNumElements();
3165 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00003166}
3167
Nate Begemanf322eab2008-05-09 06:41:27 +00003168/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00003169bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00003170 // FIXME: Refactor this code to an accessor on the AST node which returns the
3171 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003172 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00003173
3174 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003175 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00003176 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003177
Nate Begeman7e5185b2009-01-18 02:01:21 +00003178 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003179 if (Comp[0] == 's' || Comp[0] == 'S')
3180 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003181
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003182 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003183 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00003184 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00003185
Steve Naroff0d595ca2007-07-30 03:29:09 +00003186 return false;
3187}
Chris Lattner885b4952007-08-02 23:36:59 +00003188
Nate Begemanf322eab2008-05-09 06:41:27 +00003189/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00003190void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003191 SmallVectorImpl<unsigned> &Elts) const {
3192 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003193 if (Comp[0] == 's' || Comp[0] == 'S')
3194 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00003195
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003196 bool isHi = Comp == "hi";
3197 bool isLo = Comp == "lo";
3198 bool isEven = Comp == "even";
3199 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00003200
Nate Begemanf322eab2008-05-09 06:41:27 +00003201 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3202 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00003203
Nate Begemanf322eab2008-05-09 06:41:27 +00003204 if (isHi)
3205 Index = e + i;
3206 else if (isLo)
3207 Index = i;
3208 else if (isEven)
3209 Index = 2 * i;
3210 else if (isOdd)
3211 Index = 2 * i + 1;
3212 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00003213 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00003214
Nate Begemand3862152008-05-13 21:03:02 +00003215 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00003216 }
Nate Begemanf322eab2008-05-09 06:41:27 +00003217}
3218
Douglas Gregor9a129192010-04-21 00:45:42 +00003219ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003220 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003221 SourceLocation LBracLoc,
3222 SourceLocation SuperLoc,
3223 bool IsInstanceSuper,
3224 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003225 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003226 ArrayRef<SourceLocation> SelLocs,
3227 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003228 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003229 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003230 SourceLocation RBracLoc,
3231 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003232 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003233 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003234 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003235 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00003236 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3237 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003238 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003239 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3240 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00003241{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003242 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003243 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003244}
3245
Douglas Gregor9a129192010-04-21 00:45:42 +00003246ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003247 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003248 SourceLocation LBracLoc,
3249 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003250 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003251 ArrayRef<SourceLocation> SelLocs,
3252 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003253 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003254 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003255 SourceLocation RBracLoc,
3256 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003257 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003258 T->isDependentType(), T->isInstantiationDependentType(),
3259 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003260 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3261 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003262 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003263 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003264 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003265{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003266 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003267 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00003268}
3269
Douglas Gregor9a129192010-04-21 00:45:42 +00003270ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003271 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003272 SourceLocation LBracLoc,
3273 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003274 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003275 ArrayRef<SourceLocation> SelLocs,
3276 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003277 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003278 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003279 SourceLocation RBracLoc,
3280 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00003281 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003282 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003283 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003284 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00003285 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3286 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00003287 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003288 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003289 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00003290{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003291 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00003292 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003293}
3294
3295void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3296 ArrayRef<SourceLocation> SelLocs,
3297 SelectorLocationsKind SelLocsK) {
3298 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00003299 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003300 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003301 if (Args[I]->isTypeDependent())
3302 ExprBits.TypeDependent = true;
3303 if (Args[I]->isValueDependent())
3304 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003305 if (Args[I]->isInstantiationDependent())
3306 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003307 if (Args[I]->containsUnexpandedParameterPack())
3308 ExprBits.ContainsUnexpandedParameterPack = true;
3309
3310 MyArgs[I] = Args[I];
3311 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003312
Benjamin Kramer2325b242012-02-20 00:20:48 +00003313 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003314 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00003315 if (SelLocsK == SelLoc_NonStandard)
3316 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3317 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00003318}
3319
Douglas Gregor9a129192010-04-21 00:45:42 +00003320ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003321 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003322 SourceLocation LBracLoc,
3323 SourceLocation SuperLoc,
3324 bool IsInstanceSuper,
3325 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003326 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003327 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003328 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003329 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003330 SourceLocation RBracLoc,
3331 bool isImplicit) {
3332 assert((!SelLocs.empty() || isImplicit) &&
3333 "No selector locs for non-implicit message");
3334 ObjCMessageExpr *Mem;
3335 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3336 if (isImplicit)
3337 Mem = alloc(Context, Args.size(), 0);
3338 else
3339 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003340 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003341 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003342 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003343}
3344
3345ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003346 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003347 SourceLocation LBracLoc,
3348 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003349 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003350 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003351 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003352 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003353 SourceLocation RBracLoc,
3354 bool isImplicit) {
3355 assert((!SelLocs.empty() || isImplicit) &&
3356 "No selector locs for non-implicit message");
3357 ObjCMessageExpr *Mem;
3358 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3359 if (isImplicit)
3360 Mem = alloc(Context, Args.size(), 0);
3361 else
3362 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003363 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003364 SelLocs, SelLocsK, Method, Args, RBracLoc,
3365 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003366}
3367
3368ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003369 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003370 SourceLocation LBracLoc,
3371 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003372 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003373 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003374 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003375 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003376 SourceLocation RBracLoc,
3377 bool isImplicit) {
3378 assert((!SelLocs.empty() || isImplicit) &&
3379 "No selector locs for non-implicit message");
3380 ObjCMessageExpr *Mem;
3381 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3382 if (isImplicit)
3383 Mem = alloc(Context, Args.size(), 0);
3384 else
3385 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003386 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003387 SelLocs, SelLocsK, Method, Args, RBracLoc,
3388 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003389}
3390
Alexis Hunta8136cc2010-05-05 15:23:54 +00003391ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003392 unsigned NumArgs,
3393 unsigned NumStoredSelLocs) {
3394 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003395 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3396}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003397
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003398ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3399 ArrayRef<Expr *> Args,
3400 SourceLocation RBraceLoc,
3401 ArrayRef<SourceLocation> SelLocs,
3402 Selector Sel,
3403 SelectorLocationsKind &SelLocsK) {
3404 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3405 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3406 : 0;
3407 return alloc(C, Args.size(), NumStoredSelLocs);
3408}
3409
3410ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3411 unsigned NumArgs,
3412 unsigned NumStoredSelLocs) {
3413 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3414 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3415 return (ObjCMessageExpr *)C.Allocate(Size,
3416 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3417}
3418
3419void ObjCMessageExpr::getSelectorLocs(
3420 SmallVectorImpl<SourceLocation> &SelLocs) const {
3421 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3422 SelLocs.push_back(getSelectorLoc(i));
3423}
3424
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003425SourceRange ObjCMessageExpr::getReceiverRange() const {
3426 switch (getReceiverKind()) {
3427 case Instance:
3428 return getInstanceReceiver()->getSourceRange();
3429
3430 case Class:
3431 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3432
3433 case SuperInstance:
3434 case SuperClass:
3435 return getSuperLoc();
3436 }
3437
David Blaikiee4d798f2012-01-20 21:50:17 +00003438 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003439}
3440
Douglas Gregor9a129192010-04-21 00:45:42 +00003441Selector ObjCMessageExpr::getSelector() const {
3442 if (HasMethod)
3443 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3444 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003445 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003446}
3447
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003448QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor9a129192010-04-21 00:45:42 +00003449 switch (getReceiverKind()) {
3450 case Instance:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003451 return getInstanceReceiver()->getType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003452 case Class:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003453 return getClassReceiver();
Douglas Gregor9a129192010-04-21 00:45:42 +00003454 case SuperInstance:
Douglas Gregor9a129192010-04-21 00:45:42 +00003455 case SuperClass:
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003456 return getSuperType();
Douglas Gregor9a129192010-04-21 00:45:42 +00003457 }
3458
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00003459 llvm_unreachable("unexpected receiver kind");
3460}
3461
3462ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3463 QualType T = getReceiverType();
3464
3465 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3466 return Ptr->getInterfaceDecl();
3467
3468 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3469 return Ty->getInterface();
3470
Douglas Gregor9a129192010-04-21 00:45:42 +00003471 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003472}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003473
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003474StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003475 switch (getBridgeKind()) {
3476 case OBC_Bridge:
3477 return "__bridge";
3478 case OBC_BridgeTransfer:
3479 return "__bridge_transfer";
3480 case OBC_BridgeRetained:
3481 return "__bridge_retained";
3482 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003483
3484 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003485}
3486
Jay Foad39c79802011-01-12 09:06:06 +00003487bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smithcaf33902011-10-10 18:28:20 +00003488 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00003489}
3490
Benjamin Kramerc215e762012-08-24 11:54:20 +00003491ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregora6e053e2010-12-15 01:34:56 +00003492 QualType Type, SourceLocation BLoc,
3493 SourceLocation RP)
3494 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3495 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003496 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003497 Type->containsUnexpandedParameterPack()),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003498 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregora6e053e2010-12-15 01:34:56 +00003499{
Benjamin Kramerc215e762012-08-24 11:54:20 +00003500 SubExprs = new (C) Stmt*[args.size()];
3501 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003502 if (args[i]->isTypeDependent())
3503 ExprBits.TypeDependent = true;
3504 if (args[i]->isValueDependent())
3505 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003506 if (args[i]->isInstantiationDependent())
3507 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003508 if (args[i]->containsUnexpandedParameterPack())
3509 ExprBits.ContainsUnexpandedParameterPack = true;
3510
3511 SubExprs[i] = args[i];
3512 }
3513}
3514
Nate Begeman48745922009-08-12 02:28:50 +00003515void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3516 unsigned NumExprs) {
3517 if (SubExprs) C.Deallocate(SubExprs);
3518
3519 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00003520 this->NumExprs = NumExprs;
3521 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00003522}
Nate Begeman48745922009-08-12 02:28:50 +00003523
Peter Collingbourne91147592011-04-15 00:35:48 +00003524GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3525 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003526 ArrayRef<TypeSourceInfo*> AssocTypes,
3527 ArrayRef<Expr*> AssocExprs,
3528 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003529 SourceLocation RParenLoc,
3530 bool ContainsUnexpandedParameterPack,
3531 unsigned ResultIndex)
3532 : Expr(GenericSelectionExprClass,
3533 AssocExprs[ResultIndex]->getType(),
3534 AssocExprs[ResultIndex]->getValueKind(),
3535 AssocExprs[ResultIndex]->getObjectKind(),
3536 AssocExprs[ResultIndex]->isTypeDependent(),
3537 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003538 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003539 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003540 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3541 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3542 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3543 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003544 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003545 assert(AssocTypes.size() == AssocExprs.size());
3546 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3547 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003548}
3549
3550GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3551 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003552 ArrayRef<TypeSourceInfo*> AssocTypes,
3553 ArrayRef<Expr*> AssocExprs,
3554 SourceLocation DefaultLoc,
Peter Collingbourne91147592011-04-15 00:35:48 +00003555 SourceLocation RParenLoc,
3556 bool ContainsUnexpandedParameterPack)
3557 : Expr(GenericSelectionExprClass,
3558 Context.DependentTy,
3559 VK_RValue,
3560 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003561 /*isTypeDependent=*/true,
3562 /*isValueDependent=*/true,
3563 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003564 ContainsUnexpandedParameterPack),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003565 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3566 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3567 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3568 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbourne91147592011-04-15 00:35:48 +00003569 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramerc215e762012-08-24 11:54:20 +00003570 assert(AssocTypes.size() == AssocExprs.size());
3571 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3572 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbourne91147592011-04-15 00:35:48 +00003573}
3574
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003575//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003576// DesignatedInitExpr
3577//===----------------------------------------------------------------------===//
3578
Chandler Carruth631abd92011-06-16 06:47:06 +00003579IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003580 assert(Kind == FieldDesignator && "Only valid on a field designator");
3581 if (Field.NameOrField & 0x01)
3582 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3583 else
3584 return getField()->getIdentifier();
3585}
3586
Alexis Hunta8136cc2010-05-05 15:23:54 +00003587DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003588 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003589 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003590 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003591 bool GNUSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003592 ArrayRef<Expr*> IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003593 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003594 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003595 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003596 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003597 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003598 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003599 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003600 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003601 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003602
3603 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003604 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003605 *Child++ = Init;
3606
3607 // Copy the designators and their subexpressions, computing
3608 // value-dependence along the way.
3609 unsigned IndexIdx = 0;
3610 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003611 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003612
3613 if (this->Designators[I].isArrayDesignator()) {
3614 // Compute type- and value-dependence.
3615 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003616 if (Index->isTypeDependent() || Index->isValueDependent())
3617 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003618 if (Index->isInstantiationDependent())
3619 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003620 // Propagate unexpanded parameter packs.
3621 if (Index->containsUnexpandedParameterPack())
3622 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003623
3624 // Copy the index expressions into permanent storage.
3625 *Child++ = IndexExprs[IndexIdx++];
3626 } else if (this->Designators[I].isArrayRangeDesignator()) {
3627 // Compute type- and value-dependence.
3628 Expr *Start = IndexExprs[IndexIdx];
3629 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003630 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003631 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003632 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003633 ExprBits.InstantiationDependent = true;
3634 } else if (Start->isInstantiationDependent() ||
3635 End->isInstantiationDependent()) {
3636 ExprBits.InstantiationDependent = true;
3637 }
3638
Douglas Gregora6e053e2010-12-15 01:34:56 +00003639 // Propagate unexpanded parameter packs.
3640 if (Start->containsUnexpandedParameterPack() ||
3641 End->containsUnexpandedParameterPack())
3642 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003643
3644 // Copy the start/end expressions into permanent storage.
3645 *Child++ = IndexExprs[IndexIdx++];
3646 *Child++ = IndexExprs[IndexIdx++];
3647 }
3648 }
3649
Benjamin Kramerc215e762012-08-24 11:54:20 +00003650 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003651}
3652
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003653DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003654DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003655 unsigned NumDesignators,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003656 ArrayRef<Expr*> IndexExprs,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003657 SourceLocation ColonOrEqualLoc,
3658 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003659 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00003660 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003661 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003662 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003663 IndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003664}
3665
Mike Stump11289f42009-09-09 15:08:12 +00003666DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003667 unsigned NumIndexExprs) {
3668 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3669 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3670 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3671}
3672
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003673void DesignatedInitExpr::setDesignators(ASTContext &C,
3674 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003675 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003676 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003677 NumDesignators = NumDesigs;
3678 for (unsigned I = 0; I != NumDesigs; ++I)
3679 Designators[I] = Desigs[I];
3680}
3681
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003682SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3683 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3684 if (size() == 1)
3685 return DIE->getDesignator(0)->getSourceRange();
3686 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3687 DIE->getDesignator(size()-1)->getEndLocation());
3688}
3689
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003690SourceRange DesignatedInitExpr::getSourceRange() const {
3691 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003692 Designator &First =
3693 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003694 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003695 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003696 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3697 else
3698 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3699 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003700 StartLoc =
3701 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003702 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3703}
3704
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003705Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3706 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3707 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3708 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003709 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3710 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3711}
3712
3713Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003714 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003715 "Requires array range designator");
3716 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3717 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003718 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3719 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3720}
3721
3722Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003723 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003724 "Requires array range designator");
3725 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3726 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003727 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3728 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3729}
3730
Douglas Gregord5846a12009-04-15 06:41:24 +00003731/// \brief Replaces the designator at index @p Idx with the series
3732/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003733void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003734 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003735 const Designator *Last) {
3736 unsigned NumNewDesignators = Last - First;
3737 if (NumNewDesignators == 0) {
3738 std::copy_backward(Designators + Idx + 1,
3739 Designators + NumDesignators,
3740 Designators + Idx);
3741 --NumNewDesignators;
3742 return;
3743 } else if (NumNewDesignators == 1) {
3744 Designators[Idx] = *First;
3745 return;
3746 }
3747
Mike Stump11289f42009-09-09 15:08:12 +00003748 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003749 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003750 std::copy(Designators, Designators + Idx, NewDesignators);
3751 std::copy(First, Last, NewDesignators + Idx);
3752 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3753 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003754 Designators = NewDesignators;
3755 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3756}
3757
Mike Stump11289f42009-09-09 15:08:12 +00003758ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00003759 ArrayRef<Expr*> exprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003760 SourceLocation rparenloc)
3761 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003762 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00003763 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3764 Exprs = new (C) Stmt*[exprs.size()];
3765 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003766 if (exprs[i]->isTypeDependent())
3767 ExprBits.TypeDependent = true;
3768 if (exprs[i]->isValueDependent())
3769 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003770 if (exprs[i]->isInstantiationDependent())
3771 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003772 if (exprs[i]->containsUnexpandedParameterPack())
3773 ExprBits.ContainsUnexpandedParameterPack = true;
3774
Nate Begeman5ec4b312009-08-10 23:49:36 +00003775 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003776 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003777}
3778
John McCall1bf58462011-02-16 08:02:54 +00003779const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3780 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3781 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003782 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3783 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003784 e = cast<CXXConstructExpr>(e)->getArg(0);
3785 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3786 e = ice->getSubExpr();
3787 return cast<OpaqueValueExpr>(e);
3788}
3789
John McCallfe96e0b2011-11-06 09:01:30 +00003790PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3791 unsigned numSemanticExprs) {
3792 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3793 (1 + numSemanticExprs) * sizeof(Expr*),
3794 llvm::alignOf<PseudoObjectExpr>());
3795 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3796}
3797
3798PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3799 : Expr(PseudoObjectExprClass, shell) {
3800 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3801}
3802
3803PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3804 ArrayRef<Expr*> semantics,
3805 unsigned resultIndex) {
3806 assert(syntax && "no syntactic expression!");
3807 assert(semantics.size() && "no semantic expressions!");
3808
3809 QualType type;
3810 ExprValueKind VK;
3811 if (resultIndex == NoResult) {
3812 type = C.VoidTy;
3813 VK = VK_RValue;
3814 } else {
3815 assert(resultIndex < semantics.size());
3816 type = semantics[resultIndex]->getType();
3817 VK = semantics[resultIndex]->getValueKind();
3818 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3819 }
3820
3821 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3822 (1 + semantics.size()) * sizeof(Expr*),
3823 llvm::alignOf<PseudoObjectExpr>());
3824 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3825 resultIndex);
3826}
3827
3828PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3829 Expr *syntax, ArrayRef<Expr*> semantics,
3830 unsigned resultIndex)
3831 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3832 /*filled in at end of ctor*/ false, false, false, false) {
3833 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3834 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3835
3836 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3837 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3838 getSubExprsBuffer()[i] = E;
3839
3840 if (E->isTypeDependent())
3841 ExprBits.TypeDependent = true;
3842 if (E->isValueDependent())
3843 ExprBits.ValueDependent = true;
3844 if (E->isInstantiationDependent())
3845 ExprBits.InstantiationDependent = true;
3846 if (E->containsUnexpandedParameterPack())
3847 ExprBits.ContainsUnexpandedParameterPack = true;
3848
3849 if (isa<OpaqueValueExpr>(E))
3850 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3851 "opaque-value semantic expressions for pseudo-object "
3852 "operations must have sources");
3853 }
3854}
3855
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003856//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003857// ExprIterator.
3858//===----------------------------------------------------------------------===//
3859
3860Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3861Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3862Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3863const Expr* ConstExprIterator::operator[](size_t idx) const {
3864 return cast<Expr>(I[idx]);
3865}
3866const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3867const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3868
3869//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003870// Child Iterators for iterating over subexpressions/substatements
3871//===----------------------------------------------------------------------===//
3872
Peter Collingbournee190dee2011-03-11 19:24:49 +00003873// UnaryExprOrTypeTraitExpr
3874Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003875 // If this is of a type and the type is a VLA type (and not a typedef), the
3876 // size expression of the VLA needs to be treated as an executable expression.
3877 // Why isn't this weirdness documented better in StmtIterator?
3878 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003879 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003880 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003881 return child_range(child_iterator(T), child_iterator());
3882 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003883 }
John McCallbd066782011-02-09 08:16:59 +00003884 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003885}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003886
Steve Naroffd54978b2007-09-18 23:55:05 +00003887// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003888Stmt::child_range ObjCMessageExpr::children() {
3889 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003890 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003891 begin = reinterpret_cast<Stmt **>(this + 1);
3892 else
3893 begin = reinterpret_cast<Stmt **>(getArgs());
3894 return child_range(begin,
3895 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003896}
3897
Ted Kremeneke65b0862012-03-06 20:05:56 +00003898ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3899 QualType T, ObjCMethodDecl *Method,
3900 SourceRange SR)
3901 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3902 false, false, false, false),
3903 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3904{
3905 Expr **SaveElements = getElements();
3906 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3907 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3908 ExprBits.ValueDependent = true;
3909 if (Elements[I]->isInstantiationDependent())
3910 ExprBits.InstantiationDependent = true;
3911 if (Elements[I]->containsUnexpandedParameterPack())
3912 ExprBits.ContainsUnexpandedParameterPack = true;
3913
3914 SaveElements[I] = Elements[I];
3915 }
3916}
3917
3918ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3919 llvm::ArrayRef<Expr *> Elements,
3920 QualType T, ObjCMethodDecl * Method,
3921 SourceRange SR) {
3922 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3923 + Elements.size() * sizeof(Expr *));
3924 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3925}
3926
3927ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3928 unsigned NumElements) {
3929
3930 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3931 + NumElements * sizeof(Expr *));
3932 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3933}
3934
3935ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3936 ArrayRef<ObjCDictionaryElement> VK,
3937 bool HasPackExpansions,
3938 QualType T, ObjCMethodDecl *method,
3939 SourceRange SR)
3940 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3941 false, false),
3942 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3943 DictWithObjectsMethod(method)
3944{
3945 KeyValuePair *KeyValues = getKeyValues();
3946 ExpansionData *Expansions = getExpansionData();
3947 for (unsigned I = 0; I < NumElements; I++) {
3948 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3949 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3950 ExprBits.ValueDependent = true;
3951 if (VK[I].Key->isInstantiationDependent() ||
3952 VK[I].Value->isInstantiationDependent())
3953 ExprBits.InstantiationDependent = true;
3954 if (VK[I].EllipsisLoc.isInvalid() &&
3955 (VK[I].Key->containsUnexpandedParameterPack() ||
3956 VK[I].Value->containsUnexpandedParameterPack()))
3957 ExprBits.ContainsUnexpandedParameterPack = true;
3958
3959 KeyValues[I].Key = VK[I].Key;
3960 KeyValues[I].Value = VK[I].Value;
3961 if (Expansions) {
3962 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3963 if (VK[I].NumExpansions)
3964 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3965 else
3966 Expansions[I].NumExpansionsPlusOne = 0;
3967 }
3968 }
3969}
3970
3971ObjCDictionaryLiteral *
3972ObjCDictionaryLiteral::Create(ASTContext &C,
3973 ArrayRef<ObjCDictionaryElement> VK,
3974 bool HasPackExpansions,
3975 QualType T, ObjCMethodDecl *method,
3976 SourceRange SR) {
3977 unsigned ExpansionsSize = 0;
3978 if (HasPackExpansions)
3979 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3980
3981 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3982 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3983 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3984}
3985
3986ObjCDictionaryLiteral *
3987ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3988 bool HasPackExpansions) {
3989 unsigned ExpansionsSize = 0;
3990 if (HasPackExpansions)
3991 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3992 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3993 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3994 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3995 HasPackExpansions);
3996}
3997
3998ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3999 Expr *base,
4000 Expr *key, QualType T,
4001 ObjCMethodDecl *getMethod,
4002 ObjCMethodDecl *setMethod,
4003 SourceLocation RB) {
4004 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4005 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4006 OK_ObjCSubscript,
4007 getMethod, setMethod, RB);
4008}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004009
Benjamin Kramerc215e762012-08-24 11:54:20 +00004010AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004011 QualType t, AtomicOp op, SourceLocation RP)
4012 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4013 false, false, false, false),
Benjamin Kramerc215e762012-08-24 11:54:20 +00004014 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004015{
Benjamin Kramerc215e762012-08-24 11:54:20 +00004016 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4017 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedman8d3e43f2011-10-14 22:48:56 +00004018 if (args[i]->isTypeDependent())
4019 ExprBits.TypeDependent = true;
4020 if (args[i]->isValueDependent())
4021 ExprBits.ValueDependent = true;
4022 if (args[i]->isInstantiationDependent())
4023 ExprBits.InstantiationDependent = true;
4024 if (args[i]->containsUnexpandedParameterPack())
4025 ExprBits.ContainsUnexpandedParameterPack = true;
4026
4027 SubExprs[i] = args[i];
4028 }
4029}
Richard Smithaa22a8c2012-04-10 22:49:28 +00004030
4031unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4032 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00004033 case AO__c11_atomic_init:
4034 case AO__c11_atomic_load:
4035 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004036 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00004037
4038 case AO__c11_atomic_store:
4039 case AO__c11_atomic_exchange:
4040 case AO__atomic_load:
4041 case AO__atomic_store:
4042 case AO__atomic_store_n:
4043 case AO__atomic_exchange_n:
4044 case AO__c11_atomic_fetch_add:
4045 case AO__c11_atomic_fetch_sub:
4046 case AO__c11_atomic_fetch_and:
4047 case AO__c11_atomic_fetch_or:
4048 case AO__c11_atomic_fetch_xor:
4049 case AO__atomic_fetch_add:
4050 case AO__atomic_fetch_sub:
4051 case AO__atomic_fetch_and:
4052 case AO__atomic_fetch_or:
4053 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00004054 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00004055 case AO__atomic_add_fetch:
4056 case AO__atomic_sub_fetch:
4057 case AO__atomic_and_fetch:
4058 case AO__atomic_or_fetch:
4059 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00004060 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004061 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00004062
4063 case AO__atomic_exchange:
4064 return 4;
4065
4066 case AO__c11_atomic_compare_exchange_strong:
4067 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00004068 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00004069
4070 case AO__atomic_compare_exchange:
4071 case AO__atomic_compare_exchange_n:
4072 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00004073 }
4074 llvm_unreachable("unknown atomic op");
4075}