blob: 5fd38bbfe59ee89425b3d40c53f823b8219fc3bb [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
Daniel Dunbar396ec672012-03-09 15:39:15 +0000448SourceLocation DeclRefExpr::getLocStart() const {
449 if (hasQualifier())
450 return getQualifierLoc().getBeginLoc();
451 return getNameInfo().getLocStart();
452}
453SourceLocation DeclRefExpr::getLocEnd() const {
454 if (hasExplicitTemplateArgs())
455 return getRAngleLoc();
456 return getNameInfo().getLocEnd();
457}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000458
Anders Carlsson3a082d82009-09-08 18:24:21 +0000459// FIXME: Maybe this should use DeclPrinter with a special "print predefined
460// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000461std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
462 ASTContext &Context = CurrentDecl->getASTContext();
463
Anders Carlsson3a082d82009-09-08 18:24:21 +0000464 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000465 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000466 return FD->getNameAsString();
467
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000468 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000469 llvm::raw_svector_ostream Out(Name);
470
471 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000472 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000473 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000474 if (MD->isStatic())
475 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000476 }
477
David Blaikie4e4d0842012-03-11 07:00:24 +0000478 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000479 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000480 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000481
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000482 const FunctionDecl *Decl = FD;
483 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
484 Decl = Pattern;
485 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000486 const FunctionProtoType *FT = 0;
487 if (FD->hasWrittenPrototype())
488 FT = dyn_cast<FunctionProtoType>(AFT);
489
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000490 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000491 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000492 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000493 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000494 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000495 }
496
497 if (FT->isVariadic()) {
498 if (FD->getNumParams()) POut << ", ";
499 POut << "...";
500 }
501 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000502 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503
Sam Weinig4eadcc52009-12-27 01:38:20 +0000504 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Argyrios Kyrtzidis4ae711b2012-12-14 19:44:11 +0000505 const FunctionType *FT = MD->getType()->castAs<FunctionType>();
David Blaikie4ef832f2012-08-10 00:55:35 +0000506 if (FT->isConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000507 POut << " const";
David Blaikie4ef832f2012-08-10 00:55:35 +0000508 if (FT->isVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000509 POut << " volatile";
510 RefQualifierKind Ref = MD->getRefQualifier();
511 if (Ref == RQ_LValue)
512 POut << " &";
513 else if (Ref == RQ_RValue)
514 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000515 }
516
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000517 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
518 SpecsTy Specs;
519 const DeclContext *Ctx = FD->getDeclContext();
520 while (Ctx && isa<NamedDecl>(Ctx)) {
521 const ClassTemplateSpecializationDecl *Spec
522 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
523 if (Spec && !Spec->isExplicitSpecialization())
524 Specs.push_back(Spec);
525 Ctx = Ctx->getParent();
526 }
527
528 std::string TemplateParams;
529 llvm::raw_string_ostream TOut(TemplateParams);
530 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
531 I != E; ++I) {
532 const TemplateParameterList *Params
533 = (*I)->getSpecializedTemplate()->getTemplateParameters();
534 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
535 assert(Params->size() == Args.size());
536 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
537 StringRef Param = Params->getParam(i)->getName();
538 if (Param.empty()) continue;
539 TOut << Param << " = ";
540 Args.get(i).print(Policy, TOut);
541 TOut << ", ";
542 }
543 }
544
545 FunctionTemplateSpecializationInfo *FSI
546 = FD->getTemplateSpecializationInfo();
547 if (FSI && !FSI->isExplicitSpecialization()) {
548 const TemplateParameterList* Params
549 = FSI->getTemplate()->getTemplateParameters();
550 const TemplateArgumentList* Args = FSI->TemplateArguments;
551 assert(Params->size() == Args->size());
552 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
553 StringRef Param = Params->getParam(i)->getName();
554 if (Param.empty()) continue;
555 TOut << Param << " = ";
556 Args->get(i).print(Policy, TOut);
557 TOut << ", ";
558 }
559 }
560
561 TOut.flush();
562 if (!TemplateParams.empty()) {
563 // remove the trailing comma and space
564 TemplateParams.resize(TemplateParams.size() - 2);
565 POut << " [" << TemplateParams << "]";
566 }
567
568 POut.flush();
569
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000570 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
571 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000572
573 Out << Proto;
574
575 Out.flush();
576 return Name.str().str();
577 }
578 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000579 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000580 llvm::raw_svector_ostream Out(Name);
581 Out << (MD->isInstanceMethod() ? '-' : '+');
582 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000583
584 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
585 // a null check to avoid a crash.
586 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000587 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000588
Anders Carlsson3a082d82009-09-08 18:24:21 +0000589 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000590 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000591 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000592
Anders Carlsson3a082d82009-09-08 18:24:21 +0000593 Out << ' ';
594 Out << MD->getSelector().getAsString();
595 Out << ']';
596
597 Out.flush();
598 return Name.str().str();
599 }
600 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
601 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
602 return "top level";
603 }
604 return "";
605}
606
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000607void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
608 if (hasAllocation())
609 C.Deallocate(pVal);
610
611 BitWidth = Val.getBitWidth();
612 unsigned NumWords = Val.getNumWords();
613 const uint64_t* Words = Val.getRawData();
614 if (NumWords > 1) {
615 pVal = new (C) uint64_t[NumWords];
616 std::copy(Words, Words + NumWords, pVal);
617 } else if (NumWords == 1)
618 VAL = Words[0];
619 else
620 VAL = 0;
621}
622
Benjamin Kramer478851c2012-07-04 17:04:04 +0000623IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
624 QualType type, SourceLocation l)
625 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
626 false, false),
627 Loc(l) {
628 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
629 assert(V.getBitWidth() == C.getIntWidth(type) &&
630 "Integer type is not the correct size for constant.");
631 setValue(C, V);
632}
633
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000634IntegerLiteral *
635IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
636 QualType type, SourceLocation l) {
637 return new (C) IntegerLiteral(C, V, type, l);
638}
639
640IntegerLiteral *
641IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
642 return new (C) IntegerLiteral(Empty);
643}
644
Benjamin Kramer478851c2012-07-04 17:04:04 +0000645FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
646 bool isexact, QualType Type, SourceLocation L)
647 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
648 false, false), Loc(L) {
649 FloatingLiteralBits.IsIEEE =
650 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
651 FloatingLiteralBits.IsExact = isexact;
652 setValue(C, V);
653}
654
655FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
656 : Expr(FloatingLiteralClass, Empty) {
657 FloatingLiteralBits.IsIEEE =
658 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
659 FloatingLiteralBits.IsExact = false;
660}
661
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000662FloatingLiteral *
663FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
664 bool isexact, QualType Type, SourceLocation L) {
665 return new (C) FloatingLiteral(C, V, isexact, Type, L);
666}
667
668FloatingLiteral *
669FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000670 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000671}
672
Chris Lattnerda8249e2008-06-07 22:13:43 +0000673/// getValueAsApproximateDouble - This returns the value as an inaccurate
674/// double. Note that this may cause loss of precision, but is useful for
675/// debugging dumps, etc.
676double FloatingLiteral::getValueAsApproximateDouble() const {
677 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000678 bool ignored;
679 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
680 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000681 return V.convertToDouble();
682}
683
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000684int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000685 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000686 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000687 case Ascii:
688 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000689 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000690 break;
691 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000692 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000693 break;
694 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000695 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000696 break;
697 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000698 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000699 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000700 }
701 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
702 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000703 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000704 && "character byte widths supported are 1, 2, and 4 only");
705 return CharByteWidth;
706}
707
Chris Lattner5f9e2722011-07-23 10:55:15 +0000708StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000709 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000710 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000711 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000712 // Allocate enough space for the StringLiteral plus an array of locations for
713 // any concatenated string tokens.
714 void *Mem = C.Allocate(sizeof(StringLiteral)+
715 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000716 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000717 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000720 SL->setString(C,Str,Kind,Pascal);
721
Chris Lattner2085fd62009-02-18 06:40:38 +0000722 SL->TokLocs[0] = Loc[0];
723 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724
Chris Lattner726e1682009-02-18 05:49:11 +0000725 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000726 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
727 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000728}
729
Douglas Gregor673ecd62009-04-15 16:35:07 +0000730StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
731 void *Mem = C.Allocate(sizeof(StringLiteral)+
732 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000733 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000734 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000735 SL->CharByteWidth = 0;
736 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000737 SL->NumConcatenated = NumStrs;
738 return SL;
739}
740
Richard Trieu8ab09da2012-06-13 20:25:24 +0000741void StringLiteral::outputString(raw_ostream &OS) {
742 switch (getKind()) {
743 case Ascii: break; // no prefix.
744 case Wide: OS << 'L'; break;
745 case UTF8: OS << "u8"; break;
746 case UTF16: OS << 'u'; break;
747 case UTF32: OS << 'U'; break;
748 }
749 OS << '"';
750 static const char Hex[] = "0123456789ABCDEF";
751
752 unsigned LastSlashX = getLength();
753 for (unsigned I = 0, N = getLength(); I != N; ++I) {
754 switch (uint32_t Char = getCodeUnit(I)) {
755 default:
756 // FIXME: Convert UTF-8 back to codepoints before rendering.
757
758 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
759 // Leave invalid surrogates alone; we'll use \x for those.
760 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
761 Char <= 0xdbff) {
762 uint32_t Trail = getCodeUnit(I + 1);
763 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
764 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
765 ++I;
766 }
767 }
768
769 if (Char > 0xff) {
770 // If this is a wide string, output characters over 0xff using \x
771 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
772 // codepoint: use \x escapes for invalid codepoints.
773 if (getKind() == Wide ||
774 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
775 // FIXME: Is this the best way to print wchar_t?
776 OS << "\\x";
777 int Shift = 28;
778 while ((Char >> Shift) == 0)
779 Shift -= 4;
780 for (/**/; Shift >= 0; Shift -= 4)
781 OS << Hex[(Char >> Shift) & 15];
782 LastSlashX = I;
783 break;
784 }
785
786 if (Char > 0xffff)
787 OS << "\\U00"
788 << Hex[(Char >> 20) & 15]
789 << Hex[(Char >> 16) & 15];
790 else
791 OS << "\\u";
792 OS << Hex[(Char >> 12) & 15]
793 << Hex[(Char >> 8) & 15]
794 << Hex[(Char >> 4) & 15]
795 << Hex[(Char >> 0) & 15];
796 break;
797 }
798
799 // If we used \x... for the previous character, and this character is a
800 // hexadecimal digit, prevent it being slurped as part of the \x.
801 if (LastSlashX + 1 == I) {
802 switch (Char) {
803 case '0': case '1': case '2': case '3': case '4':
804 case '5': case '6': case '7': case '8': case '9':
805 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
806 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
807 OS << "\"\"";
808 }
809 }
810
811 assert(Char <= 0xff &&
812 "Characters above 0xff should already have been handled.");
813
814 if (isprint(Char))
815 OS << (char)Char;
816 else // Output anything hard as an octal escape.
817 OS << '\\'
818 << (char)('0' + ((Char >> 6) & 7))
819 << (char)('0' + ((Char >> 3) & 7))
820 << (char)('0' + ((Char >> 0) & 7));
821 break;
822 // Handle some common non-printable cases to make dumps prettier.
823 case '\\': OS << "\\\\"; break;
824 case '"': OS << "\\\""; break;
825 case '\n': OS << "\\n"; break;
826 case '\t': OS << "\\t"; break;
827 case '\a': OS << "\\a"; break;
828 case '\b': OS << "\\b"; break;
829 }
830 }
831 OS << '"';
832}
833
Eli Friedman64f45a22011-11-01 02:23:42 +0000834void StringLiteral::setString(ASTContext &C, StringRef Str,
835 StringKind Kind, bool IsPascal) {
836 //FIXME: we assume that the string data comes from a target that uses the same
837 // code unit size and endianess for the type of string.
838 this->Kind = Kind;
839 this->IsPascal = IsPascal;
840
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000841 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000842 assert((Str.size()%CharByteWidth == 0)
843 && "size of data must be multiple of CharByteWidth");
844 Length = Str.size()/CharByteWidth;
845
846 switch(CharByteWidth) {
847 case 1: {
848 char *AStrData = new (C) char[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000849 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000850 StrData.asChar = AStrData;
851 break;
852 }
853 case 2: {
854 uint16_t *AStrData = new (C) uint16_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000855 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000856 StrData.asUInt16 = AStrData;
857 break;
858 }
859 case 4: {
860 uint32_t *AStrData = new (C) uint32_t[Length];
Argyrios Kyrtzidis66dfef12012-09-14 21:17:41 +0000861 std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData));
Eli Friedman64f45a22011-11-01 02:23:42 +0000862 StrData.asUInt32 = AStrData;
863 break;
864 }
865 default:
866 assert(false && "unsupported CharByteWidth");
867 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000868}
869
Chris Lattner08f92e32010-11-17 07:37:15 +0000870/// getLocationOfByte - Return a source location that points to the specified
871/// byte of this string literal.
872///
873/// Strings are amazingly complex. They can be formed from multiple tokens and
874/// can have escape sequences in them in addition to the usual trigraph and
875/// escaped newline business. This routine handles this complexity.
876///
877SourceLocation StringLiteral::
878getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
879 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000880 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
881 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000882
Chris Lattner08f92e32010-11-17 07:37:15 +0000883 // Loop over all of the tokens in this string until we find the one that
884 // contains the byte we're looking for.
885 unsigned TokNo = 0;
886 while (1) {
887 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
888 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
889
890 // Get the spelling of the string so that we can get the data that makes up
891 // the string literal, not the identifier for the macro it is potentially
892 // expanded through.
893 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
894
895 // Re-lex the token to get its length and original spelling.
896 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
897 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000898 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000899 if (Invalid)
900 return StrTokSpellingLoc;
901
902 const char *StrData = Buffer.data()+LocInfo.second;
903
Chris Lattner08f92e32010-11-17 07:37:15 +0000904 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000905 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
906 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000907 Token TheTok;
908 TheLexer.LexFromRawLexer(TheTok);
909
910 // Use the StringLiteralParser to compute the length of the string in bytes.
911 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
912 unsigned TokNumBytes = SLP.GetStringLength();
913
914 // If the byte is in this token, return the location of the byte.
915 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000916 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000917 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
918
919 // Now that we know the offset of the token in the spelling, use the
920 // preprocessor to get the offset in the original source.
921 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
922 }
923
924 // Move to the next string token.
925 ++TokNo;
926 ByteNo -= TokNumBytes;
927 }
928}
929
930
931
Reid Spencer5f016e22007-07-11 17:01:13 +0000932/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
933/// corresponds to, e.g. "sizeof" or "[pre]++".
David Blaikie0bea8632012-10-08 01:11:04 +0000934StringRef UnaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000936 case UO_PostInc: return "++";
937 case UO_PostDec: return "--";
938 case UO_PreInc: return "++";
939 case UO_PreDec: return "--";
940 case UO_AddrOf: return "&";
941 case UO_Deref: return "*";
942 case UO_Plus: return "+";
943 case UO_Minus: return "-";
944 case UO_Not: return "~";
945 case UO_LNot: return "!";
946 case UO_Real: return "__real";
947 case UO_Imag: return "__imag";
948 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000950 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
John McCall2de56d12010-08-25 11:45:40 +0000953UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000954UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
955 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000956 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000957 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
958 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
959 case OO_Amp: return UO_AddrOf;
960 case OO_Star: return UO_Deref;
961 case OO_Plus: return UO_Plus;
962 case OO_Minus: return UO_Minus;
963 case OO_Tilde: return UO_Not;
964 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000965 }
966}
967
968OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
969 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000970 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
971 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
972 case UO_AddrOf: return OO_Amp;
973 case UO_Deref: return OO_Star;
974 case UO_Plus: return OO_Plus;
975 case UO_Minus: return OO_Minus;
976 case UO_Not: return OO_Tilde;
977 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000978 default: return OO_None;
979 }
980}
981
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983//===----------------------------------------------------------------------===//
984// Postfix Operators.
985//===----------------------------------------------------------------------===//
986
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000987CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000988 ArrayRef<Expr*> args, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000989 SourceLocation rparenloc)
990 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000991 fn->isTypeDependent(),
992 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000993 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000994 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000995 NumArgs(args.size()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000997 SubExprs = new (C) Stmt*[args.size()+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000998 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000999 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001000 if (args[i]->isTypeDependent())
1001 ExprBits.TypeDependent = true;
1002 if (args[i]->isValueDependent())
1003 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001004 if (args[i]->isInstantiationDependent())
1005 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001006 if (args[i]->containsUnexpandedParameterPack())
1007 ExprBits.ContainsUnexpandedParameterPack = true;
1008
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001009 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001010 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001011
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001012 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +00001013 RParenLoc = rparenloc;
1014}
Nate Begemane2ce1d92008-01-17 17:46:27 +00001015
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001016CallExpr::CallExpr(ASTContext& C, Expr *fn, ArrayRef<Expr*> args,
John McCallf89e55a2010-11-18 06:31:45 +00001017 QualType t, ExprValueKind VK, SourceLocation rparenloc)
1018 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001019 fn->isTypeDependent(),
1020 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001021 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001022 fn->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001023 NumArgs(args.size()) {
Ted Kremenek668bf912009-02-09 20:51:47 +00001024
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001025 SubExprs = new (C) Stmt*[args.size()+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001026 SubExprs[FN] = fn;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001027 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001028 if (args[i]->isTypeDependent())
1029 ExprBits.TypeDependent = true;
1030 if (args[i]->isValueDependent())
1031 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001032 if (args[i]->isInstantiationDependent())
1033 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001034 if (args[i]->containsUnexpandedParameterPack())
1035 ExprBits.ContainsUnexpandedParameterPack = true;
1036
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001037 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001038 }
Ted Kremenek668bf912009-02-09 20:51:47 +00001039
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001040 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 RParenLoc = rparenloc;
1042}
1043
Mike Stump1eb44332009-09-09 15:08:12 +00001044CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
1045 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001046 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001047 SubExprs = new (C) Stmt*[PREARGS_START];
1048 CallExprBits.NumPreArgs = 0;
1049}
1050
1051CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
1052 EmptyShell Empty)
1053 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
1054 // FIXME: Why do we allocate this?
1055 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
1056 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001057}
1058
Nuno Lopesd20254f2009-12-20 23:11:08 +00001059Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +00001060 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +00001061
1062 while (SubstNonTypeTemplateParmExpr *NTTP
1063 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
1064 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1065 }
1066
Sebastian Redl20012152010-09-10 20:55:30 +00001067 // If we're calling a dereference, look at the pointer instead.
1068 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1069 if (BO->isPtrMemOp())
1070 CEE = BO->getRHS()->IgnoreParenCasts();
1071 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1072 if (UO->getOpcode() == UO_Deref)
1073 CEE = UO->getSubExpr()->IgnoreParenCasts();
1074 }
Chris Lattner6346f962009-07-17 15:46:27 +00001075 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001076 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001077 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1078 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001079
1080 return 0;
1081}
1082
Nuno Lopesd20254f2009-12-20 23:11:08 +00001083FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001084 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001085}
1086
Chris Lattnerd18b3292007-12-28 05:25:02 +00001087/// setNumArgs - This changes the number of arguments present in this call.
1088/// Any orphaned expressions are deleted by this, and any new operands are set
1089/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001090void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001091 // No change, just return.
1092 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattnerd18b3292007-12-28 05:25:02 +00001094 // If shrinking # arguments, just delete the extras and forgot them.
1095 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001096 this->NumArgs = NumArgs;
1097 return;
1098 }
1099
1100 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001101 unsigned NumPreArgs = getNumPreArgs();
1102 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001103 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001104 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001105 NewSubExprs[i] = SubExprs[i];
1106 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001107 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1108 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001109 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor88c9a462009-04-17 21:46:47 +00001111 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001112 SubExprs = NewSubExprs;
1113 this->NumArgs = NumArgs;
1114}
1115
Chris Lattnercb888962008-10-06 05:00:53 +00001116/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1117/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001118unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001119 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001120 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001121 // ImplicitCastExpr.
1122 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1123 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001124 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001126 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1127 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001128 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Anders Carlssonbcba2012008-01-31 02:13:57 +00001130 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1131 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001132 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001134 if (!FDecl->getIdentifier())
1135 return 0;
1136
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001137 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001138}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001139
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001140QualType CallExpr::getCallReturnType() const {
1141 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001142 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001143 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001144 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001145 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001146 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1147 // This should never be overloaded and so should never return null.
1148 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001149
John McCall864c0412011-04-26 20:42:42 +00001150 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001151 return FnType->getResultType();
1152}
Chris Lattnercb888962008-10-06 05:00:53 +00001153
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001154SourceLocation CallExpr::getLocStart() const {
1155 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001156 return cast<CXXOperatorCallExpr>(this)->getLocStart();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001157
1158 SourceLocation begin = getCallee()->getLocStart();
1159 if (begin.isInvalid() && getNumArgs() > 0)
1160 begin = getArg(0)->getLocStart();
1161 return begin;
1162}
1163SourceLocation CallExpr::getLocEnd() const {
1164 if (isa<CXXOperatorCallExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +00001165 return cast<CXXOperatorCallExpr>(this)->getLocEnd();
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001166
1167 SourceLocation end = getRParenLoc();
1168 if (end.isInvalid() && getNumArgs() > 0)
1169 end = getArg(getNumArgs() - 1)->getLocEnd();
1170 return end;
1171}
John McCall2882eca2011-02-21 06:23:05 +00001172
Sean Huntc3021132010-05-05 15:23:54 +00001173OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001174 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001175 TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001176 ArrayRef<OffsetOfNode> comps,
1177 ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001178 SourceLocation RParenLoc) {
1179 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001180 sizeof(OffsetOfNode) * comps.size() +
1181 sizeof(Expr*) * exprs.size());
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001182
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001183 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs,
1184 RParenLoc);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001185}
1186
1187OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1188 unsigned numComps, unsigned numExprs) {
1189 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1190 sizeof(OffsetOfNode) * numComps +
1191 sizeof(Expr*) * numExprs);
1192 return new (Mem) OffsetOfExpr(numComps, numExprs);
1193}
1194
Sean Huntc3021132010-05-05 15:23:54 +00001195OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001196 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001197 ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001198 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001199 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1200 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001201 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001202 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001203 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001204 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001205 NumComps(comps.size()), NumExprs(exprs.size())
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001206{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001207 for (unsigned i = 0; i != comps.size(); ++i) {
1208 setComponent(i, comps[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001209 }
Sean Huntc3021132010-05-05 15:23:54 +00001210
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001211 for (unsigned i = 0; i != exprs.size(); ++i) {
1212 if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001213 ExprBits.ValueDependent = true;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001214 if (exprs[i]->containsUnexpandedParameterPack())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001215 ExprBits.ContainsUnexpandedParameterPack = true;
1216
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001217 setIndexExpr(i, exprs[i]);
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001218 }
1219}
1220
1221IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1222 assert(getKind() == Field || getKind() == Identifier);
1223 if (getKind() == Field)
1224 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001225
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001226 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1227}
1228
Mike Stump1eb44332009-09-09 15:08:12 +00001229MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001230 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001231 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001232 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001233 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001234 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001235 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001236 QualType ty,
1237 ExprValueKind vk,
1238 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001239 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001240
Douglas Gregor40d96a62011-02-28 21:54:11 +00001241 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001242 founddecl.getDecl() != memberdecl ||
1243 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001244 if (hasQualOrFound)
1245 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001246
John McCalld5532b62009-11-23 01:53:49 +00001247 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001248 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1249 else if (TemplateKWLoc.isValid())
1250 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Chris Lattner32488542010-10-30 05:14:06 +00001252 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001253 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1254 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001255
1256 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001257 // FIXME: Wrong. We should be looking at the member declaration we found.
1258 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001259 E->setValueDependent(true);
1260 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001261 E->setInstantiationDependent(true);
1262 }
1263 else if (QualifierLoc &&
1264 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1265 E->setInstantiationDependent(true);
1266
John McCall6bb80172010-03-30 21:47:33 +00001267 E->HasQualifierOrFoundDecl = true;
1268
1269 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001270 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001271 NQ->FoundDecl = founddecl;
1272 }
1273
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001274 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1275
John McCall6bb80172010-03-30 21:47:33 +00001276 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001277 bool Dependent = false;
1278 bool InstantiationDependent = false;
1279 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001280 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1281 Dependent,
1282 InstantiationDependent,
1283 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001284 if (InstantiationDependent)
1285 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001286 } else if (TemplateKWLoc.isValid()) {
1287 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001288 }
1289
1290 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001291}
1292
Daniel Dunbar396ec672012-03-09 15:39:15 +00001293SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001294 if (isImplicitAccess()) {
1295 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001296 return getQualifierLoc().getBeginLoc();
1297 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001298 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001299
Daniel Dunbar396ec672012-03-09 15:39:15 +00001300 // FIXME: We don't want this to happen. Rather, we should be able to
1301 // detect all kinds of implicit accesses more cleanly.
1302 SourceLocation BaseStartLoc = getBase()->getLocStart();
1303 if (BaseStartLoc.isValid())
1304 return BaseStartLoc;
1305 return MemberLoc;
1306}
1307SourceLocation MemberExpr::getLocEnd() const {
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001308 SourceLocation EndLoc = getMemberNameInfo().getEndLoc();
Daniel Dunbar396ec672012-03-09 15:39:15 +00001309 if (hasExplicitTemplateArgs())
Abramo Bagnara13fd6842012-11-08 13:52:58 +00001310 EndLoc = getRAngleLoc();
1311 else if (EndLoc.isInvalid())
1312 EndLoc = getBase()->getLocEnd();
1313 return EndLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001314}
1315
John McCall1d9b3b22011-09-09 05:25:32 +00001316void CastExpr::CheckCastConsistency() const {
1317 switch (getCastKind()) {
1318 case CK_DerivedToBase:
1319 case CK_UncheckedDerivedToBase:
1320 case CK_DerivedToBaseMemberPointer:
1321 case CK_BaseToDerived:
1322 case CK_BaseToDerivedMemberPointer:
1323 assert(!path_empty() && "Cast kind should have a base path!");
1324 break;
1325
1326 case CK_CPointerToObjCPointerCast:
1327 assert(getType()->isObjCObjectPointerType());
1328 assert(getSubExpr()->getType()->isPointerType());
1329 goto CheckNoBasePath;
1330
1331 case CK_BlockPointerToObjCPointerCast:
1332 assert(getType()->isObjCObjectPointerType());
1333 assert(getSubExpr()->getType()->isBlockPointerType());
1334 goto CheckNoBasePath;
1335
John McCall4d4e5c12012-02-15 01:22:51 +00001336 case CK_ReinterpretMemberPointer:
1337 assert(getType()->isMemberPointerType());
1338 assert(getSubExpr()->getType()->isMemberPointerType());
1339 goto CheckNoBasePath;
1340
John McCall1d9b3b22011-09-09 05:25:32 +00001341 case CK_BitCast:
1342 // Arbitrary casts to C pointer types count as bitcasts.
1343 // Otherwise, we should only have block and ObjC pointer casts
1344 // here if they stay within the type kind.
1345 if (!getType()->isPointerType()) {
1346 assert(getType()->isObjCObjectPointerType() ==
1347 getSubExpr()->getType()->isObjCObjectPointerType());
1348 assert(getType()->isBlockPointerType() ==
1349 getSubExpr()->getType()->isBlockPointerType());
1350 }
1351 goto CheckNoBasePath;
1352
1353 case CK_AnyPointerToBlockPointerCast:
1354 assert(getType()->isBlockPointerType());
1355 assert(getSubExpr()->getType()->isAnyPointerType() &&
1356 !getSubExpr()->getType()->isBlockPointerType());
1357 goto CheckNoBasePath;
1358
Douglas Gregorac1303e2012-02-22 05:02:47 +00001359 case CK_CopyAndAutoreleaseBlockObject:
1360 assert(getType()->isBlockPointerType());
1361 assert(getSubExpr()->getType()->isBlockPointerType());
1362 goto CheckNoBasePath;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001363
1364 case CK_FunctionToPointerDecay:
1365 assert(getType()->isPointerType());
1366 assert(getSubExpr()->getType()->isFunctionType());
1367 goto CheckNoBasePath;
1368
John McCall1d9b3b22011-09-09 05:25:32 +00001369 // These should not have an inheritance path.
1370 case CK_Dynamic:
1371 case CK_ToUnion:
1372 case CK_ArrayToPointerDecay:
John McCall1d9b3b22011-09-09 05:25:32 +00001373 case CK_NullToMemberPointer:
1374 case CK_NullToPointer:
1375 case CK_ConstructorConversion:
1376 case CK_IntegralToPointer:
1377 case CK_PointerToIntegral:
1378 case CK_ToVoid:
1379 case CK_VectorSplat:
1380 case CK_IntegralCast:
1381 case CK_IntegralToFloating:
1382 case CK_FloatingToIntegral:
1383 case CK_FloatingCast:
1384 case CK_ObjCObjectLValueCast:
1385 case CK_FloatingRealToComplex:
1386 case CK_FloatingComplexToReal:
1387 case CK_FloatingComplexCast:
1388 case CK_FloatingComplexToIntegralComplex:
1389 case CK_IntegralRealToComplex:
1390 case CK_IntegralComplexToReal:
1391 case CK_IntegralComplexCast:
1392 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001393 case CK_ARCProduceObject:
1394 case CK_ARCConsumeObject:
1395 case CK_ARCReclaimReturnedObject:
1396 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001397 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1398 goto CheckNoBasePath;
1399
1400 case CK_Dependent:
1401 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001402 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001403 case CK_AtomicToNonAtomic:
1404 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001405 case CK_PointerToBoolean:
1406 case CK_IntegralToBoolean:
1407 case CK_FloatingToBoolean:
1408 case CK_MemberPointerToBoolean:
1409 case CK_FloatingComplexToBoolean:
1410 case CK_IntegralComplexToBoolean:
1411 case CK_LValueBitCast: // -> bool&
1412 case CK_UserDefinedConversion: // operator bool()
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001413 case CK_BuiltinFnToFnPtr:
John McCall1d9b3b22011-09-09 05:25:32 +00001414 CheckNoBasePath:
1415 assert(path_empty() && "Cast kind should not have a base path!");
1416 break;
1417 }
1418}
1419
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001420const char *CastExpr::getCastKindName() const {
1421 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001422 case CK_Dependent:
1423 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001424 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001425 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001426 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001427 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001428 case CK_LValueToRValue:
1429 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001430 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001431 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001432 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001433 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001434 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001435 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001436 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001437 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001438 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001439 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001440 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001441 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001442 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001443 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001444 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001445 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001446 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001447 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001448 case CK_NullToPointer:
1449 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001450 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001451 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001452 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001453 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001454 case CK_ReinterpretMemberPointer:
1455 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001456 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001457 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001458 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001459 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001460 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001461 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001462 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001463 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001464 case CK_PointerToBoolean:
1465 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001466 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001467 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001468 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001469 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001470 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001471 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001472 case CK_IntegralToBoolean:
1473 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001474 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001475 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001476 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001477 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001478 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001479 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001480 case CK_FloatingToBoolean:
1481 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001482 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001483 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001484 case CK_CPointerToObjCPointerCast:
1485 return "CPointerToObjCPointerCast";
1486 case CK_BlockPointerToObjCPointerCast:
1487 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001488 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001489 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001490 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001491 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001492 case CK_FloatingRealToComplex:
1493 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001494 case CK_FloatingComplexToReal:
1495 return "FloatingComplexToReal";
1496 case CK_FloatingComplexToBoolean:
1497 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001498 case CK_FloatingComplexCast:
1499 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001500 case CK_FloatingComplexToIntegralComplex:
1501 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001502 case CK_IntegralRealToComplex:
1503 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001504 case CK_IntegralComplexToReal:
1505 return "IntegralComplexToReal";
1506 case CK_IntegralComplexToBoolean:
1507 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001508 case CK_IntegralComplexCast:
1509 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001510 case CK_IntegralComplexToFloatingComplex:
1511 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001512 case CK_ARCConsumeObject:
1513 return "ARCConsumeObject";
1514 case CK_ARCProduceObject:
1515 return "ARCProduceObject";
1516 case CK_ARCReclaimReturnedObject:
1517 return "ARCReclaimReturnedObject";
1518 case CK_ARCExtendBlockObject:
1519 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001520 case CK_AtomicToNonAtomic:
1521 return "AtomicToNonAtomic";
1522 case CK_NonAtomicToAtomic:
1523 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001524 case CK_CopyAndAutoreleaseBlockObject:
1525 return "CopyAndAutoreleaseBlockObject";
Eli Friedmana6c66ce2012-08-31 00:14:07 +00001526 case CK_BuiltinFnToFnPtr:
1527 return "BuiltinFnToFnPtr";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
John McCall2bb5d002010-11-13 09:02:35 +00001530 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001531}
1532
Douglas Gregor6eef5192009-12-14 19:27:10 +00001533Expr *CastExpr::getSubExprAsWritten() {
1534 Expr *SubExpr = 0;
1535 CastExpr *E = this;
1536 do {
1537 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001538
1539 // Skip through reference binding to temporary.
1540 if (MaterializeTemporaryExpr *Materialize
1541 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1542 SubExpr = Materialize->GetTemporaryExpr();
1543
Douglas Gregor6eef5192009-12-14 19:27:10 +00001544 // Skip any temporary bindings; they're implicit.
1545 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1546 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001547
Douglas Gregor6eef5192009-12-14 19:27:10 +00001548 // Conversions by constructor and conversion functions have a
1549 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001550 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001551 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001552 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001553 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001554
Douglas Gregor6eef5192009-12-14 19:27:10 +00001555 // If the subexpression we're left with is an implicit cast, look
1556 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001557 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1558
Douglas Gregor6eef5192009-12-14 19:27:10 +00001559 return SubExpr;
1560}
1561
John McCallf871d0c2010-08-07 06:22:56 +00001562CXXBaseSpecifier **CastExpr::path_buffer() {
1563 switch (getStmtClass()) {
1564#define ABSTRACT_STMT(x)
1565#define CASTEXPR(Type, Base) \
1566 case Stmt::Type##Class: \
1567 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1568#define STMT(Type, Base)
1569#include "clang/AST/StmtNodes.inc"
1570 default:
1571 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001572 }
1573}
1574
1575void CastExpr::setCastPath(const CXXCastPath &Path) {
1576 assert(Path.size() == path_size());
1577 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1578}
1579
1580ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1581 CastKind Kind, Expr *Operand,
1582 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001583 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001584 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1585 void *Buffer =
1586 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1587 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001588 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001589 if (PathSize) E->setCastPath(*BasePath);
1590 return E;
1591}
1592
1593ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1594 unsigned PathSize) {
1595 void *Buffer =
1596 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1597 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1598}
1599
1600
1601CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001602 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001603 const CXXCastPath *BasePath,
1604 TypeSourceInfo *WrittenTy,
1605 SourceLocation L, SourceLocation R) {
1606 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1607 void *Buffer =
1608 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1609 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001610 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001611 if (PathSize) E->setCastPath(*BasePath);
1612 return E;
1613}
1614
1615CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1616 void *Buffer =
1617 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1618 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1619}
1620
Reid Spencer5f016e22007-07-11 17:01:13 +00001621/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1622/// corresponds to, e.g. "<<=".
David Blaikie0bea8632012-10-08 01:11:04 +00001623StringRef BinaryOperator::getOpcodeStr(Opcode Op) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001625 case BO_PtrMemD: return ".*";
1626 case BO_PtrMemI: return "->*";
1627 case BO_Mul: return "*";
1628 case BO_Div: return "/";
1629 case BO_Rem: return "%";
1630 case BO_Add: return "+";
1631 case BO_Sub: return "-";
1632 case BO_Shl: return "<<";
1633 case BO_Shr: return ">>";
1634 case BO_LT: return "<";
1635 case BO_GT: return ">";
1636 case BO_LE: return "<=";
1637 case BO_GE: return ">=";
1638 case BO_EQ: return "==";
1639 case BO_NE: return "!=";
1640 case BO_And: return "&";
1641 case BO_Xor: return "^";
1642 case BO_Or: return "|";
1643 case BO_LAnd: return "&&";
1644 case BO_LOr: return "||";
1645 case BO_Assign: return "=";
1646 case BO_MulAssign: return "*=";
1647 case BO_DivAssign: return "/=";
1648 case BO_RemAssign: return "%=";
1649 case BO_AddAssign: return "+=";
1650 case BO_SubAssign: return "-=";
1651 case BO_ShlAssign: return "<<=";
1652 case BO_ShrAssign: return ">>=";
1653 case BO_AndAssign: return "&=";
1654 case BO_XorAssign: return "^=";
1655 case BO_OrAssign: return "|=";
1656 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001658
David Blaikie30263482012-01-20 21:50:17 +00001659 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001660}
1661
John McCall2de56d12010-08-25 11:45:40 +00001662BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001663BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1664 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001665 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001666 case OO_Plus: return BO_Add;
1667 case OO_Minus: return BO_Sub;
1668 case OO_Star: return BO_Mul;
1669 case OO_Slash: return BO_Div;
1670 case OO_Percent: return BO_Rem;
1671 case OO_Caret: return BO_Xor;
1672 case OO_Amp: return BO_And;
1673 case OO_Pipe: return BO_Or;
1674 case OO_Equal: return BO_Assign;
1675 case OO_Less: return BO_LT;
1676 case OO_Greater: return BO_GT;
1677 case OO_PlusEqual: return BO_AddAssign;
1678 case OO_MinusEqual: return BO_SubAssign;
1679 case OO_StarEqual: return BO_MulAssign;
1680 case OO_SlashEqual: return BO_DivAssign;
1681 case OO_PercentEqual: return BO_RemAssign;
1682 case OO_CaretEqual: return BO_XorAssign;
1683 case OO_AmpEqual: return BO_AndAssign;
1684 case OO_PipeEqual: return BO_OrAssign;
1685 case OO_LessLess: return BO_Shl;
1686 case OO_GreaterGreater: return BO_Shr;
1687 case OO_LessLessEqual: return BO_ShlAssign;
1688 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1689 case OO_EqualEqual: return BO_EQ;
1690 case OO_ExclaimEqual: return BO_NE;
1691 case OO_LessEqual: return BO_LE;
1692 case OO_GreaterEqual: return BO_GE;
1693 case OO_AmpAmp: return BO_LAnd;
1694 case OO_PipePipe: return BO_LOr;
1695 case OO_Comma: return BO_Comma;
1696 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001697 }
1698}
1699
1700OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1701 static const OverloadedOperatorKind OverOps[] = {
1702 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1703 OO_Star, OO_Slash, OO_Percent,
1704 OO_Plus, OO_Minus,
1705 OO_LessLess, OO_GreaterGreater,
1706 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1707 OO_EqualEqual, OO_ExclaimEqual,
1708 OO_Amp,
1709 OO_Caret,
1710 OO_Pipe,
1711 OO_AmpAmp,
1712 OO_PipePipe,
1713 OO_Equal, OO_StarEqual,
1714 OO_SlashEqual, OO_PercentEqual,
1715 OO_PlusEqual, OO_MinusEqual,
1716 OO_LessLessEqual, OO_GreaterGreaterEqual,
1717 OO_AmpEqual, OO_CaretEqual,
1718 OO_PipeEqual,
1719 OO_Comma
1720 };
1721 return OverOps[Opc];
1722}
1723
Ted Kremenek709210f2010-04-13 23:39:13 +00001724InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001725 ArrayRef<Expr*> initExprs, SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001726 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001727 false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001728 InitExprs(C, initExprs.size()),
Abramo Bagnara23700f02012-11-08 18:41:43 +00001729 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(0, true)
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001730{
1731 sawArrayRangeDesignator(false);
1732 setInitializesStdInitializerList(false);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001733 for (unsigned I = 0; I != initExprs.size(); ++I) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001734 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001735 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001736 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001737 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001738 if (initExprs[I]->isInstantiationDependent())
1739 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001740 if (initExprs[I]->containsUnexpandedParameterPack())
1741 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001742 }
Sean Huntc3021132010-05-05 15:23:54 +00001743
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001744 InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end());
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001745}
Reid Spencer5f016e22007-07-11 17:01:13 +00001746
Ted Kremenek709210f2010-04-13 23:39:13 +00001747void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001748 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001749 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001750}
1751
Ted Kremenek709210f2010-04-13 23:39:13 +00001752void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001753 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001754}
1755
Ted Kremenek709210f2010-04-13 23:39:13 +00001756Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001757 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001758 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001759 InitExprs.back() = expr;
1760 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Douglas Gregor4c678342009-01-28 21:54:33 +00001763 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1764 InitExprs[Init] = expr;
1765 return Result;
1766}
1767
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001768void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001769 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001770 ArrayFillerOrUnionFieldInit = filler;
1771 // Fill out any "holes" in the array due to designated initializers.
1772 Expr **inits = getInits();
1773 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1774 if (inits[i] == 0)
1775 inits[i] = filler;
1776}
1777
Richard Smithfe587202012-04-15 02:50:59 +00001778bool InitListExpr::isStringLiteralInit() const {
1779 if (getNumInits() != 1)
1780 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001781 const ArrayType *AT = getType()->getAsArrayTypeUnsafe();
1782 if (!AT || !AT->getElementType()->isIntegerType())
Richard Smithfe587202012-04-15 02:50:59 +00001783 return false;
Eli Friedmanf0a26492012-08-20 20:55:45 +00001784 const Expr *Init = getInit(0)->IgnoreParens();
Richard Smithfe587202012-04-15 02:50:59 +00001785 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1786}
1787
Erik Verbruggen65d78312012-12-25 14:51:39 +00001788SourceLocation InitListExpr::getLocStart() const {
Abramo Bagnara23700f02012-11-08 18:41:43 +00001789 if (InitListExpr *SyntacticForm = getSyntacticForm())
Erik Verbruggen65d78312012-12-25 14:51:39 +00001790 return SyntacticForm->getLocStart();
1791 SourceLocation Beg = LBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001792 if (Beg.isInvalid()) {
1793 // Find the first non-null initializer.
1794 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1795 E = InitExprs.end();
1796 I != E; ++I) {
1797 if (Stmt *S = *I) {
1798 Beg = S->getLocStart();
1799 break;
1800 }
1801 }
1802 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001803 return Beg;
1804}
1805
1806SourceLocation InitListExpr::getLocEnd() const {
1807 if (InitListExpr *SyntacticForm = getSyntacticForm())
1808 return SyntacticForm->getLocEnd();
1809 SourceLocation End = RBraceLoc;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001810 if (End.isInvalid()) {
1811 // Find the first non-null initializer from the end.
1812 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
Erik Verbruggen65d78312012-12-25 14:51:39 +00001813 E = InitExprs.rend();
1814 I != E; ++I) {
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001815 if (Stmt *S = *I) {
Erik Verbruggen65d78312012-12-25 14:51:39 +00001816 End = S->getLocEnd();
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001817 break;
Erik Verbruggen65d78312012-12-25 14:51:39 +00001818 }
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001819 }
1820 }
Erik Verbruggen65d78312012-12-25 14:51:39 +00001821 return End;
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001822}
1823
Steve Naroffbfdcae62008-09-04 15:31:07 +00001824/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001825///
John McCalla345edb2012-02-17 03:32:35 +00001826const FunctionProtoType *BlockExpr::getFunctionType() const {
1827 // The block pointer is never sugared, but the function type might be.
1828 return cast<BlockPointerType>(getType())
1829 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001830}
1831
Mike Stump1eb44332009-09-09 15:08:12 +00001832SourceLocation BlockExpr::getCaretLocation() const {
1833 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001834}
Mike Stump1eb44332009-09-09 15:08:12 +00001835const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001836 return TheBlock->getBody();
1837}
Mike Stump1eb44332009-09-09 15:08:12 +00001838Stmt *BlockExpr::getBody() {
1839 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001840}
Steve Naroff56ee6892008-10-08 17:01:13 +00001841
1842
Reid Spencer5f016e22007-07-11 17:01:13 +00001843//===----------------------------------------------------------------------===//
1844// Generic Expression Routines
1845//===----------------------------------------------------------------------===//
1846
Chris Lattner026dc962009-02-14 07:37:35 +00001847/// isUnusedResultAWarning - Return true if this immediate expression should
1848/// be warned about if the result is unused. If so, fill in Loc and Ranges
1849/// with location to warn on and the source range[s] to report with the
1850/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001851bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1852 SourceRange &R1, SourceRange &R2,
1853 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001854 // Don't warn if the expr is type dependent. The type could end up
1855 // instantiating to void.
1856 if (isTypeDependent())
1857 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 switch (getStmtClass()) {
1860 default:
John McCall0faede62010-03-12 07:11:26 +00001861 if (getType()->isVoidType())
1862 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001863 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001864 Loc = getExprLoc();
1865 R1 = getSourceRange();
1866 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001868 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001869 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001870 case GenericSelectionExprClass:
1871 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001872 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 case UnaryOperatorClass: {
1874 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001877 case UO_Plus:
1878 case UO_Minus:
1879 case UO_AddrOf:
1880 case UO_Not:
1881 case UO_LNot:
1882 case UO_Deref:
1883 break;
John McCall2de56d12010-08-25 11:45:40 +00001884 case UO_PostInc:
1885 case UO_PostDec:
1886 case UO_PreInc:
1887 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001888 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001889 case UO_Real:
1890 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001892 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1893 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001894 return false;
1895 break;
John McCall2de56d12010-08-25 11:45:40 +00001896 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001897 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 }
Eli Friedmana6115062012-05-24 00:47:05 +00001899 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001900 Loc = UO->getOperatorLoc();
1901 R1 = UO->getSubExpr()->getSourceRange();
1902 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001904 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001905 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001906 switch (BO->getOpcode()) {
1907 default:
1908 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001909 // Consider the RHS of comma for side effects. LHS was checked by
1910 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001911 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001912 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1913 // lvalue-ness) of an assignment written in a macro.
1914 if (IntegerLiteral *IE =
1915 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1916 if (IE->getValue() == 0)
1917 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001918 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001919 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001920 case BO_LAnd:
1921 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001922 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1923 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001924 return false;
1925 break;
John McCallbf0ee352010-02-16 04:10:53 +00001926 }
Chris Lattner026dc962009-02-14 07:37:35 +00001927 if (BO->isAssignmentOp())
1928 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001929 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001930 Loc = BO->getOperatorLoc();
1931 R1 = BO->getLHS()->getSourceRange();
1932 R2 = BO->getRHS()->getSourceRange();
1933 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001934 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001935 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001936 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001937 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001938 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001939
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001940 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001941 // If only one of the LHS or RHS is a warning, the operator might
1942 // be being used for control flow. Only warn if both the LHS and
1943 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001944 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001945 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001946 return false;
1947 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001948 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001949 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001950 }
1951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001953 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001954 Loc = cast<MemberExpr>(this)->getMemberLoc();
1955 R1 = SourceRange(Loc, Loc);
1956 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1957 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001960 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001961 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1962 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1963 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1964 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001965
Chandler Carruth9b106832011-08-17 09:49:44 +00001966 case CXXOperatorCallExprClass: {
1967 // We warn about operator== and operator!= even when user-defined operator
1968 // overloads as there is no reasonable way to define these such that they
1969 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1970 // warning: these operators are commonly typo'ed, and so warning on them
1971 // provides additional value as well. If this list is updated,
1972 // DiagnoseUnusedComparison should be as well.
1973 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1974 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001975 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001976 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001977 Loc = Op->getOperatorLoc();
1978 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001979 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001980 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001981
1982 // Fallthrough for generic call handling.
1983 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001985 case CXXMemberCallExprClass:
1986 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001987 // If this is a direct call, get the callee.
1988 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001989 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001990 // If the callee has attribute pure, const, or warn_unused_result, warn
1991 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001992 //
1993 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1994 // updated to match for QoI.
1995 if (FD->getAttr<WarnUnusedResultAttr>() ||
1996 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001997 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001998 Loc = CE->getCallee()->getLocStart();
1999 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00002001 if (unsigned NumArgs = CE->getNumArgs())
2002 R2 = SourceRange(CE->getArg(0)->getLocStart(),
2003 CE->getArg(NumArgs-1)->getLocEnd());
2004 return true;
2005 }
Chris Lattner026dc962009-02-14 07:37:35 +00002006 }
2007 return false;
2008 }
Anders Carlsson58beed92009-11-17 17:11:23 +00002009
Matt Beaumont-Gay84c3b972012-10-23 06:15:26 +00002010 // If we don't know precisely what we're looking at, let's not warn.
2011 case UnresolvedLookupExprClass:
2012 case CXXUnresolvedConstructExprClass:
2013 return false;
2014
Anders Carlsson58beed92009-11-17 17:11:23 +00002015 case CXXTemporaryObjectExprClass:
2016 case CXXConstructExprClass:
2017 return false;
2018
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002019 case ObjCMessageExprClass: {
2020 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00002021 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002022 ME->isInstanceMessage() &&
2023 !ME->getType()->isVoidType() &&
2024 ME->getSelector().getIdentifierInfoForSlot(0) &&
2025 ME->getSelector().getIdentifierInfoForSlot(0)
2026 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00002027 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00002028 Loc = getExprLoc();
2029 R1 = ME->getSourceRange();
2030 return true;
2031 }
2032
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002033 const ObjCMethodDecl *MD = ME->getMethodDecl();
2034 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00002035 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002036 Loc = getExprLoc();
2037 return true;
2038 }
Chris Lattner026dc962009-02-14 07:37:35 +00002039 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00002040 }
Mike Stump1eb44332009-09-09 15:08:12 +00002041
John McCall12f78a62010-12-02 01:19:52 +00002042 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002043 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00002044 Loc = getExprLoc();
2045 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00002046 return true;
John McCall12f78a62010-12-02 01:19:52 +00002047
John McCall4b9c2d22011-11-06 09:01:30 +00002048 case PseudoObjectExprClass: {
2049 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2050
2051 // Only complain about things that have the form of a getter.
2052 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
2053 isa<BinaryOperator>(PO->getSyntacticForm()))
2054 return false;
2055
Eli Friedmana6115062012-05-24 00:47:05 +00002056 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00002057 Loc = getExprLoc();
2058 R1 = getSourceRange();
2059 return true;
2060 }
2061
Chris Lattner611b2ec2008-07-26 19:51:01 +00002062 case StmtExprClass: {
2063 // Statement exprs don't logically have side effects themselves, but are
2064 // sometimes used in macros in ways that give them a type that is unused.
2065 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
2066 // however, if the result of the stmt expr is dead, we don't want to emit a
2067 // warning.
2068 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002069 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002070 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002071 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002072 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2073 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002074 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002075 }
Mike Stump1eb44332009-09-09 15:08:12 +00002076
John McCall0faede62010-03-12 07:11:26 +00002077 if (getType()->isVoidType())
2078 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002079 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002080 Loc = cast<StmtExpr>(this)->getLParenLoc();
2081 R1 = getSourceRange();
2082 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002083 }
Eli Friedman63199172012-09-24 23:02:26 +00002084 case CXXFunctionalCastExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00002085 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002086 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002087 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002088 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002089 if (CE->getCastKind() == CK_ToVoid) {
2090 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002091 CE->getSubExpr()->getType().isVolatileQualified()) {
2092 const DeclRefExpr *DRE =
2093 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2094 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2095 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2096 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2097 R1, R2, Ctx);
2098 }
2099 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002100 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002101 }
Eli Friedman4059da82012-05-24 21:05:41 +00002102
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +00002103 // Ignore casts within macro expansions.
2104 if (getExprLoc().isMacroID())
2105 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2106
Eli Friedmana6115062012-05-24 00:47:05 +00002107 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002108 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002109 if (CE->getCastKind() == CK_ConstructorConversion)
2110 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002111
Eli Friedmana6115062012-05-24 00:47:05 +00002112 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002113 if (const CXXFunctionalCastExpr *CXXCE =
2114 dyn_cast<CXXFunctionalCastExpr>(this)) {
2115 Loc = CXXCE->getTypeBeginLoc();
2116 R1 = CXXCE->getSubExpr()->getSourceRange();
2117 } else {
2118 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2119 Loc = CStyleCE->getLParenLoc();
2120 R1 = CStyleCE->getSubExpr()->getSourceRange();
2121 }
Chris Lattner026dc962009-02-14 07:37:35 +00002122 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002123 }
Eli Friedmana6115062012-05-24 00:47:05 +00002124 case ImplicitCastExprClass: {
2125 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002126
Eli Friedmana6115062012-05-24 00:47:05 +00002127 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2128 if (ICE->getCastKind() == CK_LValueToRValue &&
2129 ICE->getSubExpr()->getType().isVolatileQualified())
2130 return false;
2131
2132 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2133 }
Chris Lattner04421082008-04-08 04:40:51 +00002134 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002135 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002136 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002137
2138 case CXXNewExprClass:
2139 // FIXME: In theory, there might be new expressions that don't have side
2140 // effects (e.g. a placement new with an uninitialized POD).
2141 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002142 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002143 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002144 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002145 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002146 case ExprWithCleanupsClass:
2147 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002148 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002149 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002150}
2151
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002152/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002153/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002154bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002155 const Expr *E = IgnoreParens();
2156 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002157 default:
2158 return false;
2159 case ObjCIvarRefExprClass:
2160 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002161 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002162 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002163 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002164 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002165 case MaterializeTemporaryExprClass:
2166 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2167 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002168 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002169 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002170 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002171 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002172
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002173 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2174 if (VD->hasGlobalStorage())
2175 return true;
2176 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002177 // dereferencing to a pointer is always a gc'able candidate,
2178 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002179 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002180 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002181 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002182 return false;
2183 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002184 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002185 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002186 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002187 }
2188 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002189 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002190 }
2191}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002192
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002193bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2194 if (isTypeDependent())
2195 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002196 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002197}
2198
John McCall864c0412011-04-26 20:42:42 +00002199QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002200 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002201
2202 // Bound member expressions are always one of these possibilities:
2203 // x->m x.m x->*y x.*y
2204 // (possibly parenthesized)
2205
2206 expr = expr->IgnoreParens();
2207 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2208 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2209 return mem->getMemberDecl()->getType();
2210 }
2211
2212 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2213 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2214 ->getPointeeType();
2215 assert(type->isFunctionType());
2216 return type;
2217 }
2218
2219 assert(isa<UnresolvedMemberExpr>(expr));
2220 return QualType();
2221}
2222
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002223Expr* Expr::IgnoreParens() {
2224 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002225 while (true) {
2226 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2227 E = P->getSubExpr();
2228 continue;
2229 }
2230 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2231 if (P->getOpcode() == UO_Extension) {
2232 E = P->getSubExpr();
2233 continue;
2234 }
2235 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002236 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2237 if (!P->isResultDependent()) {
2238 E = P->getResultExpr();
2239 continue;
2240 }
2241 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002242 return E;
2243 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002244}
2245
Chris Lattner56f34942008-02-13 01:02:39 +00002246/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2247/// or CastExprs or ImplicitCastExprs, returning their operand.
2248Expr *Expr::IgnoreParenCasts() {
2249 Expr *E = this;
2250 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002251 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002252 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002253 continue;
2254 }
2255 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002256 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002257 continue;
2258 }
2259 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2260 if (P->getOpcode() == UO_Extension) {
2261 E = P->getSubExpr();
2262 continue;
2263 }
2264 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002265 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2266 if (!P->isResultDependent()) {
2267 E = P->getResultExpr();
2268 continue;
2269 }
2270 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002271 if (MaterializeTemporaryExpr *Materialize
2272 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2273 E = Materialize->GetTemporaryExpr();
2274 continue;
2275 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002276 if (SubstNonTypeTemplateParmExpr *NTTP
2277 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2278 E = NTTP->getReplacement();
2279 continue;
2280 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002281 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002282 }
2283}
2284
John McCall9c5d70c2010-12-04 08:24:19 +00002285/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2286/// casts. This is intended purely as a temporary workaround for code
2287/// that hasn't yet been rewritten to do the right thing about those
2288/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002289Expr *Expr::IgnoreParenLValueCasts() {
2290 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002291 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002292 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2293 E = P->getSubExpr();
2294 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002295 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002296 if (P->getCastKind() == CK_LValueToRValue) {
2297 E = P->getSubExpr();
2298 continue;
2299 }
John McCall9c5d70c2010-12-04 08:24:19 +00002300 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2301 if (P->getOpcode() == UO_Extension) {
2302 E = P->getSubExpr();
2303 continue;
2304 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002305 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2306 if (!P->isResultDependent()) {
2307 E = P->getResultExpr();
2308 continue;
2309 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002310 } else if (MaterializeTemporaryExpr *Materialize
2311 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2312 E = Materialize->GetTemporaryExpr();
2313 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002314 } else if (SubstNonTypeTemplateParmExpr *NTTP
2315 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2316 E = NTTP->getReplacement();
2317 continue;
John McCallf6a16482010-12-04 03:47:34 +00002318 }
2319 break;
2320 }
2321 return E;
2322}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002323
2324Expr *Expr::ignoreParenBaseCasts() {
2325 Expr *E = this;
2326 while (true) {
2327 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2328 E = P->getSubExpr();
2329 continue;
2330 }
2331 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2332 if (CE->getCastKind() == CK_DerivedToBase ||
2333 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2334 CE->getCastKind() == CK_NoOp) {
2335 E = CE->getSubExpr();
2336 continue;
2337 }
2338 }
2339
2340 return E;
2341 }
2342}
2343
John McCall2fc46bf2010-05-05 22:59:52 +00002344Expr *Expr::IgnoreParenImpCasts() {
2345 Expr *E = this;
2346 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002347 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002348 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002349 continue;
2350 }
2351 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002352 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002353 continue;
2354 }
2355 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2356 if (P->getOpcode() == UO_Extension) {
2357 E = P->getSubExpr();
2358 continue;
2359 }
2360 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002361 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2362 if (!P->isResultDependent()) {
2363 E = P->getResultExpr();
2364 continue;
2365 }
2366 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002367 if (MaterializeTemporaryExpr *Materialize
2368 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2369 E = Materialize->GetTemporaryExpr();
2370 continue;
2371 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002372 if (SubstNonTypeTemplateParmExpr *NTTP
2373 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2374 E = NTTP->getReplacement();
2375 continue;
2376 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002377 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002378 }
2379}
2380
Hans Wennborg2f072b42011-06-09 17:06:51 +00002381Expr *Expr::IgnoreConversionOperator() {
2382 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002383 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002384 return MCE->getImplicitObjectArgument();
2385 }
2386 return this;
2387}
2388
Chris Lattnerecdd8412009-03-13 17:28:01 +00002389/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2390/// value (including ptr->int casts of the same size). Strip off any
2391/// ParenExpr or CastExprs, returning their operand.
2392Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2393 Expr *E = this;
2394 while (true) {
2395 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2396 E = P->getSubExpr();
2397 continue;
2398 }
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Chris Lattnerecdd8412009-03-13 17:28:01 +00002400 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2401 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002402 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002403 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Chris Lattnerecdd8412009-03-13 17:28:01 +00002405 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2406 E = SE;
2407 continue;
2408 }
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002410 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002411 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002412 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002413 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002414 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2415 E = SE;
2416 continue;
2417 }
2418 }
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002420 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2421 if (P->getOpcode() == UO_Extension) {
2422 E = P->getSubExpr();
2423 continue;
2424 }
2425 }
2426
Peter Collingbournef111d932011-04-15 00:35:48 +00002427 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2428 if (!P->isResultDependent()) {
2429 E = P->getResultExpr();
2430 continue;
2431 }
2432 }
2433
Douglas Gregorc0244c52011-09-08 17:56:33 +00002434 if (SubstNonTypeTemplateParmExpr *NTTP
2435 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2436 E = NTTP->getReplacement();
2437 continue;
2438 }
2439
Chris Lattnerecdd8412009-03-13 17:28:01 +00002440 return E;
2441 }
2442}
2443
Douglas Gregor6eef5192009-12-14 19:27:10 +00002444bool Expr::isDefaultArgument() const {
2445 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002446 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2447 E = M->GetTemporaryExpr();
2448
Douglas Gregor6eef5192009-12-14 19:27:10 +00002449 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2450 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002451
Douglas Gregor6eef5192009-12-14 19:27:10 +00002452 return isa<CXXDefaultArgExpr>(E);
2453}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002454
Douglas Gregor2f599792010-04-02 18:24:57 +00002455/// \brief Skip over any no-op casts and any temporary-binding
2456/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002457static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002458 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2459 E = M->GetTemporaryExpr();
2460
Douglas Gregor2f599792010-04-02 18:24:57 +00002461 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002462 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002463 E = ICE->getSubExpr();
2464 else
2465 break;
2466 }
2467
2468 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2469 E = BE->getSubExpr();
2470
2471 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002472 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002473 E = ICE->getSubExpr();
2474 else
2475 break;
2476 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002477
2478 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002479}
2480
John McCall558d2ab2010-09-15 10:14:12 +00002481/// isTemporaryObject - Determines if this expression produces a
2482/// temporary of the given class type.
2483bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2484 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2485 return false;
2486
Anders Carlssonf8b30152010-11-28 16:40:49 +00002487 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002488
John McCall58277b52010-09-15 20:59:13 +00002489 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002490 if (!E->Classify(C).isPRValue()) {
2491 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002492 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002493 return false;
2494 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002495
John McCall19e60ad2010-09-16 06:57:56 +00002496 // Black-list a few cases which yield pr-values of class type that don't
2497 // refer to temporaries of that type:
2498
2499 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002500 if (isa<ImplicitCastExpr>(E)) {
2501 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2502 case CK_DerivedToBase:
2503 case CK_UncheckedDerivedToBase:
2504 return false;
2505 default:
2506 break;
2507 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002508 }
2509
John McCall19e60ad2010-09-16 06:57:56 +00002510 // - member expressions (all)
2511 if (isa<MemberExpr>(E))
2512 return false;
2513
Eli Friedman32f498a2012-06-15 23:51:06 +00002514 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2515 if (BO->isPtrMemOp())
2516 return false;
2517
John McCall56ca35d2011-02-17 10:25:35 +00002518 // - opaque values (all)
2519 if (isa<OpaqueValueExpr>(E))
2520 return false;
2521
John McCall558d2ab2010-09-15 10:14:12 +00002522 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002523}
2524
Douglas Gregor75e85042011-03-02 21:06:53 +00002525bool Expr::isImplicitCXXThis() const {
2526 const Expr *E = this;
2527
2528 // Strip away parentheses and casts we don't care about.
2529 while (true) {
2530 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2531 E = Paren->getSubExpr();
2532 continue;
2533 }
2534
2535 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2536 if (ICE->getCastKind() == CK_NoOp ||
2537 ICE->getCastKind() == CK_LValueToRValue ||
2538 ICE->getCastKind() == CK_DerivedToBase ||
2539 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2540 E = ICE->getSubExpr();
2541 continue;
2542 }
2543 }
2544
2545 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2546 if (UnOp->getOpcode() == UO_Extension) {
2547 E = UnOp->getSubExpr();
2548 continue;
2549 }
2550 }
2551
Douglas Gregor03e80032011-06-21 17:03:29 +00002552 if (const MaterializeTemporaryExpr *M
2553 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2554 E = M->GetTemporaryExpr();
2555 continue;
2556 }
2557
Douglas Gregor75e85042011-03-02 21:06:53 +00002558 break;
2559 }
2560
2561 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2562 return This->isImplicit();
2563
2564 return false;
2565}
2566
Douglas Gregor898574e2008-12-05 23:32:09 +00002567/// hasAnyTypeDependentArguments - Determines if any of the expressions
2568/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002569bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2570 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002571 if (Exprs[I]->isTypeDependent())
2572 return true;
2573
2574 return false;
2575}
2576
John McCall4204f072010-08-02 21:13:48 +00002577bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002578 // This function is attempting whether an expression is an initializer
2579 // which can be evaluated at compile-time. isEvaluatable handles most
2580 // of the cases, but it can't deal with some initializer-specific
2581 // expressions, and it can't deal with aggregates; we deal with those here,
2582 // and fall back to isEvaluatable for the other cases.
2583
John McCall4204f072010-08-02 21:13:48 +00002584 // If we ever capture reference-binding directly in the AST, we can
2585 // kill the second parameter.
2586
2587 if (IsForRef) {
2588 EvalResult Result;
2589 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2590 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002591
Anders Carlssone8a32b82008-11-24 05:23:59 +00002592 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002593 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002594 case IntegerLiteralClass:
2595 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002596 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002597 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002598 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002599 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002600 case CXXTemporaryObjectExprClass:
2601 case CXXConstructExprClass: {
2602 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002603
2604 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002605 if (CE->getConstructor()->isTrivial()) {
2606 // 1) an application of the trivial default constructor or
2607 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002608
Richard Smith180f4792011-11-10 06:34:14 +00002609 // 2) an elidable trivial copy construction of an operand which is
2610 // itself a constant initializer. Note that we consider the
2611 // operand on its own, *not* as a reference binding.
2612 if (CE->isElidable() &&
2613 CE->getArg(0)->isConstantInitializer(Ctx, false))
2614 return true;
2615 }
2616
2617 // 3) a foldable constexpr constructor.
2618 break;
John McCallb4b9b152010-08-01 21:51:45 +00002619 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002620 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002621 // This handles gcc's extension that allows global initializers like
2622 // "struct x {int x;} x = (struct x) {};".
2623 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002624 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002625 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002626 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002627 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002628 // FIXME: This doesn't deal with fields with reference types correctly.
2629 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2630 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002631 const InitListExpr *Exp = cast<InitListExpr>(this);
2632 unsigned numInits = Exp->getNumInits();
2633 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002634 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002635 return false;
2636 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002637 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002638 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002639 case ImplicitValueInitExprClass:
2640 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002641 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002642 return cast<ParenExpr>(this)->getSubExpr()
2643 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002644 case GenericSelectionExprClass:
2645 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2646 return false;
2647 return cast<GenericSelectionExpr>(this)->getResultExpr()
2648 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002649 case ChooseExprClass:
2650 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2651 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002652 case UnaryOperatorClass: {
2653 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002654 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002655 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002656 break;
2657 }
John McCall4204f072010-08-02 21:13:48 +00002658 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002659 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002660 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002661 case CStyleCastExprClass: {
2662 const CastExpr *CE = cast<CastExpr>(this);
2663
David Chisnall7a7ee302012-01-16 17:27:18 +00002664 // If we're promoting an integer to an _Atomic type then this is constant
2665 // if the integer is constant. We also need to check the converse in case
2666 // someone does something like:
2667 //
2668 // int a = (_Atomic(int))42;
2669 //
2670 // I doubt anyone would write code like this directly, but it's quite
2671 // possible as the result of macro expansions.
2672 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2673 CE->getCastKind() == CK_AtomicToNonAtomic)
2674 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2675
Richard Smithd62ca372011-12-06 22:44:34 +00002676 // Handle bitcasts of vector constants.
2677 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2678 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2679
Eli Friedman6bd97192011-12-21 00:43:02 +00002680 // Handle misc casts we want to ignore.
2681 // FIXME: Is it really safe to ignore all these?
2682 if (CE->getCastKind() == CK_NoOp ||
2683 CE->getCastKind() == CK_LValueToRValue ||
2684 CE->getCastKind() == CK_ToUnion ||
2685 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002686 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2687
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002688 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002689 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002690 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002691 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002692 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002693 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002694 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002695}
2696
Richard Smith8ae4ec22012-08-07 04:16:51 +00002697bool Expr::HasSideEffects(const ASTContext &Ctx) const {
2698 if (isInstantiationDependent())
2699 return true;
2700
2701 switch (getStmtClass()) {
2702 case NoStmtClass:
2703 #define ABSTRACT_STMT(Type)
2704 #define STMT(Type, Base) case Type##Class:
2705 #define EXPR(Type, Base)
2706 #include "clang/AST/StmtNodes.inc"
2707 llvm_unreachable("unexpected Expr kind");
2708
2709 case DependentScopeDeclRefExprClass:
2710 case CXXUnresolvedConstructExprClass:
2711 case CXXDependentScopeMemberExprClass:
2712 case UnresolvedLookupExprClass:
2713 case UnresolvedMemberExprClass:
2714 case PackExpansionExprClass:
2715 case SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +00002716 case FunctionParmPackExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002717 llvm_unreachable("shouldn't see dependent / unresolved nodes here");
2718
Richard Smith60b70382012-08-07 05:18:29 +00002719 case DeclRefExprClass:
2720 case ObjCIvarRefExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002721 case PredefinedExprClass:
2722 case IntegerLiteralClass:
2723 case FloatingLiteralClass:
2724 case ImaginaryLiteralClass:
2725 case StringLiteralClass:
2726 case CharacterLiteralClass:
2727 case OffsetOfExprClass:
2728 case ImplicitValueInitExprClass:
2729 case UnaryExprOrTypeTraitExprClass:
2730 case AddrLabelExprClass:
2731 case GNUNullExprClass:
2732 case CXXBoolLiteralExprClass:
2733 case CXXNullPtrLiteralExprClass:
2734 case CXXThisExprClass:
2735 case CXXScalarValueInitExprClass:
2736 case TypeTraitExprClass:
2737 case UnaryTypeTraitExprClass:
2738 case BinaryTypeTraitExprClass:
2739 case ArrayTypeTraitExprClass:
2740 case ExpressionTraitExprClass:
2741 case CXXNoexceptExprClass:
2742 case SizeOfPackExprClass:
2743 case ObjCStringLiteralClass:
2744 case ObjCEncodeExprClass:
2745 case ObjCBoolLiteralExprClass:
2746 case CXXUuidofExprClass:
2747 case OpaqueValueExprClass:
2748 // These never have a side-effect.
2749 return false;
2750
2751 case CallExprClass:
2752 case CompoundAssignOperatorClass:
2753 case VAArgExprClass:
2754 case AtomicExprClass:
2755 case StmtExprClass:
2756 case CXXOperatorCallExprClass:
2757 case CXXMemberCallExprClass:
2758 case UserDefinedLiteralClass:
2759 case CXXThrowExprClass:
2760 case CXXNewExprClass:
2761 case CXXDeleteExprClass:
2762 case ExprWithCleanupsClass:
2763 case CXXBindTemporaryExprClass:
2764 case BlockExprClass:
2765 case CUDAKernelCallExprClass:
2766 // These always have a side-effect.
2767 return true;
2768
2769 case ParenExprClass:
2770 case ArraySubscriptExprClass:
2771 case MemberExprClass:
2772 case ConditionalOperatorClass:
2773 case BinaryConditionalOperatorClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002774 case CompoundLiteralExprClass:
2775 case ExtVectorElementExprClass:
2776 case DesignatedInitExprClass:
2777 case ParenListExprClass:
Richard Smith8ae4ec22012-08-07 04:16:51 +00002778 case CXXPseudoDestructorExprClass:
2779 case SubstNonTypeTemplateParmExprClass:
2780 case MaterializeTemporaryExprClass:
2781 case ShuffleVectorExprClass:
2782 case AsTypeExprClass:
2783 // These have a side-effect if any subexpression does.
2784 break;
2785
Richard Smith60b70382012-08-07 05:18:29 +00002786 case UnaryOperatorClass:
2787 if (cast<UnaryOperator>(this)->isIncrementDecrementOp())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002788 return true;
2789 break;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002790
2791 case BinaryOperatorClass:
2792 if (cast<BinaryOperator>(this)->isAssignmentOp())
2793 return true;
2794 break;
2795
Richard Smith8ae4ec22012-08-07 04:16:51 +00002796 case InitListExprClass:
2797 // FIXME: The children for an InitListExpr doesn't include the array filler.
2798 if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller())
2799 if (E->HasSideEffects(Ctx))
2800 return true;
2801 break;
2802
2803 case GenericSelectionExprClass:
2804 return cast<GenericSelectionExpr>(this)->getResultExpr()->
2805 HasSideEffects(Ctx);
2806
2807 case ChooseExprClass:
2808 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->HasSideEffects(Ctx);
2809
2810 case CXXDefaultArgExprClass:
2811 return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects(Ctx);
2812
2813 case CXXDynamicCastExprClass: {
2814 // A dynamic_cast expression has side-effects if it can throw.
2815 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this);
2816 if (DCE->getTypeAsWritten()->isReferenceType() &&
2817 DCE->getCastKind() == CK_Dynamic)
2818 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002819 } // Fall through.
2820 case ImplicitCastExprClass:
2821 case CStyleCastExprClass:
2822 case CXXStaticCastExprClass:
2823 case CXXReinterpretCastExprClass:
2824 case CXXConstCastExprClass:
2825 case CXXFunctionalCastExprClass: {
2826 const CastExpr *CE = cast<CastExpr>(this);
2827 if (CE->getCastKind() == CK_LValueToRValue &&
2828 CE->getSubExpr()->getType().isVolatileQualified())
2829 return true;
Richard Smith8ae4ec22012-08-07 04:16:51 +00002830 break;
2831 }
2832
Richard Smith0d729102012-08-13 20:08:14 +00002833 case CXXTypeidExprClass:
2834 // typeid might throw if its subexpression is potentially-evaluated, so has
2835 // side-effects in that case whether or not its subexpression does.
2836 return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated();
Richard Smith8ae4ec22012-08-07 04:16:51 +00002837
2838 case CXXConstructExprClass:
2839 case CXXTemporaryObjectExprClass: {
2840 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
Richard Smith60b70382012-08-07 05:18:29 +00002841 if (!CE->getConstructor()->isTrivial())
Richard Smith8ae4ec22012-08-07 04:16:51 +00002842 return true;
Richard Smith60b70382012-08-07 05:18:29 +00002843 // A trivial constructor does not add any side-effects of its own. Just look
2844 // at its arguments.
Richard Smith8ae4ec22012-08-07 04:16:51 +00002845 break;
2846 }
2847
2848 case LambdaExprClass: {
2849 const LambdaExpr *LE = cast<LambdaExpr>(this);
2850 for (LambdaExpr::capture_iterator I = LE->capture_begin(),
2851 E = LE->capture_end(); I != E; ++I)
2852 if (I->getCaptureKind() == LCK_ByCopy)
2853 // FIXME: Only has a side-effect if the variable is volatile or if
2854 // the copy would invoke a non-trivial copy constructor.
2855 return true;
2856 return false;
2857 }
2858
2859 case PseudoObjectExprClass: {
2860 // Only look for side-effects in the semantic form, and look past
2861 // OpaqueValueExpr bindings in that form.
2862 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
2863 for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(),
2864 E = PO->semantics_end();
2865 I != E; ++I) {
2866 const Expr *Subexpr = *I;
2867 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr))
2868 Subexpr = OVE->getSourceExpr();
2869 if (Subexpr->HasSideEffects(Ctx))
2870 return true;
2871 }
2872 return false;
2873 }
2874
2875 case ObjCBoxedExprClass:
2876 case ObjCArrayLiteralClass:
2877 case ObjCDictionaryLiteralClass:
2878 case ObjCMessageExprClass:
2879 case ObjCSelectorExprClass:
2880 case ObjCProtocolExprClass:
2881 case ObjCPropertyRefExprClass:
2882 case ObjCIsaExprClass:
2883 case ObjCIndirectCopyRestoreExprClass:
2884 case ObjCSubscriptRefExprClass:
2885 case ObjCBridgedCastExprClass:
2886 // FIXME: Classify these cases better.
2887 return true;
2888 }
2889
2890 // Recurse to children.
2891 for (const_child_range SubStmts = children(); SubStmts; ++SubStmts)
2892 if (const Stmt *S = *SubStmts)
2893 if (cast<Expr>(S)->HasSideEffects(Ctx))
2894 return true;
2895
2896 return false;
2897}
2898
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002899namespace {
2900 /// \brief Look for a call to a non-trivial function within an expression.
2901 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2902 {
2903 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2904
2905 bool NonTrivial;
2906
2907 public:
2908 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002909 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002910
2911 bool hasNonTrivialCall() const { return NonTrivial; }
2912
2913 void VisitCallExpr(CallExpr *E) {
2914 if (CXXMethodDecl *Method
2915 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2916 if (Method->isTrivial()) {
2917 // Recurse to children of the call.
2918 Inherited::VisitStmt(E);
2919 return;
2920 }
2921 }
2922
2923 NonTrivial = true;
2924 }
2925
2926 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2927 if (E->getConstructor()->isTrivial()) {
2928 // Recurse to children of the call.
2929 Inherited::VisitStmt(E);
2930 return;
2931 }
2932
2933 NonTrivial = true;
2934 }
2935
2936 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2937 if (E->getTemporary()->getDestructor()->isTrivial()) {
2938 Inherited::VisitStmt(E);
2939 return;
2940 }
2941
2942 NonTrivial = true;
2943 }
2944 };
2945}
2946
2947bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2948 NonTrivialCallFinder Finder(Ctx);
2949 Finder.Visit(this);
2950 return Finder.hasNonTrivialCall();
2951}
2952
Chandler Carruth82214a82011-02-18 23:54:50 +00002953/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2954/// pointer constant or not, as well as the specific kind of constant detected.
2955/// Null pointer constants can be integer constant expressions with the
2956/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2957/// (a GNU extension).
2958Expr::NullPointerConstantKind
2959Expr::isNullPointerConstant(ASTContext &Ctx,
2960 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002961 if (isValueDependent()) {
2962 switch (NPC) {
2963 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002964 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002965 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002966 if (isTypeDependent() || getType()->isIntegralType(Ctx))
David Blaikie50800fc2012-08-08 17:33:31 +00002967 return NPCK_ZeroExpression;
Chandler Carruth82214a82011-02-18 23:54:50 +00002968 else
2969 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002970
Douglas Gregorce940492009-09-25 04:25:58 +00002971 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002972 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002973 }
2974 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002975
Sebastian Redl07779722008-10-31 14:43:28 +00002976 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002977 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002978 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002979 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002980 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002981 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002982 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002983 Pointee->isVoidType() && // to void*
2984 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002985 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002986 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002987 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002988 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2989 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002990 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002991 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2992 // Accept ((void*)0) as a null pointer constant, as many other
2993 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002994 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002995 } else if (const GenericSelectionExpr *GE =
2996 dyn_cast<GenericSelectionExpr>(this)) {
2997 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002998 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002999 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00003000 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00003001 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003002 } else if (isa<GNUNullExpr>(this)) {
3003 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00003004 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00003005 } else if (const MaterializeTemporaryExpr *M
3006 = dyn_cast<MaterializeTemporaryExpr>(this)) {
3007 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00003008 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
3009 if (const Expr *Source = OVE->getSourceExpr())
3010 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00003011 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00003012
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003013 // C++0x nullptr_t is always a null pointer constant.
3014 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00003015 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003016
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00003017 if (const RecordType *UT = getType()->getAsUnionType())
3018 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
3019 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
3020 const Expr *InitExpr = CLE->getInitializer();
3021 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
3022 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
3023 }
Steve Naroffaa58f002008-01-14 16:10:57 +00003024 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00003025 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003026 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00003027 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Reid Spencer5f016e22007-07-11 17:01:13 +00003029 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00003030 // test for the value 0. Don't use the C++11 constant expression semantics
3031 // for this, for now; once the dust settles on core issue 903, we might only
3032 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00003033 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00003034 if (!isCXX98IntegralConstantExpr(Ctx))
3035 return NPCK_NotNull;
3036 } else {
3037 if (!isIntegerConstantExpr(Ctx))
3038 return NPCK_NotNull;
3039 }
Chandler Carruth82214a82011-02-18 23:54:50 +00003040
David Blaikie50800fc2012-08-08 17:33:31 +00003041 if (EvaluateKnownConstInt(Ctx) != 0)
3042 return NPCK_NotNull;
3043
3044 if (isa<IntegerLiteral>(this))
3045 return NPCK_ZeroLiteral;
3046 return NPCK_ZeroExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00003047}
Steve Naroff31a45842007-07-28 23:10:27 +00003048
John McCallf6a16482010-12-04 03:47:34 +00003049/// \brief If this expression is an l-value for an Objective C
3050/// property, find the underlying property reference expression.
3051const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
3052 const Expr *E = this;
3053 while (true) {
3054 assert((E->getValueKind() == VK_LValue &&
3055 E->getObjectKind() == OK_ObjCProperty) &&
3056 "expression is not a property reference");
3057 E = E->IgnoreParenCasts();
3058 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
3059 if (BO->getOpcode() == BO_Comma) {
3060 E = BO->getRHS();
3061 continue;
3062 }
3063 }
3064
3065 break;
3066 }
3067
3068 return cast<ObjCPropertyRefExpr>(E);
3069}
3070
Anna Zaksbbff82f2012-10-01 20:34:04 +00003071bool Expr::isObjCSelfExpr() const {
3072 const Expr *E = IgnoreParenImpCasts();
3073
3074 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3075 if (!DRE)
3076 return false;
3077
3078 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
3079 if (!Param)
3080 return false;
3081
3082 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
3083 if (!M)
3084 return false;
3085
3086 return M->getSelfDecl() == Param;
3087}
3088
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003089FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00003090 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003091
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003092 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00003093 if (ICE->getCastKind() == CK_LValueToRValue ||
3094 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00003095 E = ICE->getSubExpr()->IgnoreParens();
3096 else
3097 break;
3098 }
3099
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003100 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00003101 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003102 if (Field->isBitField())
3103 return Field;
3104
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00003105 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
3106 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
3107 if (Field->isBitField())
3108 return Field;
3109
Eli Friedman42068e92011-07-13 02:05:57 +00003110 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003111 if (BinOp->isAssignmentOp() && BinOp->getLHS())
3112 return BinOp->getLHS()->getBitField();
3113
Eli Friedman42068e92011-07-13 02:05:57 +00003114 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
3115 return BinOp->getRHS()->getBitField();
3116 }
3117
Douglas Gregor33bbbc52009-05-02 02:18:30 +00003118 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00003119}
3120
Anders Carlsson09380262010-01-31 17:18:49 +00003121bool Expr::refersToVectorElement() const {
3122 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00003123
Anders Carlsson09380262010-01-31 17:18:49 +00003124 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00003125 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00003126 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00003127 E = ICE->getSubExpr()->IgnoreParens();
3128 else
3129 break;
3130 }
Sean Huntc3021132010-05-05 15:23:54 +00003131
Anders Carlsson09380262010-01-31 17:18:49 +00003132 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
3133 return ASE->getBase()->getType()->isVectorType();
3134
3135 if (isa<ExtVectorElementExpr>(E))
3136 return true;
3137
3138 return false;
3139}
3140
Chris Lattner2140e902009-02-16 22:14:05 +00003141/// isArrow - Return true if the base expression is a pointer to vector,
3142/// return false if the base expression is a vector.
3143bool ExtVectorElementExpr::isArrow() const {
3144 return getBase()->getType()->isPointerType();
3145}
3146
Nate Begeman213541a2008-04-18 23:10:10 +00003147unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003148 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003149 return VT->getNumElements();
3150 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003151}
3152
Nate Begeman8a997642008-05-09 06:41:27 +00003153/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003154bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003155 // FIXME: Refactor this code to an accessor on the AST node which returns the
3156 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003157 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003158
3159 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003160 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003161 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Nate Begeman190d6a22009-01-18 02:01:21 +00003163 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003164 if (Comp[0] == 's' || Comp[0] == 'S')
3165 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003166
Daniel Dunbar15027422009-10-17 23:53:04 +00003167 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003168 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003169 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003170
Steve Narofffec0b492007-07-30 03:29:09 +00003171 return false;
3172}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003173
Nate Begeman8a997642008-05-09 06:41:27 +00003174/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003175void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003176 SmallVectorImpl<unsigned> &Elts) const {
3177 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003178 if (Comp[0] == 's' || Comp[0] == 'S')
3179 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003181 bool isHi = Comp == "hi";
3182 bool isLo = Comp == "lo";
3183 bool isEven = Comp == "even";
3184 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Nate Begeman8a997642008-05-09 06:41:27 +00003186 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3187 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Nate Begeman8a997642008-05-09 06:41:27 +00003189 if (isHi)
3190 Index = e + i;
3191 else if (isLo)
3192 Index = i;
3193 else if (isEven)
3194 Index = 2 * i;
3195 else if (isOdd)
3196 Index = 2 * i + 1;
3197 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003198 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003199
Nate Begeman3b8d1162008-05-13 21:03:02 +00003200 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003201 }
Nate Begeman8a997642008-05-09 06:41:27 +00003202}
3203
Douglas Gregor04badcf2010-04-21 00:45:42 +00003204ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003205 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003206 SourceLocation LBracLoc,
3207 SourceLocation SuperLoc,
3208 bool IsInstanceSuper,
3209 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003210 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003211 ArrayRef<SourceLocation> SelLocs,
3212 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003213 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003214 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003215 SourceLocation RBracLoc,
3216 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003217 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003218 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003219 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003220 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003221 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3222 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003223 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003224 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3225 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003226{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003227 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003228 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003229}
3230
Douglas Gregor04badcf2010-04-21 00:45:42 +00003231ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003232 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003233 SourceLocation LBracLoc,
3234 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003235 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003236 ArrayRef<SourceLocation> SelLocs,
3237 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003238 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003239 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003240 SourceLocation RBracLoc,
3241 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003242 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003243 T->isDependentType(), T->isInstantiationDependentType(),
3244 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003245 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3246 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003247 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003248 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003249 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003250{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003251 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003252 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003253}
3254
Douglas Gregor04badcf2010-04-21 00:45:42 +00003255ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003256 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003257 SourceLocation LBracLoc,
3258 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003259 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003260 ArrayRef<SourceLocation> SelLocs,
3261 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003262 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003263 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003264 SourceLocation RBracLoc,
3265 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003266 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003267 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003268 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003269 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003270 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3271 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003272 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003273 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003274 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003275{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003276 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003277 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003278}
3279
3280void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3281 ArrayRef<SourceLocation> SelLocs,
3282 SelectorLocationsKind SelLocsK) {
3283 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003284 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003285 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003286 if (Args[I]->isTypeDependent())
3287 ExprBits.TypeDependent = true;
3288 if (Args[I]->isValueDependent())
3289 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003290 if (Args[I]->isInstantiationDependent())
3291 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003292 if (Args[I]->containsUnexpandedParameterPack())
3293 ExprBits.ContainsUnexpandedParameterPack = true;
3294
3295 MyArgs[I] = Args[I];
3296 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003297
Benjamin Kramer19562c92012-02-20 00:20:48 +00003298 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003299 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003300 if (SelLocsK == SelLoc_NonStandard)
3301 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3302 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003303}
3304
Douglas Gregor04badcf2010-04-21 00:45:42 +00003305ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003306 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003307 SourceLocation LBracLoc,
3308 SourceLocation SuperLoc,
3309 bool IsInstanceSuper,
3310 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003311 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003312 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003313 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003314 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003315 SourceLocation RBracLoc,
3316 bool isImplicit) {
3317 assert((!SelLocs.empty() || isImplicit) &&
3318 "No selector locs for non-implicit message");
3319 ObjCMessageExpr *Mem;
3320 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3321 if (isImplicit)
3322 Mem = alloc(Context, Args.size(), 0);
3323 else
3324 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003325 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003326 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003327 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003328}
3329
3330ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003331 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003332 SourceLocation LBracLoc,
3333 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003334 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003335 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003336 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003337 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003338 SourceLocation RBracLoc,
3339 bool isImplicit) {
3340 assert((!SelLocs.empty() || isImplicit) &&
3341 "No selector locs for non-implicit message");
3342 ObjCMessageExpr *Mem;
3343 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3344 if (isImplicit)
3345 Mem = alloc(Context, Args.size(), 0);
3346 else
3347 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003348 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003349 SelLocs, SelLocsK, Method, Args, RBracLoc,
3350 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003351}
3352
3353ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003354 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003355 SourceLocation LBracLoc,
3356 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003357 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003358 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003359 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003360 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003361 SourceLocation RBracLoc,
3362 bool isImplicit) {
3363 assert((!SelLocs.empty() || isImplicit) &&
3364 "No selector locs for non-implicit message");
3365 ObjCMessageExpr *Mem;
3366 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3367 if (isImplicit)
3368 Mem = alloc(Context, Args.size(), 0);
3369 else
3370 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003371 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003372 SelLocs, SelLocsK, Method, Args, RBracLoc,
3373 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003374}
3375
Sean Huntc3021132010-05-05 15:23:54 +00003376ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003377 unsigned NumArgs,
3378 unsigned NumStoredSelLocs) {
3379 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003380 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3381}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003382
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003383ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3384 ArrayRef<Expr *> Args,
3385 SourceLocation RBraceLoc,
3386 ArrayRef<SourceLocation> SelLocs,
3387 Selector Sel,
3388 SelectorLocationsKind &SelLocsK) {
3389 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3390 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3391 : 0;
3392 return alloc(C, Args.size(), NumStoredSelLocs);
3393}
3394
3395ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3396 unsigned NumArgs,
3397 unsigned NumStoredSelLocs) {
3398 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3399 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3400 return (ObjCMessageExpr *)C.Allocate(Size,
3401 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3402}
3403
3404void ObjCMessageExpr::getSelectorLocs(
3405 SmallVectorImpl<SourceLocation> &SelLocs) const {
3406 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3407 SelLocs.push_back(getSelectorLoc(i));
3408}
3409
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003410SourceRange ObjCMessageExpr::getReceiverRange() const {
3411 switch (getReceiverKind()) {
3412 case Instance:
3413 return getInstanceReceiver()->getSourceRange();
3414
3415 case Class:
3416 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3417
3418 case SuperInstance:
3419 case SuperClass:
3420 return getSuperLoc();
3421 }
3422
David Blaikie30263482012-01-20 21:50:17 +00003423 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003424}
3425
Douglas Gregor04badcf2010-04-21 00:45:42 +00003426Selector ObjCMessageExpr::getSelector() const {
3427 if (HasMethod)
3428 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3429 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003430 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003431}
3432
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003433QualType ObjCMessageExpr::getReceiverType() const {
Douglas Gregor04badcf2010-04-21 00:45:42 +00003434 switch (getReceiverKind()) {
3435 case Instance:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003436 return getInstanceReceiver()->getType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003437 case Class:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003438 return getClassReceiver();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003439 case SuperInstance:
Douglas Gregor04badcf2010-04-21 00:45:42 +00003440 case SuperClass:
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003441 return getSuperType();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003442 }
3443
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00003444 llvm_unreachable("unexpected receiver kind");
3445}
3446
3447ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3448 QualType T = getReceiverType();
3449
3450 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
3451 return Ptr->getInterfaceDecl();
3452
3453 if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
3454 return Ty->getInterface();
3455
Douglas Gregor04badcf2010-04-21 00:45:42 +00003456 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003457}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003458
Chris Lattner5f9e2722011-07-23 10:55:15 +00003459StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003460 switch (getBridgeKind()) {
3461 case OBC_Bridge:
3462 return "__bridge";
3463 case OBC_BridgeTransfer:
3464 return "__bridge_transfer";
3465 case OBC_BridgeRetained:
3466 return "__bridge_retained";
3467 }
David Blaikie30263482012-01-20 21:50:17 +00003468
3469 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003470}
3471
Jay Foad4ba2a172011-01-12 09:06:06 +00003472bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003473 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003474}
3475
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003476ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, ArrayRef<Expr*> args,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003477 QualType Type, SourceLocation BLoc,
3478 SourceLocation RP)
3479 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3480 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003481 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003482 Type->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003483 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003484{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003485 SubExprs = new (C) Stmt*[args.size()];
3486 for (unsigned i = 0; i != args.size(); i++) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003487 if (args[i]->isTypeDependent())
3488 ExprBits.TypeDependent = true;
3489 if (args[i]->isValueDependent())
3490 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003491 if (args[i]->isInstantiationDependent())
3492 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003493 if (args[i]->containsUnexpandedParameterPack())
3494 ExprBits.ContainsUnexpandedParameterPack = true;
3495
3496 SubExprs[i] = args[i];
3497 }
3498}
3499
Nate Begeman888376a2009-08-12 02:28:50 +00003500void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3501 unsigned NumExprs) {
3502 if (SubExprs) C.Deallocate(SubExprs);
3503
3504 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003505 this->NumExprs = NumExprs;
3506 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003507}
Nate Begeman888376a2009-08-12 02:28:50 +00003508
Peter Collingbournef111d932011-04-15 00:35:48 +00003509GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3510 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003511 ArrayRef<TypeSourceInfo*> AssocTypes,
3512 ArrayRef<Expr*> AssocExprs,
3513 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003514 SourceLocation RParenLoc,
3515 bool ContainsUnexpandedParameterPack,
3516 unsigned ResultIndex)
3517 : Expr(GenericSelectionExprClass,
3518 AssocExprs[ResultIndex]->getType(),
3519 AssocExprs[ResultIndex]->getValueKind(),
3520 AssocExprs[ResultIndex]->getObjectKind(),
3521 AssocExprs[ResultIndex]->isTypeDependent(),
3522 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003523 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003524 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003525 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3526 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3527 NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex),
3528 GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003529 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003530 assert(AssocTypes.size() == AssocExprs.size());
3531 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3532 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003533}
3534
3535GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3536 SourceLocation GenericLoc, Expr *ControllingExpr,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003537 ArrayRef<TypeSourceInfo*> AssocTypes,
3538 ArrayRef<Expr*> AssocExprs,
3539 SourceLocation DefaultLoc,
Peter Collingbournef111d932011-04-15 00:35:48 +00003540 SourceLocation RParenLoc,
3541 bool ContainsUnexpandedParameterPack)
3542 : Expr(GenericSelectionExprClass,
3543 Context.DependentTy,
3544 VK_RValue,
3545 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003546 /*isTypeDependent=*/true,
3547 /*isValueDependent=*/true,
3548 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003549 ContainsUnexpandedParameterPack),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003550 AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]),
3551 SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]),
3552 NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc),
3553 DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) {
Peter Collingbournef111d932011-04-15 00:35:48 +00003554 SubExprs[CONTROLLING] = ControllingExpr;
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003555 assert(AssocTypes.size() == AssocExprs.size());
3556 std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes);
3557 std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR);
Peter Collingbournef111d932011-04-15 00:35:48 +00003558}
3559
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003560//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003561// DesignatedInitExpr
3562//===----------------------------------------------------------------------===//
3563
Chandler Carruthb1138242011-06-16 06:47:06 +00003564IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003565 assert(Kind == FieldDesignator && "Only valid on a field designator");
3566 if (Field.NameOrField & 0x01)
3567 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3568 else
3569 return getField()->getIdentifier();
3570}
3571
Sean Huntc3021132010-05-05 15:23:54 +00003572DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003573 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003574 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003575 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003576 bool GNUSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003577 ArrayRef<Expr*> IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003578 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003579 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003580 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003581 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003582 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003583 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003584 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003585 NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003586 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003587
3588 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003589 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003590 *Child++ = Init;
3591
3592 // Copy the designators and their subexpressions, computing
3593 // value-dependence along the way.
3594 unsigned IndexIdx = 0;
3595 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003596 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003597
3598 if (this->Designators[I].isArrayDesignator()) {
3599 // Compute type- and value-dependence.
3600 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003601 if (Index->isTypeDependent() || Index->isValueDependent())
3602 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003603 if (Index->isInstantiationDependent())
3604 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003605 // Propagate unexpanded parameter packs.
3606 if (Index->containsUnexpandedParameterPack())
3607 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003608
3609 // Copy the index expressions into permanent storage.
3610 *Child++ = IndexExprs[IndexIdx++];
3611 } else if (this->Designators[I].isArrayRangeDesignator()) {
3612 // Compute type- and value-dependence.
3613 Expr *Start = IndexExprs[IndexIdx];
3614 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003615 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003616 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003617 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003618 ExprBits.InstantiationDependent = true;
3619 } else if (Start->isInstantiationDependent() ||
3620 End->isInstantiationDependent()) {
3621 ExprBits.InstantiationDependent = true;
3622 }
3623
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003624 // Propagate unexpanded parameter packs.
3625 if (Start->containsUnexpandedParameterPack() ||
3626 End->containsUnexpandedParameterPack())
3627 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003628
3629 // Copy the start/end expressions into permanent storage.
3630 *Child++ = IndexExprs[IndexIdx++];
3631 *Child++ = IndexExprs[IndexIdx++];
3632 }
3633 }
3634
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003635 assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003636}
3637
Douglas Gregor05c13a32009-01-22 00:58:24 +00003638DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003639DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003640 unsigned NumDesignators,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003641 ArrayRef<Expr*> IndexExprs,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003642 SourceLocation ColonOrEqualLoc,
3643 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003644 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003645 sizeof(Stmt *) * (IndexExprs.size() + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003646 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003647 ColonOrEqualLoc, UsesColonSyntax,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003648 IndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003649}
3650
Mike Stump1eb44332009-09-09 15:08:12 +00003651DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003652 unsigned NumIndexExprs) {
3653 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3654 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3655 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3656}
3657
Douglas Gregor319d57f2010-01-06 23:17:19 +00003658void DesignatedInitExpr::setDesignators(ASTContext &C,
3659 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003660 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003661 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003662 NumDesignators = NumDesigs;
3663 for (unsigned I = 0; I != NumDesigs; ++I)
3664 Designators[I] = Desigs[I];
3665}
3666
Abramo Bagnara24f46742011-03-16 15:08:46 +00003667SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3668 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3669 if (size() == 1)
3670 return DIE->getDesignator(0)->getSourceRange();
Erik Verbruggen65d78312012-12-25 14:51:39 +00003671 return SourceRange(DIE->getDesignator(0)->getLocStart(),
3672 DIE->getDesignator(size()-1)->getLocEnd());
Abramo Bagnara24f46742011-03-16 15:08:46 +00003673}
3674
Erik Verbruggen65d78312012-12-25 14:51:39 +00003675SourceLocation DesignatedInitExpr::getLocStart() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003676 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003677 Designator &First =
3678 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003679 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003680 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003681 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3682 else
3683 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3684 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003685 StartLoc =
3686 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Erik Verbruggen65d78312012-12-25 14:51:39 +00003687 return StartLoc;
3688}
3689
3690SourceLocation DesignatedInitExpr::getLocEnd() const {
3691 return getInit()->getLocEnd();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003692}
3693
Douglas Gregor05c13a32009-01-22 00:58:24 +00003694Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3695 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3696 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3697 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003698 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3699 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3700}
3701
3702Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003703 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003704 "Requires array range designator");
3705 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3706 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003707 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3708 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3709}
3710
3711Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003712 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003713 "Requires array range designator");
3714 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3715 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003716 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3717 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3718}
3719
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003720/// \brief Replaces the designator at index @p Idx with the series
3721/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003722void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003723 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003724 const Designator *Last) {
3725 unsigned NumNewDesignators = Last - First;
3726 if (NumNewDesignators == 0) {
3727 std::copy_backward(Designators + Idx + 1,
3728 Designators + NumDesignators,
3729 Designators + Idx);
3730 --NumNewDesignators;
3731 return;
3732 } else if (NumNewDesignators == 1) {
3733 Designators[Idx] = *First;
3734 return;
3735 }
3736
Mike Stump1eb44332009-09-09 15:08:12 +00003737 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003738 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003739 std::copy(Designators, Designators + Idx, NewDesignators);
3740 std::copy(First, Last, NewDesignators + Idx);
3741 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3742 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003743 Designators = NewDesignators;
3744 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3745}
3746
Mike Stump1eb44332009-09-09 15:08:12 +00003747ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003748 ArrayRef<Expr*> exprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003749 SourceLocation rparenloc)
3750 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003751 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003752 NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) {
3753 Exprs = new (C) Stmt*[exprs.size()];
3754 for (unsigned i = 0; i != exprs.size(); ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003755 if (exprs[i]->isTypeDependent())
3756 ExprBits.TypeDependent = true;
3757 if (exprs[i]->isValueDependent())
3758 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003759 if (exprs[i]->isInstantiationDependent())
3760 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003761 if (exprs[i]->containsUnexpandedParameterPack())
3762 ExprBits.ContainsUnexpandedParameterPack = true;
3763
Nate Begeman2ef13e52009-08-10 23:49:36 +00003764 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003765 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003766}
3767
John McCalle996ffd2011-02-16 08:02:54 +00003768const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3769 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3770 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003771 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3772 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003773 e = cast<CXXConstructExpr>(e)->getArg(0);
3774 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3775 e = ice->getSubExpr();
3776 return cast<OpaqueValueExpr>(e);
3777}
3778
John McCall4b9c2d22011-11-06 09:01:30 +00003779PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3780 unsigned numSemanticExprs) {
3781 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3782 (1 + numSemanticExprs) * sizeof(Expr*),
3783 llvm::alignOf<PseudoObjectExpr>());
3784 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3785}
3786
3787PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3788 : Expr(PseudoObjectExprClass, shell) {
3789 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3790}
3791
3792PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3793 ArrayRef<Expr*> semantics,
3794 unsigned resultIndex) {
3795 assert(syntax && "no syntactic expression!");
3796 assert(semantics.size() && "no semantic expressions!");
3797
3798 QualType type;
3799 ExprValueKind VK;
3800 if (resultIndex == NoResult) {
3801 type = C.VoidTy;
3802 VK = VK_RValue;
3803 } else {
3804 assert(resultIndex < semantics.size());
3805 type = semantics[resultIndex]->getType();
3806 VK = semantics[resultIndex]->getValueKind();
3807 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3808 }
3809
3810 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3811 (1 + semantics.size()) * sizeof(Expr*),
3812 llvm::alignOf<PseudoObjectExpr>());
3813 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3814 resultIndex);
3815}
3816
3817PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3818 Expr *syntax, ArrayRef<Expr*> semantics,
3819 unsigned resultIndex)
3820 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3821 /*filled in at end of ctor*/ false, false, false, false) {
3822 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3823 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3824
3825 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3826 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3827 getSubExprsBuffer()[i] = E;
3828
3829 if (E->isTypeDependent())
3830 ExprBits.TypeDependent = true;
3831 if (E->isValueDependent())
3832 ExprBits.ValueDependent = true;
3833 if (E->isInstantiationDependent())
3834 ExprBits.InstantiationDependent = true;
3835 if (E->containsUnexpandedParameterPack())
3836 ExprBits.ContainsUnexpandedParameterPack = true;
3837
3838 if (isa<OpaqueValueExpr>(E))
3839 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3840 "opaque-value semantic expressions for pseudo-object "
3841 "operations must have sources");
3842 }
3843}
3844
Douglas Gregor05c13a32009-01-22 00:58:24 +00003845//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003846// ExprIterator.
3847//===----------------------------------------------------------------------===//
3848
3849Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3850Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3851Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3852const Expr* ConstExprIterator::operator[](size_t idx) const {
3853 return cast<Expr>(I[idx]);
3854}
3855const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3856const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3857
3858//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003859// Child Iterators for iterating over subexpressions/substatements
3860//===----------------------------------------------------------------------===//
3861
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003862// UnaryExprOrTypeTraitExpr
3863Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003864 // If this is of a type and the type is a VLA type (and not a typedef), the
3865 // size expression of the VLA needs to be treated as an executable expression.
3866 // Why isn't this weirdness documented better in StmtIterator?
3867 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003868 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003869 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003870 return child_range(child_iterator(T), child_iterator());
3871 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003872 }
John McCall63c00d72011-02-09 08:16:59 +00003873 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003874}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003875
Steve Naroff563477d2007-09-18 23:55:05 +00003876// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003877Stmt::child_range ObjCMessageExpr::children() {
3878 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003879 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003880 begin = reinterpret_cast<Stmt **>(this + 1);
3881 else
3882 begin = reinterpret_cast<Stmt **>(getArgs());
3883 return child_range(begin,
3884 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003885}
3886
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003887ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3888 QualType T, ObjCMethodDecl *Method,
3889 SourceRange SR)
3890 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3891 false, false, false, false),
3892 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3893{
3894 Expr **SaveElements = getElements();
3895 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3896 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3897 ExprBits.ValueDependent = true;
3898 if (Elements[I]->isInstantiationDependent())
3899 ExprBits.InstantiationDependent = true;
3900 if (Elements[I]->containsUnexpandedParameterPack())
3901 ExprBits.ContainsUnexpandedParameterPack = true;
3902
3903 SaveElements[I] = Elements[I];
3904 }
3905}
3906
3907ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3908 llvm::ArrayRef<Expr *> Elements,
3909 QualType T, ObjCMethodDecl * Method,
3910 SourceRange SR) {
3911 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3912 + Elements.size() * sizeof(Expr *));
3913 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3914}
3915
3916ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3917 unsigned NumElements) {
3918
3919 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3920 + NumElements * sizeof(Expr *));
3921 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3922}
3923
3924ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3925 ArrayRef<ObjCDictionaryElement> VK,
3926 bool HasPackExpansions,
3927 QualType T, ObjCMethodDecl *method,
3928 SourceRange SR)
3929 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3930 false, false),
3931 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3932 DictWithObjectsMethod(method)
3933{
3934 KeyValuePair *KeyValues = getKeyValues();
3935 ExpansionData *Expansions = getExpansionData();
3936 for (unsigned I = 0; I < NumElements; I++) {
3937 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3938 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3939 ExprBits.ValueDependent = true;
3940 if (VK[I].Key->isInstantiationDependent() ||
3941 VK[I].Value->isInstantiationDependent())
3942 ExprBits.InstantiationDependent = true;
3943 if (VK[I].EllipsisLoc.isInvalid() &&
3944 (VK[I].Key->containsUnexpandedParameterPack() ||
3945 VK[I].Value->containsUnexpandedParameterPack()))
3946 ExprBits.ContainsUnexpandedParameterPack = true;
3947
3948 KeyValues[I].Key = VK[I].Key;
3949 KeyValues[I].Value = VK[I].Value;
3950 if (Expansions) {
3951 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3952 if (VK[I].NumExpansions)
3953 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3954 else
3955 Expansions[I].NumExpansionsPlusOne = 0;
3956 }
3957 }
3958}
3959
3960ObjCDictionaryLiteral *
3961ObjCDictionaryLiteral::Create(ASTContext &C,
3962 ArrayRef<ObjCDictionaryElement> VK,
3963 bool HasPackExpansions,
3964 QualType T, ObjCMethodDecl *method,
3965 SourceRange SR) {
3966 unsigned ExpansionsSize = 0;
3967 if (HasPackExpansions)
3968 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3969
3970 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3971 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3972 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3973}
3974
3975ObjCDictionaryLiteral *
3976ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3977 bool HasPackExpansions) {
3978 unsigned ExpansionsSize = 0;
3979 if (HasPackExpansions)
3980 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3981 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3982 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3983 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3984 HasPackExpansions);
3985}
3986
3987ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3988 Expr *base,
3989 Expr *key, QualType T,
3990 ObjCMethodDecl *getMethod,
3991 ObjCMethodDecl *setMethod,
3992 SourceLocation RB) {
3993 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3994 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3995 OK_ObjCSubscript,
3996 getMethod, setMethod, RB);
3997}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003998
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00003999AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args,
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004000 QualType t, AtomicOp op, SourceLocation RP)
4001 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
4002 false, false, false, false),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004003 NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004004{
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00004005 assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions");
4006 for (unsigned i = 0; i != args.size(); i++) {
Eli Friedmandfa64ba2011-10-14 22:48:56 +00004007 if (args[i]->isTypeDependent())
4008 ExprBits.TypeDependent = true;
4009 if (args[i]->isValueDependent())
4010 ExprBits.ValueDependent = true;
4011 if (args[i]->isInstantiationDependent())
4012 ExprBits.InstantiationDependent = true;
4013 if (args[i]->containsUnexpandedParameterPack())
4014 ExprBits.ContainsUnexpandedParameterPack = true;
4015
4016 SubExprs[i] = args[i];
4017 }
4018}
Richard Smithe1b2abc2012-04-10 22:49:28 +00004019
4020unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
4021 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00004022 case AO__c11_atomic_init:
4023 case AO__c11_atomic_load:
4024 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004025 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00004026
4027 case AO__c11_atomic_store:
4028 case AO__c11_atomic_exchange:
4029 case AO__atomic_load:
4030 case AO__atomic_store:
4031 case AO__atomic_store_n:
4032 case AO__atomic_exchange_n:
4033 case AO__c11_atomic_fetch_add:
4034 case AO__c11_atomic_fetch_sub:
4035 case AO__c11_atomic_fetch_and:
4036 case AO__c11_atomic_fetch_or:
4037 case AO__c11_atomic_fetch_xor:
4038 case AO__atomic_fetch_add:
4039 case AO__atomic_fetch_sub:
4040 case AO__atomic_fetch_and:
4041 case AO__atomic_fetch_or:
4042 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00004043 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00004044 case AO__atomic_add_fetch:
4045 case AO__atomic_sub_fetch:
4046 case AO__atomic_and_fetch:
4047 case AO__atomic_or_fetch:
4048 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00004049 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004050 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00004051
4052 case AO__atomic_exchange:
4053 return 4;
4054
4055 case AO__c11_atomic_compare_exchange_strong:
4056 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00004057 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00004058
4059 case AO__atomic_compare_exchange:
4060 case AO__atomic_compare_exchange_n:
4061 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00004062 }
4063 llvm_unreachable("unknown atomic op");
4064}