blob: 3264e4ce0e240d17b9be33bd252d39fcf0ea1c6c [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
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000033#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattner2b334bb2010-04-16 23:34:13 +000036/// isKnownToHaveBooleanValue - Return true if this is an integer expression
37/// that is known to return 0 or 1. This happens for _Bool/bool expressions
38/// but also int expressions which are produced by things like comparisons in
39/// C.
40bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000041 const Expr *E = IgnoreParens();
42
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000045 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000047
Peter Collingbournef111d932011-04-15 00:35:48 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000051 return UO->getSubExpr()->isKnownToHaveBooleanValue();
52 default:
53 return false;
54 }
55 }
Sean Huntc3021132010-05-05 15:23:54 +000056
John McCall6907fbe2010-06-12 01:56:02 +000057 // Only look through implicit casts. If the user writes
58 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000061
Peter Collingbournef111d932011-04-15 00:35:48 +000062 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 switch (BO->getOpcode()) {
64 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000065 case BO_LT: // Relational operators.
66 case BO_GT:
67 case BO_LE:
68 case BO_GE:
69 case BO_EQ: // Equality operators.
70 case BO_NE:
71 case BO_LAnd: // AND operator.
72 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000073 return true;
Sean Huntc3021132010-05-05 15:23:54 +000074
John McCall2de56d12010-08-25 11:45:40 +000075 case BO_And: // Bitwise AND operator.
76 case BO_Xor: // Bitwise XOR operator.
77 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000078 // Handle things like (x==2)|(y==12).
79 return BO->getLHS()->isKnownToHaveBooleanValue() &&
80 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000081
John McCall2de56d12010-08-25 11:45:40 +000082 case BO_Comma:
83 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000084 return BO->getRHS()->isKnownToHaveBooleanValue();
85 }
86 }
Sean Huntc3021132010-05-05 15:23:54 +000087
Peter Collingbournef111d932011-04-15 00:35:48 +000088 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
90 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000091
Chris Lattner2b334bb2010-04-16 23:34:13 +000092 return false;
93}
94
John McCall63c00d72011-02-09 08:16:59 +000095// Amusing macro metaprogramming hack: check whether a class provides
96// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +000097//
98// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +000099namespace {
100 /// This implementation is used when a class provides a custom
101 /// implementation of getExprLoc.
102 template <class E, class T>
103 SourceLocation getExprLocImpl(const Expr *expr,
104 SourceLocation (T::*v)() const) {
105 return static_cast<const E*>(expr)->getExprLoc();
106 }
107
108 /// This implementation is used when a class doesn't provide
109 /// a custom implementation of getExprLoc. Overload resolution
110 /// should pick it over the implementation above because it's
111 /// more specialized according to function template partial ordering.
112 template <class E>
113 SourceLocation getExprLocImpl(const Expr *expr,
114 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000115 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000116 }
117}
118
119SourceLocation Expr::getExprLoc() const {
120 switch (getStmtClass()) {
121 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
122#define ABSTRACT_STMT(type)
123#define STMT(type, base) \
124 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
125#define EXPR(type, base) \
126 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
127#include "clang/AST/StmtNodes.inc"
128 }
129 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132//===----------------------------------------------------------------------===//
133// Primary Expressions.
134//===----------------------------------------------------------------------===//
135
Douglas Gregor561f8122011-07-01 01:22:09 +0000136/// \brief Compute the type-, value-, and instantiation-dependence of a
137/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000138/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000139static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000140 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000141 bool &ValueDependent,
142 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000143 TypeDependent = false;
144 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000146
147 // (TD) C++ [temp.dep.expr]p3:
148 // An id-expression is type-dependent if it contains:
149 //
Sean Huntc3021132010-05-05 15:23:54 +0000150 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 //
152 // (VD) C++ [temp.dep.constexpr]p2:
153 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000154
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 // (TD) - an identifier that was declared with dependent type
156 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000157 if (T->isDependentType()) {
158 TypeDependent = true;
159 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000161 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000162 } else if (T->isInstantiationDependentType()) {
163 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 }
Douglas Gregord967e312011-01-19 21:52:31 +0000165
Douglas Gregor0da76df2009-11-23 11:41:28 +0000166 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000168 == DeclarationName::CXXConversionFunctionName) {
169 QualType T = D->getDeclName().getCXXNameType();
170 if (T->isDependentType()) {
171 TypeDependent = true;
172 ValueDependent = true;
173 InstantiationDependent = true;
174 return;
175 }
176
177 if (T->isInstantiationDependentType())
178 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000180
Douglas Gregor0da76df2009-11-23 11:41:28 +0000181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000184 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000185 return;
186 }
187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188 // (VD) - a constant with integral or enumeration type and is
189 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000190 // (VD) - a constant with literal type and is initialized with an
191 // expression that is value-dependent [C++11].
192 // (VD) - FIXME: Missing from the standard:
193 // - an entity with reference type and is initialized with an
194 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000195 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000196 if ((Ctx.getLangOptions().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000197 Var->getType()->isLiteralType() :
198 Var->getType()->isIntegralOrEnumerationType()) &&
199 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
200 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000201 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000206 }
207
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000208 // (VD) - FIXME: Missing from the standard:
209 // - a member function or a static data member of the current
210 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000211 if (Var->isStaticDataMember() &&
212 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000213 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 InstantiationDependent = true;
215 }
Douglas Gregord967e312011-01-19 21:52:31 +0000216
217 return;
218 }
219
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000220 // (VD) - FIXME: Missing from the standard:
221 // - a member function or a static data member of the current
222 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000223 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
224 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000225 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000226 }
Douglas Gregord967e312011-01-19 21:52:31 +0000227}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000228
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000229void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000230 bool TypeDependent = false;
231 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000233 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
234 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000235
236 // (TD) C++ [temp.dep.expr]p3:
237 // An id-expression is type-dependent if it contains:
238 //
239 // and
240 //
241 // (VD) C++ [temp.dep.constexpr]p2:
242 // An identifier is value-dependent if it is:
243 if (!TypeDependent && !ValueDependent &&
244 hasExplicitTemplateArgs() &&
245 TemplateSpecializationType::anyDependentTemplateArguments(
246 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 getNumTemplateArgs(),
248 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000249 TypeDependent = true;
250 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000251 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000252 }
253
254 ExprBits.TypeDependent = TypeDependent;
255 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000256 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000257
Douglas Gregor10738d32010-12-23 23:51:58 +0000258 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000259 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000261}
262
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000263DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000266 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000267 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000268 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000269 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000270 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000271 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
272 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000273 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000274 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000275 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
276 if (FoundD)
277 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000278 DeclRefExprBits.HasTemplateKWAndArgsInfo
279 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000280 if (TemplateArgs) {
281 bool Dependent = false;
282 bool InstantiationDependent = false;
283 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000284 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
285 Dependent,
286 InstantiationDependent,
287 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000288 if (InstantiationDependent)
289 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000290 } else if (TemplateKWLoc.isValid()) {
291 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000292 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000293 DeclRefExprBits.HadMultipleCandidates = 0;
294
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000295 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000296}
297
Douglas Gregora2813ce2009-10-23 18:54:35 +0000298DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000299 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000300 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000301 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000302 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000303 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000304 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000305 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000306 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000307 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000308 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000309 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000310}
311
312DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000313 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000314 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000315 ValueDecl *D,
316 const DeclarationNameInfo &NameInfo,
317 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000318 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000319 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000320 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000321 // Filter out cases where the found Decl is the same as the value refenenced.
322 if (D == FoundD)
323 FoundD = 0;
324
Douglas Gregora2813ce2009-10-23 18:54:35 +0000325 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000326 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000327 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000328 if (FoundD)
329 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000330 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000331 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
332 else if (TemplateKWLoc.isValid())
333 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000334
Chris Lattner32488542010-10-30 05:14:06 +0000335 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000336 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
337 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000338}
339
Chandler Carruth3aa81402011-05-01 23:48:14 +0000340DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000341 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000342 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000343 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000344 unsigned NumTemplateArgs) {
345 std::size_t Size = sizeof(DeclRefExpr);
346 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000347 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000348 if (HasFoundDecl)
349 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000350 if (HasTemplateKWAndArgsInfo)
351 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000352
Chris Lattner32488542010-10-30 05:14:06 +0000353 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000354 return new (Mem) DeclRefExpr(EmptyShell());
355}
356
Douglas Gregora2813ce2009-10-23 18:54:35 +0000357SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000358 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000359 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000360 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000361 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000362 R.setEnd(getRAngleLoc());
363 return R;
364}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000365SourceLocation DeclRefExpr::getLocStart() const {
366 if (hasQualifier())
367 return getQualifierLoc().getBeginLoc();
368 return getNameInfo().getLocStart();
369}
370SourceLocation DeclRefExpr::getLocEnd() const {
371 if (hasExplicitTemplateArgs())
372 return getRAngleLoc();
373 return getNameInfo().getLocEnd();
374}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000375
Anders Carlsson3a082d82009-09-08 18:24:21 +0000376// FIXME: Maybe this should use DeclPrinter with a special "print predefined
377// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000378std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
379 ASTContext &Context = CurrentDecl->getASTContext();
380
Anders Carlsson3a082d82009-09-08 18:24:21 +0000381 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000382 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000383 return FD->getNameAsString();
384
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000385 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000386 llvm::raw_svector_ostream Out(Name);
387
388 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000389 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000390 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000391 if (MD->isStatic())
392 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000393 }
394
395 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396
397 std::string Proto = FD->getQualifiedNameAsString(Policy);
398
John McCall183700f2009-09-21 23:43:11 +0000399 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000400 const FunctionProtoType *FT = 0;
401 if (FD->hasWrittenPrototype())
402 FT = dyn_cast<FunctionProtoType>(AFT);
403
404 Proto += "(";
405 if (FT) {
406 llvm::raw_string_ostream POut(Proto);
407 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
408 if (i) POut << ", ";
409 std::string Param;
410 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
411 POut << Param;
412 }
413
414 if (FT->isVariadic()) {
415 if (FD->getNumParams()) POut << ", ";
416 POut << "...";
417 }
418 }
419 Proto += ")";
420
Sam Weinig4eadcc52009-12-27 01:38:20 +0000421 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
422 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
423 if (ThisQuals.hasConst())
424 Proto += " const";
425 if (ThisQuals.hasVolatile())
426 Proto += " volatile";
427 }
428
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000429 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
430 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000431
432 Out << Proto;
433
434 Out.flush();
435 return Name.str().str();
436 }
437 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000438 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000439 llvm::raw_svector_ostream Out(Name);
440 Out << (MD->isInstanceMethod() ? '-' : '+');
441 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000442
443 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
444 // a null check to avoid a crash.
445 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000446 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000447
Anders Carlsson3a082d82009-09-08 18:24:21 +0000448 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000449 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000450 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000451
Anders Carlsson3a082d82009-09-08 18:24:21 +0000452 Out << ' ';
453 Out << MD->getSelector().getAsString();
454 Out << ']';
455
456 Out.flush();
457 return Name.str().str();
458 }
459 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
460 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
461 return "top level";
462 }
463 return "";
464}
465
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000466void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
467 if (hasAllocation())
468 C.Deallocate(pVal);
469
470 BitWidth = Val.getBitWidth();
471 unsigned NumWords = Val.getNumWords();
472 const uint64_t* Words = Val.getRawData();
473 if (NumWords > 1) {
474 pVal = new (C) uint64_t[NumWords];
475 std::copy(Words, Words + NumWords, pVal);
476 } else if (NumWords == 1)
477 VAL = Words[0];
478 else
479 VAL = 0;
480}
481
482IntegerLiteral *
483IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
484 QualType type, SourceLocation l) {
485 return new (C) IntegerLiteral(C, V, type, l);
486}
487
488IntegerLiteral *
489IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
490 return new (C) IntegerLiteral(Empty);
491}
492
493FloatingLiteral *
494FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
495 bool isexact, QualType Type, SourceLocation L) {
496 return new (C) FloatingLiteral(C, V, isexact, Type, L);
497}
498
499FloatingLiteral *
500FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000501 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000502}
503
Chris Lattnerda8249e2008-06-07 22:13:43 +0000504/// getValueAsApproximateDouble - This returns the value as an inaccurate
505/// double. Note that this may cause loss of precision, but is useful for
506/// debugging dumps, etc.
507double FloatingLiteral::getValueAsApproximateDouble() const {
508 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000509 bool ignored;
510 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
511 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000512 return V.convertToDouble();
513}
514
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000515int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000516 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000517 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000518 case Ascii:
519 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000520 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000521 break;
522 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000523 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000524 break;
525 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000526 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000527 break;
528 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000529 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000530 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000531 }
532 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
533 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000534 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000535 && "character byte widths supported are 1, 2, and 4 only");
536 return CharByteWidth;
537}
538
Chris Lattner5f9e2722011-07-23 10:55:15 +0000539StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000540 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000541 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000542 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000543 // Allocate enough space for the StringLiteral plus an array of locations for
544 // any concatenated string tokens.
545 void *Mem = C.Allocate(sizeof(StringLiteral)+
546 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000547 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000548 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000551 SL->setString(C,Str,Kind,Pascal);
552
Chris Lattner2085fd62009-02-18 06:40:38 +0000553 SL->TokLocs[0] = Loc[0];
554 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
Chris Lattner726e1682009-02-18 05:49:11 +0000556 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000557 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
558 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000559}
560
Douglas Gregor673ecd62009-04-15 16:35:07 +0000561StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
562 void *Mem = C.Allocate(sizeof(StringLiteral)+
563 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000564 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000565 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000566 SL->CharByteWidth = 0;
567 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000568 SL->NumConcatenated = NumStrs;
569 return SL;
570}
571
Eli Friedman64f45a22011-11-01 02:23:42 +0000572void StringLiteral::setString(ASTContext &C, StringRef Str,
573 StringKind Kind, bool IsPascal) {
574 //FIXME: we assume that the string data comes from a target that uses the same
575 // code unit size and endianess for the type of string.
576 this->Kind = Kind;
577 this->IsPascal = IsPascal;
578
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000579 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000580 assert((Str.size()%CharByteWidth == 0)
581 && "size of data must be multiple of CharByteWidth");
582 Length = Str.size()/CharByteWidth;
583
584 switch(CharByteWidth) {
585 case 1: {
586 char *AStrData = new (C) char[Length];
587 std::memcpy(AStrData,Str.data(),Str.size());
588 StrData.asChar = AStrData;
589 break;
590 }
591 case 2: {
592 uint16_t *AStrData = new (C) uint16_t[Length];
593 std::memcpy(AStrData,Str.data(),Str.size());
594 StrData.asUInt16 = AStrData;
595 break;
596 }
597 case 4: {
598 uint32_t *AStrData = new (C) uint32_t[Length];
599 std::memcpy(AStrData,Str.data(),Str.size());
600 StrData.asUInt32 = AStrData;
601 break;
602 }
603 default:
604 assert(false && "unsupported CharByteWidth");
605 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000606}
607
Chris Lattner08f92e32010-11-17 07:37:15 +0000608/// getLocationOfByte - Return a source location that points to the specified
609/// byte of this string literal.
610///
611/// Strings are amazingly complex. They can be formed from multiple tokens and
612/// can have escape sequences in them in addition to the usual trigraph and
613/// escaped newline business. This routine handles this complexity.
614///
615SourceLocation StringLiteral::
616getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
617 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000618 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
619
Chris Lattner08f92e32010-11-17 07:37:15 +0000620 // Loop over all of the tokens in this string until we find the one that
621 // contains the byte we're looking for.
622 unsigned TokNo = 0;
623 while (1) {
624 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
625 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
626
627 // Get the spelling of the string so that we can get the data that makes up
628 // the string literal, not the identifier for the macro it is potentially
629 // expanded through.
630 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
631
632 // Re-lex the token to get its length and original spelling.
633 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
634 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000635 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000636 if (Invalid)
637 return StrTokSpellingLoc;
638
639 const char *StrData = Buffer.data()+LocInfo.second;
640
641 // Create a langops struct and enable trigraphs. This is sufficient for
642 // relexing tokens.
643 LangOptions LangOpts;
644 LangOpts.Trigraphs = true;
645
646 // Create a lexer starting at the beginning of this token.
647 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
648 Buffer.end());
649 Token TheTok;
650 TheLexer.LexFromRawLexer(TheTok);
651
652 // Use the StringLiteralParser to compute the length of the string in bytes.
653 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
654 unsigned TokNumBytes = SLP.GetStringLength();
655
656 // If the byte is in this token, return the location of the byte.
657 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000658 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000659 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
660
661 // Now that we know the offset of the token in the spelling, use the
662 // preprocessor to get the offset in the original source.
663 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
664 }
665
666 // Move to the next string token.
667 ++TokNo;
668 ByteNo -= TokNumBytes;
669 }
670}
671
672
673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
675/// corresponds to, e.g. "sizeof" or "[pre]++".
676const char *UnaryOperator::getOpcodeStr(Opcode Op) {
677 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000678 case UO_PostInc: return "++";
679 case UO_PostDec: return "--";
680 case UO_PreInc: return "++";
681 case UO_PreDec: return "--";
682 case UO_AddrOf: return "&";
683 case UO_Deref: return "*";
684 case UO_Plus: return "+";
685 case UO_Minus: return "-";
686 case UO_Not: return "~";
687 case UO_LNot: return "!";
688 case UO_Real: return "__real";
689 case UO_Imag: return "__imag";
690 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000692 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000693}
694
John McCall2de56d12010-08-25 11:45:40 +0000695UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000696UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
697 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000698 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000699 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
700 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
701 case OO_Amp: return UO_AddrOf;
702 case OO_Star: return UO_Deref;
703 case OO_Plus: return UO_Plus;
704 case OO_Minus: return UO_Minus;
705 case OO_Tilde: return UO_Not;
706 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000707 }
708}
709
710OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
711 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000712 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
713 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
714 case UO_AddrOf: return OO_Amp;
715 case UO_Deref: return OO_Star;
716 case UO_Plus: return OO_Plus;
717 case UO_Minus: return OO_Minus;
718 case UO_Not: return OO_Tilde;
719 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000720 default: return OO_None;
721 }
722}
723
724
Reid Spencer5f016e22007-07-11 17:01:13 +0000725//===----------------------------------------------------------------------===//
726// Postfix Operators.
727//===----------------------------------------------------------------------===//
728
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000729CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
730 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000731 SourceLocation rparenloc)
732 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000733 fn->isTypeDependent(),
734 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000735 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000736 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000737 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000739 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000740 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000741 for (unsigned i = 0; i != numargs; ++i) {
742 if (args[i]->isTypeDependent())
743 ExprBits.TypeDependent = true;
744 if (args[i]->isValueDependent())
745 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000746 if (args[i]->isInstantiationDependent())
747 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000748 if (args[i]->containsUnexpandedParameterPack())
749 ExprBits.ContainsUnexpandedParameterPack = true;
750
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000751 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000752 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000753
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000754 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000755 RParenLoc = rparenloc;
756}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000757
Ted Kremenek668bf912009-02-09 20:51:47 +0000758CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000759 QualType t, ExprValueKind VK, SourceLocation rparenloc)
760 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000761 fn->isTypeDependent(),
762 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000763 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000764 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000765 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000766
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000767 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000768 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000769 for (unsigned i = 0; i != numargs; ++i) {
770 if (args[i]->isTypeDependent())
771 ExprBits.TypeDependent = true;
772 if (args[i]->isValueDependent())
773 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000774 if (args[i]->isInstantiationDependent())
775 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000776 if (args[i]->containsUnexpandedParameterPack())
777 ExprBits.ContainsUnexpandedParameterPack = true;
778
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000779 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000780 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000781
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000782 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 RParenLoc = rparenloc;
784}
785
Mike Stump1eb44332009-09-09 15:08:12 +0000786CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
787 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000788 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000789 SubExprs = new (C) Stmt*[PREARGS_START];
790 CallExprBits.NumPreArgs = 0;
791}
792
793CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
794 EmptyShell Empty)
795 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
796 // FIXME: Why do we allocate this?
797 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
798 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000799}
800
Nuno Lopesd20254f2009-12-20 23:11:08 +0000801Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000802 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000803
804 while (SubstNonTypeTemplateParmExpr *NTTP
805 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
806 CEE = NTTP->getReplacement()->IgnoreParenCasts();
807 }
808
Sebastian Redl20012152010-09-10 20:55:30 +0000809 // If we're calling a dereference, look at the pointer instead.
810 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
811 if (BO->isPtrMemOp())
812 CEE = BO->getRHS()->IgnoreParenCasts();
813 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
814 if (UO->getOpcode() == UO_Deref)
815 CEE = UO->getSubExpr()->IgnoreParenCasts();
816 }
Chris Lattner6346f962009-07-17 15:46:27 +0000817 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000818 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000819 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
820 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000821
822 return 0;
823}
824
Nuno Lopesd20254f2009-12-20 23:11:08 +0000825FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000826 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000827}
828
Chris Lattnerd18b3292007-12-28 05:25:02 +0000829/// setNumArgs - This changes the number of arguments present in this call.
830/// Any orphaned expressions are deleted by this, and any new operands are set
831/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000832void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000833 // No change, just return.
834 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattnerd18b3292007-12-28 05:25:02 +0000836 // If shrinking # arguments, just delete the extras and forgot them.
837 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000838 this->NumArgs = NumArgs;
839 return;
840 }
841
842 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000843 unsigned NumPreArgs = getNumPreArgs();
844 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000845 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000846 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000847 NewSubExprs[i] = SubExprs[i];
848 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000849 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
850 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000851 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor88c9a462009-04-17 21:46:47 +0000853 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000854 SubExprs = NewSubExprs;
855 this->NumArgs = NumArgs;
856}
857
Chris Lattnercb888962008-10-06 05:00:53 +0000858/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
859/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000860unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000861 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000862 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000863 // ImplicitCastExpr.
864 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
865 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000866 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000868 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
869 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000870 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Anders Carlssonbcba2012008-01-31 02:13:57 +0000872 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
873 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000874 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000876 if (!FDecl->getIdentifier())
877 return 0;
878
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000879 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000880}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000881
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000882QualType CallExpr::getCallReturnType() const {
883 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000884 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000885 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000886 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000887 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000888 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
889 // This should never be overloaded and so should never return null.
890 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000891
John McCall864c0412011-04-26 20:42:42 +0000892 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000893 return FnType->getResultType();
894}
Chris Lattnercb888962008-10-06 05:00:53 +0000895
John McCall2882eca2011-02-21 06:23:05 +0000896SourceRange CallExpr::getSourceRange() const {
897 if (isa<CXXOperatorCallExpr>(this))
898 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
899
900 SourceLocation begin = getCallee()->getLocStart();
901 if (begin.isInvalid() && getNumArgs() > 0)
902 begin = getArg(0)->getLocStart();
903 SourceLocation end = getRParenLoc();
904 if (end.isInvalid() && getNumArgs() > 0)
905 end = getArg(getNumArgs() - 1)->getLocEnd();
906 return SourceRange(begin, end);
907}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +0000908SourceLocation CallExpr::getLocStart() const {
909 if (isa<CXXOperatorCallExpr>(this))
910 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
911
912 SourceLocation begin = getCallee()->getLocStart();
913 if (begin.isInvalid() && getNumArgs() > 0)
914 begin = getArg(0)->getLocStart();
915 return begin;
916}
917SourceLocation CallExpr::getLocEnd() const {
918 if (isa<CXXOperatorCallExpr>(this))
919 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
920
921 SourceLocation end = getRParenLoc();
922 if (end.isInvalid() && getNumArgs() > 0)
923 end = getArg(getNumArgs() - 1)->getLocEnd();
924 return end;
925}
John McCall2882eca2011-02-21 06:23:05 +0000926
Sean Huntc3021132010-05-05 15:23:54 +0000927OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000928 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000929 TypeSourceInfo *tsi,
930 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000931 Expr** exprsPtr, unsigned numExprs,
932 SourceLocation RParenLoc) {
933 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000934 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000935 sizeof(Expr*) * numExprs);
936
937 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
938 exprsPtr, numExprs, RParenLoc);
939}
940
941OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
942 unsigned numComps, unsigned numExprs) {
943 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
944 sizeof(OffsetOfNode) * numComps +
945 sizeof(Expr*) * numExprs);
946 return new (Mem) OffsetOfExpr(numComps, numExprs);
947}
948
Sean Huntc3021132010-05-05 15:23:54 +0000949OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000950 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000951 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000952 Expr** exprsPtr, unsigned numExprs,
953 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000954 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
955 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000956 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000957 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000958 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000959 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
960 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000961{
962 for(unsigned i = 0; i < numComps; ++i) {
963 setComponent(i, compsPtr[i]);
964 }
Sean Huntc3021132010-05-05 15:23:54 +0000965
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000966 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000967 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
968 ExprBits.ValueDependent = true;
969 if (exprsPtr[i]->containsUnexpandedParameterPack())
970 ExprBits.ContainsUnexpandedParameterPack = true;
971
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000972 setIndexExpr(i, exprsPtr[i]);
973 }
974}
975
976IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
977 assert(getKind() == Field || getKind() == Identifier);
978 if (getKind() == Field)
979 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000980
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000981 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
982}
983
Mike Stump1eb44332009-09-09 15:08:12 +0000984MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000985 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000986 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000987 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000988 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000989 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000990 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000991 QualType ty,
992 ExprValueKind vk,
993 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000994 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000995
Douglas Gregor40d96a62011-02-28 21:54:11 +0000996 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000997 founddecl.getDecl() != memberdecl ||
998 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000999 if (hasQualOrFound)
1000 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
John McCalld5532b62009-11-23 01:53:49 +00001002 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001003 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1004 else if (TemplateKWLoc.isValid())
1005 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner32488542010-10-30 05:14:06 +00001007 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001008 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1009 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001010
1011 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001012 // FIXME: Wrong. We should be looking at the member declaration we found.
1013 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001014 E->setValueDependent(true);
1015 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001016 E->setInstantiationDependent(true);
1017 }
1018 else if (QualifierLoc &&
1019 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1020 E->setInstantiationDependent(true);
1021
John McCall6bb80172010-03-30 21:47:33 +00001022 E->HasQualifierOrFoundDecl = true;
1023
1024 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001025 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001026 NQ->FoundDecl = founddecl;
1027 }
1028
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001029 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1030
John McCall6bb80172010-03-30 21:47:33 +00001031 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001032 bool Dependent = false;
1033 bool InstantiationDependent = false;
1034 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001035 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1036 Dependent,
1037 InstantiationDependent,
1038 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001039 if (InstantiationDependent)
1040 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001041 } else if (TemplateKWLoc.isValid()) {
1042 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001043 }
1044
1045 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001046}
1047
Douglas Gregor75e85042011-03-02 21:06:53 +00001048SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001049 return SourceRange(getLocStart(), getLocEnd());
1050}
1051SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001052 if (isImplicitAccess()) {
1053 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001054 return getQualifierLoc().getBeginLoc();
1055 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001056 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001057
Daniel Dunbar396ec672012-03-09 15:39:15 +00001058 // FIXME: We don't want this to happen. Rather, we should be able to
1059 // detect all kinds of implicit accesses more cleanly.
1060 SourceLocation BaseStartLoc = getBase()->getLocStart();
1061 if (BaseStartLoc.isValid())
1062 return BaseStartLoc;
1063 return MemberLoc;
1064}
1065SourceLocation MemberExpr::getLocEnd() const {
1066 if (hasExplicitTemplateArgs())
1067 return getRAngleLoc();
1068 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001069}
1070
John McCall1d9b3b22011-09-09 05:25:32 +00001071void CastExpr::CheckCastConsistency() const {
1072 switch (getCastKind()) {
1073 case CK_DerivedToBase:
1074 case CK_UncheckedDerivedToBase:
1075 case CK_DerivedToBaseMemberPointer:
1076 case CK_BaseToDerived:
1077 case CK_BaseToDerivedMemberPointer:
1078 assert(!path_empty() && "Cast kind should have a base path!");
1079 break;
1080
1081 case CK_CPointerToObjCPointerCast:
1082 assert(getType()->isObjCObjectPointerType());
1083 assert(getSubExpr()->getType()->isPointerType());
1084 goto CheckNoBasePath;
1085
1086 case CK_BlockPointerToObjCPointerCast:
1087 assert(getType()->isObjCObjectPointerType());
1088 assert(getSubExpr()->getType()->isBlockPointerType());
1089 goto CheckNoBasePath;
1090
John McCall4d4e5c12012-02-15 01:22:51 +00001091 case CK_ReinterpretMemberPointer:
1092 assert(getType()->isMemberPointerType());
1093 assert(getSubExpr()->getType()->isMemberPointerType());
1094 goto CheckNoBasePath;
1095
John McCall1d9b3b22011-09-09 05:25:32 +00001096 case CK_BitCast:
1097 // Arbitrary casts to C pointer types count as bitcasts.
1098 // Otherwise, we should only have block and ObjC pointer casts
1099 // here if they stay within the type kind.
1100 if (!getType()->isPointerType()) {
1101 assert(getType()->isObjCObjectPointerType() ==
1102 getSubExpr()->getType()->isObjCObjectPointerType());
1103 assert(getType()->isBlockPointerType() ==
1104 getSubExpr()->getType()->isBlockPointerType());
1105 }
1106 goto CheckNoBasePath;
1107
1108 case CK_AnyPointerToBlockPointerCast:
1109 assert(getType()->isBlockPointerType());
1110 assert(getSubExpr()->getType()->isAnyPointerType() &&
1111 !getSubExpr()->getType()->isBlockPointerType());
1112 goto CheckNoBasePath;
1113
Douglas Gregorac1303e2012-02-22 05:02:47 +00001114 case CK_CopyAndAutoreleaseBlockObject:
1115 assert(getType()->isBlockPointerType());
1116 assert(getSubExpr()->getType()->isBlockPointerType());
1117 goto CheckNoBasePath;
1118
John McCall1d9b3b22011-09-09 05:25:32 +00001119 // These should not have an inheritance path.
1120 case CK_Dynamic:
1121 case CK_ToUnion:
1122 case CK_ArrayToPointerDecay:
1123 case CK_FunctionToPointerDecay:
1124 case CK_NullToMemberPointer:
1125 case CK_NullToPointer:
1126 case CK_ConstructorConversion:
1127 case CK_IntegralToPointer:
1128 case CK_PointerToIntegral:
1129 case CK_ToVoid:
1130 case CK_VectorSplat:
1131 case CK_IntegralCast:
1132 case CK_IntegralToFloating:
1133 case CK_FloatingToIntegral:
1134 case CK_FloatingCast:
1135 case CK_ObjCObjectLValueCast:
1136 case CK_FloatingRealToComplex:
1137 case CK_FloatingComplexToReal:
1138 case CK_FloatingComplexCast:
1139 case CK_FloatingComplexToIntegralComplex:
1140 case CK_IntegralRealToComplex:
1141 case CK_IntegralComplexToReal:
1142 case CK_IntegralComplexCast:
1143 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001144 case CK_ARCProduceObject:
1145 case CK_ARCConsumeObject:
1146 case CK_ARCReclaimReturnedObject:
1147 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001148 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1149 goto CheckNoBasePath;
1150
1151 case CK_Dependent:
1152 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001153 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001154 case CK_AtomicToNonAtomic:
1155 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001156 case CK_PointerToBoolean:
1157 case CK_IntegralToBoolean:
1158 case CK_FloatingToBoolean:
1159 case CK_MemberPointerToBoolean:
1160 case CK_FloatingComplexToBoolean:
1161 case CK_IntegralComplexToBoolean:
1162 case CK_LValueBitCast: // -> bool&
1163 case CK_UserDefinedConversion: // operator bool()
1164 CheckNoBasePath:
1165 assert(path_empty() && "Cast kind should not have a base path!");
1166 break;
1167 }
1168}
1169
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001170const char *CastExpr::getCastKindName() const {
1171 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001172 case CK_Dependent:
1173 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001174 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001175 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001176 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001177 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001178 case CK_LValueToRValue:
1179 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001180 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001181 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001182 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001183 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001184 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001185 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001186 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001187 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001188 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001189 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001190 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001191 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001192 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001193 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001194 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001195 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001196 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001197 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001198 case CK_NullToPointer:
1199 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001200 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001201 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001202 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001203 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001204 case CK_ReinterpretMemberPointer:
1205 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001206 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001207 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001208 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001209 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001210 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001211 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001212 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001213 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001214 case CK_PointerToBoolean:
1215 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001216 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001217 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001218 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001219 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001220 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001221 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001222 case CK_IntegralToBoolean:
1223 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001224 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001225 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001226 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001227 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001228 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001229 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001230 case CK_FloatingToBoolean:
1231 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001232 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001233 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001234 case CK_CPointerToObjCPointerCast:
1235 return "CPointerToObjCPointerCast";
1236 case CK_BlockPointerToObjCPointerCast:
1237 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001238 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001239 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001240 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001241 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001242 case CK_FloatingRealToComplex:
1243 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001244 case CK_FloatingComplexToReal:
1245 return "FloatingComplexToReal";
1246 case CK_FloatingComplexToBoolean:
1247 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001248 case CK_FloatingComplexCast:
1249 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001250 case CK_FloatingComplexToIntegralComplex:
1251 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001252 case CK_IntegralRealToComplex:
1253 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001254 case CK_IntegralComplexToReal:
1255 return "IntegralComplexToReal";
1256 case CK_IntegralComplexToBoolean:
1257 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001258 case CK_IntegralComplexCast:
1259 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001260 case CK_IntegralComplexToFloatingComplex:
1261 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001262 case CK_ARCConsumeObject:
1263 return "ARCConsumeObject";
1264 case CK_ARCProduceObject:
1265 return "ARCProduceObject";
1266 case CK_ARCReclaimReturnedObject:
1267 return "ARCReclaimReturnedObject";
1268 case CK_ARCExtendBlockObject:
1269 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001270 case CK_AtomicToNonAtomic:
1271 return "AtomicToNonAtomic";
1272 case CK_NonAtomicToAtomic:
1273 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001274 case CK_CopyAndAutoreleaseBlockObject:
1275 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001276 }
Mike Stump1eb44332009-09-09 15:08:12 +00001277
John McCall2bb5d002010-11-13 09:02:35 +00001278 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001279}
1280
Douglas Gregor6eef5192009-12-14 19:27:10 +00001281Expr *CastExpr::getSubExprAsWritten() {
1282 Expr *SubExpr = 0;
1283 CastExpr *E = this;
1284 do {
1285 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001286
1287 // Skip through reference binding to temporary.
1288 if (MaterializeTemporaryExpr *Materialize
1289 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1290 SubExpr = Materialize->GetTemporaryExpr();
1291
Douglas Gregor6eef5192009-12-14 19:27:10 +00001292 // Skip any temporary bindings; they're implicit.
1293 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1294 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001295
Douglas Gregor6eef5192009-12-14 19:27:10 +00001296 // Conversions by constructor and conversion functions have a
1297 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001298 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001299 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001300 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001301 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001302
Douglas Gregor6eef5192009-12-14 19:27:10 +00001303 // If the subexpression we're left with is an implicit cast, look
1304 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001305 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1306
Douglas Gregor6eef5192009-12-14 19:27:10 +00001307 return SubExpr;
1308}
1309
John McCallf871d0c2010-08-07 06:22:56 +00001310CXXBaseSpecifier **CastExpr::path_buffer() {
1311 switch (getStmtClass()) {
1312#define ABSTRACT_STMT(x)
1313#define CASTEXPR(Type, Base) \
1314 case Stmt::Type##Class: \
1315 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1316#define STMT(Type, Base)
1317#include "clang/AST/StmtNodes.inc"
1318 default:
1319 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001320 }
1321}
1322
1323void CastExpr::setCastPath(const CXXCastPath &Path) {
1324 assert(Path.size() == path_size());
1325 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1326}
1327
1328ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1329 CastKind Kind, Expr *Operand,
1330 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001331 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001332 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1333 void *Buffer =
1334 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1335 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001336 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001337 if (PathSize) E->setCastPath(*BasePath);
1338 return E;
1339}
1340
1341ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1342 unsigned PathSize) {
1343 void *Buffer =
1344 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1345 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1346}
1347
1348
1349CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001350 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001351 const CXXCastPath *BasePath,
1352 TypeSourceInfo *WrittenTy,
1353 SourceLocation L, SourceLocation R) {
1354 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1355 void *Buffer =
1356 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1357 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001358 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001359 if (PathSize) E->setCastPath(*BasePath);
1360 return E;
1361}
1362
1363CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1364 void *Buffer =
1365 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1366 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1367}
1368
Reid Spencer5f016e22007-07-11 17:01:13 +00001369/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1370/// corresponds to, e.g. "<<=".
1371const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1372 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001373 case BO_PtrMemD: return ".*";
1374 case BO_PtrMemI: return "->*";
1375 case BO_Mul: return "*";
1376 case BO_Div: return "/";
1377 case BO_Rem: return "%";
1378 case BO_Add: return "+";
1379 case BO_Sub: return "-";
1380 case BO_Shl: return "<<";
1381 case BO_Shr: return ">>";
1382 case BO_LT: return "<";
1383 case BO_GT: return ">";
1384 case BO_LE: return "<=";
1385 case BO_GE: return ">=";
1386 case BO_EQ: return "==";
1387 case BO_NE: return "!=";
1388 case BO_And: return "&";
1389 case BO_Xor: return "^";
1390 case BO_Or: return "|";
1391 case BO_LAnd: return "&&";
1392 case BO_LOr: return "||";
1393 case BO_Assign: return "=";
1394 case BO_MulAssign: return "*=";
1395 case BO_DivAssign: return "/=";
1396 case BO_RemAssign: return "%=";
1397 case BO_AddAssign: return "+=";
1398 case BO_SubAssign: return "-=";
1399 case BO_ShlAssign: return "<<=";
1400 case BO_ShrAssign: return ">>=";
1401 case BO_AndAssign: return "&=";
1402 case BO_XorAssign: return "^=";
1403 case BO_OrAssign: return "|=";
1404 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001406
David Blaikie30263482012-01-20 21:50:17 +00001407 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001408}
1409
John McCall2de56d12010-08-25 11:45:40 +00001410BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001411BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1412 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001413 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001414 case OO_Plus: return BO_Add;
1415 case OO_Minus: return BO_Sub;
1416 case OO_Star: return BO_Mul;
1417 case OO_Slash: return BO_Div;
1418 case OO_Percent: return BO_Rem;
1419 case OO_Caret: return BO_Xor;
1420 case OO_Amp: return BO_And;
1421 case OO_Pipe: return BO_Or;
1422 case OO_Equal: return BO_Assign;
1423 case OO_Less: return BO_LT;
1424 case OO_Greater: return BO_GT;
1425 case OO_PlusEqual: return BO_AddAssign;
1426 case OO_MinusEqual: return BO_SubAssign;
1427 case OO_StarEqual: return BO_MulAssign;
1428 case OO_SlashEqual: return BO_DivAssign;
1429 case OO_PercentEqual: return BO_RemAssign;
1430 case OO_CaretEqual: return BO_XorAssign;
1431 case OO_AmpEqual: return BO_AndAssign;
1432 case OO_PipeEqual: return BO_OrAssign;
1433 case OO_LessLess: return BO_Shl;
1434 case OO_GreaterGreater: return BO_Shr;
1435 case OO_LessLessEqual: return BO_ShlAssign;
1436 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1437 case OO_EqualEqual: return BO_EQ;
1438 case OO_ExclaimEqual: return BO_NE;
1439 case OO_LessEqual: return BO_LE;
1440 case OO_GreaterEqual: return BO_GE;
1441 case OO_AmpAmp: return BO_LAnd;
1442 case OO_PipePipe: return BO_LOr;
1443 case OO_Comma: return BO_Comma;
1444 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001445 }
1446}
1447
1448OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1449 static const OverloadedOperatorKind OverOps[] = {
1450 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1451 OO_Star, OO_Slash, OO_Percent,
1452 OO_Plus, OO_Minus,
1453 OO_LessLess, OO_GreaterGreater,
1454 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1455 OO_EqualEqual, OO_ExclaimEqual,
1456 OO_Amp,
1457 OO_Caret,
1458 OO_Pipe,
1459 OO_AmpAmp,
1460 OO_PipePipe,
1461 OO_Equal, OO_StarEqual,
1462 OO_SlashEqual, OO_PercentEqual,
1463 OO_PlusEqual, OO_MinusEqual,
1464 OO_LessLessEqual, OO_GreaterGreaterEqual,
1465 OO_AmpEqual, OO_CaretEqual,
1466 OO_PipeEqual,
1467 OO_Comma
1468 };
1469 return OverOps[Opc];
1470}
1471
Ted Kremenek709210f2010-04-13 23:39:13 +00001472InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001473 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001474 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001475 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001476 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001477 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001478 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1479{
1480 sawArrayRangeDesignator(false);
1481 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001482 for (unsigned I = 0; I != numInits; ++I) {
1483 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001484 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001485 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001486 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001487 if (initExprs[I]->isInstantiationDependent())
1488 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001489 if (initExprs[I]->containsUnexpandedParameterPack())
1490 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001491 }
Sean Huntc3021132010-05-05 15:23:54 +00001492
Ted Kremenek709210f2010-04-13 23:39:13 +00001493 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001494}
Reid Spencer5f016e22007-07-11 17:01:13 +00001495
Ted Kremenek709210f2010-04-13 23:39:13 +00001496void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001497 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001498 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001499}
1500
Ted Kremenek709210f2010-04-13 23:39:13 +00001501void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001502 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001503}
1504
Ted Kremenek709210f2010-04-13 23:39:13 +00001505Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001506 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001507 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001508 InitExprs.back() = expr;
1509 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Douglas Gregor4c678342009-01-28 21:54:33 +00001512 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1513 InitExprs[Init] = expr;
1514 return Result;
1515}
1516
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001517void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001518 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001519 ArrayFillerOrUnionFieldInit = filler;
1520 // Fill out any "holes" in the array due to designated initializers.
1521 Expr **inits = getInits();
1522 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1523 if (inits[i] == 0)
1524 inits[i] = filler;
1525}
1526
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001527SourceRange InitListExpr::getSourceRange() const {
1528 if (SyntacticForm)
1529 return SyntacticForm->getSourceRange();
1530 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1531 if (Beg.isInvalid()) {
1532 // Find the first non-null initializer.
1533 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1534 E = InitExprs.end();
1535 I != E; ++I) {
1536 if (Stmt *S = *I) {
1537 Beg = S->getLocStart();
1538 break;
1539 }
1540 }
1541 }
1542 if (End.isInvalid()) {
1543 // Find the first non-null initializer from the end.
1544 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1545 E = InitExprs.rend();
1546 I != E; ++I) {
1547 if (Stmt *S = *I) {
1548 End = S->getSourceRange().getEnd();
1549 break;
1550 }
1551 }
1552 }
1553 return SourceRange(Beg, End);
1554}
1555
Steve Naroffbfdcae62008-09-04 15:31:07 +00001556/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001557///
John McCalla345edb2012-02-17 03:32:35 +00001558const FunctionProtoType *BlockExpr::getFunctionType() const {
1559 // The block pointer is never sugared, but the function type might be.
1560 return cast<BlockPointerType>(getType())
1561 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001562}
1563
Mike Stump1eb44332009-09-09 15:08:12 +00001564SourceLocation BlockExpr::getCaretLocation() const {
1565 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001566}
Mike Stump1eb44332009-09-09 15:08:12 +00001567const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001568 return TheBlock->getBody();
1569}
Mike Stump1eb44332009-09-09 15:08:12 +00001570Stmt *BlockExpr::getBody() {
1571 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001572}
Steve Naroff56ee6892008-10-08 17:01:13 +00001573
1574
Reid Spencer5f016e22007-07-11 17:01:13 +00001575//===----------------------------------------------------------------------===//
1576// Generic Expression Routines
1577//===----------------------------------------------------------------------===//
1578
Chris Lattner026dc962009-02-14 07:37:35 +00001579/// isUnusedResultAWarning - Return true if this immediate expression should
1580/// be warned about if the result is unused. If so, fill in Loc and Ranges
1581/// with location to warn on and the source range[s] to report with the
1582/// warning.
1583bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001584 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001585 // Don't warn if the expr is type dependent. The type could end up
1586 // instantiating to void.
1587 if (isTypeDependent())
1588 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 switch (getStmtClass()) {
1591 default:
John McCall0faede62010-03-12 07:11:26 +00001592 if (getType()->isVoidType())
1593 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001594 Loc = getExprLoc();
1595 R1 = getSourceRange();
1596 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001598 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001599 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001600 case GenericSelectionExprClass:
1601 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1602 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 case UnaryOperatorClass: {
1604 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001607 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001608 case UO_PostInc:
1609 case UO_PostDec:
1610 case UO_PreInc:
1611 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001612 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001613 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001615 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001616 return false;
1617 break;
John McCall2de56d12010-08-25 11:45:40 +00001618 case UO_Real:
1619 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001621 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1622 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001623 return false;
1624 break;
John McCall2de56d12010-08-25 11:45:40 +00001625 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001626 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 }
Chris Lattner026dc962009-02-14 07:37:35 +00001628 Loc = UO->getOperatorLoc();
1629 R1 = UO->getSubExpr()->getSourceRange();
1630 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001632 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001633 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001634 switch (BO->getOpcode()) {
1635 default:
1636 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001637 // Consider the RHS of comma for side effects. LHS was checked by
1638 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001639 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001640 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1641 // lvalue-ness) of an assignment written in a macro.
1642 if (IntegerLiteral *IE =
1643 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1644 if (IE->getValue() == 0)
1645 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001646 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1647 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001648 case BO_LAnd:
1649 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001650 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1651 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1652 return false;
1653 break;
John McCallbf0ee352010-02-16 04:10:53 +00001654 }
Chris Lattner026dc962009-02-14 07:37:35 +00001655 if (BO->isAssignmentOp())
1656 return false;
1657 Loc = BO->getOperatorLoc();
1658 R1 = BO->getLHS()->getSourceRange();
1659 R2 = BO->getRHS()->getSourceRange();
1660 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001661 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001662 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001663 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001664 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001665 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001666
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001667 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001668 // If only one of the LHS or RHS is a warning, the operator might
1669 // be being used for control flow. Only warn if both the LHS and
1670 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001671 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001672 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1673 return false;
1674 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001675 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001676 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001677 }
1678
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001680 // If the base pointer or element is to a volatile pointer/field, accessing
1681 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001682 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001683 return false;
1684 Loc = cast<MemberExpr>(this)->getMemberLoc();
1685 R1 = SourceRange(Loc, Loc);
1686 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1687 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 case ArraySubscriptExprClass:
1690 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001691 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001692 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001693 return false;
1694 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1695 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1696 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1697 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001698
Chandler Carruth9b106832011-08-17 09:49:44 +00001699 case CXXOperatorCallExprClass: {
1700 // We warn about operator== and operator!= even when user-defined operator
1701 // overloads as there is no reasonable way to define these such that they
1702 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1703 // warning: these operators are commonly typo'ed, and so warning on them
1704 // provides additional value as well. If this list is updated,
1705 // DiagnoseUnusedComparison should be as well.
1706 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1707 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001708 Op->getOperator() == OO_ExclaimEqual) {
1709 Loc = Op->getOperatorLoc();
1710 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001711 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001712 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001713
1714 // Fallthrough for generic call handling.
1715 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001717 case CXXMemberCallExprClass:
1718 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001719 // If this is a direct call, get the callee.
1720 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001721 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001722 // If the callee has attribute pure, const, or warn_unused_result, warn
1723 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001724 //
1725 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1726 // updated to match for QoI.
1727 if (FD->getAttr<WarnUnusedResultAttr>() ||
1728 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1729 Loc = CE->getCallee()->getLocStart();
1730 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001732 if (unsigned NumArgs = CE->getNumArgs())
1733 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1734 CE->getArg(NumArgs-1)->getLocEnd());
1735 return true;
1736 }
Chris Lattner026dc962009-02-14 07:37:35 +00001737 }
1738 return false;
1739 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001740
1741 case CXXTemporaryObjectExprClass:
1742 case CXXConstructExprClass:
1743 return false;
1744
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001745 case ObjCMessageExprClass: {
1746 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001747 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1748 ME->isInstanceMessage() &&
1749 !ME->getType()->isVoidType() &&
1750 ME->getSelector().getIdentifierInfoForSlot(0) &&
1751 ME->getSelector().getIdentifierInfoForSlot(0)
1752 ->getName().startswith("init")) {
1753 Loc = getExprLoc();
1754 R1 = ME->getSourceRange();
1755 return true;
1756 }
1757
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001758 const ObjCMethodDecl *MD = ME->getMethodDecl();
1759 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1760 Loc = getExprLoc();
1761 return true;
1762 }
Chris Lattner026dc962009-02-14 07:37:35 +00001763 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
John McCall12f78a62010-12-02 01:19:52 +00001766 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001767 Loc = getExprLoc();
1768 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001769 return true;
John McCall12f78a62010-12-02 01:19:52 +00001770
John McCall4b9c2d22011-11-06 09:01:30 +00001771 case PseudoObjectExprClass: {
1772 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1773
1774 // Only complain about things that have the form of a getter.
1775 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1776 isa<BinaryOperator>(PO->getSyntacticForm()))
1777 return false;
1778
1779 Loc = getExprLoc();
1780 R1 = getSourceRange();
1781 return true;
1782 }
1783
Chris Lattner611b2ec2008-07-26 19:51:01 +00001784 case StmtExprClass: {
1785 // Statement exprs don't logically have side effects themselves, but are
1786 // sometimes used in macros in ways that give them a type that is unused.
1787 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1788 // however, if the result of the stmt expr is dead, we don't want to emit a
1789 // warning.
1790 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001791 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001792 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001793 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001794 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1795 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1796 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1797 }
Mike Stump1eb44332009-09-09 15:08:12 +00001798
John McCall0faede62010-03-12 07:11:26 +00001799 if (getType()->isVoidType())
1800 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001801 Loc = cast<StmtExpr>(this)->getLParenLoc();
1802 R1 = getSourceRange();
1803 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001804 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001805 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001806 // If this is an explicit cast to void, allow it. People do this when they
1807 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001808 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001809 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001810 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1811 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1812 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001813 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001814 if (getType()->isVoidType())
1815 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001816 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001817
Anders Carlsson58beed92009-11-17 17:11:23 +00001818 // If this is a cast to void or a constructor conversion, check the operand.
1819 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001820 if (CE->getCastKind() == CK_ToVoid ||
1821 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001822 return (cast<CastExpr>(this)->getSubExpr()
1823 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001824 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1825 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1826 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001827 }
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Eli Friedman4be1f472008-05-19 21:24:43 +00001829 case ImplicitCastExprClass:
1830 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001831 return (cast<ImplicitCastExpr>(this)
1832 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001833
Chris Lattner04421082008-04-08 04:40:51 +00001834 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001835 return (cast<CXXDefaultArgExpr>(this)
1836 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001837
1838 case CXXNewExprClass:
1839 // FIXME: In theory, there might be new expressions that don't have side
1840 // effects (e.g. a placement new with an uninitialized POD).
1841 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001842 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001843 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001844 return (cast<CXXBindTemporaryExpr>(this)
1845 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001846 case ExprWithCleanupsClass:
1847 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001848 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001849 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001850}
1851
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001852/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001853/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001854bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001855 const Expr *E = IgnoreParens();
1856 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001857 default:
1858 return false;
1859 case ObjCIvarRefExprClass:
1860 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001861 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001862 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001863 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001864 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001865 case MaterializeTemporaryExprClass:
1866 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1867 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001868 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001869 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001870 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001871 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001872
1873 const Decl *D;
1874 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1875 D = BDRE->getDecl();
1876 else
1877 D = cast<DeclRefExpr>(E)->getDecl();
1878
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001879 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1880 if (VD->hasGlobalStorage())
1881 return true;
1882 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001883 // dereferencing to a pointer is always a gc'able candidate,
1884 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001885 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001886 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001887 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001888 return false;
1889 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001890 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001891 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001892 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001893 }
1894 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001895 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001896 }
1897}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001898
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001899bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1900 if (isTypeDependent())
1901 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001902 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001903}
1904
John McCall864c0412011-04-26 20:42:42 +00001905QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001906 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001907
1908 // Bound member expressions are always one of these possibilities:
1909 // x->m x.m x->*y x.*y
1910 // (possibly parenthesized)
1911
1912 expr = expr->IgnoreParens();
1913 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1914 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1915 return mem->getMemberDecl()->getType();
1916 }
1917
1918 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1919 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1920 ->getPointeeType();
1921 assert(type->isFunctionType());
1922 return type;
1923 }
1924
1925 assert(isa<UnresolvedMemberExpr>(expr));
1926 return QualType();
1927}
1928
Sebastian Redl369e51f2010-09-10 20:55:33 +00001929static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1930 Expr::CanThrowResult CT2) {
1931 // CanThrowResult constants are ordered so that the maximum is the correct
1932 // merge result.
1933 return CT1 > CT2 ? CT1 : CT2;
1934}
1935
1936static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1937 Expr *E = const_cast<Expr*>(CE);
1938 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001939 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001940 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1941 }
1942 return R;
1943}
1944
Richard Smith7a614d82011-06-11 17:19:42 +00001945static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1946 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001947 bool NullThrows = true) {
1948 if (!D)
1949 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1950
1951 // See if we can get a function type from the decl somehow.
1952 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1953 if (!VD) // If we have no clue what we're calling, assume the worst.
1954 return Expr::CT_Can;
1955
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001956 // As an extension, we assume that __attribute__((nothrow)) functions don't
1957 // throw.
1958 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1959 return Expr::CT_Cannot;
1960
Sebastian Redl369e51f2010-09-10 20:55:33 +00001961 QualType T = VD->getType();
1962 const FunctionProtoType *FT;
1963 if ((FT = T->getAs<FunctionProtoType>())) {
1964 } else if (const PointerType *PT = T->getAs<PointerType>())
1965 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1966 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1967 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1968 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1969 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1970 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1971 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1972
1973 if (!FT)
1974 return Expr::CT_Can;
1975
Richard Smith7a614d82011-06-11 17:19:42 +00001976 if (FT->getExceptionSpecType() == EST_Delayed) {
1977 assert(isa<CXXConstructorDecl>(D) &&
1978 "only constructor exception specs can be unknown");
1979 Ctx.getDiagnostics().Report(E->getLocStart(),
1980 diag::err_exception_spec_unknown)
1981 << E->getSourceRange();
1982 return Expr::CT_Can;
1983 }
1984
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001985 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001986}
1987
1988static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1989 if (DC->isTypeDependent())
1990 return Expr::CT_Dependent;
1991
Sebastian Redl295995c2010-09-10 20:55:47 +00001992 if (!DC->getTypeAsWritten()->isReferenceType())
1993 return Expr::CT_Cannot;
1994
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001995 if (DC->getSubExpr()->isTypeDependent())
1996 return Expr::CT_Dependent;
1997
Sebastian Redl369e51f2010-09-10 20:55:33 +00001998 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1999}
2000
2001static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
2002 const CXXTypeidExpr *DC) {
2003 if (DC->isTypeOperand())
2004 return Expr::CT_Cannot;
2005
2006 Expr *Op = DC->getExprOperand();
2007 if (Op->isTypeDependent())
2008 return Expr::CT_Dependent;
2009
2010 const RecordType *RT = Op->getType()->getAs<RecordType>();
2011 if (!RT)
2012 return Expr::CT_Cannot;
2013
2014 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
2015 return Expr::CT_Cannot;
2016
2017 if (Op->Classify(C).isPRValue())
2018 return Expr::CT_Cannot;
2019
2020 return Expr::CT_Can;
2021}
2022
2023Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
2024 // C++ [expr.unary.noexcept]p3:
2025 // [Can throw] if in a potentially-evaluated context the expression would
2026 // contain:
2027 switch (getStmtClass()) {
2028 case CXXThrowExprClass:
2029 // - a potentially evaluated throw-expression
2030 return CT_Can;
2031
2032 case CXXDynamicCastExprClass: {
2033 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
2034 // where T is a reference type, that requires a run-time check
2035 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2036 if (CT == CT_Can)
2037 return CT;
2038 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2039 }
2040
2041 case CXXTypeidExprClass:
2042 // - a potentially evaluated typeid expression applied to a glvalue
2043 // expression whose type is a polymorphic class type
2044 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2045
2046 // - a potentially evaluated call to a function, member function, function
2047 // pointer, or member function pointer that does not have a non-throwing
2048 // exception-specification
2049 case CallExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002050 case CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002051 case CXXOperatorCallExprClass:
2052 case UserDefinedLiteralClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002053 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002054 CanThrowResult CT;
2055 if (isTypeDependent())
2056 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002057 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2058 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002059 else
Richard Smith7a614d82011-06-11 17:19:42 +00002060 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002061 if (CT == CT_Can)
2062 return CT;
2063 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2064 }
2065
Sebastian Redl295995c2010-09-10 20:55:47 +00002066 case CXXConstructExprClass:
2067 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002068 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002069 cast<CXXConstructExpr>(this)->getConstructor());
2070 if (CT == CT_Can)
2071 return CT;
2072 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2073 }
2074
Douglas Gregor01d08012012-02-07 10:09:13 +00002075 case LambdaExprClass: {
2076 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2077 CanThrowResult CT = Expr::CT_Cannot;
2078 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2079 CapEnd = Lambda->capture_init_end();
2080 Cap != CapEnd; ++Cap)
2081 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2082 return CT;
2083 }
2084
Sebastian Redl369e51f2010-09-10 20:55:33 +00002085 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002086 CanThrowResult CT;
2087 if (isTypeDependent())
2088 CT = CT_Dependent;
2089 else
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002090 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002091 if (CT == CT_Can)
2092 return CT;
2093 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2094 }
2095
2096 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002097 CanThrowResult CT;
2098 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2099 if (DTy.isNull() || DTy->isDependentType()) {
2100 CT = CT_Dependent;
2101 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002102 CT = CanCalleeThrow(C, this,
2103 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002104 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2105 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002106 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002107 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002108 if (CT == CT_Can)
2109 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002110 }
2111 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2112 }
2113
2114 case CXXBindTemporaryExprClass: {
2115 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002116 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002117 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2118 if (CT == CT_Can)
2119 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002120 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2121 }
2122
2123 // ObjC message sends are like function calls, but never have exception
2124 // specs.
2125 case ObjCMessageExprClass:
2126 case ObjCPropertyRefExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002127 case ObjCSubscriptRefExprClass:
2128 return CT_Can;
2129
2130 // All the ObjC literals that are implemented as calls are
2131 // potentially throwing unless we decide to close off that
2132 // possibility.
2133 case ObjCArrayLiteralClass:
2134 case ObjCBoolLiteralExprClass:
2135 case ObjCDictionaryLiteralClass:
2136 case ObjCNumericLiteralClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002137 return CT_Can;
2138
2139 // Many other things have subexpressions, so we have to test those.
2140 // Some are simple:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002141 case ConditionalOperatorClass:
2142 case CompoundLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002143 case CXXConstCastExprClass:
2144 case CXXDefaultArgExprClass:
2145 case CXXReinterpretCastExprClass:
2146 case DesignatedInitExprClass:
2147 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002148 case ExtVectorElementExprClass:
2149 case InitListExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002150 case MemberExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002151 case ObjCIsaExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002152 case ObjCIvarRefExprClass:
2153 case ParenExprClass:
2154 case ParenListExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002155 case ShuffleVectorExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002156 case VAArgExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002157 return CanSubExprsThrow(C, this);
2158
2159 // Some might be dependent for other reasons.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002160 case ArraySubscriptExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002161 case BinaryOperatorClass:
2162 case CompoundAssignOperatorClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002163 case CStyleCastExprClass:
2164 case CXXStaticCastExprClass:
2165 case CXXFunctionalCastExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002166 case ImplicitCastExprClass:
2167 case MaterializeTemporaryExprClass:
2168 case UnaryOperatorClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002169 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2170 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2171 }
2172
2173 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2174 case StmtExprClass:
2175 return CT_Can;
2176
2177 case ChooseExprClass:
2178 if (isTypeDependent() || isValueDependent())
2179 return CT_Dependent;
2180 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2181
Peter Collingbournef111d932011-04-15 00:35:48 +00002182 case GenericSelectionExprClass:
2183 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2184 return CT_Dependent;
2185 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2186
Sebastian Redl369e51f2010-09-10 20:55:33 +00002187 // Some expressions are always dependent.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002188 case CXXDependentScopeMemberExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002189 case CXXUnresolvedConstructExprClass:
2190 case DependentScopeDeclRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002191 return CT_Dependent;
2192
Eli Friedmanc9674be2012-01-31 01:21:45 +00002193 case AtomicExprClass:
2194 case AsTypeExprClass:
2195 case BinaryConditionalOperatorClass:
2196 case BlockExprClass:
2197 case BlockDeclRefExprClass:
2198 case CUDAKernelCallExprClass:
2199 case DeclRefExprClass:
2200 case ObjCBridgedCastExprClass:
2201 case ObjCIndirectCopyRestoreExprClass:
2202 case ObjCProtocolExprClass:
2203 case ObjCSelectorExprClass:
2204 case OffsetOfExprClass:
2205 case PackExpansionExprClass:
2206 case PseudoObjectExprClass:
2207 case SubstNonTypeTemplateParmExprClass:
2208 case SubstNonTypeTemplateParmPackExprClass:
2209 case UnaryExprOrTypeTraitExprClass:
2210 case UnresolvedLookupExprClass:
2211 case UnresolvedMemberExprClass:
2212 // FIXME: Can any of the above throw? If so, when?
Sebastian Redl369e51f2010-09-10 20:55:33 +00002213 return CT_Cannot;
Eli Friedmanc9674be2012-01-31 01:21:45 +00002214
2215 case AddrLabelExprClass:
2216 case ArrayTypeTraitExprClass:
2217 case BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002218 case TypeTraitExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002219 case CXXBoolLiteralExprClass:
2220 case CXXNoexceptExprClass:
2221 case CXXNullPtrLiteralExprClass:
2222 case CXXPseudoDestructorExprClass:
2223 case CXXScalarValueInitExprClass:
2224 case CXXThisExprClass:
2225 case CXXUuidofExprClass:
2226 case CharacterLiteralClass:
2227 case ExpressionTraitExprClass:
2228 case FloatingLiteralClass:
2229 case GNUNullExprClass:
2230 case ImaginaryLiteralClass:
2231 case ImplicitValueInitExprClass:
2232 case IntegerLiteralClass:
2233 case ObjCEncodeExprClass:
2234 case ObjCStringLiteralClass:
2235 case OpaqueValueExprClass:
2236 case PredefinedExprClass:
2237 case SizeOfPackExprClass:
2238 case StringLiteralClass:
2239 case UnaryTypeTraitExprClass:
2240 // These expressions can never throw.
2241 return CT_Cannot;
2242
2243#define STMT(CLASS, PARENT) case CLASS##Class:
2244#define STMT_RANGE(Base, First, Last)
2245#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2246#define EXPR(CLASS, PARENT)
2247#define ABSTRACT_STMT(STMT)
2248#include "clang/AST/StmtNodes.inc"
2249 case NoStmtClass:
2250 llvm_unreachable("Invalid class for expression");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002251 }
Matt Beaumont-Gay56e68b72012-01-31 18:59:25 +00002252 llvm_unreachable("Bogus StmtClass");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002253}
2254
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002255Expr* Expr::IgnoreParens() {
2256 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002257 while (true) {
2258 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2259 E = P->getSubExpr();
2260 continue;
2261 }
2262 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2263 if (P->getOpcode() == UO_Extension) {
2264 E = P->getSubExpr();
2265 continue;
2266 }
2267 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002268 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2269 if (!P->isResultDependent()) {
2270 E = P->getResultExpr();
2271 continue;
2272 }
2273 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002274 return E;
2275 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002276}
2277
Chris Lattner56f34942008-02-13 01:02:39 +00002278/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2279/// or CastExprs or ImplicitCastExprs, returning their operand.
2280Expr *Expr::IgnoreParenCasts() {
2281 Expr *E = this;
2282 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002283 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002284 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002285 continue;
2286 }
2287 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002288 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002289 continue;
2290 }
2291 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2292 if (P->getOpcode() == UO_Extension) {
2293 E = P->getSubExpr();
2294 continue;
2295 }
2296 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002297 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2298 if (!P->isResultDependent()) {
2299 E = P->getResultExpr();
2300 continue;
2301 }
2302 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002303 if (MaterializeTemporaryExpr *Materialize
2304 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2305 E = Materialize->GetTemporaryExpr();
2306 continue;
2307 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002308 if (SubstNonTypeTemplateParmExpr *NTTP
2309 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2310 E = NTTP->getReplacement();
2311 continue;
2312 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002313 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002314 }
2315}
2316
John McCall9c5d70c2010-12-04 08:24:19 +00002317/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2318/// casts. This is intended purely as a temporary workaround for code
2319/// that hasn't yet been rewritten to do the right thing about those
2320/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002321Expr *Expr::IgnoreParenLValueCasts() {
2322 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002323 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002324 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2325 E = P->getSubExpr();
2326 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002327 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002328 if (P->getCastKind() == CK_LValueToRValue) {
2329 E = P->getSubExpr();
2330 continue;
2331 }
John McCall9c5d70c2010-12-04 08:24:19 +00002332 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2333 if (P->getOpcode() == UO_Extension) {
2334 E = P->getSubExpr();
2335 continue;
2336 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002337 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2338 if (!P->isResultDependent()) {
2339 E = P->getResultExpr();
2340 continue;
2341 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002342 } else if (MaterializeTemporaryExpr *Materialize
2343 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2344 E = Materialize->GetTemporaryExpr();
2345 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002346 } else if (SubstNonTypeTemplateParmExpr *NTTP
2347 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2348 E = NTTP->getReplacement();
2349 continue;
John McCallf6a16482010-12-04 03:47:34 +00002350 }
2351 break;
2352 }
2353 return E;
2354}
2355
John McCall2fc46bf2010-05-05 22:59:52 +00002356Expr *Expr::IgnoreParenImpCasts() {
2357 Expr *E = this;
2358 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002359 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002360 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002361 continue;
2362 }
2363 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002364 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002365 continue;
2366 }
2367 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2368 if (P->getOpcode() == UO_Extension) {
2369 E = P->getSubExpr();
2370 continue;
2371 }
2372 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002373 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2374 if (!P->isResultDependent()) {
2375 E = P->getResultExpr();
2376 continue;
2377 }
2378 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002379 if (MaterializeTemporaryExpr *Materialize
2380 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2381 E = Materialize->GetTemporaryExpr();
2382 continue;
2383 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002384 if (SubstNonTypeTemplateParmExpr *NTTP
2385 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2386 E = NTTP->getReplacement();
2387 continue;
2388 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002389 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002390 }
2391}
2392
Hans Wennborg2f072b42011-06-09 17:06:51 +00002393Expr *Expr::IgnoreConversionOperator() {
2394 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002395 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002396 return MCE->getImplicitObjectArgument();
2397 }
2398 return this;
2399}
2400
Chris Lattnerecdd8412009-03-13 17:28:01 +00002401/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2402/// value (including ptr->int casts of the same size). Strip off any
2403/// ParenExpr or CastExprs, returning their operand.
2404Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2405 Expr *E = this;
2406 while (true) {
2407 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2408 E = P->getSubExpr();
2409 continue;
2410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattnerecdd8412009-03-13 17:28:01 +00002412 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2413 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002414 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002415 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Chris Lattnerecdd8412009-03-13 17:28:01 +00002417 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2418 E = SE;
2419 continue;
2420 }
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002422 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002423 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002424 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002425 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002426 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2427 E = SE;
2428 continue;
2429 }
2430 }
Mike Stump1eb44332009-09-09 15:08:12 +00002431
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002432 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2433 if (P->getOpcode() == UO_Extension) {
2434 E = P->getSubExpr();
2435 continue;
2436 }
2437 }
2438
Peter Collingbournef111d932011-04-15 00:35:48 +00002439 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2440 if (!P->isResultDependent()) {
2441 E = P->getResultExpr();
2442 continue;
2443 }
2444 }
2445
Douglas Gregorc0244c52011-09-08 17:56:33 +00002446 if (SubstNonTypeTemplateParmExpr *NTTP
2447 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2448 E = NTTP->getReplacement();
2449 continue;
2450 }
2451
Chris Lattnerecdd8412009-03-13 17:28:01 +00002452 return E;
2453 }
2454}
2455
Douglas Gregor6eef5192009-12-14 19:27:10 +00002456bool Expr::isDefaultArgument() const {
2457 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002458 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2459 E = M->GetTemporaryExpr();
2460
Douglas Gregor6eef5192009-12-14 19:27:10 +00002461 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2462 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002463
Douglas Gregor6eef5192009-12-14 19:27:10 +00002464 return isa<CXXDefaultArgExpr>(E);
2465}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002466
Douglas Gregor2f599792010-04-02 18:24:57 +00002467/// \brief Skip over any no-op casts and any temporary-binding
2468/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002469static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002470 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2471 E = M->GetTemporaryExpr();
2472
Douglas Gregor2f599792010-04-02 18:24:57 +00002473 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002474 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002475 E = ICE->getSubExpr();
2476 else
2477 break;
2478 }
2479
2480 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2481 E = BE->getSubExpr();
2482
2483 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002484 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002485 E = ICE->getSubExpr();
2486 else
2487 break;
2488 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002489
2490 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002491}
2492
John McCall558d2ab2010-09-15 10:14:12 +00002493/// isTemporaryObject - Determines if this expression produces a
2494/// temporary of the given class type.
2495bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2496 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2497 return false;
2498
Anders Carlssonf8b30152010-11-28 16:40:49 +00002499 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002500
John McCall58277b52010-09-15 20:59:13 +00002501 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002502 if (!E->Classify(C).isPRValue()) {
2503 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002504 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002505 return false;
2506 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002507
John McCall19e60ad2010-09-16 06:57:56 +00002508 // Black-list a few cases which yield pr-values of class type that don't
2509 // refer to temporaries of that type:
2510
2511 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002512 if (isa<ImplicitCastExpr>(E)) {
2513 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2514 case CK_DerivedToBase:
2515 case CK_UncheckedDerivedToBase:
2516 return false;
2517 default:
2518 break;
2519 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002520 }
2521
John McCall19e60ad2010-09-16 06:57:56 +00002522 // - member expressions (all)
2523 if (isa<MemberExpr>(E))
2524 return false;
2525
John McCall56ca35d2011-02-17 10:25:35 +00002526 // - opaque values (all)
2527 if (isa<OpaqueValueExpr>(E))
2528 return false;
2529
John McCall558d2ab2010-09-15 10:14:12 +00002530 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002531}
2532
Douglas Gregor75e85042011-03-02 21:06:53 +00002533bool Expr::isImplicitCXXThis() const {
2534 const Expr *E = this;
2535
2536 // Strip away parentheses and casts we don't care about.
2537 while (true) {
2538 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2539 E = Paren->getSubExpr();
2540 continue;
2541 }
2542
2543 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2544 if (ICE->getCastKind() == CK_NoOp ||
2545 ICE->getCastKind() == CK_LValueToRValue ||
2546 ICE->getCastKind() == CK_DerivedToBase ||
2547 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2548 E = ICE->getSubExpr();
2549 continue;
2550 }
2551 }
2552
2553 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2554 if (UnOp->getOpcode() == UO_Extension) {
2555 E = UnOp->getSubExpr();
2556 continue;
2557 }
2558 }
2559
Douglas Gregor03e80032011-06-21 17:03:29 +00002560 if (const MaterializeTemporaryExpr *M
2561 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2562 E = M->GetTemporaryExpr();
2563 continue;
2564 }
2565
Douglas Gregor75e85042011-03-02 21:06:53 +00002566 break;
2567 }
2568
2569 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2570 return This->isImplicit();
2571
2572 return false;
2573}
2574
Douglas Gregor898574e2008-12-05 23:32:09 +00002575/// hasAnyTypeDependentArguments - Determines if any of the expressions
2576/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002577bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2578 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002579 if (Exprs[I]->isTypeDependent())
2580 return true;
2581
2582 return false;
2583}
2584
John McCall4204f072010-08-02 21:13:48 +00002585bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002586 // This function is attempting whether an expression is an initializer
2587 // which can be evaluated at compile-time. isEvaluatable handles most
2588 // of the cases, but it can't deal with some initializer-specific
2589 // expressions, and it can't deal with aggregates; we deal with those here,
2590 // and fall back to isEvaluatable for the other cases.
2591
John McCall4204f072010-08-02 21:13:48 +00002592 // If we ever capture reference-binding directly in the AST, we can
2593 // kill the second parameter.
2594
2595 if (IsForRef) {
2596 EvalResult Result;
2597 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2598 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002599
Anders Carlssone8a32b82008-11-24 05:23:59 +00002600 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002601 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002602 case IntegerLiteralClass:
2603 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002604 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002605 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002606 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002607 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002608 case CXXTemporaryObjectExprClass:
2609 case CXXConstructExprClass: {
2610 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002611
2612 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002613 if (CE->getConstructor()->isTrivial()) {
2614 // 1) an application of the trivial default constructor or
2615 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002616
Richard Smith180f4792011-11-10 06:34:14 +00002617 // 2) an elidable trivial copy construction of an operand which is
2618 // itself a constant initializer. Note that we consider the
2619 // operand on its own, *not* as a reference binding.
2620 if (CE->isElidable() &&
2621 CE->getArg(0)->isConstantInitializer(Ctx, false))
2622 return true;
2623 }
2624
2625 // 3) a foldable constexpr constructor.
2626 break;
John McCallb4b9b152010-08-01 21:51:45 +00002627 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002628 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002629 // This handles gcc's extension that allows global initializers like
2630 // "struct x {int x;} x = (struct x) {};".
2631 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002632 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002633 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002634 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002635 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002636 // FIXME: This doesn't deal with fields with reference types correctly.
2637 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2638 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002639 const InitListExpr *Exp = cast<InitListExpr>(this);
2640 unsigned numInits = Exp->getNumInits();
2641 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002642 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002643 return false;
2644 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002645 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002646 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002647 case ImplicitValueInitExprClass:
2648 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002649 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002650 return cast<ParenExpr>(this)->getSubExpr()
2651 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002652 case GenericSelectionExprClass:
2653 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2654 return false;
2655 return cast<GenericSelectionExpr>(this)->getResultExpr()
2656 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002657 case ChooseExprClass:
2658 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2659 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002660 case UnaryOperatorClass: {
2661 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002662 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002663 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002664 break;
2665 }
John McCall4204f072010-08-02 21:13:48 +00002666 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002667 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002668 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002669 case CStyleCastExprClass: {
2670 const CastExpr *CE = cast<CastExpr>(this);
2671
David Chisnall7a7ee302012-01-16 17:27:18 +00002672 // If we're promoting an integer to an _Atomic type then this is constant
2673 // if the integer is constant. We also need to check the converse in case
2674 // someone does something like:
2675 //
2676 // int a = (_Atomic(int))42;
2677 //
2678 // I doubt anyone would write code like this directly, but it's quite
2679 // possible as the result of macro expansions.
2680 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2681 CE->getCastKind() == CK_AtomicToNonAtomic)
2682 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2683
Richard Smithd62ca372011-12-06 22:44:34 +00002684 // Handle bitcasts of vector constants.
2685 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2686 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2687
Eli Friedman6bd97192011-12-21 00:43:02 +00002688 // Handle misc casts we want to ignore.
2689 // FIXME: Is it really safe to ignore all these?
2690 if (CE->getCastKind() == CK_NoOp ||
2691 CE->getCastKind() == CK_LValueToRValue ||
2692 CE->getCastKind() == CK_ToUnion ||
2693 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002694 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2695
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002696 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002697 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002698 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002699 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002700 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002701 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002702 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002703}
2704
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002705namespace {
2706 /// \brief Look for a call to a non-trivial function within an expression.
2707 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2708 {
2709 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2710
2711 bool NonTrivial;
2712
2713 public:
2714 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002715 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002716
2717 bool hasNonTrivialCall() const { return NonTrivial; }
2718
2719 void VisitCallExpr(CallExpr *E) {
2720 if (CXXMethodDecl *Method
2721 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2722 if (Method->isTrivial()) {
2723 // Recurse to children of the call.
2724 Inherited::VisitStmt(E);
2725 return;
2726 }
2727 }
2728
2729 NonTrivial = true;
2730 }
2731
2732 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2733 if (E->getConstructor()->isTrivial()) {
2734 // Recurse to children of the call.
2735 Inherited::VisitStmt(E);
2736 return;
2737 }
2738
2739 NonTrivial = true;
2740 }
2741
2742 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2743 if (E->getTemporary()->getDestructor()->isTrivial()) {
2744 Inherited::VisitStmt(E);
2745 return;
2746 }
2747
2748 NonTrivial = true;
2749 }
2750 };
2751}
2752
2753bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2754 NonTrivialCallFinder Finder(Ctx);
2755 Finder.Visit(this);
2756 return Finder.hasNonTrivialCall();
2757}
2758
Chandler Carruth82214a82011-02-18 23:54:50 +00002759/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2760/// pointer constant or not, as well as the specific kind of constant detected.
2761/// Null pointer constants can be integer constant expressions with the
2762/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2763/// (a GNU extension).
2764Expr::NullPointerConstantKind
2765Expr::isNullPointerConstant(ASTContext &Ctx,
2766 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002767 if (isValueDependent()) {
2768 switch (NPC) {
2769 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002770 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002771 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002772 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2773 return NPCK_ZeroInteger;
2774 else
2775 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002776
Douglas Gregorce940492009-09-25 04:25:58 +00002777 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002778 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002779 }
2780 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002781
Sebastian Redl07779722008-10-31 14:43:28 +00002782 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002783 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002784 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002785 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002786 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002787 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002788 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002789 Pointee->isVoidType() && // to void*
2790 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002791 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002792 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002793 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002794 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2795 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002796 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002797 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2798 // Accept ((void*)0) as a null pointer constant, as many other
2799 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002800 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002801 } else if (const GenericSelectionExpr *GE =
2802 dyn_cast<GenericSelectionExpr>(this)) {
2803 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002804 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002805 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002806 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002807 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002808 } else if (isa<GNUNullExpr>(this)) {
2809 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002810 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002811 } else if (const MaterializeTemporaryExpr *M
2812 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2813 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002814 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2815 if (const Expr *Source = OVE->getSourceExpr())
2816 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002817 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002818
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002819 // C++0x nullptr_t is always a null pointer constant.
2820 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002821 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002822
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002823 if (const RecordType *UT = getType()->getAsUnionType())
2824 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2825 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2826 const Expr *InitExpr = CLE->getInitializer();
2827 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2828 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2829 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002830 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002831 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002832 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002833 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Reid Spencer5f016e22007-07-11 17:01:13 +00002835 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002836 // test for the value 0. Don't use the C++11 constant expression semantics
2837 // for this, for now; once the dust settles on core issue 903, we might only
2838 // allow a literal 0 here in C++11 mode.
2839 if (Ctx.getLangOptions().CPlusPlus0x) {
2840 if (!isCXX98IntegralConstantExpr(Ctx))
2841 return NPCK_NotNull;
2842 } else {
2843 if (!isIntegerConstantExpr(Ctx))
2844 return NPCK_NotNull;
2845 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002846
Richard Smith70488e22012-02-14 21:38:30 +00002847 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002848}
Steve Naroff31a45842007-07-28 23:10:27 +00002849
John McCallf6a16482010-12-04 03:47:34 +00002850/// \brief If this expression is an l-value for an Objective C
2851/// property, find the underlying property reference expression.
2852const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2853 const Expr *E = this;
2854 while (true) {
2855 assert((E->getValueKind() == VK_LValue &&
2856 E->getObjectKind() == OK_ObjCProperty) &&
2857 "expression is not a property reference");
2858 E = E->IgnoreParenCasts();
2859 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2860 if (BO->getOpcode() == BO_Comma) {
2861 E = BO->getRHS();
2862 continue;
2863 }
2864 }
2865
2866 break;
2867 }
2868
2869 return cast<ObjCPropertyRefExpr>(E);
2870}
2871
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002872FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002873 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002874
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002875 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002876 if (ICE->getCastKind() == CK_LValueToRValue ||
2877 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002878 E = ICE->getSubExpr()->IgnoreParens();
2879 else
2880 break;
2881 }
2882
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002883 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002884 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002885 if (Field->isBitField())
2886 return Field;
2887
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002888 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2889 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2890 if (Field->isBitField())
2891 return Field;
2892
Eli Friedman42068e92011-07-13 02:05:57 +00002893 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002894 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2895 return BinOp->getLHS()->getBitField();
2896
Eli Friedman42068e92011-07-13 02:05:57 +00002897 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2898 return BinOp->getRHS()->getBitField();
2899 }
2900
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002901 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002902}
2903
Anders Carlsson09380262010-01-31 17:18:49 +00002904bool Expr::refersToVectorElement() const {
2905 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002906
Anders Carlsson09380262010-01-31 17:18:49 +00002907 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002908 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002909 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002910 E = ICE->getSubExpr()->IgnoreParens();
2911 else
2912 break;
2913 }
Sean Huntc3021132010-05-05 15:23:54 +00002914
Anders Carlsson09380262010-01-31 17:18:49 +00002915 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2916 return ASE->getBase()->getType()->isVectorType();
2917
2918 if (isa<ExtVectorElementExpr>(E))
2919 return true;
2920
2921 return false;
2922}
2923
Chris Lattner2140e902009-02-16 22:14:05 +00002924/// isArrow - Return true if the base expression is a pointer to vector,
2925/// return false if the base expression is a vector.
2926bool ExtVectorElementExpr::isArrow() const {
2927 return getBase()->getType()->isPointerType();
2928}
2929
Nate Begeman213541a2008-04-18 23:10:10 +00002930unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002931 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002932 return VT->getNumElements();
2933 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002934}
2935
Nate Begeman8a997642008-05-09 06:41:27 +00002936/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002937bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002938 // FIXME: Refactor this code to an accessor on the AST node which returns the
2939 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002940 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002941
2942 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002943 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002944 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Nate Begeman190d6a22009-01-18 02:01:21 +00002946 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002947 if (Comp[0] == 's' || Comp[0] == 'S')
2948 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Daniel Dunbar15027422009-10-17 23:53:04 +00002950 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002951 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002952 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002953
Steve Narofffec0b492007-07-30 03:29:09 +00002954 return false;
2955}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002956
Nate Begeman8a997642008-05-09 06:41:27 +00002957/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002958void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002959 SmallVectorImpl<unsigned> &Elts) const {
2960 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002961 if (Comp[0] == 's' || Comp[0] == 'S')
2962 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002964 bool isHi = Comp == "hi";
2965 bool isLo = Comp == "lo";
2966 bool isEven = Comp == "even";
2967 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002968
Nate Begeman8a997642008-05-09 06:41:27 +00002969 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2970 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002971
Nate Begeman8a997642008-05-09 06:41:27 +00002972 if (isHi)
2973 Index = e + i;
2974 else if (isLo)
2975 Index = i;
2976 else if (isEven)
2977 Index = 2 * i;
2978 else if (isOdd)
2979 Index = 2 * i + 1;
2980 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002981 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002982
Nate Begeman3b8d1162008-05-13 21:03:02 +00002983 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002984 }
Nate Begeman8a997642008-05-09 06:41:27 +00002985}
2986
Douglas Gregor04badcf2010-04-21 00:45:42 +00002987ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002988 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002989 SourceLocation LBracLoc,
2990 SourceLocation SuperLoc,
2991 bool IsInstanceSuper,
2992 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002993 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002994 ArrayRef<SourceLocation> SelLocs,
2995 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002996 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002997 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002998 SourceLocation RBracLoc,
2999 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003000 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003001 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003002 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003003 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003004 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3005 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003006 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003007 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3008 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003009{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003010 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003011 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003012}
3013
Douglas Gregor04badcf2010-04-21 00:45:42 +00003014ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003015 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003016 SourceLocation LBracLoc,
3017 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003018 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003019 ArrayRef<SourceLocation> SelLocs,
3020 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003021 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003022 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003023 SourceLocation RBracLoc,
3024 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003025 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003026 T->isDependentType(), T->isInstantiationDependentType(),
3027 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003028 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3029 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003030 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003031 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003032 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003033{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003034 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003035 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003036}
3037
Douglas Gregor04badcf2010-04-21 00:45:42 +00003038ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003039 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003040 SourceLocation LBracLoc,
3041 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003042 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003043 ArrayRef<SourceLocation> SelLocs,
3044 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003045 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003046 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003047 SourceLocation RBracLoc,
3048 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003049 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003050 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003051 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003052 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003053 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3054 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003055 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003056 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003057 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003058{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003059 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003060 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003061}
3062
3063void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3064 ArrayRef<SourceLocation> SelLocs,
3065 SelectorLocationsKind SelLocsK) {
3066 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003067 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003068 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003069 if (Args[I]->isTypeDependent())
3070 ExprBits.TypeDependent = true;
3071 if (Args[I]->isValueDependent())
3072 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003073 if (Args[I]->isInstantiationDependent())
3074 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003075 if (Args[I]->containsUnexpandedParameterPack())
3076 ExprBits.ContainsUnexpandedParameterPack = true;
3077
3078 MyArgs[I] = Args[I];
3079 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003080
Benjamin Kramer19562c92012-02-20 00:20:48 +00003081 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003082 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003083 if (SelLocsK == SelLoc_NonStandard)
3084 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3085 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003086}
3087
Douglas Gregor04badcf2010-04-21 00:45:42 +00003088ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003089 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003090 SourceLocation LBracLoc,
3091 SourceLocation SuperLoc,
3092 bool IsInstanceSuper,
3093 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003094 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003095 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003096 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003097 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003098 SourceLocation RBracLoc,
3099 bool isImplicit) {
3100 assert((!SelLocs.empty() || isImplicit) &&
3101 "No selector locs for non-implicit message");
3102 ObjCMessageExpr *Mem;
3103 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3104 if (isImplicit)
3105 Mem = alloc(Context, Args.size(), 0);
3106 else
3107 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003108 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003109 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003110 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003111}
3112
3113ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003114 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003115 SourceLocation LBracLoc,
3116 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003117 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003118 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003119 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003120 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003121 SourceLocation RBracLoc,
3122 bool isImplicit) {
3123 assert((!SelLocs.empty() || isImplicit) &&
3124 "No selector locs for non-implicit message");
3125 ObjCMessageExpr *Mem;
3126 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3127 if (isImplicit)
3128 Mem = alloc(Context, Args.size(), 0);
3129 else
3130 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003131 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003132 SelLocs, SelLocsK, Method, Args, RBracLoc,
3133 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003134}
3135
3136ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003137 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003138 SourceLocation LBracLoc,
3139 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003140 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003141 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003142 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003143 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003144 SourceLocation RBracLoc,
3145 bool isImplicit) {
3146 assert((!SelLocs.empty() || isImplicit) &&
3147 "No selector locs for non-implicit message");
3148 ObjCMessageExpr *Mem;
3149 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3150 if (isImplicit)
3151 Mem = alloc(Context, Args.size(), 0);
3152 else
3153 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003154 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003155 SelLocs, SelLocsK, Method, Args, RBracLoc,
3156 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003157}
3158
Sean Huntc3021132010-05-05 15:23:54 +00003159ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003160 unsigned NumArgs,
3161 unsigned NumStoredSelLocs) {
3162 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003163 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3164}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003165
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003166ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3167 ArrayRef<Expr *> Args,
3168 SourceLocation RBraceLoc,
3169 ArrayRef<SourceLocation> SelLocs,
3170 Selector Sel,
3171 SelectorLocationsKind &SelLocsK) {
3172 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3173 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3174 : 0;
3175 return alloc(C, Args.size(), NumStoredSelLocs);
3176}
3177
3178ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3179 unsigned NumArgs,
3180 unsigned NumStoredSelLocs) {
3181 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3182 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3183 return (ObjCMessageExpr *)C.Allocate(Size,
3184 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3185}
3186
3187void ObjCMessageExpr::getSelectorLocs(
3188 SmallVectorImpl<SourceLocation> &SelLocs) const {
3189 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3190 SelLocs.push_back(getSelectorLoc(i));
3191}
3192
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003193SourceRange ObjCMessageExpr::getReceiverRange() const {
3194 switch (getReceiverKind()) {
3195 case Instance:
3196 return getInstanceReceiver()->getSourceRange();
3197
3198 case Class:
3199 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3200
3201 case SuperInstance:
3202 case SuperClass:
3203 return getSuperLoc();
3204 }
3205
David Blaikie30263482012-01-20 21:50:17 +00003206 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003207}
3208
Douglas Gregor04badcf2010-04-21 00:45:42 +00003209Selector ObjCMessageExpr::getSelector() const {
3210 if (HasMethod)
3211 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3212 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003213 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003214}
3215
3216ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3217 switch (getReceiverKind()) {
3218 case Instance:
3219 if (const ObjCObjectPointerType *Ptr
3220 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3221 return Ptr->getInterfaceDecl();
3222 break;
3223
3224 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003225 if (const ObjCObjectType *Ty
3226 = getClassReceiver()->getAs<ObjCObjectType>())
3227 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003228 break;
3229
3230 case SuperInstance:
3231 if (const ObjCObjectPointerType *Ptr
3232 = getSuperType()->getAs<ObjCObjectPointerType>())
3233 return Ptr->getInterfaceDecl();
3234 break;
3235
3236 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003237 if (const ObjCObjectType *Iface
3238 = getSuperType()->getAs<ObjCObjectType>())
3239 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003240 break;
3241 }
3242
3243 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003244}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003245
Chris Lattner5f9e2722011-07-23 10:55:15 +00003246StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003247 switch (getBridgeKind()) {
3248 case OBC_Bridge:
3249 return "__bridge";
3250 case OBC_BridgeTransfer:
3251 return "__bridge_transfer";
3252 case OBC_BridgeRetained:
3253 return "__bridge_retained";
3254 }
David Blaikie30263482012-01-20 21:50:17 +00003255
3256 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003257}
3258
Jay Foad4ba2a172011-01-12 09:06:06 +00003259bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003260 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003261}
3262
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003263ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3264 QualType Type, SourceLocation BLoc,
3265 SourceLocation RP)
3266 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3267 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003268 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003269 Type->containsUnexpandedParameterPack()),
3270 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3271{
3272 SubExprs = new (C) Stmt*[nexpr];
3273 for (unsigned i = 0; i < nexpr; i++) {
3274 if (args[i]->isTypeDependent())
3275 ExprBits.TypeDependent = true;
3276 if (args[i]->isValueDependent())
3277 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003278 if (args[i]->isInstantiationDependent())
3279 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003280 if (args[i]->containsUnexpandedParameterPack())
3281 ExprBits.ContainsUnexpandedParameterPack = true;
3282
3283 SubExprs[i] = args[i];
3284 }
3285}
3286
Nate Begeman888376a2009-08-12 02:28:50 +00003287void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3288 unsigned NumExprs) {
3289 if (SubExprs) C.Deallocate(SubExprs);
3290
3291 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003292 this->NumExprs = NumExprs;
3293 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003294}
Nate Begeman888376a2009-08-12 02:28:50 +00003295
Peter Collingbournef111d932011-04-15 00:35:48 +00003296GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3297 SourceLocation GenericLoc, Expr *ControllingExpr,
3298 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3299 unsigned NumAssocs, SourceLocation DefaultLoc,
3300 SourceLocation RParenLoc,
3301 bool ContainsUnexpandedParameterPack,
3302 unsigned ResultIndex)
3303 : Expr(GenericSelectionExprClass,
3304 AssocExprs[ResultIndex]->getType(),
3305 AssocExprs[ResultIndex]->getValueKind(),
3306 AssocExprs[ResultIndex]->getObjectKind(),
3307 AssocExprs[ResultIndex]->isTypeDependent(),
3308 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003309 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003310 ContainsUnexpandedParameterPack),
3311 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3312 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3313 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3314 RParenLoc(RParenLoc) {
3315 SubExprs[CONTROLLING] = ControllingExpr;
3316 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3317 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3318}
3319
3320GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3321 SourceLocation GenericLoc, Expr *ControllingExpr,
3322 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3323 unsigned NumAssocs, SourceLocation DefaultLoc,
3324 SourceLocation RParenLoc,
3325 bool ContainsUnexpandedParameterPack)
3326 : Expr(GenericSelectionExprClass,
3327 Context.DependentTy,
3328 VK_RValue,
3329 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003330 /*isTypeDependent=*/true,
3331 /*isValueDependent=*/true,
3332 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003333 ContainsUnexpandedParameterPack),
3334 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3335 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3336 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3337 RParenLoc(RParenLoc) {
3338 SubExprs[CONTROLLING] = ControllingExpr;
3339 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3340 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3341}
3342
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003343//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003344// DesignatedInitExpr
3345//===----------------------------------------------------------------------===//
3346
Chandler Carruthb1138242011-06-16 06:47:06 +00003347IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003348 assert(Kind == FieldDesignator && "Only valid on a field designator");
3349 if (Field.NameOrField & 0x01)
3350 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3351 else
3352 return getField()->getIdentifier();
3353}
3354
Sean Huntc3021132010-05-05 15:23:54 +00003355DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003356 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003357 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003358 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003359 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003360 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003361 unsigned NumIndexExprs,
3362 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003363 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003364 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003365 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003366 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003367 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003368 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3369 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003370 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003371
3372 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003373 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003374 *Child++ = Init;
3375
3376 // Copy the designators and their subexpressions, computing
3377 // value-dependence along the way.
3378 unsigned IndexIdx = 0;
3379 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003380 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003381
3382 if (this->Designators[I].isArrayDesignator()) {
3383 // Compute type- and value-dependence.
3384 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003385 if (Index->isTypeDependent() || Index->isValueDependent())
3386 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003387 if (Index->isInstantiationDependent())
3388 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003389 // Propagate unexpanded parameter packs.
3390 if (Index->containsUnexpandedParameterPack())
3391 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003392
3393 // Copy the index expressions into permanent storage.
3394 *Child++ = IndexExprs[IndexIdx++];
3395 } else if (this->Designators[I].isArrayRangeDesignator()) {
3396 // Compute type- and value-dependence.
3397 Expr *Start = IndexExprs[IndexIdx];
3398 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003399 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003400 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003401 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003402 ExprBits.InstantiationDependent = true;
3403 } else if (Start->isInstantiationDependent() ||
3404 End->isInstantiationDependent()) {
3405 ExprBits.InstantiationDependent = true;
3406 }
3407
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003408 // Propagate unexpanded parameter packs.
3409 if (Start->containsUnexpandedParameterPack() ||
3410 End->containsUnexpandedParameterPack())
3411 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003412
3413 // Copy the start/end expressions into permanent storage.
3414 *Child++ = IndexExprs[IndexIdx++];
3415 *Child++ = IndexExprs[IndexIdx++];
3416 }
3417 }
3418
3419 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003420}
3421
Douglas Gregor05c13a32009-01-22 00:58:24 +00003422DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003423DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003424 unsigned NumDesignators,
3425 Expr **IndexExprs, unsigned NumIndexExprs,
3426 SourceLocation ColonOrEqualLoc,
3427 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003428 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003429 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003430 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003431 ColonOrEqualLoc, UsesColonSyntax,
3432 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003433}
3434
Mike Stump1eb44332009-09-09 15:08:12 +00003435DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003436 unsigned NumIndexExprs) {
3437 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3438 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3439 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3440}
3441
Douglas Gregor319d57f2010-01-06 23:17:19 +00003442void DesignatedInitExpr::setDesignators(ASTContext &C,
3443 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003444 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003445 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003446 NumDesignators = NumDesigs;
3447 for (unsigned I = 0; I != NumDesigs; ++I)
3448 Designators[I] = Desigs[I];
3449}
3450
Abramo Bagnara24f46742011-03-16 15:08:46 +00003451SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3452 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3453 if (size() == 1)
3454 return DIE->getDesignator(0)->getSourceRange();
3455 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3456 DIE->getDesignator(size()-1)->getEndLocation());
3457}
3458
Douglas Gregor05c13a32009-01-22 00:58:24 +00003459SourceRange DesignatedInitExpr::getSourceRange() const {
3460 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003461 Designator &First =
3462 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003463 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003464 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003465 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3466 else
3467 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3468 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003469 StartLoc =
3470 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003471 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3472}
3473
Douglas Gregor05c13a32009-01-22 00:58:24 +00003474Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3475 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3476 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3477 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003478 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3479 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3480}
3481
3482Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003483 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003484 "Requires array range designator");
3485 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3486 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003487 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3488 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3489}
3490
3491Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003492 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003493 "Requires array range designator");
3494 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3495 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003496 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3497 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3498}
3499
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003500/// \brief Replaces the designator at index @p Idx with the series
3501/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003502void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003503 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003504 const Designator *Last) {
3505 unsigned NumNewDesignators = Last - First;
3506 if (NumNewDesignators == 0) {
3507 std::copy_backward(Designators + Idx + 1,
3508 Designators + NumDesignators,
3509 Designators + Idx);
3510 --NumNewDesignators;
3511 return;
3512 } else if (NumNewDesignators == 1) {
3513 Designators[Idx] = *First;
3514 return;
3515 }
3516
Mike Stump1eb44332009-09-09 15:08:12 +00003517 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003518 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003519 std::copy(Designators, Designators + Idx, NewDesignators);
3520 std::copy(First, Last, NewDesignators + Idx);
3521 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3522 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003523 Designators = NewDesignators;
3524 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3525}
3526
Mike Stump1eb44332009-09-09 15:08:12 +00003527ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003528 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003529 SourceLocation rparenloc)
3530 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003531 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003532 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003533 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003534 for (unsigned i = 0; i != nexprs; ++i) {
3535 if (exprs[i]->isTypeDependent())
3536 ExprBits.TypeDependent = true;
3537 if (exprs[i]->isValueDependent())
3538 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003539 if (exprs[i]->isInstantiationDependent())
3540 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003541 if (exprs[i]->containsUnexpandedParameterPack())
3542 ExprBits.ContainsUnexpandedParameterPack = true;
3543
Nate Begeman2ef13e52009-08-10 23:49:36 +00003544 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003545 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003546}
3547
John McCalle996ffd2011-02-16 08:02:54 +00003548const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3549 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3550 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003551 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3552 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003553 e = cast<CXXConstructExpr>(e)->getArg(0);
3554 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3555 e = ice->getSubExpr();
3556 return cast<OpaqueValueExpr>(e);
3557}
3558
John McCall4b9c2d22011-11-06 09:01:30 +00003559PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3560 unsigned numSemanticExprs) {
3561 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3562 (1 + numSemanticExprs) * sizeof(Expr*),
3563 llvm::alignOf<PseudoObjectExpr>());
3564 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3565}
3566
3567PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3568 : Expr(PseudoObjectExprClass, shell) {
3569 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3570}
3571
3572PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3573 ArrayRef<Expr*> semantics,
3574 unsigned resultIndex) {
3575 assert(syntax && "no syntactic expression!");
3576 assert(semantics.size() && "no semantic expressions!");
3577
3578 QualType type;
3579 ExprValueKind VK;
3580 if (resultIndex == NoResult) {
3581 type = C.VoidTy;
3582 VK = VK_RValue;
3583 } else {
3584 assert(resultIndex < semantics.size());
3585 type = semantics[resultIndex]->getType();
3586 VK = semantics[resultIndex]->getValueKind();
3587 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3588 }
3589
3590 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3591 (1 + semantics.size()) * sizeof(Expr*),
3592 llvm::alignOf<PseudoObjectExpr>());
3593 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3594 resultIndex);
3595}
3596
3597PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3598 Expr *syntax, ArrayRef<Expr*> semantics,
3599 unsigned resultIndex)
3600 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3601 /*filled in at end of ctor*/ false, false, false, false) {
3602 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3603 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3604
3605 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3606 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3607 getSubExprsBuffer()[i] = E;
3608
3609 if (E->isTypeDependent())
3610 ExprBits.TypeDependent = true;
3611 if (E->isValueDependent())
3612 ExprBits.ValueDependent = true;
3613 if (E->isInstantiationDependent())
3614 ExprBits.InstantiationDependent = true;
3615 if (E->containsUnexpandedParameterPack())
3616 ExprBits.ContainsUnexpandedParameterPack = true;
3617
3618 if (isa<OpaqueValueExpr>(E))
3619 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3620 "opaque-value semantic expressions for pseudo-object "
3621 "operations must have sources");
3622 }
3623}
3624
Douglas Gregor05c13a32009-01-22 00:58:24 +00003625//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003626// ExprIterator.
3627//===----------------------------------------------------------------------===//
3628
3629Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3630Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3631Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3632const Expr* ConstExprIterator::operator[](size_t idx) const {
3633 return cast<Expr>(I[idx]);
3634}
3635const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3636const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3637
3638//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003639// Child Iterators for iterating over subexpressions/substatements
3640//===----------------------------------------------------------------------===//
3641
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003642// UnaryExprOrTypeTraitExpr
3643Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003644 // If this is of a type and the type is a VLA type (and not a typedef), the
3645 // size expression of the VLA needs to be treated as an executable expression.
3646 // Why isn't this weirdness documented better in StmtIterator?
3647 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003648 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003649 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003650 return child_range(child_iterator(T), child_iterator());
3651 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003652 }
John McCall63c00d72011-02-09 08:16:59 +00003653 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003654}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003655
Steve Naroff563477d2007-09-18 23:55:05 +00003656// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003657Stmt::child_range ObjCMessageExpr::children() {
3658 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003659 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003660 begin = reinterpret_cast<Stmt **>(this + 1);
3661 else
3662 begin = reinterpret_cast<Stmt **>(getArgs());
3663 return child_range(begin,
3664 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003665}
3666
Steve Naroff4eb206b2008-09-03 18:15:37 +00003667// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003668BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003669 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003670 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003671 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003672 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003673 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003674{
Douglas Gregord967e312011-01-19 21:52:31 +00003675 bool TypeDependent = false;
3676 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003677 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00003678 computeDeclRefDependence(D->getASTContext(), D, getType(), TypeDependent,
3679 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003680 ExprBits.TypeDependent = TypeDependent;
3681 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003682 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003683}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003684
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003685ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3686 QualType T, ObjCMethodDecl *Method,
3687 SourceRange SR)
3688 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3689 false, false, false, false),
3690 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3691{
3692 Expr **SaveElements = getElements();
3693 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3694 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3695 ExprBits.ValueDependent = true;
3696 if (Elements[I]->isInstantiationDependent())
3697 ExprBits.InstantiationDependent = true;
3698 if (Elements[I]->containsUnexpandedParameterPack())
3699 ExprBits.ContainsUnexpandedParameterPack = true;
3700
3701 SaveElements[I] = Elements[I];
3702 }
3703}
3704
3705ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3706 llvm::ArrayRef<Expr *> Elements,
3707 QualType T, ObjCMethodDecl * Method,
3708 SourceRange SR) {
3709 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3710 + Elements.size() * sizeof(Expr *));
3711 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3712}
3713
3714ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3715 unsigned NumElements) {
3716
3717 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3718 + NumElements * sizeof(Expr *));
3719 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3720}
3721
3722ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3723 ArrayRef<ObjCDictionaryElement> VK,
3724 bool HasPackExpansions,
3725 QualType T, ObjCMethodDecl *method,
3726 SourceRange SR)
3727 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3728 false, false),
3729 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3730 DictWithObjectsMethod(method)
3731{
3732 KeyValuePair *KeyValues = getKeyValues();
3733 ExpansionData *Expansions = getExpansionData();
3734 for (unsigned I = 0; I < NumElements; I++) {
3735 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3736 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3737 ExprBits.ValueDependent = true;
3738 if (VK[I].Key->isInstantiationDependent() ||
3739 VK[I].Value->isInstantiationDependent())
3740 ExprBits.InstantiationDependent = true;
3741 if (VK[I].EllipsisLoc.isInvalid() &&
3742 (VK[I].Key->containsUnexpandedParameterPack() ||
3743 VK[I].Value->containsUnexpandedParameterPack()))
3744 ExprBits.ContainsUnexpandedParameterPack = true;
3745
3746 KeyValues[I].Key = VK[I].Key;
3747 KeyValues[I].Value = VK[I].Value;
3748 if (Expansions) {
3749 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3750 if (VK[I].NumExpansions)
3751 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3752 else
3753 Expansions[I].NumExpansionsPlusOne = 0;
3754 }
3755 }
3756}
3757
3758ObjCDictionaryLiteral *
3759ObjCDictionaryLiteral::Create(ASTContext &C,
3760 ArrayRef<ObjCDictionaryElement> VK,
3761 bool HasPackExpansions,
3762 QualType T, ObjCMethodDecl *method,
3763 SourceRange SR) {
3764 unsigned ExpansionsSize = 0;
3765 if (HasPackExpansions)
3766 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3767
3768 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3769 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3770 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3771}
3772
3773ObjCDictionaryLiteral *
3774ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3775 bool HasPackExpansions) {
3776 unsigned ExpansionsSize = 0;
3777 if (HasPackExpansions)
3778 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3779 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3780 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3781 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3782 HasPackExpansions);
3783}
3784
3785ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3786 Expr *base,
3787 Expr *key, QualType T,
3788 ObjCMethodDecl *getMethod,
3789 ObjCMethodDecl *setMethod,
3790 SourceLocation RB) {
3791 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3792 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3793 OK_ObjCSubscript,
3794 getMethod, setMethod, RB);
3795}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003796
3797AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3798 QualType t, AtomicOp op, SourceLocation RP)
3799 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3800 false, false, false, false),
3801 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3802{
3803 for (unsigned i = 0; i < nexpr; i++) {
3804 if (args[i]->isTypeDependent())
3805 ExprBits.TypeDependent = true;
3806 if (args[i]->isValueDependent())
3807 ExprBits.ValueDependent = true;
3808 if (args[i]->isInstantiationDependent())
3809 ExprBits.InstantiationDependent = true;
3810 if (args[i]->containsUnexpandedParameterPack())
3811 ExprBits.ContainsUnexpandedParameterPack = true;
3812
3813 SubExprs[i] = args[i];
3814 }
3815}