blob: d3f8ac0f824111e8d6bc7770271f41277e7b6b97 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera4d55d82008-10-06 06:40:35 +000014#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000017#include "clang/AST/DeclCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000020#include "clang/AST/EvaluatedExprVisitor.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000028#include "clang/Lex/Lexer.h"
29#include "clang/Lex/LiteralSupport.h"
30#include "clang/Sema/SemaDiagnostic.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000031#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000033#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000034#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Rafael Espindola8d852e32012-06-27 18:18:05 +000037const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000038 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000039
40 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000041 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
42 DerivedType = PTy->getPointeeType();
43
Rafael Espindola251c4492012-07-17 20:24:05 +000044 if (DerivedType->isDependentType())
45 return NULL;
46
Rafael Espindola0b4fe502012-06-26 17:45:31 +000047 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000048 Decl *D = Ty->getDecl();
49 return cast<CXXRecordDecl>(D);
50}
51
Rafael Espindola0a7dd832012-10-27 01:03:43 +000052const Expr *
53Expr::skipRValueSubobjectAdjustments(
54 SmallVectorImpl<SubobjectAdjustment> &Adjustments) const {
55 const Expr *E = this;
56 while (true) {
57 E = E->IgnoreParens();
58
59 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
60 if ((CE->getCastKind() == CK_DerivedToBase ||
61 CE->getCastKind() == CK_UncheckedDerivedToBase) &&
62 E->getType()->isRecordType()) {
63 E = CE->getSubExpr();
64 CXXRecordDecl *Derived
65 = cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
66 Adjustments.push_back(SubobjectAdjustment(CE, Derived));
67 continue;
68 }
69
70 if (CE->getCastKind() == CK_NoOp) {
71 E = CE->getSubExpr();
72 continue;
73 }
74 } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
75 if (!ME->isArrow() && ME->getBase()->isRValue()) {
76 assert(ME->getBase()->getType()->isRecordType());
77 if (FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
78 E = ME->getBase();
79 Adjustments.push_back(SubobjectAdjustment(Field));
80 continue;
81 }
82 }
83 } else if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
84 if (BO->isPtrMemOp()) {
Rafael Espindolaef4b6662012-11-01 14:32:20 +000085 assert(BO->getRHS()->isRValue());
Rafael Espindola0a7dd832012-10-27 01:03:43 +000086 E = BO->getLHS();
87 const MemberPointerType *MPT =
88 BO->getRHS()->getType()->getAs<MemberPointerType>();
89 Adjustments.push_back(SubobjectAdjustment(MPT, BO->getRHS()));
90 }
91 }
92
93 // Nothing changed.
94 break;
95 }
96 return E;
97}
98
99const Expr *
100Expr::findMaterializedTemporary(const MaterializeTemporaryExpr *&MTE) const {
101 const Expr *E = this;
102 // Look through single-element init lists that claim to be lvalues. They're
103 // just syntactic wrappers in this case.
104 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(E)) {
105 if (ILE->getNumInits() == 1 && ILE->isGLValue())
106 E = ILE->getInit(0);
107 }
108
109 // Look through expressions for materialized temporaries (for now).
110 if (const MaterializeTemporaryExpr *M
111 = dyn_cast<MaterializeTemporaryExpr>(E)) {
112 MTE = M;
113 E = M->GetTemporaryExpr();
114 }
115
116 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
117 E = DAE->getExpr();
118 return E;
119}
120
Chris Lattner2b334bb2010-04-16 23:34:13 +0000121/// isKnownToHaveBooleanValue - Return true if this is an integer expression
122/// that is known to return 0 or 1. This happens for _Bool/bool expressions
123/// but also int expressions which are produced by things like comparisons in
124/// C.
125bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +0000126 const Expr *E = IgnoreParens();
127
Chris Lattner2b334bb2010-04-16 23:34:13 +0000128 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +0000129 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +0000130 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +0000131 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +0000132
Peter Collingbournef111d932011-04-15 00:35:48 +0000133 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000134 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000135 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000136 return UO->getSubExpr()->isKnownToHaveBooleanValue();
137 default:
138 return false;
139 }
140 }
Sean Huntc3021132010-05-05 15:23:54 +0000141
John McCall6907fbe2010-06-12 01:56:02 +0000142 // Only look through implicit casts. If the user writes
143 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +0000144 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000145 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000146
Peter Collingbournef111d932011-04-15 00:35:48 +0000147 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +0000148 switch (BO->getOpcode()) {
149 default: return false;
John McCall2de56d12010-08-25 11:45:40 +0000150 case BO_LT: // Relational operators.
151 case BO_GT:
152 case BO_LE:
153 case BO_GE:
154 case BO_EQ: // Equality operators.
155 case BO_NE:
156 case BO_LAnd: // AND operator.
157 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000158 return true;
Sean Huntc3021132010-05-05 15:23:54 +0000159
John McCall2de56d12010-08-25 11:45:40 +0000160 case BO_And: // Bitwise AND operator.
161 case BO_Xor: // Bitwise XOR operator.
162 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +0000163 // Handle things like (x==2)|(y==12).
164 return BO->getLHS()->isKnownToHaveBooleanValue() &&
165 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000166
John McCall2de56d12010-08-25 11:45:40 +0000167 case BO_Comma:
168 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +0000169 return BO->getRHS()->isKnownToHaveBooleanValue();
170 }
171 }
Sean Huntc3021132010-05-05 15:23:54 +0000172
Peter Collingbournef111d932011-04-15 00:35:48 +0000173 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000174 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
175 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000176
Chris Lattner2b334bb2010-04-16 23:34:13 +0000177 return false;
178}
179
John McCall63c00d72011-02-09 08:16:59 +0000180// Amusing macro metaprogramming hack: check whether a class provides
181// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000182//
183// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +0000184namespace {
185 /// This implementation is used when a class provides a custom
186 /// implementation of getExprLoc.
187 template <class E, class T>
188 SourceLocation getExprLocImpl(const Expr *expr,
189 SourceLocation (T::*v)() const) {
190 return static_cast<const E*>(expr)->getExprLoc();
191 }
192
193 /// This implementation is used when a class doesn't provide
194 /// a custom implementation of getExprLoc. Overload resolution
195 /// should pick it over the implementation above because it's
196 /// more specialized according to function template partial ordering.
197 template <class E>
198 SourceLocation getExprLocImpl(const Expr *expr,
199 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000200 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000201 }
202}
203
204SourceLocation Expr::getExprLoc() const {
205 switch (getStmtClass()) {
206 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
207#define ABSTRACT_STMT(type)
208#define STMT(type, base) \
209 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
210#define EXPR(type, base) \
211 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
212#include "clang/AST/StmtNodes.inc"
213 }
214 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000215}
216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217//===----------------------------------------------------------------------===//
218// Primary Expressions.
219//===----------------------------------------------------------------------===//
220
Douglas Gregor561f8122011-07-01 01:22:09 +0000221/// \brief Compute the type-, value-, and instantiation-dependence of a
222/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000223/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000224static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000225 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000226 bool &ValueDependent,
227 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000228 TypeDependent = false;
229 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000230 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000231
232 // (TD) C++ [temp.dep.expr]p3:
233 // An id-expression is type-dependent if it contains:
234 //
Sean Huntc3021132010-05-05 15:23:54 +0000235 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000236 //
237 // (VD) C++ [temp.dep.constexpr]p2:
238 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000239
Douglas Gregor0da76df2009-11-23 11:41:28 +0000240 // (TD) - an identifier that was declared with dependent type
241 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000242 if (T->isDependentType()) {
243 TypeDependent = true;
244 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000245 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000246 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 } else if (T->isInstantiationDependentType()) {
248 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000249 }
Douglas Gregord967e312011-01-19 21:52:31 +0000250
Douglas Gregor0da76df2009-11-23 11:41:28 +0000251 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000252 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000253 == DeclarationName::CXXConversionFunctionName) {
254 QualType T = D->getDeclName().getCXXNameType();
255 if (T->isDependentType()) {
256 TypeDependent = true;
257 ValueDependent = true;
258 InstantiationDependent = true;
259 return;
260 }
261
262 if (T->isInstantiationDependentType())
263 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000264 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000265
Douglas Gregor0da76df2009-11-23 11:41:28 +0000266 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000267 if (isa<NonTypeTemplateParmDecl>(D)) {
268 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000269 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000270 return;
271 }
272
Douglas Gregor0da76df2009-11-23 11:41:28 +0000273 // (VD) - a constant with integral or enumeration type and is
274 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000275 // (VD) - a constant with literal type and is initialized with an
276 // expression that is value-dependent [C++11].
277 // (VD) - FIXME: Missing from the standard:
278 // - an entity with reference type and is initialized with an
279 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000280 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000281 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000282 Var->getType()->isLiteralType() :
283 Var->getType()->isIntegralOrEnumerationType()) &&
David Blaikie4ef832f2012-08-10 00:55:35 +0000284 (Var->getType().isConstQualified() ||
Richard Smithdb1822c2011-11-08 01:31:09 +0000285 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000286 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000287 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000288 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000289 InstantiationDependent = true;
290 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000291 }
292
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000293 // (VD) - FIXME: Missing from the standard:
294 // - a member function or a static data member of the current
295 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000296 if (Var->isStaticDataMember() &&
297 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000298 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000299 InstantiationDependent = true;
300 }
Douglas Gregord967e312011-01-19 21:52:31 +0000301
302 return;
303 }
304
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000305 // (VD) - FIXME: Missing from the standard:
306 // - a member function or a static data member of the current
307 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000308 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
309 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000310 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000311 }
Douglas Gregord967e312011-01-19 21:52:31 +0000312}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000313
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000314void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000315 bool TypeDependent = false;
316 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000317 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000318 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
319 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000320
321 // (TD) C++ [temp.dep.expr]p3:
322 // An id-expression is type-dependent if it contains:
323 //
324 // and
325 //
326 // (VD) C++ [temp.dep.constexpr]p2:
327 // An identifier is value-dependent if it is:
328 if (!TypeDependent && !ValueDependent &&
329 hasExplicitTemplateArgs() &&
330 TemplateSpecializationType::anyDependentTemplateArguments(
331 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000332 getNumTemplateArgs(),
333 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000334 TypeDependent = true;
335 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000336 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000337 }
338
339 ExprBits.TypeDependent = TypeDependent;
340 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000341 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000342
Douglas Gregor10738d32010-12-23 23:51:58 +0000343 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000344 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000345 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000346}
347
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000348DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
349 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000350 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000351 ValueDecl *D, bool RefersToEnclosingLocal,
352 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000353 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000354 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000355 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000356 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000357 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
358 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000359 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000360 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000361 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
362 if (FoundD)
363 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000364 DeclRefExprBits.HasTemplateKWAndArgsInfo
365 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000366 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000367 if (TemplateArgs) {
368 bool Dependent = false;
369 bool InstantiationDependent = false;
370 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000371 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
372 Dependent,
373 InstantiationDependent,
374 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000375 if (InstantiationDependent)
376 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000377 } else if (TemplateKWLoc.isValid()) {
378 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000379 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000380 DeclRefExprBits.HadMultipleCandidates = 0;
381
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000382 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000383}
384
Douglas Gregora2813ce2009-10-23 18:54:35 +0000385DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000386 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000387 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000388 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000389 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000390 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000391 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000392 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000393 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000394 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000395 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000396 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000398 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000399}
400
401DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000402 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000403 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000405 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000406 const DeclarationNameInfo &NameInfo,
407 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000408 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000409 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000410 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000411 // Filter out cases where the found Decl is the same as the value refenenced.
412 if (D == FoundD)
413 FoundD = 0;
414
Douglas Gregora2813ce2009-10-23 18:54:35 +0000415 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000416 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000417 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000418 if (FoundD)
419 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000420 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000421 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
422 else if (TemplateKWLoc.isValid())
423 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000424
Chris Lattner32488542010-10-30 05:14:06 +0000425 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000426 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000427 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000428 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000429}
430
Chandler Carruth3aa81402011-05-01 23:48:14 +0000431DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000432 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000433 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000434 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000435 unsigned NumTemplateArgs) {
436 std::size_t Size = sizeof(DeclRefExpr);
437 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000438 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000439 if (HasFoundDecl)
440 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000441 if (HasTemplateKWAndArgsInfo)
442 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000443
Chris Lattner32488542010-10-30 05:14:06 +0000444 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000445 return new (Mem) DeclRefExpr(EmptyShell());
446}
447
Douglas Gregora2813ce2009-10-23 18:54:35 +0000448SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000449 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000450 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000451 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000452 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000453 R.setEnd(getRAngleLoc());
454 return R;
455}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000456SourceLocation DeclRefExpr::getLocStart() const {
457 if (hasQualifier())
458 return getQualifierLoc().getBeginLoc();
459 return getNameInfo().getLocStart();
460}
461SourceLocation DeclRefExpr::getLocEnd() const {
462 if (hasExplicitTemplateArgs())
463 return getRAngleLoc();
464 return getNameInfo().getLocEnd();
465}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000466
Anders Carlsson3a082d82009-09-08 18:24:21 +0000467// FIXME: Maybe this should use DeclPrinter with a special "print predefined
468// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000469std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
470 ASTContext &Context = CurrentDecl->getASTContext();
471
Anders Carlsson3a082d82009-09-08 18:24:21 +0000472 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000473 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000474 return FD->getNameAsString();
475
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000476 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000477 llvm::raw_svector_ostream Out(Name);
478
479 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000480 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000481 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000482 if (MD->isStatic())
483 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000484 }
485
David Blaikie4e4d0842012-03-11 07:00:24 +0000486 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000487 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000488 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000489
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000490 const FunctionDecl *Decl = FD;
491 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
492 Decl = Pattern;
493 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000494 const FunctionProtoType *FT = 0;
495 if (FD->hasWrittenPrototype())
496 FT = dyn_cast<FunctionProtoType>(AFT);
497
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000498 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000499 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000500 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000501 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000502 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503 }
504
505 if (FT->isVariadic()) {
506 if (FD->getNumParams()) POut << ", ";
507 POut << "...";
508 }
509 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000510 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000511
Sam Weinig4eadcc52009-12-27 01:38:20 +0000512 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000513 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000514 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000515 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000516 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000517 POut << " volatile";
518 RefQualifierKind Ref = MD->getRefQualifier();
519 if (Ref == RQ_LValue)
520 POut << " &";
521 else if (Ref == RQ_RValue)
522 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000523 }
524
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000525 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
526 SpecsTy Specs;
527 const DeclContext *Ctx = FD->getDeclContext();
528 while (Ctx && isa<NamedDecl>(Ctx)) {
529 const ClassTemplateSpecializationDecl *Spec
530 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
531 if (Spec && !Spec->isExplicitSpecialization())
532 Specs.push_back(Spec);
533 Ctx = Ctx->getParent();
534 }
535
536 std::string TemplateParams;
537 llvm::raw_string_ostream TOut(TemplateParams);
538 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
539 I != E; ++I) {
540 const TemplateParameterList *Params
541 = (*I)->getSpecializedTemplate()->getTemplateParameters();
542 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
543 assert(Params->size() == Args.size());
544 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
545 StringRef Param = Params->getParam(i)->getName();
546 if (Param.empty()) continue;
547 TOut << Param << " = ";
548 Args.get(i).print(Policy, TOut);
549 TOut << ", ";
550 }
551 }
552
553 FunctionTemplateSpecializationInfo *FSI
554 = FD->getTemplateSpecializationInfo();
555 if (FSI && !FSI->isExplicitSpecialization()) {
556 const TemplateParameterList* Params
557 = FSI->getTemplate()->getTemplateParameters();
558 const TemplateArgumentList* Args = FSI->TemplateArguments;
559 assert(Params->size() == Args->size());
560 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
561 StringRef Param = Params->getParam(i)->getName();
562 if (Param.empty()) continue;
563 TOut << Param << " = ";
564 Args->get(i).print(Policy, TOut);
565 TOut << ", ";
566 }
567 }
568
569 TOut.flush();
570 if (!TemplateParams.empty()) {
571 // remove the trailing comma and space
572 TemplateParams.resize(TemplateParams.size() - 2);
573 POut << " [" << TemplateParams << "]";
574 }
575
576 POut.flush();
577
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000578 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
579 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000580
581 Out << Proto;
582
583 Out.flush();
584 return Name.str().str();
585 }
586 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000587 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000588 llvm::raw_svector_ostream Out(Name);
589 Out << (MD->isInstanceMethod() ? '-' : '+');
590 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000591
592 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
593 // a null check to avoid a crash.
594 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000595 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000596
Anders Carlsson3a082d82009-09-08 18:24:21 +0000597 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000598 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000599 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000600
Anders Carlsson3a082d82009-09-08 18:24:21 +0000601 Out << ' ';
602 Out << MD->getSelector().getAsString();
603 Out << ']';
604
605 Out.flush();
606 return Name.str().str();
607 }
608 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
609 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
610 return "top level";
611 }
612 return "";
613}
614
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000615void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
616 if (hasAllocation())
617 C.Deallocate(pVal);
618
619 BitWidth = Val.getBitWidth();
620 unsigned NumWords = Val.getNumWords();
621 const uint64_t* Words = Val.getRawData();
622 if (NumWords > 1) {
623 pVal = new (C) uint64_t[NumWords];
624 std::copy(Words, Words + NumWords, pVal);
625 } else if (NumWords == 1)
626 VAL = Words[0];
627 else
628 VAL = 0;
629}
630
Benjamin Kramer478851c2012-07-04 17:04:04 +0000631IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
632 QualType type, SourceLocation l)
633 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
634 false, false),
635 Loc(l) {
636 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
637 assert(V.getBitWidth() == C.getIntWidth(type) &&
638 "Integer type is not the correct size for constant.");
639 setValue(C, V);
640}
641
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000642IntegerLiteral *
643IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
644 QualType type, SourceLocation l) {
645 return new (C) IntegerLiteral(C, V, type, l);
646}
647
648IntegerLiteral *
649IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
650 return new (C) IntegerLiteral(Empty);
651}
652
Benjamin Kramer478851c2012-07-04 17:04:04 +0000653FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
654 bool isexact, QualType Type, SourceLocation L)
655 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
656 false, false), Loc(L) {
657 FloatingLiteralBits.IsIEEE =
658 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
659 FloatingLiteralBits.IsExact = isexact;
660 setValue(C, V);
661}
662
663FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
664 : Expr(FloatingLiteralClass, Empty) {
665 FloatingLiteralBits.IsIEEE =
666 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
667 FloatingLiteralBits.IsExact = false;
668}
669
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000670FloatingLiteral *
671FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
672 bool isexact, QualType Type, SourceLocation L) {
673 return new (C) FloatingLiteral(C, V, isexact, Type, L);
674}
675
676FloatingLiteral *
677FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000678 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000679}
680
Chris Lattnerda8249e2008-06-07 22:13:43 +0000681/// getValueAsApproximateDouble - This returns the value as an inaccurate
682/// double. Note that this may cause loss of precision, but is useful for
683/// debugging dumps, etc.
684double FloatingLiteral::getValueAsApproximateDouble() const {
685 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000686 bool ignored;
687 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
688 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000689 return V.convertToDouble();
690}
691
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000692int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000693 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000694 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000695 case Ascii:
696 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000697 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000698 break;
699 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000700 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000701 break;
702 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000703 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000704 break;
705 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000706 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000707 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000708 }
709 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
710 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000711 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000712 && "character byte widths supported are 1, 2, and 4 only");
713 return CharByteWidth;
714}
715
Chris Lattner5f9e2722011-07-23 10:55:15 +0000716StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000717 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000718 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000719 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000720 // Allocate enough space for the StringLiteral plus an array of locations for
721 // any concatenated string tokens.
722 void *Mem = C.Allocate(sizeof(StringLiteral)+
723 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000724 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000725 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000728 SL->setString(C,Str,Kind,Pascal);
729
Chris Lattner2085fd62009-02-18 06:40:38 +0000730 SL->TokLocs[0] = Loc[0];
731 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Chris Lattner726e1682009-02-18 05:49:11 +0000733 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000734 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
735 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000736}
737
Douglas Gregor673ecd62009-04-15 16:35:07 +0000738StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
739 void *Mem = C.Allocate(sizeof(StringLiteral)+
740 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000741 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000742 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000743 SL->CharByteWidth = 0;
744 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000745 SL->NumConcatenated = NumStrs;
746 return SL;
747}
748
Richard Trieu8ab09da2012-06-13 20:25:24 +0000749void StringLiteral::outputString(raw_ostream &OS) {
750 switch (getKind()) {
751 case Ascii: break; // no prefix.
752 case Wide: OS << 'L'; break;
753 case UTF8: OS << "u8"; break;
754 case UTF16: OS << 'u'; break;
755 case UTF32: OS << 'U'; break;
756 }
757 OS << '"';
758 static const char Hex[] = "0123456789ABCDEF";
759
760 unsigned LastSlashX = getLength();
761 for (unsigned I = 0, N = getLength(); I != N; ++I) {
762 switch (uint32_t Char = getCodeUnit(I)) {
763 default:
764 // FIXME: Convert UTF-8 back to codepoints before rendering.
765
766 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
767 // Leave invalid surrogates alone; we'll use \x for those.
768 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
769 Char <= 0xdbff) {
770 uint32_t Trail = getCodeUnit(I + 1);
771 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
772 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
773 ++I;
774 }
775 }
776
777 if (Char > 0xff) {
778 // If this is a wide string, output characters over 0xff using \x
779 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
780 // codepoint: use \x escapes for invalid codepoints.
781 if (getKind() == Wide ||
782 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
783 // FIXME: Is this the best way to print wchar_t?
784 OS << "\\x";
785 int Shift = 28;
786 while ((Char >> Shift) == 0)
787 Shift -= 4;
788 for (/**/; Shift >= 0; Shift -= 4)
789 OS << Hex[(Char >> Shift) & 15];
790 LastSlashX = I;
791 break;
792 }
793
794 if (Char > 0xffff)
795 OS << "\\U00"
796 << Hex[(Char >> 20) & 15]
797 << Hex[(Char >> 16) & 15];
798 else
799 OS << "\\u";
800 OS << Hex[(Char >> 12) & 15]
801 << Hex[(Char >> 8) & 15]
802 << Hex[(Char >> 4) & 15]
803 << Hex[(Char >> 0) & 15];
804 break;
805 }
806
807 // If we used \x... for the previous character, and this character is a
808 // hexadecimal digit, prevent it being slurped as part of the \x.
809 if (LastSlashX + 1 == I) {
810 switch (Char) {
811 case '0': case '1': case '2': case '3': case '4':
812 case '5': case '6': case '7': case '8': case '9':
813 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
814 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
815 OS << "\"\"";
816 }
817 }
818
819 assert(Char <= 0xff &&
820 "Characters above 0xff should already have been handled.");
821
822 if (isprint(Char))
823 OS << (char)Char;
824 else // Output anything hard as an octal escape.
825 OS << '\\'
826 << (char)('0' + ((Char >> 6) & 7))
827 << (char)('0' + ((Char >> 3) & 7))
828 << (char)('0' + ((Char >> 0) & 7));
829 break;
830 // Handle some common non-printable cases to make dumps prettier.
831 case '\\': OS << "\\\\"; break;
832 case '"': OS << "\\\""; break;
833 case '\n': OS << "\\n"; break;
834 case '\t': OS << "\\t"; break;
835 case '\a': OS << "\\a"; break;
836 case '\b': OS << "\\b"; break;
837 }
838 }
839 OS << '"';
840}
841
Eli Friedman64f45a22011-11-01 02:23:42 +0000842void StringLiteral::setString(ASTContext &C, StringRef Str,
843 StringKind Kind, bool IsPascal) {
844 //FIXME: we assume that the string data comes from a target that uses the same
845 // code unit size and endianess for the type of string.
846 this->Kind = Kind;
847 this->IsPascal = IsPascal;
848
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000849 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000850 assert((Str.size()%CharByteWidth == 0)
851 && "size of data must be multiple of CharByteWidth");
852 Length = Str.size()/CharByteWidth;
853
854 switch(CharByteWidth) {
855 case 1: {
856 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000857 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000858 StrData.asChar = AStrData;
859 break;
860 }
861 case 2: {
862 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000863 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000864 StrData.asUInt16 = AStrData;
865 break;
866 }
867 case 4: {
868 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000869 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000870 StrData.asUInt32 = AStrData;
871 break;
872 }
873 default:
874 assert(false && "unsupported CharByteWidth");
875 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000876}
877
Chris Lattner08f92e32010-11-17 07:37:15 +0000878/// getLocationOfByte - Return a source location that points to the specified
879/// byte of this string literal.
880///
881/// Strings are amazingly complex. They can be formed from multiple tokens and
882/// can have escape sequences in them in addition to the usual trigraph and
883/// escaped newline business. This routine handles this complexity.
884///
885SourceLocation StringLiteral::
886getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
887 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000888 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
889 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000890
Chris Lattner08f92e32010-11-17 07:37:15 +0000891 // Loop over all of the tokens in this string until we find the one that
892 // contains the byte we're looking for.
893 unsigned TokNo = 0;
894 while (1) {
895 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
896 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
897
898 // Get the spelling of the string so that we can get the data that makes up
899 // the string literal, not the identifier for the macro it is potentially
900 // expanded through.
901 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
902
903 // Re-lex the token to get its length and original spelling.
904 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
905 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000906 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000907 if (Invalid)
908 return StrTokSpellingLoc;
909
910 const char *StrData = Buffer.data()+LocInfo.second;
911
Chris Lattner08f92e32010-11-17 07:37:15 +0000912 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000913 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
914 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000915 Token TheTok;
916 TheLexer.LexFromRawLexer(TheTok);
917
918 // Use the StringLiteralParser to compute the length of the string in bytes.
919 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
920 unsigned TokNumBytes = SLP.GetStringLength();
921
922 // If the byte is in this token, return the location of the byte.
923 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000924 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000925 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
926
927 // Now that we know the offset of the token in the spelling, use the
928 // preprocessor to get the offset in the original source.
929 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
930 }
931
932 // Move to the next string token.
933 ++TokNo;
934 ByteNo -= TokNumBytes;
935 }
936}
937
938
939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
941/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000942StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000944 case UO_PostInc: return "++";
945 case UO_PostDec: return "--";
946 case UO_PreInc: return "++";
947 case UO_PreDec: return "--";
948 case UO_AddrOf: return "&";
949 case UO_Deref: return "*";
950 case UO_Plus: return "+";
951 case UO_Minus: return "-";
952 case UO_Not: return "~";
953 case UO_LNot: return "!";
954 case UO_Real: return "__real";
955 case UO_Imag: return "__imag";
956 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000958 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000959}
960
John McCall2de56d12010-08-25 11:45:40 +0000961UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000962UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
963 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000964 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000965 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
966 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
967 case OO_Amp: return UO_AddrOf;
968 case OO_Star: return UO_Deref;
969 case OO_Plus: return UO_Plus;
970 case OO_Minus: return UO_Minus;
971 case OO_Tilde: return UO_Not;
972 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000973 }
974}
975
976OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
977 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000978 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
979 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
980 case UO_AddrOf: return OO_Amp;
981 case UO_Deref: return OO_Star;
982 case UO_Plus: return OO_Plus;
983 case UO_Minus: return OO_Minus;
984 case UO_Not: return OO_Tilde;
985 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000986 default: return OO_None;
987 }
988}
989
990
Reid Spencer5f016e22007-07-11 17:01:13 +0000991//===----------------------------------------------------------------------===//
992// Postfix Operators.
993//===----------------------------------------------------------------------===//
994
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000995CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000996 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000997 SourceLocation rparenloc)
998 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000999 fn->isTypeDependent(),
1000 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001001 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001002 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001003 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001005 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +00001006 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001007 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001008 if (args[i]->isTypeDependent())
1009 ExprBits.TypeDependent = true;
1010 if (args[i]->isValueDependent())
1011 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001012 if (args[i]->isInstantiationDependent())
1013 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001014 if (args[i]->containsUnexpandedParameterPack())
1015 ExprBits.ContainsUnexpandedParameterPack = true;
1016
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001017 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001018 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001019
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001020 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001021 RParenLoc = rparenloc;
1022}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001023
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001024CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001025 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1026 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001027 fn->isTypeDependent(),
1028 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001029 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001030 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001031 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001032
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001033 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001034 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001035 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001036 if (args[i]->isTypeDependent())
1037 ExprBits.TypeDependent = true;
1038 if (args[i]->isValueDependent())
1039 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001040 if (args[i]->isInstantiationDependent())
1041 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001042 if (args[i]->containsUnexpandedParameterPack())
1043 ExprBits.ContainsUnexpandedParameterPack = true;
1044
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001045 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001046 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001047
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001048 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 RParenLoc = rparenloc;
1050}
1051
Mike Stump1eb44332009-09-09 15:08:12 +00001052CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1053 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001054 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001055 SubExprs = new (C) Stmt*[PREARGS_START];
1056 CallExprBits.NumPreArgs = 0;
1057}
1058
1059CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1060 EmptyShell Empty)
1061 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1062 // FIXME: Why do we allocate this?
1063 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1064 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001065}
1066
Nuno Lopesd20254f2009-12-20 23:11:08 +00001067Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001068 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001069
1070 while (SubstNonTypeTemplateParmExpr *NTTP
1071 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1072 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1073 }
1074
Sebastian Redl20012152010-09-10 20:55:30 +00001075 // If we're calling a dereference, look at the pointer instead.
1076 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1077 if (BO->isPtrMemOp())
1078 CEE = BO->getRHS()->IgnoreParenCasts();
1079 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1080 if (UO->getOpcode() == UO_Deref)
1081 CEE = UO->getSubExpr()->IgnoreParenCasts();
1082 }
Chris Lattner6346f962009-07-17 15:46:27 +00001083 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001084 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001085 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1086 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001087
1088 return 0;
1089}
1090
Nuno Lopesd20254f2009-12-20 23:11:08 +00001091FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001092 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001093}
1094
Chris Lattnerd18b3292007-12-28 05:25:02 +00001095/// setNumArgs - This changes the number of arguments present in this call.
1096/// Any orphaned expressions are deleted by this, and any new operands are set
1097/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001098void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001099 // No change, just return.
1100 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattnerd18b3292007-12-28 05:25:02 +00001102 // If shrinking # arguments, just delete the extras and forgot them.
1103 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001104 this->NumArgs = NumArgs;
1105 return;
1106 }
1107
1108 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001109 unsigned NumPreArgs = getNumPreArgs();
1110 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001111 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001112 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001113 NewSubExprs[i] = SubExprs[i];
1114 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001115 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1116 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001117 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor88c9a462009-04-17 21:46:47 +00001119 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001120 SubExprs = NewSubExprs;
1121 this->NumArgs = NumArgs;
1122}
1123
Chris Lattnercb888962008-10-06 05:00:53 +00001124/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1125/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001126unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001127 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001128 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001129 // ImplicitCastExpr.
1130 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1131 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001132 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001134 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1135 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001136 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Anders Carlssonbcba2012008-01-31 02:13:57 +00001138 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1139 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001140 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001142 if (!FDecl->getIdentifier())
1143 return 0;
1144
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001145 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001146}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001147
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001148QualType CallExpr::getCallReturnType() const {
1149 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001150 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001151 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001152 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001153 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001154 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1155 // This should never be overloaded and so should never return null.
1156 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001157
John McCall864c0412011-04-26 20:42:42 +00001158 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001159 return FnType->getResultType();
1160}
Chris Lattnercb888962008-10-06 05:00:53 +00001161
John McCall2882eca2011-02-21 06:23:05 +00001162SourceRange CallExpr::getSourceRange() const {
1163 if (isa<CXXOperatorCallExpr>(this))
1164 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
1165
1166 SourceLocation begin = getCallee()->getLocStart();
1167 if (begin.isInvalid() && getNumArgs() > 0)
1168 begin = getArg(0)->getLocStart();
1169 SourceLocation end = getRParenLoc();
1170 if (end.isInvalid() && getNumArgs() > 0)
1171 end = getArg(getNumArgs() - 1)->getLocEnd();
1172 return SourceRange(begin, end);
1173}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001174SourceLocation CallExpr::getLocStart() const {
1175 if (isa<CXXOperatorCallExpr>(this))
1176 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
1177
1178 SourceLocation begin = getCallee()->getLocStart();
1179 if (begin.isInvalid() && getNumArgs() > 0)
1180 begin = getArg(0)->getLocStart();
1181 return begin;
1182}
1183SourceLocation CallExpr::getLocEnd() const {
1184 if (isa<CXXOperatorCallExpr>(this))
1185 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
1186
1187 SourceLocation end = getRParenLoc();
1188 if (end.isInvalid() && getNumArgs() > 0)
1189 end = getArg(getNumArgs() - 1)->getLocEnd();
1190 return end;
1191}
John McCall2882eca2011-02-21 06:23:05 +00001192
Sean Huntc3021132010-05-05 15:23:54 +00001193OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001194 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001195 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001196 ArrayRef<OffsetOfNode> comps,
1197 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001198 SourceLocation RParenLoc) {
1199 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001200 sizeof(OffsetOfNode) * comps.size() +
1201 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001202
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001203 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1204 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001205}
1206
1207OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1208 unsigned numComps, unsigned numExprs) {
1209 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1210 sizeof(OffsetOfNode) * numComps +
1211 sizeof(Expr*) * numExprs);
1212 return new (Mem) OffsetOfExpr(numComps, numExprs);
1213}
1214
Sean Huntc3021132010-05-05 15:23:54 +00001215OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001216 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001217 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001218 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001219 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1220 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001221 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001222 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001223 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001224 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001225 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001226{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001227 for (unsigned i = 0; i != comps.size(); ++i) {
1228 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001229 }
Sean Huntc3021132010-05-05 15:23:54 +00001230
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001231 for (unsigned i = 0; i != exprs.size(); ++i) {
1232 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001233 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001234 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001235 ExprBits.ContainsUnexpandedParameterPack = true;
1236
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001237 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001238 }
1239}
1240
1241IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1242 assert(getKind() == Field || getKind() == Identifier);
1243 if (getKind() == Field)
1244 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001245
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001246 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1247}
1248
Mike Stump1eb44332009-09-09 15:08:12 +00001249MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001250 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001251 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001252 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001253 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001254 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001255 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001256 QualType ty,
1257 ExprValueKind vk,
1258 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001259 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001260
Douglas Gregor40d96a62011-02-28 21:54:11 +00001261 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001262 founddecl.getDecl() != memberdecl ||
1263 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001264 if (hasQualOrFound)
1265 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
John McCalld5532b62009-11-23 01:53:49 +00001267 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001268 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1269 else if (TemplateKWLoc.isValid())
1270 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Chris Lattner32488542010-10-30 05:14:06 +00001272 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001273 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1274 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001275
1276 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001277 // FIXME: Wrong. We should be looking at the member declaration we found.
1278 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001279 E->setValueDependent(true);
1280 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001281 E->setInstantiationDependent(true);
1282 }
1283 else if (QualifierLoc &&
1284 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1285 E->setInstantiationDependent(true);
1286
John McCall6bb80172010-03-30 21:47:33 +00001287 E->HasQualifierOrFoundDecl = true;
1288
1289 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001290 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001291 NQ->FoundDecl = founddecl;
1292 }
1293
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001294 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1295
John McCall6bb80172010-03-30 21:47:33 +00001296 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001297 bool Dependent = false;
1298 bool InstantiationDependent = false;
1299 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001300 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1301 Dependent,
1302 InstantiationDependent,
1303 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001304 if (InstantiationDependent)
1305 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001306 } else if (TemplateKWLoc.isValid()) {
1307 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001308 }
1309
1310 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001311}
1312
Douglas Gregor75e85042011-03-02 21:06:53 +00001313SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001314 return SourceRange(getLocStart(), getLocEnd());
1315}
1316SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001317 if (isImplicitAccess()) {
1318 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001319 return getQualifierLoc().getBeginLoc();
1320 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001321 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001322
Daniel Dunbar396ec672012-03-09 15:39:15 +00001323 // FIXME: We don't want this to happen. Rather, we should be able to
1324 // detect all kinds of implicit accesses more cleanly.
1325 SourceLocation BaseStartLoc = getBase()->getLocStart();
1326 if (BaseStartLoc.isValid())
1327 return BaseStartLoc;
1328 return MemberLoc;
1329}
1330SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001331 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001332 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001333 EndLoc = getRAngleLoc();
1334 else if (EndLoc.isInvalid())
1335 EndLoc = getBase()->getLocEnd();
1336 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001337}
1338
John McCall1d9b3b22011-09-09 05:25:32 +00001339void CastExpr::CheckCastConsistency() const {
1340 switch (getCastKind()) {
1341 case CK_DerivedToBase:
1342 case CK_UncheckedDerivedToBase:
1343 case CK_DerivedToBaseMemberPointer:
1344 case CK_BaseToDerived:
1345 case CK_BaseToDerivedMemberPointer:
1346 assert(!path_empty() && "Cast kind should have a base path!");
1347 break;
1348
1349 case CK_CPointerToObjCPointerCast:
1350 assert(getType()->isObjCObjectPointerType());
1351 assert(getSubExpr()->getType()->isPointerType());
1352 goto CheckNoBasePath;
1353
1354 case CK_BlockPointerToObjCPointerCast:
1355 assert(getType()->isObjCObjectPointerType());
1356 assert(getSubExpr()->getType()->isBlockPointerType());
1357 goto CheckNoBasePath;
1358
John McCall4d4e5c12012-02-15 01:22:51 +00001359 case CK_ReinterpretMemberPointer:
1360 assert(getType()->isMemberPointerType());
1361 assert(getSubExpr()->getType()->isMemberPointerType());
1362 goto CheckNoBasePath;
1363
John McCall1d9b3b22011-09-09 05:25:32 +00001364 case CK_BitCast:
1365 // Arbitrary casts to C pointer types count as bitcasts.
1366 // Otherwise, we should only have block and ObjC pointer casts
1367 // here if they stay within the type kind.
1368 if (!getType()->isPointerType()) {
1369 assert(getType()->isObjCObjectPointerType() ==
1370 getSubExpr()->getType()->isObjCObjectPointerType());
1371 assert(getType()->isBlockPointerType() ==
1372 getSubExpr()->getType()->isBlockPointerType());
1373 }
1374 goto CheckNoBasePath;
1375
1376 case CK_AnyPointerToBlockPointerCast:
1377 assert(getType()->isBlockPointerType());
1378 assert(getSubExpr()->getType()->isAnyPointerType() &&
1379 !getSubExpr()->getType()->isBlockPointerType());
1380 goto CheckNoBasePath;
1381
Douglas Gregorac1303e2012-02-22 05:02:47 +00001382 case CK_CopyAndAutoreleaseBlockObject:
1383 assert(getType()->isBlockPointerType());
1384 assert(getSubExpr()->getType()->isBlockPointerType());
1385 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001386
1387 case CK_FunctionToPointerDecay:
1388 assert(getType()->isPointerType());
1389 assert(getSubExpr()->getType()->isFunctionType());
1390 goto CheckNoBasePath;
1391
John McCall1d9b3b22011-09-09 05:25:32 +00001392 // These should not have an inheritance path.
1393 case CK_Dynamic:
1394 case CK_ToUnion:
1395 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001396 case CK_NullToMemberPointer:
1397 case CK_NullToPointer:
1398 case CK_ConstructorConversion:
1399 case CK_IntegralToPointer:
1400 case CK_PointerToIntegral:
1401 case CK_ToVoid:
1402 case CK_VectorSplat:
1403 case CK_IntegralCast:
1404 case CK_IntegralToFloating:
1405 case CK_FloatingToIntegral:
1406 case CK_FloatingCast:
1407 case CK_ObjCObjectLValueCast:
1408 case CK_FloatingRealToComplex:
1409 case CK_FloatingComplexToReal:
1410 case CK_FloatingComplexCast:
1411 case CK_FloatingComplexToIntegralComplex:
1412 case CK_IntegralRealToComplex:
1413 case CK_IntegralComplexToReal:
1414 case CK_IntegralComplexCast:
1415 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001416 case CK_ARCProduceObject:
1417 case CK_ARCConsumeObject:
1418 case CK_ARCReclaimReturnedObject:
1419 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001420 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1421 goto CheckNoBasePath;
1422
1423 case CK_Dependent:
1424 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001425 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001426 case CK_AtomicToNonAtomic:
1427 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001428 case CK_PointerToBoolean:
1429 case CK_IntegralToBoolean:
1430 case CK_FloatingToBoolean:
1431 case CK_MemberPointerToBoolean:
1432 case CK_FloatingComplexToBoolean:
1433 case CK_IntegralComplexToBoolean:
1434 case CK_LValueBitCast: // -> bool&
1435 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001436 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001437 CheckNoBasePath:
1438 assert(path_empty() && "Cast kind should not have a base path!");
1439 break;
1440 }
1441}
1442
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001443const char *CastExpr::getCastKindName() const {
1444 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001445 case CK_Dependent:
1446 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001447 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001448 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001449 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001450 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001451 case CK_LValueToRValue:
1452 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001453 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001454 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001455 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001456 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001457 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001458 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001459 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001460 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001461 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001462 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001463 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001464 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001465 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001466 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001467 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001468 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001469 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001470 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001471 case CK_NullToPointer:
1472 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001473 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001474 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001475 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001476 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001477 case CK_ReinterpretMemberPointer:
1478 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001479 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001480 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001481 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001482 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001483 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001484 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001485 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001486 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001487 case CK_PointerToBoolean:
1488 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001489 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001490 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001491 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001492 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001493 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001494 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001495 case CK_IntegralToBoolean:
1496 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001497 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001498 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001499 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001500 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001501 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001502 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001503 case CK_FloatingToBoolean:
1504 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001505 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001506 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001507 case CK_CPointerToObjCPointerCast:
1508 return "CPointerToObjCPointerCast";
1509 case CK_BlockPointerToObjCPointerCast:
1510 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001511 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001512 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001513 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001514 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001515 case CK_FloatingRealToComplex:
1516 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001517 case CK_FloatingComplexToReal:
1518 return "FloatingComplexToReal";
1519 case CK_FloatingComplexToBoolean:
1520 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001521 case CK_FloatingComplexCast:
1522 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001523 case CK_FloatingComplexToIntegralComplex:
1524 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001525 case CK_IntegralRealToComplex:
1526 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001527 case CK_IntegralComplexToReal:
1528 return "IntegralComplexToReal";
1529 case CK_IntegralComplexToBoolean:
1530 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001531 case CK_IntegralComplexCast:
1532 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001533 case CK_IntegralComplexToFloatingComplex:
1534 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001535 case CK_ARCConsumeObject:
1536 return "ARCConsumeObject";
1537 case CK_ARCProduceObject:
1538 return "ARCProduceObject";
1539 case CK_ARCReclaimReturnedObject:
1540 return "ARCReclaimReturnedObject";
1541 case CK_ARCExtendBlockObject:
1542 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001543 case CK_AtomicToNonAtomic:
1544 return "AtomicToNonAtomic";
1545 case CK_NonAtomicToAtomic:
1546 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001547 case CK_CopyAndAutoreleaseBlockObject:
1548 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001549 case CK_BuiltinFnToFnPtr:
1550 return "BuiltinFnToFnPtr";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
John McCall2bb5d002010-11-13 09:02:35 +00001553 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001554}
1555
Douglas Gregor6eef5192009-12-14 19:27:10 +00001556Expr *CastExpr::getSubExprAsWritten() {
1557 Expr *SubExpr = 0;
1558 CastExpr *E = this;
1559 do {
1560 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001561
1562 // Skip through reference binding to temporary.
1563 if (MaterializeTemporaryExpr *Materialize
1564 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1565 SubExpr = Materialize->GetTemporaryExpr();
1566
Douglas Gregor6eef5192009-12-14 19:27:10 +00001567 // Skip any temporary bindings; they're implicit.
1568 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1569 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001570
Douglas Gregor6eef5192009-12-14 19:27:10 +00001571 // Conversions by constructor and conversion functions have a
1572 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001573 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001574 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001575 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001576 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001577
Douglas Gregor6eef5192009-12-14 19:27:10 +00001578 // If the subexpression we're left with is an implicit cast, look
1579 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001580 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1581
Douglas Gregor6eef5192009-12-14 19:27:10 +00001582 return SubExpr;
1583}
1584
John McCallf871d0c2010-08-07 06:22:56 +00001585CXXBaseSpecifier **CastExpr::path_buffer() {
1586 switch (getStmtClass()) {
1587#define ABSTRACT_STMT(x)
1588#define CASTEXPR(Type, Base) \
1589 case Stmt::Type##Class: \
1590 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1591#define STMT(Type, Base)
1592#include "clang/AST/StmtNodes.inc"
1593 default:
1594 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001595 }
1596}
1597
1598void CastExpr::setCastPath(const CXXCastPath &Path) {
1599 assert(Path.size() == path_size());
1600 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1601}
1602
1603ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1604 CastKind Kind, Expr *Operand,
1605 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001606 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001607 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1608 void *Buffer =
1609 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1610 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001611 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001612 if (PathSize) E->setCastPath(*BasePath);
1613 return E;
1614}
1615
1616ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1617 unsigned PathSize) {
1618 void *Buffer =
1619 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1620 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1621}
1622
1623
1624CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001625 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001626 const CXXCastPath *BasePath,
1627 TypeSourceInfo *WrittenTy,
1628 SourceLocation L, SourceLocation R) {
1629 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1630 void *Buffer =
1631 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1632 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001633 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001634 if (PathSize) E->setCastPath(*BasePath);
1635 return E;
1636}
1637
1638CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1639 void *Buffer =
1640 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1641 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1642}
1643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1645/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001646StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001648 case BO_PtrMemD: return ".*";
1649 case BO_PtrMemI: return "->*";
1650 case BO_Mul: return "*";
1651 case BO_Div: return "/";
1652 case BO_Rem: return "%";
1653 case BO_Add: return "+";
1654 case BO_Sub: return "-";
1655 case BO_Shl: return "<<";
1656 case BO_Shr: return ">>";
1657 case BO_LT: return "<";
1658 case BO_GT: return ">";
1659 case BO_LE: return "<=";
1660 case BO_GE: return ">=";
1661 case BO_EQ: return "==";
1662 case BO_NE: return "!=";
1663 case BO_And: return "&";
1664 case BO_Xor: return "^";
1665 case BO_Or: return "|";
1666 case BO_LAnd: return "&&";
1667 case BO_LOr: return "||";
1668 case BO_Assign: return "=";
1669 case BO_MulAssign: return "*=";
1670 case BO_DivAssign: return "/=";
1671 case BO_RemAssign: return "%=";
1672 case BO_AddAssign: return "+=";
1673 case BO_SubAssign: return "-=";
1674 case BO_ShlAssign: return "<<=";
1675 case BO_ShrAssign: return ">>=";
1676 case BO_AndAssign: return "&=";
1677 case BO_XorAssign: return "^=";
1678 case BO_OrAssign: return "|=";
1679 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001681
David Blaikie30263482012-01-20 21:50:17 +00001682 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001683}
1684
John McCall2de56d12010-08-25 11:45:40 +00001685BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001686BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1687 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001688 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001689 case OO_Plus: return BO_Add;
1690 case OO_Minus: return BO_Sub;
1691 case OO_Star: return BO_Mul;
1692 case OO_Slash: return BO_Div;
1693 case OO_Percent: return BO_Rem;
1694 case OO_Caret: return BO_Xor;
1695 case OO_Amp: return BO_And;
1696 case OO_Pipe: return BO_Or;
1697 case OO_Equal: return BO_Assign;
1698 case OO_Less: return BO_LT;
1699 case OO_Greater: return BO_GT;
1700 case OO_PlusEqual: return BO_AddAssign;
1701 case OO_MinusEqual: return BO_SubAssign;
1702 case OO_StarEqual: return BO_MulAssign;
1703 case OO_SlashEqual: return BO_DivAssign;
1704 case OO_PercentEqual: return BO_RemAssign;
1705 case OO_CaretEqual: return BO_XorAssign;
1706 case OO_AmpEqual: return BO_AndAssign;
1707 case OO_PipeEqual: return BO_OrAssign;
1708 case OO_LessLess: return BO_Shl;
1709 case OO_GreaterGreater: return BO_Shr;
1710 case OO_LessLessEqual: return BO_ShlAssign;
1711 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1712 case OO_EqualEqual: return BO_EQ;
1713 case OO_ExclaimEqual: return BO_NE;
1714 case OO_LessEqual: return BO_LE;
1715 case OO_GreaterEqual: return BO_GE;
1716 case OO_AmpAmp: return BO_LAnd;
1717 case OO_PipePipe: return BO_LOr;
1718 case OO_Comma: return BO_Comma;
1719 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001720 }
1721}
1722
1723OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1724 static const OverloadedOperatorKind OverOps[] = {
1725 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1726 OO_Star, OO_Slash, OO_Percent,
1727 OO_Plus, OO_Minus,
1728 OO_LessLess, OO_GreaterGreater,
1729 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1730 OO_EqualEqual, OO_ExclaimEqual,
1731 OO_Amp,
1732 OO_Caret,
1733 OO_Pipe,
1734 OO_AmpAmp,
1735 OO_PipePipe,
1736 OO_Equal, OO_StarEqual,
1737 OO_SlashEqual, OO_PercentEqual,
1738 OO_PlusEqual, OO_MinusEqual,
1739 OO_LessLessEqual, OO_GreaterGreaterEqual,
1740 OO_AmpEqual, OO_CaretEqual,
1741 OO_PipeEqual,
1742 OO_Comma
1743 };
1744 return OverOps[Opc];
1745}
1746
Ted Kremenek709210f2010-04-13 23:39:13 +00001747InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001748 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001749 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001750 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001751 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001752 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001753{
1754 sawArrayRangeDesignator(false);
1755 setInitializesStdInitializerList(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001756 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001757 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001758 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001759 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001760 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001761 if (initExprs[I]->isInstantiationDependent())
1762 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001763 if (initExprs[I]->containsUnexpandedParameterPack())
1764 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001765 }
Sean Huntc3021132010-05-05 15:23:54 +00001766
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001767 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001768}
Reid Spencer5f016e22007-07-11 17:01:13 +00001769
Ted Kremenek709210f2010-04-13 23:39:13 +00001770void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001771 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001772 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001773}
1774
Ted Kremenek709210f2010-04-13 23:39:13 +00001775void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001776 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001777}
1778
Ted Kremenek709210f2010-04-13 23:39:13 +00001779Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001780 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001781 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001782 InitExprs.back() = expr;
1783 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Douglas Gregor4c678342009-01-28 21:54:33 +00001786 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1787 InitExprs[Init] = expr;
1788 return Result;
1789}
1790
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001791void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001792 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001793 ArrayFillerOrUnionFieldInit = filler;
1794 // Fill out any "holes" in the array due to designated initializers.
1795 Expr **inits = getInits();
1796 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1797 if (inits[i] == 0)
1798 inits[i] = filler;
1799}
1800
Richard Smithfe587202012-04-15 02:50:59 +00001801bool InitListExpr::isStringLiteralInit() const {
1802 if (getNumInits() != 1)
1803 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001804 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1805 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001806 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001807 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001808 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1809}
1810
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001811SourceRange InitListExpr::getSourceRange() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001812 if (InitListExpr *SyntacticForm = getSyntacticForm())
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001813 return SyntacticForm->getSourceRange();
1814 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1815 if (Beg.isInvalid()) {
1816 // Find the first non-null initializer.
1817 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1818 E = InitExprs.end();
1819 I != E; ++I) {
1820 if (Stmt *S = *I) {
1821 Beg = S->getLocStart();
1822 break;
1823 }
1824 }
1825 }
1826 if (End.isInvalid()) {
1827 // Find the first non-null initializer from the end.
1828 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1829 E = InitExprs.rend();
1830 I != E; ++I) {
1831 if (Stmt *S = *I) {
1832 End = S->getSourceRange().getEnd();
1833 break;
1834 }
1835 }
1836 }
1837 return SourceRange(Beg, End);
1838}
1839
Steve Naroffbfdcae62008-09-04 15:31:07 +00001840/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001841///
John McCalla345edb2012-02-17 03:32:35 +00001842const FunctionProtoType *BlockExpr::getFunctionType() const {
1843 // The block pointer is never sugared, but the function type might be.
1844 return cast<BlockPointerType>(getType())
1845 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001846}
1847
Mike Stump1eb44332009-09-09 15:08:12 +00001848SourceLocation BlockExpr::getCaretLocation() const {
1849 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001850}
Mike Stump1eb44332009-09-09 15:08:12 +00001851const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001852 return TheBlock->getBody();
1853}
Mike Stump1eb44332009-09-09 15:08:12 +00001854Stmt *BlockExpr::getBody() {
1855 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001856}
Steve Naroff56ee6892008-10-08 17:01:13 +00001857
1858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859//===----------------------------------------------------------------------===//
1860// Generic Expression Routines
1861//===----------------------------------------------------------------------===//
1862
Chris Lattner026dc962009-02-14 07:37:35 +00001863/// isUnusedResultAWarning - Return true if this immediate expression should
1864/// be warned about if the result is unused. If so, fill in Loc and Ranges
1865/// with location to warn on and the source range[s] to report with the
1866/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001867bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1868 SourceRange &R1, SourceRange &R2,
1869 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001870 // Don't warn if the expr is type dependent. The type could end up
1871 // instantiating to void.
1872 if (isTypeDependent())
1873 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 switch (getStmtClass()) {
1876 default:
John McCall0faede62010-03-12 07:11:26 +00001877 if (getType()->isVoidType())
1878 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001879 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001880 Loc = getExprLoc();
1881 R1 = getSourceRange();
1882 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001883 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001884 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001885 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001886 case GenericSelectionExprClass:
1887 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001888 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 case UnaryOperatorClass: {
1890 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001893 case UO_Plus:
1894 case UO_Minus:
1895 case UO_AddrOf:
1896 case UO_Not:
1897 case UO_LNot:
1898 case UO_Deref:
1899 break;
John McCall2de56d12010-08-25 11:45:40 +00001900 case UO_PostInc:
1901 case UO_PostDec:
1902 case UO_PreInc:
1903 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001904 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001905 case UO_Real:
1906 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001908 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1909 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001910 return false;
1911 break;
John McCall2de56d12010-08-25 11:45:40 +00001912 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001913 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 }
Eli Friedmana6115062012-05-24 00:47:05 +00001915 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001916 Loc = UO->getOperatorLoc();
1917 R1 = UO->getSubExpr()->getSourceRange();
1918 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001920 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001921 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001922 switch (BO->getOpcode()) {
1923 default:
1924 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001925 // Consider the RHS of comma for side effects. LHS was checked by
1926 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001927 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001928 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1929 // lvalue-ness) of an assignment written in a macro.
1930 if (IntegerLiteral *IE =
1931 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1932 if (IE->getValue() == 0)
1933 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001934 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001935 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001936 case BO_LAnd:
1937 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001938 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1939 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001940 return false;
1941 break;
John McCallbf0ee352010-02-16 04:10:53 +00001942 }
Chris Lattner026dc962009-02-14 07:37:35 +00001943 if (BO->isAssignmentOp())
1944 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001945 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001946 Loc = BO->getOperatorLoc();
1947 R1 = BO->getLHS()->getSourceRange();
1948 R2 = BO->getRHS()->getSourceRange();
1949 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001950 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001951 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001952 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001953 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001954 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001955
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001956 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001957 // If only one of the LHS or RHS is a warning, the operator might
1958 // be being used for control flow. Only warn if both the LHS and
1959 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001960 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001961 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001962 return false;
1963 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001964 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001965 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001966 }
1967
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001969 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001970 Loc = cast<MemberExpr>(this)->getMemberLoc();
1971 R1 = SourceRange(Loc, Loc);
1972 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1973 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001976 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001977 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1978 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1979 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1980 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001981
Chandler Carruth9b106832011-08-17 09:49:44 +00001982 case CXXOperatorCallExprClass: {
1983 // We warn about operator== and operator!= even when user-defined operator
1984 // overloads as there is no reasonable way to define these such that they
1985 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1986 // warning: these operators are commonly typo'ed, and so warning on them
1987 // provides additional value as well. If this list is updated,
1988 // DiagnoseUnusedComparison should be as well.
1989 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1990 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001991 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001992 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001993 Loc = Op->getOperatorLoc();
1994 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001995 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001996 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001997
1998 // Fallthrough for generic call handling.
1999 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002000 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002001 case CXXMemberCallExprClass:
2002 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00002003 // If this is a direct call, get the callee.
2004 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00002005 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00002006 // If the callee has attribute pure, const, or warn_unused_result, warn
2007 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002008 //
2009 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
2010 // updated to match for QoI.
2011 if (FD->getAttr<WarnUnusedResultAttr>() ||
2012 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002013 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002014 Loc = CE->getCallee()->getLocStart();
2015 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002017 if (unsigned NumArgs = CE->getNumArgs())
2018 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2019 CE->getArg(NumArgs-1)->getLocEnd());
2020 return true;
2021 }
Chris Lattner026dc962009-02-14 07:37:35 +00002022 }
2023 return false;
2024 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002025
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002026 // If we don't know precisely what we're looking at, let's not warn.
2027 case UnresolvedLookupExprClass:
2028 case CXXUnresolvedConstructExprClass:
2029 return false;
2030
Anders Carlsson58beed92009-11-17 17:11:23 +00002031 case CXXTemporaryObjectExprClass:
2032 case CXXConstructExprClass:
2033 return false;
2034
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002035 case ObjCMessageExprClass: {
2036 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002037 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002038 ME->isInstanceMessage() &&
2039 !ME->getType()->isVoidType() &&
2040 ME->getSelector().getIdentifierInfoForSlot(0) &&
2041 ME->getSelector().getIdentifierInfoForSlot(0)
2042 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00002043 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002044 Loc = getExprLoc();
2045 R1 = ME->getSourceRange();
2046 return true;
2047 }
2048
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002049 const ObjCMethodDecl *MD = ME->getMethodDecl();
2050 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002051 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002052 Loc = getExprLoc();
2053 return true;
2054 }
Chris Lattner026dc962009-02-14 07:37:35 +00002055 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002056 }
Mike Stump1eb44332009-09-09 15:08:12 +00002057
John McCall12f78a62010-12-02 01:19:52 +00002058 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002059 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002060 Loc = getExprLoc();
2061 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002062 return true;
John McCall12f78a62010-12-02 01:19:52 +00002063
John McCall4b9c2d22011-11-06 09:01:30 +00002064 case PseudoObjectExprClass: {
2065 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2066
2067 // Only complain about things that have the form of a getter.
2068 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2069 isa<BinaryOperator>(PO->getSyntacticForm()))
2070 return false;
2071
Eli Friedmana6115062012-05-24 00:47:05 +00002072 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002073 Loc = getExprLoc();
2074 R1 = getSourceRange();
2075 return true;
2076 }
2077
Chris Lattner611b2ec2008-07-26 19:51:01 +00002078 case StmtExprClass: {
2079 // Statement exprs don't logically have side effects themselves, but are
2080 // sometimes used in macros in ways that give them a type that is unused.
2081 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2082 // however, if the result of the stmt expr is dead, we don't want to emit a
2083 // warning.
2084 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002085 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002086 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002087 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002088 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2089 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002090 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002091 }
Mike Stump1eb44332009-09-09 15:08:12 +00002092
John McCall0faede62010-03-12 07:11:26 +00002093 if (getType()->isVoidType())
2094 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002095 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002096 Loc = cast<StmtExpr>(this)->getLParenLoc();
2097 R1 = getSourceRange();
2098 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002099 }
Eli Friedman63199172012-09-24 23:02:26 +00002100 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002101 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002102 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002103 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002104 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002105 if (CE->getCastKind() == CK_ToVoid) {
2106 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002107 CE->getSubExpr()->getType().isVolatileQualified()) {
2108 const DeclRefExpr *DRE =
2109 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2110 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2111 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2112 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2113 R1, R2, Ctx);
2114 }
2115 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002116 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002117 }
Eli Friedman4059da82012-05-24 21:05:41 +00002118
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +00002119 // Ignore casts within macro expansions.
2120 if (getExprLoc().isMacroID())
2121 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2122
Eli Friedmana6115062012-05-24 00:47:05 +00002123 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002124 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002125 if (CE->getCastKind() == CK_ConstructorConversion)
2126 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002127
Eli Friedmana6115062012-05-24 00:47:05 +00002128 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002129 if (const CXXFunctionalCastExpr *CXXCE =
2130 dyn_cast<CXXFunctionalCastExpr>(this)) {
2131 Loc = CXXCE->getTypeBeginLoc();
2132 R1 = CXXCE->getSubExpr()->getSourceRange();
2133 } else {
2134 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2135 Loc = CStyleCE->getLParenLoc();
2136 R1 = CStyleCE->getSubExpr()->getSourceRange();
2137 }
Chris Lattner026dc962009-02-14 07:37:35 +00002138 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002139 }
Eli Friedmana6115062012-05-24 00:47:05 +00002140 case ImplicitCastExprClass: {
2141 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002142
Eli Friedmana6115062012-05-24 00:47:05 +00002143 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2144 if (ICE->getCastKind() == CK_LValueToRValue &&
2145 ICE->getSubExpr()->getType().isVolatileQualified())
2146 return false;
2147
2148 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2149 }
Chris Lattner04421082008-04-08 04:40:51 +00002150 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002151 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002152 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002153
2154 case CXXNewExprClass:
2155 // FIXME: In theory, there might be new expressions that don't have side
2156 // effects (e.g. a placement new with an uninitialized POD).
2157 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002158 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002159 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002160 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002161 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002162 case ExprWithCleanupsClass:
2163 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002164 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002165 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002166}
2167
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002168/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002169/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002170bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002171 const Expr *E = IgnoreParens();
2172 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002173 default:
2174 return false;
2175 case ObjCIvarRefExprClass:
2176 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002177 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002178 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002179 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002180 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002181 case MaterializeTemporaryExprClass:
2182 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2183 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002184 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002185 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002186 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002187 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002188
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002189 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2190 if (VD->hasGlobalStorage())
2191 return true;
2192 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002193 // dereferencing to a pointer is always a gc'able candidate,
2194 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002195 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002196 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002197 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002198 return false;
2199 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002200 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002201 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002202 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002203 }
2204 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002205 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002206 }
2207}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002208
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002209bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2210 if (isTypeDependent())
2211 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002212 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002213}
2214
John McCall864c0412011-04-26 20:42:42 +00002215QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002216 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002217
2218 // Bound member expressions are always one of these possibilities:
2219 // x->m x.m x->*y x.*y
2220 // (possibly parenthesized)
2221
2222 expr = expr->IgnoreParens();
2223 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2224 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2225 return mem->getMemberDecl()->getType();
2226 }
2227
2228 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2229 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2230 ->getPointeeType();
2231 assert(type->isFunctionType());
2232 return type;
2233 }
2234
2235 assert(isa<UnresolvedMemberExpr>(expr));
2236 return QualType();
2237}
2238
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002239Expr* Expr::IgnoreParens() {
2240 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002241 while (true) {
2242 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2243 E = P->getSubExpr();
2244 continue;
2245 }
2246 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2247 if (P->getOpcode() == UO_Extension) {
2248 E = P->getSubExpr();
2249 continue;
2250 }
2251 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002252 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2253 if (!P->isResultDependent()) {
2254 E = P->getResultExpr();
2255 continue;
2256 }
2257 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002258 return E;
2259 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002260}
2261
Chris Lattner56f34942008-02-13 01:02:39 +00002262/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2263/// or CastExprs or ImplicitCastExprs, returning their operand.
2264Expr *Expr::IgnoreParenCasts() {
2265 Expr *E = this;
2266 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002267 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002268 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002269 continue;
2270 }
2271 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002272 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002273 continue;
2274 }
2275 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2276 if (P->getOpcode() == UO_Extension) {
2277 E = P->getSubExpr();
2278 continue;
2279 }
2280 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002281 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2282 if (!P->isResultDependent()) {
2283 E = P->getResultExpr();
2284 continue;
2285 }
2286 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002287 if (MaterializeTemporaryExpr *Materialize
2288 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2289 E = Materialize->GetTemporaryExpr();
2290 continue;
2291 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002292 if (SubstNonTypeTemplateParmExpr *NTTP
2293 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2294 E = NTTP->getReplacement();
2295 continue;
2296 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002297 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002298 }
2299}
2300
John McCall9c5d70c2010-12-04 08:24:19 +00002301/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2302/// casts. This is intended purely as a temporary workaround for code
2303/// that hasn't yet been rewritten to do the right thing about those
2304/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002305Expr *Expr::IgnoreParenLValueCasts() {
2306 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002307 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002308 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2309 E = P->getSubExpr();
2310 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002311 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002312 if (P->getCastKind() == CK_LValueToRValue) {
2313 E = P->getSubExpr();
2314 continue;
2315 }
John McCall9c5d70c2010-12-04 08:24:19 +00002316 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2317 if (P->getOpcode() == UO_Extension) {
2318 E = P->getSubExpr();
2319 continue;
2320 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002321 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2322 if (!P->isResultDependent()) {
2323 E = P->getResultExpr();
2324 continue;
2325 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002326 } else if (MaterializeTemporaryExpr *Materialize
2327 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2328 E = Materialize->GetTemporaryExpr();
2329 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002330 } else if (SubstNonTypeTemplateParmExpr *NTTP
2331 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2332 E = NTTP->getReplacement();
2333 continue;
John McCallf6a16482010-12-04 03:47:34 +00002334 }
2335 break;
2336 }
2337 return E;
2338}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002339
2340Expr *Expr::ignoreParenBaseCasts() {
2341 Expr *E = this;
2342 while (true) {
2343 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2344 E = P->getSubExpr();
2345 continue;
2346 }
2347 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2348 if (CE->getCastKind() == CK_DerivedToBase ||
2349 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2350 CE->getCastKind() == CK_NoOp) {
2351 E = CE->getSubExpr();
2352 continue;
2353 }
2354 }
2355
2356 return E;
2357 }
2358}
2359
John McCall2fc46bf2010-05-05 22:59:52 +00002360Expr *Expr::IgnoreParenImpCasts() {
2361 Expr *E = this;
2362 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002363 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002364 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002365 continue;
2366 }
2367 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002368 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002369 continue;
2370 }
2371 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2372 if (P->getOpcode() == UO_Extension) {
2373 E = P->getSubExpr();
2374 continue;
2375 }
2376 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002377 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2378 if (!P->isResultDependent()) {
2379 E = P->getResultExpr();
2380 continue;
2381 }
2382 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002383 if (MaterializeTemporaryExpr *Materialize
2384 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2385 E = Materialize->GetTemporaryExpr();
2386 continue;
2387 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002388 if (SubstNonTypeTemplateParmExpr *NTTP
2389 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2390 E = NTTP->getReplacement();
2391 continue;
2392 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002393 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002394 }
2395}
2396
Hans Wennborg2f072b42011-06-09 17:06:51 +00002397Expr *Expr::IgnoreConversionOperator() {
2398 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002399 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002400 return MCE->getImplicitObjectArgument();
2401 }
2402 return this;
2403}
2404
Chris Lattnerecdd8412009-03-13 17:28:01 +00002405/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2406/// value (including ptr->int casts of the same size). Strip off any
2407/// ParenExpr or CastExprs, returning their operand.
2408Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2409 Expr *E = this;
2410 while (true) {
2411 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2412 E = P->getSubExpr();
2413 continue;
2414 }
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Chris Lattnerecdd8412009-03-13 17:28:01 +00002416 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2417 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002418 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002419 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002420
Chris Lattnerecdd8412009-03-13 17:28:01 +00002421 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2422 E = SE;
2423 continue;
2424 }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002426 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002427 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002428 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002429 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002430 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2431 E = SE;
2432 continue;
2433 }
2434 }
Mike Stump1eb44332009-09-09 15:08:12 +00002435
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002436 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2437 if (P->getOpcode() == UO_Extension) {
2438 E = P->getSubExpr();
2439 continue;
2440 }
2441 }
2442
Peter Collingbournef111d932011-04-15 00:35:48 +00002443 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2444 if (!P->isResultDependent()) {
2445 E = P->getResultExpr();
2446 continue;
2447 }
2448 }
2449
Douglas Gregorc0244c52011-09-08 17:56:33 +00002450 if (SubstNonTypeTemplateParmExpr *NTTP
2451 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2452 E = NTTP->getReplacement();
2453 continue;
2454 }
2455
Chris Lattnerecdd8412009-03-13 17:28:01 +00002456 return E;
2457 }
2458}
2459
Douglas Gregor6eef5192009-12-14 19:27:10 +00002460bool Expr::isDefaultArgument() const {
2461 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002462 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2463 E = M->GetTemporaryExpr();
2464
Douglas Gregor6eef5192009-12-14 19:27:10 +00002465 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2466 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002467
Douglas Gregor6eef5192009-12-14 19:27:10 +00002468 return isa<CXXDefaultArgExpr>(E);
2469}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002470
Douglas Gregor2f599792010-04-02 18:24:57 +00002471/// \brief Skip over any no-op casts and any temporary-binding
2472/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002473static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002474 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2475 E = M->GetTemporaryExpr();
2476
Douglas Gregor2f599792010-04-02 18:24:57 +00002477 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002478 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002479 E = ICE->getSubExpr();
2480 else
2481 break;
2482 }
2483
2484 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2485 E = BE->getSubExpr();
2486
2487 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002488 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002489 E = ICE->getSubExpr();
2490 else
2491 break;
2492 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002493
2494 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002495}
2496
John McCall558d2ab2010-09-15 10:14:12 +00002497/// isTemporaryObject - Determines if this expression produces a
2498/// temporary of the given class type.
2499bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2500 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2501 return false;
2502
Anders Carlssonf8b30152010-11-28 16:40:49 +00002503 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002504
John McCall58277b52010-09-15 20:59:13 +00002505 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002506 if (!E->Classify(C).isPRValue()) {
2507 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002508 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002509 return false;
2510 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002511
John McCall19e60ad2010-09-16 06:57:56 +00002512 // Black-list a few cases which yield pr-values of class type that don't
2513 // refer to temporaries of that type:
2514
2515 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002516 if (isa<ImplicitCastExpr>(E)) {
2517 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2518 case CK_DerivedToBase:
2519 case CK_UncheckedDerivedToBase:
2520 return false;
2521 default:
2522 break;
2523 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002524 }
2525
John McCall19e60ad2010-09-16 06:57:56 +00002526 // - member expressions (all)
2527 if (isa<MemberExpr>(E))
2528 return false;
2529
Eli Friedman32f498a2012-06-15 23:51:06 +00002530 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2531 if (BO->isPtrMemOp())
2532 return false;
2533
John McCall56ca35d2011-02-17 10:25:35 +00002534 // - opaque values (all)
2535 if (isa<OpaqueValueExpr>(E))
2536 return false;
2537
John McCall558d2ab2010-09-15 10:14:12 +00002538 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002539}
2540
Douglas Gregor75e85042011-03-02 21:06:53 +00002541bool Expr::isImplicitCXXThis() const {
2542 const Expr *E = this;
2543
2544 // Strip away parentheses and casts we don't care about.
2545 while (true) {
2546 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2547 E = Paren->getSubExpr();
2548 continue;
2549 }
2550
2551 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2552 if (ICE->getCastKind() == CK_NoOp ||
2553 ICE->getCastKind() == CK_LValueToRValue ||
2554 ICE->getCastKind() == CK_DerivedToBase ||
2555 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2556 E = ICE->getSubExpr();
2557 continue;
2558 }
2559 }
2560
2561 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2562 if (UnOp->getOpcode() == UO_Extension) {
2563 E = UnOp->getSubExpr();
2564 continue;
2565 }
2566 }
2567
Douglas Gregor03e80032011-06-21 17:03:29 +00002568 if (const MaterializeTemporaryExpr *M
2569 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2570 E = M->GetTemporaryExpr();
2571 continue;
2572 }
2573
Douglas Gregor75e85042011-03-02 21:06:53 +00002574 break;
2575 }
2576
2577 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2578 return This->isImplicit();
2579
2580 return false;
2581}
2582
Douglas Gregor898574e2008-12-05 23:32:09 +00002583/// hasAnyTypeDependentArguments - Determines if any of the expressions
2584/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002585bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2586 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002587 if (Exprs[I]->isTypeDependent())
2588 return true;
2589
2590 return false;
2591}
2592
John McCall4204f072010-08-02 21:13:48 +00002593bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002594 // This function is attempting whether an expression is an initializer
2595 // which can be evaluated at compile-time. isEvaluatable handles most
2596 // of the cases, but it can't deal with some initializer-specific
2597 // expressions, and it can't deal with aggregates; we deal with those here,
2598 // and fall back to isEvaluatable for the other cases.
2599
John McCall4204f072010-08-02 21:13:48 +00002600 // If we ever capture reference-binding directly in the AST, we can
2601 // kill the second parameter.
2602
2603 if (IsForRef) {
2604 EvalResult Result;
2605 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2606 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002607
Anders Carlssone8a32b82008-11-24 05:23:59 +00002608 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002609 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002610 case IntegerLiteralClass:
2611 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002612 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002613 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002614 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002615 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002616 case CXXTemporaryObjectExprClass:
2617 case CXXConstructExprClass: {
2618 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002619
2620 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002621 if (CE->getConstructor()->isTrivial()) {
2622 // 1) an application of the trivial default constructor or
2623 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002624
Richard Smith180f4792011-11-10 06:34:14 +00002625 // 2) an elidable trivial copy construction of an operand which is
2626 // itself a constant initializer. Note that we consider the
2627 // operand on its own, *not* as a reference binding.
2628 if (CE->isElidable() &&
2629 CE->getArg(0)->isConstantInitializer(Ctx, false))
2630 return true;
2631 }
2632
2633 // 3) a foldable constexpr constructor.
2634 break;
John McCallb4b9b152010-08-01 21:51:45 +00002635 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002636 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002637 // This handles gcc's extension that allows global initializers like
2638 // "struct x {int x;} x = (struct x) {};".
2639 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002640 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002641 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002642 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002643 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002644 // FIXME: This doesn't deal with fields with reference types correctly.
2645 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2646 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002647 const InitListExpr *Exp = cast<InitListExpr>(this);
2648 unsigned numInits = Exp->getNumInits();
2649 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002650 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002651 return false;
2652 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002653 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002654 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002655 case ImplicitValueInitExprClass:
2656 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002657 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002658 return cast<ParenExpr>(this)->getSubExpr()
2659 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002660 case GenericSelectionExprClass:
2661 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2662 return false;
2663 return cast<GenericSelectionExpr>(this)->getResultExpr()
2664 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002665 case ChooseExprClass:
2666 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2667 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002668 case UnaryOperatorClass: {
2669 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002670 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002671 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002672 break;
2673 }
John McCall4204f072010-08-02 21:13:48 +00002674 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002675 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002676 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002677 case CStyleCastExprClass: {
2678 const CastExpr *CE = cast<CastExpr>(this);
2679
David Chisnall7a7ee302012-01-16 17:27:18 +00002680 // If we're promoting an integer to an _Atomic type then this is constant
2681 // if the integer is constant. We also need to check the converse in case
2682 // someone does something like:
2683 //
2684 // int a = (_Atomic(int))42;
2685 //
2686 // I doubt anyone would write code like this directly, but it's quite
2687 // possible as the result of macro expansions.
2688 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2689 CE->getCastKind() == CK_AtomicToNonAtomic)
2690 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2691
Richard Smithd62ca372011-12-06 22:44:34 +00002692 // Handle bitcasts of vector constants.
2693 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2694 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2695
Eli Friedman6bd97192011-12-21 00:43:02 +00002696 // Handle misc casts we want to ignore.
2697 // FIXME: Is it really safe to ignore all these?
2698 if (CE->getCastKind() == CK_NoOp ||
2699 CE->getCastKind() == CK_LValueToRValue ||
2700 CE->getCastKind() == CK_ToUnion ||
2701 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002702 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2703
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002704 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002705 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002706 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002707 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002708 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002709 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002710 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002711}
2712
Richard Smith8ae4ec22012-08-07 04:16:51 +00002713bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2714 if (isInstantiationDependent())
2715 return true;
2716
2717 switch (getStmtClass()) {
2718 case NoStmtClass:
2719 #define ABSTRACT_STMT(Type)
2720 #define STMT(Type, Base) case Type##Class:
2721 #define EXPR(Type, Base)
2722 #include "clang/AST/StmtNodes.inc"
2723 llvm_unreachable("unexpected Expr kind");
2724
2725 case DependentScopeDeclRefExprClass:
2726 case CXXUnresolvedConstructExprClass:
2727 case CXXDependentScopeMemberExprClass:
2728 case UnresolvedLookupExprClass:
2729 case UnresolvedMemberExprClass:
2730 case PackExpansionExprClass:
2731 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002732 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002733 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2734
Richard Smith60b70382012-08-07 05:18:29 +00002735 case DeclRefExprClass:
2736 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002737 case PredefinedExprClass:
2738 case IntegerLiteralClass:
2739 case FloatingLiteralClass:
2740 case ImaginaryLiteralClass:
2741 case StringLiteralClass:
2742 case CharacterLiteralClass:
2743 case OffsetOfExprClass:
2744 case ImplicitValueInitExprClass:
2745 case UnaryExprOrTypeTraitExprClass:
2746 case AddrLabelExprClass:
2747 case GNUNullExprClass:
2748 case CXXBoolLiteralExprClass:
2749 case CXXNullPtrLiteralExprClass:
2750 case CXXThisExprClass:
2751 case CXXScalarValueInitExprClass:
2752 case TypeTraitExprClass:
2753 case UnaryTypeTraitExprClass:
2754 case BinaryTypeTraitExprClass:
2755 case ArrayTypeTraitExprClass:
2756 case ExpressionTraitExprClass:
2757 case CXXNoexceptExprClass:
2758 case SizeOfPackExprClass:
2759 case ObjCStringLiteralClass:
2760 case ObjCEncodeExprClass:
2761 case ObjCBoolLiteralExprClass:
2762 case CXXUuidofExprClass:
2763 case OpaqueValueExprClass:
2764 // These never have a side-effect.
2765 return false;
2766
2767 case CallExprClass:
2768 case CompoundAssignOperatorClass:
2769 case VAArgExprClass:
2770 case AtomicExprClass:
2771 case StmtExprClass:
2772 case CXXOperatorCallExprClass:
2773 case CXXMemberCallExprClass:
2774 case UserDefinedLiteralClass:
2775 case CXXThrowExprClass:
2776 case CXXNewExprClass:
2777 case CXXDeleteExprClass:
2778 case ExprWithCleanupsClass:
2779 case CXXBindTemporaryExprClass:
2780 case BlockExprClass:
2781 case CUDAKernelCallExprClass:
2782 // These always have a side-effect.
2783 return true;
2784
2785 case ParenExprClass:
2786 case ArraySubscriptExprClass:
2787 case MemberExprClass:
2788 case ConditionalOperatorClass:
2789 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002790 case CompoundLiteralExprClass:
2791 case ExtVectorElementExprClass:
2792 case DesignatedInitExprClass:
2793 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002794 case CXXPseudoDestructorExprClass:
2795 case SubstNonTypeTemplateParmExprClass:
2796 case MaterializeTemporaryExprClass:
2797 case ShuffleVectorExprClass:
2798 case AsTypeExprClass:
2799 // These have a side-effect if any subexpression does.
2800 break;
2801
Richard Smith60b70382012-08-07 05:18:29 +00002802 case UnaryOperatorClass:
2803 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002804 return true;
2805 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002806
2807 case BinaryOperatorClass:
2808 if (cast<BinaryOperator>(this)->isAssignmentOp())
2809 return true;
2810 break;
2811
Richard Smith8ae4ec22012-08-07 04:16:51 +00002812 case InitListExprClass:
2813 // FIXME: The children for an InitListExpr doesn't include the array filler.
2814 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2815 if (E->HasSideEffects(Ctx))
2816 return true;
2817 break;
2818
2819 case GenericSelectionExprClass:
2820 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2821 HasSideEffects(Ctx);
2822
2823 case ChooseExprClass:
2824 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2825
2826 case CXXDefaultArgExprClass:
2827 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2828
2829 case CXXDynamicCastExprClass: {
2830 // A dynamic_cast expression has side-effects if it can throw.
2831 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2832 if (DCE->getTypeAsWritten()->isReferenceType() &&
2833 DCE->getCastKind() == CK_Dynamic)
2834 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002835 } // Fall through.
2836 case ImplicitCastExprClass:
2837 case CStyleCastExprClass:
2838 case CXXStaticCastExprClass:
2839 case CXXReinterpretCastExprClass:
2840 case CXXConstCastExprClass:
2841 case CXXFunctionalCastExprClass: {
2842 const CastExpr *CE = cast<CastExpr>(this);
2843 if (CE->getCastKind() == CK_LValueToRValue &&
2844 CE->getSubExpr()->getType().isVolatileQualified())
2845 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002846 break;
2847 }
2848
Richard Smith0d729102012-08-13 20:08:14 +00002849 case CXXTypeidExprClass:
2850 // typeid might throw if its subexpression is potentially-evaluated, so has
2851 // side-effects in that case whether or not its subexpression does.
2852 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002853
2854 case CXXConstructExprClass:
2855 case CXXTemporaryObjectExprClass: {
2856 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002857 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002858 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002859 // A trivial constructor does not add any side-effects of its own. Just look
2860 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002861 break;
2862 }
2863
2864 case LambdaExprClass: {
2865 const LambdaExpr *LE = cast<LambdaExpr>(this);
2866 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2867 E = LE->capture_end(); I != E; ++I)
2868 if (I->getCaptureKind() == LCK_ByCopy)
2869 // FIXME: Only has a side-effect if the variable is volatile or if
2870 // the copy would invoke a non-trivial copy constructor.
2871 return true;
2872 return false;
2873 }
2874
2875 case PseudoObjectExprClass: {
2876 // Only look for side-effects in the semantic form, and look past
2877 // OpaqueValueExpr bindings in that form.
2878 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2879 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2880 E = PO->semantics_end();
2881 I != E; ++I) {
2882 const Expr *Subexpr = *I;
2883 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2884 Subexpr = OVE->getSourceExpr();
2885 if (Subexpr->HasSideEffects(Ctx))
2886 return true;
2887 }
2888 return false;
2889 }
2890
2891 case ObjCBoxedExprClass:
2892 case ObjCArrayLiteralClass:
2893 case ObjCDictionaryLiteralClass:
2894 case ObjCMessageExprClass:
2895 case ObjCSelectorExprClass:
2896 case ObjCProtocolExprClass:
2897 case ObjCPropertyRefExprClass:
2898 case ObjCIsaExprClass:
2899 case ObjCIndirectCopyRestoreExprClass:
2900 case ObjCSubscriptRefExprClass:
2901 case ObjCBridgedCastExprClass:
2902 // FIXME: Classify these cases better.
2903 return true;
2904 }
2905
2906 // Recurse to children.
2907 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2908 if (const Stmt *S = *SubStmts)
2909 if (cast<Expr>(S)->HasSideEffects(Ctx))
2910 return true;
2911
2912 return false;
2913}
2914
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002915namespace {
2916 /// \brief Look for a call to a non-trivial function within an expression.
2917 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2918 {
2919 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2920
2921 bool NonTrivial;
2922
2923 public:
2924 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002925 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002926
2927 bool hasNonTrivialCall() const { return NonTrivial; }
2928
2929 void VisitCallExpr(CallExpr *E) {
2930 if (CXXMethodDecl *Method
2931 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2932 if (Method->isTrivial()) {
2933 // Recurse to children of the call.
2934 Inherited::VisitStmt(E);
2935 return;
2936 }
2937 }
2938
2939 NonTrivial = true;
2940 }
2941
2942 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2943 if (E->getConstructor()->isTrivial()) {
2944 // Recurse to children of the call.
2945 Inherited::VisitStmt(E);
2946 return;
2947 }
2948
2949 NonTrivial = true;
2950 }
2951
2952 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2953 if (E->getTemporary()->getDestructor()->isTrivial()) {
2954 Inherited::VisitStmt(E);
2955 return;
2956 }
2957
2958 NonTrivial = true;
2959 }
2960 };
2961}
2962
2963bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2964 NonTrivialCallFinder Finder(Ctx);
2965 Finder.Visit(this);
2966 return Finder.hasNonTrivialCall();
2967}
2968
Chandler Carruth82214a82011-02-18 23:54:50 +00002969/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2970/// pointer constant or not, as well as the specific kind of constant detected.
2971/// Null pointer constants can be integer constant expressions with the
2972/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2973/// (a GNU extension).
2974Expr::NullPointerConstantKind
2975Expr::isNullPointerConstant(ASTContext &Ctx,
2976 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002977 if (isValueDependent()) {
2978 switch (NPC) {
2979 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002980 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002981 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002982 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00002983 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00002984 else
2985 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002986
Douglas Gregorce940492009-09-25 04:25:58 +00002987 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002988 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002989 }
2990 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002991
Sebastian Redl07779722008-10-31 14:43:28 +00002992 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002993 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002994 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002995 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002996 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002997 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002998 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002999 Pointee->isVoidType() && // to void*
3000 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00003001 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00003002 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003003 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003004 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
3005 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00003006 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00003007 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
3008 // Accept ((void*)0) as a null pointer constant, as many other
3009 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00003010 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00003011 } else if (const GenericSelectionExpr *GE =
3012 dyn_cast<GenericSelectionExpr>(this)) {
3013 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00003014 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00003015 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00003016 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00003017 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003018 } else if (isa<GNUNullExpr>(this)) {
3019 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003020 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003021 } else if (const MaterializeTemporaryExpr *M
3022 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3023 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003024 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3025 if (const Expr *Source = OVE->getSourceExpr())
3026 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003027 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003028
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003029 // C++0x nullptr_t is always a null pointer constant.
3030 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00003031 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003032
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003033 if (const RecordType *UT = getType()->getAsUnionType())
3034 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3035 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3036 const Expr *InitExpr = CLE->getInitializer();
3037 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3038 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3039 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003040 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003041 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003042 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003043 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Reid Spencer5f016e22007-07-11 17:01:13 +00003045 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00003046 // test for the value 0. Don't use the C++11 constant expression semantics
3047 // for this, for now; once the dust settles on core issue 903, we might only
3048 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003049 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00003050 if (!isCXX98IntegralConstantExpr(Ctx))
3051 return NPCK_NotNull;
3052 } else {
3053 if (!isIntegerConstantExpr(Ctx))
3054 return NPCK_NotNull;
3055 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003056
David Blaikie50800fc2012-08-08 17:33:31 +00003057 if (EvaluateKnownConstInt(Ctx) != 0)
3058 return NPCK_NotNull;
3059
3060 if (isa<IntegerLiteral>(this))
3061 return NPCK_ZeroLiteral;
3062 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003063}
Steve Naroff31a45842007-07-28 23:10:27 +00003064
John McCallf6a16482010-12-04 03:47:34 +00003065/// \brief If this expression is an l-value for an Objective C
3066/// property, find the underlying property reference expression.
3067const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3068 const Expr *E = this;
3069 while (true) {
3070 assert((E->getValueKind() == VK_LValue &&
3071 E->getObjectKind() == OK_ObjCProperty) &&
3072 "expression is not a property reference");
3073 E = E->IgnoreParenCasts();
3074 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3075 if (BO->getOpcode() == BO_Comma) {
3076 E = BO->getRHS();
3077 continue;
3078 }
3079 }
3080
3081 break;
3082 }
3083
3084 return cast<ObjCPropertyRefExpr>(E);
3085}
3086
Anna Zaksbbff82f2012-10-01 20:34:04 +00003087bool Expr::isObjCSelfExpr() const {
3088 const Expr *E = IgnoreParenImpCasts();
3089
3090 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3091 if (!DRE)
3092 return false;
3093
3094 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3095 if (!Param)
3096 return false;
3097
3098 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3099 if (!M)
3100 return false;
3101
3102 return M->getSelfDecl() == Param;
3103}
3104
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003105FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003106 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003107
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003108 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003109 if (ICE->getCastKind() == CK_LValueToRValue ||
3110 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003111 E = ICE->getSubExpr()->IgnoreParens();
3112 else
3113 break;
3114 }
3115
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003116 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003117 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003118 if (Field->isBitField())
3119 return Field;
3120
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003121 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3122 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3123 if (Field->isBitField())
3124 return Field;
3125
Eli Friedman42068e92011-07-13 02:05:57 +00003126 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003127 if (BinOp->isAssignmentOp() && BinOp->getLHS())
3128 return BinOp->getLHS()->getBitField();
3129
Eli Friedman42068e92011-07-13 02:05:57 +00003130 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3131 return BinOp->getRHS()->getBitField();
3132 }
3133
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003134 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003135}
3136
Anders Carlsson09380262010-01-31 17:18:49 +00003137bool Expr::refersToVectorElement() const {
3138 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003139
Anders Carlsson09380262010-01-31 17:18:49 +00003140 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003141 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003142 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003143 E = ICE->getSubExpr()->IgnoreParens();
3144 else
3145 break;
3146 }
Sean Huntc3021132010-05-05 15:23:54 +00003147
Anders Carlsson09380262010-01-31 17:18:49 +00003148 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3149 return ASE->getBase()->getType()->isVectorType();
3150
3151 if (isa<ExtVectorElementExpr>(E))
3152 return true;
3153
3154 return false;
3155}
3156
Chris Lattner2140e902009-02-16 22:14:05 +00003157/// isArrow - Return true if the base expression is a pointer to vector,
3158/// return false if the base expression is a vector.
3159bool ExtVectorElementExpr::isArrow() const {
3160 return getBase()->getType()->isPointerType();
3161}
3162
Nate Begeman213541a2008-04-18 23:10:10 +00003163unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003164 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003165 return VT->getNumElements();
3166 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003167}
3168
Nate Begeman8a997642008-05-09 06:41:27 +00003169/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003170bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003171 // FIXME: Refactor this code to an accessor on the AST node which returns the
3172 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003173 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003174
3175 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003176 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003177 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003178
Nate Begeman190d6a22009-01-18 02:01:21 +00003179 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003180 if (Comp[0] == 's' || Comp[0] == 'S')
3181 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003182
Daniel Dunbar15027422009-10-17 23:53:04 +00003183 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003184 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003185 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003186
Steve Narofffec0b492007-07-30 03:29:09 +00003187 return false;
3188}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003189
Nate Begeman8a997642008-05-09 06:41:27 +00003190/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003191void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003192 SmallVectorImpl<unsigned> &Elts) const {
3193 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003194 if (Comp[0] == 's' || Comp[0] == 'S')
3195 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003197 bool isHi = Comp == "hi";
3198 bool isLo = Comp == "lo";
3199 bool isEven = Comp == "even";
3200 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003201
Nate Begeman8a997642008-05-09 06:41:27 +00003202 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3203 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Nate Begeman8a997642008-05-09 06:41:27 +00003205 if (isHi)
3206 Index = e + i;
3207 else if (isLo)
3208 Index = i;
3209 else if (isEven)
3210 Index = 2 * i;
3211 else if (isOdd)
3212 Index = 2 * i + 1;
3213 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003214 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003215
Nate Begeman3b8d1162008-05-13 21:03:02 +00003216 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003217 }
Nate Begeman8a997642008-05-09 06:41:27 +00003218}
3219
Douglas Gregor04badcf2010-04-21 00:45:42 +00003220ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003221 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003222 SourceLocation LBracLoc,
3223 SourceLocation SuperLoc,
3224 bool IsInstanceSuper,
3225 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003226 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003227 ArrayRef<SourceLocation> SelLocs,
3228 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003229 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003230 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003231 SourceLocation RBracLoc,
3232 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003233 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003234 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003235 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003236 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003237 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3238 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003239 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003240 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3241 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003242{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003243 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003244 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003245}
3246
Douglas Gregor04badcf2010-04-21 00:45:42 +00003247ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003248 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003249 SourceLocation LBracLoc,
3250 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003251 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003252 ArrayRef<SourceLocation> SelLocs,
3253 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003254 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003255 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003256 SourceLocation RBracLoc,
3257 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003258 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003259 T->isDependentType(), T->isInstantiationDependentType(),
3260 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003261 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3262 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003263 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003264 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003265 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003266{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003267 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003268 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003269}
3270
Douglas Gregor04badcf2010-04-21 00:45:42 +00003271ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003272 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003273 SourceLocation LBracLoc,
3274 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003275 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003276 ArrayRef<SourceLocation> SelLocs,
3277 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003278 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003279 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003280 SourceLocation RBracLoc,
3281 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003282 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003283 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003284 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003285 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003286 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3287 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003288 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003289 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003290 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003291{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003292 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003293 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003294}
3295
3296void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3297 ArrayRef<SourceLocation> SelLocs,
3298 SelectorLocationsKind SelLocsK) {
3299 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003300 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003301 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003302 if (Args[I]->isTypeDependent())
3303 ExprBits.TypeDependent = true;
3304 if (Args[I]->isValueDependent())
3305 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003306 if (Args[I]->isInstantiationDependent())
3307 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003308 if (Args[I]->containsUnexpandedParameterPack())
3309 ExprBits.ContainsUnexpandedParameterPack = true;
3310
3311 MyArgs[I] = Args[I];
3312 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003313
Benjamin Kramer19562c92012-02-20 00:20:48 +00003314 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003315 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003316 if (SelLocsK == SelLoc_NonStandard)
3317 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3318 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003319}
3320
Douglas Gregor04badcf2010-04-21 00:45:42 +00003321ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003322 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003323 SourceLocation LBracLoc,
3324 SourceLocation SuperLoc,
3325 bool IsInstanceSuper,
3326 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003327 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003328 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003329 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003330 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003331 SourceLocation RBracLoc,
3332 bool isImplicit) {
3333 assert((!SelLocs.empty() || isImplicit) &&
3334 "No selector locs for non-implicit message");
3335 ObjCMessageExpr *Mem;
3336 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3337 if (isImplicit)
3338 Mem = alloc(Context, Args.size(), 0);
3339 else
3340 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003341 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003342 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003343 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003344}
3345
3346ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003347 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003348 SourceLocation LBracLoc,
3349 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003350 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003351 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003352 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003353 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003354 SourceLocation RBracLoc,
3355 bool isImplicit) {
3356 assert((!SelLocs.empty() || isImplicit) &&
3357 "No selector locs for non-implicit message");
3358 ObjCMessageExpr *Mem;
3359 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3360 if (isImplicit)
3361 Mem = alloc(Context, Args.size(), 0);
3362 else
3363 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003364 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003365 SelLocs, SelLocsK, Method, Args, RBracLoc,
3366 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003367}
3368
3369ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003370 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003371 SourceLocation LBracLoc,
3372 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003373 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003374 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003375 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003376 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003377 SourceLocation RBracLoc,
3378 bool isImplicit) {
3379 assert((!SelLocs.empty() || isImplicit) &&
3380 "No selector locs for non-implicit message");
3381 ObjCMessageExpr *Mem;
3382 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3383 if (isImplicit)
3384 Mem = alloc(Context, Args.size(), 0);
3385 else
3386 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003387 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003388 SelLocs, SelLocsK, Method, Args, RBracLoc,
3389 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003390}
3391
Sean Huntc3021132010-05-05 15:23:54 +00003392ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003393 unsigned NumArgs,
3394 unsigned NumStoredSelLocs) {
3395 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003396 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3397}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003398
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003399ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3400 ArrayRef<Expr *> Args,
3401 SourceLocation RBraceLoc,
3402 ArrayRef<SourceLocation> SelLocs,
3403 Selector Sel,
3404 SelectorLocationsKind &SelLocsK) {
3405 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3406 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3407 : 0;
3408 return alloc(C, Args.size(), NumStoredSelLocs);
3409}
3410
3411ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3412 unsigned NumArgs,
3413 unsigned NumStoredSelLocs) {
3414 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3415 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3416 return (ObjCMessageExpr *)C.Allocate(Size,
3417 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3418}
3419
3420void ObjCMessageExpr::getSelectorLocs(
3421 SmallVectorImpl<SourceLocation> &SelLocs) const {
3422 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3423 SelLocs.push_back(getSelectorLoc(i));
3424}
3425
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003426SourceRange ObjCMessageExpr::getReceiverRange() const {
3427 switch (getReceiverKind()) {
3428 case Instance:
3429 return getInstanceReceiver()->getSourceRange();
3430
3431 case Class:
3432 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3433
3434 case SuperInstance:
3435 case SuperClass:
3436 return getSuperLoc();
3437 }
3438
David Blaikie30263482012-01-20 21:50:17 +00003439 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003440}
3441
Douglas Gregor04badcf2010-04-21 00:45:42 +00003442Selector ObjCMessageExpr::getSelector() const {
3443 if (HasMethod)
3444 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3445 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003446 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003447}
3448
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003449QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003450 switch (getReceiverKind()) {
3451 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003452 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003453 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003454 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003455 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003456 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003457 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003458 }
3459
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003460 llvm_unreachable("unexpected receiver kind");
3461}
3462
3463ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3464 QualType T = getReceiverType();
3465
3466 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3467 return Ptr->getInterfaceDecl();
3468
3469 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3470 return Ty->getInterface();
3471
Douglas Gregor04badcf2010-04-21 00:45:42 +00003472 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003473}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003474
Chris Lattner5f9e2722011-07-23 10:55:15 +00003475StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003476 switch (getBridgeKind()) {
3477 case OBC_Bridge:
3478 return "__bridge";
3479 case OBC_BridgeTransfer:
3480 return "__bridge_transfer";
3481 case OBC_BridgeRetained:
3482 return "__bridge_retained";
3483 }
David Blaikie30263482012-01-20 21:50:17 +00003484
3485 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003486}
3487
Jay Foad4ba2a172011-01-12 09:06:06 +00003488bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003489 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003490}
3491
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003492ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003493 QualType Type, SourceLocation BLoc,
3494 SourceLocation RP)
3495 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3496 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003497 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003498 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003499 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003500{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003501 SubExprs = new (C) Stmt*[args.size()];
3502 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003503 if (args[i]->isTypeDependent())
3504 ExprBits.TypeDependent = true;
3505 if (args[i]->isValueDependent())
3506 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003507 if (args[i]->isInstantiationDependent())
3508 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003509 if (args[i]->containsUnexpandedParameterPack())
3510 ExprBits.ContainsUnexpandedParameterPack = true;
3511
3512 SubExprs[i] = args[i];
3513 }
3514}
3515
Nate Begeman888376a2009-08-12 02:28:50 +00003516void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3517 unsigned NumExprs) {
3518 if (SubExprs) C.Deallocate(SubExprs);
3519
3520 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003521 this->NumExprs = NumExprs;
3522 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003523}
Nate Begeman888376a2009-08-12 02:28:50 +00003524
Peter Collingbournef111d932011-04-15 00:35:48 +00003525GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3526 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003527 ArrayRef<TypeSourceInfo*> AssocTypes,
3528 ArrayRef<Expr*> AssocExprs,
3529 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003530 SourceLocation RParenLoc,
3531 bool ContainsUnexpandedParameterPack,
3532 unsigned ResultIndex)
3533 : Expr(GenericSelectionExprClass,
3534 AssocExprs[ResultIndex]->getType(),
3535 AssocExprs[ResultIndex]->getValueKind(),
3536 AssocExprs[ResultIndex]->getObjectKind(),
3537 AssocExprs[ResultIndex]->isTypeDependent(),
3538 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003539 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003540 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003541 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3542 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3543 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3544 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003545 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003546 assert(AssocTypes.size() == AssocExprs.size());
3547 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3548 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003549}
3550
3551GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3552 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003553 ArrayRef<TypeSourceInfo*> AssocTypes,
3554 ArrayRef<Expr*> AssocExprs,
3555 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003556 SourceLocation RParenLoc,
3557 bool ContainsUnexpandedParameterPack)
3558 : Expr(GenericSelectionExprClass,
3559 Context.DependentTy,
3560 VK_RValue,
3561 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003562 /*isTypeDependent=*/true,
3563 /*isValueDependent=*/true,
3564 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003565 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003566 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3567 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3568 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3569 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003570 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003571 assert(AssocTypes.size() == AssocExprs.size());
3572 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3573 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003574}
3575
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003576//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003577// DesignatedInitExpr
3578//===----------------------------------------------------------------------===//
3579
Chandler Carruthb1138242011-06-16 06:47:06 +00003580IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003581 assert(Kind == FieldDesignator && "Only valid on a field designator");
3582 if (Field.NameOrField & 0x01)
3583 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3584 else
3585 return getField()->getIdentifier();
3586}
3587
Sean Huntc3021132010-05-05 15:23:54 +00003588DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003589 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003590 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003591 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003592 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003593 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003594 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003595 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003596 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003597 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003598 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003599 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003600 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003601 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003602 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003603
3604 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003605 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003606 *Child++ = Init;
3607
3608 // Copy the designators and their subexpressions, computing
3609 // value-dependence along the way.
3610 unsigned IndexIdx = 0;
3611 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003612 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003613
3614 if (this->Designators[I].isArrayDesignator()) {
3615 // Compute type- and value-dependence.
3616 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003617 if (Index->isTypeDependent() || Index->isValueDependent())
3618 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003619 if (Index->isInstantiationDependent())
3620 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003621 // Propagate unexpanded parameter packs.
3622 if (Index->containsUnexpandedParameterPack())
3623 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003624
3625 // Copy the index expressions into permanent storage.
3626 *Child++ = IndexExprs[IndexIdx++];
3627 } else if (this->Designators[I].isArrayRangeDesignator()) {
3628 // Compute type- and value-dependence.
3629 Expr *Start = IndexExprs[IndexIdx];
3630 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003631 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003632 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003633 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003634 ExprBits.InstantiationDependent = true;
3635 } else if (Start->isInstantiationDependent() ||
3636 End->isInstantiationDependent()) {
3637 ExprBits.InstantiationDependent = true;
3638 }
3639
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003640 // Propagate unexpanded parameter packs.
3641 if (Start->containsUnexpandedParameterPack() ||
3642 End->containsUnexpandedParameterPack())
3643 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003644
3645 // Copy the start/end expressions into permanent storage.
3646 *Child++ = IndexExprs[IndexIdx++];
3647 *Child++ = IndexExprs[IndexIdx++];
3648 }
3649 }
3650
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003651 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003652}
3653
Douglas Gregor05c13a32009-01-22 00:58:24 +00003654DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003655DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003656 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003657 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003658 SourceLocation ColonOrEqualLoc,
3659 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003660 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003661 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003662 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003663 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003664 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003665}
3666
Mike Stump1eb44332009-09-09 15:08:12 +00003667DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003668 unsigned NumIndexExprs) {
3669 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3670 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3671 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3672}
3673
Douglas Gregor319d57f2010-01-06 23:17:19 +00003674void DesignatedInitExpr::setDesignators(ASTContext &C,
3675 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003676 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003677 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003678 NumDesignators = NumDesigs;
3679 for (unsigned I = 0; I != NumDesigs; ++I)
3680 Designators[I] = Desigs[I];
3681}
3682
Abramo Bagnara24f46742011-03-16 15:08:46 +00003683SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3684 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3685 if (size() == 1)
3686 return DIE->getDesignator(0)->getSourceRange();
3687 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3688 DIE->getDesignator(size()-1)->getEndLocation());
3689}
3690
Douglas Gregor05c13a32009-01-22 00:58:24 +00003691SourceRange DesignatedInitExpr::getSourceRange() const {
3692 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003693 Designator &First =
3694 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003695 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003696 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003697 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3698 else
3699 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3700 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003701 StartLoc =
3702 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003703 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3704}
3705
Douglas Gregor05c13a32009-01-22 00:58:24 +00003706Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3707 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3708 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3709 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003710 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3711 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3712}
3713
3714Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003715 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003716 "Requires array range designator");
3717 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3718 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003719 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3720 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3721}
3722
3723Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003724 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003725 "Requires array range designator");
3726 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3727 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003728 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3729 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3730}
3731
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003732/// \brief Replaces the designator at index @p Idx with the series
3733/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003734void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003735 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003736 const Designator *Last) {
3737 unsigned NumNewDesignators = Last - First;
3738 if (NumNewDesignators == 0) {
3739 std::copy_backward(Designators + Idx + 1,
3740 Designators + NumDesignators,
3741 Designators + Idx);
3742 --NumNewDesignators;
3743 return;
3744 } else if (NumNewDesignators == 1) {
3745 Designators[Idx] = *First;
3746 return;
3747 }
3748
Mike Stump1eb44332009-09-09 15:08:12 +00003749 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003750 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003751 std::copy(Designators, Designators + Idx, NewDesignators);
3752 std::copy(First, Last, NewDesignators + Idx);
3753 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3754 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003755 Designators = NewDesignators;
3756 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3757}
3758
Mike Stump1eb44332009-09-09 15:08:12 +00003759ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003760 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003761 SourceLocation rparenloc)
3762 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003763 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003764 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3765 Exprs = new (C) Stmt*[exprs.size()];
3766 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003767 if (exprs[i]->isTypeDependent())
3768 ExprBits.TypeDependent = true;
3769 if (exprs[i]->isValueDependent())
3770 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003771 if (exprs[i]->isInstantiationDependent())
3772 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003773 if (exprs[i]->containsUnexpandedParameterPack())
3774 ExprBits.ContainsUnexpandedParameterPack = true;
3775
Nate Begeman2ef13e52009-08-10 23:49:36 +00003776 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003777 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003778}
3779
John McCalle996ffd2011-02-16 08:02:54 +00003780const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3781 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3782 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003783 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3784 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003785 e = cast<CXXConstructExpr>(e)->getArg(0);
3786 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3787 e = ice->getSubExpr();
3788 return cast<OpaqueValueExpr>(e);
3789}
3790
John McCall4b9c2d22011-11-06 09:01:30 +00003791PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3792 unsigned numSemanticExprs) {
3793 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3794 (1 + numSemanticExprs) * sizeof(Expr*),
3795 llvm::alignOf<PseudoObjectExpr>());
3796 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3797}
3798
3799PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3800 : Expr(PseudoObjectExprClass, shell) {
3801 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3802}
3803
3804PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3805 ArrayRef<Expr*> semantics,
3806 unsigned resultIndex) {
3807 assert(syntax && "no syntactic expression!");
3808 assert(semantics.size() && "no semantic expressions!");
3809
3810 QualType type;
3811 ExprValueKind VK;
3812 if (resultIndex == NoResult) {
3813 type = C.VoidTy;
3814 VK = VK_RValue;
3815 } else {
3816 assert(resultIndex < semantics.size());
3817 type = semantics[resultIndex]->getType();
3818 VK = semantics[resultIndex]->getValueKind();
3819 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3820 }
3821
3822 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3823 (1 + semantics.size()) * sizeof(Expr*),
3824 llvm::alignOf<PseudoObjectExpr>());
3825 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3826 resultIndex);
3827}
3828
3829PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3830 Expr *syntax, ArrayRef<Expr*> semantics,
3831 unsigned resultIndex)
3832 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3833 /*filled in at end of ctor*/ false, false, false, false) {
3834 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3835 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3836
3837 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3838 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3839 getSubExprsBuffer()[i] = E;
3840
3841 if (E->isTypeDependent())
3842 ExprBits.TypeDependent = true;
3843 if (E->isValueDependent())
3844 ExprBits.ValueDependent = true;
3845 if (E->isInstantiationDependent())
3846 ExprBits.InstantiationDependent = true;
3847 if (E->containsUnexpandedParameterPack())
3848 ExprBits.ContainsUnexpandedParameterPack = true;
3849
3850 if (isa<OpaqueValueExpr>(E))
3851 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3852 "opaque-value semantic expressions for pseudo-object "
3853 "operations must have sources");
3854 }
3855}
3856
Douglas Gregor05c13a32009-01-22 00:58:24 +00003857//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003858// ExprIterator.
3859//===----------------------------------------------------------------------===//
3860
3861Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3862Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3863Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3864const Expr* ConstExprIterator::operator[](size_t idx) const {
3865 return cast<Expr>(I[idx]);
3866}
3867const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3868const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3869
3870//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003871// Child Iterators for iterating over subexpressions/substatements
3872//===----------------------------------------------------------------------===//
3873
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003874// UnaryExprOrTypeTraitExpr
3875Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003876 // If this is of a type and the type is a VLA type (and not a typedef), the
3877 // size expression of the VLA needs to be treated as an executable expression.
3878 // Why isn't this weirdness documented better in StmtIterator?
3879 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003880 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003881 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003882 return child_range(child_iterator(T), child_iterator());
3883 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003884 }
John McCall63c00d72011-02-09 08:16:59 +00003885 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003886}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003887
Steve Naroff563477d2007-09-18 23:55:05 +00003888// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003889Stmt::child_range ObjCMessageExpr::children() {
3890 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003891 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003892 begin = reinterpret_cast<Stmt **>(this + 1);
3893 else
3894 begin = reinterpret_cast<Stmt **>(getArgs());
3895 return child_range(begin,
3896 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003897}
3898
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003899ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3900 QualType T, ObjCMethodDecl *Method,
3901 SourceRange SR)
3902 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3903 false, false, false, false),
3904 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3905{
3906 Expr **SaveElements = getElements();
3907 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3908 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3909 ExprBits.ValueDependent = true;
3910 if (Elements[I]->isInstantiationDependent())
3911 ExprBits.InstantiationDependent = true;
3912 if (Elements[I]->containsUnexpandedParameterPack())
3913 ExprBits.ContainsUnexpandedParameterPack = true;
3914
3915 SaveElements[I] = Elements[I];
3916 }
3917}
3918
3919ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3920 llvm::ArrayRef<Expr *> Elements,
3921 QualType T, ObjCMethodDecl * Method,
3922 SourceRange SR) {
3923 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3924 + Elements.size() * sizeof(Expr *));
3925 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3926}
3927
3928ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3929 unsigned NumElements) {
3930
3931 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3932 + NumElements * sizeof(Expr *));
3933 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3934}
3935
3936ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3937 ArrayRef<ObjCDictionaryElement> VK,
3938 bool HasPackExpansions,
3939 QualType T, ObjCMethodDecl *method,
3940 SourceRange SR)
3941 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3942 false, false),
3943 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3944 DictWithObjectsMethod(method)
3945{
3946 KeyValuePair *KeyValues = getKeyValues();
3947 ExpansionData *Expansions = getExpansionData();
3948 for (unsigned I = 0; I < NumElements; I++) {
3949 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3950 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3951 ExprBits.ValueDependent = true;
3952 if (VK[I].Key->isInstantiationDependent() ||
3953 VK[I].Value->isInstantiationDependent())
3954 ExprBits.InstantiationDependent = true;
3955 if (VK[I].EllipsisLoc.isInvalid() &&
3956 (VK[I].Key->containsUnexpandedParameterPack() ||
3957 VK[I].Value->containsUnexpandedParameterPack()))
3958 ExprBits.ContainsUnexpandedParameterPack = true;
3959
3960 KeyValues[I].Key = VK[I].Key;
3961 KeyValues[I].Value = VK[I].Value;
3962 if (Expansions) {
3963 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3964 if (VK[I].NumExpansions)
3965 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3966 else
3967 Expansions[I].NumExpansionsPlusOne = 0;
3968 }
3969 }
3970}
3971
3972ObjCDictionaryLiteral *
3973ObjCDictionaryLiteral::Create(ASTContext &C,
3974 ArrayRef<ObjCDictionaryElement> VK,
3975 bool HasPackExpansions,
3976 QualType T, ObjCMethodDecl *method,
3977 SourceRange SR) {
3978 unsigned ExpansionsSize = 0;
3979 if (HasPackExpansions)
3980 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3981
3982 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3983 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3984 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3985}
3986
3987ObjCDictionaryLiteral *
3988ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3989 bool HasPackExpansions) {
3990 unsigned ExpansionsSize = 0;
3991 if (HasPackExpansions)
3992 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3993 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3994 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3995 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3996 HasPackExpansions);
3997}
3998
3999ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
4000 Expr *base,
4001 Expr *key, QualType T,
4002 ObjCMethodDecl *getMethod,
4003 ObjCMethodDecl *setMethod,
4004 SourceLocation RB) {
4005 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
4006 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
4007 OK_ObjCSubscript,
4008 getMethod, setMethod, RB);
4009}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004010
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004011AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004012 QualType t, AtomicOp op, SourceLocation RP)
4013 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4014 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004015 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004016{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004017 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4018 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004019 if (args[i]->isTypeDependent())
4020 ExprBits.TypeDependent = true;
4021 if (args[i]->isValueDependent())
4022 ExprBits.ValueDependent = true;
4023 if (args[i]->isInstantiationDependent())
4024 ExprBits.InstantiationDependent = true;
4025 if (args[i]->containsUnexpandedParameterPack())
4026 ExprBits.ContainsUnexpandedParameterPack = true;
4027
4028 SubExprs[i] = args[i];
4029 }
4030}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004031
4032unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4033 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004034 case AO__c11_atomic_init:
4035 case AO__c11_atomic_load:
4036 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004037 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004038
4039 case AO__c11_atomic_store:
4040 case AO__c11_atomic_exchange:
4041 case AO__atomic_load:
4042 case AO__atomic_store:
4043 case AO__atomic_store_n:
4044 case AO__atomic_exchange_n:
4045 case AO__c11_atomic_fetch_add:
4046 case AO__c11_atomic_fetch_sub:
4047 case AO__c11_atomic_fetch_and:
4048 case AO__c11_atomic_fetch_or:
4049 case AO__c11_atomic_fetch_xor:
4050 case AO__atomic_fetch_add:
4051 case AO__atomic_fetch_sub:
4052 case AO__atomic_fetch_and:
4053 case AO__atomic_fetch_or:
4054 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004055 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004056 case AO__atomic_add_fetch:
4057 case AO__atomic_sub_fetch:
4058 case AO__atomic_and_fetch:
4059 case AO__atomic_or_fetch:
4060 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004061 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004062 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004063
4064 case AO__atomic_exchange:
4065 return 4;
4066
4067 case AO__c11_atomic_compare_exchange_strong:
4068 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004069 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004070
4071 case AO__atomic_compare_exchange:
4072 case AO__atomic_compare_exchange_n:
4073 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004074 }
4075 llvm_unreachable("unknown atomic op");
4076}